SHARE
TWEET

Untitled

a guest Sep 15th, 2014 152 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef GL_ES
  2.         precision mediump float;
  3. #endif
  4.  
  5. uniform highp sampler2D u_depthTexture;
  6. uniform highp sampler2D u_gBufferTexture;
  7. varying vec3 v_worldPos;
  8. varying vec4 v_position;
  9. uniform mat4 u_projTrans;
  10.  
  11. uniform vec3 u_viewObjectSpace;
  12. uniform vec4 u_lightColor;
  13. uniform highp mat4 u_invProj;
  14. uniform mat3 u_normalMatrix;
  15. uniform vec3 u_sphereCenter;
  16. uniform float u_sphereRadius;
  17.  
  18. vec3 decode (vec2 enc)
  19. {
  20.     vec2 fenc = enc * 4.0 - 2.0;
  21.     float f = dot(fenc,fenc);
  22.     float g = sqrt(1.0 + (f * -0.25));
  23.     vec3 n;
  24.     n.xy = fenc*g;
  25.     n.z = 1.0 - f * 0.5;
  26.     return n;
  27. }
  28.  
  29. // unpack float-packed depth
  30. float unpack (vec4 colour)
  31. {
  32.     const vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
  33.                                 1.0 / (256.0 * 256.0),
  34.                                 1.0 / 256.0,
  35.                                 1.0);
  36.  
  37.     return dot(colour , bitShifts);
  38. }
  39.  
  40. void main()
  41. {
  42.     vec4 screenSpaceTexturePos = v_position / v_position.w;
  43.     highp vec4 worldPos = vec4(screenSpaceTexturePos.xy, 0.0, 1.0);
  44.     screenSpaceTexturePos = (screenSpaceTexturePos * 0.5) + 0.5;
  45.  
  46.     float depth = unpack(texture2D(u_depthTexture, screenSpaceTexturePos.xy));
  47.  
  48.     worldPos.z = depth;// * 2.0 - 1.0;
  49.     // TODO: inv operation!!
  50.     worldPos = u_invProj * worldPos;
  51.     // the world pos of the gbuffer
  52.     worldPos *= 1.0 / worldPos.w;
  53.  
  54.     vec3 n = decode(texture2D(u_gBufferTexture, screenSpaceTexturePos.xy).xy);
  55.  
  56.     // calculate normalized light vector and distance to sphere light surface
  57.     vec3 l = u_sphereCenter - worldPos.xyz;
  58.     float distance = length(l);
  59.     float d = max(u_sphereRadius - distance, 0.0);
  60.     l /= distance;
  61.     float attenuation = d / u_sphereRadius;
  62.  
  63.     float dotNL = max(dot(l, n), 0.0);
  64.     float specDot = dot(reflect(l, -n), u_viewObjectSpace);
  65.     float specularReflection = pow(specDot, 14.0);
  66.  
  67.     vec3 light = attenuation * dotNL * (u_lightColor.xyz + specularReflection);
  68.  
  69.     gl_FragColor = vec4(light, 1.0);
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top