vectorlight conversion cleanups, pointlight port

This commit is contained in:
Azaezel 2018-12-09 15:26:14 -06:00
parent c8b76d82c3
commit 89c534ce21
6 changed files with 86 additions and 131 deletions

View file

@ -23,7 +23,7 @@
#include "./torque.glsl" #include "./torque.glsl"
#include "./brdf.glsl" #include "./brdf.glsl"
#ifndef TORQUE_SHADERGEN #ifndef TORQUE_SHADERGEN
#line 26
// These are the uniforms used by most lighting shaders. // These are the uniforms used by most lighting shaders.
uniform vec4 inLightPos[3]; uniform vec4 inLightPos[3];

View file

@ -22,7 +22,7 @@
#ifndef _TORQUE_GLSL_ #ifndef _TORQUE_GLSL_
#define _TORQUE_GLSL_ #define _TORQUE_GLSL_
#line 25
float M_HALFPI_F = 1.57079632679489661923; float M_HALFPI_F = 1.57079632679489661923;
float M_PI_F = 3.14159265358979323846; float M_PI_F = 3.14159265358979323846;

View file

@ -21,7 +21,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include "../../../torque.glsl" #include "../../../torque.glsl"
#line 24
in vec4 hpos; in vec4 hpos;
in vec2 uv0; in vec2 uv0;

View file

