Getting direction from mesh


I have a mesh in a running babylonjs Scene (either imported by calling SceneLoader.ImportMeshAsync or created by a function like CreateBox - in case that matters).

I set its position and direction (which I am assuming is the orientation in the scene…) like this:

mesh.setAbsolutePosition(new Vector3(1, 2, 3));
mesh.setDirection(new Vector3(1, 0, 0));

I activated a GizmoManager in the scene for rotation and enabled gizmo on my mesh:

const m = new GizmoManager(scene);
m.rotationGizmoEnabled = true;
m.attachableMeshes = [mesh];

I see the mesh rendered on the canvas.

Let’s say that as a user, at runtime, I click the mesh, see the gizmo, and use gizmo to rotate that mesh just very slightly.

Now as I user I click a button that dumps this mesh’ getDirection() to console, and I want to see that the mesh’ orientation changed just very slightly… Here is where I have a problem:

First, of course, getDirection() has a required argument; I don’t know what to pass in there. All I want if to get back the same (or slightly different if the mesh has been rotated slightly by the user) direction/orientation that I passed to that mesh previously by calling setDirection(). If I pass to getDirection some Vector3 values (say new Vector3(1, 0, 0) or new Vector3(1, 0, 1) or whatever), I do get some values returned by that getDirection(), but they are unexpected and I don’t know how to interpret them.

So my simple question is - how do I get the direction/orientation from the mesh, having previously set it using mesh.setDirection(new Vector3(1, 0, 1)) ? Or maybe calling setDirection isn’t the way to specify the mesh’ orientation in the the scene… then what is the way?..

If someone offers guidance on how I should be thinking about that localAxis that’s associated with mesh’ direction, I would also appreciate that greatly.

Thank you very much!

getDirection is simply transforming the direction you pass in by the world matrix of the mesh, so you won’t get the axis you pass to setDirection this way (I’m not sure why getDirection is called this way).

You would need to invert the calculation done by setDirection to retrieve the axis you seek.

I tried to do it in this PG:

Look at the console, it prints the value you passed to setDirection, then the value generated by the local getDirection method.

It seems to work, but I don’t give any guarantee!

Note that it won’t work in the general case if you set a roll (rotation around the Z axis), because the calculation only takes into account pitch and yaw. So, if you use the gizmo, you won’t get the right axis if you rotate around Z.

Regarding the localAxis passed to setDirection, you should think of it as the “deviation” of the mesh according to the natural orientation of the mesh, which is (0,0,1). So, if you pass (0,0,1), the orientation of the mesh won’t change. If you pass (1,0,1), the mesh will be reorientated so that the (0,0,1) axis matches (1,0,1), meaning the mesh will be rotated around Y “on the right”.

I think the PG I gave above will help you understand it: start with a (0,0,1) axis to see what is the original orientation of the cones when there’s no rotation applied (one cone is pointing to the +X direction and the other to +Y), then change the axis and see how it rotates the mesh.

Many thanks, @Evgeni_Popov

Your answer seems to answer the question as I posed it and attempts to explain the intuition behind localAxis.

It at also helped me to finally see that calling setDirection to just set mesh’ orientation/rotation in the scene is plain wrong, it’s a misuse of that (confusingly named and poorly documented) method.

// In my defense, I inherited, didn’t write the code that called setDirection; not that anyone cares.

In this particular case all I care about is setting the mesh’ orientation/rotation in the Scene and seeing it hanging there with the orientation that I set, and maybe later changing the orientation at runtime and querying the mesh to get its up-to-date orientation (no animation, no physics involved at the moment).

For doing that, is it fair to say that I should simply set and read AbstractMesh#rotation and leave the localAxis (aka “direction”) of the mesh at its default value of (0,0,1)?..

Indeed, mesh.rotation is all you need if you want to update the orientation of the mesh (or mesh.rotationQuaternion if you want to use quaternions instead of Euler angles).

Note that there is no “mesh direction” per see: there’s no property for that. The setDirection method simply takes a vector and compute some values for the rotation property assuming the vector you pass is a “deviation” around the (0,0,1) vector.