How to generate a mesh with multiple materials?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zylann
:warning: Old Version Published before Godot 3 was released.

I generate a mesh with SurfaceTool, but I wonder how can I define multiple materials for different parts of the mesh? Like: solid part, transparent part, two-sided part etc

The only way I have at the moment is create a MeshInstance node per material, but it sounds ineficient because the engine has to cull several times the same instance at the same position…

Just for the context, I am doing voxel terrain generation where some of the chunks have transparent cubes, some are two-sided and others might have special shaders etc.

In blender every object of a mesh for my projects has to be separate objects. Then I put them under a single object type then export. The engine really doesn’t have good support for meshes.

SupToasty | 2016-04-26 13:21

I don’t use Blender, I’m doing procedural generation within Godot directly.
I see there are “surfaces” inside a mesh, but SurfaceTool only allows to commit geometry in a mesh., and I don’t know how multi-materials work.
Do you mean I have to create a separate MeshInstance node for each part of my mesh?

Zylann | 2016-04-26 13:24

Sadly yes. If you don’t use a tool like blender you can’t get just one mesh.

SupToasty | 2016-04-26 13:27

I can get a mesh without Blender (otherwise it would be really really sad), the only problem is how do I generate it so it has multiple materials on it.

Zylann | 2016-04-26 13:29

Miscommunication on my part I forgot to add with multiple materials to the end of that last comment. Sorry. I known you can get a mesh without a tool like blender.

SupToasty | 2016-04-26 13:35

So you mean it’s not possible to get multi-materials on meshes that don’t go through the mesh importer?

Zylann | 2016-04-26 13:38

:bust_in_silhouette: Reply From: eaglecat

use add_surface() and surface_set_material() in Mesh classenter image description here

this is not code generated. but this may help you understand

This is how my meshes look after I import them.

SupToasty | 2016-04-26 16:42

:bust_in_silhouette: Reply From: Zylann

bojidar-bg told me that simply using set_material() on SurfaceTool would do the trick:

var st =
# add vertices...
var mesh = st.commit()
# add other vertices...
st.commit(mesh) # This appends a new surface to the mesh

In my case I even use two SurfaceTools so my generator can create the mesh in one pass.
That does the trick :slight_smile: