[SOLVED] Manipulating the Material of a Single Entity Instantiated from a Template

This small sample project demonstrates the topic.

  • I made a template
  • I instantiate the template 3 times at runtime
  • I try to modify the material on just one entity

These instantiated entities have an undefined model property. The only other place I could find a reference to material was in the entity.render.meshInstances[0].material component. Modifying this material changes all three instantiated entities.

Could anyone help me understand best way to manipulate the appearance of individual objects within sets of instantiated templates? Things I can think of are modifying the material, swapping the material, or swapping the entire entity. Is the use of templates here the wrong approach?

Hi @bdb! You need to clone the material first. Then you can modify this cloned material and apply it to a specific entity. The topic below is related.

1 Like

Thank you for the response Albertos, I updated the sample project with a solution, the following pseudo-code summarizes the implementation:

  • let clonedMaterial = entity.render.meshInstances[0].material.clone();
  • clonedMaterial.diffuse.set(0, 1, 0, 1);
  • entity.render.meshInstances[0].material = clonedMaterial;
1 Like

Thanks for sharing! Does that solve the problem for you?

Yes it does, thank you. I wanted to explore the topic because there were a few things I did not understand.

For my current use case it was better to swap between two pre-defined materials instead of cloning each instance’s material.

1 Like