加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – SceneKit – 自定义几何体不显示

发布时间:2020-12-14 05:38:26 所属栏目:百科 来源:网络整理
导读:我应该看到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,SCNVector3(x: 1.0,z: 0
我应该看到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,SCNVector3(x:  1.0,z:  0.0)],count:4),SCNGeometrySource(normals:[
                SCNVector3(x:  0.0,y:  0.0,z: -1.0),SCNVector3(x:  0.0,z: -1.0)],count:4)
        ]

        let elements = [
            SCNGeometryElement(indices: [0,2,3,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)

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

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

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

我将你的元素设置分解为:

let indices = [0,2] // [Int]
    print(sizeof(Int))               // 8
    print(sizeof(CInt))              // 4
    let elements = [
        SCNGeometryElement(indices: indices,primitiveType: .Triangles)
    ]

要使索引像预期的C数组一样打包,请显式声明类型:

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读