1

Add Instance Uniforms to Shader for Unique Lightning Effects on Instantiation

Suggested by
on February 22, '25
Completed
add instance uniforms to my shader to get variations when it's instantiated so the lightning doesn't all look the same

Watchers

[ #1 ] 10 months ago by

To add variation each time your shader effect is instantiated, you can use Godot 4's per-instance shader uniforms feature. This allows you to set unique values for each instance without duplicating the entire shader. Here's how you can modify your code to achieve this: In your shader code, declare the parameters you want to vary as instance uniforms:

text shader_type spatial; // or canvas_item, depending on your use case

instance uniform float random_seed : hint_range(0, 1); instance uniform float speed_variation : hint_range(0.5, 1.5);

void fragment() { vec2 uv = 2.0 * UV - 1.0;

// Use the random_seed to create variation float varied_speed = speed * speed_variation; uv += 2.0 * fbm(uv + TIME * varied_speed + random_seed, octave_count) - 1.0;

float dist = abs(uv.x); vec3 color = effect_color * mix(0.0, 0.3, hash12(vec2(TIME + random_seed))) / dist; COLOR = vec4(color, 0.8); } In your GDScript code, when instantiating the object with the shader, set unique values for these instance uniforms:

text func instantiate_effect(): var instance = preload("res://your_effect_scene.tscn").instantiate() add_child(instance)

# Assuming your shader is on a MeshInstance3D named "EffectMesh" var material = instance.get_node("EffectMesh").get_surface_override_material(0)

# Set unique values for instance uniforms material.set_shader_parameter("random_seed", randf()) material.set_shader_parameter("speed_variation", randf_range(0.5, 1.5)) This approach will create variation in several ways: The random_seed will offset the fbm function, creating unique patterns for each instance16. The speed_variation will make each instance's animation slightly faster or slower1. The mix() function in the color calculation will now use a unique seed, varying the color intensity for each instance1. Remember to adjust the ranges and parameters to suit your specific effect needs. This method allows you to maintain a single shader while still achieving varied results across multiple instances267. Citations: https://godotengine.org/article/godot-40-gets-global-and-instance-shader-uniforms/ https://www.reddit.com/r/godot/comments/16yyxes/godot_4_setting_a_shader_param_affects_all/ https://forum.godotengine.org/t/how-to-change-a-shaders-parameters-individually-for-multiple-instances/3750 https://forum.godotengine.org/t/change-shader-param-in-multiple-instances-from-gdscript/7293 https://forum.godotengine.org/t/strange-unique-shader-instance-behaviour/96779 https://forum.godotengine.org/t/adjust-shaders-per-instance/37948 https://github.com/godotengine/godot/issues/83472 https://www.youtube.com/watch?v=BquGTgW7Te4 Answer from Perplexity: pplx.ai/share

[ #2 ] 10 months ago by

can't add instance uniforms to canvas_item shaders. dealt with this in rand c code

You don't have permission to comment on this issue.