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

时间:2016-12-12 10:44:38

标签: ios swift graphics scenekit

我将在SceneKit中画出很多行。我搜索了如何画线并找到了this answer

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

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

1 个答案:

答案 0 :(得分:5)

referenceed answer中描述的方法是正确的,您只是没有为每一行创建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

然后使用stride:

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

如果您有10000行,那么您的头寸缓冲区将是2 * 3 * 10000 * 8 = 480KB ,而指数2 * 10000 * 4 = 80KB ,其中GPU真的不多。

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

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

相关问题