Drawing a line between two points using SceneKit

后端 未结 7 463
轻奢々
轻奢々 2020-12-04 20:12

I have two points (let\'s call them pointA and pointB) of type SCNVector3. I want to draw a line between them. Seems like it should be easy, but can\'t find a way to do it.<

7条回答
  •  暖寄归人
    2020-12-04 21:10

    Here is a swift5 version:

    func lineBetweenNodes(positionA: SCNVector3, positionB: SCNVector3, inScene: SCNScene) -> SCNNode {
        let vector = SCNVector3(positionA.x - positionB.x, positionA.y - positionB.y, positionA.z - positionB.z)
        let distance = sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z)
        let midPosition = SCNVector3 (x:(positionA.x + positionB.x) / 2, y:(positionA.y + positionB.y) / 2, z:(positionA.z + positionB.z) / 2)
    
        let lineGeometry = SCNCylinder()
        lineGeometry.radius = 0.05
        lineGeometry.height = distance
        lineGeometry.radialSegmentCount = 5
        lineGeometry.firstMaterial!.diffuse.contents = GREEN
    
        let lineNode = SCNNode(geometry: lineGeometry)
        lineNode.position = midPosition
        lineNode.look (at: positionB, up: inScene.rootNode.worldUp, localFront: lineNode.worldUp)
        return lineNode
    }
    

提交回复
热议问题