The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+6 votes

Hi community :)

I have a question about using MultiMesh with a different material per instance.

Let's say I want to draw a chunk that contains 16x16x256 cubes like in Minecraft.

The way I actually do this is by using one MultiMeshInstance node with a MultiMesh assigned to it. The instance_count of the appropriate MultiMesh is set to 65536. The mesh of the MultiMesh is set to an ArrayMesh that represents my cube. After setting all things up, I can draw exactly 65536 cubes with only one draw call which is what I wanted so far. But now comes the problem: Assigning materials to the cubes.

First I only wanted every instance to use the same material. In this case a simple SpatialMaterial so that all cubes look like grass blocks. This was very easy so at the end all 65536 cubes looked like grass blocks. To achieve this I used the surface_set_material() function of the ArrayMesh instance that represents my cube.

Then I wanted two different block types within my chunk, a grass block and a stone block. And there is my problem: I can not figure out how to set a material per instance. Currently I have to set the material directly within the ArrayMesh instance that represents my cube. Which leads to only grass blocks or only stone blocks within my chunk...

Is there a way to have one MultiMeshInstance node with one MultiMesh assigned to it and a different material per instance of the MultiMesh's mesh?
So that I can just say something like:

# No really code:
MultiMesh.mesh = cube
MultiMesh.instance_count = 65536
MultiMesh.set_instance_material(0, grassMaterial)
MultiMesh.set_instance_material(1, stoneMaterial)

Or is there another way to achieve this?

I hope that I presented my problem in a way that it's understandable what I try to achieve.
If you need more detail then just let me know.
And sorry for my english. English is not my native language :)

Thank you! :)

in Engine by (22 points)
edited by

You can apparently do this with SurfaceTool if you want to generate your own meshes but I am not sure about MulitMesh. I would like to know as well if someone has figured it out!

This is interesting, can u share a sample code / demo version of ur game ?

1 Answer

+1 vote

You can't use several materials because the instances will be rendered in a single draw call.
But you can use a single material that depends on the custom data of each instance.
void setinstancecustomdata ( int instance, Color customdata )

by (16 points)
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 Frequently asked questions and 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 [email protected] with your username.