MultimeshInstance2D Animation Variance
I started using the PerfBullets addon, but found that it wasn’t flexible enough for what I wanted from a projectile system so I decided to build my own. Using MultimeshInstance2D and some shader shenanigans, I was able to get thousands of animated projectiles on the screen at once with a noise texture to introduce color variance over time.
Here’s the shader code for that, enjoy!
Shader code
shader_type canvas_item;
// separates the texture into animation frames; derived from PerfBullets' implementation
uniform float col; //float for performance sake but never input a float here, only int
uniform float row;
uniform vec4 custom; //visiblity, offsetx, offsety (defines which frame to show)
uniform bool should_flicker_brightness; // add color variance to animation instances
uniform sampler2D noise : hint_normal; // only if should_flicker_brightness == true
varying vec2 v_local_position;
varying vec2 v_normalized_screen_position;
void vertex() {
if (should_flicker_brightness == true) {
v_local_position = UV;
vec2 v_clip_position = (SCREEN_MATRIX * MODEL_MATRIX * vec4(v_local_position, 0.0, 1.0)).xy;
v_normalized_screen_position = v_clip_position * 0.5 + vec2(0.5);
}
}
void fragment() {
vec2 uv = UV;
uv = vec2(uv.x/col, uv.y/row);
uv.y += custom.b/row; // only use ints or sadness happens
uv.x += custom.g/col;
vec4 pixel_color = texture(TEXTURE, uv);
if (should_flicker_brightness == true){
vec4 noise_adjust = texture(noise, vec2(v_normalized_screen_position.x, 1.0-v_normalized_screen_position.y));
pixel_color *= (noise_adjust + 0.25 );
}
COLOR = pixel_color;
}