+2 votes

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 Engine by (28,744 points)
edited by

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.

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?

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

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.

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.

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

2 Answers

+3 votes
Best answer

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

var st = SurfaceTool.new()
# 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 :)

by (28,744 points)
+1 vote

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

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

by (485 points)
edited by

This is how my meshes look after I import them.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.