Search Unity

Shader result is different between 5.3.4f1 and 5.6.0f1

Discussion in 'Shaders' started by stevenchan_playstudios, Mar 23, 2017.

  1. stevenchan_playstudios

    stevenchan_playstudios

    Joined:
    Mar 1, 2017
    Posts:
    39
    payline_5.3.PNG payline_5.6.PNG

    Code (CSharp):
    1. Shader "PaylineFlow"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Main Color", Color) = (1,1,1,1)
    6.         _DiffuseTex("Diffuse Mask Texture", 2D) = "white" {}
    7.         _GlowColor("Glow Color", Color) = (1,1,1,1)
    8.         _GlowTex("Glow Mask Texture", 2D) = "white" {}
    9.         _MaskTex("Alpha Texture", 2D) = "white" {}
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags{ "Queue" = "Transparent" }
    15.  
    16.         Blend SrcAlpha OneMinusSrcAlpha
    17.  
    18.         Pass
    19.         {
    20.  
    21.             CGPROGRAM
    22.     #pragma vertex v
    23.     #pragma fragment p
    24.  
    25.             sampler2D _DiffuseTex;
    26.             sampler2D _GlowTex;
    27.             float4 _DiffuseTex_ST;
    28.  
    29.             sampler2D _MaskTex;
    30.             float4 _MaskTex_ST;
    31.  
    32.             float4 _FlowVector;
    33.             float4 _Color;
    34.             float4 _GlowColor;
    35.  
    36.             struct VertOut
    37.             {
    38.                 float4 position : POSITION;
    39.                 float2 uv : TEXCOORD0;
    40.             };
    41.  
    42.             VertOut v(float4 position : POSITION, float3 norm : NORMAL, float2 uv : TEXCOORD0)
    43.             {
    44.                 VertOut OUT;
    45.  
    46.                 OUT.position = mul(UNITY_MATRIX_MVP, position);
    47.                 OUT.uv = uv;
    48.  
    49.                 return OUT;
    50.             }
    51.  
    52.             struct PixelOut
    53.             {
    54.                 float4 color : COLOR;
    55.             };
    56.  
    57.             PixelOut p(VertOut input)
    58.             {
    59.                 PixelOut OUT;
    60.  
    61.                 float2 maskUV = input.uv *_MaskTex_ST.xy + _MaskTex_ST.zw;
    62.                 float4 diffuseColor = tex2D(_DiffuseTex, input.uv);
    63.                 float4 glowColor = tex2D(_GlowTex, input.uv);
    64.                 float4 maskColor = tex2D(_MaskTex, input.uv);
    65.  
    66.                 float4 finalColor = float4(diffuseColor.rgb * _Color.rgb + glowColor * _GlowColor.rgb, maskColor.a);
    67.  
    68.                 OUT.color = finalColor;
    69.  
    70.                 return OUT;
    71.             }
    72.             ENDCG
    73.         }
    74.     }
    75.        
    76.     FallBack "Diffuse"
    77. }
     

    Attached Files:

  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,723
    What are you trying to do?

    In all honesty, the result in 5.6 seems like the correct one.

    I think the ambiguity comes from doing glowColor * _GlowColor.rgb, which is a float4 multiplied with a float3 and maybe is cast differently in each version?
     
  3. stevenchan_playstudios

    stevenchan_playstudios

    Joined:
    Mar 1, 2017
    Posts:
    39
    In Unity5.6, if turn on scene lighting, the main color seems appear again in scene view.
    Payline_Scene_5.6.PNG
     
  4. stevenchan_playstudios

    stevenchan_playstudios

    Joined:
    Mar 1, 2017
    Posts:
    39
    Any Unity guys can help me ?
     
  5. Matt-Roper

    Matt-Roper

    <Of The Graphics> Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    106
    Hey Steven,

    Can you verify that your HDR is enabled in the graphics settings?

     
  6. stevenchan_playstudios

    stevenchan_playstudios

    Joined:
    Mar 1, 2017
    Posts:
    39
    Nothing change if HDR is enabled or not
     
  7. stevenchan_playstudios

    stevenchan_playstudios

    Joined:
    Mar 1, 2017
    Posts:
    39
    However, if the MainCamera 's "Allow HDR" is disabled, the Scene and Game window become identical.
     
  8. stevenchan_playstudios

    stevenchan_playstudios

    Joined:
    Mar 1, 2017
    Posts:
    39
    Finally, the shader result is different when both Camera -> Allow HDR is checked and GraphicsSettings Tier settings ->Android->Tier->Use HDR is checked.
     
    Last edited: Apr 21, 2017
  9. stevenchan_playstudios

    stevenchan_playstudios

    Joined:
    Mar 1, 2017
    Posts:
    39
    It is a simpified version of shader that only have two colors and two textures, the result of frag is different between Unity5.3.x and Unity5.6.x

    Code (CSharp):
    1. Shader "PaylineFlow"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Main Color", Color) = (1,1,1,1)
    6.         _DiffuseTex("Diffuse Mask Texture", 2D) = "white" {}
    7.         _GlowColor("Glow Color", Color) = (1,1,1,1)
    8.         _GlowTex("Glow Mask Texture", 2D) = "white" {}
    9.     }
    10.     SubShader
    11.     {
    12.         Tags{ "Queue" = "Transparent" }
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.             sampler2D _DiffuseTex;
    21.             sampler2D _GlowTex;
    22.             float4 _DiffuseTex_ST;
    23.             fixed4 _Color;
    24.             fixed4 _GlowColor;
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float4 vertex : SV_POSITION;
    34.                 float2 uv : TEXCOORD0;
    35.             };
    36.             v2f vert(appdata v)
    37.             {
    38.                 v2f o;
    39.                 o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
    40.                 o.uv = v.uv;
    41.                 return o;
    42.             }
    43.             fixed4 frag(v2f i) : SV_Target
    44.             {
    45.                 fixed4 Color = tex2D(_DiffuseTex, i.uv);
    46.                 fixed4 glowColor = tex2D(_GlowTex, i.uv);
    47.                 return Color * _Color + glowColor * _GlowColor;
    48.             }
    49.             ENDCG
    50.         }
    51.     }
    52. }
     
  10. Andre_Mcgrail

    Andre_Mcgrail

    Unity Technologies

    Joined:
    Dec 9, 2016
    Posts:
    244
    Hey Steven,

    That latest shader you posted looks identical on both 5.3 and 5.6, the change in frag is due to us moving on from using mul(UNITY_MATRIX_MVP, * ) as it more expensive than the newer UnityObjectToClipPos( * ) in 5.6 and newer our shader compiler automatically replaces this code.

    As for the initial issue is its as Matt-Roper said, the HDR setting causes the shaders colour calculation to be vastly different, this is because in the shader your adding two colours together, this will produce values over one, but then you are using an alpha to bring the final screen pixels way down.

    Here is a gif of the two above shaders in 5.3.6f1 and 5.6.0p2:
    HDR clamping.gif

    5.3.6f1 on the left and 5.6.0p2 on the right, I'm switching the cameras HDR bool on and off and also switching the scene lighting on and off(like in your second post)

    The current way this shader does the outline is a hack that is relying on the camera to render HDR values, I have made adjustments to the shader so that it acts like it does in HDR in nonHDR as well, this is done by multiplying the rgb values with the alpha instead of using the alpha to mask the rgb values, I have also changed the blend type to additive

    Code (CSharp):
    1. Shader "PaylineFlow"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Main Color", Color) = (1,1,1,1)
    6.         _DiffuseTex("Diffuse Mask Texture", 2D) = "white" {}
    7.         _GlowColor("Glow Color", Color) = (1,1,1,1)
    8.         _GlowTex("Glow Mask Texture", 2D) = "white" {}
    9.         _MaskTex("Alpha Texture", 2D) = "white" {}
    10.     }
    11.     SubShader
    12.     {
    13.         Tags{ "Queue" = "Transparent" }
    14.         Blend One One
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.     #pragma vertex v
    19.     #pragma fragment p
    20.             sampler2D _DiffuseTex;
    21.             sampler2D _GlowTex;
    22.             float4 _DiffuseTex_ST;
    23.             sampler2D _MaskTex;
    24.             float4 _MaskTex_ST;
    25.             float4 _FlowVector;
    26.             float4 _Color;
    27.             float4 _GlowColor;
    28.             struct VertOut
    29.             {
    30.                 float4 position : POSITION;
    31.                 float2 uv : TEXCOORD0;
    32.             };
    33.             VertOut v(float4 position : POSITION, float3 norm : NORMAL, float2 uv : TEXCOORD0)
    34.             {
    35.                 VertOut OUT;
    36.                 OUT.position = mul(UNITY_MATRIX_MVP, position);
    37.                 OUT.uv = uv;
    38.                 return OUT;
    39.             }
    40.             struct PixelOut
    41.             {
    42.                 float4 color : COLOR;
    43.             };
    44.             PixelOut p(VertOut input)
    45.             {
    46.                 PixelOut OUT;
    47.                 float2 maskUV = input.uv *_MaskTex_ST.xy + _MaskTex_ST.zw;
    48.                 float4 diffuseColor = tex2D(_DiffuseTex, input.uv);
    49.                 float4 glowColor = tex2D(_GlowTex, input.uv);
    50.                 float4 maskColor = tex2D(_MaskTex, input.uv);
    51.                 float4 finalColor = float4((diffuseColor.rgb * _Color.rgb + glowColor * _GlowColor.rgb) * maskColor.a, 1);
    52.                 OUT.color = finalColor;
    53.                 return OUT;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58.      
    59.     FallBack "Diffuse"
    60. }
    Let me know how this works for you.

    Cheers,
    Andre
    Unity Gfx Test Engineer
     
  11. Andre_Mcgrail

    Andre_Mcgrail

    Unity Technologies

    Joined:
    Dec 9, 2016
    Posts:
    244
    Also here is the second shader tweaked

    Code (CSharp):
    1. Shader "PaylineFlow"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Main Color", Color) = (1,1,1,1)
    6.         _DiffuseTex("Diffuse Mask Texture", 2D) = "white" {}
    7.         _GlowColor("Glow Color", Color) = (1,1,1,1)
    8.         _GlowTex("Glow Mask Texture", 2D) = "white" {}
    9.     }
    10.     SubShader
    11.     {
    12.         Tags{ "Queue" = "Transparent" }
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             sampler2D _DiffuseTex;
    20.             sampler2D _GlowTex;
    21.             float4 _DiffuseTex_ST;
    22.             fixed4 _Color;
    23.             fixed4 _GlowColor;
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.             };
    29.             struct v2f
    30.             {
    31.                 float4 vertex : SV_POSITION;
    32.                 float2 uv : TEXCOORD0;
    33.             };
    34.             v2f vert(appdata v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = UnityObjectToClipPos(v.vertex);
    38.                 o.uv = v.uv;
    39.                 return o;
    40.             }
    41.             fixed4 frag(v2f i) : SV_Target
    42.             {
    43.                 fixed4 Color = tex2D(_DiffuseTex, i.uv) * _Color;
    44.                 fixed4 glowColor = tex2D(_GlowTex, i.uv) * _GlowColor;
    45.                 return (Color * Color.a) + glowColor;
    46.             }
    47.             ENDCG
    48.         }
    49.     }
    50. }
    Cheers,
    Andre
    Gfx Test Engineer
     
    Matt-Roper likes this.
  12. stevenchan_playstudios

    stevenchan_playstudios

    Joined:
    Mar 1, 2017
    Posts:
    39
    I tried both tweaked shaders,
    the first tweaked shader with Alpha Texture looks to bright and incorrect with some color combination.
    the second shader without the Alpha Texture looks similar to my original shader result.

    Although it is not perfect, it can show two color is fine to us.
    Thank you for your help.
     
    Last edited: May 2, 2017
    Matt-Roper likes this.