Skip to content

Commit e196d78

Browse files
willeastcottMartin Valigursky
authored andcommitted
[FIX] Convert sRGB colors to linear when writing glTF (#8267)
* [FIX] Convert linear colors to sRGB when writing glTF * Drop comments * Tweak exporter * Use clone * Use array destructuring
1 parent b49d0b8 commit e196d78

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

src/extras/exporters/gltf-exporter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ class GltfExporter extends CoreExporter {
386386
const pbr = output.pbrMetallicRoughness;
387387

388388
if (!diffuse.equals(Color.WHITE) || opacity !== 1) {
389-
pbr.baseColorFactor = [diffuse.r, diffuse.g, diffuse.b, opacity];
389+
const { r, g, b } = diffuse.clone().linear();
390+
pbr.baseColorFactor = [r, g, b, opacity];
390391
}
391392

392393
if (metalness !== 1) {
@@ -402,7 +403,8 @@ class GltfExporter extends CoreExporter {
402403
this.attachTexture(resources, mat, pbr, 'metallicRoughnessTexture', 'metalnessMap', json);
403404

404405
if (!emissive.equals(Color.BLACK)) {
405-
output.emissiveFactor = [emissive.r, emissive.g, emissive.b];
406+
const { r, g, b } = emissive.clone().linear();
407+
output.emissiveFactor = [r, g, b];
406408
}
407409
}
408410

src/framework/parsers/glb-parser.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -867,12 +867,11 @@ const extractTextureTransform = (source, material, maps) => {
867867
};
868868

869869
const extensionPbrSpecGlossiness = (data, material, textures) => {
870-
let color, texture;
870+
let texture;
871871
if (data.hasOwnProperty('diffuseFactor')) {
872-
color = data.diffuseFactor;
873-
// Convert from linear space to sRGB space
874-
material.diffuse.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
875-
material.opacity = color[3];
872+
const [r, g, b, a] = data.diffuseFactor;
873+
material.diffuse.set(r, g, b).gamma();
874+
material.opacity = a;
876875
} else {
877876
material.diffuse.set(1, 1, 1);
878877
material.opacity = 1;
@@ -890,9 +889,8 @@ const extensionPbrSpecGlossiness = (data, material, textures) => {
890889
}
891890
material.useMetalness = false;
892891
if (data.hasOwnProperty('specularFactor')) {
893-
color = data.specularFactor;
894-
// Convert from linear space to sRGB space
895-
material.specular.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
892+
const [r, g, b] = data.specularFactor;
893+
material.specular.set(r, g, b).gamma();
896894
} else {
897895
material.specular.set(1, 1, 1);
898896
}
@@ -986,8 +984,8 @@ const extensionSpecular = (data, material, textures) => {
986984
}
987985

988986
if (data.hasOwnProperty('specularColorFactor')) {
989-
const color = data.specularColorFactor;
990-
material.specular.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
987+
const [r, g, b] = data.specularColorFactor;
988+
material.specular.set(r, g, b).gamma();
991989
} else {
992990
material.specular.set(1, 1, 1);
993991
}
@@ -1034,8 +1032,8 @@ const extensionTransmission = (data, material, textures) => {
10341032
const extensionSheen = (data, material, textures) => {
10351033
material.useSheen = true;
10361034
if (data.hasOwnProperty('sheenColorFactor')) {
1037-
const color = data.sheenColorFactor;
1038-
material.sheen.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
1035+
const [r, g, b] = data.sheenColorFactor;
1036+
material.sheen.set(r, g, b).gamma();
10391037
} else {
10401038
material.sheen.set(1, 1, 1);
10411039
}
@@ -1070,8 +1068,8 @@ const extensionVolume = (data, material, textures) => {
10701068
material.attenuationDistance = data.attenuationDistance;
10711069
}
10721070
if (data.hasOwnProperty('attenuationColor')) {
1073-
const color = data.attenuationColor;
1074-
material.attenuation.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
1071+
const [r, g, b] = data.attenuationColor;
1072+
material.attenuation.set(r, g, b).gamma();
10751073
}
10761074
};
10771075

@@ -1150,15 +1148,14 @@ const createMaterial = (gltfMaterial, textures) => {
11501148
material.glossInvert = true;
11511149
material.useMetalness = true;
11521150

1153-
let color, texture;
1151+
let texture;
11541152
if (gltfMaterial.hasOwnProperty('pbrMetallicRoughness')) {
11551153
const pbrData = gltfMaterial.pbrMetallicRoughness;
11561154

11571155
if (pbrData.hasOwnProperty('baseColorFactor')) {
1158-
color = pbrData.baseColorFactor;
1159-
// Convert from linear space to sRGB space
1160-
material.diffuse.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
1161-
material.opacity = color[3];
1156+
const [r, g, b, a] = pbrData.baseColorFactor;
1157+
material.diffuse.set(r, g, b).gamma();
1158+
material.opacity = a;
11621159
}
11631160
if (pbrData.hasOwnProperty('baseColorTexture')) {
11641161
const baseColorTexture = pbrData.baseColorTexture;
@@ -1208,9 +1205,8 @@ const createMaterial = (gltfMaterial, textures) => {
12081205
}
12091206

12101207
if (gltfMaterial.hasOwnProperty('emissiveFactor')) {
1211-
color = gltfMaterial.emissiveFactor;
1212-
// Convert from linear space to sRGB space
1213-
material.emissive.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
1208+
const [r, g, b] = gltfMaterial.emissiveFactor;
1209+
material.emissive.set(r, g, b).gamma();
12141210
}
12151211

12161212
if (gltfMaterial.hasOwnProperty('emissiveTexture')) {

0 commit comments

Comments
 (0)