Skip to content

Commit

Permalink
Set THREE.ColorManagement.legacyMode to false when using color manage…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
mrxz committed May 7, 2023
1 parent 68156a3 commit 682ea9d
Show file tree
Hide file tree
Showing 18 changed files with 19 additions and 61 deletions.
1 change: 0 additions & 1 deletion src/components/generic-tracked-controller-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ module.exports.Component = registerComponent('generic-tracked-controller-control
this.controllerPresent = false;
this.wasControllerConnected = false;
this.lastControllerCheck = 0;
this.rendererSystem = this.el.sceneEl.systems.renderer;
this.bindMethods();

// generic-tracked-controller-controls has the lowest precedence.
Expand Down
1 change: 0 additions & 1 deletion src/components/hp-mixed-reality-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ module.exports.Component = registerComponent('hp-mixed-reality-controls', {
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self, self.data.hand); };
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self, self.data.hand); };
this.previousButtonValues = {};
this.rendererSystem = this.el.sceneEl.systems.renderer;

this.bindMethods();
},
Expand Down
6 changes: 0 additions & 6 deletions src/components/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ module.exports.Component = registerComponent('light', {
var el = this.el;
this.light = null;
this.defaultTarget = null;
this.rendererSystem = this.el.sceneEl.systems.renderer;
this.system.registerLight(el);
},

Expand All @@ -67,7 +66,6 @@ module.exports.Component = registerComponent('light', {
var data = this.data;
var diffData = diff(data, oldData);
var light = this.light;
var rendererSystem = this.rendererSystem;
var self = this;

// Existing light.
Expand All @@ -80,13 +78,11 @@ module.exports.Component = registerComponent('light', {
switch (key) {
case 'color': {
light.color.set(value);
rendererSystem.applyColorCorrection(light.color);
break;
}

case 'groundColor': {
light.groundColor.set(value);
rendererSystem.applyColorCorrection(light.groundColor);
break;
}

Expand Down Expand Up @@ -281,12 +277,10 @@ module.exports.Component = registerComponent('light', {
getLight: function (data) {
var angle = data.angle;
var color = new THREE.Color(data.color);
this.rendererSystem.applyColorCorrection(color);
color = color.getHex();
var decay = data.decay;
var distance = data.distance;
var groundColor = new THREE.Color(data.groundColor);
this.rendererSystem.applyColorCorrection(groundColor);
groundColor = groundColor.getHex();
var intensity = data.intensity;
var type = data.type;
Expand Down
3 changes: 0 additions & 3 deletions src/components/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports.Component = registerComponent('line', {
var data = this.data;
var geometry;
var material;
this.rendererSystem = this.el.sceneEl.systems.renderer;
material = this.material = new THREE.LineBasicMaterial({
color: data.color,
opacity: data.opacity,
Expand All @@ -26,7 +25,6 @@ module.exports.Component = registerComponent('line', {
geometry = this.geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(2 * 3), 3));

this.rendererSystem.applyColorCorrection(material.color);
this.line = new THREE.Line(geometry, material);
this.el.setObject3D(this.attrName, this.line);
},
Expand Down Expand Up @@ -59,7 +57,6 @@ module.exports.Component = registerComponent('line', {
}

material.color.setStyle(data.color);
this.rendererSystem.applyColorCorrection(material.color);
material.opacity = data.opacity;
material.transparent = data.opacity < 1;
material.visible = data.visible;
Expand Down
1 change: 0 additions & 1 deletion src/components/magicleap-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ module.exports.Component = registerComponent('magicleap-controls', {
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self); };
this.previousButtonValues = {};
this.rendererSystem = this.el.sceneEl.systems.renderer;

this.bindMethods();
},
Expand Down
2 changes: 0 additions & 2 deletions src/components/obj-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ module.exports.Component = registerComponent('obj-model', {
self.model.traverse(function (object) {
if (object.isMesh) {
var material = object.material;
if (material.color) rendererSystem.applyColorCorrection(material.color);
if (material.map) rendererSystem.applyColorCorrection(material.map);
if (material.emissive) rendererSystem.applyColorCorrection(material.emissive);
if (material.emissiveMap) rendererSystem.applyColorCorrection(material.emissiveMap);
}
});
Expand Down
2 changes: 0 additions & 2 deletions src/components/oculus-go-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ module.exports.Component = registerComponent('oculus-go-controls', {
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
this.controllerPresent = false;
this.lastControllerCheck = 0;
this.rendererSystem = this.el.sceneEl.systems.renderer;
this.bindMethods();
},

Expand Down Expand Up @@ -197,6 +196,5 @@ module.exports.Component = registerComponent('oculus-go-controls', {
}
button = buttonMeshes[buttonName];
button.material.color.set(color);
this.rendererSystem.applyColorCorrection(button.material.color);
}
});
2 changes: 0 additions & 2 deletions src/components/oculus-touch-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
this.controllerPresent = false;
this.lastControllerCheck = 0;
this.previousButtonValues = {};
this.rendererSystem = this.el.sceneEl.systems.renderer;
this.bindMethods();
this.triggerEuler = new THREE.Euler();
},
Expand Down Expand Up @@ -501,7 +500,6 @@ module.exports.Component = registerComponent('oculus-touch-controls', {
color = (state === 'up' || state === 'touchend') ? buttonMeshes[buttonName].originalColor || this.data.buttonColor : state === 'touchstart' ? this.data.buttonTouchColor : this.data.buttonHighlightColor;
button = buttonMeshes[buttonName];
button.material.color.set(color);
this.rendererSystem.applyColorCorrection(button.material.color);
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/scene/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module.exports.Component = registerComponent('screenshot', {

getRenderTarget: function (width, height) {
return new THREE.WebGLRenderTarget(width, height, {
encoding: this.el.sceneEl.renderer.outputEncoding,
colorSpace: this.el.sceneEl.renderer.outputColorSpace,
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
wrapS: THREE.ClampToEdgeWrapping,
Expand Down Expand Up @@ -153,7 +153,7 @@ module.exports.Component = registerComponent('screenshot', {
format: THREE.RGBFormat,
generateMipmaps: true,
minFilter: THREE.LinearMipmapLinearFilter,
encoding: THREE.sRGBEncoding
colorSpace: THREE.SRGBColorSpace
});
// Create cube camera and copy position from scene camera.
cubeCamera = new THREE.CubeCamera(el.camera.near, el.camera.far, cubeRenderTarget);
Expand Down
1 change: 0 additions & 1 deletion src/components/valve-index-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ module.exports.Component = registerComponent('valve-index-controls', {
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self); };
this.previousButtonValues = {};
this.rendererSystem = this.el.sceneEl.systems.renderer;

this.bindMethods();
},
Expand Down
5 changes: 0 additions & 5 deletions src/components/vive-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ module.exports.Component = registerComponent('vive-controls', {
this.onButtonTouchEnd = function (evt) { onButtonEvent(evt.detail.id, 'touchend', self); };
this.onButtonTouchStart = function (evt) { onButtonEvent(evt.detail.id, 'touchstart', self); };
this.previousButtonValues = {};
this.rendererSystem = this.el.sceneEl.systems.renderer;

this.bindMethods();
},
Expand Down Expand Up @@ -241,19 +240,15 @@ module.exports.Component = registerComponent('vive-controls', {

setButtonColor: function (buttonName, color) {
var buttonMeshes = this.buttonMeshes;
var rendererSystem = this.rendererSystem;

if (!buttonMeshes) { return; }

// Need to do both left and right sides for grip.
if (buttonName === 'grip') {
buttonMeshes.grip.left.material.color.set(color);
buttonMeshes.grip.right.material.color.set(color);
rendererSystem.applyColorCorrection(buttonMeshes.grip.left.material.color);
rendererSystem.applyColorCorrection(buttonMeshes.grip.right.material.color);
return;
}
buttonMeshes[buttonName].material.color.set(color);
rendererSystem.applyColorCorrection(buttonMeshes[buttonName].material.color);
}
});
3 changes: 0 additions & 3 deletions src/shaders/flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ module.exports.Shader = registerShader('flat', {
* Adds a reference from the scene to this entity as the camera.
*/
init: function (data) {
this.rendererSystem = this.el.sceneEl.systems.renderer;
this.materialData = {color: new THREE.Color()};
this.textureSrc = null;
getMaterialData(data, this.materialData);
this.rendererSystem.applyColorCorrection(this.materialData.color);
this.material = new THREE.MeshBasicMaterial(this.materialData);
},

Expand All @@ -45,7 +43,6 @@ module.exports.Shader = registerShader('flat', {
updateMaterial: function (data) {
var key;
getMaterialData(data, this.materialData);
this.rendererSystem.applyColorCorrection(this.materialData.color);
for (key in this.materialData) {
this.material[key] = this.materialData[key];
}
Expand Down
3 changes: 0 additions & 3 deletions src/shaders/phong.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ module.exports.Shader = registerShader('phong', {
* Adds a reference from the scene to this entity as the camera.
*/
init: function (data) {
this.rendererSystem = this.el.sceneEl.systems.renderer;
this.materialData = { color: new THREE.Color(), specular: new THREE.Color(), emissive: new THREE.Color() };
this.textureSrc = null;
getMaterialData(data, this.materialData);
this.rendererSystem.applyColorCorrection(this.materialData.color);
this.material = new THREE.MeshPhongMaterial(this.materialData);
utils.material.updateMap(this, data);
},
Expand All @@ -79,7 +77,6 @@ module.exports.Shader = registerShader('phong', {
updateMaterial: function (data) {
var key;
getMaterialData(data, this.materialData);
this.rendererSystem.applyColorCorrection(this.materialData.color);
for (key in this.materialData) {
this.material[key] = this.materialData[key];
}
Expand Down
1 change: 0 additions & 1 deletion src/shaders/shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports.Shader = registerShader('shadow', {
* Adds a reference from the scene to this entity as the camera.
*/
init: function (data) {
this.rendererSystem = this.el.sceneEl.systems.renderer;
this.material = new THREE.ShadowMaterial();
},

Expand Down
5 changes: 0 additions & 5 deletions src/shaders/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ module.exports.Shader = registerShader('standard', {
* Adds a reference from the scene to this entity as the camera.
*/
init: function (data) {
this.rendererSystem = this.el.sceneEl.systems.renderer;
this.materialData = {color: new THREE.Color(), emissive: new THREE.Color()};
getMaterialData(data, this.materialData);
this.rendererSystem.applyColorCorrection(this.materialData.color);
this.rendererSystem.applyColorCorrection(this.materialData.emissive);
this.material = new THREE.MeshStandardMaterial(this.materialData);
},

Expand All @@ -90,8 +87,6 @@ module.exports.Shader = registerShader('standard', {
var key;
var material = this.material;
getMaterialData(data, this.materialData);
this.rendererSystem.applyColorCorrection(this.materialData.color);
this.rendererSystem.applyColorCorrection(this.materialData.emissive);
for (key in this.materialData) {
material[key] = this.materialData[key];
}
Expand Down
21 changes: 7 additions & 14 deletions src/systems/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ module.exports.System = registerSystem('renderer', {
toneMapping: {default: 'no', oneOf: ['no', 'ACESFilmic', 'linear', 'reinhard', 'cineon']},
precision: {default: 'high', oneOf: ['high', 'medium', 'low']},
sortObjects: {default: false},
colorManagement: {default: false},
gammaOutput: {default: false},
colorManagement: {default: true},
alpha: {default: true},
foveationLevel: {default: 1}
},
Expand All @@ -36,12 +35,8 @@ module.exports.System = registerSystem('renderer', {
renderer.physicallyCorrectLights = data.physicallyCorrectLights;
renderer.toneMapping = THREE[toneMappingName + 'ToneMapping'];

if (data.colorManagement || data.gammaOutput) {
renderer.outputEncoding = THREE.sRGBEncoding;
if (data.gammaOutput) {
warn('Property `gammaOutput` is deprecated. Use `renderer="colorManagement: true;"` instead.');
}
}
THREE.ColorManagement.enabled = data.colorManagement;
renderer.outputColorSpace = data.colorManagement ? THREE.SRGBColorSpace : THREE.LinearSRGBColorSpace;

if (sceneEl.hasAttribute('antialias')) {
warn('Component `antialias` is deprecated. Use `renderer="antialias: true"` instead.');
Expand All @@ -62,13 +57,11 @@ module.exports.System = registerSystem('renderer', {
renderer.xr.setFoveation(data.foveationLevel);
},

applyColorCorrection: function (colorOrTexture) {
if (!this.data.colorManagement || !colorOrTexture) {
applyColorCorrection: function (texture) {
if (!this.data.colorManagement || !texture) {
return;
} else if (colorOrTexture.isColor) {
colorOrTexture.convertSRGBToLinear();
} else if (colorOrTexture.isTexture) {
colorOrTexture.encoding = THREE.sRGBEncoding;
} else if (texture.isTexture) {
texture.colorSpace = THREE.SRGBColorSpace;
}
},

Expand Down
10 changes: 5 additions & 5 deletions tests/components/scene/screenshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ suite('screenshot', function () {
var component;
var sceneEl;

function checkRenderTarget (renderTarget, encoding) {
function checkRenderTarget (renderTarget, colorSpace) {
const texture = renderTarget.texture;
assert.equal(texture.encoding, encoding);
assert.equal(texture.colorSpace, colorSpace);
assert.equal(texture.minFilter, THREE.LinearFilter);
assert.equal(texture.magFilter, THREE.LinearFilter);
assert.equal(texture.wrapS, THREE.ClampToEdgeWrapping);
Expand Down Expand Up @@ -38,20 +38,20 @@ suite('screenshot', function () {
});

test('capture renders screenshot correctly (w/o Color Management)', function () {
sceneEl.setAttribute('renderer', 'colorManagement: false');
sceneEl.addEventListener('loaded', () => {
component = sceneEl.components.screenshot;
const renderTarget = component.getRenderTarget();
checkRenderTarget(renderTarget, THREE.LinearEncoding);
checkRenderTarget(renderTarget, THREE.LinearSRGBColorSpace);
});
document.body.appendChild(sceneEl);
});

test('capture renders screenshot correctly (w/ Color Management)', function () {
sceneEl.setAttribute('renderer', 'colorManagement: true');
sceneEl.addEventListener('loaded', () => {
component = sceneEl.components.screenshot;
const renderTarget = component.getRenderTarget();
checkRenderTarget(renderTarget, THREE.sRGBEncoding);
checkRenderTarget(renderTarget, THREE.SRGBColorSpace);
});
document.body.appendChild(sceneEl);
});
Expand Down
9 changes: 5 additions & 4 deletions tests/systems/renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ suite('renderer', function () {
assert.strictEqual(rendererSystem.highRefreshRate, false);
assert.strictEqual(rendererSystem.physicallyCorrectLights, false);
assert.strictEqual(rendererSystem.sortObjects, false);
assert.strictEqual(rendererSystem.colorManagement, true);

// Verify properties that are transferred from the renderer system to the rendering engine.
var renderingEngine = sceneEl.renderer;
assert.notOk(renderingEngine.outputEncoding);
assert.strictEqual(renderingEngine.outputColorSpace, THREE.SRGBColorSpace);
assert.notOk(renderingEngine.sortObjects);
assert.strictEqual(renderingEngine.physicallyCorrectLights, false);
done();
Expand All @@ -29,10 +30,10 @@ suite('renderer', function () {

test('change renderer colorManagement', function (done) {
var sceneEl = createScene();
sceneEl.setAttribute('renderer', 'colorManagement: true;');
sceneEl.setAttribute('renderer', 'colorManagement: false;');
sceneEl.addEventListener('loaded', function () {
assert.ok(sceneEl.renderer.outputEncoding);
assert.equal(sceneEl.renderer.outputEncoding, THREE.sRGBEncoding);
assert.ok(sceneEl.renderer.outputColorSpace);
assert.equal(sceneEl.renderer.outputColorSpace, THREE.LinearSRGBColorSpace);
done();
});
document.body.appendChild(sceneEl);
Expand Down

0 comments on commit 682ea9d

Please sign in to comment.