Changing Textures on Imported GLB

I’ve seen this in a few forum posts, but I wanted to change the textures on an imported GLB file. I’ve seen in most examples that a new material is created and the GLB file then gets that new material. Is this best practice?

When I import a GLB file I tried to change the texture map (color map) as such:

mesh.material.albedoTexture = new Texture('texture path', scene);

or

mesh.material.diffuseTexture = new Texture('texture path', scene);

In both those scenarios, the albedo/diffuse texture does not pop up as being part of that mesh’s material. So is it safe to assume that I cannot change the textures of the GLB file but instead must create a new material and assign that to the GLB (with the new textures)?

The repro would be helpful. It seems that probably you either don’t find the right mesh or don’t assign a texture properly.
Did you try to have a look at your model in the Inspector?
You don’t need to create new material if you want to change the texture of existing one.
Here is one of examples - Mu-so 2nd Generation 3D , you may change texture with click.

2 Likes

Seems to be the same question answered here:

1 Like

It’s somewhat the same. I know how to swap out the textures. My question is how would I directly change the texture on the material that comes with the GLB file? In the link you provided, the solution included creating a new material and assigning that to the mesh then assigning a texture to that new material. That’s the approach I’ve seen in most examples, but I haven’t seen any yet that just directly change the texture on the GLB file.

Here is a playground (https://playground.babylonjs.com/#JUKXQD#1944) that includes some models I’ve created in Blender with some textures. It’s fairly simple and should be easy to modify but I can’t seem to change the textures directly. In the code I reference the cube after importing. When I start typing out:

cube.material

I don’t see anything regarding the ability to change any kind of texture, albedo or diffuse. So is this something that can’t be done directly? Do I need to create a new material, assign it to the cube, then change the texture?

To add to this, why can’t I access the diffuse texture of a material when I chain it as such?:
sphere0.material.diffuseTexture

Here is the playground for that (I included the code snippet above)

I can access the diffuse texture just fine if I reference the material directly, but not if I reference the material through the mesh. Is there some other way I’m supposed to access this? A while back I was trying to change the uv scale of the texture and was having a hard time understanding why I couldn’t change this through the material. I had to create a texture separately then I could access it.

1 Like

I am not sure what is your issue here and why you seem not to be able to access it. You can see that I took your example playground here and changed the texture directly on existing material without any issue.

3 Likes

Maybe this simple example will suit your needs - https://playground.babylonjs.com/#QCU8DJ#166
And here all other old textures are disposed too - https://playground.babylonjs.com/#QCU8DJ#167

1 Like

Also note that GLB file will always come in with PBRMaterials. So diffuseTexture on GLB files doesn’t exists and you cannot access it as it is not part of the PBR workflow. Instead you can access albedoTexture.

3 Likes

And you always can check all PBRMaterial methods and properties here - PBRMaterial | Babylon.js Documentation

3 Likes

I think the problem is just that when you create the material and assign it to a variable then the editor knows what type it is, but when you use the material property, which is of type Material, then the editor doesn’t know what sub class the accessed object is and so can’t provide proper type checking and code completion…

1 Like

I get everything you all are saying but for some reason, it doesn’t work on my end. Here is the updated playground (https://playground.babylonjs.com/#JUKXQD#1946). As far as I know, everything is the same as the examples provided but nothing changes.

I even changed the way I grabbed the mesh, I used the scene.getMeshByName(“Cube”) thinking that was the issue, but it doesn’t do anything then either. I saved it and reloaded it multiple times. What am I doing wrong here?

I uploaded a new texture to test out as well and was able to use it just fine here: https://playground.babylonjs.com/#QCU8DJ#168

On my own GLB files it just doesn’t seem to work. I don’t get why this wouldn’t work on my end, feels like I’m taking crazy pills here.

I also added went into the Sandbox to see if I could manually change the texture and that seems to work fine.

1 Like

Looks like it’s using the emissive texture, changing that to the brick works with your model. :slight_smile:

1 Like

Thank you. I was noticing that I could see a “glint” of the brick texture and started swapping out some textures and finally started to see it come through. That combined with what you mentioned earlier about the material not being referenced previously (and not showing material properties) threw me off.

Thanks to everyone for helping out with this, I know it’s frustrating when it seems so simple and straightforward but sometimes small things end up causing unforeseen issues.

2 Likes