Draw SceneKit object between two points

前端 未结 7 2013
别跟我提以往
别跟我提以往 2020-12-07 23:51

Having made some progress in the geometry side of things I\'m moving on to putting together an entire scene. That scene has a couple dozen objects, each defined by a boundin

7条回答
  •  忘掉有多难
    2020-12-08 00:08

    Draw the line between two nodes:

    func generateLine( startPoint: SCNVector3, endPoint: SCNVector3) -> SCNGeometry {
    
            let vertices: [SCNVector3] = [startPoint, endPoint]
            let data = NSData(bytes: vertices, length: MemoryLayout.size * vertices.count) as Data
    
            let vertexSource = SCNGeometrySource(data: data,
                                                 semantic: .vertex,
                                                 vectorCount: vertices.count,
                                                 usesFloatComponents: true,
                                                 componentsPerVector: 3,
                                                 bytesPerComponent: MemoryLayout.size,
                                                 dataOffset: 0,
                                                 dataStride: MemoryLayout.stride)
    
            let indices: [Int32] = [ 0, 1]
    
            let indexData = NSData(bytes: indices, length: MemoryLayout.size * indices.count) as Data
    
            let element = SCNGeometryElement(data: indexData,
                                             primitiveType: .line,
                                             primitiveCount: indices.count/2,
                                             bytesPerIndex: MemoryLayout.size)
    
            return SCNGeometry(sources: [vertexSource], elements: [element])
    
        }
    

    How To Use

    let line = generateLine(startPoint: SCNVector3Make(1, 1, 1), endPoint: SCNVector3Make(8, 8, 8))
            let lineNode = SCNNode(geometry: line)
            lineNode.position = SCNVector3Make(15, 15, 10)
            scene.rootNode.addChildNode(lineNode)
    

    The thickness of the line requires implementing the SCNSceneRendererDelegate, in particular:

    func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval){
            glLineWidth(10)
    }
    

提交回复
热议问题