Skip to content

Instantly share code, notes, and snippets.

@dotMorten

dotMorten/ARApp.Android.cs

Last active Mar 16, 2018
Embed
What would you like to do?
Abstract UrhoSharp AR class
#if __ANDROID__
using Urho;
using System.Linq;
using Com.Google.AR.Core;
using Urho.Droid;
namespace UrhoAR
{
public abstract partial class ARApp : SimpleApplication
{
private ARCoreComponent ArCore;
private Frame currentFrame;
private void SetupAR()
{
ArCore = Scene.CreateComponent<ARCoreComponent>();
ArCore.ARFrameUpdated += OnARFrameUpdated;
ArCore.ConfigRequested += ArCore_ConfigRequested;
ArCore.Run();
}
protected Vector3? HitTest(float screenX = 0.5f, float screenY = 0.5f)
{
var hit = currentFrame?.HitTest(screenX * Graphics.Width, screenY * Graphics.Height).FirstOrDefault();
if (hit != null)
{
var hitPos = hit.HitPose;
return new Vector3(hitPos.Tx(), hitPos.Ty(), -hitPos.Tz());
}
return null;
}
private void ArCore_ConfigRequested(Config config)
{
config.SetPlaneFindingMode(Config.PlaneFindingMode.Horizontal);
config.SetLightEstimationMode(Config.LightEstimationMode.AmbientIntensity);
config.SetUpdateMode(Config.UpdateMode.LatestCameraImage); //non blocking
}
private void OnARFrameUpdated(Frame arFrame)
{
currentFrame = arFrame;
}
}
}
#endif
using System;
using System.Collections.Generic;
using System.Text;
using Urho;
using System.Linq;
namespace UrhoAR
{
public abstract partial class ARApp
{
private bool scaling;
[Preserve]
protected ARApp(ApplicationOptions options) : base(options) { }
protected override void Start()
{
base.Start();
SetupAR();
#if !NETFX_CORE
Input.TouchBegin += OnTouchBegin;
Input.TouchEnd += OnTouchEnd;
#endif
}
#if !NETFX_CORE
private void OnTouchBegin(TouchBeginEventArgs e)
{
scaling = false;
}
protected override void OnUpdate(float timeStep)
{
if (Input.NumTouches == 2)
{
scaling = true;
}
base.OnUpdate(timeStep);
}
private void OnTouchEnd(TouchEndEventArgs e)
{
if (scaling)
return;
OnTapped(e.X / (float)Graphics.Width, e.Y / (float)Graphics.Height);
}
#endif
protected virtual void OnTapped(float x, float y)
{
}
public Vector3 UserLocation
{
get
{
#if NETFX_CORE
var x = (LeftCamera.View.m20 + RightCamera.View.m20) * .5f;
var y = (LeftCamera.View.m21 + RightCamera.View.m21) * .5f;
var z = (LeftCamera.View.m22 + RightCamera.View.m22) * .5f;
return new Vector3(x, y, z);
#else
return Camera.Node.WorldPosition;
#endif
}
}
}
}
#if __IOS__
using Urho;
using System.Linq;
using Urho.iOS;
using ARKit;
namespace UrhoAR
{
public abstract partial class ARApp : SimpleApplication
{
private ARKitComponent arkitComponent;
private void SetupAR()
{
arkitComponent = Scene.CreateComponent<ARKitComponent>();
arkitComponent.Orientation = UIKit.UIInterfaceOrientation.Portrait;
arkitComponent.ARConfiguration = new ARWorldTrackingConfiguration
{
PlaneDetection = ARPlaneDetection.Horizontal,
};
arkitComponent.RunEngineFramesInARKitCallbakcs = Options.DelayedStart;
arkitComponent.Run();
}
protected Vector3? HitTest(float screenX = 0.5f, float screenY = 0.5f)
{
var result = arkitComponent.ARSession.CurrentFrame.HitTest(new CoreGraphics.CGPoint(screenX, screenY),
ARHitTestResultType.ExistingPlaneUsingExtent)?.FirstOrDefault();
if (result != null)
{
var row = result.WorldTransform.Row3;
return new Vector3(row.X, row.Y, -row.Z);
}
return null;
}
}
}
#endif
#if !NETFX_CORE && !__IOS__ && !__ANDROID__
using Urho;
namespace UrhoAR
{
public abstract partial class ARApp : SimpleApplication
{
private void SetupAR()
{
Viewport.SetClearColor(Color.Black);
}
protected Vector3? HitTest(float screenX = 0.5f, float screenY = 0.5f)
{
return null;
}
}
}
#endif
#if NETFX_CORE
using Urho;
namespace UrhoAR
{
public abstract partial class ARApp : Urho.SharpReality.StereoApplication
{
private void SetupAR()
{
}
public override void OnGestureTapped()
{
base.OnGestureTapped();
OnTapped(.5f, .5f);
}
protected Vector3? HitTest(float screenX = 0.5f, float screenY = 0.5f)
{
throw new NotImplementedException("TODO");
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment