diff --git a/src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js b/src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js index 5b7eb59b185..419db4a0148 100644 --- a/src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js +++ b/src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js @@ -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'; @@ -285,7 +284,6 @@ const shaderChunksWGSL = { // lightDirPointPS, // lightingPS, // lightmapAddPS, - // lightmapDirAddPS, // lightmapDirPS, // lightmapSinglePS, // lightSpecularAnisoGGXPS, diff --git a/src/scene/shader-lib/chunks/chunk-validation.js b/src/scene/shader-lib/chunks/chunk-validation.js index d8ac4f1b0ed..48825fa1ba1 100644 --- a/src/scene/shader-lib/chunks/chunk-validation.js +++ b/src/scene/shader-lib/chunks/chunk-validation.js @@ -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 }; @@ -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. diff --git a/src/scene/shader-lib/chunks/chunks.js b/src/scene/shader-lib/chunks/chunks.js index 88dcfb3adc2..236c61a1f31 100644 --- a/src/scene/shader-lib/chunks/chunks.js +++ b/src/scene/shader-lib/chunks/chunks.js @@ -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'; @@ -285,7 +284,6 @@ const shaderChunks = { lightDirPointPS, lightingPS, lightmapAddPS, - lightmapDirAddPS, lightmapDirPS, lightmapSinglePS, lightSpecularAnisoGGXPS, diff --git a/src/scene/shader-lib/chunks/lit/frag/lightmapAdd.js b/src/scene/shader-lib/chunks/lit/frag/lightmapAdd.js index 7cdea51f0e1..b13dc7d4704 100644 --- a/src/scene/shader-lib/chunks/lit/frag/lightmapAdd.js +++ b/src/scene/shader-lib/chunks/lit/frag/lightmapAdd.js @@ -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 } `; diff --git a/src/scene/shader-lib/chunks/lit/frag/lightmapDirAdd.js b/src/scene/shader-lib/chunks/lit/frag/lightmapDirAdd.js deleted file mode 100644 index 0443f11b06b..00000000000 --- a/src/scene/shader-lib/chunks/lit/frag/lightmapDirAdd.js +++ /dev/null @@ -1,44 +0,0 @@ -export default /* glsl */` -void addLightMap( - vec3 lightmap, - vec3 dir, - vec3 worldNormal, - vec3 viewDir, - vec3 reflectionDir, - float gloss, - vec3 specularity, - vec3 vertexNormal, - mat3 tbn -#if defined(LIT_IRIDESCENCE) - vec3 iridescenceFresnel, - float iridescenceIntensity -#endif -) { - 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; - } -} -`; diff --git a/src/scene/shader-lib/programs/lit-shader.js b/src/scene/shader-lib/programs/lit-shader.js index b9fa4641cac..7f7adb3dc87 100644 --- a/src/scene/shader-lib/programs/lit-shader.js +++ b/src/scene/shader-lib/programs/lit-shader.js @@ -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 @@ -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; @@ -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) {