SceneKit - 自定义几何体不显示

问题描述 投票:6回答:2

我应该看到2个黄色三角形,但我什么也看不见。

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)

我看到其他节点具有非自定义几何体。怎么了?

swift scenekit scnnode
2个回答
3
投票

注意:请参阅Ash的答案,对于现代Swift而言,这是一个比这个更好的方法。

您的索引数组具有错误的size元素。它被推断为[Int]。你需要[CInt]

我把你的elements设置分解成:

    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数组一样打包,请显式声明类型:

    let indices: [CInt] = [0, 2, 3, 0, 1, 2]

Custom SceneKit Geometry in Swift on iOS not working but equivalent Objective C code does详细介绍,但它是针对Swift 1编写的,所以你必须做一些翻译。

SCNGeometryElement(indices:, primitiveType:)似乎没有在任何地方记录,尽管它确实出现在标题中。


4
投票

Hal Mueller非常正确,因为所涉及的索引必须是指定的类型,但应该注意的是,在最近版本的Swift语言中,此功能已发生显着变化。值得注意的是,SCNGeometryElement(indices:, primitiveType:)现在在Swift 4中运行得非常好,我建议不要使用对我不起作用的CInt。而是使用符合FixedWidthInteger协议的标准整数类型之一,即Int32。如果您知道网格中涉及最大数量的顶点,请使用可以包含所有顶点的最小位数。

例:

    let vertices = [
        SCNVector3(x: 5, y: 4, z: 0),
        SCNVector3(x: -5 , y: 4, z: 0),
        SCNVector3(x: -5, y: -5, z: 0),
        SCNVector3(x: 5, y: -5, z: 0)
    ]

    let allPrimitives: [Int32] = [0, 1, 2, 0, 2, 3]
    let vertexSource = SCNGeometrySource(vertices: vertices)
    let element = SCNGeometryElement(indices: allPrimitives, primitiveType: .triangles)
    let geometry = SCNGeometry(sources: [vertexSource], elements: [element])

    SCNNode(geometry: geometry)

这里发生了什么事?

首先,我们创建一个vertices数组,描述三维空间中的点。 allPrimitives数组描述了这些顶点如何链接。每个元素都是vertices数组的索引。由于我们使用三角形,因此应将这些三角形考虑在内,每个角落一个。为简单起见,我在这里做了一个简单的扁平方块。然后,我们使用所有顶点的原始数组和使用vertices数组的几何元素创建语义类型为allPrimitives的几何源,同时通知它们它们是三角形,因此它知道将它们分组为三角形。这些可以用于创建SCNGeometry对象,我们初始化我们的SCNNode

考虑它的一种简单方法是顶点源仅存在于列出对象中的所有顶点。几何元素仅用于描述这些顶点是如何链接的。它是SCNGeometry将这两个对象组合在一起以创建最终的物理表示。

© www.soinside.com 2019 - 2024. All rights reserved.