Skip to content

Commit b6565f4

Browse files
AlaricBaraoulojjic
authored andcommitted
fix(BatchedText): Correct color rendering with Three.js Color Management
1 parent 82622e5 commit b6565f4

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

packages/troika-three-text/src/BatchedText.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Text } from "./Text.js";
2-
import { DataTexture, FloatType, RGBAFormat, Vector2, Box3, Color, DynamicDrawUsage } from "three";
2+
import { ColorManagement, DataTexture, FloatType, RGBAFormat, Vector2, Box3, Color, DynamicDrawUsage } from "three";
33
import { glyphBoundsAttrName, glyphIndexAttrName } from "./GlyphsGeometry.js";
44
import { createDerivedMaterial } from "troika-three-utils";
55
import { createTextDerivedMaterial } from "./TextDerivedMaterial.js";
@@ -425,8 +425,14 @@ function createBatchedTextMaterial (baseMaterial) {
425425
// language=GLSL
426426
vertexDefs: `
427427
uniform bool uTroikaIsOutline;
428+
// sRGB->Linear conversion function, from Three.js https://github.com/mrdoob/three.js/blob/dev/src/renderers/shaders/ShaderChunk/colorspace_pars_fragment.glsl.js
429+
vec4 sRGBTransferEOTF( in vec4 value ) {
430+
return vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.a );
431+
}
428432
vec3 troikaFloatToColor(float v) {
429-
return mod(floor(vec3(v / 65536.0, v / 256.0, v)), 256.0) / 256.0;
433+
vec3 srgbColor = mod(floor(vec3(v / 65536.0, v / 256.0, v)), 256.0) / 255.0;
434+
435+
return ${ColorManagement.enabled ? 'sRGBTransferEOTF(vec4(srgbColor, 1.0)).rgb' : 'srgbColor'};
430436
}
431437
`,
432438
// language=GLSL prefix="void main() {" suffix="}"

0 commit comments

Comments
 (0)