Cylinder Orientation between two points on a sphere, Scenekit, Quaternions IOS

后端 未结 6 885
南笙
南笙 2020-12-17 00:04

I\'ve been trying to draw a cylinder between two points on the outer edge of a sphere using SceneKit. I have already produced a line between these two points using primitive

6条回答
  •  我在风中等你
    2020-12-17 00:22

    Thank you, Rickster! I have taken it a little further and made a class out of it:

    class LineNode: SCNNode
    {
        init( parent: SCNNode,     // because this node has not yet been assigned to a parent.
                  v1: SCNVector3,  // where line starts
                  v2: SCNVector3,  // where line ends
              radius: CGFloat,     // line thicknes
          radSegmentCount: Int,    // number of sides of the line
            material: [SCNMaterial] )  // any material.
        {
            super.init()
            let  height = v1.distance(v2)
    
            position = v1
    
            let ndV2 = SCNNode()
    
            ndV2.position = v2
            parent.addChildNode(ndV2)
    
            let ndZAlign = SCNNode()
            ndZAlign.eulerAngles.x = Float(M_PI_2)
    
            let cylgeo = SCNCylinder(radius: radius, height: CGFloat(height))
            cylgeo.radialSegmentCount = radSegmentCount
            cylgeo.materials = material
    
            let ndCylinder = SCNNode(geometry: cylgeo )
            ndCylinder.position.y = -height/2
            ndZAlign.addChildNode(ndCylinder)
    
            addChildNode(ndZAlign)
    
            constraints = [SCNLookAtConstraint(target: ndV2)]
        }
    
        override init() {
            super.init()
        }
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
     }
    

    I have tested this class successfully in an iOS app, using this function, which draws 100 lines (oops cylinders :o).

        func linesTest3()
        {
            let mat = SCNMaterial()
            mat.diffuse.contents  = UIColor.whiteColor()
            mat.specular.contents = UIColor.whiteColor()
    
            for _ in 1...100    // draw 100 lines (as cylinders) between random points.
            {
                let v1 =  SCNVector3( x: Float.random(min: -50, max: 50),
                                      y: Float.random(min: -50, max: 50),
                                      z: Float.random(min: -50, max: 50) )
    
                let v2 =  SCNVector3( x: Float.random(min: -50, max: 50),
                                      y: Float.random(min: -50, max: 50),
                                      z: Float.random(min: -50, max: 50) )
    
                // Just for testing, add two little spheres to check if lines are drawn correctly:
                // each line should run exactly from a green sphere to a red one:
    
                root.addChildNode(makeSphere(v1, radius: 0.5, color: UIColor.greenColor()))
                root.addChildNode(makeSphere(v2, radius: 0.5, color: UIColor.redColor()))
    
                // Have to pass the parentnode because 
                // it is not known during class instantiation of LineNode.
    
                let ndLine = LineNode(
                             parent: scene.rootNode, // ** needed
                                 v1: v1,    // line (cylinder) starts here
                                 v2: v2,    // line ends here
                             radius: 0.2,   // line thickness
                    radSegmentCount: 6,     // hexagon tube
                           material: [mat] )  // any material
    
                root.addChildNode(ndLine)
            }
        }
    

    100 random lines Regards. (btw. I can only see 3D objects.. I have never seen a "line" in my life :o)

提交回复
热议问题