Change materials realtime

Godot Version

4.2

Question

Is it possible to toggle between material override next pass? Or assign multiple material slots to a mesh and toggle in realtime?

Yes, why wouldn’t it be possible? You can override the material of a surface with MeshInstance3D.set_surface_override_material(), override all the materials with GeometryInstance3D.material_override and set an extra material as an overlay with GeometryInstance3D.material_overlay

How to create another material slot to the mesh? I couldn’t find the option in inspector

Think of that as the active material. You can’t have several active materials at the same time. It’s just not how shaders work. You have one shader that renders each surface. You can have each surface have a different shader, but you can only assign one shader at a time to each surface.
If you want to hold multiple materials, you can just do so in an array you make, then swap the one that is in the override to change which one will render that surface.
The reason a mesh can have multiple materials is because each face of a mesh can be a different surface, but you have to define that during mesh creation (in blender) and in code you can only assign to those sets of faces, unless you do much more complex things to the mesh.

So from where do i get the other material to make an array and swap? Only for mesh imported from blender?

You can @export var my_materials: Array[Material] and then set them up in the editor. If you do this on a class that extends MeshInstance3D you can more easily use them in code.

I still don’t know from where do i get the extra material to add in Array[Material] :crazy_face:

A Material is a Resource, so you can make it in the editor, import it from something else, like a blender file, or make it in code. That’s your choice. If you have questions specific to how to make custom materials, that’s an entirely different topic, so ask a more concise question.

Now i know from where i can get the other Material to swap. Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.