Skip to content

Commit

Permalink
Refactor the way lit-shader integrates lightmapping (#7388)
Browse files Browse the repository at this point in the history
* Refactor the way lit-shader integrates lightmapping

* whitespce

---------

Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Feb 27, 2025
1 parent 213918e commit b132b62
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 72 deletions.
2 changes: 0 additions & 2 deletions src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import immediateLineVS from './internal/vert/immediateLine.js';
// import lightDirPointPS from './lit/frag/lightDirPoint.js';
// import lightingPS from './lit/frag/lighting/lighting.js';
// import lightmapAddPS from './lit/frag/lightmapAdd.js';
// import lightmapDirAddPS from './lit/frag/lightmapDirAdd.js';
// import lightmapDirPS from './standard/frag/lightmapDir.js';
// import lightmapSinglePS from './standard/frag/lightmapSingle.js';
// import lightSpecularAnisoGGXPS from './lit/frag/lightSpecularAnisoGGX.js';
Expand Down Expand Up @@ -285,7 +284,6 @@ const shaderChunksWGSL = {
// lightDirPointPS,
// lightingPS,
// lightmapAddPS,
// lightmapDirAddPS,
// lightmapDirPS,
// lightmapSinglePS,
// lightSpecularAnisoGGXPS,
Expand Down
4 changes: 2 additions & 2 deletions src/scene/shader-lib/chunks/chunk-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const chunkVersions = {
fresnelSchlickPS: CHUNKAPI_1_65,
iridescenceDiffractionPS: CHUNKAPI_1_65,
lightmapAddPS: CHUNKAPI_1_65,
lightmapDirAddPS: CHUNKAPI_1_65,
refractionCubePS: CHUNKAPI_1_70,
refractionDynamicPS: CHUNKAPI_1_70
};
Expand Down Expand Up @@ -120,7 +119,8 @@ const removedChunks = {
endVS: CHUNKAPI_2_6,
baseVS: CHUNKAPI_2_6,
baseNineSlicedVS: CHUNKAPI_2_6,
viewNormalVS: CHUNKAPI_2_6
viewNormalVS: CHUNKAPI_2_6,
lightmapDirAddPS: CHUNKAPI_2_6
};

// compare two "major.minor" semantic version strings and return true if a is a smaller version than b.
Expand Down
2 changes: 0 additions & 2 deletions src/scene/shader-lib/chunks/chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import lightDiffuseLambertPS from './lit/frag/lightDiffuseLambert.js';
import lightDirPointPS from './lit/frag/lightDirPoint.js';
import lightingPS from './lit/frag/lighting/lighting.js';
import lightmapAddPS from './lit/frag/lightmapAdd.js';
import lightmapDirAddPS from './lit/frag/lightmapDirAdd.js';
import lightmapDirPS from './standard/frag/lightmapDir.js';
import lightmapSinglePS from './standard/frag/lightmapSingle.js';
import lightSpecularAnisoGGXPS from './lit/frag/lightSpecularAnisoGGX.js';
Expand Down Expand Up @@ -285,7 +284,6 @@ const shaderChunks = {
lightDirPointPS,
lightingPS,
lightmapAddPS,
lightmapDirAddPS,
lightmapDirPS,
lightmapSinglePS,
lightSpecularAnisoGGXPS,
Expand Down
38 changes: 37 additions & 1 deletion src/scene/shader-lib/chunks/lit/frag/lightmapAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ void addLightMap(
float iridescenceIntensity
#endif
) {
dDiffuseLight += lightmap;
// directional lightmap
#if defined(LIT_SPECULAR) && defined(LIT_DIR_LIGHTMAP)
if (dot(dir, dir) < 0.0001) {
dDiffuseLight += lightmap;
} else {
float vlight = saturate(dot(dir, -vertexNormal));
float flight = saturate(dot(dir, -worldNormal));
float nlight = (flight / max(vlight, 0.01)) * 0.5;
dDiffuseLight += lightmap * nlight * 2.0;
vec3 halfDir = normalize(-dir + viewDir);
vec3 specularLight = lightmap * getLightSpecular(halfDir, reflectionDir, worldNormal, viewDir, dir, gloss, tbn);
#ifdef LIT_SPECULAR_FRESNEL
specularLight *=
getFresnel(dot(viewDir, halfDir),
gloss,
specularity
#if defined(LIT_IRIDESCENCE)
, iridescenceFresnel,
iridescenceIntensity
#endif
);
#endif
dSpecularLight += specularLight;
}
#else // non-directional lightmap
dDiffuseLight += lightmap;
#endif
}
`;
44 changes: 0 additions & 44 deletions src/scene/shader-lib/chunks/lit/frag/lightmapDirAdd.js

This file was deleted.

48 changes: 27 additions & 21 deletions src/scene/shader-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ class LitShader {
this.fDefineSet(options.useCubeMapRotation, 'CUBEMAP_ROTATION');
this.fDefineSet(options.occludeSpecularFloat, 'LIT_OCCLUDE_SPECULAR_FLOAT');
this.fDefineSet(options.twoSidedLighting, 'LIT_TWO_SIDED_LIGHTING');
this.fDefineSet(options.lightMapEnabled, 'LIT_LIGHTMAP');
this.fDefineSet(options.dirLightMapEnabled, 'LIT_DIR_LIGHTMAP');

// FRAGMENT SHADER INPUTS: UNIFORMS

Expand Down Expand Up @@ -711,12 +713,14 @@ class LitShader {
}
}

func.append(chunks.combinePS);
func.append(`
// lightmap support
if (options.lightMapEnabled) {
func.append((options.useSpecular && options.dirLightMapEnabled) ? chunks.lightmapDirAddPS : chunks.lightmapAddPS);
}
#include "combinePS"
#ifdef LIT_LIGHTMAP
#include "lightmapAddPS"
#endif
`);

const addAmbient = !options.lightMapEnabled || options.lightMapWithoutAmbient;

Expand Down Expand Up @@ -873,23 +877,25 @@ class LitShader {
backend.append(' occludeDiffuse(litArgs_ao);');
}

if (options.lightMapEnabled) {
backend.append(` addLightMap(
litArgs_lightmap,
litArgs_lightmapDir,
litArgs_worldNormal,
dViewDirW,
dReflDirW,
litArgs_gloss,
litArgs_specularity,
dVertexNormalW,
dTBN
#if defined(LIT_IRIDESCENCE)
, iridescenceFresnel,
litArgs_iridescence_intensity
backend.append(`
#ifdef LIT_LIGHTMAP
addLightMap(
litArgs_lightmap,
litArgs_lightmapDir,
litArgs_worldNormal,
dViewDirW,
dReflDirW,
litArgs_gloss,
litArgs_specularity,
dVertexNormalW,
dTBN
#if defined(LIT_IRIDESCENCE)
, iridescenceFresnel,
litArgs_iridescence_intensity
#endif
);
#endif
);`);
}
`);

if (this.lighting || this.reflections) {
if (this.reflections) {
Expand Down

0 comments on commit b132b62

Please sign in to comment.