Dir light as quad

This commit is contained in:
Lubos Lenco 2017-03-15 15:49:13 +01:00
parent 0fc938911c
commit 7c28d2e140
7 changed files with 307 additions and 164 deletions

View file

@ -56,7 +56,6 @@ uniform vec2 spotlightData;
uniform sampler2D sltcMag;
#endif
uniform vec3 eye;
// uniform vec3 eyeLook;
#ifdef _SSRS
uniform mat4 VP;
#endif
@ -66,8 +65,6 @@ uniform vec3 eye;
#endif
in vec4 wvpposition;
// in vec2 texCoord;
// in vec3 viewRay;
out vec4 fragColor;
// Separable SSS Transmittance Function, ref to sss_pass
@ -184,26 +181,17 @@ void main() {
// Per-light
vec3 l;
if (lightType == 0) { // Sun
l = lightDir;
}
else if (lightType == 2) { // Spot
l = normalize(lightPos - p);
#ifndef _NoLampFalloff
visibility *= attenuate(distance(p, lightPos));
#endif
l = normalize(lightPos - p);
#ifndef _NoLampFalloff
visibility *= attenuate(distance(p, lightPos));
#endif
if (lightType == 2) { // Spot
float spotEffect = dot(lightDir, l);
// x - cutoff, y - cutoff - exponent
if (spotEffect < spotlightData.x) {
visibility *= smoothstep(spotlightData.y, spotlightData.x, spotEffect);
}
}
else { // Point = 1, Area = 3
l = normalize(lightPos - p);
#ifndef _NoLampFalloff
visibility *= attenuate(distance(p, lightPos));
#endif
}
vec3 h = normalize(v + l);
float dotNH = dot(n, h);

View file

@ -84,11 +84,6 @@
"name": "eye",
"link": "_cameraPosition"
},
{
"name": "eyeLook",
"link": "_cameraLook",
"ifdef": ["_Disabled"]
},
{
"name": "invVP",
"link": "_inverseViewProjectionMatrix"
@ -97,36 +92,6 @@
"name": "LWVP",
"link": "_biasLampWorldViewProjectionMatrix"
},
{
"name": "screenSize",
"link": "_screenSize",
"ifdef": ["_Disabled"]
},
{
"name": "shirr",
"link": "_envmapIrradiance",
"ifdef": ["_Disabled"]
},
{
"name": "senvmapRadiance",
"link": "_envmapRadiance",
"ifdef": ["_Disabled"]
},
{
"name": "envmapNumMipmaps",
"link": "_envmapNumMipmaps",
"ifdef": ["_Disabled"]
},
{
"name": "senvmapBrdf",
"link": "_envmapBrdf",
"ifdef": ["_Disabled"]
},
{
"name": "envmapStrength",
"link": "_envmapStrength",
"ifdef": ["_Disabled"]
},
{
"name": "snoise",
"link": "_noise64",

View file

@ -1,106 +0,0 @@
{
"contexts": [
{
"name": "deferred_light",
"depth_write": false,
"compare_mode": "always",
"cull_mode": "none",
"blend_source": "blend_one",
"blend_destination": "blend_one",
"blend_operation": "add",
"alpha_blend_source": "blend_one",
"alpha_blend_destination": "blend_one",
"alpha_blend_operation": "add",
"links": [
{
"name": "lightPos",
"link": "_lampPosition"
},
{
"name": "lightDir",
"link": "_lampDirection"
},
{
"name": "lightType",
"link": "_lampType"
},
{
"name": "lightIndex",
"link": "_lampIndex",
"ifdef": ["_Disabled"]
},
{
"name": "lightColor",
"link": "_lampColor"
},
{
"name": "lightBias",
"link": "_lampBias"
},
{
"name": "spotlightData",
"link": "_spotlampData"
},
{
"name": "eye",
"link": "_cameraPosition"
},
{
"name": "eyeLook",
"link": "_cameraLook"
},
{
"name": "invVP",
"link": "_inverseViewProjectionMatrix"
},
{
"name": "LWVP",
"link": "_biasLampWorldViewProjectionMatrix"
},
{
"name": "shirr",
"link": "_envmapIrradiance",
"ifdef": ["_Disabled"]
},
{
"name": "senvmapRadiance",
"link": "_envmapRadiance",
"ifdef": ["_Disabled"]
},
{
"name": "envmapNumMipmaps",
"link": "_envmapNumMipmaps",
"ifdef": ["_Disabled"]
},
{
"name": "senvmapBrdf",
"link": "_envmapBrdf",
"ifdef": ["_Disabled"]
},
{
"name": "envmapStrength",
"link": "_envmapStrength",
"ifdef": ["_Disabled"]
},
{
"name": "sltcMat",
"link": "_ltcMat",
"ifdef": ["_PolyLight"]
},
{
"name": "sltcMag",
"link": "_ltcMag",
"ifdef": ["_PolyLight"]
},
{
"name": "time",
"link": "_time",
"ifdef": ["_PolyLight"]
}
],
"vertex_shader": "deferred_light.vert.glsl",
"vertex_shader_path": "../include/pass_viewray.vert.glsl",
"fragment_shader": "deferred_light.frag.glsl"
}
]
}

View file

@ -0,0 +1,206 @@
#version 450
#ifdef GL_ES
precision mediump float;
#endif
#include "../compiled.glsl"
#include "../std/brdf.glsl"
#include "../std/math.glsl"
#ifndef _NoShadows
#ifdef _PCSS
#include "../std/shadows_pcss.glsl"
#else
#include "../std/shadows.glsl"
#endif
#endif
#include "../std/gbuffer.glsl"
uniform sampler2D gbufferD;
uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
#ifndef _NoShadows
//!uniform sampler2D shadowMap;
#ifdef _PCSS
//!uniform sampler2D snoise;
//!uniform float lampSizeUV;
#endif
#endif
uniform mat4 LWVP;
uniform vec3 lightColor;
uniform vec3 l; // lightDir
uniform int lightShadow;
uniform float shadowsBias;
uniform vec3 eye;
uniform vec3 eyeLook;
#ifdef _SSRS
uniform mat4 VP;
#endif
#ifdef _LampColTex
uniform sampler2D texlampcolor;
#endif
in vec2 texCoord;
in vec3 viewRay;
out vec4 fragColor;
// Separable SSS Transmittance Function, ref to sss_pass
#ifdef _SSS
vec3 SSSSTransmittance(float translucency, float sssWidth, vec3 worldPosition, vec3 worldNormal, vec3 lightDir) {
float scale = 8.25 * (1.0 - translucency) / sssWidth;
vec4 shrinkedPos = vec4(worldPosition - 0.005 * worldNormal, 1.0);
vec4 shadowPosition = LWVP * shrinkedPos;
float d1 = texture(shadowMap, shadowPosition.xy / shadowPosition.w).r; // 'd1' has a range of 0..1
float d2 = shadowPosition.z; // 'd2' has a range of 0..'lightFarPlane'
const float lightFarPlane = 120 / 3.5;
d1 *= lightFarPlane; // So we scale 'd1' accordingly:
float d = scale * abs(d1 - d2);
float dd = -d * d;
vec3 profile = vec3(0.233, 0.455, 0.649) * exp(dd / 0.0064) +
vec3(0.1, 0.336, 0.344) * exp(dd / 0.0484) +
vec3(0.118, 0.198, 0.0) * exp(dd / 0.187) +
vec3(0.113, 0.007, 0.007) * exp(dd / 0.567) +
vec3(0.358, 0.004, 0.0) * exp(dd / 1.99) +
vec3(0.078, 0.0, 0.0) * exp(dd / 7.41);
return profile * clamp(0.3 + dot(lightDir, -worldNormal), 0.0, 1.0);
}
#endif
#ifndef _NoShadows
float shadowTest(const vec3 lPos) {
#ifdef _Clampstc
// Filtering out of bounds, remove
// const vec2 border = vec2(1.0 / shadowmapSize[0], 1.0 / shadowmapSize[1]) * 2.0;
// lPos.xy = clamp(lPos.xy, border[0], 1.0 - border[1]);
if (lPos.x < 0.0) return 1.0;
if (lPos.y < 0.0) return 1.0;
if (lPos.x > 1.0) return 1.0;
if (lPos.y > 1.0) return 1.0;
#endif
#ifdef _PCSS
return PCSS(lPos.xy, lPos.z - shadowsBias);
#else
return PCF(lPos.xy, lPos.z - shadowsBias);
#endif
}
#endif
#ifdef _SSRS
vec2 getProjectedCoord(vec3 hitCoord) {
vec4 projectedCoord = VP * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
return projectedCoord.xy;
}
float getDeltaDepth(vec3 hitCoord) {
vec2 texCoord = getProjectedCoord(hitCoord);
vec4 g0 = texture(gbuffer0, texCoord);
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
// TODO: store_depth
// float depth = (1.0 - g0.a) * 2.0 - 1.0;
vec3 wpos = getPos(eye, eyeLook, viewRay, depth);
float d1 = length(eye - wpos);
float d2 = length(eye - hitCoord);
return d1 - d2;
}
float traceShadow(vec3 dir, vec3 hitCoord) {
dir *= ssrsRayStep;
// for (int i = 0; i < maxSteps; i++) {
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return 0.0;
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return 0.0;
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return 0.0;
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return 0.0;
//}
return 1.0;
}
#endif
void main() {
vec4 g0 = texture(gbuffer0, texCoord); // Normal.xy, metallic/roughness, occlusion
vec4 g1 = texture(gbuffer1, texCoord); // Basecolor.rgb,
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0; // 0 - 1 => -1 - 1
// TODO: store_depth
// float depth = (1.0 - g0.a) * 2.0 - 1.0;
vec3 n;
n.z = 1.0 - abs(g0.x) - abs(g0.y);
n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy);
n = normalize(n);
vec3 p = getPos(eye, eyeLook, viewRay, depth);
vec2 metrough = unpackFloat(g0.b);
vec3 v = normalize(eye - p);
float dotNV = dot(n, v);
vec3 albedo = surfaceAlbedo(g1.rgb, metrough.x); // g1.rgb - basecolor
vec3 f0 = surfaceF0(g1.rgb, metrough.x);
float visibility = 1.0;
#ifndef _NoShadows
if (lightShadow == 1) {
vec4 lampPos = LWVP * vec4(p, 1.0);
if (lampPos.w > 0.0) {
visibility = shadowTest(lampPos.xyz / lampPos.w);
}
}
#endif
// Per-light
// vec3 l = lightDir; // lightType == 0 // Sun
vec3 h = normalize(v + l);
float dotNH = dot(n, h);
float dotVH = dot(v, h);
float dotNL = dot(n, l);
// float dotLV = dot(l, v);
// float dotLH = dot(l, h);
#ifdef _OrenNayar
fragColor.rgb = orenNayarDiffuseBRDF(albedo, metrough.y, dotNV, dotNL, dotVH) + specularBRDF(f0, metrough.y, dotNL, dotNH, dotNV, dotVH);
#else
fragColor.rgb = lambertDiffuseBRDF(albedo, dotNL) + specularBRDF(f0, metrough.y, dotNL, dotNH, dotNV, dotVH);
#endif
// Aniso spec
// #ifdef _Aniso
// float shinyParallel = metrough.y;
// float shinyPerpendicular = 0.08;
// vec3 fiberDirection = vec3(0.0, 1.0, 8.0);
// fragColor.rgb = diffuseBRDF(albedo, metrough.y, dotNV, dotNL, dotVH, dotLV) + wardSpecular(n, h, dotNL, dotNV, dotNH, fiberDirection, shinyParallel, shinyPerpendicular);
// #endif
fragColor.rgb *= lightColor;
#ifdef _LampColTex
// fragColor.rgb *= texture(texlampcolor, envMapEquirect(l)).rgb;
fragColor.rgb *= pow(texture(texlampcolor, l.xy).rgb, vec3(2.2));
#endif
#ifdef _SSS
float mask = g0.a;
if (mask == 2.0) {
fragColor.rgb *= SSSSTransmittance(1.0, 0.005, p, n, l);
}
#endif
#ifdef _SSRS
float tvis = traceShadow(-l, p);
// vec2 coords = getProjectedCoord(hitCoord);
// vec2 deltaCoords = abs(vec2(0.5, 0.5) - coords.xy);
// float screenEdgeFactor = clamp(1.0 - (deltaCoords.x + deltaCoords.y), 0.0, 1.0);
// tvis *= screenEdgeFactor;
visibility *= tvis;
#endif
fragColor.rgb *= visibility;
}

View file

@ -0,0 +1,74 @@
{
"contexts": [
{
"name": "deferred_light_quad",
"depth_write": false,
"color_write_alpha": false,
"compare_mode": "always",
"cull_mode": "none",
"blend_source": "blend_one",
"blend_destination": "blend_one",
"blend_operation": "add",
"alpha_blend_source": "blend_one",
"alpha_blend_destination": "blend_one",
"alpha_blend_operation": "add",
"links": [
{
"name": "invVP",
"link": "_inverseViewProjectionMatrix"
},
{
"name": "l",
"link": "_lampDirection"
},
{
"name": "lightColor",
"link": "_lampColor"
},
{
"name": "lightShadow",
"link": "_lampCastShadow"
},
{
"name": "texlampcolor",
"link": "_lampColorTexture",
"ifdef": ["_LampColTex"]
},
{
"name": "shadowsBias",
"link": "_lampShadowsBias"
},
{
"name": "eye",
"link": "_cameraPosition"
},
{
"name": "eyeLook",
"link": "_cameraLook"
},
{
"name": "LWVP",
"link": "_biasLampWorldViewProjectionMatrix"
},
{
"name": "snoise",
"link": "_noise64",
"ifdef": ["_PCSS"]
},
{
"name": "lampSizeUV",
"link": "_lampSizeUV",
"ifdef": ["_PCSS"]
},
{
"name": "VP",
"link": "_viewProjectionMatrix",
"ifdef": ["_SSRS"]
}
],
"vertex_shader": "deferred_light_quad.vert.glsl",
"vertex_shader_path": "../include/pass_viewray.vert.glsl",
"fragment_shader": "deferred_light_quad.frag.glsl"
}
]
}

View file

@ -310,7 +310,7 @@ def process_call_function(stage, stages, node, node_group):
afterMergeNode = nodes.find_node_by_link_from(node_group, margeNode, margeNode.outputs[0])
buildNode(stages, afterMergeNode, node_group)
def make_quad_pass(stages, node_group, node, target_index=1, bind_target_indices=[3, 5, 7], bind_target_constants=None, shader_context=None, viewport_scale=1.0, with_clear=False):
def make_quad_pass(stages, node_group, node, target_index=1, bind_target_indices=[3, 5, 7], bind_target_constants=None, shader_context=None, viewport_scale=1.0, with_clear=False, with_draw_quad=True):
# Set target
if target_index != None and node.inputs[target_index].is_linked:
stage = {}
@ -342,8 +342,9 @@ def make_quad_pass(stages, node_group, node, target_index=1, bind_target_indices
stage = {}
stage['params'] = []
# Draw quad
make_draw_quad(stage, node_group, node, context_index=2, shader_context=shader_context)
stages.append(stage)
if with_draw_quad:
make_draw_quad(stage, node_group, node, context_index=2, shader_context=shader_context)
stages.append(stage)
def make_ssao_pass(stages, node_group, node):
sc = 0.5 if bpy.data.worlds['Arm'].generate_ssao_half_res else 1.0
@ -443,9 +444,22 @@ def make_water_pass(stages, node_group, node):
make_quad_pass(stages, node_group, node, target_index=1, bind_target_indices=[2, 3], bind_target_constants=['tex', 'gbufferD'], shader_context='water_pass/water_pass/water_pass')
def make_deferred_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=[2, 3], bind_target_constants=['gbuffer', 'shadowMap'], shader_context='deferred_light/deferred_light/deferred_light')
stages[-1]['command'] = 'draw_lamp_volume'
make_quad_pass(stages, node_group, node, target_index=1, bind_target_indices=[2, 3], bind_target_constants=['gbuffer', '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='deferred_light_quad/deferred_light_quad/deferred_light_quad')
# Draw lamp volume
stage_false = {}
stage_false['params'] = []
make_draw_quad(stage_false, node_group, node, context_index=2, shader_context='deferred_light/deferred_light/deferred_light')
stage_false['command'] = 'draw_lamp_volume'
stage['returns_true'] = [stage_true]
stage['returns_false'] = [stage_false]
stages.append(stage)
def make_volumetric_light_pass(stages, node_group, node):
# Draw lamp volume - TODO: properly generate stage

View file

@ -849,6 +849,7 @@ node_categories = [
]
def register():
bpy.utils.register_class(CGPipelineTree)
bpy.utils.register_class(BeginNode)
bpy.utils.register_class(DrawMeshesNode)
bpy.utils.register_class(DrawDecalsNode)
@ -909,6 +910,7 @@ def register():
def unregister():
nodeitems_utils.unregister_node_categories("CG_PIPELINE_NODES")
bpy.utils.unregister_class(CGPipelineTree)
bpy.utils.unregister_class(BeginNode)
bpy.utils.unregister_class(DrawMeshesNode)
bpy.utils.unregister_class(DrawDecalsNode)