我正在尝试学习如何在 SceneKit 中创建自定义几何体。但是,我尝试制作一个三角形,但没有显示任何内容。我不知道如何调试它。有没有办法确定三角形是否有效?我只是不知道从哪里开始。
作为引用,有问题的 Playground 代码如下。请注意,它是针对 Swift 4 编写的,但 Swift 3 和 Swift 4 之间的变化是如此之小,以至于在 Swift 3 中编译它是微不足道的。
import UIKit
import SceneKit
let points = [
SCNVector3Make(0, 0, 0),
SCNVector3Make(0, 10, 0),
SCNVector3Make(10, 0, 0),
]
let indices = [
0,2,1,
]
let vertexSource = SCNGeometrySource(vertices: points)
let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
let geo = SCNGeometry(sources: [vertexSource], elements: [element])
最佳答案
创建自定义 SCNGeometryElement
时,索引的类型需要为 Int16
1。我认为这没有记录在任何地方。但是当你也改变索引的声明时
let indices: [Int16] = [
0, 2, 1
]
应该出现三角形。
编辑
1:正如@mnuages 所指出的,SceneKit 仅支持 32 位整数作为索引。因此,您可以使用 Int8
、Int16
和 Int32
。
关于ios - 如何使用 Swift 在 SceneKit 中调试自定义几何体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480111/