Diffuse GLSL

Hey Guys !

I’m a unity dev for some time and i never try shaders so i though than now is the time ! I did some research and apparently CG is still the best way to write shaders on unity … But since GLSL is growing i thought than may be will be nice start with GLSL …
Anyway for my first shader i try to make the unity diffuse … I have to problems

1 : I got a lot of pieces of code but unfortunately i’m still confused about the matrix multiplications and what that means . You guys know a good place to learn the basic shader math ? May be a book ?

2 : My shader still do not look like the default diffuse somebody can help me to fixe it ?

Cheers guys !

Shader "GLSL/Lambert" 
{
	Properties 
	{
		_Color("Color" , COLOR) = (1,0,0,1)
	}
   SubShader 
   {
      Pass 
      {    
        Tags {"LightMode" = "ForwardBase"}
 
 		Lighting ON 
 
        GLSLPROGRAM
        
 		uniform vec4 _Color;
 		uniform vec4 _LightColor0;
 		 		
 		uniform vec4 _WorldSpaceLightPos0;
 		
 		varying vec3 faceNormal;
 		varying vec3 vecPos;
 
         #ifdef VERTEX
 
         void main()
         {  
         	faceNormal = normalize(gl_NormalMatrix * gl_Normal); // transpoe a normal do modelo para o mundo
         	vecPos = vec3(gl_ModelViewMatrix * gl_Vertex);
         	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;         	
         }
 
         #endif
 
         #ifdef FRAGMENT
 
         void main()
         {
         	
         	vec3 lightNormal = normalize(_WorldSpaceLightPos0.xyz - vecPos);
         	
         	float angle = max(0.0,dot(faceNormal , lightNormal));
         	
         	vec4 diffuse = clamp(_Color * angle * _LightColor0 , 0.0,1.0);
         	vec4 ambient = gl_LightModel.ambient * _Color;
         
            gl_FragColor =  ambient + diffuse;
         }
 
         #endif
 
         ENDGLSL
      }
	}
}

Even though you want to learn GLSL, in stead of Cg, the NVIDIA Cg tutorial is still one of the best places to start, since it explains almost everything you need to know. The language is more similar to HLSL than it is to GLSL, but it shouldn’t be all that difficult to switch to GLSL once you’re comfortable creating shaders using Cg.

http://en.wikibooks.org/wiki/GLSL_Programming/Unity