Setting Material on meshInsance problem

Hi guys,
why does this work?

var lamp = this.app.root.findByName('lamp');
var newMat = this.app.assets.find('PurplePlastic');
lamp.model.meshInstances[4].material = newMat.resource;

and why does this don’t work:

var lamp = this.app.root.findByName('lamp');
var newMat = this.app.assets.find('PurplePlastic');
lamp.findByName('Bulp').material = newMat.resource;

Bildschirmfoto 2020-09-04 um 16.33.29

Hi @Karl47,

So in your 2nd code excerpt you are accessing a property named material on a pc.Entity instance, which doesn’t exist.

The findByName method will return a pc.Entity or pc.GraphNode instance, not a pc.MeshInstance.

As you corrected coded it in the first try.

But @yaustar told me in previous post:

You can also this.entity.findByName with the name of the node incase the order of the meshinstances change or the model is updated.

Yes, to find and target a node, that is a pc.GraphNode, which every mesh instance has one.

But to change the material of a mesh instance, you still have to target the mesh instance of that node. Which I am not sure you can find this way, you will have to search by name in the initial mesh instances array to target a certain node:

var meshInstFound;
lamp.model.meshInstances.forEach( function(meshInstance){
   if( meshInstance.node.name === 'PurplePlastic'){
      meshInstFound = meshInstance;
   } 
});

if( meshInstFound){
   meshInstFound.material = newMat.resource;
} 
1 Like

Understand! I wasn’t aware that this finds a GraphNode. I thought it gives back a meshInstance. Thanks, as always, for your quick response :slightly_smiling_face:

Ok, and one final question: Why dies this do’nt work?

var lamp = this.app.root.findByName('lamp');
var newMat = this.app.assets.find('PurplePlastic');
// This following line does not work
lamp.model.meshInstances[4].material.setParameter('emissive', [1, 0, 1]);

So, two things:

  • the parameter name is material_emissive, those are undocumented right now but you can check the engine repo for the right names.
  • drop the .material property and target directly the mesh instance:
lamp.model.meshInstances[4].setParameter('material_emissive', [1, 0, 1]);
1 Like