@ -24,12 +24,11 @@
#include "shadergen:/autogenConditioners.h" #include "shadergen:/autogenConditioners.h"
#include "farFrustumQuad.glsl" #include "farFrustumQuad.glsl"
#include "lightingUtils.glsl"
#include "../../../gl/lighting.glsl" #include "../../../gl/lighting.glsl"
#include "../../shadowMap/shadowMapIO_GLSL.h" #include "../../shadowMap/shadowMapIO_GLSL.h"
#include "softShadow.glsl" #include "softShadow.glsl"
#include "../../../gl/torque.glsl" #include "../../../gl/torque.glsl"
#line 31
in vec4 wsEyeDir; in vec4 wsEyeDir;
in vec4 ssPos; in vec4 ssPos;
in vec4 vsEyeDir; in vec4 vsEyeDir;
@ -38,7 +37,7 @@ in vec4 color;
#ifdef USE_COOKIE_TEX #ifdef USE_COOKIE_TEX
/// The texture for cookie rendering. /// The texture for cookie rendering.
uniform samplerCube cookieMap ; uniform samplerCube cookieMap;
#endif #endif
@ -85,7 +84,9 @@ uniform samplerCube cookieMap ;
// this value was found via experementation // this value was found via experementation
// NOTE: this is wrong, it only biases in one direction, not towards the uv // NOTE: this is wrong, it only biases in one direction, not towards the uv
// center ( 0.5 0.5 ). // center ( 0.5 0.5 ).
//shadowCoord.xy *= 0.997; float offsetVal = 0.95;
shadowCoord.xy *= offsetVal;
shadowCoord.xy += vec2(1.0-offsetVal) / 2.0;
#ifndef SHADOW_PARABOLOID #ifndef SHADOW_PARABOLOID
@ -122,153 +123,86 @@ uniform vec3 lightPosition;
uniform vec4 lightColor; uniform vec4 lightColor;
uniform float lightBrightness; uniform float lightBrightness;
uniform float lightRange; uniform float lightRange;
uniform vec2 lightAttenuation;
uniform vec4 lightMapParams; uniform vec4 lightMapParams;
uniform vec4 vsFarPlane; uniform vec4 vsFarPlane;
uniform mat3 viewToLightProj;
uniform mat3 dynamicViewToLightProj;
uniform vec4 lightParams; uniform vec4 lightParams;
uniform float lightInvSqrRange;
uniform float shadowSoftness; uniform float shadowSoftness;
uniform mat3 worldToLightProj;
uniform mat3 dynamicWorldToLightProj;
uniform vec3 eyePosWorld;
uniform mat4 cameraToWorld;
out vec4 OUT_col; out vec4 OUT_col;
out vec4 OUT_col1;
void main() void main()
{ {
// Compute scene UV // Compute scene UV
vec3 ssPos = ssPos.xyz / ssPos.w; vec2 uvScene = getUVFromSSPos(ssPos.xyz/ssPos.w, rtParams0);
vec2 uvScene = getUVFromSSPos( ssPos, rtParams0 );
//unpack normal and linear depth
vec4 normDepth = deferredUncondition(deferredBuffer, uvScene);
//eye ray WS/VS
vec3 vsEyeRay = getDistanceVectorToPlane( -vsFarPlane.w, vsEyeDir.xyz, vsFarPlane );
vec3 wsEyeRay = tMul(cameraToWorld, vec4(vsEyeRay, 0)).xyz;
//create surface
Surface surface = createSurface( normDepth, colorBuffer, matInfoBuffer,
uvScene, eyePosWorld, wsEyeRay, cameraToWorld);
// Matinfo flags
vec4 matInfo = texture( matInfoBuffer, uvScene );
//early out if emissive //early out if emissive
bool emissive = getFlag( matInfo.r, 0 ); if (getFlag(surface.matFlag, 0))
if ( emissive )
{ {
OUT_col = vec4(0.0, 0.0, 0.0, 0.0); OUT_col = vec4(0.0);
OUT_col1 = vec4(0.0, 0.0, 0.0, 0.0); return;
return;
} }
vec4 colorSample = texture( colorBuffer, uvScene ); vec3 L = lightPosition - surface.P;
vec3 subsurface = vec3(0.0,0.0,0.0); float dist = length(L);
if (getFlag( matInfo.r, 1 )) vec3 lighting = vec3(0.0);
if(dist < lightRange)
{ {
subsurface = colorSample.rgb; float distToLight = dist / lightRange;
if (colorSample.r>colorSample.g) SurfaceToLight surfaceToLight = createSurfaceToLight(surface, L);
subsurface = vec3(0.772549, 0.337255, 0.262745);
else
subsurface = vec3(0.337255, 0.772549, 0.262745);
}
// Sample/unpack the normal/z data
vec4 deferredSample = deferredUncondition( deferredBuffer, uvScene );
vec3 normal = deferredSample.rgb;
float depth = deferredSample.a;
// Eye ray - Eye -> Pixel
vec3 eyeRay = getDistanceVectorToPlane( -vsFarPlane.w, vsEyeDir.xyz, vsFarPlane );
vec3 viewSpacePos = eyeRay * depth;
// Build light vec, get length, clip pixel if needed
vec3 lightVec = lightPosition - viewSpacePos;
float lenLightV = length( lightVec );
clip( lightRange - lenLightV );
// Get the attenuated falloff.
float atten = attenuate( lightColor, lightAttenuation, lenLightV );
clip( atten - 1e-6 );
// Normalize lightVec
lightVec /= lenLightV;
// If we can do dynamic branching then avoid wasting
// fillrate on pixels that are backfacing to the light.
float nDotL = dot( lightVec, normal );
//DB_CLIP( nDotL < 0 );
#ifdef NO_SHADOW #ifdef NO_SHADOW
float shadowed = 1.0; float shadowed = 1.0;
#else #else
// Get a linear depth from the light source.
float distToLight = lenLightV / lightRange;
#ifdef SHADOW_CUBE #ifdef SHADOW_CUBE
// TODO: We need to fix shadow cube to handle soft shadows! // TODO: We need to fix shadow cube to handle soft shadows!
float occ = texture( shadowMap, tMul( viewToLightProj, -lightVec ) ).r; float occ = texture( shadowMap, ttMul( worldToLightProj, -surfaceToLight.L ) ).r;
float shadowed = saturate( exp( lightParams.y * ( occ - distToLight ) ) ); float shadowed = saturate( exp( lightParams.y * ( occ - distToLight ) ) );
#else #else
vec2 shadowCoord = decodeShadowCoord( tMul( worldToLightProj, -surfaceToLight.L ) ).xy;
vec2 shadowCoord = decodeShadowCoord( tMul( viewToLightProj, -lightVec ) ).xy; vec2 dynShadowCoord = decodeShadowCoord( tMul( dynamicWorldToLightProj, -surfaceToLight.L ) ).xy;
float static_shadowed = softShadow_filter(shadowMap, ssPos.xy, shadowCoord, shadowSoftness, distToLight, surfaceToLight.NdotL, lightParams.y);
float static_shadowed = softShadow_filter( shadowMap, float dynamic_shadowed = softShadow_filter(dynamicShadowMap, ssPos.xy, dynShadowCoord, shadowSoftness, distToLight, surfaceToLight.NdotL, lightParams.y);
ssPos.xy,
shadowCoord,
shadowSoftness,
distToLight,
nDotL,
lightParams.y );
vec2 dynamicShadowCoord = decodeShadowCoord( tMul( dynamicViewToLightProj, -lightVec ) ).xy;
float dynamic_shadowed = softShadow_filter( dynamicShadowMap,
ssPos.xy,
dynamicShadowCoord,
shadowSoftness,
distToLight,
nDotL,
lightParams.y );
float shadowed = min(static_shadowed, dynamic_shadowed); float shadowed = min(static_shadowed, dynamic_shadowed);
#endif #endif
#endif // !NO_SHADOW #endif // !NO_SHADOW
vec3 lightcol = lightColor.rgb; vec3 lightCol = lightColor.rgb;
#ifdef USE_COOKIE_TEX #ifdef USE_COOKIE_TEX
// Lookup the cookie sample. // Lookup the cookie sample.
vec4 cookie = texture( cookieMap, tMul( viewToLightProj, -lightVec ) ); vec4 cookie = texture(cookieMap, ttMul(worldToLightProj, -surfaceToLight.L));
// Multiply the light with the cookie tex. // Multiply the light with the cookie tex.
lightcol *= cookie.rgb; lightCol *= cookie.rgb;
// Use a maximum channel luminance to attenuate // Use a maximum channel luminance to attenuate
// the lighting else we get specular in the dark // the lighting else we get specular in the dark
// regions of the cookie texture. // regions of the cookie texture.
atten *= max( cookie.r, max( cookie.g, cookie.b ) ); lightCol *= max(cookie.r, max(cookie.g, cookie.b));
#endif #endif
// NOTE: Do not clip on fully shadowed pixels as it would
// cause the hardware occlusion query to disable the shadow.
vec3 l = lightVec;// normalize(-lightDirection); //get punctual light contribution
vec3 v = eyeRay;// normalize(eyePosWorld - worldPos.xyz); lighting = getPunctualLight(surface, surfaceToLight, lightCol, lightBrightness, lightInvSqrRange, shadowed);
}
vec3 h = normalize(v + l); OUT_col = vec4(lighting, 0);
float dotNLa = clamp(dot(normal, l), 0.0, 1.0);
float dotNVa = clamp(dot(normal, v), 0.0, 1.0);
float dotNHa = clamp(dot(normal, h), 0.0, 1.0);
float dotHVa = clamp(dot(normal, v), 0.0, 1.0);
float dotLHa = clamp(dot(l, h), 0.0, 1.0);
float roughness = 1.0001-matInfo.b;
float metalness = matInfo.a;
//diffuse
float disDiff = Fr_DisneyDiffuse(dotNVa, dotNLa, dotLHa, roughness);
vec3 diffuse = vec3(disDiff, disDiff, disDiff) / M_PI_F;
//specular
vec3 specular = directSpecular(normal, v, l, roughness, 1.0) * lightColor.rgb;
if (nDotL<0) shadowed = 0;
float Sat_NL_Att = saturate( nDotL * shadowed ) * lightBrightness;
//output
OUT_col = float4(diffuse * lightBrightness*Sat_NL_Att*shadowed,1.0);
OUT_col1 = float4(specular * lightBrightness*Sat_NL_Att*shadowed,1.0);
} }

