Change material / textures on a imported GLB trough MeshAssetTask

Hi I am beginner and havent found an example on this:

I imported an animated model with a lot of nested objects, each have a different material. in total there are 8 materials in the imported GLB. I want to edit all of them. How can i access and change them?
I havent find any example to change the material using a MeshAssetTask.

At the moment my code looks like this, and its working fine(its scaling an animating):

meshTask.onSuccess = function (task) {
alert(“Model Loaded”);
var screen = task;
screen.loadedMeshes[0].scaling = new BABYLON.Vector3(0.05, 0.05, 0.05);
screen.loadedAnimationGroups[1].loopAnimation = true;
screen.loadedAnimationGroups[1].start(true);
}

i have found getMaterialByName class but its apply to the scene. if I write sindide the function:

scene.getMaterialByID(“shine”).diffuseColor = new BABYLON.Color3(1, 0, 1);

and i console log the material, looks like it has been changed, but my model doesnt react to it. so i guess MeshAssetTask is making an instance that is different.

probably this is very easy so sorry in advance :slight_smile:

Hi.

Well it can be simple as task.loadedMeshes[0].material. Which simply changes your material on that mesh. Or in case you want to target that mesh by name you can create for if loops to check for the meshes names, and if name is proper apply material on that mesh.

Like this.

https://www.babylonjs-playground.com/#SXEV7#5

To be honest I think scene.getMaterialByName should work also.

https://www.babylonjs-playground.com/#SXEV7#6

I am not sure why it doesn’t work in your example. Maybe you didn’t put it in onSuccess callback.

2 Likes

By the way. I just noticed that you are talking about getMaterialByName, but you used getMaterialByID. So maybe that’s the problem :smiley:

hi!, thanks for answering. i cannot try things right now, i will try them tomorrow morning. but here are some toughts:
my task.loadedMeshes[0] points to the root object, an empty parent holding everything. so it doesnt have a material. There are several hundred meshes inside the 3d model (its a nested animation thats why). thats why i cannot check them one by one or check for some pattern in the names.

since there are only 6 materials i wanted to change directly whatever babylon already imported. I am going to try again with getMaterialByName, thank you!

Hi,

i have already try! both options you send work. thank you very much!

Mmmm, sorry but its not working as i expected :frowning:

if I put this onSuccess callback:

    for (var i = 0; i < task.loadedMeshes.length; i++) {
        if (task.loadedMeshes[i].name.toLowerCase().includes('cyl')) {
            task.loadedMeshes[i].material = metal;

        }
    }

I am getting a PBR material. “metal” es being created when my HDRI is loaded. If I turned the loop off and make:

    var grayNr = new BABYLON.PBRMaterial("graynr", scene);
    grayNr = scene.getMaterialByName("grayFBXASC032normal"); 
    grayNr.reflectionTexture = hdrTexture;
    grayNr.microSurface = 1;
    grayNr.reflectivityColor = new BABYLON.Color3(1, 1, 1);
    grayNr.albedoColor = new BABYLON.Color3(1, 1, 1);

the color is changing, but its not reflecting the HDR, so it doenst look like it is PBR (probably just standardMaterial.

ideas?

hmm. Well I am not sure if getting material with scene.getMaterialByName overwrites the initial declaration
var grayNr = new BABYLON.PBRMaterial(“graynr”, scene);

So maybe if you create grayNr as PBR and after that setting that variable to existing material which is not PBR but StandardMaterial it overwrites inital PBR definition. Not sure tho that’s how it works

But what first come my mind is to try to play with metallic and roughness properties.
so

grayNr.rougness = 0;
grayNr.metallic = 1;

That would tell you if that is the problem, it should be mirrorlike reflection. So if that’s the problem, just play with those properties.

HI!

so i created a playground. as you see I am trying to get the same result on my cylinder as in my candle, but it doesnt work:

https://www.babylonjs-playground.com/#TSCJPP#4

Okay. Well, as I understood you imported Material_002 with your file? I believe that your material is imported as StandardMaterial (and I believe you cannot import it as PBR material into babylon, I may be wrong, I would like if someone else could confirm or deny this). So that’s the reason Material_002 is StandardMaterial.

Also, I don’t think you can just switch StandardMaterail to PBRMaterial, because they work on different principles.

I believe that you have to recreate that material as PBR from scratch inside Babylon code. I tried few things to try to avoid this but nothing worked.

I will let you know if I find something out.

1 Like