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): Shader "Custom/VertexColor" { Properties{ _MainTex("Texture", 2D) = "white" { } } SubShader { Tags { "Queue" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha //Cull Off CGPROGRAM #pragma surface surf SimpleLambert alpha:blend half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) { half NdotL = .5 + .5 * dot(s.Normal, lightDir); NdotL = NdotL * NdotL; NdotL = NdotL * .3 + .7; half4 c; c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten); //c.rgb = half3(1, 0, 0); c.a = s.Alpha; return c; } struct Input { float4 color : COLOR; float2 uv_MainTex; }; sampler2D _MainTex; void surf(Input IN, inout SurfaceOutput o) { o.Albedo = IN.color * tex2D(_MainTex, IN.uv_MainTex); o.Alpha = IN.color.a; } ENDCG } Fallback "Diffuse" } Is it using the fallback? Is there some kind of error I don't see? Any help is appreciated!
Hi, I ran a test of changing the shader code to output RED just as a test to see what would happen. 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): Shader "Custom/VertexColor" { Properties{ _MainTex("Texture", 2D) = "white" { } } SubShader { Tags { "Queue" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha //Cull Off CGPROGRAM #pragma surface surf SimpleLambert alpha:blend half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) { half NdotL = .5 + .5 * dot(s.Normal, lightDir); NdotL = NdotL * NdotL; NdotL = NdotL * .3 + .7; half4 c; c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten); c.rgb = half3(1, 0, 0); c.a = s.Alpha; return c; } struct Input { float4 color : COLOR; float2 uv_MainTex; }; sampler2D _MainTex; void surf(Input IN, inout SurfaceOutput o) { o.Albedo = IN.color * tex2D(_MainTex, IN.uv_MainTex); o.Alpha = IN.color.a; } ENDCG } Fallback "Diffuse" }
Ok, tried a new version with a new shader and it basically just spit out the vertex color on iOS: And here is the new shader code: Code (CSharp): Shader "Custom/VertexColor" { Properties { _MainTex("Texture", 2D) = "white" {} } SubShader { Tags { "Queue" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha Pass { Tags {"LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 color : COLOR; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert(appdata_full v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); half3 worldNormal = UnityObjectToWorldNormal(v.normal); half NdotL = dot(worldNormal, _WorldSpaceLightPos0.xyz) * .5 + .5; NdotL = NdotL * NdotL; NdotL = NdotL * .3 + .7; o.color.rgb = v.color.rgb * NdotL; o.color.a = v.color.a; return o; } fixed4 frag(v2f i) : SV_Target { // sample the texture fixed4 col = i.color; // tex2D(_MainTex, i.uv); return col; } ENDCG } } }
New version same issue Here is the new Shader code: Code (CSharp): Shader "Custom/VertexColor" { Properties { _MainTex("Texture", 2D) = "white" {} } SubShader { Tags { "Queue" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha Pass { Tags {"LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float3 normal : NORMAL; float4 color : COLOR; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert(appdata_full v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); half3 worldNormal = UnityObjectToWorldNormal(v.normal); o.normal = worldNormal; o.color = v.color; return o; } fixed4 frag(v2f i) : SV_Target { half NdotL = dot(i.normal, _WorldSpaceLightPos0.xyz) * .5 + .5; NdotL = NdotL * NdotL; NdotL = NdotL * .3 + .7; NdotL = clamp(NdotL, 0.0, 1.0); fixed4 col; col.rgb = i.color.rgb* NdotL; col.a = i.color.a; return col; } ENDCG } } }
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?
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): Shader "Custom/VertexColor" { Properties { _MainTex("Texture", 2D) = "white" {} } SubShader { Tags { "Queue" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float3 normal : NORMAL; float4 color : COLOR; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert(appdata_full v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); half3 worldNormal = UnityObjectToWorldNormal(v.normal); o.normal = worldNormal; o.color = v.color; return o; } fixed4 frag(v2f i) : SV_Target { half3 LightDir = normalize(half3 (-.25,.5,-1)); half NdotL = dot(i.normal, LightDir) * .5 + .5; NdotL = NdotL * NdotL; NdotL = NdotL * .3 + .9; fixed4 col; col.rgb = clamp(i.color.rgb* NdotL, 0.0, 1.0); col.a = i.color.a; return col; } ENDCG } } }