View file

@ -1,10 +1,30 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "../../../gl/hlslCompat.glsl" #include "../../../gl/hlslCompat.glsl"
#include "shadergen:/autogenConditioners.h" #include "shadergen:/autogenConditioners.h"
#include "farFrustumQuad.glsl" #include "farFrustumQuad.glsl"
#include "../../../gl/lighting.glsl"
#include "../../../gl/torque.glsl" #include "../../../gl/torque.glsl"
#line 7 #include "../../../gl/lighting.glsl"
#line 6
in vec4 pos; in vec4 pos;
in vec4 wsEyeDir; in vec4 wsEyeDir;

View file

@ -27,7 +27,7 @@
#include "../../../gl/lighting.glsl" #include "../../../gl/lighting.glsl"
#include "../../shadowMap/shadowMapIO_GLSL.h" #include "../../shadowMap/shadowMapIO_GLSL.h"
#include "softShadow.glsl" #include "softShadow.glsl"
#line 30
in vec4 hpos; in vec4 hpos;
in vec2 uv0; in vec2 uv0;
in vec3 wsEyeRay; in vec3 wsEyeRay;
@ -103,7 +103,7 @@ vec4 AL_VectorLightShadowCast( sampler2D _sourceShadowMap,
// for all of the splits and then check if its valid. // for all of the splits and then check if its valid.
vec4 shadowCoordX = baseShadowCoord.xxxx; vec4 shadowCoordX = baseShadowCoord.xxxx;
vec4 shadowCoordY = baseShadowCoord.yyyy; vec4 shadowCoordY = baseShadowCoord.yyyy;
vec4 farPlaneDists = distToLight.xxxx; vec4 farPlaneDists = vec4(distToLight);
shadowCoordX *= _scaleX; shadowCoordX *= _scaleX;
shadowCoordY *= _scaleY; shadowCoordY *= _scaleY;
shadowCoordX += _offsetX; shadowCoordX += _offsetX;
@ -170,10 +170,10 @@ vec4 AL_VectorLightShadowCast( sampler2D _sourceShadowMap,
// Move around inside of atlas // Move around inside of atlas
vec2 aOffset; vec2 aOffset;
aOffset.x = dot(finalMask, _atlasXOffset); aOffset.x = dot(finalMask, atlasXOffset);
aOffset.y = dot(finalMask, _atlasYOffset); aOffset.y = dot(finalMask, atlasYOffset);
shadowCoord *= _atlasScale; shadowCoord *= atlasScale;
shadowCoord += aOffset; shadowCoord += aOffset;
// Each split has a different far plane, take this into account. // Each split has a different far plane, take this into account.
@ -184,17 +184,17 @@ vec4 AL_VectorLightShadowCast( sampler2D _sourceShadowMap,
softShadow_filter( _sourceShadowMap, softShadow_filter( _sourceShadowMap,
_texCoord, _texCoord,
shadowCoord, shadowCoord,
farPlaneScale * _shadowSoftness, farPlaneScale * shadowSoftness,
distToLight, distToLight,
_dotNL, _dotNL,
dot( finalMask, _overDarkPSSM ) ) ); dot( finalMask, overDarkPSSM ) ) );
} }
out vec4 OUT_col; out vec4 OUT_col;
void main() void main()
{ {
//unpack normal and linear depth //unpack normal and linear depth
vec4 normDepth = TORQUE_DEFERRED_UNCONDITION(deferredBuffer, uv0); vec4 normDepth = deferredUncondition(deferredBuffer, uv0);
//create surface //create surface
Surface surface = createSurface( normDepth, colorBuffer, matInfoBuffer, Surface surface = createSurface( normDepth, colorBuffer, matInfoBuffer,
@ -203,7 +203,8 @@ void main()
//early out if emissive //early out if emissive
if (getFlag(surface.matFlag, 0)) if (getFlag(surface.matFlag, 0))
{ {
return 0.0.xxxx; OUT_col = vec4(0);
return;
} }
//create surface to light //create surface to light
@ -217,13 +218,13 @@ void main()
#else #else
// Fade out the shadow at the end of the range. // Fade out the shadow at the end of the range.
vec4 zDist = (zNearFarInvNearFar.x + zNearFarInvNearFar.y * surface.depth); vec4 zDist = vec4(zNearFarInvNearFar.x + zNearFarInvNearFar.y * surface.depth);
float fadeOutAmt = ( zDist.x - fadeStartLength.x ) * fadeStartLength.y; float fadeOutAmt = ( zDist.x - fadeStartLength.x ) * fadeStartLength.y;
vec4 static_shadowed_colors = AL_VectorLightShadowCast( TORQUE_SAMPLER2D_MAKEARG(shadowMap), uv0.xy, worldToLightProj, surface.P, scaleX, scaleY, offsetX, offsetY, vec4 static_shadowed_colors = AL_VectorLightShadowCast( shadowMap, uv0.xy, worldToLightProj, surface.P, scaleX, scaleY, offsetX, offsetY,
farPlaneScalePSSM, surfaceToLight.NdotL); farPlaneScalePSSM, surfaceToLight.NdotL);
vec4 dynamic_shadowed_colors = AL_VectorLightShadowCast( TORQUE_SAMPLER2D_MAKEARG(dynamicShadowMap), uv0.xy, dynamicWorldToLightProj, surface.P, dynamicScaleX, vec4 dynamic_shadowed_colors = AL_VectorLightShadowCast( dynamicShadowMap, uv0.xy, dynamicWorldToLightProj, surface.P, dynamicScaleX,
dynamicScaleY, dynamicOffsetX, dynamicOffsetY, dynamicFarPlaneScalePSSM, surfaceToLight.NdotL); dynamicScaleY, dynamicOffsetX, dynamicOffsetY, dynamicFarPlaneScalePSSM, surfaceToLight.NdotL);
float static_shadowed = static_shadowed_colors.a; float static_shadowed = static_shadowed_colors.a;
@ -252,5 +253,5 @@ void main()
//get directional light contribution //get directional light contribution
vec3 lighting = getDirectionalLight(surface, surfaceToLight, lightingColor.rgb, lightBrightness, shadow); vec3 lighting = getDirectionalLight(surface, surfaceToLight, lightingColor.rgb, lightBrightness, shadow);
return vec4(lighting, 0); OUT_col = vec4(lighting, 0);
} }