2016-08-31 00:46:10 +02:00
|
|
|
// http://sebastien.hillaire.free.fr/index.php?option=com_content&view=article&id=72&Itemid=106
|
|
|
|
#version 450
|
|
|
|
|
|
|
|
#ifdef GL_ES
|
|
|
|
precision mediump float;
|
|
|
|
#endif
|
|
|
|
|
2016-09-08 14:08:31 +02:00
|
|
|
#include "../compiled.glsl"
|
2016-10-23 15:32:26 +02:00
|
|
|
#include "../std/gbuffer.glsl"
|
2016-09-08 14:08:31 +02:00
|
|
|
|
2016-08-31 00:46:10 +02:00
|
|
|
uniform sampler2D gbufferD;
|
|
|
|
#ifndef _NoShadows
|
|
|
|
uniform sampler2D shadowMap;
|
|
|
|
#endif
|
|
|
|
uniform sampler2D snoise;
|
|
|
|
|
|
|
|
uniform vec2 screenSize;
|
|
|
|
uniform mat4 invVP;
|
|
|
|
uniform mat4 LWVP;
|
|
|
|
uniform vec3 viewPos;
|
|
|
|
uniform vec3 lightPos;
|
|
|
|
uniform vec3 lightColor;
|
|
|
|
uniform float lightRadius;
|
|
|
|
uniform float shadowsBias;
|
|
|
|
|
|
|
|
in vec4 wvpposition;
|
2016-10-12 17:52:27 +02:00
|
|
|
out vec4 fragColor;
|
2016-08-31 00:46:10 +02:00
|
|
|
|
|
|
|
const float tScat = 0.08;
|
|
|
|
const float tAbs = 0.0;
|
|
|
|
const float tExt = 0.0; //tScat + tAbs;
|
|
|
|
// const float stepLen = 1.0 / 11.0;
|
2016-09-08 14:08:31 +02:00
|
|
|
const float stepLen = 1.0 / 80; // Temporary..
|
2016-08-31 00:46:10 +02:00
|
|
|
|
|
|
|
// const float lighting = 0.4;
|
|
|
|
float lighting(vec3 p) {
|
2016-10-17 17:39:40 +02:00
|
|
|
vec3 L = lightPos.xyz - p.xyz;
|
|
|
|
float Ldist = length(lightPos.xyz - p.xyz);
|
|
|
|
vec3 Lnorm = L / Ldist;
|
2016-08-31 00:46:10 +02:00
|
|
|
|
2016-10-17 17:39:40 +02:00
|
|
|
float linearAtenuation = min(1.0, max(0.0, (lightRadius - Ldist) / lightRadius));
|
|
|
|
return linearAtenuation; //* min(1.0, 1.0 / (Ldist * Ldist));
|
2016-08-31 00:46:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rayStep(inout vec3 curPos, inout float curOpticalDepth, inout float scatteredLightAmount, float stepLenWorld, vec3 viewVecNorm) {
|
|
|
|
curPos += stepLenWorld * viewVecNorm;
|
|
|
|
float density = 1.0;
|
|
|
|
|
|
|
|
float l1 = lighting(curPos) * stepLenWorld * tScat * density;
|
|
|
|
curOpticalDepth *= exp(-tExt * stepLenWorld * density);
|
|
|
|
// vec3 lightDir = (lightPos - curPos);
|
|
|
|
// vec3 lightDirNorm = -normalize(lightDir);
|
|
|
|
|
|
|
|
float visibility = 1.0;
|
2016-10-15 12:17:33 +02:00
|
|
|
vec4 lampPos = LWVP * vec4(curPos, 1.0);
|
|
|
|
if (lampPos.w > 0.0) {
|
|
|
|
lampPos.xyz /= lampPos.w;
|
2016-10-17 17:39:40 +02:00
|
|
|
lampPos.xy = lampPos.xy * 0.5 + 0.5;
|
2016-10-15 12:17:33 +02:00
|
|
|
float distanceFromLight = texture(shadowMap, lampPos.xy).r * 2.0 - 1.0;
|
|
|
|
visibility = float(distanceFromLight > lampPos.z - shadowsBias);
|
2016-08-31 00:46:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scatteredLightAmount += curOpticalDepth * l1 * visibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
vec2 screenPosition = wvpposition.xy / wvpposition.w;
|
|
|
|
vec2 texCoord = screenPosition * 0.5 + 0.5;
|
2016-09-02 23:11:04 +02:00
|
|
|
// texCoord += vec2(0.5 / screenSize); // Half pixel offset
|
2016-08-31 00:46:10 +02:00
|
|
|
|
2016-10-17 17:39:40 +02:00
|
|
|
float pixelRayMarchNoise = texture(snoise, texCoord).r;
|
2016-08-31 00:46:10 +02:00
|
|
|
|
|
|
|
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
|
2016-10-17 17:39:40 +02:00
|
|
|
vec3 worldPos = getPos2(invVP, depth, texCoord);
|
2016-08-31 00:46:10 +02:00
|
|
|
|
|
|
|
vec3 viewVec = worldPos - viewPos;
|
2016-10-17 17:39:40 +02:00
|
|
|
float worldPosDist = length(viewVec);
|
|
|
|
vec3 viewVecNorm = viewVec / worldPosDist;
|
|
|
|
|
|
|
|
float startDepth = 0.1;
|
|
|
|
startDepth = min(worldPosDist, startDepth);
|
|
|
|
float endDepth = 20.0;
|
|
|
|
endDepth = min(worldPosDist, endDepth);
|
|
|
|
|
|
|
|
vec3 curPos = viewPos + viewVecNorm * startDepth;
|
|
|
|
float stepLenWorld = stepLen * (endDepth - startDepth);
|
|
|
|
float curOpticalDepth = exp(-tExt * stepLenWorld);
|
|
|
|
float scatteredLightAmount = 0.0;
|
|
|
|
|
|
|
|
curPos += stepLenWorld * viewVecNorm * (2.0 * pixelRayMarchNoise - 1.0);
|
|
|
|
|
|
|
|
// for(float l = stepLen; l < 0.99999; l += stepLen) {// Do not do the first and last steps
|
|
|
|
// Raw steps for now..
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
|
|
|
|
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// curOpticalDepth
|
|
|
|
fragColor = vec4(scatteredLightAmount * lightColor.rgb * volumAirColor * volumAirTurbidity, 0.0);
|
2016-10-12 17:52:27 +02:00
|
|
|
// fragColor = vec4(scatteredLightAmount * lightColor.rgb * ((1.0 - depth) * 10.0), 0.0);
|
2016-08-31 00:46:10 +02:00
|
|
|
}
|