我正在尝试创建一个自定义的SCNPlane类,它可以是任何多边形的形式,因为常规的SCNPlane只能是一个矩形。
我试图通过扩展SCNGeometry来实现这一点。下面是我使用的代码:
extension SCNGeometry {
static func polygonPlane(vertices: [SCNVector3]) -> SCNGeometry {
let src = SCNGeometrySource(vertices: vertices)
var indices: [UInt32] = []
var index: UInt32 = 0
for _ in vertices {
indices.append(index)
index += 1
}
let inds = SCNGeometryElement(indices: indices, primitiveType: .polygon)
return SCNGeometry(sources: [src], elements: [inds])
}
}
正如您所看到的,该方法接受一个顶点数组作为输入,我的愿望是将这些顶点连接起来,以便使用提供的顶点生成一个具有指定几何形状的平面。
在上面的代码中使用SCNGeometryPrimitiveType.polygon时,我得到了一个奇怪的错误:“Thread1: EXC_BREAKPOINT (code=1,subcode=0x106362e20)”。似乎这种情况下的功能并没有在swift中实现,但它是为Objective-C实现的?有什么方法可以让我利用这个吗?
我还尝试使用primitiveType-value的其他选项,使用这些值没有崩溃,这进一步强化了我的信念,即.polygon的情况有问题。
发布于 2019-03-25 02:17:40
使用多边形时,必须使用
init(data: Data?, primitiveType: SCNGeometryPrimitiveType, primitiveCount: Int, bytesPerIndex: Int)
初始化SCNGeometryElement。有关详细信息,请参阅苹果文档。
https://stackoverflow.com/questions/55325825
复制相似问题