Jellyfish

glint · Examples · Tutorials · Gallery

Three glowing jellyfish swimming upwards in pulses (the bell squeezes, they shoot up, drift), trailing wavy tentacles through falling marine snow.

Jellyfish: a GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

vec3 jelly(vec2 uv, float seed, float scale, vec3 tint) {
    float t = time * 0.5 + seed * 7.0;
    float pulse = pow(0.5 + 0.5 * sin(t * 3.0), 3.0);
    // Swims up in spurts (the pulse), wrapping round.
    float y = mod(t * 0.2 + 0.15 * (t - sin(t * 3.0) / 3.0) * 0.3 + seed, 1.6) - 0.8;
    vec2 p = (uv - vec2(sin(t * 0.3 + seed * 3.0) * 0.5, y)) / scale;
    vec3 col = vec3(0.0);
    // The bell: the top half of an ellipse, narrower when it squeezes.
    vec2 b = p / vec2(0.16 * (1.0 - 0.2 * pulse), 0.12 * (1.0 + 0.15 * pulse));
    float d = length(b) - 1.0;
    if (p.y > -0.01) {
        col += tint * smoothstep(0.1, -0.3, d) * (0.3 + 0.5 * smoothstep(-1.0, 0.0, d));
        col += tint * 0.02 / (abs(d) * 0.12 + 0.01) * 0.2;
    }
    // Tentacles.
    for (int i = 0; i < 6; i++) {
        float fi = float(i);
        float x0 = (fi / 5.0 - 0.5) * 0.24;
        float len = 0.35 + 0.15 * hash(fi + seed);
        float down = -p.y;
        float x = x0 * (1.0 - down) + sin(down * 12.0 - t * 4.0 + fi) * 0.03 * down;
        float dd = abs(p.x - x);
        col += tint * 0.003 / (dd + 0.004) * step(0.0, down) * smoothstep(len, 0.0, down) * 0.35;
    }
    return col * scale;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 col = mix(vec3(0.0, 0.02, 0.06), vec3(0.0, 0.1, 0.18), uv.y + 0.5);
    // Marine snow drifting down.
    vec2 s = uv * 30.0 + vec2(0.0, time * 0.6);
    col += vec3(0.4, 0.5, 0.6) * step(0.985, hash(floor(s))) * smoothstep(0.3, 0.0, length(fract(s) - 0.5)) * 0.5;
    col += jelly(uv, 0.1, 0.7, vec3(0.3, 0.6, 1.0));
    col += jelly(uv, 0.47, 1.0, vec3(1.0, 0.35, 0.7));
    col += jelly(uv, 0.83, 0.55, vec3(0.5, 1.0, 0.7));
    fragColor = vec4(1.0 - exp(-col * 1.3), 1.0);
}

More More GLSL examples

Invaders · Gears · Phyllotaxis · Pong · Fireflies · Newton fractal · Burning ship · Perfect loop