[SOLVED] Multiple materials on same meshinstance - Looking to change an assetreg material

As far as I can tell from the screenshots, is that you are trying to change one material of a single mesh instance. In this case, [13]. Is that correct?

The model instance in Blender;

Both part of the same instance. I want to access and alter these two materials;
image

  • of which (after export and import) lays in the asset registry.
    Yes, I understand you concern about that kind of change, but the surface rendering should only get pink/NaN temporarely, in case I get to a workaround place >> where a new material can be set on the slot … I would hope.

Are those two materials listed in this screenshot?

Yes, 4th from the bottom and 8th from the bottom :slight_smile:

MBLab_skin2_Halsen
MBLab_skin2_V_SkulderOverarm

  • in asset view to the left

I rephrase, are those materials listed in the meshinstance list on the right?

  • the scroll was out of space vertically

Cool, as they are in that list, that means you can access the meshinstance and change that material via the model component.

There’s one material to one meshinstance. AFAIK, you can’t have multiple materials on a single mesh instance.

Basically, we are back to this solution.

->> hmm … how do you explain ‘the editor screendump’ where I made the front; dark grey then? (there are only 10 meshes, but slots from 11 to 19 as well)

Will try a little further myself, but thanks anyway :slight_smile:

In PlayCanvas, there is a mesh instance per material. As you have 20 materials, you have 20 meshinstances on this mesh.

 var entityK = this.app.root.findByName("LightCookie_VersAfKvinde__FullBody");
 var meshInstances = entityK.model.meshInstances; console.log('length:'+meshInstances.length);

equals this in console

length:10

How about that. Not sure what to tell you here as the code in the engine shows that a mesh instance only has one material: https://github.com/playcanvas/engine/blob/master/src/scene/mesh-instance.js#L80

Is it possible to share that model? I would like take a peek at it?

I can do that ( - made it now, but will send it later)

Otherwise I am just trying to access very simply, like accessing the arm or stomach material like this:
pc.app.assets.find(38251248).diffuse = new pc.Color(1,0,1);
or …
this.app.assets.find(38251248).diffuse = new pc.Color(1,0,1);

As long as you know what materials they are, you can do that. The example that was posted above seemed to be about swapping materials rather than modifying an existing one which is harder as you need to assign the material back to the meshinstance.

Lays beneath ‘Models’:
https://playcanvas.com/editor/scene/1028837

pc.app.assets.find(‘38251248’).diffuse = new pc.Color(1,0,1);

ends out with:

[raycast.js?id=38253976&branchId=062396da-e562-458c-8459-ba900f1730e8:83]: Uncaught TypeError: Cannot set property ‘diffuse’ of undefined

Check the API.
https://developer.playcanvas.com/en/api/pc.AssetRegistry.html#find

find expects the name of the asset, not the id.

You want https://developer.playcanvas.com/en/api/pc.AssetRegistry.html#get
Which takes a number, not a string.

The example scene you sent me has 4 meshinstances which is inline with one meshinstance for one material

ok - almost

var matGet = app.assets.get(38251248);
console.log(‘matGet :’+matGet );

matGet.diffuse = new pc.Color(1,0,1);

  • outputs me (yet again):

raycast.js?id=38253976&branchId=062396da-e562-458c-8459-ba900f1730e8:82
matGet:[object Object]

  • while the actual diffuse-command runs through without an error.
    All I need is the ressouce-output - the listing you showed me in the screen dump … I can never remember how to access that list (think I know how if it is nodes, but I do not remember; the out of all the " … materialAsset [ …], meshInstances: Array[4] … etc")

is it done with a [0] to access the object? , but then what ??? :-s

ps: don’t bother the ‘???’ (meant for my own approach/energy) :slight_smile:

So I finally found the problem, and it lays within the structure and export of the model.
As I am using MB-lab (which is insanely awesome for graphic artists that use Blender), the finalizing of a character casts a certain structure for all its model objects and materials. This structure has an individual material for each of the appendix oriented objects (eyes, eyelashes, nails, tongue etc.). Upon the body it makes room for (kind of an) anti-erotic approach, where the ‘chest’ and ‘shorts’ are sought to be rendered with a black material. Never the less, this counts up for two different materials upon the same object - the body (skin-material and the anti-erotic black material).
Thus my confusion rose, when I just could not expand the material slots within Blender, for then to control them in PlayCanvas afterwards (?).

Solution: While putting the correct UV-mapping (that MB-lab otherwise provides) a side, I will still be able to achieve my goal within PlayCanvas >>

Here is how to bypass the MB-lab material node-structure, in order for PlayCanvas to read the dedicated material slots as meshinstances:
Open the tab ‘Shading’, and within the node interface, observe the initially cast structure from the finalized MB-lab character:


From here click ‘Surface’ to select ‘Diffuse BSDF’:

Do this to all the materials one wants ‘carried at export’ to Playcanvas for meshinstance.

[Note: At least this what I consider to be ‘most’ true, as I just found out that a refresh of my script-window alongside parsing of the script within editor, made some ‘break throughs’ for this code part:

var entityK = this.app.root.findByName("XYZ");
   // var meshInstances = this.entity.model.meshInstances;
    var meshInstances = entityK.model.meshInstances; console.log('length:'+meshInstances.length);
     for (var i = 0; i < meshInstances.length; ++i) { 
        var mesh = meshInstances[i];
         console.log('m_name:'+meshInstances[i].material.name);
         if(i===2) mesh.material = materialList.resource;
    }

And I do sit on a cabled internet-connection and a new Lenovo P330/i9-9900 machine => thus; to whome it might concern in regards to local user connections, CPU-to-polygon-editorhandling and remote engine-servers etc.
]