This question follows from http://forum.unity3d.com/threads/135310-Shader-works-in-unity-window-(OSX)-but-fails-on-iPhone
The shader in that post works fine on OS X (ie renders correctly in the unity preview window) but fails on an actual iPhone device.
As has been pointed out by steego, this #pragma needs to be taken out:
//#pragma exclude_renderers gles
( looks like it only got but there automatically in the first place because I didn’t have an explicit fragment shader; I have since explicitly written out a trivial fragment shader, and commented out the #pragma )
but then this line fails:
float LdotN = saturate( dot ( glstate.light[0].position, N ) );
… with the error: ‘glstate: undeclared identifier’
If I understand correctly from dreamora, ‘glstate’ is simply not available on GL ES.
So how can I access this data?
Martin links me to http://unity3d.com/support/documentation/Components/SL-BuiltinValues.html which I have already visited…
That page seems to be saying that I should recode the above line to:
float LdotN = saturate( dot ( _ObjectSpaceLightPos[0].position, N ) );
…but this now gives an error of ‘_ObjectSpaceLightPos: undeclared identifier’
Next I dig into:
#include "UnityCG.cginc"
…and discover that the text ‘_ObjectSpaceLightPos’ doesn’t appear anywhere.
Does this mean that the page referenced above is outdated?
Reading through the file, I notice that there is a
float4 unity_LightColor[4];
float4 unity_LightPosition[4];
on line 150.
FINALLY, I get something working with:
// DIFFUSE
float3 N = mul(UNITY_MATRIX_IT_MV, float4(i.normal, 1));
float LdotN = saturate( dot ( unity_LightPosition[0], N ) );
o.color += LdotN * _Color * unity_LightColor[0];
// original was:
// float LdotN = saturate( dot (glstate.light[0].position, N) ); //_ObjectSpaceLightPos[0].position fails
// o.color += LdotN * _Color * glstate.light[0].diffuse;
O joy it even runs on the iPhone!

Does this mean that this unity help page referenced above needs to be updated?
But wait! That is not right. It is failing to calculate the diffuse colour correctly. It looks as though LdotN must always be coming out at the same value.
Now I am against stuck. What is going wrong here?