我正在尝试使用场景工具(Scene Kit)复制这个立方体图像形状(在获得原始创作者的许可后)。
到目前为止,我已经有了线条和顶点的绘图代码。 我不能使用图像,因为背景必须是透明的。
我现在要解决的问题是如何编辑 SCNGeometryPrimitiveType.Line
元素的笔画宽度。
我创建线条的基本方法如下:
private func squareVertices(length: Float) -> [SCNVector3] {
let m = length/Float(2)
let topLeft = SCNVector3Make(-m-q, m+q, m+q)
let topRight = SCNVector3Make( m+q, m+q, m+q)
let bottomLeft = SCNVector3Make(-m-q, -m-q, m+q)
let bottomRight = SCNVector3Make( m+q, -m-q, m+q)
return [topLeft, topRight, bottomLeft, bottomRight]
}
private func cubeFace() -> SCNGeometry {
let vertices : [SCNVector3] = squareVertices(l)
let geoSrc = SCNGeometrySource(vertices: UnsafePointer<SCNVector3>(vertices), count: vertices.count)
// index buffer
let idx1 : [Int32] = [0, 3]
let data1 = NSData(bytes: idx1, length: (sizeof(Int32) * idx1.count))
let geoElements1 = SCNGeometryElement(data: data1, primitiveType: SCNGeometryPrimitiveType.Line, primitiveCount: idx1.count, bytesPerIndex: sizeof(Int32))
let idx2 : [Int32] = [1, 2]
let data2 = NSData(bytes: idx2, length: (sizeof(Int32) * idx2.count))
let geoElements2 = SCNGeometryElement(data: data2, primitiveType: SCNGeometryPrimitiveType.Line, primitiveCount: idx2.count, bytesPerIndex: sizeof(Int32))
let geo = SCNGeometry(sources: [geoSrc], elements: [geoElements1, geoElements2])
return geo
}
private func setupFaceNodes() {
// sides
for i in 0..<4 {
let face = SCNNode(geometry: cubeFace())
face.rotation = SCNVector4Make(0, 1, 0, Float(i) * Float(M_PI_2))
rootNode.addChildNode(face)
}
// top/bottom
for i in [1, 3] {
let face = SCNNode(geometry: cubeFace())
face.rotation = SCNVector4Make(1, 0, 0, Float(i) * Float(M_PI_2))
rootNode.addChildNode(face)
}
}
我有一个与正确的整体形状相似的东西:
但我无法弄清楚如何使用SceneKit增加正在绘制的线的宽度。我该怎么做呢?
对于那些感兴趣的人,这里是一个示例项目。
glLineWidth
对于Metal无效(也没有Metal等效物)。 - rickster[[SCNView alloc] initWithFrame:CGRectZero options:@{SCNViewOptionPreferredRenderingAPI:@(SCNRenderingAPIOpenGLES2)}];
- CocoaBob