From 23b908da56c9369446dcd99d23287538c8691d0e Mon Sep 17 00:00:00 2001 From: AzaezelX Date: Tue, 15 Mar 2022 19:26:37 -0500 Subject: [PATCH] crashfix and projection fix for spotlights with cookies --- .../lighting/advanced/gl/spotLightP.glsl | 24 +++++++------- .../shaders/lighting/advanced/spotLightP.hlsl | 31 +++++++++---------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Templates/BaseGame/game/core/rendering/shaders/lighting/advanced/gl/spotLightP.glsl b/Templates/BaseGame/game/core/rendering/shaders/lighting/advanced/gl/spotLightP.glsl index 6c80cc1eb..5d0079afa 100644 --- a/Templates/BaseGame/game/core/rendering/shaders/lighting/advanced/gl/spotLightP.glsl +++ b/Templates/BaseGame/game/core/rendering/shaders/lighting/advanced/gl/spotLightP.glsl @@ -96,6 +96,8 @@ void main() if(dist < lightRange) { SurfaceToLight surfaceToLight = createSurfaceToLight(surface, L); + vec3 lightCol = lightColor.rgb; + #ifdef NO_SHADOW float shadowed = 1.0; #else @@ -107,20 +109,18 @@ void main() //distance to light in shadow map space float distToLight = pxlPosLightProj.z / lightRange; float shadowed = softShadow_filter(shadowMap, ssPos.xy/ssPos.w, shadowCoord, shadowSoftness, distToLight, surfaceToLight.NdotL, lightParams.y); + #ifdef USE_COOKIE_TEX + // Lookup the cookie sample. + vec4 cookie = texture(cookieMap, shadowCoord); + // Multiply the light with the cookie tex. + lightCol *= cookie.rgb; + // Use a maximum channel luminance to attenuate + // the lighting else we get specular in the dark + // regions of the cookie texture. + lightCol *= max(cookie.r, max(cookie.g, cookie.b)); + #endif #endif - vec3 lightCol = lightColor.rgb; - #ifdef USE_COOKIE_TEX - - // Lookup the cookie sample. - vec4 cookie = texture(cookieMap, tMul(worldToLightProj, -surfaceToLight.L)); - // Multiply the light with the cookie tex. - lightCol *= cookie.rgb; - // Use a maximum channel luminance to attenuate - // the lighting else we get specular in the dark - // regions of the cookie texture. - lightCol *= max(cookie.r, max(cookie.g, cookie.b)); - #endif #ifdef DIFFUSE_LIGHT_VIZ float attenuation = getDistanceAtt(surfaceToLight.Lu, radius); diff --git a/Templates/BaseGame/game/core/rendering/shaders/lighting/advanced/spotLightP.hlsl b/Templates/BaseGame/game/core/rendering/shaders/lighting/advanced/spotLightP.hlsl index fb3d4aae0..15b4b4e37 100644 --- a/Templates/BaseGame/game/core/rendering/shaders/lighting/advanced/spotLightP.hlsl +++ b/Templates/BaseGame/game/core/rendering/shaders/lighting/advanced/spotLightP.hlsl @@ -43,7 +43,7 @@ TORQUE_UNIFORM_SAMPLER2D(shadowMap, 1); #ifdef USE_COOKIE_TEX /// The texture for cookie rendering. -TORQUE_UNIFORM_SAMPLER2D(cookieMap, 3); +TORQUE_UNIFORM_SAMPLER2D(cookieMap, 2); #endif TORQUE_UNIFORM_SAMPLER2D(colorBuffer, 5); @@ -101,6 +101,8 @@ float4 main( ConvexConnectP IN ) : SV_TARGET if(dist < lightRange) { SurfaceToLight surfaceToLight = createSurfaceToLight(surface, L); + float3 lightCol = lightColor.rgb; + #ifdef NO_SHADOW float shadowed = 1.0; #else @@ -108,23 +110,20 @@ float4 main( ConvexConnectP IN ) : SV_TARGET float4 pxlPosLightProj = mul( worldToLightProj, float4( surface.P, 1 ) ); float2 shadowCoord = ( ( pxlPosLightProj.xy / pxlPosLightProj.w ) * 0.5 ) + float2( 0.5, 0.5 ); shadowCoord.y = 1.0f - shadowCoord.y; - - //distance to light in shadow map space + //distance to light in shadow map space float distToLight = pxlPosLightProj.z / lightRange; float shadowed = softShadow_filter(TORQUE_SAMPLER2D_MAKEARG(shadowMap), ssPos.xy, shadowCoord, shadowSoftness, distToLight, surfaceToLight.NdotL, lightParams.y); - #endif - - float3 lightCol = lightColor.rgb; - #ifdef USE_COOKIE_TEX - // Lookup the cookie sample. - float4 cookie = TORQUE_TEXCUBE(cookieMap, mul(worldToLightProj, -surfaceToLight.L)); - // Multiply the light with the cookie tex. - lightCol *= cookie.rgb; - // Use a maximum channel luminance to attenuate - // the lighting else we get specular in the dark - // regions of the cookie texture. - lightCol *= max(cookie.r, max(cookie.g, cookie.b)); - #endif + #ifdef USE_COOKIE_TEX + // Lookup the cookie sample. + float4 cookie = TORQUE_TEX2D(cookieMap, shadowCoord); + // Multiply the light with the cookie tex. + lightCol *= cookie.rgb; + // Use a maximum channel luminance to attenuate + // the lighting else we get specular in the dark + // regions of the cookie texture. + lightCol *= max(cookie.r, max(cookie.g, cookie.b)); + #endif + #endif #ifdef DIFFUSE_LIGHT_VIZ float attenuation = getDistanceAtt(surfaceToLight.Lu, radius);