Xamarin just announced that the game engine UhroSharp can be use to create Hololens apps. It’s an interesting alternative to full DirectX app, UWP 2D or Unity3D.

Create the basics

In order to build an Hololens application with UrhoSharp, we need to create a new UWP project and to add the NuGet package UrhoSharp.Hololens. Once done, remove the App.xaml/App.xaml.cs and the MainView.xaml/MainView.xaml.cs because the entry point will be a little bit different and we will not use XAML at all.

Create a class named Program who will be our new entry point :

using System;
using Windows.ApplicationModel.Core;

namespace UrhoTest
{
    internal class Program
    {
        [MTAThread]
        private static void Main()
        {
            var exclusiveViewApplicationSource = new AppViewSource();
            CoreApplication.Run(exclusiveViewApplicationSource);
        }
    }
}

That class have one goal : define the view source used by the app in order to display something on the screen. The next step is, of course, the creation of the view source (IFrameworkViewSource) with a little help by the UrhoSharp engine.

using Windows.ApplicationModel.Core;
using Urho.HoloLens;

namespace UrhoTest
{
    internal class AppViewSource : IFrameworkViewSource
    {
        public IFrameworkView CreateView()
        {
            return UrhoAppView.Create<MyApplication>(null);
        }
    }
}

The generic static method UhroAppView.Create<T> allows us to create a new “game application”. By “game application” I mean that the app will have a life-cycle adapted for a game (update loop, camera, …). UrhoSharp have a class named HoloApplication that will handle tons of stuff for us and provide methods that can be override to add logic (Start, Stop, OnUpdate, OnGestureTapped, …).

using Urho.Holographics;

namespace UrhoTest
{
    public class MyApplication : HoloApplication
    {
        public MyApplication(string pak, bool emulator)
            : base(pak, emulator) { }
    }
}

You can now run the app on your Hololens or on the emulator and, magic, nothing will appear !

Show me some stuff

A ball, a single one

Like Unity3D or others game engine, you need to add contents in a Scene to be visible on the screen. Basically, a Scene is a hierarchy of game object (named Node in UrhoSharp) that can accept several components like renderer, physical property, collision and many more.

Well, a blue ball is what we really need to become a Jedi, at least try to. In the method Start, so at the beginning of the app, we create a Node to display our ball. The ball node will have another node to define his model (a sphere).

private Node _ballNode;
private Node _ballModelNode;

protected override void Start()
{
    base.Start();

    CreateBall();
}

private void CreateBall()
{
    // Create a new node on the scene
    _ballNode = Scene.CreateChild();
    _ballNode.Position = new Vector3(0, 0, 3);
    _ballNode.Scale = new Vector3(0.1f, 0.1f, 0.1f);

    // Add the model
    _ballModelNode = _ballNode.CreateChild();
    var sphere = _ballModelNode.CreateComponent<Sphere>();
    sphere.Color = Color.Blue;
}

Do NOT use the Setup method to initialize your scene. The Start method is what you need !

The scene hierarchy can be represented like that :

  • Scene
    • Ball
      • Model

Balls everywhere

In this sample, we will use the OnUpdate method and the TimeStep property to display a new ball each second. The TimeStep is the time passed between the last frame.

private float _spawnTimer = 1;
private float _spawnDeltaTime;

protected override void OnUpdate(float timeStep)
{
    base.OnUpdate(timeStep);

    _spawnDeltaTime += timeStep;
    if (_spawnDeltaTime >= _spawnTimer)
    {
        _spawnDeltaTime = 0;

        var randomPosition = new Vector3(Randoms.Next(-0.3f, 0.3f), Randoms.Next(-0.3f, 0.3f), Randoms.Next(2, 5));
        CreateBall(randomPosition);
    }
}

private void CreateBall(Vector3 position)
{
    // Create a new node on the scene
    var ballNode = Scene.CreateChild();
    ballNode.Position = position;
    ballNode.Scale = new Vector3(0.1f, 0.1f, 0.1f);

    // Add the model
    var ballModelNode = ballNode.CreateChild();
    var sphere = ballModelNode.CreateComponent<Sphere>();
    sphere.Color = Color.Blue;
}

The scene hierarchy can be represented like that :

  • Scene
    • Ball (1)
      • Model
    • Ball (2)
      • Model
    • Ball (3)
      • Model

urhosharp_balls_everywhere

In the next post, we will talk about the physic engine.

Stay tuned !