Drawing a line between two points using SceneKit

后端 未结 7 428
轻奢々
轻奢々 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

    So inside your ViewController.cs define your vector points and call a Draw function, then on the last line there - it's just rotating it to look at point b.

           var a = someVector3;
           var b = someOtherVector3;
           nfloat cLength = (nfloat)Vector3Helper.DistanceBetweenPoints(a, b);
           var cyclinderLine = CreateGeometry.DrawCylinderBetweenPoints(a, b, cLength, 0.05f, 10);
           ARView.Scene.RootNode.Add(cyclinderLine);
           cyclinderLine.Look(b, ARView.Scene.RootNode.WorldUp, cyclinderLine.WorldUp);
    

    Create a static CreateGeomery class and put this static method in there

            public static SCNNode DrawCylinderBetweenPoints(SCNVector3 a,SCNVector3 b, nfloat length, nfloat radius, int radialSegments){
    
             SCNNode cylinderNode;
             SCNCylinder cylinder = new SCNCylinder();
             cylinder.Radius = radius;
             cylinder.Height = length;
             cylinder.RadialSegmentCount = radialSegments;
             cylinderNode = SCNNode.FromGeometry(cylinder);
             cylinderNode.Position = Vector3Helper.GetMidpoint(a,b);
    
             return cylinderNode;
            }
    

    you may also want these utility methods in a static helper class

            public static double DistanceBetweenPoints(SCNVector3 a, SCNVector3 b)
            {
             SCNVector3 vector = new SCNVector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
             return Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z);
            }
    
    
        public static SCNVector3 GetMidpoint(SCNVector3 a, SCNVector3 b){
    
            float x = (a.X + b.X) / 2;
            float y = (a.Y + b.Y) / 2;
            float z = (a.Z + b.Z) / 2;
    
            return new SCNVector3(x, y, z);
        }
    

    For all my Xamarin c# homies out there.

    0 讨论(0)
提交回复
热议问题