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

0 votes

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

Godot version v3.2.3.stable.official
in Engine by (23 points)

1 Answer

0 votes

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

by (749 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.