Skip to content

Commit

Permalink
Add support for linear colors (#15791)
Browse files Browse the repository at this point in the history
  • Loading branch information
Popov72 authored Nov 8, 2024
1 parent ae0d771 commit 5fbce3b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 13 additions & 2 deletions packages/dev/core/src/Layers/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export class Layer {
*/
public renderOnlyInRenderTargetTextures = false;

/**
* Define if the colors of the layer should be generated in linear space (default: false)
*/
public convertToLinearSpace = false;

/**
* Define if the layer is enabled (ie. should be displayed). Default: true
*/
Expand Down Expand Up @@ -263,8 +268,14 @@ export class Layer {
defines = "#define ALPHATEST";
}

if (this.texture && !this.texture.gammaSpace) {
defines += "\n#define LINEAR";
if (this.texture) {
if (this.texture.gammaSpace) {
if (this.convertToLinearSpace) {
defines += "\n#define CONVERT_TO_LINEAR";
}
} else if (!this.convertToLinearSpace) {
defines += "\n#define CONVERT_TO_GAMMA";
}
}

if (this._previousDefines !== defines) {
Expand Down
4 changes: 3 additions & 1 deletion packages/dev/core/src/Shaders/layer.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ void main(void) {

vec4 baseColor = texture2D(textureSampler, vUV);

#ifdef LINEAR
#if defined(CONVERT_TO_GAMMA)
baseColor.rgb = toGammaSpace(baseColor.rgb);
#elif defined(CONVERT_TO_LINEAR)
baseColor.rgb = toLinearSpace(baseColor.rgb);
#endif

#ifdef ALPHATEST
Expand Down
6 changes: 4 additions & 2 deletions packages/dev/core/src/ShadersWGSL/layer.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ fn main(input: FragmentInputs) -> FragmentOutputs {

var baseColor: vec4f = textureSample(textureSampler, textureSamplerSampler, input.vUV);

#ifdef LINEAR
baseColor = vec4f(toGammaSpace(baseColor.rgb), baseColor.a);
#if defined(CONVERT_TO_GAMMA)
baseColor = toGammaSpace(baseColor);
#elif defined(CONVERT_TO_LINEAR)
baseColor = toLinearSpaceVec4(baseColor);
#endif

#ifdef ALPHATEST
Expand Down

0 comments on commit 5fbce3b

Please sign in to comment.