Ios 如何在SceneKit中绘制数万条线?
我将在Ios 如何在SceneKit中绘制数万条线?,ios,swift,graphics,scenekit,Ios,Swift,Graphics,Scenekit,我将在SceneKit中画很多线。我搜索了如何画线,发现 它对我来说很好,只是它不适合画大量的线。当我画数万行时,占用的RAM将是可怕的(n Gb) 我想知道是否有一种方法可以有效地画出大量的线。我所需要的只是3D线条,其从到坐标和长度可能不同。中描述的方法是正确的,您只是没有为每条线条创建SCNGeometrySource/SCNGeometryElement/SCNNode,只需填充数组即可: SCNVector3 positions[] = { SCNVector3Make(0.0
SceneKit
中画很多线。我搜索了如何画线,发现
它对我来说很好,只是它不适合画大量的线。当我画数万行时,占用的RAM将是可怕的(n Gb)
我想知道是否有一种方法可以有效地画出大量的线。我所需要的只是3D线条,其从到坐标和长度可能不同。中描述的方法是正确的,您只是没有为每条线条创建
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
然后使用跨步从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
中对具有相同属性的所有行进行分组。中描述的方法是正确的,您只是没有为每一行创建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
然后使用跨步从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
中对具有相同属性的所有行进行分组,如中所述,您可以跳过NSData
转换,并使用SCNGeometryElement(索引:原语类型:)
生成索引缓冲区。这正是我要找的!非常感谢。对于其他需要解决类似问题的人来说,在一个几何体中画太多线(如300线)会使你的应用程序崩溃。我不知道原因,但它确实发生了。如中所述,您可以跳过NSData
转换,使用SCNGeometryElement(索引:primitiveType:)
生成索引缓冲区。这正是我要找的!非常感谢。对于其他需要解决类似问题的人来说,在一个几何体中画太多线(如300线)会使你的应用程序崩溃。我不知道原因,但它确实发生了。