x


How do I set the global scale of an object in a transform hierachy?

I've written a COLLADA importer that uses the element of the COLLADA file to define rotation, translation, and scale values for transforms. The values of the matrix are as follows:

xx,xy,xz,t1,yx,yy,yz,t2,zx,zy,zz,t3,0,0,0,1

Rotation and translation are working correctly by creating the coordinate basis from the x,y, and z axes, and taking the x,y, and z position values. The problem is that scale values, which are derived from the magnitude of the basis vectors represent scaling in the global coordinate system (not the local system as one would assume). This is a problem because after creating the transform, parenting it, and setting it's translation and rotation, the local system is rotated and setting transform.localScale (the only scale property available) applies the global scale values to the local system.

How do I set a transform's global scale in a transform hierarchy?

more ▼

asked Sep 14 '10 at 05:06 PM

ikeo gravatar image

ikeo
136 9 11 18

These are naive brute force approaches: Could you not just traverse the hierarchy and compute local scale relative to the parents? Even more simply, could you not store the parent, detach from it, scale and then re-attach to it?

Sep 14 '10 at 09:17 PM skovacs1

Do you mean you're writing your own COLLADA-to-Unity importer? Why? Unity can import COLLADA files already...

Sep 15 '10 at 02:05 PM Paulius Liekis

It's part of my app that downloads 3D models and builds them in Unity at run-time. So I needed a script to parse COLLADA models. Also, my script reads extra information inside the COLLADA.

Sep 16 '10 at 12:53 AM ikeo
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Maybe it will be helpful:

function GetParentScale(): Vector3 {
    var sf: Vector3 = Vector3.one;

    var parentTransform: Transform = null;
    if (thisTransform.parent) parentTransform = thisTransform.parent;

    while (parentTransform) {
        sf.x *= 1.0f / parentTransform.localScale.x;
        sf.y *= 1.0f / parentTransform.localScale.y;
        sf.z *= 1.0f / parentTransform.localScale.z;
        if (parentTransform.parent) {
            parentTransform = parentTransform.parent;
        } else {
            parentTransform = null;
        }
    }
    return sf;      
}


function SetScale(globalScale: Vector3) {
    var parentGlobalScale: Vector3 = GetParentScale();
    thisTransform.localScale.x = globalScale.x * parentGlobalScale.x;
    thisTransform.localScale.y = globalScale.y * parentGlobalScale.y;
    thisTransform.localScale.z = globalScale.z * parentGlobalScale.z;
}

where thisTransform = gameObject.transform

more ▼

answered Apr 18 '11 at 10:30 AM

Alex 17 gravatar image

Alex 17
2 1 1 1

(comments are locked)
10|3000 characters needed characters left

It was much easier than I was making it out to be. The COLLADA matrices make complete sense if you decompose them and set the localPosition, localScale, and localRotation as follows:

Vector3 x = new Vector3((float)matrix[0], (float)matrix[4], (float)matrix[8]);
Vector3 y = new Vector3((float)matrix[1], (float)matrix[5], (float)matrix[9]);
Vector3 z = new Vector3((float)matrix[2], (float)matrix[6], (float)matrix[10]);
float xScale         = x.magnitude;
float yScale        = y.magnitude;
float zScale        = z.magnitude;

newParentTrans.localPosition = new Vector3((float)matrix[3] * conversion,
                                                            (float)matrix[7] * conversion,
                                                            (float)matrix[11] * conversion);
newParentTrans.localScale = new Vector3(xScale, yScale, zScale);

Quaternion rotation = newParentTrans.rotation;
rotation.SetLookRotation(z/zScale,y/yScale);
newParentTrans.localRotation = rotation;

Because well-formed COLLADA files have one top-level element, you can rotate the entire scene by -90 deg around the x axis, if Z_UP is true.

more ▼

answered Sep 18 '10 at 03:12 AM

ikeo gravatar image

ikeo
136 9 11 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1331
x401
x151
x127

asked: Sep 14 '10 at 05:06 PM

Seen: 3574 times

Last Updated: Sep 16 '10 at 02:46 AM