Scenekit: custom geometry not showing

I have to see two yellow triangles, but I don’t see anything.

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
    }
}

I use it as follows:

    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-standard geometry that I see. What's wrong?

+4
source share
2 answers

Your index array has the wrong size element. This is displayed as [Int]. You need to [CInt].

I broke your setup elementsinto:

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

In order for the indices to be packaged as the expected array of C, declare the type explicitly:

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

Custom SceneKit Geometry Swift iOS , Objective C code , Swift 1, .

SCNGeometryElement(indices:, primitiveType:), , , .

+2

, , , 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, , .

+2

Source: https://habr.com/ru/post/1625101/


All Articles