Adjust Shaders Per Instance

Godot Version

4.1.3

Question

Saw that it was now possible to adjust shader parameters per instance without having to duplicate the shader:

Unfortunately, there is like zero information online about this. Does anyone know how to accomplish this?
Was wondering how one would go about doing that in GDScript.

This is documented here: Shading language — Godot Engine (stable) documentation in English

Is there any plans on doing it for 2D as well?

2 Likes

It’s currently being worked on for particle shaders. The goal is to do the same for CanvasItem shaders in the future, although I can’t give an ETA.

1 Like

For other searching for this, it’s now implemented for Sprite2D (canvas_item shader).

In GDScript:

($Sprite2D as Sprite2D).set_instance_shader_parameter("outline_color", Color.BLUE)

In the shader:

instance uniform vec4 outline_color : source_color = vec4(1.0, 0.0, 0.0, 1.0);

Note that the method is called on the Sprite2D instead of the material (as is done with previous uniform vars).

You can still set parameters from the material if you like:

sprite.material.set_shader_parameter("progress", 0.0)