godrays.glsl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //@renderpasses 0
  2. uniform sampler2D r_SourceBuffer;
  3. uniform sampler2D r_DepthBuffer;
  4. uniform vec2 r_BufferCoordScale;
  5. varying vec2 v_BufferCoords;
  6. //@vertex
  7. attribute vec4 a_Position;
  8. void main(){
  9. v_BufferCoords=a_Position.xy * r_BufferCoordScale;
  10. gl_Position=vec4( a_Position.xy * 2.0 - 1.0,0.0,1.0 );
  11. }
  12. //@fragment
  13. uniform vec2 m_LightPosBufferCoords;
  14. #ifdef GL_ES
  15. const int m_NumSamples=100;
  16. #else
  17. uniform int m_NumSamples;
  18. #endif
  19. uniform float m_Exposure;
  20. uniform float m_Decay;
  21. uniform float m_Density;
  22. uniform vec4 m_Color;
  23. uniform float m_Weight;
  24. void main(){
  25. vec2 coords=v_BufferCoords;
  26. vec2 delta=m_LightPosBufferCoords-coords;
  27. delta*=1.0/float( m_NumSamples ) * m_Density;
  28. vec3 fragColor=vec3( 0.0 );
  29. vec3 color=m_Color.rgb * m_Weight;
  30. for( int i=0;i<m_NumSamples;++i ){
  31. coords+=delta;
  32. float depth=texture2D( r_DepthBuffer,coords ).r;
  33. vec3 sample=(depth==1.0) ? color : vec3( 0.0 );
  34. fragColor+=sample;
  35. }
  36. gl_FragColor=vec4( fragColor * m_Exposure,1.0 );
  37. /*
  38. float depth=texture2D( r_DepthBuffer,v_BufferCoords ).r;
  39. // depth=1.0;
  40. // gl_FragColor=vec4( v_BufferCoords,0.0,1.0 );
  41. gl_FragColor=vec4( depth,0.0,0.0,1.0 );
  42. */
  43. }