美文网首页
SCNGeometrySource

SCNGeometrySource

作者: AntKing | 来源:发表于2020-08-24 20:46 被阅读0次

顶点数据的容器,它构成三维对象或几何图形定义的一部分。

class SCNGeometrySource : NSObject

因为大多数几何图形使用不止一个几何图形源,并且GPU通常将来自多个数据源的数据一起使用,所以您可以通过在同一数组中交织多个语义的顶点数据来实现自定义几何图形的更好渲染性能。

为此,首先创建一个数组,其中每个元素都包含同一顶点的多种语义的值。 接下来,从该数组创建一个NSData对象,并使用offset和stride参数从该数据创建每个几何源,以指定可以在数组中找到每种语义的值的位置。 为了更方便地指定向量的大小和位置,可以为顶点定义自己的数据结构,并使用sizeof函数(在Objective-C中为offsetof),如清单1所示。

typedef struct {
    float x, y, z;    // position
    float nx, ny, nz; // normal
    float s, t;       // texture coordinates
} MyVertex;
 
MyVertex vertices[VERTEX_COUNT] = { /* ... vertex data ... */ };
NSData *data = [NSData dataWithBytes:vertices length:sizeof(vertices)];
SCNGeometrySource *vertexSource, *normalSource, *tcoordSource;
 
vertexSource = [SCNGeometrySource geometrySourceWithData:data
                                                semantic:SCNGeometrySourceSemanticVertex
                                             vectorCount:VERTEX_COUNT
                                         floatComponents:YES
                                     componentsPerVector:3 // x, y, z
                                       bytesPerComponent:sizeof(float)
                                              dataOffset:offsetof(MyVertex, x)
                                              dataStride:sizeof(MyVertex)];
 
normalSource = [SCNGeometrySource geometrySourceWithData:data
                                                semantic:SCNGeometrySourceSemanticNormal
                                             vectorCount:VERTEX_COUNT
                                         floatComponents:YES
                                     componentsPerVector:3 // nx, ny, nz
                                       bytesPerComponent:sizeof(float)
                                              dataOffset:offsetof(MyVertex, nx)
                                              dataStride:sizeof(MyVertex)];
 
tcoordSource = [SCNGeometrySource geometrySourceWithData:data
                                                semantic:SCNGeometrySourceSemanticTexcoord
                                             vectorCount:VERTEX_COUNT
                                         floatComponents:YES
                                     componentsPerVector:2 // s, t
                                       bytesPerComponent:sizeof(float)
                                              dataOffset:offsetof(MyVertex, s)
                                              dataStride:sizeof(MyVertex)];

上面的这个类方法,其实就是下面的这个init的初始化方法

convenience init(data: Data, semantic: SCNGeometrySource.Semantic, vectorCount: Int, usesFloatComponents floatComponents: Bool, componentsPerVector: Int, bytesPerComponent: Int, dataOffset offset: Int, dataStride stride: Int)

下面三个初始化方法是上面的一个方法的拆解

image.png
var data: Data { get }
The data for the geometry source.
几何源的数据

var semantic: SCNGeometrySource.Semantic { get }
几何源为每个顶点描述的语义值(或属性)。

var vectorCount: Int { get }
The number of vectors in the data.
var usesFloatComponents: Bool { get }
一个布尔值,指示矢量分量是否为浮点值。
var componentsPerVector: Int { get }
每个向量中的标量分量数。
var bytesPerComponent: Int { get }
每个向量分量的大小(以字节为单位)。
var dataOffset: Int { get }
从数据开头到要在几何图形源中使用的第一个矢量分量的偏移量(以字节为单位)。
var dataStride: Int { get }
从向量到数据中的下一个字节的字节数。

额外的一个初始化方法

image.png

相关文章

  • SCNGeometrySource

    顶点数据的容器,它构成三维对象或几何图形定义的一部分。 因为大多数几何图形使用不止一个几何图形源,并且GPU通常将...

网友评论

      本文标题:SCNGeometrySource

      本文链接:https://www.haomeiwen.com/subject/kqkojktx.html