UrhoSharp 3D Moving Object

Following on from the Introduction to UrhoSharp in Xamarin Forms, we are now looking at creating a 3D moving object. If you haven’t read the first post, please do before continuing as this based on the final result of that post. To create a 3D moving object we need to create a scene, add components and then actions to those components.

Note: I will go through each section, step by step, but please note you won’t see anything on your screen until you have completed most of the steps.


A scene is the place to add all your components, however to provide 3D support we need to add an Octree to implement spatial partitioning.

public class HelloWorld : Application
    protected override async void Start()
        await Create3DObject();

    private async Task Create3DObject()
        var scene = new Scene();

        // Note: Will continue adding code here

Node and Component

Next we want to create a node on the scene and add a component.First creating the node and adding the component (model).

Node node = scene.CreateChild();
node.Position = new Vector3(0, 0, 5);
node.Rotation = new Quaternion(60, 0, 30);

// Add Pyramid Model
StaticModel modelObject = node.CreateComponent<StaticModel>();
modelObject.Model = ResourceCache.GetModel("Models/Pyramid.mdl");

You can set the scale of the node from 0 to X. 1f is the default size, 0f is not seen and you can go above 1f to enlarge it further.


Available in UrhoSharp is a ResourceCache that contains many different prebuilt 3D models such as a pyramid or box. I add the Pyramid model and we can see a pyramid on the screen. Modifying the node we can change its position and rotation.

The Vector3 has parameters x, y, z. The x and y are left to right, with the z going back and forth. x and y = 0 will center the node on the screen. Having z at 0 will also place it at the front of the screen, so we want to set the value higher so it pushes it back and we can see it.

The Quaternion is a mathematical notation, in this context to represent the rotation of a 3D object. If we set the x, y and z all to 0 it will just look like a triangle. Adding values to each axis will rotate it. For example, if I put 180 in the x value, we will see an upside down triangle. If you change the y value it will spin on the y axis.


Now we want to shine some light on our object otherwise it looks rather dull.

Node light = scene.CreateChild(name: "light");
light.SetDirection(new Vector3(0.4f, -0.5f, 0.3f));

Continuing on the same API pattern, you can set the light to come in at a specific direction via x, y, z coordinates.


Lights, now setting a Camera. Next we will be saying Action. In order to see the object, we need to setup a Camera and define the View-port. This will be the perspective we are looking at the object from.

Node cameraNode = scene.CreateChild(name: "camera");
Camera camera = cameraNode.CreateComponent<Camera>();

Renderer.SetViewport(0, new Viewport(scene, camera, null));

You can now run your application and see a static, slightly rotated, slightly lit up pyramid.


Now lets get this pyramid moving. We are going to rotate the Pyramid along the Y-Axis to create a spinning effect.

await node.RunActionsAsync(
   new RepeatForever(new RotateBy(duration: 1,
      deltaAngleX: 0, deltaAngleY: 90, deltaAngleZ: 0)));

Run the project and you will see a spinning Pyramid in all its glory.


If you want to see the working code, I created a sample project UrhoSharp.Demo3D.

Microsoft MVP (Xamarin) | Exrin | Xamarin Forms Developer | Melbourne, Australia | Open to sponsorship to Canada or US

Related Posts

Leave A Comment?