Monday, 15 October 2018

Xamarin iOS : Move specific node in Augmented Reality with Scene-kit using touch

This is my code related to adding SCNNode :

void AddPlane(ARHitTestResult hitTestResult)
    {

        // Create a new scene
        var scene = SCNScene.FromFile("art.scnassets/plane_banner.scn");

        planeNode = scene.RootNode.FindChildNode("planeBanner", true);

        planeNode.Position = new SCNVector3(hitTestResult.WorldTransform.Column3.X, hitTestResult.WorldTransform.Column3.Y, hitTestResult.WorldTransform.Column3.Z);

        planeNode.Scale = new SCNVector3((float)0.005, (float)0.005, (float)0.005);

        this.sceneView.Scene.RootNode.AddChildNode(planeNode);

        // Pan gesture
        tappedObjectNode = planeNode;
        planeNode.Name = "planeBanner";
        var panGestureRecognizer = new UIPanGestureRecognizer(move);
        sceneView.AddGestureRecognizer(panGestureRecognizer);

    }

This is my "move" method :

void move(UIPanGestureRecognizer sender)
    {
        if (movingNow)
        {
            CGPoint translation;
            translation = sender.TranslationInView(sceneView);
            SCNVector3 result = CGPointToSCNVector3(sceneView, tappedObjectNode.Position.Z, translation);
            tappedObjectNode.Position = result;
        }

        var test = new NSDictionary();


        var tapPosition = sender.LocationInView(this.View);
        var hitNode = sceneView.HitTest(tapPosition, test);
        if (hitNode != null && hitNode.Length > 0)
        {
            movingNow = true;

            if(hitNode[0].Node.Name == "planeBanner"){
                tappedObjectNode = hitNode[0].Node;
            }
        }

        if(sender.State == UIGestureRecognizerState.Ended) {

        }

    }

here is a method which I found during search for the answer to getting SCNVector3 from the point.

SCNVector3 CGPointToSCNVector3(SCNView aView, float aDepth, CGPoint aPoint)
    {
        var projectOrigin = aView.ProjectPoint(new SCNVector3(0, 0, aDepth));
        var locationWithZ = new SCNVector3((float)aPoint.X, (float)aPoint.Y, projectOrigin.Z);
        return aView.UnprojectPoint(locationWithZ);
    }

The problem is, when I am trying to move the object, it is getting wither zoom out or start from a very left portion of the screen.

I go through various solutions but didn't find anything which can be helpful in this case.

Can anyone guide/help me with respect to this?

how can I make a perfect movable object in AR using Scenekit?



from Xamarin iOS : Move specific node in Augmented Reality with Scene-kit using touch

No comments:

Post a Comment