0

I can’t find any examples online showing the proper creation of triangles for SCNGeometry that don't use the NSData/Data API. I’m trying to do this but nothing renders:

    func
    testTriangles()
        -> SCNNode
    {
        //  Vertices…
        
        var vertices = [SCNVector3]()
        vertices.append(SCNVector3(x: 0.0, y: 0.0, z: 0.0))
        vertices.append(SCNVector3(x: 0.0, y: 0.0, z: 50.0))
        vertices.append(SCNVector3(x: 50.0, y: 0.0, z: 50.0))
        let vertexSource = SCNGeometrySource(vertices: vertices)
        
        var normals = [SCNVector3]()
        normals.append(SCNVector3(x: 0.0, y: 1.0, z: 0.0))
        normals.append(SCNVector3(x: 0.0, y: 1.0, z: 0.0))
        normals.append(SCNVector3(x: 0.0, y: 1.0, z: 0.0))
        let normalSource = SCNGeometrySource(normals: normals)
        
        //  Indices…
        
        var indices = [0, 1, 2]
        let triangles = SCNGeometryElement(indices: indices, primitiveType: .triangles)
        
        let geom = SCNGeometry(sources: [vertexSource, normalSource], elements: [triangles])
        
        let node = SCNNode()
        node.geometry = geom
        
        let mat = SCNMaterial()
        mat.diffuse.contents = NSColor.green
        geom.materials = [mat]
        
        return node
    }
Rick
  • 3,298
  • 3
  • 29
  • 47
  • 1
    Does this answer your question? [SceneKit – Custom geometry does not show up](https://stackoverflow.com/questions/34897040/scenekit-custom-geometry-does-not-show-up) – James P Jul 28 '20 at 10:29
  • 1
    Taken from that linked answer, I can get your triangle to show using `var indices:[Int32] = [0, 1, 2]` – James P Jul 28 '20 at 10:30
  • 1
    Does this answer your question? [How to debug custom geometry in SceneKit with Swift](https://stackoverflow.com/questions/44480111/how-to-debug-custom-geometry-in-scenekit-with-swift) – mnuages Jul 28 '20 at 11:07
  • @mnuages This is the right answer. Thank you. – Rick Jul 28 '20 at 18:01
  • @james-p your answer also worked; the referenced link's answer from Ash has good advice: use the smallest width that allows for all the vertices in the model. I never would've figured this out on my own. Apple’s docs are inadequate. – Rick Jul 28 '20 at 18:01

1 Answers1

0

Take care with Int32 for the indices

Will not work:

    let indices = [0,1,2] 

Works:

    let indices: [Int32] = [0,1,2] 
Fattie
  • 27,874
  • 70
  • 431
  • 719