Skip to content

Commit

Permalink
Refactor reflection-source related chunk and code (#7401)
Browse files Browse the repository at this point in the history
* Refactor reflection-source related chunk and code

* updated validation version

* small rearangement

---------

Co-authored-by: Martin Valigursky <[email protected]>
  • Loading branch information
mvaligursky and Martin Valigursky authored Mar 5, 2025
1 parent adb05e9 commit 84dc61c
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 64 deletions.
48 changes: 48 additions & 0 deletions src/scene/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,54 @@ export const specularOcclusionNames = {
[SPECOCC_GLOSSDEPENDENT]: 'GLOSSDEPENDENT'
};

/**
* There is no reflection source.
*
* @type {string}
* @category Graphics
*/
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 = {
[REFLECTIONSRC_NONE]: 'NONE',
[REFLECTIONSRC_ENVATLAS]: 'ENVATLAS',
[REFLECTIONSRC_ENVATLASHQ]: 'ENVATLASHQ',
[REFLECTIONSRC_CUBEMAP]: 'CUBEMAP',
[REFLECTIONSRC_SPHEREMAP]: 'SPHEREMAP'
};

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

class LitMaterialOptionsBuilder {
Expand Down Expand Up @@ -95,33 +96,33 @@ class LitMaterialOptionsBuilder {

// source of reflections
if (material.useSkybox && scene.envAtlas && scene.skybox) {
litOptions.reflectionSource = 'envAtlasHQ';
litOptions.reflectionSource = REFLECTIONSRC_ENVATLASHQ;
litOptions.reflectionEncoding = scene.envAtlas.encoding;
litOptions.reflectionCubemapEncoding = scene.skybox.encoding;
} else if (material.useSkybox && scene.envAtlas) {
litOptions.reflectionSource = 'envAtlas';
litOptions.reflectionSource = REFLECTIONSRC_ENVATLAS;
litOptions.reflectionEncoding = scene.envAtlas.encoding;
} else if (material.useSkybox && scene.skybox) {
litOptions.reflectionSource = 'cubeMap';
litOptions.reflectionSource = REFLECTIONSRC_CUBEMAP;
litOptions.reflectionEncoding = scene.skybox.encoding;
} else {
litOptions.reflectionSource = null;
litOptions.reflectionSource = REFLECTIONSRC_NONE;
litOptions.reflectionEncoding = null;
}

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

const hasSkybox = !!litOptions.reflectionSource;
const hasSkybox = litOptions.reflectionSource !== REFLECTIONSRC_NONE;
litOptions.skyboxIntensity = hasSkybox;
litOptions.useCubeMapRotation = hasSkybox && scene._skyboxRotationShaderInclude;
}
Expand Down
21 changes: 11 additions & 10 deletions src/scene/materials/standard-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
TONEMAP_NONE,
DITHER_NONE,
SHADERDEF_MORPH_TEXTURE_BASED_INT, SHADERDEF_BATCH,
FOG_NONE
FOG_NONE,
REFLECTIONSRC_NONE, REFLECTIONSRC_ENVATLAS, REFLECTIONSRC_ENVATLASHQ, REFLECTIONSRC_CUBEMAP, REFLECTIONSRC_SPHEREMAP
} from '../constants.js';
import { _matTex2D } from '../shader-lib/programs/standard.js';
import { LitMaterialOptionsBuilder } from './lit-material-options-builder.js';
Expand Down Expand Up @@ -274,7 +275,7 @@ class StandardMaterialOptionsBuilder {
options.litOptions.useSpecularityFactor = (specularityFactorTint || !!stdMat.specularityFactorMap) && stdMat.useMetalnessSpecularColor;
options.litOptions.enableGGXSpecular = stdMat.enableGGXSpecular;
options.litOptions.fresnelModel = stdMat.fresnelModel;
options.litOptions.useRefraction = (stdMat.refraction || !!stdMat.refractionMap) && (stdMat.useDynamicRefraction || !!options.litOptions.reflectionSource);
options.litOptions.useRefraction = (stdMat.refraction || !!stdMat.refractionMap) && (stdMat.useDynamicRefraction || options.litOptions.reflectionSource !== REFLECTIONSRC_NONE);
options.litOptions.useClearCoat = !!stdMat.clearCoat;
options.litOptions.useSheen = stdMat.useSheen;
options.litOptions.useIridescence = stdMat.useIridescence && stdMat.iridescence !== 0.0;
Expand All @@ -292,33 +293,33 @@ class StandardMaterialOptionsBuilder {

// source of environment reflections is as follows:
if (stdMat.envAtlas && stdMat.cubeMap) {
options.litOptions.reflectionSource = 'envAtlasHQ';
options.litOptions.reflectionSource = REFLECTIONSRC_ENVATLASHQ;
options.litOptions.reflectionEncoding = stdMat.envAtlas.encoding;
options.litOptions.reflectionCubemapEncoding = stdMat.cubeMap.encoding;
} else if (stdMat.envAtlas) {
options.litOptions.reflectionSource = 'envAtlas';
options.litOptions.reflectionSource = REFLECTIONSRC_ENVATLAS;
options.litOptions.reflectionEncoding = stdMat.envAtlas.encoding;
} else if (stdMat.cubeMap) {
options.litOptions.reflectionSource = 'cubeMap';
options.litOptions.reflectionSource = REFLECTIONSRC_CUBEMAP;
options.litOptions.reflectionEncoding = stdMat.cubeMap.encoding;
} else if (stdMat.sphereMap) {
options.litOptions.reflectionSource = 'sphereMap';
options.litOptions.reflectionSource = REFLECTIONSRC_SPHEREMAP;
options.litOptions.reflectionEncoding = stdMat.sphereMap.encoding;
} else if (stdMat.useSkybox && scene.envAtlas && scene.skybox) {
options.litOptions.reflectionSource = 'envAtlasHQ';
options.litOptions.reflectionSource = REFLECTIONSRC_ENVATLASHQ;
options.litOptions.reflectionEncoding = scene.envAtlas.encoding;
options.litOptions.reflectionCubemapEncoding = scene.skybox.encoding;
usingSceneEnv = true;
} else if (stdMat.useSkybox && scene.envAtlas) {
options.litOptions.reflectionSource = 'envAtlas';
options.litOptions.reflectionSource = REFLECTIONSRC_ENVATLAS;
options.litOptions.reflectionEncoding = scene.envAtlas.encoding;
usingSceneEnv = true;
} else if (stdMat.useSkybox && scene.skybox) {
options.litOptions.reflectionSource = 'cubeMap';
options.litOptions.reflectionSource = REFLECTIONSRC_CUBEMAP;
options.litOptions.reflectionEncoding = scene.skybox.encoding;
usingSceneEnv = true;
} else {
options.litOptions.reflectionSource = null;
options.litOptions.reflectionSource = REFLECTIONSRC_NONE;
options.litOptions.reflectionEncoding = null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/scene/shader-lib/chunk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const encodeTable = {
class ChunkUtils {
// returns the name of the decode function for the texture encoding
static decodeFunc(encoding) {
return decodeTable[encoding] || 'decodeGamma';
return decodeTable[encoding] ?? 'decodeGamma';
}

static encodeFunc(encoding) {
return encodeTable[encoding] || 'encodeGamma';
return encodeTable[encoding] ?? 'encodeGamma';
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/scene/shader-lib/chunks/chunk-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ const chunkVersions = {
reflDirPS: CHUNKAPI_1_62,
reflDirAnisoPS: CHUNKAPI_1_62,
reflectionCCPS: CHUNKAPI_1_62,
reflectionCubePS: CHUNKAPI_1_62,
reflectionEnvPS: CHUNKAPI_1_62,
reflectionEnvHQPS: CHUNKAPI_1_62,
reflectionCubePS: CHUNKAPI_2_6,
reflectionEnvPS: CHUNKAPI_2_6,
reflectionEnvHQPS: CHUNKAPI_2_6,
reflectionSpherePS: CHUNKAPI_2_6,
reflectionSheenPS: CHUNKAPI_1_62,
reflectionSpherePS: CHUNKAPI_1_62,
shadowCommonPS: CHUNKAPI_1_62,
shadowCoordPS: CHUNKAPI_1_62,
shadowCoordPerspZBufferPS: CHUNKAPI_1_62,
Expand Down
2 changes: 1 addition & 1 deletion src/scene/shader-lib/chunks/lit/frag/reflectionCube.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ uniform float material_reflectivity;
vec3 calcReflection(vec3 reflDir, float gloss) {
vec3 lookupVec = cubeMapProject(reflDir);
lookupVec.x *= -1.0;
return $DECODE(textureCube(texture_cubeMap, lookupVec));
return {reflectionDecode}(textureCube(texture_cubeMap, lookupVec));
}
void addReflection(vec3 reflDir, float gloss) {
Expand Down
6 changes: 3 additions & 3 deletions src/scene/shader-lib/chunks/lit/frag/reflectionEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ vec3 calcReflection(vec3 reflDir, float gloss) {
weight = 0.0;
}
vec3 linearA = $DECODE(texture2D(texture_envAtlas, uv0));
vec3 linearB = $DECODE(texture2D(texture_envAtlas, uv1));
vec3 linearA = {reflectionDecode}(texture2D(texture_envAtlas, uv0));
vec3 linearB = {reflectionDecode}(texture2D(texture_envAtlas, uv1));
vec3 linear0 = mix(linearA, linearB, weight);
vec3 linear1 = $DECODE(texture2D(texture_envAtlas, mapRoughnessUv(uv, ilevel + 1.0)));
vec3 linear1 = {reflectionDecode}(texture2D(texture_envAtlas, mapRoughnessUv(uv, ilevel + 1.0)));
return processEnvironment(mix(linear0, linear1, level - ilevel));
}
Expand Down
6 changes: 3 additions & 3 deletions src/scene/shader-lib/chunks/lit/frag/reflectionEnvHQ.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ vec3 calcReflection(vec3 reflDir, float gloss) {
float ilevel = floor(level);
float flevel = level - ilevel;
vec3 sharp = $DECODE_CUBEMAP(textureCube(texture_cubeMap, dir));
vec3 roughA = $DECODE(texture2D(texture_envAtlas, mapRoughnessUv(uv, ilevel)));
vec3 roughB = $DECODE(texture2D(texture_envAtlas, mapRoughnessUv(uv, ilevel + 1.0)));
vec3 sharp = {reflectionCubemapDecode}(textureCube(texture_cubeMap, dir));
vec3 roughA = {reflectionDecode}(texture2D(texture_envAtlas, mapRoughnessUv(uv, ilevel)));
vec3 roughB = {reflectionDecode}(texture2D(texture_envAtlas, mapRoughnessUv(uv, ilevel + 1.0)));
return processEnvironment(mix(sharp, mix(roughA, roughB, flevel), min(level, 1.0)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/scene/shader-lib/chunks/lit/frag/reflectionSphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ vec3 calcReflection(vec3 reflDir, float gloss) {
float m = 2.0 * sqrt( dot(reflDirV.xy, reflDirV.xy) + (reflDirV.z+1.0)*(reflDirV.z+1.0) );
vec2 sphereMapUv = reflDirV.xy / m + 0.5;
return $DECODE(texture2D(texture_sphereMap, sphereMapUv));
return {reflectionDecode}(texture2D(texture_sphereMap, sphereMapUv));
}
void addReflection(vec3 reflDir, float gloss) {
Expand Down
6 changes: 3 additions & 3 deletions src/scene/shader-lib/programs/lit-shader-options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BLEND_NONE, DITHER_NONE, FOG_NONE, GAMMA_NONE } from '../../constants.js';
import { BLEND_NONE, DITHER_NONE, FOG_NONE, GAMMA_NONE, REFLECTIONSRC_NONE } from '../../constants.js';

/**
* The lit shader options determines how the lit-shader gets generated. It specifies a set of
Expand Down Expand Up @@ -252,11 +252,11 @@ class LitShaderOptions {
toneMap = -1;

/**
* One of "envAtlasHQ", "envAtlas", "cubeMap", "sphereMap".
* One of REFLECTIONSRC_*** constants.
*
* @type {string}
*/
reflectionSource = null;
reflectionSource = REFLECTIONSRC_NONE;

reflectionEncoding = null;

Expand Down
Loading

0 comments on commit 84dc61c

Please sign in to comment.