Skip to content

Commit

Permalink
Refactor of lit-shader generator, mostly related to the ambient lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Valigursky committed Mar 5, 2025
1 parent 726c0bc commit d2eb253
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 162 deletions.
46 changes: 12 additions & 34 deletions src/scene/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,44 +748,11 @@ export const specularOcclusionNames = {
[SPECOCC_GLOSSDEPENDENT]: 'GLOSSDEPENDENT'
};

/**
* There is no reflection source.
*
* @type {string}
* @category Graphics
*/
// reflection source used by the shader generation
export const REFLECTIONSRC_NONE = 'none';

/**
* EnvAtlas is used as a source for the reflection.
*
* @type {string}
* @category Graphics
*/
export const REFLECTIONSRC_ENVATLAS = 'envAtlas';

/**
* EnvAtlas and high resolution cubemap are used as a source for the reflection.
*
* @type {string}
* @category Graphics
*/
export const REFLECTIONSRC_ENVATLASHQ = 'envAtlasHQ';

/**
* Cubemap is used as a source for the reflection.
*
* @type {string}
* @category Graphics
*/
export const REFLECTIONSRC_CUBEMAP = 'cubeMap';

/**
* Spheremap is used as a source for the reflection.
*
* @type {string}
* @category Graphics
*/
export const REFLECTIONSRC_SPHEREMAP = 'sphereMap';

export const reflectionSrcNames = {
Expand All @@ -796,6 +763,17 @@ export const reflectionSrcNames = {
[REFLECTIONSRC_SPHEREMAP]: 'SPHEREMAP'
};

// ambient source used by the shader generation
export const AMBIENTSRC_AMBIENTSH = 'ambientSH';
export const AMBIENTSRC_ENVALATLAS = 'envAtlas';
export const AMBIENTSRC_CONSTANT = 'constant';

export const ambientSrcNames = {
[AMBIENTSRC_AMBIENTSH]: 'AMBIENTSH',
[AMBIENTSRC_ENVALATLAS]: 'ENVALATLAS',
[AMBIENTSRC_CONSTANT]: 'CONSTANT'
};

// 16 bits for shader defs
export const SHADERDEF_NOSHADOW = 1;
export const SHADERDEF_SKIN = 2;
Expand Down
9 changes: 5 additions & 4 deletions src/scene/materials/lit-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
SHADERDEF_NOSHADOW, SHADERDEF_TANGENTS, SPRITE_RENDERMODE_SIMPLE,
SHADERDEF_MORPH_TEXTURE_BASED_INT,
FOG_NONE,
REFLECTIONSRC_NONE, REFLECTIONSRC_ENVATLAS, REFLECTIONSRC_ENVATLASHQ, REFLECTIONSRC_CUBEMAP
REFLECTIONSRC_NONE, REFLECTIONSRC_ENVATLAS, REFLECTIONSRC_ENVATLASHQ, REFLECTIONSRC_CUBEMAP,
AMBIENTSRC_AMBIENTSH, AMBIENTSRC_ENVALATLAS, AMBIENTSRC_CONSTANT
} from '../constants.js';

