Skip to content

Commit 9401034

Browse files
mvaligurskyMartin Valigursky
andauthored
Refactor new and existing internal lighting related chunks to avoid globals (#7418)
* Refactor new and existing internal lighting related chunks to avoid globals * fre more globals removed * revert example * line reorder --------- Co-authored-by: Martin Valigursky <[email protected]>
1 parent b40f0e3 commit 9401034

File tree

15 files changed

+284
-263
lines changed

15 files changed

+284
-263
lines changed

src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ import immediateLineVS from './internal/vert/immediateLine.js';
6565
// import lightDiffuseLambertPS from './lit/frag/lightDiffuseLambert.js';
6666
// import lightDirPointPS from './lit/frag/lightDirPoint.js';
6767
// import lightEvaluationPS from './lit/frag/lighting/lightEvaluation.js';
68-
// import lightFunctionPS from './lit/frag/lighting/lightFunction.js';
68+
// import lightFunctionLightPS from './lit/frag/lighting/lightFunctionLight.js';
69+
// import lightFunctionShadowPS from './lit/frag/lighting/lightFunctionShadow.js';
6970
// import lightingPS from './lit/frag/lighting/lighting.js';
7071
// import lightmapAddPS from './lit/frag/lightmapAdd.js';
7172
// import lightmapDirPS from './standard/frag/lightmapDir.js';
@@ -273,7 +274,8 @@ const shaderChunksWGSL = {
273274
// lightDiffuseLambertPS,
274275
// lightDirPointPS,
275276
// lightEvaluationPS,
276-
// lightFunctionPS,
277+
// lightFunctionLightPS,
278+
// lightFunctionShadowPS,
277279
// lightingPS,
278280
// lightmapAddPS,
279281
// lightmapDirPS,

src/scene/shader-lib/chunks/chunks.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ import lightDeclarationPS from './lit/frag/lighting/lightDeclaration.js';
6565
import lightDiffuseLambertPS from './lit/frag/lightDiffuseLambert.js';
6666
import lightDirPointPS from './lit/frag/lightDirPoint.js';
6767
import lightEvaluationPS from './lit/frag/lighting/lightEvaluation.js';
68-
import lightFunctionPS from './lit/frag/lighting/lightFunction.js';
68+
import lightFunctionLightPS from './lit/frag/lighting/lightFunctionLight.js';
69+
import lightFunctionShadowPS from './lit/frag/lighting/lightFunctionShadow.js';
6970
import lightingPS from './lit/frag/lighting/lighting.js';
7071
import lightmapAddPS from './lit/frag/lightmapAdd.js';
7172
import lightmapDirPS from './standard/frag/lightmapDir.js';
@@ -272,7 +273,8 @@ const shaderChunks = {
272273
lightDiffuseLambertPS,
273274
lightDirPointPS,
274275
lightEvaluationPS,
275-
lightFunctionPS,
276+
lightFunctionLightPS,
277+
lightFunctionShadowPS,
276278
lightingPS,
277279
lightmapAddPS,
278280
lightmapDirPS,

src/scene/shader-lib/chunks/lit/frag/clusteredLight.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ void evaluateLight(
264264
float falloffAttenuation = 1.0;
265265
266266
// evaluate omni part of the light
267-
getLightDirPoint(light.position);
267+
vec3 lightDirW;
268+
vec3 lightDirNormW;
269+
evalOmniLight(light.position, lightDirW, lightDirNormW);
268270
269271
#ifdef CLUSTER_AREALIGHTS
270272
@@ -283,7 +285,7 @@ void evaluateLight(
283285
calcSphereLightValues(light.position, light.halfWidth, light.halfHeight);
284286
}
285287
286-
falloffAttenuation = getFalloffWindow(light.range, dLightDirW);
288+
falloffAttenuation = getFalloffWindow(light.range, lightDirW);
287289
288290
} else
289291
@@ -292,9 +294,9 @@ void evaluateLight(
292294
{ // punctual light
293295
294296
if (isClusteredLightFalloffLinear(light))
295-
falloffAttenuation = getFalloffLinear(light.range, dLightDirW);
297+
falloffAttenuation = getFalloffLinear(light.range, lightDirW);
296298
else
297-
falloffAttenuation = getFalloffInvSquared(light.range, dLightDirW);
299+
falloffAttenuation = getFalloffInvSquared(light.range, lightDirW);
298300
}
299301
300302
if (falloffAttenuation > 0.00001) {
@@ -305,25 +307,25 @@ void evaluateLight(
305307
306308
// handle light shape
307309
if (isClusteredLightRect(light)) {
308-
diffuseAttenuation = getRectLightDiffuse(worldNormal, viewDir, dLightDirW, dLightDirNormW) * 16.0;
310+
diffuseAttenuation = getRectLightDiffuse(worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;
309311
} else if (isClusteredLightDisk(light)) {
310-
diffuseAttenuation = getDiskLightDiffuse(worldNormal, viewDir, dLightDirW, dLightDirNormW) * 16.0;
312+
diffuseAttenuation = getDiskLightDiffuse(worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;
311313
} else { // sphere
312-
diffuseAttenuation = getSphereLightDiffuse(worldNormal, viewDir, dLightDirW, dLightDirNormW) * 16.0;
314+
diffuseAttenuation = getSphereLightDiffuse(worldNormal, viewDir, lightDirW, lightDirNormW) * 16.0;
313315
}
314316
315317
} else
316318
317319
#endif
318320
319321
{
320-
falloffAttenuation *= getLightDiffuse(worldNormal, viewDir, dLightDirW, dLightDirNormW);
322+
falloffAttenuation *= getLightDiffuse(worldNormal, viewDir, lightDirNormW);
321323
}
322324
323325
// spot light falloff
324326
if (isClusteredLightSpot(light)) {
325327
decodeClusterLightSpot(light);
326-
falloffAttenuation *= getSpotEffect(light.direction, light.innerConeAngleCos, light.outerConeAngleCos, dLightDirNormW);
328+
falloffAttenuation *= getSpotEffect(light.direction, light.innerConeAngleCos, light.outerConeAngleCos, lightDirNormW);
327329
}
328330
329331
#if defined(CLUSTER_COOKIES) || defined(CLUSTER_SHADOWS)
@@ -352,7 +354,7 @@ void evaluateLight(
352354
if (isClusteredLightSpot(light)) {
353355
cookieAttenuation = getCookie2DClustered(TEXTURE_PASS(cookieAtlasTexture), lightProjectionMatrix, vPositionW, light.cookieIntensity, isClusteredLightCookieRgb(light), light.cookieChannelMask);
354356
} else {
355-
cookieAttenuation = getCookieCubeClustered(TEXTURE_PASS(cookieAtlasTexture), dLightDirW, light.cookieIntensity, isClusteredLightCookieRgb(light), light.cookieChannelMask, shadowTextureResolution, shadowEdgePixels, light.omniAtlasViewport);
357+
cookieAttenuation = getCookieCubeClustered(TEXTURE_PASS(cookieAtlasTexture), lightDirW, light.cookieIntensity, isClusteredLightCookieRgb(light), light.cookieChannelMask, shadowTextureResolution, shadowEdgePixels, light.omniAtlasViewport);
356358
}
357359
}
358360
@@ -369,23 +371,23 @@ void evaluateLight(
369371
if (isClusteredLightSpot(light)) {
370372
371373
// spot shadow
372-
getShadowCoordPerspZbufferNormalOffset(lightProjectionMatrix, shadowParams, geometricNormal);
374+
vec3 shadowCoord = getShadowCoordPerspZbufferNormalOffset(lightProjectionMatrix, shadowParams, geometricNormal);
373375
374376
#if defined(CLUSTER_SHADOW_TYPE_PCF1)
375-
float shadow = getShadowSpotClusteredPCF1(SHADOWMAP_PASS(shadowAtlasTexture), dShadowCoord, shadowParams);
377+
float shadow = getShadowSpotClusteredPCF1(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);
376378
#elif defined(CLUSTER_SHADOW_TYPE_PCF3)
377-
float shadow = getShadowSpotClusteredPCF3(SHADOWMAP_PASS(shadowAtlasTexture), dShadowCoord, shadowParams);
379+
float shadow = getShadowSpotClusteredPCF3(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);
378380
#elif defined(CLUSTER_SHADOW_TYPE_PCF5)
379-
float shadow = getShadowSpotClusteredPCF5(SHADOWMAP_PASS(shadowAtlasTexture), dShadowCoord, shadowParams);
381+
float shadow = getShadowSpotClusteredPCF5(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);
380382
#elif defined(CLUSTER_SHADOW_TYPE_PCSS)
381-
float shadow = getShadowSpotClusteredPCSS(SHADOWMAP_PASS(shadowAtlasTexture), dShadowCoord, shadowParams);
383+
float shadow = getShadowSpotClusteredPCSS(SHADOWMAP_PASS(shadowAtlasTexture), shadowCoord, shadowParams);
382384
#endif
383385
falloffAttenuation *= mix(1.0, shadow, light.shadowIntensity);
384386
385387
} else {
386388
387389
// omni shadow
388-
vec3 dir = normalOffsetPointShadow(shadowParams, dLightPosW, dLightDirW, dLightDirNormW, geometricNormal); // normalBias adjusted for distance
390+
vec3 dir = normalOffsetPointShadow(shadowParams, light.position, lightDirW, lightDirNormW, geometricNormal); // normalBias adjusted for distance
389391
390392
#if defined(CLUSTER_SHADOW_TYPE_PCF1)
391393
float shadow = getShadowOmniClusteredPCF1(SHADOWMAP_PASS(shadowAtlasTexture), shadowParams, light.omniAtlasViewport, shadowEdgePixels, dir);
@@ -478,12 +480,12 @@ void evaluateLight(
478480
// specular and clear coat are material settings and get included by a define based on the material
479481
#ifdef LIT_SPECULAR
480482
481-
vec3 halfDir = normalize(-dLightDirNormW + viewDir);
483+
vec3 halfDir = normalize(-lightDirNormW + viewDir);
482484
483485
// specular
484486
#ifdef LIT_SPECULAR_FRESNEL
485487
dSpecularLight +=
486-
getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, dLightDirNormW, gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation *
488+
getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, lightDirNormW, gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation *
487489
getFresnel(
488490
dot(viewDir, halfDir),
489491
gloss,
@@ -494,19 +496,19 @@ void evaluateLight(
494496
#endif
495497
);
496498
#else
497-
dSpecularLight += getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, dLightDirNormW, gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * specularity;
499+
dSpecularLight += getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, lightDirNormW, gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * specularity;
498500
#endif
499501
500502
#ifdef LIT_CLEARCOAT
501503
#ifdef LIT_SPECULAR_FRESNEL
502-
ccSpecularLight += getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, dLightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * getFresnelCC(dot(viewDir, halfDir));
504+
ccSpecularLight += getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, lightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation * getFresnelCC(dot(viewDir, halfDir));
503505
#else
504-
ccSpecularLight += getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, dLightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation;
506+
ccSpecularLight += getLightSpecular(halfDir, clearcoatReflectionDir, clearcoat_worldNormal, viewDir, lightDirNormW, clearcoat_gloss, tbn) * falloffAttenuation * light.color * cookieAttenuation;
505507
#endif
506508
#endif
507509
508510
#ifdef LIT_SHEEN
509-
sSpecularLight += getLightSpecularSheen(halfDir, worldNormal, viewDir, dLightDirNormW, sheen_gloss) * falloffAttenuation * light.color * cookieAttenuation;
511+
sSpecularLight += getLightSpecularSheen(halfDir, worldNormal, viewDir, lightDirNormW, sheen_gloss) * falloffAttenuation * light.color * cookieAttenuation;
510512
#endif
511513
512514
#endif
@@ -515,8 +517,7 @@ void evaluateLight(
515517
516518
// Write to global attenuation values (for lightmapper)
517519
dAtten = falloffAttenuation;
518-
dAttenD = diffuseAttenuation;
519-
dAtten3 = cookieAttenuation;
520+
dLightDirNormW = lightDirNormW;
520521
}
521522
522523
void evaluateClusterLight(

src/scene/shader-lib/chunks/lit/frag/clusteredLightShadows.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
export default /* glsl */`
22
// Clustered Omni Sampling using atlas
33
4-
void _getShadowCoordPerspZbuffer(mat4 shadowMatrix, vec4 shadowParams, vec3 wPos) {
4+
vec3 _getShadowCoordPerspZbuffer(mat4 shadowMatrix, vec4 shadowParams, vec3 wPos) {
55
vec4 projPos = shadowMatrix * vec4(wPos, 1.0);
66
projPos.xyz /= projPos.w;
7-
dShadowCoord = projPos.xyz;
7+
return projPos.xyz;
88
// depth bias is already applied on render
99
}
1010
11-
void getShadowCoordPerspZbufferNormalOffset(mat4 shadowMatrix, vec4 shadowParams, vec3 normal) {
11+
vec3 getShadowCoordPerspZbufferNormalOffset(mat4 shadowMatrix, vec4 shadowParams, vec3 normal) {
1212
vec3 wPos = vPositionW + normal * shadowParams.y;
13-
_getShadowCoordPerspZbuffer(shadowMatrix, shadowParams, wPos);
13+
return _getShadowCoordPerspZbuffer(shadowMatrix, shadowParams, wPos);
1414
}
1515
1616
vec3 normalOffsetPointShadow(vec4 shadowParams, vec3 lightPos, inout vec3 lightDir, vec3 lightDirNorm, vec3 normal) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default /* glsl */`
2-
float getLightDiffuse(vec3 worldNormal, vec3 viewDir, vec3 lightDir, vec3 lightDirNorm) {
2+
float getLightDiffuse(vec3 worldNormal, vec3 viewDir, vec3 lightDirNorm) {
33
return max(dot(worldNormal, -lightDirNorm), 0.0);
44
}
55
`;
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export default /* glsl */`
2-
void getLightDirPoint(vec3 lightPosW) {
3-
dLightDirW = vPositionW - lightPosW;
4-
dLightDirNormW = normalize(dLightDirW);
5-
dLightPosW = lightPosW;
2+
void evalOmniLight(vec3 lightPosW, out vec3 lightDirW, out vec3 lightDirNormW) {
3+
lightDirW = vPositionW - lightPosW;
4+
lightDirNormW = normalize(lightDirW);
65
}
76
`;

0 commit comments

Comments
 (0)