Place plane in scene

Share on Twitter Share on Facebook Share on LinkedIn

Place plane in scene

Description

We will place a 2D plane into the scene. You will notice that when placing the plane in the scene, it is placed vertically (not facing up).

These are especially important as we will be placing images on those planes later.

In a later lesson, we will do something cool and many panes and have them all face the camera automatically.


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,
                DebugOptions = ARSCNDebugOptions.ShowWorldOrigin
            };

            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.GravityAndHeading
            }, ARSessionRunOptions.ResetTracking | ARSessionRunOptions.RemoveExistingAnchors);

            // Lesson: Add plane to scene
            var width = 0.1f;
            var length = 0.1f;

            var planeNode = new PlaneNode(width, length, UIColor.Red)
            {
                Position = new SCNVector3(0, 0, 0)
            };

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

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

            this.sceneView.Session.Pause();
        }

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

    public class PlaneNode : SCNNode
    {
        public PlaneNode(float width, float length, UIColor color)
        {
            var rootNode = new SCNNode
            {
                Geometry = CreateGeometry(width, length, color),
                Position = new SCNVector3(0, 0, 0)
            };

            AddChildNode(rootNode);
        }

        private static SCNGeometry CreateGeometry(float width, float length, UIColor color)
        {
            var geometry = SCNPlane.Create(width, length);
            var material = new SCNMaterial();
            material.Diffuse.Contents = color;
            material.DoubleSided = true;
            geometry.Materials = new[] { material };

            return geometry;
        }
    }
}

Next Step : Sizes

After you have mastered this you should try Sizes