在您的参考答案中描述的方法是正确的,您只是没有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.