Different Shader Display on 3gs vs. 4s

Hey all,

I wrote a shader in cg which fakes something similar to vertex lighting.

It works as intended in the editor, on iPad 2, new iPad, and iPhone 4s.

On the original iPad, iPhones 3gs and 4, however, it seems to work, but the “lighting” (output color) is much fainter to the point that it is almost not visible (it is there, though).

I realize that those models have the sgx535 graphics processor, while the others have the sgx543mp2 (mp4 for new iPad), but why would that make the shader behave differently?

I also noticed that the built - in unity shader “Mobile/Diffuse” seems to fall back to the vertex-lit shader for these devices as well, despite manually setting the shader LOD myself, not sure if this is related at all.

Thank you for any help you can give, source for the shader follows.

Shader "Custom/UnlitCollectable" {
    Properties {
    	_MainTex ("Base (RGB)", 2D) = "white" {}
    	_Color ("Color", Color) = (1,1,1,1)
    	_Length("Length", Float) = 8
    }
    SubShader {
    	Tags { "RenderType"="Opaque" }
    	LOD 150
    	
    	Pass {
    		Lighting Off
    		
    		CGPROGRAM
    		#pragma vertex vert
    		#pragma fragment frag
    		
    		#include "UnityCG.cginc"
    
    		sampler2D _MainTex;
    		fixed4 _Color;
    		float4 _Position; //Set using Shader.SetGlobalVector
    		fixed _Length;
    
    		struct v2f {
    			float4  pos : SV_POSITION;
        		float2  uv : TEXCOORD0;
        		float4  color : COLOR;
    		};
    		
    		float4 _MainTex_ST;
    		v2f vert (appdata_base v) {
    			v2f o;
    			o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    			o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
    			
    			float4 P = mul(_Object2World, v.vertex);
    			float dist = distance(P, _Position);
    			float4 L = normalize(_Position - P);
    			float3 n = mul((float3x3)_Object2World, v.normal);
    			
    			float light = dot(n, L.xyz) + ((_Length - dist) / _Length);
    			light = clamp(light, 0, 1);
    			o.color = light * _Color;
    	
    			return o;
    		}
    		
    		half4 frag (v2f i) : COLOR {
    			half4 texcol = tex2D (_MainTex, i.uv);
    			return texcol + i.color;
    		}
    		ENDCG
    	}
    }
    
    Fallback "Mobile/Unlit (Supports Lightmap)"
}

Hey everyone,

I found what I think the issue was with this shader while working on another one.

It seems that a float can only hold a 0 -1 range on devices with the sgx535 graphics processor, while on the later processors it can hold something higher.

This is a weird behavior and as far as I know is not documented anywhere, so heads up to anyone facing the same issue.

Thanks again for the help.

Thanks for the answer on this, color spaces on different devices was not something I was aware of. I’m not sure this is the problem I am experiencing, though.

I did a test by changing this line -

o.color = light * _Color;

to this -

o.color =  _Color;

which ignores all the lighting calculations. This shader produces the same output on all devices, which leads me to believe that my issue is most likely related to the lighting calculations producing different results depending on device.

Once again thank you for your help.