SHARE
TWEET
Untitled
a guest
Sep 7th, 2014
205
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- #ifdef GL_ES
- precision highp float;
- #endif
- uniform float exposure;
- uniform float decay;
- uniform float density;
- uniform float weight;
- uniform vec2 lightPositionOnScreen;
- uniform sampler2D depthTexture;
- uniform sampler2D u_prevFrame;
- uniform mat4 u_MVP;
- uniform mat4 u_invViewProj;
- uniform mat4 u_prevFrameMVP;
- uniform mat4 u_curInvMvp;
- uniform vec2 jitter;
- uniform float width;
- uniform float height;
- uniform float frameCounter;
- const float ITERATION_SIZE = 60;
- 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);
- }
- float getDepth(vec2 texCoord)
- {
- return unpack(texture2D(depthTexture, texCoord));
- }
- // reproject coordinates to previous frame
- vec3 getLastFrameScreenCoords(vec2 texCoord, float currentDepth)
- {
- // TODO: precalculate at CPU
- vec4 currentWorldCoords = u_prevFrameMVP * (u_curInvMvp * vec4(texCoord, currentDepth, 1));
- return currentWorldCoords.xyz / currentWorldCoords.w;
- }
- // calculate god ray from depth map
- float calcGodRay(vec2 texCoord)
- {
- float sample = unpack(texture2D(depthTexture, texCoord));
- sample = step(0.99999, sample);
- return sample;
- }
- // fetched from the internets
- float rand(vec2 co){
- return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
- }
- void main()
- {
- // get (albeit low-precision) depth
- float currentDepth = getDepth(v_texCoord0);
- // get last frame coordinates of our current uv's
- vec3 lastFrameScreenCoords = getLastFrameScreenCoords(v_texCoord0 * 2.0 - 1.0, currentDepth * 2.0 - 1.0);
- // set up some variables...
- float accumulatedGodray = 0.0;
- vec2 screenSpaceDistance = vec2(v_texCoord0.st - lightPositionOnScreen.xy);
- float sampleLength = length(screenSpaceDistance);
- vec2 textureToLightPosNormal = normalize(screenSpaceDistance);
- vec2 k = textureToLightPosNormal * sampleLength;
- // previous frame
- vec4 prevFrameData = texture2D(u_prevFrame, lastFrameScreenCoords.xy / 2.0 + 0.5);
- // offset raymarching start point
- vec2 startPos = v_texCoord0 - (fract(jitter.x + rand(v_texCoord0 + jitter.y))) * screenSpaceDistance;
- // calculate remaining distance
- vec2 remainingDistance = startPos - lightPositionOnScreen.xy;
- // raymarch
- // TODO: kalle_h: binary occluder depth map generation
- // TODO: store map @ rgb565 red channel
- for (float i = 0; i < ITERATION_SIZE; i += 1)
- {
- vec2 sampleDistance = startPos - (i / ITERATION_SIZE) * remainingDistance;
- float godRay = calcGodRay(sampleDistance);
- accumulatedGodray += godRay;
- }
- // last frame data
- float prevFrameAccumulatedGodray = prevFrameData.x;
- // difference in z
- float zDist = distance(lastFrameScreenCoords.z / 2.0 + 0.5, prevFrameData.y);
- float lambda = 0;
- // handle out of bounds uv
- // TODO: uv's larger than target
- if (sign(lastFrameScreenCoords.x) / 2.0 + 0.5 < 0 || sign(lastFrameScreenCoords.y) / 2.0 + 0.5 < 0)
- {
- lambda = 0;
- }
- else
- {
- lambda = 0.4;
- }
- // cache hit / cache miss ?
- float cacheResult = step(1.0 / 256.0, zDist);
- const float probability = ITERATION_SIZE;
- if (cacheResult == 0.0)
- {
- // combine last frame with current frame
- accumulatedGodray = mix(prevFrameAccumulatedGodray,accumulatedGodray / probability, lambda);
- }
- else
- {
- // cache miss, use only current frame
- accumulatedGodray = (accumulatedGodray / probability);
- }
- // clamp samples
- accumulatedGodray *= step(7.0 / 256.0, accumulatedGodray);
- gl_FragColor = vec4(accumulatedGodray, currentDepth, 0, 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.
