Skip to content

Commit

Permalink
ColladaLoader: Convert color from srgb to linear on load (#23401)
Browse files Browse the repository at this point in the history
* ColladaLoader: label color textures, material colors as sRGB

* ColladaLoader: convert vertex colors

* use outputEncoding = sRGBEncoding in collada examples

* update failing screenshots

* fix deepscan issues

* Update ColladaLoader.js

* update KMZ example
  • Loading branch information
gkjohnson authored Feb 1, 2022
1 parent 108deac commit 2bbe2a6
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 9 deletions.
44 changes: 36 additions & 8 deletions examples/jsm/loaders/ColladaLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
TextureLoader,
Vector2,
Vector3,
VectorKeyframeTrack
VectorKeyframeTrack,
sRGBEncoding
} from 'three';
import { TGALoader } from '../loaders/TGALoader.js';

Expand Down Expand Up @@ -1579,7 +1580,7 @@ class ColladaLoader extends Loader {

material.name = data.name || '';

function getTexture( textureObject ) {
function getTexture( textureObject, encoding = null ) {

const sampler = effect.profile.samplers[ textureObject.id ];
let image = null;
Expand Down Expand Up @@ -1627,6 +1628,12 @@ class ColladaLoader extends Loader {

}

if ( encoding !== null ) {

texture.encoding = encoding;

}

return texture;

} else {
Expand Down Expand Up @@ -1657,7 +1664,7 @@ class ColladaLoader extends Loader {

case 'diffuse':
if ( parameter.color ) material.color.fromArray( parameter.color );
if ( parameter.texture ) material.map = getTexture( parameter.texture );
if ( parameter.texture ) material.map = getTexture( parameter.texture, sRGBEncoding );
break;
case 'specular':
if ( parameter.color && material.specular ) material.specular.fromArray( parameter.color );
Expand All @@ -1667,20 +1674,24 @@ class ColladaLoader extends Loader {
if ( parameter.texture ) material.normalMap = getTexture( parameter.texture );
break;
case 'ambient':
if ( parameter.texture ) material.lightMap = getTexture( parameter.texture );
if ( parameter.texture ) material.lightMap = getTexture( parameter.texture, sRGBEncoding );
break;
case 'shininess':
if ( parameter.float && material.shininess ) material.shininess = parameter.float;
break;
case 'emission':
if ( parameter.color && material.emissive ) material.emissive.fromArray( parameter.color );
if ( parameter.texture ) material.emissiveMap = getTexture( parameter.texture );
if ( parameter.texture ) material.emissiveMap = getTexture( parameter.texture, sRGBEncoding );
break;

}

}

material.color.convertSRGBToLinear();
if ( material.specular ) material.specular.convertSRGBToLinear();
if ( material.emissive ) material.emissive.convertSRGBToLinear();

//

let transparent = parameters[ 'transparent' ];
Expand Down Expand Up @@ -2015,7 +2026,7 @@ class ColladaLoader extends Loader {

case 'color':
const array = parseFloats( child.textContent );
data.color = new Color().fromArray( array );
data.color = new Color().fromArray( array ).convertSRGBToLinear();
break;

case 'falloff_angle':
Expand Down Expand Up @@ -2481,7 +2492,7 @@ class ColladaLoader extends Loader {
break;

case 'COLOR':
buildGeometryData( primitive, sources[ input.id ], input.offset, color.array );
buildGeometryData( primitive, sources[ input.id ], input.offset, color.array, true );
color.stride = sources[ input.id ].stride;
break;

Expand Down Expand Up @@ -2520,7 +2531,7 @@ class ColladaLoader extends Loader {

}

function buildGeometryData( primitive, source, offset, array ) {
function buildGeometryData( primitive, source, offset, array, isColor = false ) {

const indices = primitive.p;
const stride = primitive.stride;
Expand All @@ -2537,6 +2548,22 @@ class ColladaLoader extends Loader {

}

if ( isColor ) {

// convert the vertex colors from srgb to linear if present
const startIndex = array.length - sourceStride - 1;
tempColor.setRGB(
array[ startIndex + 0 ],
array[ startIndex + 1 ],
array[ startIndex + 2 ]
).convertSRGBToLinear();

array[ startIndex + 0 ] = tempColor.r;
array[ startIndex + 1 ] = tempColor.g;
array[ startIndex + 2 ] = tempColor.b;

}

}

const sourceArray = source.array;
Expand Down Expand Up @@ -3981,6 +4008,7 @@ class ColladaLoader extends Loader {

//

const tempColor = new Color();
const animations = [];
let kinematics = {};
let count = 0;
Expand Down
Binary file modified examples/screenshots/webgl_loader_collada_kinematics.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgl_loader_collada_skinning.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgl_loader_kmz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/webgl_loader_collada.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
//

renderer = new THREE.WebGLRenderer();
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
Expand Down
1 change: 1 addition & 0 deletions examples/webgl_loader_collada_kinematics.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
particleLight.add( pointLight );

renderer = new THREE.WebGLRenderer();
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
Expand Down
1 change: 1 addition & 0 deletions examples/webgl_loader_collada_skinning.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
//

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
Expand Down
3 changes: 2 additions & 1 deletion examples/webgl_loader_kmz.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@

scene.add( camera );

const grid = new THREE.GridHelper( 50, 50, 0xffffff, 0x555555 );
const grid = new THREE.GridHelper( 50, 50, 0xffffff, 0x333333 );
scene.add( grid );

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
Expand Down

0 comments on commit 2bbe2a6

Please sign in to comment.