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.

Scene

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()
    {
        base.Start();
        await Create3DObject();
    }

    private async Task Create3DObject()
    {
        var scene = new Scene();
        scene.CreateComponent<Octree>();

        // 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);
node.SetScale(1f);

// 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.

Component

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.

Light

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));
light.CreateComponent<Light>();

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

Camera

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.

Action

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.

urhosharp_pyramid

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


XAMARIN.FORMS MONTHLY NEWSLETTER

JOIN 1,000+ SUBSCRIBERS

  1. Don't miss out on updates
  2. The latest info from this site, Xamarin and the community
  3. Unsubscribe at any time*

* We use MailChimp, with double opt-in, and instant unsubscribe

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer | Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Related Posts

6 Comments

  1. Abra

    Is there any possibility to change the spinning speed of the pyramide at runtime (for instance by editing the rotation speed in a textbox, or by using a slider to speed up and down) ?

  2. Abra

    Thank you for your reply. I am not sure how to achieve practically the change of the duration at runtime. In your sample the duration is hardcoded and set at the application start in the RotateBy() constructor.
    Would be possible to add for instance a TextBox in MainPage.xaml (with the value bound to some property in a view model), and when this changes (after user input) to modify the duration of RotateBy() ? Is it possible to modify only the duration inside the Urho action, or must the Urho action be recreated for the node (the actions are supposed to be immutable) ? (I am just starting using UrhoSurface and trying to understand the different mechanisms)

  3. JinJia.Hsu

    How to remove specific action?
    RemoveAction doesn’t work but RemoveAllActions works!
    Is it a bug?

    var finiteAction = new RepeatForever(new RotateBy(duration: 1, deltaAngleX: 0, deltaAngleY: 90, deltaAngleZ: 0));
    await node.RunActionsAsync(finiteAction);
    node.RemoveAction(finiteAction); // Fail
    node.RemoveAllActions(); // Success

    1. Adam Pedley

      It sounds like a bug, if it doesn’t throw an error or anything, I would post that on the Xamarin Forums to see if one of the Xamarin team can help. If not, lodge a bug report on bugzilla.xamarin.com

Leave A Comment?