Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/scene/materials/standard-material-options-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ class StandardMaterialOptionsBuilder {
options.litOptions.useDynamicRefraction = stdMat.useDynamicRefraction;
options.litOptions.dispersion = stdMat.dispersion > 0;
options.litOptions.shadowCatcher = stdMat.shadowCatcher;

options.litOptions.useVertexColorGamma = stdMat.vertexColorGamma;
}

_updateEnvOptions(options, stdMat, scene, cameraShaderParams) {
Expand Down
2 changes: 2 additions & 0 deletions src/scene/materials/standard-material-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class StandardMaterialOptions {

lightMapEncoding = 'linear';

vertexColorGamma = false;

/**
* If normal map contains X in RGB, Y in Alpha, and Z must be reconstructed.
*
Expand Down
2 changes: 2 additions & 0 deletions src/scene/materials/standard-material-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const standardMaterialParameterTypes = {
..._textureParameter('diffuseDetail', true, false),
diffuseDetailMode: 'string',

vertexColorGamma: 'boolean',

specular: 'rgb',
specularTint: 'boolean',
..._textureParameter('specular'),
Expand Down
6 changes: 6 additions & 0 deletions src/scene/materials/standard-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ const _tempColor = new Color();
* backfaces.
* @property {boolean} shadowCatcher When enabled, the material will output accumulated directional
* shadow value in linear space as the color.
* @property {boolean} vertexColorGamma When set to true, the vertex shader converts vertex colors
* from gamma to linear space to ensure correct interpolation in the fragment shader. This flag is
* provided for backwards compatibility, allowing users to mark their materials to handle vertex
* colors in gamma space. Defaults to false, which indicates that vertex colors are stored in
* linear space.
*
* @category Graphics
*/
Expand Down Expand Up @@ -1194,6 +1199,7 @@ function _defineMaterialProps() {
_defineFlag('opacityDither', DITHER_NONE);
_defineFlag('opacityShadowDither', DITHER_NONE);
_defineFlag('shadowCatcher', false);
_defineFlag('vertexColorGamma', false);

_defineTex2D('diffuse');
_defineTex2D('specular');
Expand Down
15 changes: 14 additions & 1 deletion src/scene/shader-lib/glsl/chunks/lit/vert/litMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ mat4 dModelMatrix;
#include "litUserCodeVS"
#ifdef VERTEX_COLOR
vec3 decodeGamma(vec3 raw) {
return pow(raw, vec3(2.2));
}
vec4 gammaCorrectInput(vec4 color) {
return vec4(decodeGamma(color.xyz), color.w);
}
#endif
void main(void) {
#include "litUserMainStartVS"
Expand Down Expand Up @@ -102,7 +111,11 @@ void main(void) {
#include "uvTransformVS, UV_TRANSFORMS_COUNT"
#ifdef VERTEX_COLOR
vVertexColor = vertex_color;
#ifdef STD_VERTEX_COLOR_GAMMA
vVertexColor = gammaCorrectInput(vertex_color);
#else
vVertexColor = vertex_color;
#endif
#endif
#ifdef LINEAR_DEPTH
Expand Down
2 changes: 1 addition & 1 deletion src/scene/shader-lib/glsl/chunks/standard/frag/diffuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void getAlbedo() {
#endif

#ifdef STD_DIFFUSE_VERTEX
dAlbedo *= gammaCorrectInput(saturate(vVertexColor.{STD_DIFFUSE_VERTEX_CHANNEL}));
dAlbedo *= saturate(vVertexColor.{STD_DIFFUSE_VERTEX_CHANNEL});
#endif
}
`;
2 changes: 1 addition & 1 deletion src/scene/shader-lib/glsl/chunks/standard/frag/emissive.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void getEmission() {
#endif

#ifdef STD_EMISSIVE_VERTEX
dEmission *= gammaCorrectInput(saturate(vVertexColor.{STD_EMISSIVE_VERTEX_CHANNEL}));
dEmission *= saturate(vVertexColor.{STD_EMISSIVE_VERTEX_CHANNEL});
#endif
}
`;
2 changes: 2 additions & 0 deletions src/scene/shader-lib/programs/lit-shader-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class LitShaderOptions {

vertexColors = false;

useVertexColorGamma = false;

lightMapEnabled = false;

dirLightMapEnabled = false;
Expand Down
3 changes: 3 additions & 0 deletions src/scene/shader-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ class LitShader {
attributes.vertex_color = SEMANTIC_COLOR;
vDefines.set('VERTEX_COLOR', true);
varyings.set('vVertexColor', 'vec4');
if (options.useVertexColorGamma) {
vDefines.set('STD_VERTEX_COLOR_GAMMA', '');
}
}

if (options.useMsdf && options.msdfTextAttribute) {
Expand Down
16 changes: 11 additions & 5 deletions src/scene/shader-lib/programs/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ class ShaderGeneratorStandard extends ShaderGenerator {
return expression;
}

_validateMapChunk(propName, chunkName, chunks) {
_validateMapChunk(code, propName, chunkName, chunks) {
Debug.call(() => {
// strip comments from the chunk
const code = chunks.get(chunkName).replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1');

const requiredChangeStrings = [];

// Helper function to add a formatted change string if the old syntax is found
Expand Down Expand Up @@ -170,7 +167,16 @@ class ShaderGeneratorStandard extends ShaderGenerator {
// log errors if the chunk format is deprecated (format changed in engine 2.7)
Debug.call(() => {
if (chunkCode) {
this._validateMapChunk(propNameCaps, chunkName, chunks);
// strip comments from the chunk
const code = chunks.get(chunkName).replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1');

this._validateMapChunk(code, propNameCaps, chunkName, chunks);

if (vertexColorOption) {
if (code.includes('gammaCorrectInputVec3(saturate3(vVertexColor.') || code.includes('gammaCorrectInput(saturate(vVertexColor.')) {
Debug.errorOnce(`Shader chunk ${chunkName} contains gamma correction code which is incompatible with vertexColorGamma=true. Please remove gamma correction calls from the chunk.`, { code: code });
}
}
}
});

Expand Down
15 changes: 14 additions & 1 deletion src/scene/shader-lib/wgsl/chunks/lit/vert/litMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ var<private> dModelMatrix: mat4x4f;

#include "litUserCodeVS"

#ifdef VERTEX_COLOR
fn decodeGamma3(raw: vec3f) -> vec3f {
return pow(raw, vec3f(2.2));
}
fn gammaCorrectInputVec4(color: vec4f) -> vec4f {
return vec4f(decodeGamma3(color.xyz), color.w);
}
#endif

@vertex
fn vertexMain(input : VertexInput) -> VertexOutput {

Expand Down Expand Up @@ -104,7 +113,11 @@ fn vertexMain(input : VertexInput) -> VertexOutput {
#include "uvTransformVS, UV_TRANSFORMS_COUNT"

#ifdef VERTEX_COLOR
output.vVertexColor = vertex_color;
#ifdef STD_VERTEX_COLOR_GAMMA
output.vVertexColor = gammaCorrectInputVec4(vertex_color);
#else
output.vVertexColor = vertex_color;
#endif
#endif

#ifdef LINEAR_DEPTH
Expand Down
2 changes: 1 addition & 1 deletion src/scene/shader-lib/wgsl/chunks/standard/frag/diffuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn getAlbedo() {
#endif

#ifdef STD_DIFFUSE_VERTEX
dAlbedo = dAlbedo * gammaCorrectInputVec3(saturate3(vVertexColor.{STD_DIFFUSE_VERTEX_CHANNEL}));
dAlbedo = dAlbedo * saturate3(vVertexColor.{STD_DIFFUSE_VERTEX_CHANNEL});
#endif
}
`;
2 changes: 1 addition & 1 deletion src/scene/shader-lib/wgsl/chunks/standard/frag/emissive.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn getEmission() {
#endif

#ifdef STD_EMISSIVE_VERTEX
dEmission = dEmission * gammaCorrectInputVec3(saturate3(vVertexColor.{STD_EMISSIVE_VERTEX_CHANNEL}));
dEmission = dEmission * saturate3(vVertexColor.{STD_EMISSIVE_VERTEX_CHANNEL});
#endif
}
`;
1 change: 1 addition & 0 deletions test/scene/materials/standard-material.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ describe('StandardMaterial', function () {
expect(material.useMetalness).to.equal(false);
expect(material.useMetalnessSpecularColor).to.equal(false);
expect(material.useSkybox).to.equal(true);
expect(material.vertexColorGamma).to.equal(false);
}

describe('#constructor()', function () {
Expand Down