Volumetric quad pass

This commit is contained in:
Lubos Lenco 2017-07-08 14:12:18 +02:00
parent baf41655b0
commit 321de4bbdd
5 changed files with 268 additions and 7 deletions

View file

@ -19,7 +19,7 @@ uniform sampler2D snoise;
uniform vec2 screenSize;
uniform mat4 invVP;
uniform mat4 LWVP;
uniform vec3 viewPos;
uniform vec3 eye;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform float lightRadius;
@ -61,6 +61,7 @@ void rayStep(inout vec3 curPos, inout float curOpticalDepth, inout float scatter
lampPos.xyz /= lampPos.w;
visibility = float(texture(shadowMap, lampPos.xy).r > lampPos.z - shadowsBias);
}
// Cubemap
// visibility = float(texture(shadowMapCube, -l).r + shadowsBias > lpToDepth(lp, lightPlane));
@ -77,7 +78,7 @@ void main() {
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
vec3 worldPos = getPos2(invVP, depth, texCoord);
vec3 viewVec = worldPos - viewPos;
vec3 viewVec = worldPos - eye;
float worldPosDist = length(viewVec);
vec3 viewVecNorm = viewVec / worldPosDist;
@ -86,7 +87,7 @@ void main() {
float endDepth = 20.0;
endDepth = min(worldPosDist, endDepth);
vec3 curPos = viewPos + viewVecNorm * startDepth;
vec3 curPos = eye + viewVecNorm * startDepth;
float stepLenWorld = stepLen * (endDepth - startDepth);
float curOpticalDepth = exp(-tExt * stepLenWorld);
float scatteredLightAmount = 0.0;

View file

@ -27,7 +27,7 @@
"link": "_screenSize"
},
{
"name": "viewPos",
"name": "eye",
"link": "_cameraPosition"
},
{

View file

@ -0,0 +1,188 @@
// 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
#include "../compiled.glsl"
#include "../std/gbuffer.glsl"
#include "../std/shadows.glsl"
uniform sampler2D gbufferD;
#ifndef _NoShadows
//!uniform sampler2D shadowMap;
//!uniform samplerCube shadowMapCube;
#endif
uniform sampler2D snoise;
uniform mat4 LWVP;
uniform vec3 eye;
uniform vec3 eyeLook;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform float lightRadius;
uniform float shadowsBias;
// uniform int lightShadow;
// uniform vec2 lightPlane;
in vec2 texCoord;
in vec3 viewRay;
out vec4 fragColor;
const float tScat = 0.08;
const float tAbs = 0.0;
const float tExt = 0.0; //tScat + tAbs;
// const float stepLen = 1.0 / 11.0;
const float stepLen = 1.0 / 80; // Temporary..
// const float lighting = 0.4;
float lighting(vec3 p) {
vec3 L = lightPos.xyz - p.xyz;
float Ldist = length(lightPos.xyz - p.xyz);
vec3 Lnorm = L / Ldist;
float linearAtenuation = min(1.0, max(0.0, (lightRadius - Ldist) / lightRadius));
return linearAtenuation; //* min(1.0, 1.0 / (Ldist * Ldist));
}
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;
vec4 lampPos = LWVP * vec4(curPos, 1.0);
if (lampPos.w > 0.0) {
lampPos.xyz /= lampPos.w;
visibility = float(texture(shadowMap, lampPos.xy).r > lampPos.z - shadowsBias);
}
// Cubemap
// visibility = float(texture(shadowMapCube, -l).r + shadowsBias > lpToDepth(lp, lightPlane));
scatteredLightAmount += curOpticalDepth * l1 * visibility;
}
void main() {
float pixelRayMarchNoise = texture(snoise, texCoord).r;
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
vec3 worldPos = getPos(eye, eyeLook, viewRay, depth);
vec3 viewVec = worldPos - eye;
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 = eye + 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(vec3(scatteredLightAmount * volumAirColor * normalize(lightColor.rgb) * volumAirTurbidity), 1.0); // * ((1.0 - depth) * 10.0)
}

View file

@ -0,0 +1,58 @@
{
"contexts": [
{
"name": "volumetric_light_quad",
"depth_write": false,
"compare_mode": "always",
"cull_mode": "none",
"links": [
{
"name": "LWVP",
"link": "_biasLampWorldViewProjectionMatrix"
},
{
"name": "shadowsBias",
"link": "_lampShadowsBias"
},
{
"name": "lightPos",
"link": "_lampPosition"
},
{
"name": "lightRadius",
"link": "_lampRadius"
},
{
"name": "lightColor",
"link": "_lampColor"
},
{
"name": "snoise",
"link": "_noise8"
},
{
"name": "lightPlane",
"link": "_lampPlane",
"ifdef": ["_Disabled"]
},
{
"name": "lightShadow",
"link": "_lampCastShadow",
"ifdef": ["_Disabled"]
},
{
"name": "eye",
"link": "_cameraPosition"
},
{
"name": "eyeLook",
"link": "_cameraLook"
}
],
"texture_params": [],
"vertex_shader": "volumetric_light_quad.vert.glsl",
"vertex_shader_path": "../include/pass_viewray.vert.glsl",
"fragment_shader": "volumetric_light_quad.frag.glsl"
}
]
}

View file

@ -474,10 +474,24 @@ def make_deferred_light_pass(stages, node_group, node):
stages.append(stage)
def make_volumetric_light_pass(stages, node_group, node):
# Draw lamp volume - TODO: properly generate stage
# make_quad_pass(stages, node_group, node, target_index=1, bind_target_indices=[5, 6], bind_target_constants=['gbufferD', 'shadowMap'], shader_context='volumetric_light/volumetric_light/volumetric_light_blend')
make_quad_pass(stages, node_group, node, target_index=2, bind_target_indices=[5, 6], bind_target_constants=['gbufferD', 'shadowMap'], shader_context='volumetric_light/volumetric_light/volumetric_light')
stages[-1]['command'] = 'draw_lamp_volume'
make_quad_pass(stages, node_group, node, target_index=2, bind_target_indices=[5, 6], bind_target_constants=['gbufferD', 'shadowMap'], shader_context='', with_draw_quad=False)
stage = {}
stage['command'] = 'call_function'
stage['params'] = ['iron.data.RenderPath.lampIsSun']
# Draw fs quad
stage_true = {}
stage_true['params'] = []
make_draw_quad(stage_true, node_group, node, context_index=2, shader_context='volumetric_light_quad/volumetric_light_quad/volumetric_light_quad')
# Draw lamp volume
stage_false = {}
stage_false['params'] = []
make_draw_quad(stage_false, node_group, node, context_index=2, shader_context='volumetric_light/volumetric_light/volumetric_light')
stage_false['command'] = 'draw_lamp_volume'
stage['returns_true'] = [stage_true]
stage['returns_false'] = [stage_false]
stages.append(stage)
# Blur
make_quad_pass(stages, node_group, node, target_index=3, bind_target_indices=[2, 4], bind_target_constants=['tex', 'gbuffer0'], shader_context='blur_edge_pass/blur_edge_pass/blur_edge_pass_x')
make_quad_pass(stages, node_group, node, target_index=1, bind_target_indices=[3, 4], bind_target_constants=['tex', 'gbuffer0'], shader_context='blur_edge_pass/blur_edge_pass/blur_edge_pass_y_blend_add')