I want to control the different materials of a meshinstance. At top and initialize I declare:
Raycast.attributes.add("materials", {type: "asset", assetType: "material", array: true, title: "Materials"});
// initialize code called once per entity
Raycast.prototype.initialize = function() {
this.materialIndex = 0;
};
and at doRaycast I seek to change the material on my model’s shoulder, by altering the asset material:
Raycast.prototype.doRaycast = function (screenX, screenY) {
this.materialIndex = (this.materialIndex + 1) % this.materials.length;
var materialAtList = this.materials[this.materialIndex];
// The pc.Vec3 to raycast from (the position of the camera)
var from = this.entity.getPosition();
// The pc.Vec3 to raycast to (the click position projected onto the camera's far clip plane)
var to = this.entity.camera.screenToWorld(screenX, screenY, this.entity.camera.farClip);
// Raycast between the two points and return the closest hit result
var result = this.app.systems.rigidbody.raycastFirst(from, to); try{ console.log('RC DRC:'+result.name);}catch(err){}
// If there was a hit, store the entity
if (result) {
var hitEntity = result.entity;
if(hitEntity.name === "Capsule_UpperRightShoulder") {
var materialSta = new pc.StandardMaterial(); materialSta = pc.app.assets.get(38251253);
this.entityKvindeOAMAT =materialSta; console.log("this.entityKvinde: "+ this.entityKvindeOAMAT);
this.entityKvindeOAMAT.material = materialAtList.resource; this.entityKvindeOAMAT.material.update();
}
}
};
The console.log at ‘this.entityKvindeOAMAT’, does provide me with an [Object object], but the material does not ‘update’?
Yes, I know I got colorblind after a lot of forum researching already.
But it is also important to stress that, I am not trying to run through the meshinstances of the model.
I want to run through the ‘Asset materials’ of one of the model’s meshinstances only:
Title: “Multiple materials on same meshinstance …”
Above are the imported materials that came along with the FBX model. Many of them are put on the bodyskin-meshinstance only (where as the rest are put on eyes, eyelashes etc … but those meshinstances are irrelevant - I am only interested in changing bodyskin materials … if I had to cut up the bodyskin’s vertices the model will crack open at slices while animating :-/ )
At this illustration, my approach should be more accessible - cf blue markers:
Top left: by collision/rigibody raycast I want to click a green capsule (later with invisible material), in order to change various body parts on the skin (the body meshinstance). In this case the stomach/breast is to be changed to a grey (as shown).
Bottom left: The two top used registry assets. But actually one/you should notice all the bottom assets ‘MBLab_skin2_ …’, being the materials at stake. All of those are from the skin (the body meshinstance).
Top right: Here it says ‘Meshinstances[19]’, as if they are to be accessed ‘over there’ (by script).
Bottom left: The index of [13], is where I have manually changed the stomach/breast - I need to do this by script.
I actually came upon an old post, where I used nodes (as shown by you) in relation to an gtlf-file that had multiple meshes.
Here I am wondering if nodes are relevant, while accessing the same-mesh materials?
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?
Both part of the same instance. I want to access and alter these two materials;
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.
->> 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
var entityK = this.app.root.findByName("LightCookie_VersAfKvinde__FullBody");
var meshInstances = entityK.model.meshInstances; console.log('length:'+meshInstances.length);
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.