From cb34e9849d3ab7c088bcc9ee0e1b18bcd3e15747 Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Thu, 3 Aug 2023 22:05:39 -0400 Subject: [PATCH] GLTFLoader: Specify color space of 'srgb-linear' sources --- examples/jsm/loaders/GLTFLoader.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index 3c8a276fc3eb88..24eb7cc9c3d0e3 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -6,6 +6,7 @@ import { BufferGeometry, ClampToEdgeWrapping, Color, + ColorManagement, DirectionalLight, DoubleSide, FileLoader, @@ -25,6 +26,7 @@ import { LinearFilter, LinearMipmapLinearFilter, LinearMipmapNearestFilter, + LinearSRGBColorSpace, Loader, LoaderUtils, Material, @@ -545,7 +547,7 @@ class GLTFLightsExtension { const color = new Color( 0xffffff ); - if ( lightDef.color !== undefined ) color.fromArray( lightDef.color ); + if ( lightDef.color !== undefined ) color.setRGB( ...lightDef.color, LinearSRGBColorSpace ) const range = lightDef.range !== undefined ? lightDef.range : 0; @@ -663,7 +665,7 @@ class GLTFMaterialsUnlitExtension { const array = metallicRoughness.baseColorFactor; - materialParams.color.fromArray( array ); + materialParams.color.setRGB( ...array, LinearSRGBColorSpace ); materialParams.opacity = array[ 3 ]; } @@ -939,7 +941,7 @@ class GLTFMaterialsSheenExtension { if ( extension.sheenColorFactor !== undefined ) { - materialParams.sheenColor.fromArray( extension.sheenColorFactor ); + materialParams.sheenColor.setRGB( ...extension.sheenColorFactor, LinearSRGBColorSpace ); } @@ -1077,7 +1079,7 @@ class GLTFMaterialsVolumeExtension { materialParams.attenuationDistance = extension.attenuationDistance || Infinity; const colorArray = extension.attenuationColor || [ 1, 1, 1 ]; - materialParams.attenuationColor = new Color( colorArray[ 0 ], colorArray[ 1 ], colorArray[ 2 ] ); + materialParams.attenuationColor = new Color().setRGB( ...colorArray, LinearSRGBColorSpace ); return Promise.all( pending ); @@ -1180,7 +1182,7 @@ class GLTFMaterialsSpecularExtension { } const colorArray = extension.specularColorFactor || [ 1, 1, 1 ]; - materialParams.specularColor = new Color( colorArray[ 0 ], colorArray[ 1 ], colorArray[ 2 ] ); + materialParams.specularColor = new Color().setRGB( ...colorArray, LinearSRGBColorSpace ); if ( extension.specularColorTexture !== undefined ) { @@ -3386,7 +3388,7 @@ class GLTFParser { const array = metallicRoughness.baseColorFactor; - materialParams.color.fromArray( array ); + materialParams.color.setRGB( array[ 0 ], array[ 1 ], array[ 2 ], LinearSRGBColorSpace ); materialParams.opacity = array[ 3 ]; } @@ -3478,7 +3480,7 @@ class GLTFParser { if ( materialDef.emissiveFactor !== undefined && materialType !== MeshBasicMaterial ) { - materialParams.emissive = new Color().fromArray( materialDef.emissiveFactor ); + materialParams.emissive = new Color().setRGB( ...materialDef.emissiveFactor, LinearSRGBColorSpace ); } @@ -4559,6 +4561,12 @@ function addPrimitiveAttributes( geometry, primitiveDef, parser ) { } + if ( ColorManagement.workingColorSpace !== LinearSRGBColorSpace && 'COLOR_0' in attributes ) { + + console.warn( `THREE.GLTFLoader: Converting vertex colors from "srgb-linear" to "${ColorManagement.workingColorSpace}" not supported.` ); + + } + assignExtrasToUserData( geometry, primitiveDef ); computeBounds( geometry, primitiveDef, parser );