vectorlight conversion cleanups, pointlight port

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

View file

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

View file

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

View file

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

View file

@ -24,12 +24,11 @@
#include "shadergen:/autogenConditioners.h"
#include "farFrustumQuad.glsl"
#include "lightingUtils.glsl"
#include "../../../gl/lighting.glsl"
#include "../../shadowMap/shadowMapIO_GLSL.h"
#include "softShadow.glsl"
#include "../../../gl/torque.glsl"
#line 31
in vec4 wsEyeDir;
in vec4 ssPos;
in vec4 vsEyeDir;
@ -38,7 +37,7 @@ in vec4 color;
#ifdef USE_COOKIE_TEX
/// The texture for cookie rendering.
uniform samplerCube cookieMap ;
uniform samplerCube cookieMap;
#endif
@ -85,7 +84,9 @@ uniform samplerCube cookieMap ;
// this value was found via experementation
// NOTE: this is wrong, it only biases in one direction, not towards the uv
// 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
@ -122,153 +123,86 @@ uniform vec3 lightPosition;
uniform vec4 lightColor;
uniform float lightBrightness;
uniform float lightRange;
uniform vec2 lightAttenuation;
uniform vec4 lightMapParams;
uniform vec4 vsFarPlane;
uniform mat3 viewToLightProj;
uniform mat3 dynamicViewToLightProj;
uniform vec4 lightParams;
uniform float lightInvSqrRange;
uniform float shadowSoftness;
uniform mat3 worldToLightProj;
uniform mat3 dynamicWorldToLightProj;
uniform vec3 eyePosWorld;
uniform mat4 cameraToWorld;
out vec4 OUT_col;
out vec4 OUT_col1;
void main()
{
// Compute scene UV
vec3 ssPos = ssPos.xyz / ssPos.w;
vec2 uvScene = getUVFromSSPos( ssPos, rtParams0 );
vec2 uvScene = getUVFromSSPos(ssPos.xyz/ssPos.w, 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
bool emissive = getFlag( matInfo.r, 0 );
if ( emissive )
if (getFlag(surface.matFlag, 0))
{
OUT_col = vec4(0.0, 0.0, 0.0, 0.0);
OUT_col1 = vec4(0.0, 0.0, 0.0, 0.0);
return;
OUT_col = vec4(0.0);
return;
}
vec4 colorSample = texture( colorBuffer, uvScene );
vec3 subsurface = vec3(0.0,0.0,0.0);
if (getFlag( matInfo.r, 1 ))
vec3 L = lightPosition - surface.P;
float dist = length(L);
vec3 lighting = vec3(0.0);
if(dist < lightRange)
{
subsurface = colorSample.rgb;
if (colorSample.r>colorSample.g)
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 );
float distToLight = dist / lightRange;
SurfaceToLight surfaceToLight = createSurfaceToLight(surface, L);
#ifdef NO_SHADOW
float shadowed = 1.0;
#else
// Get a linear depth from the light source.
float distToLight = lenLightV / lightRange;
#ifdef SHADOW_CUBE
// 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 ) ) );
#else
vec2 shadowCoord = decodeShadowCoord( tMul( viewToLightProj, -lightVec ) ).xy;
float static_shadowed = softShadow_filter( shadowMap,
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 );
vec2 shadowCoord = decodeShadowCoord( tMul( worldToLightProj, -surfaceToLight.L ) ).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 dynamic_shadowed = softShadow_filter(dynamicShadowMap, ssPos.xy, dynShadowCoord, shadowSoftness, distToLight, surfaceToLight.NdotL, lightParams.y);
float shadowed = min(static_shadowed, dynamic_shadowed);
#endif
#endif // !NO_SHADOW
vec3 lightcol = lightColor.rgb;
vec3 lightCol = lightColor.rgb;
#ifdef USE_COOKIE_TEX
// 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.
lightcol *= cookie.rgb;
lightCol *= cookie.rgb;
// Use a maximum channel luminance to attenuate
// the lighting else we get specular in the dark
// regions of the cookie texture.
atten *= max( cookie.r, max( cookie.g, cookie.b ) );
lightCol *= max(cookie.r, max(cookie.g, cookie.b));
#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);
vec3 v = eyeRay;// normalize(eyePosWorld - worldPos.xyz);
//get punctual light contribution
lighting = getPunctualLight(surface, surfaceToLight, lightCol, lightBrightness, lightInvSqrRange, shadowed);
}
vec3 h = normalize(v + l);
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);
OUT_col = vec4(lighting, 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 "shadergen:/autogenConditioners.h"
#include "farFrustumQuad.glsl"
#include "../../../gl/lighting.glsl"
#include "../../../gl/torque.glsl"
#line 7
#include "../../../gl/lighting.glsl"
#line 6
in vec4 pos;
in vec4 wsEyeDir;

View file

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