Sizes

Share on Twitter Share on Facebook Share on LinkedIn

Sizes

Description

In Arkit, the size of 1 is equivalent to 1m. Therefore 0.1 is 10cm and 0.01 is 1cm.


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

            // 1cm
            var cubeNode1 = new CubeNode(0.01f, UIColor.Red)
            {
                Position = new SCNVector3(0, 0, 0)
            };

            // 10cm
            var cubeNode2 = new CubeNode(0.1f, UIColor.Green)
            {
                Position = new SCNVector3(0.1f, 0, 0)
            };

            // 50cm (0.5m)
            var cubeNode3 = new CubeNode(0.5f, UIColor.Orange)
            {
                Position = new SCNVector3(0.5f, 0, 0)
            };

            // 100cm (1m)
            var cubeNode4 = new CubeNode(1f, UIColor.Yellow)
            {
                Position = new SCNVector3(1.5f, 0, 0)
            };

            this.sceneView.Scene.RootNode.AddChildNode(cubeNode1);
            this.sceneView.Scene.RootNode.AddChildNode(cubeNode2);
            this.sceneView.Scene.RootNode.AddChildNode(cubeNode3);
            this.sceneView.Scene.RootNode.AddChildNode(cubeNode4);
        }

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

            this.sceneView.Session.Pause();
        }

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

    public class CubeNode : SCNNode
    {
        public CubeNode(float size, UIColor color)
        {
            var rootNode = new SCNNode
            {
                Geometry = CreateGeometry(size, color)
            };

            AddChildNode(rootNode);
        }

        private static SCNGeometry CreateGeometry(float size, UIColor color)
        {
            var material = new SCNMaterial();
            material.Diffuse.Contents = color;

            var geometry = SCNBox.Create(size,size,size,0);
            geometry.Materials = new[] { material };

            return geometry;
        }
    }
}

Next Step : Shapes

After you have mastered this you should try Shapes