Search Unity

[SOLVED] Unity 5.6.0b4 Use of UNITY_MATRIX_XXX vs UnityObjectToClipPos

Discussion in 'Shaders' started by Quatum1000, Jan 20, 2017.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Hi everyone,

    in previous version of unity I used the following like to get the object position with a offset.
    Code (CSharp):
    1.  float3 pivot =
    2. -mul(UNITY_MATRIX_V, mul(unity_ObjectToWorld, float4(v.vertex.xyz - float3(0, _Scale.y * 0.5, 0), 1)));
    3.  
    Now in in 5.6.bf4 using :
    Code (CSharp):
    1. float3 pivot = UnityObjectToClipPos(v.vertex - float3(0, _Scale.y * 0.5, 0));
    2. // Worked perfectly in 5.4.0f1
    3.  
    Cause completely wrong results.

    Does any one know to use UnityObjectToClipPos() in the correct context to get pivot correctly in meters?
    Thanks a lot.
     
    Last edited: Jan 20, 2017
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    https://forum.unity3d.com/threads/unityobjecttoclippos.400520/
    ?
     
  3. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
  4. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    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));
     
    Invertex likes this.
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    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:
    Code (CSharp):
    1. // Tranforms position from object to homogenous space
    2. inline float4 UnityObjectToClipPos( in float3 pos )
    3. {
    4.     // More efficient than computing M*VP matrix product
    5.     return mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(pos, 1.0)));
    6. }
    7. inline float4 UnityObjectToClipPos(float4 pos) // overload for float4; avoids "implicit truncation" warning for existing shaders
    8. {
    9.     return UnityObjectToClipPos(pos.xyz);
    10. }
     
  6. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    The strange thing is
    Code (CSharp):
    1. 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?
    Code (CSharp):
    1. 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.
     
    Last edited: Jan 22, 2017
  7. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    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.