Thousands of Fish - Dynamically changing the fish type/color/etc.

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

In the tutorial Thousands of Fish, can I (and how) dynamically change the fish in GD Script Code (e.g. in my Game Logic, randomizing the type and color of fish), such as:

Change the mesh (e.g. type of fish), from available resource mesh files
Change the color (Surface 1, Surface 2, etc.)
Keep the shader program (e.g. movement) otherwise unchanged

:bust_in_silhouette: Reply From: DDoop

You would need to create separate MultiMesh objects for each distinct fish mesh.
MultiMesh depends on using a single mesh.

You can modulate the color of a given MultiMesh mesh instance without impacting other meshes of that object. That documentation you linked outlines near the bottom (the INSTANCE_CUSTOM section) how one would go about doing it.

Coloring in GDScript:

for i in range($School.multimesh.instance_count):
     $School.multimesh.set_instance_custom_data(i, Color(randf(), randf(), randf(), randf()));

MultiMesh custom data is just a 4 component vector (perfect for color+another dimension)

If you did find more fish meshes and want to include them, you should be able to save the motion shader from your first try as a file external to the scene (this is generally recommended, as Godot’s scene-specific resources aren’t perfect). Then it’s just a matter of applying that shader to the new MultiMesh objects with the new, different meshes.

EDIT: forgot to mention the custom instance data is available in the shader as the variable INSTANCE_CUSTOM