如何在SceneKit中绘制成千上万行?

Lin*_* He 1 graphics ios scenekit swift

我打算画很多线SceneKit.我搜索了如何绘制线条并找到了答案

它适用于我,除了它不适合绘制大量的线条.当我绘制数万行时,占用的RAM将是可怕的(n Gb).

我想知道是否有办法可以有效地绘制大量的线条.我只需要3D线,其从 - 到协调和长度可能不同.

Ef *_*Dot 5

在您的参考答案中描述的方法是正确的,您只是没有SCNGeometrySource/SCNGeometryElement/SCNNode为每一行创建,只需填充数组:

SCNVector3 positions[] = {
    SCNVector3Make(0.0, 0.0, 0.0),    // line1 begin  [0]
    SCNVector3Make(10.0, 10.0, 10.0), // line1 end    [1]
    SCNVector3Make(5.0, 10.0, 10.0),  // line2 begin  [2]
    SCNVector3Make(10.0, 5.0, 10.0)   // line2 end    [3]
};

int indices[] = {0, 1, 2, 3};
              // ^^^^  ^^^^
              // 1st   2nd
              // line  line
Run Code Online (Sandbox Code Playgroud)

然后NSData使用步幅创建几何源:

NSData *data = [NSData dataWithBytes:positions length:sizeof(positions)];

SCNGeometrySource *vertexSource = [SCNGeometrySource geometrySourceWithData:data
                                            semantic:SCNGeometrySourceSemanticVertex
                                         vectorCount:POSITION_COUNT
                                     floatComponents:YES
                                 componentsPerVector:3 // x, y, z
                                   bytesPerComponent:sizeof(CGFloat) // size of x/y/z/ component of SCNVector3
                                          dataOffset:0 
                                          dataStride:sizeof(SCNVector3)*2]; // offset in buffer to the next line positions
Run Code Online (Sandbox Code Playgroud)

如果你有10000行,那么你的位置缓冲区将是2*3*10000*8 = 480KB,索引2*10000*4 = 80KB,这对GPU来说真的不多.

如果可以减少索引和/或位置的长度,甚至可以进一步减少缓冲区.例如,如果所有坐标都是-127..128范围内的整数,那么传递floatComponent:NO, bytesPerComponent:1会将位置缓冲区减少到60KB).

当然,如果所有线都具有相同的材料属性,这是适用的.否则,您必须对具有相同属性的所有行进行分组SCNGeometrySource/SCNGeometryElement/SCNNode.

  • 如http://stackoverflow.com/questions/41037428/scenekit-scngeometryelement-trianglestrip-vs-triangles中所述,您可以跳过`NSData`转换并使用`SCNGeometryElement(indices:primitiveType:)`来生成索引缓冲区. (3认同)