如何使用 Swift 在 SceneKit 中调试自定义几何体

How to debug custom geometry in SceneKit with Swift

我正在尝试学习如何在 SceneKit 中创建自定义几何体。但是,我尝试制作一个三角形,但没有显示任何内容。我不知道如何调试它。有没有办法确定三角形是否有效?我只是不知道从哪里开始。

作为参考,有问题的游乐场代码如下。请注意,它是针对 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 时,索引的类型需要为 Int161。我认为这没有记录在任何地方。但是当你也改变索引的声明时

let indices: [Int16] = [
    0, 2, 1
]

应该出现三角形。


编辑

1:正如@mnuages 所指出的,SceneKit 仅支持 32 位整数作为索引。所以你可以使用 Int8Int16Int32.

documentation for the Int type 我们得到

On 32-bit platforms, Int is the same size as Int32, and on 64-bit platforms, Int is the same size as Int64.

因此,在 Swift 游乐场中,您最终会得到 Int64

这很不幸,因为 SceneKit(和 Metal)仅支持 UInt8UInt16UInt32。在调试器控制台中,您应该能够看到 SceneKit 警告不支持的 64 位宽索引。