MultiMesh with different material per instance?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Foxcataria

Hi community :slight_smile:

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

Problem:
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…

Question:
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 :slight_smile:

Thank you! :slight_smile:

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!

path9263 | 2018-03-13 06:46

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

GameVisitor | 2019-06-09 08:57

:bust_in_silhouette: Reply From: Galmiza

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 set_instance_custom_data ( int instance, Color custom_data )