Apply shader to certain parts of mesh

Godot Version

Godot 4.3

Question

I have a mesh with a water shader, but I only want the shader to apply to small, random parts of the mesh. How would I do this?

It depends on the result you want to achieve. If you don’t mind having harsh, low-resolution patches on your mesh, you can simply assign two different materials to your mesh. The material index of each face can then be changed to fit your needs.

Blender-to-Godot Example

This example uses a subdivided cube as the source mesh.

I start by applying two different materials to the mesh. I then select a random set of faces in edit mode via: Select → Select Random. With the faces still selected, I can click on the material I want for the faces over in the Material tab and click Assign.

To export the mesh with the assigned materials, I go to: File → Export → glTF 2.0. In the export window, on the right side, I go to Data → Material and make sure that Materials is set to Export.
I tried using the Placeholder option but that doesn’t seem to work, and is corroborated by other sources (see issue).

Having selected a directory inside my Godot project, I hit Export.

image

Inside Godot, you can then apply your own material to each of the mesh’s material slots by selecting an external material file (after double-clicking the mesh file).

If you seek more fine-grained control of which parts are affected by your shader, you have to create a specialized shader that makes use of a mask texture to interpolate between two sets of material properties. This latter approach can, depending on your water shader, be a little more involved so I won’t be providing an example for this.


I hope that helps. Let me know if you have any more questions.