[SOLVED] Unity 5.6.0b4 Use of UNITY_MATRIX_XXX vs UnityObjectToClipPos

Hi everyone,

in previous version of unity I used the following like to get the object position with a offset.

 float3 pivot =
-mul(UNITY_MATRIX_V, mul(unity_ObjectToWorld, float4(v.vertex.xyz - float3(0, _Scale.y * 0.5, 0), 1)));

Now in in 5.6.bf4 using :

float3 pivot = UnityObjectToClipPos(v.vertex - float3(0, _Scale.y * 0.5, 0));
// Worked perfectly in 5.4.0f1

Cause completely wrong results.

Does any one know to use UnityObjectToClipPos() in the correct context to get pivot correctly in meters?
Thanks a lot.

https://forum.unity3d.com/threads/unityobjecttoclippos.400520/
?

This is a about an year old stuff :slight_smile:
The result is wrong, not the shader call.

Through the new inline definitions in the UnityCG.cgnic you must take care of the swizzle commands.

float3 pivot = -UnityObjectToClipPos(v.vertex.xyz - float3(0, _Scale.y * 0.5, 0));

1 Like

In the newest UnityCG it takes care of the swizzle by having a float4 input version that calls the float3 using input.xyz.
I think your problem was actually that you were trying to subtract a float3 from a float4(vertex), and thus had to define a float3 result to subtract from with .xyz.

UnityCG:

// Tranforms position from object to homogenous space
inline float4 UnityObjectToClipPos( in float3 pos )
{
    // More efficient than computing M*VP matrix product
    return mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(pos, 1.0)));
}
inline float4 UnityObjectToClipPos(float4 pos) // overload for float4; avoids "implicit truncation" warning for existing shaders
{
    return UnityObjectToClipPos(pos.xyz);
}

The strange thing is

float3 pivot = UnityObjectToClipPos(v.vertex - float3(0, _Scale.y * 0.5, 0));

Worked perfect in previous build of 5.4.xx

Now I have to invert the result?

float3 pivot = -UnityObjectToClipPos(v.vertex.xyz - float3(0, _Scale.y * 0.5, 0));

Perhaps it has anything to do with new HSL compiler, new unified the transforming, or inverted zbuffer.

I have some examples where UnityObjectToClipPos does not work generally under dx11 and does not write into the depth.

Stange, it’s been the opposite for me. It fixed the MVP method not correctly writing to the depth buffer when used as in a forward rendered shader under deferred player mode. Haven’t seen any other issues now. If you can reproduce that it would make a good bug report.