light.glsl 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //@renderpasses 4,5
  2. varying vec2 v_TexCoord0;
  3. varying vec2 v_LightPos;
  4. varying vec4 v_Color;
  5. varying vec2 v_GBufferCoords;
  6. varying vec2 v_FragPos;
  7. //@vertex
  8. attribute vec4 a_Position;
  9. attribute vec2 a_TexCoord0;
  10. attribute vec2 a_TexCoord1;
  11. attribute vec4 a_Color;
  12. uniform mat4 r_ModelViewMatrix;
  13. uniform mat4 r_ModelViewProjectionMatrix;
  14. uniform vec2 r_ViewportOrigin;
  15. uniform vec2 r_ViewportSize;
  16. uniform vec2 r_ViewportClip;
  17. uniform vec2 r_GBufferScale;
  18. uniform vec4 m_ImageColor;
  19. void main(){
  20. v_TexCoord0=a_TexCoord0;
  21. v_LightPos=a_TexCoord1;
  22. v_FragPos=a_Position.xy;
  23. v_Color=m_ImageColor * a_Color;
  24. gl_Position=r_ModelViewProjectionMatrix * a_Position;
  25. vec2 vpcoords=(gl_Position.xy * 0.5 + 0.5) * r_ViewportSize;
  26. v_GBufferCoords=(vpcoords + r_ViewportOrigin) * r_GBufferScale;
  27. }
  28. //@fragment
  29. uniform sampler2D m_ImageTexture0; //image texture
  30. uniform float m_LightDepth;
  31. uniform sampler2D r_GBuffer0; //gbuffer diffuse
  32. uniform sampler2D r_GBuffer1; //gbuffer normal
  33. void main(){
  34. vec3 normal=texture2D( r_GBuffer1,v_GBufferCoords ).xyz;
  35. float gloss=normal.z;
  36. normal.xy=normal.xy * 2.0 - 1.0;
  37. normal.z=sqrt( 1.0-dot( normal.xy,normal.xy ) );
  38. //diffuse...
  39. //
  40. vec3 lvec=normalize( vec3( v_LightPos-v_FragPos,m_LightDepth ) );
  41. float ndotl=max( dot( normal,lvec ),0.0 );
  42. vec4 tcolor=texture2D( m_ImageTexture0,v_TexCoord0 ) * v_Color;
  43. vec4 diffuse=texture2D( r_GBuffer0,v_GBufferCoords ) * tcolor * ndotl;
  44. //specular...
  45. //
  46. vec3 hvec=normalize( lvec+vec3( 0.0,0.0,1.0 ) );
  47. float ndoth=max( dot( normal,hvec ),0.0 );
  48. vec4 specular=tcolor * pow( ndoth,128.0 ) * gloss;
  49. #if MX2_RENDERPASS==5
  50. float shadow=texture2D( r_GBuffer0,v_GBufferCoords ).a;
  51. diffuse*=shadow;
  52. specular*=shadow;
  53. #endif
  54. //tada!
  55. //
  56. gl_FragColor=vec4( diffuse.rgb+vec3( specular.a ),0.0 );
  57. }