我正在尝试以具有自定义形状的平面的形式创建自定义SCNGeometry,可以将其放置在ARKit会话中。我在以下方法中使用选项SCNGeometryPrimitiveTypePolygon,它似乎可以正常工作:
extension SCNGeometry {
class func polygonfrom(vectices: [SCNVector3]) -> SCNGeometry {
let indices: [Int32] = getIndices(count: vectices.count)
let indexData = Data(bytes: indices, count: indices.count * MemoryLayout<Int32>.size)
let source = SCNGeometrySource(vertices: vectices)
let element = SCNGeometryElement(data: indexData, primitiveType: .polygon, primitiveCount: 1, bytesPerIndex: MemoryLayout<Int32>.size)
return SCNGeometry(sources: [source], elements: [element])
}
class private func getIndices(count: Int) -> [Int32] {
var indices: [Int32] = []
indices.append(Int32(count))
for i in 0..<count{
indices.append(Int32(i))
}
return indices
}
}
不幸的是,它不适用于凹面多边形:
SCNShape
类代表您创建一个合适的三角剖分。