Fireworks animation in COPs

calendar_today

23.12.2022

label

VEX, Rendering

mouse

Houdini 19.5

Description

A fireworks-like shader in COPs. Based on a Shadertoy tutorial by Martijn Steinrucken / Art of Code.

1 Code discussion

Two for loops create this effect: The explosion loop randomly sets the center of the sparks using samplecircleuniform() that are radially emitted by the firework function with sampledirectionuniform(). Play with the value dir.z in line 7 to control the blur of the sparks.

// FIREWORK
function float firework(vector uvw, pos; float tfr; int c){
    float mask = 0.0;
    int num = int(rand(c) * 64);
    for(int i = 0; i < num; i++){
        vector dir = sample_direction_uniform(rand(c, i));
        dir.z = 0.0;
        vector offset = dir * tfr * 0.5;
        offset.y -= sin(tfr * M_PI) * 0.15;
        float dist = length2(uvw - pos + offset) + 1e-5;
        float fade = sin(tfr * M_PI);
        mask += 1e-4 / dist * fade;
        }
    return mask;
}

// CANVAS
vector uvw = set(X - 0.5, Y - 0.5, 0.0);
uvw[0] *= XRES / float(YRES);

// COLOR
float t = TIME * 0.25;
vector clr = 0.0;
int explosions = 24;
for(int i = 0; i < explosions; i++){
    t += rand(i, 123);
    int tf = floor(t);
    float tfr = frac(t);
    vector col = rand(i, 143);
    vector pos = sample_circle_uniform(rand(i, tf));
    clr += col * firework(uvw, pos, tfr, i);
}

clr *= 1.0 - dot(uvw, uvw);

// OUTPUT
assign(R, G, B, clr);
download

Downloads

smart_display

Videos

Coding a fireworks effect