Transform/matrix elements

I’m a bit confused about the Transform class - I was expecting it to be a 4x4 matrix, not a Component (what does transform.transform… really mean?). What’s the internal representation? I tried looking it up with Reflector, but most of it is native code.

The thing is I need to use my own matrix/transform classes, in order to be compatible with playback engines other than Unity, and so I would like to be able to copy the data from my representation to Unity’s. I can probably do that using the exposed methods, but I’m guessing most of them will add some overhead compared to a straight copy of matrix elements.

Also, marshalling between managed and native code is normally quite expensive. I don’t know the specifics of Unity’s implementation though, so maybe it’s a moot point - otherwise, I want to keep the number of calls to a minimum.

Any pointers appreciated!

transform.transform doesn’t mean anything; that’s the same thing as gameObject.transform, which is short for gameObject.GetComponent(Transform).

transform.position is a Vector3, transform.rotation is a Quaternion, transform.localScale is a Vector3. You can use localToWorldMatrix to get a Matrix4x4, and use functions like MultiplyPoint, MultiplyVector, etc. to do matrixy stuff to it.

Probably best just to use scripting in Unity.

–Eric

As, in a number of Vector3:s (one for position, one for scaling etc)? You sure they’re not just accessors to a native Matrix4x4? And would the Quaternion be stored separately? I’m no math guy, but my guess is that most tranformation math is faster using matrices than separated vectors and quaternions.

So I can get the matrix - but how do I set it?

Judging from the disassembled UnityEngine.dll, using managed code will result in a lot of marshalling, which is what I want to avoid. I think it should be faster in my setup if I handle all the matrix math myself in managed code, and then just send the elements with a single marshalled call to the native Unity engine.

Transform class keeps position (Vector3), rotation (Quaternion), scale (Vector3), and references to transform parent/children.

The transform class has functions to get set using the matrix representation, but internally it is stored as position, rotation and scale.

OK, thanks, I will need 3 p/invokes in order to copy my matrix to the Unity transform then I guess (plus matrix rotation → quaternion conversion). I’ve come to shy away from p/invokes in .NET because of performance issues, but maybe it’s not as bad in mono. I’ll try to do some benchmarking later this week.

So let me get this straight - internally, the engine is storing vector+vector+quaternion rather than a matrix? Do you have to convert back and forth to matrices to do parent-child matrix multiplication, or do you use other algorithms?

Thanks!

But why do you want to operate on matrices?

My .NET engine uses matrices, and I’m trying to find ways to reduce overhead in using Unity as the backend (currently it’s mainly Managed DirectX). A simple copy of the elements would be the most efficient way to accomplish that if Unity uses matrices internally too.

Ok. Well, in that case you’re out of luck :slight_smile: Unity does not use matrices internally (of course it does ultimately hand out matrices to the 3D API, but they are not stored in Transform).

As a side experiment, you might want to draw everything using Graphics.DrawMesh, and not create visible objects in the scene. This call just takes a mesh, a matrix, a material and makes it appear in the scene. Well, it’s actually a bit more complex.

Cool, thanks! Sounds like a good intermediate step for a quick adaption of my engine. Later on I might well switch to quaternions myself, will look more into that once I have things up and running.

Trying this now, but getting some strange results.
Could it be that the matrix is “rotated” compared to most implementations of 4x4 3D matrices? I’m used to seeing translation as being elements m30, m31, m32 (0-based), but here it seems it could be m03, m13, m23…?

Yes. Basically, matrix is indexed using (row,column) as usual. But the way matrices are written follows OpenGL style notation; where origin is the 4th column (and three axis vectors are first three columns etc.).

Ah, gotcha. Thx!