[SOLVED] Possible Issue with updating the material diffuse color


Any idea why the following code makes the material color change? On my mindset it shouldn’t update the color. Am I doing something wrong or is it a bug?

I am trying to reset the material value to the initial set in the editor.

var MaterialColor = pc.createScript('materialColor');

// initialize code called once per entity
MaterialColor.prototype.initialize = function() {

var color = this.entity.model.meshInstances[0].material.diffuse.clone();
console.log('initial color: ', color.r, color.g, color.b);

window.setTimeout( function(){

var meshInstance = this.entity.model.meshInstances[0];
meshInstance.setParameter('material_diffuse', [color.r, color.g, color.b]); 

console.log('color update: ', color.r, color.g, color.b);
}.bind(this), 1500 );

Sample Project: https://playcanvas.com/editor/scene/766763




It’s because pc.StandardMaterial#diffuse is not gamma corrected. But the shader uniform material_diffuse is gamma corrected.

To gamma correct a color, you’d have to do:

meshInstance.setParameter('material_diffuse', [
    Math.pow(color.r, 2.2),
    Math.pow(color.g, 2.2),
    Math.pow(color.b, 2.2)

In case you’re interested to see where the engine converted non-gamma corrected color properties to gamma corrected shader uniforms, check out this source file:


Oh, great! Thank you very much. It works fine!