为了弄清楚如何创建以多边形为SCNGeometry的primitiveType,我的目标是将多边形形状的节点添加为球体节点的子节点,并使其看起来像地图工具包如本例所示的MKPolygon。

我目前的代码是:
//Take an arbitrary array of vectors
let vertices: [SCNVector3] = [
SCNVector3Make(-0.1304485, 0.551937, 0.8236193),
SCNVector3Make(0.01393811, 0.601815, 0.7985139),
SCNVector3Make(0.2971005, 0.5591929, 0.7739732),
SCNVector3Make(0.4516893, 0.5150381, 0.7285002),
SCNVector3Make(0.4629132, 0.4383712, 0.7704169),
SCNVector3Make(0.1333823, 0.5224985, 0.8421428),
SCNVector3Make(-0.1684743, 0.4694716, 0.8667254)]
//Does polygon shape require indices?
let indices: [Int] = [0,1,2,3,4,5,6]
let vertexSource = SCNGeometrySource(vertices: vertices)
let indexData = Data(bytes: indices, count: indices.count * MemoryLayout<Int>.size)
//Note!!! I get compiler error if primitiveCount is greater than 0
let element = SCNGeometryElement(data: indexData, primitiveType: .polygon, primitiveCount: 0, bytesPerIndex: MemoryLayout<Int>.size)
let geometry = SCNGeometry(sources: [vertexSource], elements: [element])
let material = SCNMaterial()
material.diffuse.contents = UIColor.purple.withAlphaComponent(0.75)
material.isDoubleSided = true
geometry.firstMaterial = material
let node = SCNNode(geometry: geometry)像这样使用SCNGeometryElement时,我会得到一个空节点。
发布于 2017-07-05 10:30:26
你有两个问题:
[Int32]。SCNGeometryPrimitiveTypePolygon的文档(只存在于Objective中):元素的数据属性包含两个值序列。
需要将索引数组更改为:
let indices: [Int32] = [7, /* We have a polygon with seven points */,
0,1,2,3,4,5,6 /* The seven indices for our polygon */
]然后,将primitiveCount设置为1(我们有一个要绘制的多边形),并更改缓冲区的大小:
let indexData = Data(bytes: indices,
count: indices.count * MemoryLayout<Int32>.size)
// Now without runtime error
let element = SCNGeometryElement(data: indexData,
primitiveType: .polygon,
primitiveCount: 1,
bytesPerIndex: MemoryLayout<Int32>.size)https://stackoverflow.com/questions/44922164
复制相似问题