Place 3d model in scene

Share on Twitter Share on Facebook Share on LinkedIn

Place 3d model in scene

Description

In this lesson we are importing a 3d model into the AR scene.


Video

No video yet


Code

using ARKit;
using SceneKit;
using System;
using UIKit;

namespace XamarinArkitSample
{
    public partial class ViewController : UIViewController
    {
        private readonly ARSCNView sceneView;

        public ViewController(IntPtr handle) : base(handle)
        {
            this.sceneView = new ARSCNView
            {
                AutoenablesDefaultLighting = true
            };

            this.View.AddSubview(this.sceneView);
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            this.sceneView.Frame = this.View.Frame;
        }

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            this.sceneView.Session.Run(new ARWorldTrackingConfiguration
            {
                AutoFocusEnabled = true,
                PlaneDetection = ARPlaneDetection.Horizontal,
                LightEstimationEnabled = true,
                WorldAlignment = ARWorldAlignment.Gravity

            }, ARSessionRunOptions.ResetTracking | ARSessionRunOptions.RemoveExistingAnchors);

            this.sceneView.Scene.RootNode.AddChildNode(CreateModelNodeFromFile("art.scnassets/tree.dae")); 
        }

        public static SCNNode CreateModelNodeFromFile(string filePath)
        {
            var sceneFromFile = SCNScene.FromFile(filePath);

            var model = sceneFromFile.RootNode.FindChildNode("Cylinder", true);
            model.Scale = new SCNVector3(0.02f, 0.4f, 0.02f);
            model.Position = new SCNVector3(0, -0.2f, 0);

            return model;
        }

        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

            this.sceneView.Session.Pause();
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
        }
    }
}

Next Step : Node animation

After you have mastered this you should try Node animation