SceneKit - 自定义几何图形不显示 [英] SceneKit – Custom geometry does not show up
问题描述
我应该看到 2 个黄色三角形,但什么也没看到.
I should see 2 yellow triangles, but I see nothing.
class Terrain {
private class func createGeometry () -> SCNGeometry {
let sources = [
SCNGeometrySource(vertices:[
SCNVector3(x: -1.0, y: -1.0, z: 0.0),
SCNVector3(x: -1.0, y: 1.0, z: 0.0),
SCNVector3(x: 1.0, y: 1.0, z: 0.0),
SCNVector3(x: 1.0, y: -1.0, z: 0.0)], count:4),
SCNGeometrySource(normals:[
SCNVector3(x: 0.0, y: 0.0, z: -1.0),
SCNVector3(x: 0.0, y: 0.0, z: -1.0),
SCNVector3(x: 0.0, y: 0.0, z: -1.0),
SCNVector3(x: 0.0, y: 0.0, z: -1.0)], count:4)
]
let elements = [
SCNGeometryElement(indices: [0, 2, 3, 0, 1, 2], primitiveType: .Triangles)
]
let geo = SCNGeometry(sources:sources, elements:elements)
let mat = SCNMaterial()
mat.diffuse.contents = UIColor.yellowColor()
mat.doubleSided = true
geo.materials = [mat]
return geo
}
class func createNode () -> SCNNode {
let node = SCNNode(geometry: createGeometry())
node.name = "Terrain"
node.position = SCNVector3()
return node
}
}
我是这样使用的:
let terrain = Terrain.createNode()
sceneView.scene?.rootNode.addChildNode(terrain)
let camera = SCNCamera()
camera.zFar = 10000
self.camera = SCNNode()
self.camera.camera = camera
self.camera.position = SCNVector3(x: -20, y: 15, z: 30)
let constraint = SCNLookAtConstraint(target: terrain)
constraint.gimbalLockEnabled = true
self.camera.constraints = [constraint]
sceneView.scene?.rootNode.addChildNode(self.camera)
我看到其他具有非自定义几何图形的节点.怎么了?
I get other nodes with non-custom geometry which I see. What's wrong?
推荐答案
注意:请参阅 Ash 的回答,对于现代 Swift 来说,这是一种比这更好的方法.
您的索引数组有错误的大小元素.它被推断为 [Int]
.你需要[CInt]
.
Your index array has the wrong size element. It's being inferred as [Int]
. You need [CInt]
.
我将您的 elements
设置分解为:
I broke out your elements
setup into:
let indices = [0, 2, 3, 0, 1, 2] // [Int]
print(sizeof(Int)) // 8
print(sizeof(CInt)) // 4
let elements = [
SCNGeometryElement(indices: indices, primitiveType: .Triangles)
]
要像预期的 C 数组一样打包索引,请显式声明类型:
To get the indices to be packed like the expected C array, declare the type explicitly:
let indices: [CInt] = [0, 2, 3, 0, 1, 2]
自定义 SceneKit 几何在 iOS 上的 Swift 中不起作用,但等效的 Objective C 代码确实更详细,但它是针对 Swift 1 编写的,因此您必须进行一些翻译.
Custom SceneKit Geometry in Swift on iOS not working but equivalent Objective C code does goes into more detail, but it's written against Swift 1, so you'll have to do some translation.
SCNGeometryElement(indices:,primitiveType:)
似乎没有记录在任何地方,尽管它确实出现在标题中.
SCNGeometryElement(indices:, primitiveType:)
doesn't appear to be documented anywhere, although it does appear in the headers.
这篇关于SceneKit - 自定义几何图形不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!