Search Unity

  1. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice

5.5 shader problem

Discussion in '5.5 Beta' started by Seto, Oct 29, 2016.

  1. Seto

    Seto

    Joined:
    Oct 10, 2010
    Posts:
    232
    Here's a simple shader. Which is originally used to render the emulation depth texture for Tango development. But this shader doesn't work like previous. I modified the shader to check the difference. I found that I apply the material with this shader to a simple cube.
    The color in 5.4.x is light red. Instead, in 5.5.x is dark red. What leads to the difference? And how can I fix it? Thanks.

    Code (csharp):
    1.  
    2. Shader "Custom/NewSurfaceShader" {
    3.     SubShader
    4.     {
    5.         Tags{ "RenderType" = "Opaque" }
    6.         LOD 100
    7.  
    8.         Pass
    9.     {
    10.         CGPROGRAM
    11. #pragma vertex vert
    12. #pragma fragment frag
    13.  
    14. #include "UnityCG.cginc"
    15.  
    16.         struct appdata
    17.     {
    18.         float4 vertex : POSITION;
    19.     };
    20.  
    21.     struct v2f
    22.     {
    23.         float4 vertex : SV_POSITION;
    24.         float clipSpaceZ : TEXCOORD0;
    25.     };
    26.  
    27.     float4x4 _ModelToTangoDepthCameraSpace;
    28.  
    29.     v2f vert(appdata v)
    30.     {
    31.         v2f o;
    32.         o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    33.         o.clipSpaceZ = o.vertex.z;
    34.         return o;
    35.     }
    36.  
    37.     float4 frag(v2f i) : SV_Target
    38.     {
    39.         return float4(i.vertex.z,0,0,1);
    40.     //float biasedUnprojectedDepth = i.clipSpaceZ + 128;
    41.     //
    42.     //float isNegative = 0;
    43.     //if(biasedUnprojectedDepth < 0)
    44.     //{
    45.     //    biasedUnprojectedDepth = biasedUnprojectedDepth * -1;
    46.     //    isNegative = 1;
    47.     //}
    48.     //
    49.     //float integerPart;
    50.     //float fractionPart = modf(biasedUnprojectedDepth, integerPart);
    51.     //
    52.     //integerPart = saturate(integerPart / 255.0);
    53.     //
    54.     ////store integer part in red, and fraction in green.
    55.     //return float4(integerPart, fractionPart, 0, 1);
    56.     }
    57.         ENDCG
    58.     }
    59.     }
    60. }
    61.  
     
    Last edited: Oct 29, 2016
  2. Seto

    Seto

    Joined:
    Oct 10, 2010
    Posts:
    232
    And also it seems that the clipSpaceZ of v2f struct is not working any more in 5.5.x.
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,589
  4. Seto

    Seto

    Joined:
    Oct 10, 2010
    Posts:
    232
  5. Seto

    Seto

    Joined:
    Oct 10, 2010
    Posts:
    232
    The shader is fixed by the code below.
    Code (csharp):
    1.  
    2. #if defined(UNITY_REVERSED_Z)
    3.                 o.clipSpaceZ = UNITY_Z_0_FAR_FROM_CLIPSPACE(o.vertex.z);
    4. #endif
    5.  
    But the projectionMatrix is not mentioned in the upgrade guide. I fixed the projectionMatrix by the code below. Is it right? Thank you.
    Code (csharp):
    1.  
    2.             Matrix4x4 projectionMatrix = GL.GetGPUProjectionMatrix(EmulatedEnvironmentRenderHelper.m_emulationCamera.projectionMatrix, false);
    3. #if UNITY_5_5
    4.             projectionMatrix.m22 = -projectionMatrix.m22 - 1;
    5.             projectionMatrix.m23 = -projectionMatrix.m23;
    6. #endif
    7.  
     
    Last edited: Oct 29, 2016
unityunity