Search Unity

Shader Rendering Error on iOS

Discussion in 'Shaders' started by michaelplzno, Sep 12, 2020.

  1. michaelplzno

    michaelplzno

    Joined:
    Dec 12, 2017
    Posts:
    49
    HexError.png

    Hi! I have a grid of "Hex Cubes" I'm rendering on iOS and PC/Android. The PC/Android rendering is correct but iOS is wrong, the left in the image above is correct, the right is wrong.

    Here is the shader I'm using:

    Code (CSharp):
    1. Shader "Custom/VertexColor"
    2. {
    3.     Properties{
    4.         _MainTex("Texture", 2D) = "white" { }
    5.     }
    6.  
    7.     SubShader
    8.     {  
    9.         Tags { "Queue" = "Transparent" }
    10.         Blend SrcAlpha OneMinusSrcAlpha
    11.         //Cull Off
    12.              
    13.         CGPROGRAM
    14.         #pragma surface surf SimpleLambert alpha:blend
    15.  
    16.         half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) {
    17.             half NdotL = .5 + .5 * dot(s.Normal, lightDir);
    18.             NdotL = NdotL * NdotL;
    19.             NdotL = NdotL * .3 + .7;
    20.             half4 c;
    21.             c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten);
    22.             //c.rgb = half3(1, 0, 0);
    23.             c.a = s.Alpha;
    24.             return c;
    25.         }
    26.  
    27.         struct Input
    28.         {
    29.             float4 color : COLOR;
    30.             float2 uv_MainTex;
    31.         };
    32.        
    33.         sampler2D _MainTex;
    34.         void surf(Input IN, inout SurfaceOutput o)
    35.         {
    36.             o.Albedo = IN.color * tex2D(_MainTex, IN.uv_MainTex);
    37.             o.Alpha = IN.color.a;
    38.         }
    39.         ENDCG
    40.     }
    41.    
    42.     Fallback "Diffuse"
    43. }
    Is it using the fallback? Is there some kind of error I don't see? Any help is appreciated!
     
  2. michaelplzno

    michaelplzno

    Joined:
    Dec 12, 2017
    Posts:
    49
    Hi, I ran a test of changing the shader code to output RED just as a test to see what would happen.

    Screenshot 2020-09-12 at 3.55.49 PM.jpg

    Its weird because it seems like its a slightly different red between the hexes? Maybe there is an error with the alpha component? Anyway, it *is* loading the shader at least because it is red so we know the shader is compiling. Maybe lighting is turned off on the iOS build? Maybe the directional light I'm using is set wrong, wrong scene or something. But then why would it work on PC and Android correctly?

    HELP!

    Here is the new Red shader code:

    Code (CSharp):
    1. Shader "Custom/VertexColor"
    2. {
    3.     Properties{
    4.         _MainTex("Texture", 2D) = "white" { }
    5.     }
    6.  
    7.     SubShader
    8.     {  
    9.         Tags { "Queue" = "Transparent" }
    10.         Blend SrcAlpha OneMinusSrcAlpha
    11.         //Cull Off
    12.              
    13.         CGPROGRAM
    14.         #pragma surface surf SimpleLambert alpha:blend
    15.  
    16.         half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) {
    17.             half NdotL = .5 + .5 * dot(s.Normal, lightDir);
    18.             NdotL = NdotL * NdotL;
    19.             NdotL = NdotL * .3 + .7;
    20.             half4 c;
    21.             c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten);
    22.             c.rgb = half3(1, 0, 0);
    23.             c.a = s.Alpha;
    24.             return c;
    25.         }
    26.  
    27.         struct Input
    28.         {
    29.             float4 color : COLOR;
    30.             float2 uv_MainTex;
    31.         };
    32.        
    33.         sampler2D _MainTex;
    34.         void surf(Input IN, inout SurfaceOutput o)
    35.         {
    36.             o.Albedo = IN.color * tex2D(_MainTex, IN.uv_MainTex);
    37.             o.Alpha = IN.color.a;
    38.         }
    39.         ENDCG
    40.     }
    41.    
    42.     Fallback "Diffuse"
    43. }
     
  3. michaelplzno

    michaelplzno

    Joined:
    Dec 12, 2017
    Posts:
    49
    Ok, tried a new version with a new shader and it basically just spit out the vertex color on iOS:

    Screenshot 2020-09-12 at 5.57.50 PM.jpg

    And here is the new shader code:

    Code (CSharp):
    1. Shader "Custom/VertexColor"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.     }
    7.    
    8.     SubShader
    9.     {
    10.         Tags { "Queue" = "Transparent" }
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.  
    13.         Pass
    14.         {
    15.             Tags {"LightMode" = "ForwardBase"}
    16.  
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.  
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.  
    25.             struct v2f
    26.             {
    27.                 float4 color : COLOR;
    28.                 float4 vertex : SV_POSITION;
    29.             };
    30.  
    31.             sampler2D _MainTex;
    32.             float4 _MainTex_ST;
    33.  
    34.             v2f vert(appdata_full v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = UnityObjectToClipPos(v.vertex);
    38.                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    39.                 half NdotL = dot(worldNormal, _WorldSpaceLightPos0.xyz) * .5 + .5;
    40.  
    41.                 NdotL = NdotL * NdotL;
    42.                 NdotL = NdotL * .3 + .7;
    43.  
    44.                 o.color.rgb = v.color.rgb * NdotL;
    45.                 o.color.a = v.color.a;
    46.  
    47.                 return o;
    48.             }
    49.  
    50.             fixed4 frag(v2f i) : SV_Target
    51.             {
    52.                 // sample the texture
    53.                 fixed4 col = i.color;  // tex2D(_MainTex, i.uv);
    54.  
    55.                 return col;
    56.             }
    57.             ENDCG
    58.         }
    59.     }
    60. }
     
  4. michaelplzno

    michaelplzno

    Joined:
    Dec 12, 2017
    Posts:
    49
    New version same issue

    Screenshot 2020-09-12 at 7.54.28 PM.jpg
    Here is the new Shader code:

    Code (CSharp):
    1. Shader "Custom/VertexColor"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.     }
    7.    
    8.     SubShader
    9.     {
    10.         Tags { "Queue" = "Transparent" }
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.  
    13.         Pass
    14.         {
    15.             Tags {"LightMode" = "ForwardBase"}
    16.  
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.  
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.  
    25.             struct v2f
    26.             {
    27.                 float3 normal : NORMAL;
    28.                 float4 color : COLOR;
    29.                 float4 vertex : SV_POSITION;
    30.             };
    31.  
    32.             sampler2D _MainTex;
    33.             float4 _MainTex_ST;
    34.  
    35.             v2f vert(appdata_full v)
    36.             {
    37.                 v2f o;
    38.                 o.vertex = UnityObjectToClipPos(v.vertex);
    39.                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    40.                 o.normal = worldNormal;
    41.                 o.color = v.color;
    42.  
    43.                 return o;
    44.             }
    45.  
    46.             fixed4 frag(v2f i) : SV_Target
    47.             {
    48.  
    49.                 half NdotL = dot(i.normal, _WorldSpaceLightPos0.xyz) * .5 + .5;
    50.  
    51.                 NdotL = NdotL * NdotL;
    52.                 NdotL = NdotL * .3 + .7;
    53.                 NdotL = clamp(NdotL, 0.0, 1.0);
    54.  
    55.                 fixed4 col;
    56.                 col.rgb = i.color.rgb* NdotL;
    57.                 col.a = i.color.a;
    58.  
    59.                 return col;
    60.             }
    61.             ENDCG
    62.         }
    63.     }
    64. }
     
  5. michaelplzno

    michaelplzno

    Joined:
    Dec 12, 2017
    Posts:
    49
    Just to point out a potential error in Unity iOS: I built the game with a shader that just sets the color to _WorldSpaceLightPos0.xyz * .5 + .5 and it was mid grey meaning that _WorldSpaceLightPos0.xyz is set to zero. I'm going to hard code in the light direction for now, but I think this may be a bug?
     
  6. michaelplzno

    michaelplzno

    Joined:
    Dec 12, 2017
    Posts:
    49
    Just FYI, hard coding the lighting direction vector fixed my problem. I do think this is an error in unity. Here is the shader I used in the end:

    Code (CSharp):
    1. Shader "Custom/VertexColor"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.     }
    7.    
    8.     SubShader
    9.     {
    10.         Tags { "Queue" = "Transparent" }
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.  
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.  
    23.             struct v2f
    24.             {
    25.                 float3 normal : NORMAL;
    26.                 float4 color : COLOR;
    27.                 float4 vertex : SV_POSITION;
    28.             };
    29.  
    30.             sampler2D _MainTex;
    31.             float4 _MainTex_ST;
    32.  
    33.             v2f vert(appdata_full v)
    34.             {
    35.                 v2f o;
    36.                 o.vertex = UnityObjectToClipPos(v.vertex);
    37.                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    38.                 o.normal = worldNormal;
    39.                 o.color = v.color;
    40.  
    41.                 return o;
    42.             }
    43.  
    44.             fixed4 frag(v2f i) : SV_Target
    45.             {
    46.                 half3 LightDir =  normalize(half3 (-.25,.5,-1));
    47.                 half NdotL = dot(i.normal, LightDir) * .5 + .5;
    48.  
    49.                 NdotL = NdotL * NdotL;
    50.                 NdotL = NdotL * .3 + .9;
    51.  
    52.                 fixed4 col;
    53.                 col.rgb = clamp(i.color.rgb* NdotL, 0.0, 1.0);
    54.                 col.a = i.color.a;
    55.  
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.     }
    61. }