Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Shader not working after Unity update

Discussion in 'Shaders' started by JussiKremant, Jan 23, 2017.

  1. JussiKremant

    JussiKremant

    Joined:
    Sep 28, 2015
    Posts:
    4
    Hi! I ran in to a little problem when opening an old project in unity (5.5), and that is that my shader stopped working. Before it drew circles on the on the materials based on the distance from the camera as seen here: https://gfycat.com/FeminineBelatedCuscus.

    But now it's just dark, anybody got an idea why that is?

    The code is
    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2.  
    3. Shader "Custom/HihglightShader"
    4. {
    5.     SubShader
    6.     {
    7.         Pass
    8.         {
    9.             CGPROGRAM
    10.             #pragma target 3.0  
    11.             #pragma vertex vert
    12.             #pragma fragment frag
    13.             #include "UnityCG.cginc"
    14.             const static int _NumberOfHighlights = 30;
    15.            
    16.             //float2 arrays because for some reason float  arrays doesn't work, seems to be a bug.
    17.             float2 _HighlightWidth[_NumberOfHighlights];
    18.             float2 _HighlightDistance[_NumberOfHighlights];
    19.             float4 _BackgroundColor;
    20.             float4 _HighlightColor[_NumberOfHighlights];
    21.             float4 _HighlightStartPosition[_NumberOfHighlights];
    22.  
    23.             struct v2f
    24.             {
    25.                 float4 position : SV_POSITION;
    26.                 float4 worldSpacePosition : TEXCOORD0;
    27.             };
    28.  
    29.             v2f vert(appdata_full vertIn)
    30.             {
    31.                 v2f o;
    32.                 o.position = mul(UNITY_MATRIX_MVP, vertIn.vertex);
    33.                 o.worldSpacePosition = mul(unity_ObjectToWorld, vertIn.vertex);
    34.                 return o;
    35.             }
    36.  
    37.             float4 frag(v2f fragIn) : COLOR
    38.             {
    39.                
    40.                 float4 pixelWorldSpacePosition = fragIn.worldSpacePosition; //The pixel position in the world grid
    41.                 float4 c_out = _BackgroundColor;
    42.  
    43.                 for (int i = 0; i < _NumberOfHighlights; i++)
    44.                 {
    45.                     //The pixel distance from the current higlights starting position, if the distance is 0, the width has been set to 0 through script.
    46.                     float _PixelDistance = length(pixelWorldSpacePosition - _HighlightStartPosition[i]);
    47.                     //_Colordifference is calculated to make shure the highlights gets the correct color if the background isn't black.
    48.                     float4 _ColorDifference = _HighlightColor[i] - _BackgroundColor;
    49.                    
    50.                     //The first step function makes shure no color is added to the backgorund before _HighlightDistance[i].x, then a smoothstep to add a fade over the highlight and last one more step function to make shur no color is added after the highlight.
    51.                     c_out += _ColorDifference *step(_HighlightDistance[i].x, _PixelDistance) * smoothstep(_HighlightDistance[i].x, _HighlightDistance[i].x + _HighlightWidth[i].x, _PixelDistance) * step(_PixelDistance, _HighlightDistance[i].x + _HighlightWidth[i].x);
    52.                 }
    53.                 return c_out;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    59.  
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    How about:
    Code (csharp):
    1.  
    2. float _PixelDistance = length(pixelWorldSpacePosition.xyz - _HighlightStartPosition[i].xyz);
    3.  
     
  3. JussiKremant

    JussiKremant

    Joined:
    Sep 28, 2015
    Posts:
    4
    Unfortunately that didn't solve it
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    Are you sure it's the shader? How were you setting the values in the arrays? If you were using SetFloat() or SetVector() there are proper SetFloatArray() functions now.
     
  5. StevenDelrue

    StevenDelrue

    Joined:
    Aug 21, 2014
    Posts:
    16
    I have a similar issue, the same setup on OSX works fine, but on windows it looks wrong. I'm pretty sure it has something to do with the depth texture:

    Windows 10


    OSX:


    Code (CSharp):
    1. Shader "Test/Render Depth"
    2. {
    3.     Properties
    4.     {
    5.         _RegularColor("Main Color", Color) = (1, 1, 1, .5)
    6.         _HighlightColor("Highlight Color", Color) = (1, 1, 1, .5)
    7.         _HighlightThresholdMax("Highlight Threshold Max", Float) = 1
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "Queue" = "Transparent" "RenderType"="Transparent"  }
    12.         Pass
    13.         {
    14.             Blend SrcAlpha OneMinusSrcAlpha
    15.             ZWrite Off
    16.             Cull Off
    17.             CGPROGRAM
    18.             #pragma target 3.0
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #include "UnityCG.cginc"
    22.             uniform sampler2D_float _CameraDepthTexture;
    23.             uniform float4 _RegularColor;
    24.             uniform float4 _HighlightColor;
    25.             uniform float _HighlightThresholdMax;
    26.             struct v2f
    27.             {
    28.                 float4 pos : SV_POSITION;
    29.                 float4 projPos : TEXCOORD1;
    30.             };
    31.             v2f vert(appdata_base v)
    32.             {
    33.                 v2f o;
    34.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    35.                 o.projPos = ComputeScreenPos(o.pos);
    36.                 return o;
    37.             }
    38.             half4 frag(v2f i) : COLOR
    39.             {
    40.                 float4 finalColor = _RegularColor;
    41.                 float sceneZ = LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);
    42.                 float partZ = i.projPos.z;
    43.                 float diff = (abs(sceneZ - partZ)) / _HighlightThresholdMax;
    44.                 if(diff <= 1)
    45.                 {
    46.                     finalColor = lerp(_HighlightColor, _RegularColor, float4(diff, diff, diff, diff));
    47.                 }
    48.  
    49.                 half4 c = diff;
    50.                
    51.                 return c;
    52.             }
    53.             ENDCG
    54.         }
    55.     }
    56.     FallBack "VertexLit"
    57. }
     
  6. JussiKremant

    JussiKremant

    Joined:
    Sep 28, 2015
    Posts:
    4
    I've tried to rewrite the shader using fixed values to avoid any problem like that, but it still doesn't work.

    The modified code:
    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2.  
    3. Shader "Custom/HihglightShader"
    4. {
    5.     SubShader
    6.     {
    7.         Pass
    8.         {
    9.             CGPROGRAM
    10.             #pragma target 3.0  
    11.             #pragma vertex vert
    12.             #pragma fragment frag
    13.             #include "UnityCG.cginc"
    14.             const static int _NumberOfHighlights = 30;
    15.            
    16.             //float2 arrays because for some reason float  arrays doesn't work, seems to be a bug.
    17.             float2 _HighlightWidth[_NumberOfHighlights];
    18.             float2 _HighlightDistance[_NumberOfHighlights];
    19.             float4 _BackgroundColor;
    20.             float4 _HighlightColor[_NumberOfHighlights];
    21.             float4 _HighlightStartPosition[_NumberOfHighlights];
    22.  
    23.             struct v2f
    24.             {
    25.                 float4 position : SV_POSITION;
    26.                 float4 worldSpacePosition : TEXCOORD0;
    27.             };
    28.  
    29.             v2f vert(appdata_full vertIn)
    30.             {
    31.                 v2f o;
    32.                 o.position = mul(UNITY_MATRIX_MVP, vertIn.vertex);
    33.                 o.worldSpacePosition = mul(unity_ObjectToWorld, vertIn.vertex);
    34.                 return o;
    35.             }
    36.  
    37.             float4 frag(v2f fragIn) : COLOR
    38.             {
    39.                
    40.                 float4 pixelWorldSpacePosition = fragIn.worldSpacePosition; //The pixel position in the world grid
    41.                 float4 c_out = _BackgroundColor;
    42.  
    43.                 for (int i = 0; i < _NumberOfHighlights; i++)
    44.                 {
    45.                     //The pixel distance from the current higlights starting position, if the distance is 0, the width has been set to 0 through script.
    46.                     float _PixelDistance = length(pixelWorldSpacePosition.xyz - _HighlightStartPosition[i].xyz);
    47.                     //_Colordifference is calculated to make shure the highlights gets the correct color if the background isn't black.
    48.                     float4 _ColorDifference = _HighlightColor[i] - _BackgroundColor;
    49.                    
    50.                     //The first step function makes shure no color is added to the backgorund before _HighlightDistance[i].x, then a smoothstep to add a fade over the highlight and last one more step function to make shur no color is added after the highlight.
    51.                     c_out += float4(1, 0, 1, 0) *step(5, _PixelDistance) * smoothstep(5, 5 + 1, _PixelDistance) * step(_PixelDistance, 5 + 1);
    52.                 }
    53.                 return c_out;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    59.  
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    That's going to make things worse. Going forward you should use UnityObjectToClipPos(vertPos); instead of mul(UNITY_MATRIX_MVP, vertpos); but that's totally different than world position.
     
  8. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Indeed, it should be the o.pos that is set to that. I shouldn't be replying when I'm sleep deprived haha. Their shader seems to work for me though, but I removed the arrays to test it with just a single point.

    I think you need to check if the platform is reversed depth buffer.
    Code (CSharp):
    1. float sceneZ = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);
    2. #ifdef UNITY_REVERSED_Z
    3.     sceneZ = 1 - sceneZ;
    4. #endif
     
    Last edited: Jan 31, 2017
  9. JussiKremant

    JussiKremant

    Joined:
    Sep 28, 2015
    Posts:
    4
    After some poking around in the scripts it turns out you where right, and now it all works! Thank you all how took the time to help me =)
     
  10. RefugeZer0

    RefugeZer0

    Joined:
    Jan 28, 2018
    Posts:
    4
    After upgrading from Unity5 to Unity2017 I had a similar issue.

    In one case I had to undo the automatic update to my shader:
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2. //o.vertex = UnityObjectToClipPos( v.vertex );  // <-- Unity added this
    3. o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);  // <-- original code, Unity removed this
    In another case with a different shader I had to disable MSAA on my camera.

    I haven't investigated either of these issues, but this was how I fixed it.