[SOLVED] How to create a deep copy of an asset?

Is this possible to create deep copy of an asset that is already loaded like a material in play mode? (Not a shallow copy that hold references).

Actually, if I applied the same material on 2 objects by code and I change the parameters of the material on the first object, the changes are reflected on the second object.

model_1.meshInstances[0].material = materialAsset.resource;
model_2.meshInstances[0].material = materialAsset.resource;

model_1.meshInstances[0].material.diffuse = new pc.Color(1, 0, 0, 1);

//Now model_1 and model_2 are red.

This is a very simple example but I want to get an asset on the registry, deep copy them on multiple objects and be able to control them individually.

You could do:

var material;
material = materialAsset.resource.clone();
model_1.meshInstances[0].material = material;
material = materialAsset.resource.clone();
model_2.meshInstances[0].material = material;

model_1.meshInstances[0].material.diffuse = new pc.Color(1, 0, 0, 1)


1 Like

Oh wow! It’s working as expected.
The solution was so simple!

Thank you for your help! You saved me a lot of time.

1 Like