Simply get the scaling of an object inside the Cg shader

5.9k Views Asked by At

Say you have a trivial Unity shader. It does not at all use any texture. It grabs simply the position ..

void vert (inout appdata_full v, out Input o) 
{
    UNITY_INITIALIZE_OUTPUT(Input,o);
    o.localPos = v.vertex.xyz;
}

and then draws a square ..

enter image description here

the quad in the example has been stretch about 3:1 using the transform.

If in the shader we simply knew the scaling (or, just the ratios) we could very easily draw "square squares"

enter image description here

This is obviously a common, everyday technique for things like billboarding, 2D images and backgrounds etc.

In current unity (2018) how the heck do you simply get the current scaling of the object, in Cg?

This is one of those crazy Unity things that is (1) totally undocumented (2) where the only information available about it is as much as 13 years old, I mean some of the folks involved may be deceased (3) it has changed drastically in different Unity versions, so often discussion about it is just totally wrong. Sigh.

How to do it? Can you?

Currently I just have a trivial script pass in the value, which is OK but a bit shoddy.

1

There are 1 best solutions below

4
On BEST ANSWER

The scale of the transform is baked into the world matrix. If your object is not rotated then you can fetch it directly with a little bit of swizzling, but most likely you want to do something like this:

half3 ObjectScale() {
return half3(
    length(unity_ObjectToWorld._m00_m10_m20),
    length(unity_ObjectToWorld._m01_m11_m21),
    length(unity_ObjectToWorld._m02_m12_m22)
);

}

heads-up: this implementation is dependent on the API, you might need to use some DEFINE to deal with this in DX/OGL, since the matrix format is different (row vs column order).

there's also different way to access matrices components: https://learn.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-per-component-math

like the examples in this thread https://forum.unity.com/threads/can-i-get-the-scale-in-the-transform-of-the-object-i-attach-a-shader-to-if-so-how.418345/