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): Shader "Custom/NewSurfaceShader" { SubShader { Tags{ "RenderType" = "Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; }; struct v2f { float4 vertex : SV_POSITION; float clipSpaceZ : TEXCOORD0; }; float4x4 _ModelToTangoDepthCameraSpace; v2f vert(appdata v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.clipSpaceZ = o.vertex.z; return o; } float4 frag(v2f i) : SV_Target { return float4(i.vertex.z,0,0,1); //float biasedUnprojectedDepth = i.clipSpaceZ + 128; // //float isNegative = 0; //if(biasedUnprojectedDepth < 0) //{ // biasedUnprojectedDepth = biasedUnprojectedDepth * -1; // isNegative = 1; //} // //float integerPart; //float fractionPart = modf(biasedUnprojectedDepth, integerPart); // //integerPart = saturate(integerPart / 255.0); // ////store integer part in red, and fraction in green. //return float4(integerPart, fractionPart, 0, 1); } ENDCG } } }
Could it be related to the "Shaders: Z-buffer float inverted" change? https://docs.unity3d.com/550/Documentation/Manual/UpgradeGuide55.html
Thank you so much. It helps. And also the projection matrix of the camera is changed. But I can convert it to the original one.
The shader is fixed by the code below. Code (csharp): #if defined(UNITY_REVERSED_Z) o.clipSpaceZ = UNITY_Z_0_FAR_FROM_CLIPSPACE(o.vertex.z); #endif 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): Matrix4x4 projectionMatrix = GL.GetGPUProjectionMatrix(EmulatedEnvironmentRenderHelper.m_emulationCamera.projectionMatrix, false); #if UNITY_5_5 projectionMatrix.m22 = -projectionMatrix.m22 - 1; projectionMatrix.m23 = -projectionMatrix.m23; #endif