Skip to content

Commit

Permalink
Do not create a sky shader variant based on skyIntensity being equal …
Browse files Browse the repository at this point in the history
…to 1 (#6254)

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky authored Apr 15, 2024
1 parent 2a136a0 commit 370a24a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/scene/materials/lit-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class LitMaterialOptionsBuilder {
}

const hasSkybox = !!litOptions.reflectionSource;
litOptions.skyboxIntensity = hasSkybox && (scene.skyboxIntensity !== 1 || scene.physicalUnits);
litOptions.skyboxIntensity = hasSkybox;
litOptions.useCubeMapRotation = hasSkybox && scene._skyboxRotationShaderInclude;
}

Expand Down
2 changes: 1 addition & 1 deletion src/scene/materials/standard-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class StandardMaterialOptionsBuilder {
}

// TODO: add a test for if non skybox cubemaps have rotation (when this is supported) - for now assume no non-skybox cubemap rotation
options.litOptions.skyboxIntensity = usingSceneEnv && (scene.skyboxIntensity !== 1 || scene.physicalUnits);
options.litOptions.skyboxIntensity = usingSceneEnv;
options.litOptions.useCubeMapRotation = usingSceneEnv && scene._skyboxRotationShaderInclude;
}

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 @@ -40,7 +40,6 @@ import encodePS from './common/frag/encode.js';
import endPS from './lit/frag/end.js';
import endVS from './lit/vert/end.js';
import envAtlasPS from './common/frag/envAtlas.js';
import envConstPS from './common/frag/envConst.js';
import envMultiplyPS from './common/frag/envMultiply.js';
import extensionPS from './lit/frag/extension.js';
import extensionVS from './lit/vert/extension.js';
Expand Down Expand Up @@ -253,7 +252,6 @@ const shaderChunks = {
endPS,
endVS,
envAtlasPS,
envConstPS,
envMultiplyPS,
extensionPS,
extensionVS,
Expand Down
5 changes: 0 additions & 5 deletions src/scene/shader-lib/chunks/common/frag/envConst.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/scene/shader-lib/chunks/skybox/frag/skyboxHDR.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void main(void) {
#endif
dir.x *= -1.0;
vec3 linear = $DECODE(textureCube(texture_cubeMap, fixSeamsStatic(dir, $FIXCONST)));
vec3 linear = $DECODE(textureCube(texture_cubeMap, fixSeamsStatic(dir, SKYBOX_MIP)));
gl_FragColor = vec4(gammaCorrectOutput(toneMap(processEnvironment(linear))), 1.0);
}
`;
2 changes: 1 addition & 1 deletion src/scene/shader-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ class LitShader {
if (this.needsNormal) {
func.append(chunks.cubeMapRotatePS);
func.append(options.cubeMapProjection > 0 ? chunks.cubeMapProjectBoxPS : chunks.cubeMapProjectNonePS);
func.append(options.skyboxIntensity ? chunks.envMultiplyPS : chunks.envConstPS);
func.append(chunks.envMultiplyPS);
}

if ((this.lighting && options.useSpecular) || this.reflections) {
Expand Down
32 changes: 20 additions & 12 deletions src/scene/shader-lib/programs/skybox.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@ import { SKYTYPE_INFINITE } from '../../constants.js';

class ShaderGeneratorSkybox extends ShaderGenerator {
generateKey(options) {
const sharedKey = `skybox-${options.type}-${options.encoding}-${options.useIntensity}-${options.gamma}-${options.toneMapping}-${options.skymesh}`;
return sharedKey + (options.type === 'cubemap' ? `-${options.fixSeams}-${options.mip}` : '');
const sharedKey = `skybox-${options.type}-${options.encoding}-${options.gamma}-${options.toneMapping}-${options.skymesh}`;
return sharedKey + (options.type === 'cubemap' ? `-${options.mip}` : '');
}

createShaderDefinition(device, options) {
const defines = options.skymesh === SKYTYPE_INFINITE ? '' : '#define SKYMESH\n';
const vshader = defines + shaderChunks.skyboxVS;
let fshader = defines;

// shared defines
const defines = new Map();
if (options.skymesh !== SKYTYPE_INFINITE) defines.set('SKYMESH', '');
if (options.type === 'cubemap') defines.set('SKY_CUBEMAP', '');

// fragment shader
let fshader = '';
if (options.type === 'cubemap') {

// mip level
const mip2size = [128, 64, /* 32 */ 16, 8, 4, 2];
defines.set('SKYBOX_MIP', (1 - 1 / mip2size[options.mip]).toString());

fshader += options.mip ? shaderChunks.fixCubemapSeamsStretchPS : shaderChunks.fixCubemapSeamsNonePS;
fshader += options.useIntensity ? shaderChunks.envMultiplyPS : shaderChunks.envConstPS;
fshader += shaderChunks.envMultiplyPS;
fshader += shaderChunks.decodePS;
fshader += ShaderGenerator.gammaCode(options.gamma);
fshader += ShaderGenerator.tonemapCode(options.toneMapping);
fshader += shaderChunks.skyboxHDRPS
.replace(/\$DECODE/g, ChunkUtils.decodeFunc(options.encoding))
.replace(/\$FIXCONST/g, (1 - 1 / mip2size[options.mip]) + "");
fshader += shaderChunks.skyboxHDRPS.replace(/\$DECODE/g, ChunkUtils.decodeFunc(options.encoding));
} else {
fshader += options.useIntensity ? shaderChunks.envMultiplyPS : shaderChunks.envConstPS;
fshader += shaderChunks.envMultiplyPS;
fshader += shaderChunks.decodePS;
fshader += ShaderGenerator.gammaCode(options.gamma);
fshader += ShaderGenerator.tonemapCode(options.toneMapping);
Expand All @@ -42,8 +48,10 @@ class ShaderGeneratorSkybox extends ShaderGenerator {
attributes: {
aPosition: SEMANTIC_POSITION
},
vertexCode: vshader,
fragmentCode: fshader
vertexCode: shaderChunks.skyboxVS,
vertexDefines: defines,
fragmentCode: fshader,
fragmentDefines: defines
});
}
}
Expand Down
1 change: 0 additions & 1 deletion src/scene/skybox/sky-mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class SkyMesh {
const options = {
pass: pass,
encoding: texture.encoding,
useIntensity: scene.skyboxIntensity !== 1 || scene.physicalUnits,
gamma: (pass === SHADER_FORWARDHDR ? (scene.gammaCorrection ? GAMMA_SRGBHDR : GAMMA_NONE) : scene.gammaCorrection),
toneMapping: (pass === SHADER_FORWARDHDR ? TONEMAP_LINEAR : scene.toneMapping),
skymesh: type
Expand Down

0 comments on commit 370a24a

Please sign in to comment.