Change Color on clicking model & retrieve the old color on clicking outside

I created one 3d model which contains 10 models inside it. I am able to change the color on clicking it using diffuse color. How to retrieve the old color after clicking outside as i don’t have the old color reference anywhere?

well create a reference then, you know, variable = value :wink: how else do you expect to know the old color? Babylon materials dont have a history state of the changes made to them :wink:

// get the model by its name
var model = scene.getMeshByName(“myModel”);

// get the material of the model
var material = model.material;

// store the original color
var originalColor = material.diffuseColor.clone();

// set the diffuse color of the material to red
material.diffuseColor = new BABYLON.Color3(1, 0, 0);

// later, retrieve the original color
material.diffuseColor = originalColor;

I am using this code but for some meshes there is no material inside it and for some others, diffusion color is null. Can u please help me to get the color of existing model with the code?

If you create a mesh using the CreateXXX functions it won’t have a material and it will use the default material. Diffusion color is a property only of Standard Materials, PBR have albedo color. Check the Material documentation: Materials | Babylon.js Documentation (babylonjs.com)

this makes it a little more complex now , honestly , i believe the default material should just be PBR. Its 2023 already … everything is PBR and any device can handle PBR. I cant think why the standard material should still hang around.

Because now the OP has to check the instanceof for the material :wink:

var material = model.material;
var originalColor = null;

if(material   instanceof PBRMaterial ){
    originalColor = material.albedoColor.clone();   
}

if(material   instanceof StandardMaterial ){
    originalColor = material.diffuseColor.clone();   
}

you could skip using instanceof and do some null coalescing on retrieval to make it shorter , but setting the color will require conditionals or redundant code etc…

@Bhanu_Prakash_Reddy , I would personally just make everything PBR and then none of this is required , then you can just retrieve and set the “albedoColor”