class LitMaterialOptionsBuilder {
Expand Down Expand Up @@ -112,13 +113,13 @@ class LitMaterialOptionsBuilder {

// source of environment ambient is as follows:
if (material.ambientSH) {
litOptions.ambientSource = 'ambientSH';
litOptions.ambientSource = AMBIENTSRC_AMBIENTSH;
litOptions.ambientEncoding = null;
} else if (litOptions.reflectionSource !== REFLECTIONSRC_NONE && scene.envAtlas) {
litOptions.ambientSource = 'envAtlas';
litOptions.ambientSource = AMBIENTSRC_ENVALATLAS;
litOptions.ambientEncoding = scene.envAtlas.encoding;
} else {
litOptions.ambientSource = 'constant';
litOptions.ambientSource = AMBIENTSRC_CONSTANT;
litOptions.ambientEncoding = null;
}

Expand Down
9 changes: 5 additions & 4 deletions src/scene/materials/standard-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
DITHER_NONE,
SHADERDEF_MORPH_TEXTURE_BASED_INT, SHADERDEF_BATCH,
FOG_NONE,
REFLECTIONSRC_NONE, REFLECTIONSRC_ENVATLAS, REFLECTIONSRC_ENVATLASHQ, REFLECTIONSRC_CUBEMAP, REFLECTIONSRC_SPHEREMAP
REFLECTIONSRC_NONE, REFLECTIONSRC_ENVATLAS, REFLECTIONSRC_ENVATLASHQ, REFLECTIONSRC_CUBEMAP, REFLECTIONSRC_SPHEREMAP,
AMBIENTSRC_AMBIENTSH, AMBIENTSRC_ENVALATLAS, AMBIENTSRC_CONSTANT
} from '../constants.js';
import { _matTex2D } from '../shader-lib/programs/standard.js';
import { LitMaterialOptionsBuilder } from './lit-material-options-builder.js';
Expand Down Expand Up @@ -325,15 +326,15 @@ class StandardMaterialOptionsBuilder {

// source of environment ambient is as follows:
if (stdMat.ambientSH) {
options.litOptions.ambientSource = 'ambientSH';
options.litOptions.ambientSource = AMBIENTSRC_AMBIENTSH;
options.litOptions.ambientEncoding = null;
} else {
const envAtlas = stdMat.envAtlas || (stdMat.useSkybox && scene.envAtlas ? scene.envAtlas : null);
if (envAtlas && !stdMat.sphereMap) {
options.litOptions.ambientSource = 'envAtlas';
options.litOptions.ambientSource = AMBIENTSRC_ENVALATLAS;
options.litOptions.ambientEncoding = envAtlas.encoding;
} else {
options.litOptions.ambientSource = 'constant';
options.litOptions.ambientSource = AMBIENTSRC_CONSTANT;
options.litOptions.ambientEncoding = null;
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/scene/shader-lib/chunks-wgsl/chunks-wgsl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// import alphaTestPS from './standard/frag/alphaTest.js';
// import ambientConstantPS from './lit/frag/ambientConstant.js';
// import ambientEnvPS from './lit/frag/ambientEnv.js';
// import ambientSHPS from './lit/frag/ambientSH.js';
// import ambientPS from './lit/frag/ambient.js';
// import aoPS from './standard/frag/ao.js';
// import aoDetailMapPS from './standard/frag/aoDetailMap.js';
// import aoDiffuseOccPS from './lit/frag/aoDiffuseOcc.js';
Expand Down Expand Up @@ -199,9 +197,7 @@ import tonemappingNonePS from './common/frag/tonemapping/tonemappingNone.js';
*/
const shaderChunksWGSL = {
// alphaTestPS,
// ambientConstantPS,
// ambientEnvPS,
// ambientSHPS,
// ambientPS,
// aoPS,
// aoDetailMapPS,
// aoDiffuseOccPS,
Expand Down
9 changes: 4 additions & 5 deletions src/scene/shader-lib/chunks/chunk-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ const chunkVersions = {

// backend
normalVertexPS: CHUNKAPI_1_55,

ambientConstantPS: CHUNKAPI_1_62,
ambientEnvPS: CHUNKAPI_1_62,
ambientSHPS: CHUNKAPI_1_62,
aoDiffuseOccPS: CHUNKAPI_1_62,
aoSpecOccPS: CHUNKAPI_2_6,
clusteredLightPS: CHUNKAPI_1_62,
Expand Down Expand Up @@ -126,7 +122,10 @@ const removedChunks = {
envConstPS: CHUNKAPI_2_6,
aoSpecOccConstPS: CHUNKAPI_2_6,
aoSpecOccConstSimplePS: CHUNKAPI_2_6,
aoSpecOccSimplePS: CHUNKAPI_2_6
aoSpecOccSimplePS: CHUNKAPI_2_6,
ambientConstantPS: CHUNKAPI_2_6,
ambientEnvPS: CHUNKAPI_2_6,
ambientSHPS: CHUNKAPI_2_6
};

// compare two "major.minor" semantic version strings and return true if a is a smaller version than b.
Expand Down
8 changes: 2 additions & 6 deletions src/scene/shader-lib/chunks/chunks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import alphaTestPS from './standard/frag/alphaTest.js';
import ambientConstantPS from './lit/frag/ambientConstant.js';
import ambientEnvPS from './lit/frag/ambientEnv.js';
import ambientSHPS from './lit/frag/ambientSH.js';
import ambientPS from './lit/frag/ambient.js';
import aoPS from './standard/frag/ao.js';
import aoDetailMapPS from './standard/frag/aoDetailMap.js';
import aoDiffuseOccPS from './lit/frag/aoDiffuseOcc.js';
Expand Down Expand Up @@ -199,9 +197,7 @@ import webgpuVS from '../../../platform/graphics/shader-chunks/vert/webgpu.js';
*/
const shaderChunks = {
alphaTestPS,
ambientConstantPS,
ambientEnvPS,
ambientSHPS,
ambientPS,
aoPS,
aoDetailMapPS,
aoDiffuseOccPS,
Expand Down
6 changes: 6 additions & 0 deletions src/scene/shader-lib/chunks/common/frag/envAtlas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default /* glsl */`
#ifndef _ENVATLAS_INCLUDED_
#define _ENVATLAS_INCLUDED_
// the envAtlas is fixed at 512 pixels. every equirect is generated with 1 pixel boundary.
const float atlasSize = 512.0;
const float seamSize = 1.0 / atlasSize;
Expand All @@ -20,4 +24,6 @@ vec2 mapShinyUv(vec2 uv, float level) {
float t = 1.0 / exp2(level);
return mapUv(uv, vec4(1.0 - t, 1.0 - t, t, t * 0.5));
}
#endif
`;
52 changes: 52 additions & 0 deletions src/scene/shader-lib/chunks/lit/frag/ambient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export default /* glsl */`
#ifdef LIT_AMBIENT_SOURCE == AMBIENTSH
uniform vec3 ambientSH[9];
#endif
#if LIT_AMBIENT_SOURCE == ENVALATLAS
#include "envAtlasPS"
#ifndef ENV_ATLAS
#define ENV_ATLAS
uniform sampler2D texture_envAtlas;
#endif
#endif
void addAmbient(vec3 worldNormal) {
#ifdef LIT_AMBIENT_SOURCE == AMBIENTSH
vec3 n = cubeMapRotate(worldNormal);
vec3 color =
ambientSH[0] +
ambientSH[1] * n.x +
ambientSH[2] * n.y +
ambientSH[3] * n.z +
ambientSH[4] * n.x * n.z +
ambientSH[5] * n.z * n.y +
ambientSH[6] * n.y * n.x +
ambientSH[7] * (3.0 * n.z * n.z - 1.0) +
ambientSH[8] * (n.x * n.x - n.y * n.y);
dDiffuseLight += processEnvironment(max(color, vec3(0.0)));
#endif
#if LIT_AMBIENT_SOURCE == ENVALATLAS
vec3 dir = normalize(cubeMapRotate(worldNormal) * vec3(-1.0, 1.0, 1.0));
vec2 uv = mapUv(toSphericalUv(dir), vec4(128.0, 256.0 + 128.0, 64.0, 32.0) / atlasSize);
vec4 raw = texture2D(texture_envAtlas, uv);
vec3 linear = {ambientDecode}(raw);
dDiffuseLight += processEnvironment(linear);
#endif
#if LIT_AMBIENT_SOURCE == CONSTANT
dDiffuseLight += light_globalAmbient;
#endif
}
`;
5 changes: 0 additions & 5 deletions src/scene/shader-lib/chunks/lit/frag/ambientConstant.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/scene/shader-lib/chunks/lit/frag/ambientEnv.js

This file was deleted.

20 changes: 0 additions & 20 deletions src/scene/shader-lib/chunks/lit/frag/ambientSH.js

This file was deleted.

Loading

0 comments on commit d2eb253

Please sign in to comment.