Skip to content

Commit 178cd31

Browse files
mvaligurskyMartin Valigursky
and
Martin Valigursky
authored
Refactor lightmap part of shader generation by the standard materials (#7527)
Co-authored-by: Martin Valigursky <[email protected]>
1 parent ec6a625 commit 178cd31

File tree

8 files changed

+69
-55
lines changed

8 files changed

+69
-55
lines changed

src/framework/lightmapper/lightmapper.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,20 @@ class Lightmapper {
248248
dDiffuseLight += vec3(${scene.ambientBakeOcclusionBrightness.toFixed(1)});
249249
dDiffuseLight = saturate(dDiffuseLight);
250250
dDiffuseLight *= dAmbientLight;
251-
${bakeLmEndChunk}`;
251+
${bakeLmEndChunk}
252+
`;
252253
} else {
253254
material.ambient = new Color(0, 0, 0); // don't bake ambient
254255
}
255256
material.chunks.basePS = shaderChunks.basePS + (this.bakeHDR ? '' : '\n#define LIGHTMAP_RGBM\n');
256257
material.chunks.endPS = bakeLmEndChunk;
257258
material.lightMap = this.blackTex;
258259
} else {
259-
material.chunks.basePS = `${shaderChunks.basePS}\nuniform sampler2D texture_dirLightMap;\nuniform float bakeDir;\n`;
260+
material.chunks.basePS = `
261+
#define STD_LIGHTMAP_DIR
262+
${shaderChunks.basePS}
263+
uniform float bakeDir;
264+
`;
260265
material.chunks.endPS = shaderChunksLightmapper.bakeDirLmEndPS;
261266
}
262267

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ import immediateLineVS from './internal/vert/immediateLine.js';
6666
// import lightFunctionShadowPS from './lit/frag/lighting/lightFunctionShadow.js';
6767
// import lightingPS from './lit/frag/lighting/lighting.js';
6868
// import lightmapAddPS from './lit/frag/lightmapAdd.js';
69-
// import lightmapDirPS from './standard/frag/lightmapDir.js';
70-
// import lightmapSinglePS from './standard/frag/lightmapSingle.js';
69+
// import lightmapPS from './standard/frag/lightmap.js';
7170
// import lightSpecularAnisoGGXPS from './lit/frag/lightSpecularAnisoGGX.js';
7271
// import lightSpecularBlinnPS from './lit/frag/lightSpecularBlinn.js';
7372
// import lightSheenPS from './lit/frag/lightSheen.js';
@@ -274,8 +273,7 @@ const shaderChunksWGSL = {
274273
// lightFunctionShadowPS,
275274
// lightingPS,
276275
// lightmapAddPS,
277-
// lightmapDirPS,
278-
// lightmapSinglePS,
276+
// lightmapPS,
279277
// lightSpecularAnisoGGXPS,
280278
// lightSpecularBlinnPS,
281279
// lightSheenPS,

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ const chunkVersions = {
1414
diffusePS: CHUNKAPI_1_57,
1515
emissivePS: CHUNKAPI_1_57,
1616
glossPS: CHUNKAPI_1_60,
17-
lightmapDirPS: CHUNKAPI_1_55,
18-
lightmapSinglePS: CHUNKAPI_1_55,
1917
metalnessPS: CHUNKAPI_1_57,
2018
normalMapPS: CHUNKAPI_1_57,
2119
opacityPS: CHUNKAPI_1_57,
@@ -129,7 +127,9 @@ const removedChunks = {
129127
normalDetailMapPS: CHUNKAPI_2_7,
130128
normalXYPS: CHUNKAPI_2_7,
131129
normalXYZPS: CHUNKAPI_2_7,
132-
aoDetailMapPS: CHUNKAPI_2_7
130+
aoDetailMapPS: CHUNKAPI_2_7,
131+
lightmapDirPS: CHUNKAPI_2_7,
132+
lightmapSinglePS: CHUNKAPI_2_7
133133
};
134134

135135
// compare two "major.minor" semantic version strings and return true if a is a smaller version than b.

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ import lightFunctionLightPS from './lit/frag/lighting/lightFunctionLight.js';
6969
import lightFunctionShadowPS from './lit/frag/lighting/lightFunctionShadow.js';
7070
import lightingPS from './lit/frag/lighting/lighting.js';
7171
import lightmapAddPS from './lit/frag/lightmapAdd.js';
72-
import lightmapDirPS from './standard/frag/lightmapDir.js';
73-
import lightmapSinglePS from './standard/frag/lightmapSingle.js';
72+
import lightmapPS from './standard/frag/lightmap.js';
7473
import lightSpecularAnisoGGXPS from './lit/frag/lightSpecularAnisoGGX.js';
7574
import lightSpecularBlinnPS from './lit/frag/lightSpecularBlinn.js';
7675
import lightSheenPS from './lit/frag/lightSheen.js';
@@ -279,8 +278,7 @@ const shaderChunks = {
279278
lightFunctionShadowPS,
280279
lightingPS,
281280
lightmapAddPS,
282-
lightmapDirPS,
283-
lightmapSinglePS,
281+
lightmapPS,
284282
lightSpecularAnisoGGXPS,
285283
lightSpecularBlinnPS,
286284
lightSheenPS,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default /* glsl */`
2+
3+
#ifdef STD_LIGHTMAP_DIR
4+
vec3 dLightmapDir;
5+
uniform sampler2D texture_dirLightMap;
6+
#endif
7+
8+
void getLightMap() {
9+
10+
dLightmap = vec3(1.0);
11+
12+
#ifdef STD_LIGHT_TEXTURE
13+
dLightmap *= {STD_LIGHT_TEXTURE_DECODE}(texture2DBias({STD_LIGHT_TEXTURE_NAME}, {STD_LIGHT_TEXTURE_UV}, textureBias)).{STD_LIGHT_TEXTURE_CHANNEL};
14+
15+
#ifdef STD_LIGHTMAP_DIR
16+
vec3 dir = texture2DBias(texture_dirLightMap, {STD_LIGHT_TEXTURE_UV}, textureBias).xyz * 2.0 - 1.0;
17+
float dirDot = dot(dir, dir);
18+
dLightmapDir = (dirDot > 0.001) ? dir / sqrt(dirDot) : vec3(0.0);
19+
#endif
20+
#endif
21+
22+
#ifdef STD_LIGHT_VERTEX
23+
dLightmap *= saturate(vVertexColor.{STD_LIGHT_VERTEX_CHANNEL});
24+
#endif
25+
}
26+
`;

src/scene/shader-lib/chunks/standard/frag/lightmapDir.js

-12
This file was deleted.

src/scene/shader-lib/chunks/standard/frag/lightmapSingle.js

-13
This file was deleted.

src/scene/shader-lib/programs/standard.js

+29-17
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ class ShaderGeneratorStandard extends ShaderGenerator {
298298
}
299299
};
300300

301+
fDefineSet(options.lightMap, 'STD_LIGHTMAP', '');
302+
fDefineSet(options.lightVertexColor, 'STD_LIGHT_VERTEX_COLOR', '');
303+
fDefineSet(options.dirLightMap && options.litOptions.useSpecular, 'STD_LIGHTMAP_DIR', '');
301304
fDefineSet(options.heightMap, 'STD_HEIGHT_MAP', '');
302305
fDefineSet(options.useSpecularColor, 'STD_SPECULAR_COLOR', '');
303306
fDefineSet(options.aoMap || options.aoVertexColor || options.useAO, 'STD_AO', '');
@@ -510,6 +513,14 @@ class ShaderGeneratorStandard extends ShaderGenerator {
510513
uniform sampler2D texture_clearCoatNormalMap;
511514
#endif
512515
#endif
516+
517+
// lightmap
518+
#if defined(STD_LIGHTMAP) || defined(STD_LIGHT_VERTEX_COLOR)
519+
vec3 dLightmap;
520+
#ifdef STD_LIGHT_TEXTURE_ALLOCATE
521+
uniform sampler2D texture_lightMap;
522+
#endif
523+
#endif
513524
#endif
514525
`);
515526

@@ -597,6 +608,11 @@ class ShaderGeneratorStandard extends ShaderGenerator {
597608
#include "clearCoatGlossPS"
598609
#include "clearCoatNormalPS"
599610
#endif
611+
612+
// lightmap
613+
#if defined(STD_LIGHTMAP) || defined(STD_LIGHT_VERTEX_COLOR)
614+
#include "lightmapPS"
615+
#endif
600616
#endif
601617
`);
602618

@@ -706,6 +722,16 @@ class ShaderGeneratorStandard extends ShaderGenerator {
706722
litArgs_clearcoat_gloss = ccGlossiness;
707723
litArgs_clearcoat_worldNormal = ccNormalW;
708724
#endif
725+
726+
// lightmap
727+
#if defined(STD_LIGHTMAP) || defined(STD_LIGHT_VERTEX_COLOR)
728+
getLightMap();
729+
litArgs_lightmap = dLightmap;
730+
731+
#ifdef STD_LIGHTMAP_DIR
732+
litArgs_lightmapDir = dLightmapDir;
733+
#endif
734+
#endif
709735
#endif
710736
`);
711737

@@ -793,24 +819,9 @@ class ShaderGeneratorStandard extends ShaderGenerator {
793819
this._addMap(fDefines, 'clearCoatNormal', 'clearCoatNormalPS', options, litShader.chunks, textureMapping, options.clearCoatPackedNormal ? 'xy' : 'xyz');
794820
}
795821

796-
797-
// STILL TO DO -------------
798-
799-
800822
// lightmap
801823
if (options.lightMap || options.lightVertexColor) {
802-
const lightmapDir = (options.dirLightMap && options.litOptions.useSpecular);
803-
const lightmapChunkPropName = lightmapDir ? 'lightmapDirPS' : 'lightmapSinglePS';
804-
decl.append('vec3 dLightmap;');
805-
if (lightmapDir) {
806-
decl.append('vec3 dLightmapDir;');
807-
}
808-
code.append(this._addMap(fDefines, 'light', lightmapChunkPropName, options, litShader.chunks, textureMapping, options.lightMapEncoding));
809-
func.append('getLightMap();');
810-
args.append('litArgs_lightmap = dLightmap;');
811-
if (lightmapDir) {
812-
args.append('litArgs_lightmapDir = dLightmapDir;');
813-
}
824+
this._addMap(fDefines, 'light', 'lightmapPS', options, litShader.chunks, textureMapping, options.lightMapEncoding);
814825
}
815826

816827
} else {
@@ -851,7 +862,8 @@ class ShaderGeneratorStandard extends ShaderGenerator {
851862
'texture_emissiveMap',
852863
'texture_clearCoatMap',
853864
'texture_clearCoatGlossMap',
854-
'texture_clearCoatNormalMap'
865+
'texture_clearCoatNormalMap',
866+
'texture_lightMap'
855867
];
856868

857869
// TODO: when refactoring is done, this loop will be removed

0 commit comments

Comments
 (0)