Vertical items from business card

Share on Twitter Share on Facebook Share on LinkedIn

Vertical items from business card

Description

In this lesson we are detecting an image, then putting some tiled images vertically above / perpendicular to it.


Video


Code

using ARKit;
using SceneKit;
using System;
using System.Linq;
using UIKit;
using XamarinArkitSample.Code;

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

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

            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);

            var detectionImages = ARReferenceImage.GetReferenceImagesInGroup("AR Resources", null);

            this.sceneView.Session.Run(new ARWorldTrackingConfiguration
            {
                AutoFocusEnabled = true,
                PlaneDetection = ARPlaneDetection.Horizontal | ARPlaneDetection.Vertical,
                LightEstimationEnabled = true,
                WorldAlignment = ARWorldAlignment.GravityAndHeading,
                DetectionImages = detectionImages,
                MaximumNumberOfTrackedImages = 10

            }, ARSessionRunOptions.ResetTracking | ARSessionRunOptions.RemoveExistingAnchors);
        }

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

            this.sceneView.Session.Pause();
        }

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

    public class SceneViewDelegate : ARSCNViewDelegate
    {
        public override void DidAddNode(ISCNSceneRenderer renderer, SCNNode node, ARAnchor anchor)
        {
            if (anchor is ARImageAnchor imageAnchor)
            {
                var scnNode = new SCNNode();
                scnNode.Position = new SCNVector3(0, 0.1f, -0.5f);
                node.AddChildNode(scnNode);

                var detectedImage = imageAnchor.ReferenceImage;

                var manufacturers = Manufacturers.GetManufacturers().OrderByDescending(m => m.Name).ToArray();

                var rows = 8;
                var columns = 8;
                var planeWidth = 0.08f;
                var planeHeight = 0.06f;
                var margin = 0.01f;

                int count = 0;
                int total = 64;

                var startingX = -0.4f;
                var startingY = 0.06f;
                var startingZ = -0.03f;

                for (int row = 0; row < rows; row++)
                {
                    for (int column = 0; column < columns; column++)
                    {
                        float x = startingX + (planeWidth * column) + (column * margin);
                        float y = startingY + (planeHeight * row) + (row * margin);
                        float z = startingZ;

                        var position = new SCNVector3(x, y, z);

                        var planeNode = new PlaneNode(planeWidth, planeHeight, position, manufacturers[count].ImagePath);

                        planeNode.Opacity = 0.9f;

                        node.AddChildNode(planeNode);

                        if (column == 16)
                        {
                            column = 0;
                            row++;
                        }

                        count++;
                    }
                }      
            }
        }

        public override void DidRemoveNode(ISCNSceneRenderer renderer, SCNNode node, ARAnchor anchor)
        {
            if (anchor is ARPlaneAnchor planeAnchor)
            {
            }
        }

        public override void DidUpdateNode(ISCNSceneRenderer renderer, SCNNode node, ARAnchor anchor)
        {
            if (anchor is ARPlaneAnchor planeAnchor)
            {
            }
        }
    }

    public class PlaneNode : SCNNode
    {
        public PlaneNode(nfloat width, nfloat length, SCNVector3 position, string imageUrl)
        {
            var rootNode = new SCNNode
            {
                Geometry = CreateGeometry(width, length, imageUrl),
                Position = position
            };

            AddChildNode(rootNode);
        }

        private static SCNGeometry CreateGeometry(nfloat width, nfloat length, string imageUrl)
        {
            var material = new SCNMaterial();

            if (!string.IsNullOrEmpty(imageUrl))
            {
                var image = UIImage.FromFile(imageUrl);
                material.Diffuse.Contents = image;
            }
            else
            {
                material.Diffuse.Contents = UIColor.Red;
            }

            material.DoubleSided = true;

            var geometry = SCNPlane.Create(width, length);
            geometry.Materials = new[] { material };

            var rootNode = new SCNNode();
            rootNode.Geometry = geometry;

            return geometry;
        }
    }
}

Next Step : Add information to business card

After you have mastered this you should try Add information to business card