- #ifdef GL_ES
- precision highp float;
- #endif
- uniform float exposure;
- uniform float decay;
- uniform float density;
- uniform float weight;
- uniform sampler2D depthTexture;
- uniform mat4 u_invMVP;
- varying vec2 v_sphereCenterScreenSpace;
- varying vec4 v_position;
- varying vec3 v_normal;
- uniform vec3 u_sphereCenter;
- uniform float u_sphereRadius;
- const float NUM_SAMPLES = 80;
- varying vec3 v_worldPos;
- varying vec2 v_texCoord0;
- // 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);
- return dot(colour , bitShifts);
- }
- // calculate god ray from depth map
- float getShadow(vec2 texCoord)
- {
- float sample = unpack(texture2D(depthTexture, texCoord));
- return step(0.9999, sample);
- vec4 sampleWorldPos = u_invMVP * vec4(texCoord * 2 - 1, sample * 2 - 1, 1);
- float dist = distance(sampleWorldPos.xyz / sampleWorldPos.w, u_sphereCenter);
- return smoothstep(0, 50, dist);
- }
- void main()
- {
- // get depth value at screen-space fragment position
- vec2 normalizedScreenCoords = (vec2(v_position.x, v_position.y) / v_position.w) / 2.0 + 0.5;
- vec2 textureSphereCenter = v_sphereCenterScreenSpace / 2.0 + 0.5;
- vec2 direction = normalizedScreenCoords - textureSphereCenter;
- float distance = length(direction);
- vec2 directionNormal = normalize(direction);
- float normalizedScreenDepth = v_position.z / v_position.w;
- // normalize interpolated normal
- vec3 N = normalize(v_normal);
- float accumulatedGodRay = 0;
- for (float i = 0; i < NUM_SAMPLES; i++)
- {
- accumulatedGodRay += 1.0 - getShadow(normalizedScreenCoords - (i / NUM_SAMPLES) * directionNormal
- * distance);
- }
- accumulatedGodRay /= NUM_SAMPLES;
- gl_FragColor = vec4(pow(accumulatedGodRay,1)) * pow(unpack(texture2D(depthTexture, normalizedScreenCoords)), 4);
- }
SHARE
TWEET
Untitled
a guest
Sep 4th, 2014
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
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.
