Rotating world

Share on Twitter Share on Facebook Share on LinkedIn

Rotating world

Description

In this example we are adding a sphere with an image of the world as a material.

We then set it to continually rotate 360 degrees over a period of 10 seconds using SCNAction.

And last but not least, we are setting its Opacity to be 99%, to give it just a hint of transparency.


Video


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

            var size = 1f;

            var sphereNode = new SphereNode(size);

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

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

            this.sceneView.Session.Pause();
        }
    }

    public class SphereNode : SCNNode
    {
        public SphereNode(float size)
        {
            var rootNode = new SCNNode
            {
                Geometry = CreateGeometry(size),
                Opacity = 0.99f
            };

            var rotateAction = SCNAction.RotateBy(0, (float)(Math.PI), 0, 10.0);
            var repeatForever = SCNAction.RepeatActionForever(rotateAction);
            rootNode.RunAction(repeatForever);

            AddChildNode(rootNode);
        }

        private static SCNGeometry CreateGeometry(float size)
        {
            var material = new SCNMaterial();
            material.Diffuse.Contents = UIImage.FromFile("Images/world-map.jpg");
            material.DoubleSided = true;

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

            return geometry;
        }
    }
}

Next Step : Environmental texturing

After you have mastered this you should try Environmental texturing