SHARE
TWEET
Untitled
a guest
Sep 15th, 2014
152
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- #ifdef GL_ES
- precision mediump float;
- #endif
- uniform highp sampler2D u_depthTexture;
- uniform highp sampler2D u_gBufferTexture;
- varying vec3 v_worldPos;
- varying vec4 v_position;
- uniform mat4 u_projTrans;
- uniform vec3 u_viewObjectSpace;
- uniform vec4 u_lightColor;
- uniform highp mat4 u_invProj;
- uniform mat3 u_normalMatrix;
- uniform vec3 u_sphereCenter;
- uniform float u_sphereRadius;
- vec3 decode (vec2 enc)
- {
- vec2 fenc = enc * 4.0 - 2.0;
- float f = dot(fenc,fenc);
- float g = sqrt(1.0 + (f * -0.25));
- vec3 n;
- n.xy = fenc*g;
- n.z = 1.0 - f * 0.5;
- return n;
- }
- // unpack float-packed depth
- float unpack (vec4 colour)
- {
- const vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
- 1.0 / (256.0 * 256.0),
- 1.0 / 256.0,
- 1.0);
- return dot(colour , bitShifts);
- }
- void main()
- {
- vec4 screenSpaceTexturePos = v_position / v_position.w;
- highp vec4 worldPos = vec4(screenSpaceTexturePos.xy, 0.0, 1.0);
- screenSpaceTexturePos = (screenSpaceTexturePos * 0.5) + 0.5;
- float depth = unpack(texture2D(u_depthTexture, screenSpaceTexturePos.xy));
- worldPos.z = depth;// * 2.0 - 1.0;
- // TODO: inv operation!!
- worldPos = u_invProj * worldPos;
- // the world pos of the gbuffer
- worldPos *= 1.0 / worldPos.w;
- vec3 n = decode(texture2D(u_gBufferTexture, screenSpaceTexturePos.xy).xy);
- // calculate normalized light vector and distance to sphere light surface
- vec3 l = u_sphereCenter - worldPos.xyz;
- float distance = length(l);
- float d = max(u_sphereRadius - distance, 0.0);
- l /= distance;
- float attenuation = d / u_sphereRadius;
- float dotNL = max(dot(l, n), 0.0);
- float specDot = dot(reflect(l, -n), u_viewObjectSpace);
- float specularReflection = pow(specDot, 14.0);
- vec3 light = attenuation * dotNL * (u_lightColor.xyz + specularReflection);
- gl_FragColor = vec4(light, 1.0);
- }
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.
