【发布时间】:2015-08-09 19:49:20
【问题描述】:
我有一个包含 SCNVector3 顶点的数组来描述轨迹路径。我想画出它的路径。
- (SCNNode *)lineNodeFromVertices:(SCNVector3 *)vertices count:(NSUInteger)count
{
NSInteger numberOfVertices = (count - 1) * 2;
int *verticesSequence = calloc(numberOfVertices, sizeof(int));
for (int index = 0; index < count; index ++)
{
if (index > 0 && index < count - 1)
{
verticesSequence[index * 2 - 1] = index;
verticesSequence[index * 2] = index;
}
else if (index == 0)
{
verticesSequence[0] = index;
}
else if (index == count - 1)
{
verticesSequence[index * 2 - 1] = index;
}
}
NSData *sequenceData = [NSData dataWithBytes:verticesSequence
length:sizeof(verticesSequence)];
free(verticesSequence);
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:sequenceData primitiveType:SCNGeometryPrimitiveTypeLine
primitiveCount:(count - 1)
bytesPerIndex:sizeof(int)];
SCNGeometrySource *source = [SCNGeometrySource geometrySourceWithVertices:vertices
count:count];
SCNGeometry *line = [SCNGeometry geometryWithSources:@[source]
elements:@[element]];
return [SCNNode nodeWithGeometry:line];
}
当我将动态创建的数组作为创建 SCNGeometryElement 的参数传递时,它不起作用。
但是当我传递具有预定义元素数量的数组时,它可以正常工作。 比如:
// Sample data
SCNVector3 vertices[] =
{
SCNVector3Make(1.0, 1.5, 0.5),
SCNVector3Make(0.8, 1.0, 0.2),
SCNVector3Make(0.4, 2.0, 1.2),
SCNVector3Make(0.0, 2.5, 2.7),
SCNVector3Make(-0.2, 2.0, 4.0),
SCNVector3Make(-0.4, 0.5, 5.0),
SCNVector3Make(-0.3, 3.0, 3.0)
};
int count = sizeof(vertices) / sizeof(vertices[0]);
// Array with predefined vertices sequence
int verticesSequence[] =
{
0, 1,
1, 2,
2, 3,
3, 4,
4, 5,
5, 6
};
NSData *sequenceData = [NSData dataWithBytes:verticesSequence
length:sizeof(verticesSequence)];
SCNGeometryElement *element = [SCNGeometryElement geometryElementWithData:sequenceData primitiveType:SCNGeometryPrimitiveTypeLine
primitiveCount:(count - 1)
bytesPerIndex:sizeof(int)];
SCNGeometrySource *source = [SCNGeometrySource geometrySourceWithVertices:vertices
count:count];
SCNGeometry *line = [SCNGeometry geometryWithSources:@[source]
elements:@[element]];
[self.rootSceneNode addChildNode:[SCNNode nodeWithGeometry:line]];
是否存在其他方法来创建具有非预定义顶点数的线节点?
【问题讨论】:
-
我认为你需要展示一个 small 一个有效的示例数据和一个无效的示例数据。
-
@DavidRönnqvist 我添加了更多信息。代码示例的第一部分不起作用,第二部分工作正常。
标签: ios objective-c scenekit