Update leenkx/Shaders/std/light.glsl

This commit is contained in:
Onek8 2025-04-07 15:45:04 +00:00
parent 5244b1b3e8
commit 519039b8b6

View File

@ -1,239 +1,244 @@
#ifndef _LIGHT_GLSL_ #ifndef _LIGHT_GLSL_
#define _LIGHT_GLSL_ #define _LIGHT_GLSL_
#include "compiled.inc" #include "compiled.inc"
#include "std/brdf.glsl" #include "std/brdf.glsl"
#include "std/math.glsl" #include "std/math.glsl"
#ifdef _ShadowMap #ifdef _ShadowMap
#include "std/shadows.glsl" #include "std/shadows.glsl"
#endif #endif
#ifdef _VoxelShadow #ifdef _VoxelShadow
#include "std/conetrace.glsl" #include "std/conetrace.glsl"
//!uniform sampler2D voxels_shadows; uniform sampler2D gbuffer2;
#endif #else
#ifdef _LTC #ifdef _VoxelGI
#include "std/ltc.glsl" uniform sampler2D gbuffer2;
#endif #endif
#ifdef _LightIES #endif
#include "std/ies.glsl" #ifdef _LTC
#endif #include "std/ltc.glsl"
#ifdef _SSRS #endif
#include "std/ssrs.glsl" #ifdef _LightIES
#endif #include "std/ies.glsl"
#ifdef _Spot #endif
#include "std/light_common.glsl" #ifdef _SSRS
#endif #include "std/ssrs.glsl"
#endif
#ifdef _ShadowMap #ifdef _Spot
#ifdef _SinglePoint #include "std/light_common.glsl"
#ifdef _Spot #endif
#ifndef _LTC
uniform sampler2DShadow shadowMapSpot[1]; #ifdef _ShadowMap
uniform sampler2D shadowMapSpotTransparent[1]; #ifdef _SinglePoint
uniform mat4 LWVPSpot[1]; #ifdef _Spot
#endif #ifndef _LTC
#else uniform sampler2DShadow shadowMapSpot[1];
uniform samplerCubeShadow shadowMapPoint[1]; uniform sampler2D shadowMapSpotTransparent[1];
uniform samplerCube shadowMapPointTransparent[1]; uniform mat4 LWVPSpot[1];
uniform vec2 lightProj; #endif
#endif #else
#endif uniform samplerCubeShadow shadowMapPoint[1];
#ifdef _Clusters uniform samplerCube shadowMapPointTransparent[1];
#ifdef _SingleAtlas uniform vec2 lightProj;
//!uniform sampler2DShadow shadowMapAtlas; #endif
//!uniform sampler2D shadowMapAtlasTransparent; #endif
#endif #ifdef _Clusters
uniform vec2 lightProj; #ifdef _SingleAtlas
#ifdef _ShadowMapAtlas //!uniform sampler2DShadow shadowMapAtlas;
#ifndef _SingleAtlas //!uniform sampler2D shadowMapAtlasTransparent;
uniform sampler2DShadow shadowMapAtlasPoint; #endif
uniform sampler2D shadowMapAtlasPointTransparent; uniform vec2 lightProj;
#endif #ifdef _ShadowMapAtlas
#else #ifndef _SingleAtlas
uniform samplerCubeShadow shadowMapPoint[4]; uniform sampler2DShadow shadowMapAtlasPoint;
uniform samplerCube shadowMapPointTransparent[4]; uniform sampler2D shadowMapAtlasPointTransparent;
#endif #endif
#ifdef _Spot #else
#ifdef _ShadowMapAtlas uniform samplerCubeShadow shadowMapPoint[4];
#ifndef _SingleAtlas uniform samplerCube shadowMapPointTransparent[4];
uniform sampler2DShadow shadowMapAtlasSpot; #endif
uniform sampler2D shadowMapAtlasSpotTransparent; #ifdef _Spot
#endif #ifdef _ShadowMapAtlas
#else #ifndef _SingleAtlas
uniform sampler2DShadow shadowMapSpot[4]; uniform sampler2DShadow shadowMapAtlasSpot;
uniform sampler2D shadowMapSpotTransparent[4]; uniform sampler2D shadowMapAtlasSpotTransparent;
#endif #endif
uniform mat4 LWVPSpotArray[maxLightsCluster]; #else
#endif uniform sampler2DShadow shadowMapSpot[4];
#endif uniform sampler2D shadowMapSpotTransparent[4];
#endif #endif
uniform mat4 LWVPSpotArray[maxLightsCluster];
#ifdef _LTC #endif
uniform vec3 lightArea0; #endif
uniform vec3 lightArea1; #endif
uniform vec3 lightArea2;
uniform vec3 lightArea3; #ifdef _LTC
uniform sampler2D sltcMat; uniform vec3 lightArea0;
uniform sampler2D sltcMag; uniform vec3 lightArea1;
#ifdef _ShadowMap uniform vec3 lightArea2;
#ifndef _Spot uniform vec3 lightArea3;
#ifdef _SinglePoint uniform sampler2D sltcMat;
uniform sampler2DShadow shadowMapSpot[1]; uniform sampler2D sltcMag;
uniform sampler2D shadowMapSpotTransparent[1]; #ifdef _ShadowMap
uniform mat4 LWVPSpot[1]; #ifndef _Spot
#endif #ifdef _SinglePoint
#ifdef _Clusters uniform sampler2DShadow shadowMapSpot[1];
uniform sampler2DShadow shadowMapSpot[maxLightsCluster]; uniform sampler2D shadowMapSpotTransparent[1];
uniform sampler2D shadowMapSpotTransparent[maxLightsCluster]; uniform mat4 LWVPSpot[1];
uniform mat4 LWVPSpotArray[maxLightsCluster]; #endif
#endif #ifdef _Clusters
#endif uniform sampler2DShadow shadowMapSpot[maxLightsCluster];
#endif uniform sampler2D shadowMapSpotTransparent[maxLightsCluster];
#endif uniform mat4 LWVPSpotArray[maxLightsCluster];
#endif
vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, const vec3 lp, const vec3 lightCol, #endif
const vec3 albedo, const float rough, const float spec, const vec3 f0 #endif
#ifdef _ShadowMap #endif
, int index, float bias, bool receiveShadow, bool transparent
#endif vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, const vec3 lp, const vec3 lightCol,
#ifdef _Spot const vec3 albedo, const float rough, const float spec, const vec3 f0
, const bool isSpot, const float spotSize, float spotBlend, vec3 spotDir, vec2 scale, vec3 right #ifdef _ShadowMap
#endif , int index, float bias, bool receiveShadow, bool transparent
#ifdef _VoxelShadow #endif
, sampler3D voxels, sampler3D voxelsSDF, float clipmaps[10 * voxelgiClipmapCount] #ifdef _Spot
#endif , const bool isSpot, const float spotSize, float spotBlend, vec3 spotDir, vec2 scale, vec3 right
#ifdef _MicroShadowing #endif
, float occ #ifdef _VoxelShadow
#endif , sampler3D voxels, sampler3D voxelsSDF, float clipmaps[10 * voxelgiClipmapCount]
#ifdef _SSRS #endif
, sampler2D gbufferD, mat4 invVP, vec3 eye #ifdef _MicroShadowing
#endif , float occ
) { #endif
vec3 ld = lp - p; #ifdef _SSRS
vec3 l = normalize(ld); , sampler2D gbufferD, mat4 invVP, vec3 eye
vec3 h = normalize(v + l); #endif
float dotNH = max(0.0, dot(n, h)); ) {
float dotVH = max(0.0, dot(v, h)); vec3 ld = lp - p;
float dotNL = max(0.0, dot(n, l)); vec3 l = normalize(ld);
vec3 h = normalize(v + l);
#ifdef _LTC float dotNH = max(0.0, dot(n, h));
float theta = acos(dotNV); float dotVH = max(0.0, dot(v, h));
vec2 tuv = vec2(rough, theta / (0.5 * PI)); float dotNL = max(0.0, dot(n, l));
tuv = tuv * LUT_SCALE + LUT_BIAS;
vec4 t = textureLod(sltcMat, tuv, 0.0); #ifdef _LTC
mat3 invM = mat3( float theta = acos(dotNV);
vec3(1.0, 0.0, t.y), vec2 tuv = vec2(rough, theta / (0.5 * PI));
vec3(0.0, t.z, 0.0), tuv = tuv * LUT_SCALE + LUT_BIAS;
vec3(t.w, 0.0, t.x)); vec4 t = textureLod(sltcMat, tuv, 0.0);
float ltcspec = ltcEvaluate(n, v, dotNV, p, invM, lightArea0, lightArea1, lightArea2, lightArea3); mat3 invM = mat3(
ltcspec *= textureLod(sltcMag, tuv, 0.0).a; vec3(1.0, 0.0, t.y),
float ltcdiff = ltcEvaluate(n, v, dotNV, p, mat3(1.0), lightArea0, lightArea1, lightArea2, lightArea3); vec3(0.0, t.z, 0.0),
vec3 direct = albedo * ltcdiff + ltcspec * spec * 0.05; vec3(t.w, 0.0, t.x));
#else float ltcspec = ltcEvaluate(n, v, dotNV, p, invM, lightArea0, lightArea1, lightArea2, lightArea3);
vec3 direct = lambertDiffuseBRDF(albedo, dotNL) + ltcspec *= textureLod(sltcMag, tuv, 0.0).a;
specularBRDF(f0, rough, dotNL, dotNH, dotNV, dotVH) * spec; float ltcdiff = ltcEvaluate(n, v, dotNV, p, mat3(1.0), lightArea0, lightArea1, lightArea2, lightArea3);
#endif vec3 direct = albedo * ltcdiff + ltcspec * spec * 0.05;
#else
direct *= attenuate(distance(p, lp)); vec3 direct = lambertDiffuseBRDF(albedo, dotNL) +
direct *= lightCol; specularBRDF(f0, rough, dotNL, dotNH, dotNV, dotVH) * spec;
#endif
#ifdef _MicroShadowing
direct *= clamp(dotNL + 2.0 * occ * occ - 1.0, 0.0, 1.0); direct *= attenuate(distance(p, lp));
#endif direct *= lightCol;
#ifdef _SSRS #ifdef _MicroShadowing
direct *= traceShadowSS(l, p, gbufferD, invVP, eye); direct *= clamp(dotNL + 2.0 * occ * occ - 1.0, 0.0, 1.0);
#endif #endif
#ifdef _VoxelShadow #ifdef _SSRS
direct *= (1.0 - traceShadow(p, n, voxels, voxelsSDF, l, clipmaps, gl_FragCoord.xy).r) * voxelgiShad; direct *= traceShadowSS(l, p, gbufferD, invVP, eye);
#endif #endif
#ifdef _LTC #ifdef _VoxelShadow
#ifdef _ShadowMap vec4 g2 = textureLod(gbuffer2, gl_FragCoord.xy, 0.0);
if (receiveShadow) { direct *= (1.0 - traceShadow(p, n, voxels, voxelsSDF, l, clipmaps, gl_FragCoord.xy, g2.rg).r) * voxelgiShad;
#ifdef _SinglePoint #endif
vec4 lPos = LWVPSpotArray[0] * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest(shadowMapSpot[0], shadowMapSpotTransparent[0], lPos.xyz / lPos.w, bias, transparent); #ifdef _LTC
#endif #ifdef _ShadowMap
#ifdef _Clusters if (receiveShadow) {
vec4 lPos = LWVPSpotArray[index] * vec4(p + n * bias * 10, 1.0); #ifdef _SinglePoint
if (index == 0) direct *= shadowTest(shadowMapSpot[0], shadowMapSpotTransparent[0], lPos.xyz / lPos.w, bias, transparent); vec4 lPos = LWVPSpotArray[0] * vec4(p + n * bias * 10, 1.0);
else if (index == 1) direct *= shadowTest(shadowMapSpot[1], shadowMapSpotTransparent[1], lPos.xyz / lPos.w, bias, transparent); direct *= shadowTest(shadowMapSpot[0], shadowMapSpotTransparent[0], lPos.xyz / lPos.w, bias, transparent);
else if (index == 2) direct *= shadowTest(shadowMapSpot[2], shadowMapSpotTransparent[2], lPos.xyz / lPos.w, bias, transparent); #endif
else if (index == 3) direct *= shadowTest(shadowMapSpot[3], shadowMapSpotTransparent[3], lPos.xyz / lPos.w, bias, transparent); #ifdef _Clusters
#endif vec4 lPos = LWVPSpotArray[index] * vec4(p + n * bias * 10, 1.0);
} if (index == 0) direct *= shadowTest(shadowMapSpot[0], shadowMapSpotTransparent[0], lPos.xyz / lPos.w, bias, transparent);
#endif else if (index == 1) direct *= shadowTest(shadowMapSpot[1], shadowMapSpotTransparent[1], lPos.xyz / lPos.w, bias, transparent);
return direct; else if (index == 2) direct *= shadowTest(shadowMapSpot[2], shadowMapSpotTransparent[2], lPos.xyz / lPos.w, bias, transparent);
#endif else if (index == 3) direct *= shadowTest(shadowMapSpot[3], shadowMapSpotTransparent[3], lPos.xyz / lPos.w, bias, transparent);
#endif
#ifdef _Spot }
if (isSpot) { #endif
direct *= spotlightMask(l, spotDir, right, scale, spotSize, spotBlend); return direct;
#endif
#ifdef _ShadowMap
if (receiveShadow) { #ifdef _Spot
#ifdef _SinglePoint if (isSpot) {
vec4 lPos = LWVPSpot[0] * vec4(p + n * bias * 10, 1.0); direct *= spotlightMask(l, spotDir, right, scale, spotSize, spotBlend);
direct *= shadowTest(shadowMapSpot[0], shadowMapSpotTransparent[0], lPos.xyz / lPos.w, bias, transparent);
#endif #ifdef _ShadowMap
#ifdef _Clusters if (receiveShadow) {
vec4 lPos = LWVPSpotArray[index] * vec4(p + n * bias * 10, 1.0); #ifdef _SinglePoint
#ifdef _ShadowMapAtlas vec4 lPos = LWVPSpot[0] * vec4(p + n * bias * 10, 1.0);
direct *= shadowTest( direct *= shadowTest(shadowMapSpot[0], shadowMapSpotTransparent[0], lPos.xyz / lPos.w, bias, transparent);
#ifndef _SingleAtlas #endif
shadowMapAtlasSpot, shadowMapAtlasSpotTransparent #ifdef _Clusters
#else vec4 lPos = LWVPSpotArray[index] * vec4(p + n * bias * 10, 1.0);
shadowMapAtlas, shadowMapAtlasTransparent #ifdef _ShadowMapAtlas
#endif direct *= shadowTest(
, lPos.xyz / lPos.w, bias, transparent #ifndef _SingleAtlas
); shadowMapAtlasSpot, shadowMapAtlasSpotTransparent
#else #else
if (index == 0) direct *= shadowTest(shadowMapSpot[0], shadowMapSpotTransparent[0], lPos.xyz / lPos.w, bias, transparent); shadowMapAtlas, shadowMapAtlasTransparent
else if (index == 1) direct *= shadowTest(shadowMapSpot[1], shadowMapSpotTransparent[1], lPos.xyz / lPos.w, bias, transparent); #endif
else if (index == 2) direct *= shadowTest(shadowMapSpot[2], shadowMapSpotTransparent[2], lPos.xyz / lPos.w, bias, transparent); , lPos.xyz / lPos.w, bias, transparent
else if (index == 3) direct *= shadowTest(shadowMapSpot[3], shadowMapSpotTransparent[3], lPos.xyz / lPos.w, bias, transparent); );
#endif #else
#endif if (index == 0) direct *= shadowTest(shadowMapSpot[0], shadowMapSpotTransparent[0], lPos.xyz / lPos.w, bias, transparent);
} else if (index == 1) direct *= shadowTest(shadowMapSpot[1], shadowMapSpotTransparent[1], lPos.xyz / lPos.w, bias, transparent);
#endif else if (index == 2) direct *= shadowTest(shadowMapSpot[2], shadowMapSpotTransparent[2], lPos.xyz / lPos.w, bias, transparent);
return direct; else if (index == 3) direct *= shadowTest(shadowMapSpot[3], shadowMapSpotTransparent[3], lPos.xyz / lPos.w, bias, transparent);
} #endif
#endif #endif
}
#ifdef _LightIES #endif
direct *= iesAttenuation(-l); return direct;
#endif }
#endif
#ifdef _ShadowMap
if (receiveShadow) { #ifdef _LightIES
#ifdef _SinglePoint direct *= iesAttenuation(-l);
#ifndef _Spot #endif
direct *= PCFCube(shadowMapPoint[0], shadowMapPointTransparent[0], ld, -l, bias, lightProj, n, transparent);
#endif #ifdef _ShadowMap
#endif if (receiveShadow) {
#ifdef _Clusters #ifdef _SinglePoint
#ifdef _ShadowMapAtlas #ifndef _Spot
direct *= PCFFakeCube( direct *= PCFCube(shadowMapPoint[0], shadowMapPointTransparent[0], ld, -l, bias, lightProj, n, transparent);
#ifndef _SingleAtlas #endif
shadowMapAtlasPoint, shadowMapAtlasPointTransparent #endif
#else #ifdef _Clusters
shadowMapAtlas, shadowMapAtlasTransparent #ifdef _ShadowMapAtlas
#endif direct *= PCFFakeCube(
, ld, -l, bias, lightProj, n, index, transparent #ifndef _SingleAtlas
); shadowMapAtlasPoint, shadowMapAtlasPointTransparent
#else #else
if (index == 0) direct *= PCFCube(shadowMapPoint[0], shadowMapPointTransparent[0], ld, -l, bias, lightProj, n, transparent); shadowMapAtlas, shadowMapAtlasTransparent
else if (index == 1) direct *= PCFCube(shadowMapPoint[1], shadowMapPointTransparent[1], ld, -l, bias, lightProj, n, transparent); #endif
else if (index == 2) direct *= PCFCube(shadowMapPoint[2], shadowMapPointTransparent[2], ld, -l, bias, lightProj, n, transparent); , ld, -l, bias, lightProj, n, index, transparent
else if (index == 3) direct *= PCFCube(shadowMapPoint[3], shadowMapPointTransparent[3], ld, -l, bias, lightProj, n, transparent); );
#endif #else
#endif if (index == 0) direct *= PCFCube(shadowMapPoint[0], shadowMapPointTransparent[0], ld, -l, bias, lightProj, n, transparent);
} else if (index == 1) direct *= PCFCube(shadowMapPoint[1], shadowMapPointTransparent[1], ld, -l, bias, lightProj, n, transparent);
#endif else if (index == 2) direct *= PCFCube(shadowMapPoint[2], shadowMapPointTransparent[2], ld, -l, bias, lightProj, n, transparent);
else if (index == 3) direct *= PCFCube(shadowMapPoint[3], shadowMapPointTransparent[3], ld, -l, bias, lightProj, n, transparent);
return direct; #endif
} #endif
}
#endif #endif
return direct;
}
#endif