I have little experience writing GLSL shaders, but no experience at all with shaderlab or CG shaders.
I’ve written a couple of shaders in shaderlab/cg that works well on desktop, an Asus 7 android tablet, but does not work on an iPhone 4S and an old Samgung Galaxy S. They look wrong or just black.
So, I’ve rewritten them in GLSL and they work perfectly in all devices (except windows, of course). But I can see in the shaders properties in the unity inspector that the CG one’s are more complex, so I’m probably doing something wrong.
I post one here:
CG version:
Shader "Custom/HudboxCG" {
Properties {
_Color ("Color", Color) = (1,1,1,.5)
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float lum;
};
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input,o);
// (0,1,-2) normalized
o.lum = dot(float3(0, 0.447214, -0.894427), v.normal) * 1.1;
}
float4 _Color;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = _Color;
o.Emission = _Color * IN.lum;
}
ENDCG
}
}
GLSL version:
Shader "Custom/Hudbox" {
Properties {
_Color ("Color", Color) = (1,1,1,.5)
}
SubShader {
Tags { "RenderType" = "Opaque" }
Pass {
GLSLPROGRAM
#ifdef VERTEX
varying float lum;
uniform vec4 _Color;
void main() {
vec4 avertex = gl_Vertex;
lum = dot(vec3(0, 0.447214, -0.894427), gl_Normal) * 1.1;
gl_Position = gl_ModelViewProjectionMatrix * avertex;
}
#endif
#ifdef FRAGMENT
varying float lum;
uniform vec4 _Color;
void main() {
gl_FragColor = _Color * lum;
}
#endif
ENDGLSL
}
}
Fallback "Custom/HudboxCG"
}
So how should I write the cg shader so the generated GLSL is similar to the one I’ve hand-written?