Search code examples
iosscenekitscnnodescnscene

How to create a angled plane given an array of scnvector3 points


I am trying to create a plane shape in scenekit using a given array of 3d coordinate points that I've converted into scnvector3 points. I've used bezier paths to create 2d planes and am wondering if there is an easy way of drawing planes in real 3d space


Solution

  • Here is what I ended up doing instead of using SCNPlane (in this case a triangle.) This will create a custom geometry with given points. For the indices, you mark which order you want to connect the points, and then to make it visible from both sides of the plane, you draw the reverse order.

        let vertices: [SCNVector3] = [SCNVector3(0, 1, 0),
                                      SCNVector3(-0.5, 0, 0.5),
                                      SCNVector3(0.5, 0, 0.5)]
    
        let source = SCNGeometrySource(vertices: vertices)
    
        let indices: [UInt16] = [0, 1, 2,
                                 2, 1, 0,]
    
        let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
    
        let geometry = SCNGeometry(sources: [source], elements: [element])
        geometry.firstMaterial?.diffuse.contents  = NSColor.green
        let node = SCNNode(geometry: geometry)
        let scnView = self.view as! SCNView
    
        scnView.scene?.rootNode.addChildNode(node)