Add information to business card
Description
In this lesson we are adding some images and text to a detected image in the scene.
One thing worth mentioning, that that can be demonstrated in the video is that transparent images work really well with ARKit. In the video you can see that images with my name and title in white with transparent background allows the image to be seen through.
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,
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 = 1
}, 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 detectedImage = imageAnchor.ReferenceImage;
node.AddChildNode(CreateImagePlane("Images/lee-englestone.png", 0.09f, 0.02f, new SCNVector3(0.005f, 0f, -0.02f)));
node.AddChildNode(CreateImagePlane("Images/software-development-manager.png", 0.09f, 0.01f, new SCNVector3(0.005f, 0f, -0.04f)));
node.AddChildNode(CreateImagePlane("Images/headshot.jpg", 0.055f, 0.055f, new SCNVector3(-0.07f, 0, 0)));
node.AddChildNode(CreateImagePlane("Images/twitter-transparent.png", 0.02f, 0.02f, new SCNVector3(-0.03f, 0, 0.038f)));
node.AddChildNode(CreateImagePlane("Images/linkedin-transparent.png", 0.02f, 0.02f, new SCNVector3(0.00f, 0, 0.038f)));
node.AddChildNode(CreateImagePlane("Images/youtube-transparent.png", 0.02f, 0.02f, new SCNVector3(0.03f, 0, 0.038f)));
}
}
public static SCNNode CreateImagePlane(string imagePath, float width, float height, SCNVector3 position)
{
var image = UIImage.FromFile(imagePath);
var material = new SCNMaterial();
material.Diffuse.Contents = image;
material.DoubleSided = true;
var geometry = SCNPlane.Create(width, height);
geometry.Materials = new[] { material };
var rootNode = new SCNNode();
rootNode.Geometry = geometry;
float angle = (float)(-Math.PI / 2);
rootNode.EulerAngles = new SCNVector3(angle, 0, 0);
rootNode.Position = position;
rootNode.Opacity = 0;
rootNode.RunAction(SCNAction.FadeIn(1));
return rootNode;
}
public static SCNNode CreateModelNodeFromFile(string filePath, string rootNodeName)
{
var sceneFromFile = SCNScene.FromFile(filePath);
var model = sceneFromFile.RootNode.FindChildNode(rootNodeName, true);
model.Scale = new SCNVector3(0.01f, 0.01f, 0.01f);
model.Position = new SCNVector3(0, 0.2f, 0);
return model;
}
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)
{
}
}
}
}
Next Step : Show 3D model on a business card
After you have mastered this you should try Show 3D model on a business card