Crimson Oni Blade Cleave shader

crimson-oni-blade-cleave.glsl · 781 lines · GLSL ES 3.00 · MIT

Crimson Oni Blade CleaveGame VFX3s Loop
Crimson Oni Blade Cleave WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 781 linesSPEC GLSL ES 3.00

Seamless PNG Texture Baker
1-Click Engine Shader Export
WebGL2 .glsl
.glsl
Godot 4 .gdshader
Three.js GLSL3

Engine shader code & wrappers

Stylized Cel VFX
crimson-oni-blade-cleave.glsl (781 lines)
#version 300 es
// Crimson Oni Blade Cleave (crimson-oni-blade-cleave.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/vfx_stylized/lib/01_crimson_oni_blade_cleave.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float), uOpen (0..1), uLightMode (0..3), uCamMode (0..1)
precision highp float;
precision highp int;

uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;

out vec4 fragColor;

// ============================================================================
// 01_crimson_oni_blade_cleave.glsl
// Spider-Verse / Hades / Valorant stylized cel-shaded 3D crimson oni katana
// cleave arc above a dark matte ink-grouted charcoal tiled arena floor.
//
// Features:
//   - Linear-space PBR/cel palette calibrated for sRGB gamma so dark matte
//     charcoal floor lands at sRGB vec3(0.08, 0.09, 0.12) with crisp dark ink
//     grout sRGB vec3(0.015) and zero washed-out pink haze
//   - Bold black-ink contour outlines (Fresnel + radial edge ink) on all meshes
//   - 3-band quantized cel shading: deep blood-black -> blazing vermilion ->
//     jagged white-hot comic lightning leading edge
//   - Hand-inked stylized Oni mask & flame markings embedded on the cleave arc
//   - 3D katana hilt (tsuka diamond wrap, gold/black ornate tsuba, habaki, kashira)
//   - Flat-shaded billowing comic smoke puffs with swirling interior ink lines
//   - Floating faceted crimson diamond graphic sparks with 2-tone cel split
//   - Crisp localized cel-stepped crimson light pool & razor-sharp graphic shadow
//   - 3-second seamless animation loop (`uTime`); `uOpen` widens cleave & smoke
// ============================================================================

const float PI  = 3.14159265359;
const float TAU = 6.28318530718;

// Locked stylized palette (in linear space; sRGB targets noted in comments)
const vec3 COL_FLOOR       = vec3(0.0040, 0.0052, 0.0095); // -> sRGB ~(0.08, 0.09, 0.12)
const vec3 COL_GROUT       = vec3(0.00022, 0.00022, 0.00028); // -> sRGB ~(0.018)
const vec3 COL_INK         = vec3(0.00020, 0.00018, 0.00026); // -> sRGB ~(0.017)
const vec3 COL_BLOOD_DARK  = vec3(0.0065, 0.0003, 0.0006); // -> deep blood-black
const vec3 COL_CRIMSON_MID = vec3(0.320,  0.0022, 0.0045); // -> rich crimson mid-band
const vec3 COL_VERMILION   = vec3(0.860,  0.0110, 0.0160); // -> blazing vermilion red
const vec3 COL_HOT_EDGE    = vec3(1.18,   1.06,   1.02);   // -> white-hot leading edge
const vec3 COL_GOLD_LIT    = vec3(0.720,  0.390,  0.072);  // -> warm stylized gold
const vec3 COL_GOLD_SHAD   = vec3(0.175,  0.062,  0.011);  // -> deep bronze-gold shadow
const vec3 COL_SMOKE_LIT   = vec3(0.540,  0.435,  0.385);  // -> warm parchment smoke
const vec3 COL_SMOKE_SHAD  = vec3(0.165,  0.120,  0.112);  // -> cel-shaded smoke shadow

// Material IDs
const float MAT_FLOOR   = 1.0;
const float MAT_BLADE   = 2.0;
const float MAT_TSUKA   = 3.0;
const float MAT_GOLD    = 4.0;
const float MAT_SMOKE   = 5.0;
const float MAT_DIAMOND = 6.0;
const float MAT_RIM     = 7.0;

// ----------------------------------------------------------------------------
// Math & Utility Helpers
// ----------------------------------------------------------------------------

float loopPhase() {
    return fract(uTime / 3.0);
}

mat2 rot2(float a) {
    float c = cos(a), s = sin(a);
    return mat2(c, -s, s, c);
}

float hash21(vec2 p) {
    p = fract(p * vec2(234.34, 435.345));
    p += dot(p, p + 34.23);
    return fract(p.x * p.y);
}

float noise2(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    float a = hash21(i);
    float b = hash21(i + vec2(1.0, 0.0));
    float c = hash21(i + vec2(0.0, 1.0));
    float d = hash21(i + vec2(1.0, 1.0));
    return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}

float smin(float a, float b, float k) {
    float h = max(k - abs(a - b), 0.0) / max(k, 1e-4);
    return min(a, b) - h * h * k * 0.25;
}

vec2 opU(vec2 a, vec2 b) {
    return (a.x < b.x) ? a : b;
}

float sdSegment2(vec2 p, vec2 a, vec2 b) {
    vec2 pa = p - a, ba = b - a;
    float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);
    return length(pa - ba * h);
}

float sdCapsule(vec3 p, vec3 a, vec3 b, float r) {
    vec3 pa = p - a, ba = b - a;
    float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);
    return length(pa - ba * h) - r;
}

float sdBox(vec3 p, vec3 b) {
    vec3 q = abs(p) - b;
    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

float sdDiamond(vec3 p, vec3 s) {
    vec3 q = abs(p) / s;
    float m = q.x + q.y + q.z - 1.0;
    return m * min(s.x, min(s.y, s.z)) * 0.57735;
}

// ----------------------------------------------------------------------------
// Cleave Frame & Smooth-Lipschitz Jagged Comic Leading Edge
// ----------------------------------------------------------------------------

// Tilted cleave local frame: tilted nicely toward the camera (~28 deg pitch)
// so the full crimson Oni crescent, white-hot edge, and cel bands read boldly.
vec3 toCleaveLocal(vec3 p) {
    float ph = loopPhase();
    float sweep = sin(ph * TAU) * 0.055;
    float bob   = sin(ph * TAU) * 0.025;

    vec3 q = p - vec3(0.30, 0.86 + bob, 0.05);
    q.xz = rot2(-0.12 + sweep * 0.35) * q.xz;
    q.yz = rot2(0.52) * q.yz;
    q.xy = rot2(0.08 + sweep) * q.xy;
    return q;
}

// Smoothly bounded jagged comic-lightning profile for the 3D outer rim (safe Lipschitz)
float jaggedEdgeSDF(float uArc) {
    float ph = loopPhase();
    float flow = uArc * 7.0 - ph * 2.0;
    float w1 = sin(flow * TAU) * 0.5 + 0.5;
    float w2 = sin(flow * TAU * 2.0 + 1.1) * 0.5 + 0.5;
    float spike = pow(w1, 2.2) * 0.075 + pow(w2, 2.6) * 0.042;
    float crestBoost = 1.0 + 0.65 * exp(-pow((uArc - 0.72) / 0.20, 2.0));
    float env = smoothstep(0.04, 0.18, uArc) * smoothstep(1.0, 0.85, uArc);
    return spike * crestBoost * env;
}

// Razor-sharp graphic sawtooth used in shading for the white-hot comic lightning edge
float jaggedEdgeGraphic(float uArc) {
    float ph = loopPhase();
    float flow = uArc * 9.0 - ph * 3.0;
    float s1 = abs(fract(flow) - 0.20);
    float s2 = abs(fract(flow * 1.65 + 0.41) - 0.18);
    float spike = smoothstep(0.34, 0.0, s1) * 0.16 + smoothstep(0.28, 0.0, s2) * 0.09;
    float crestBoost = 1.0 + 0.75 * exp(-pow((uArc - 0.72) / 0.18, 2.0));
    float env = smoothstep(0.03, 0.15, uArc) * smoothstep(1.0, 0.86, uArc);
    return spike * crestBoost * env;
}

// Evaluate cleave polar coordinates (uArc in [0,1], vRadial in [0,1], 2D SDF)
void evalCleaveArc(vec2 p2, out float uArc, out float vRadial, out float d2d) {
    float openAmt = clamp(uOpen, 0.0, 1.0);
    vec2 cOut = vec2(0.42, -0.04);
    vec2 dOut = p2 - cOut;
    float rOut = length(dOut);
    float ang = atan(dOut.y, dOut.x);
    if (ang < 0.35) ang += TAU;

    float angStart = 1.22;
    float angEnd   = 4.20 + 0.14 * openAmt;
    float angSpan  = angEnd - angStart;
    uArc = clamp((ang - angStart) / angSpan, 0.0, 1.0);

    float R_base = 1.36 + 0.20 * openAmt;
    float jag    = jaggedEdgeSDF(uArc);
    float R_out  = R_base + jag - 0.05 * smoothstep(0.84, 1.0, uArc);

    float bellyWidth = 0.50 + 0.18 * openAmt;
    float taper = sin(uArc * PI);
    float widthAtU = mix(0.045, bellyWidth, pow(max(taper, 0.0), 0.70));
    widthAtU *= smoothstep(1.005, 0.90, uArc) * 0.92 + 0.08;
    widthAtU = max(widthAtU, 0.105 * smoothstep(0.18, 0.0, uArc));

    float R_in = R_out - widthAtU;

    float dRad = max(rOut - R_out, R_in - rOut);
    float dAngStart = (angStart - ang) * max(rOut, 0.35);
    float dAngEnd   = (ang - angEnd) * max(rOut, 0.35);
    float dAng      = max(dAngStart, dAngEnd);

    d2d = max(dRad, dAng);
    vRadial = clamp((rOut - R_in) / max(R_out - R_in, 1e-4), 0.0, 1.0);
}

// ----------------------------------------------------------------------------
// Katana Hilt (Tsuka, Tsuba, Habaki, Kashira) SDF in Cleave Local Space
// ----------------------------------------------------------------------------

vec2 mapHilt(vec3 q) {
    float openAmt = clamp(uOpen, 0.0, 1.0);
    float R_base  = 1.36 + 0.20 * openAmt;
    float angStart = 1.22;

    vec2 anchor2 = vec2(0.42, -0.04) + (R_base - 0.055) * vec2(cos(angStart), sin(angStart));
    vec2 tang = normalize(vec2(sin(angStart), -cos(angStart)));
    vec2 norm = vec2(-tang.y, tang.x);

    vec3 rel = q - vec3(anchor2, 0.0);
    vec3 h = vec3(dot(rel.xy, tang), dot(rel.xy, norm), rel.z);

    vec2 res = vec2(1e5, MAT_GOLD);

    // 1. Habaki (gold tapered collar)
    vec3 hb = h - vec3(0.06, 0.0, 0.0);
    float dHabaki = sdBox(hb, vec3(0.08, 0.056, 0.042)) - 0.008;
    res = opU(res, vec2(dHabaki, MAT_GOLD));

    // 2. Tsuba (ornate lobed guard)
    vec3 tb = h - vec3(0.155, 0.0, 0.0);
    vec2 tYZ = vec2(tb.y * 0.85, tb.z * 1.12);
    float tAng = atan(tYZ.y, tYZ.x);
    float lobe = 0.018 * cos(tAng * 4.0);
    float dTsuba = max(abs(tb.x) - 0.030, length(tYZ) - (0.155 + lobe)) - 0.008;
    res = opU(res, vec2(dTsuba, MAT_GOLD));

    // 3. Tsuka (oval handle with diamond cord relief)
    float tsukaLen = 0.80;
    vec3 ts = h - vec3(0.18 + tsukaLen * 0.5, 0.0, 0.0);
    ts.y += 0.028 * (ts.x * ts.x);
    vec3 tsOval = vec3(ts.x, ts.y * 0.88, ts.z * 1.25);
    float dTsukaCore = sdCapsule(tsOval, vec3(-tsukaLen * 0.5, 0.0, 0.0), vec3(tsukaLen * 0.5, 0.0, 0.0), 0.060) * 0.78;
    float braidU = ts.x * 26.0;
    float braidV = atan(ts.z, ts.y) * 2.0 / PI;
    float diamondWave = abs(fract(braidU + braidV) - 0.5) + abs(fract(braidU - braidV) - 0.5);
    float cordRelief = smoothstep(0.45, 0.65, diamondWave) * 0.005;
    float dTsuka = dTsukaCore - cordRelief;
    res = opU(res, vec2(dTsuka, MAT_TSUKA));

    // 4. Kashira (gold pommel cap)
    vec3 ks = h - vec3(0.18 + tsukaLen + 0.036, 0.0, 0.0);
    ks.y += 0.028 * (0.42 * 0.42);
    vec3 ksOval = vec3(ks.x, ks.y * 0.88, ks.z * 1.22);
    float dKashira = sdBox(ksOval, vec3(0.046, 0.058, 0.058)) - 0.015;
    res = opU(res, vec2(dKashira, MAT_GOLD));

    return res;
}

// ----------------------------------------------------------------------------
// Stylized Comic Smoke Puffs SDF
// ----------------------------------------------------------------------------

float mapSmoke(vec3 p) {
    float ph = loopPhase();
    float openAmt = clamp(uOpen, 0.0, 1.0);
    float sc = 0.84 + 0.34 * openAmt;
    float drift  = sin(ph * TAU) * 0.065;
    float drift2 = cos(ph * TAU) * 0.045;

    // Lower-left billowing comic smoke plume
    vec3 pL = p - vec3(-1.42 + drift, 0.34 + drift2 * 0.35, 0.52);
    pL /= sc;
    float dL = length(pL - vec3(-0.18, 0.14, -0.05)) - 0.43;
    dL = smin(dL, length(pL - vec3(0.18, -0.05, 0.22)) - 0.31, 0.20);
    dL = smin(dL, length(pL - vec3(-0.46, 0.36, -0.26)) - 0.35, 0.22);
    dL = smin(dL, length(pL - vec3(0.48, -0.14, 0.46)) - 0.20, 0.16);
    dL = smin(dL, length(pL - vec3(0.72, -0.20, 0.62)) - 0.12, 0.12);
    dL = max(dL, -(p.y - 0.02));
    dL *= sc;

    // Upper-right billowing comic smoke plume behind hilt
    vec3 pR = p - vec3(1.54 - drift * 0.8, 0.44 - drift2 * 0.35, -0.78);
    pR /= sc;
    float dR = length(pR - vec3(0.24, 0.22, -0.12)) - 0.41;
    dR = smin(dR, length(pR - vec3(-0.12, 0.04, 0.10)) - 0.29, 0.18);
    dR = smin(dR, length(pR - vec3(-0.42, -0.10, 0.22)) - 0.18, 0.14);
    dR = smin(dR, length(pR - vec3(-0.66, -0.18, 0.28)) - 0.10, 0.10);
    dR = smin(dR, length(pR - vec3(0.46, 0.02, 0.18)) - 0.33, 0.18);
    dR = max(dR, -(p.y - 0.02));
    dR *= sc;

    return min(dL, dR);
}

// ----------------------------------------------------------------------------
// Floating Faceted Crimson Diamond Graphic Sparks SDF
// ----------------------------------------------------------------------------

float mapDiamonds(vec3 p) {
    float ph = loopPhase();
    float openAmt = clamp(uOpen, 0.0, 1.0);
    float spread = 0.90 + 0.24 * openAmt;
    float d = 1e5;

    for (int i = 0; i < 8; i++) {
        vec3 pos;
        vec3 sz;
        float speed;
        if (i == 0)      { pos = vec3(-1.42, 1.42, -0.68); sz = vec3(0.11, 0.16, 0.036); speed = 1.0; }
        else if (i == 1) { pos = vec3( 1.28, 0.76,  1.26); sz = vec3(0.13, 0.19, 0.042); speed = -1.0; }
        else if (i == 2) { pos = vec3( 1.05, 0.56,  0.34); sz = vec3(0.088, 0.135, 0.032); speed = 2.0; }
        else if (i == 3) { pos = vec3(-1.05, 0.70,  0.22); sz = vec3(0.068, 0.105, 0.026); speed = -2.0; }
        else if (i == 4) { pos = vec3(-0.18, 0.40,  0.50); sz = vec3(0.058, 0.092, 0.024); speed = 1.0; }
        else if (i == 5) { pos = vec3( 0.62, 0.28,  0.98); sz = vec3(0.055, 0.088, 0.022); speed = -1.0; }
        else if (i == 6) { pos = vec3(-0.72, 0.24,  1.05); sz = vec3(0.065, 0.098, 0.024); speed = 2.0; }
        else             { pos = vec3( 1.18, 1.50, -1.18); sz = vec3(0.072, 0.110, 0.026); speed = 1.0; }

        pos.xz *= spread;
        float fi = float(i);
        pos.y += 0.07 * sin(ph * TAU + fi * 1.37);
        pos.x += 0.04 * cos(ph * TAU + fi * 0.91);

        vec3 q = p - pos;
        q.xz = rot2(ph * TAU * 0.5 * speed + fi * 0.8) * q.xz;
        q.xy = rot2(0.35 + fi * 0.55) * q.xy;
        d = min(d, sdDiamond(q, sz));
    }
    return d;
}

// ----------------------------------------------------------------------------
// Master Scene SDF
// ----------------------------------------------------------------------------

vec2 mapObjects(vec3 p) {
    vec2 res = vec2(1e5, 0.0);

    // 1. Thickened, well-conditioned 3D Cleave Crescent Blade
    vec3 q = toCleaveLocal(p);
    float uArc, vRad, d2d;
    evalCleaveArc(q.xy, uArc, vRad, d2d);

    // Sculpted 3D diamond-wedge blade thickness (thick enough to avoid any slicing)
    float ridgeProfile = sin(clamp(vRad, 0.0, 1.0) * PI);
    float halfThick = 0.088 * (0.28 + 0.72 * ridgeProfile) * (0.45 + 0.55 * sin(uArc * PI));
    halfThick = max(halfThick, 0.018);

    float dZ = abs(q.z) - halfThick;
    float dBlade = length(max(vec2(d2d, dZ), 0.0)) + min(max(d2d, dZ), 0.0) - 0.006;
    // Conservative Lipschitz factor guarantees zero overstepping across spikes
    dBlade *= 0.45;
    res = opU(res, vec2(dBlade, MAT_BLADE));

    // 2. Katana hilt
    res = opU(res, mapHilt(q));

    // 3. Stylized comic smoke puffs
    res = opU(res, vec2(mapSmoke(p), MAT_SMOKE));

    // 4. Floating crimson diamond graphic sparks
    res = opU(res, vec2(mapDiamonds(p), MAT_DIAMOND));

    return res;
}

vec2 map(vec3 p) {
    vec2 res = vec2(p.y, MAT_FLOOR);

    float rFloor = length(p.xz);
    float dRim = max(abs(rFloor - 4.45) - 0.16, p.y - 0.16);
    res = opU(res, vec2(dRim, MAT_RIM));

    res = opU(res, mapObjects(p));
    return res;
}

// Unit-weight tetrahedral normal (exact zero-bug contract)
vec3 calcNormal(vec3 p) {
    const float e = 0.0020;
    const vec2 k = vec2(1.0, -1.0);
    return normalize(
        k.xyy * map(p + k.xyy * e).x +
        k.yyx * map(p + k.yyx * e).x +
        k.yxy * map(p + k.yxy * e).x +
        k.xxx * map(p + k.xxx * e).x
    );
}

// Crisp graphic hard-stepped shadow onto the arena floor
float calcGraphicShadow(vec3 ro, vec3 rd) {
    float t = 0.04;
    float minD = 1.0;
    for (int i = 0; i < 48; i++) {
        float h = mapObjects(ro + rd * t).x;
        if (h < 0.002) return 0.0;
        minD = min(minD, 14.0 * h / t);
        t += clamp(h * 0.65, 0.015, 0.20);
        if (t > 6.0) break;
    }
    return smoothstep(0.18, 0.36, minD);
}

// ----------------------------------------------------------------------------
// Hand-Inked Stylized Oni Mask & Flame Markings on Cleave Blade
// ----------------------------------------------------------------------------

float evalOniInkPattern(float uArc, float vRad) {
    float ink = 0.0;

    // 1. Sweeping flame-lick ink tendrils along the upper blade
    float flameWave = 0.30 + 0.13 * sin(uArc * 22.0) + 0.07 * sin(uArc * 41.0 + 1.2);
    float upperMask = smoothstep(0.05, 0.12, uArc) * smoothstep(0.46, 0.34, uArc);
    float flameBar  = smoothstep(0.055, 0.022, abs(vRad - flameWave)) * upperMask;
    float innerFlame = smoothstep(flameWave - 0.03, flameWave - 0.07, vRad) * upperMask;
    ink = max(ink, max(flameBar, innerFlame * 0.95));

    // 2. Stylized Oni Mask Motif at the wide mid-belly
    vec2 m = vec2((uArc - 0.56) * 6.4, (vRad - 0.42) * 3.2);

    float horn    = sdSegment2(m, vec2(-0.55, 0.24), vec2(-1.05, 0.90)) - 0.09;
    float hornTip = sdSegment2(m, vec2(-1.05, 0.90), vec2(-1.35, 1.15)) - 0.045;
    ink = max(ink, smoothstep(0.03, 0.0, min(horn, hornTip)));

    float brow     = sdSegment2(m, vec2(-0.52, 0.15), vec2(-0.08, 0.42)) - 0.07;
    float eyeOuter = length((m - vec2(-0.22, 0.16)) * vec2(1.0, 1.55)) - 0.21;
    float eyePupil = length((m - vec2(-0.22, 0.16)) * vec2(1.2, 1.2)) - 0.085;
    float eyeRing  = max(eyeOuter, -(eyeOuter + 0.07));
    ink = max(ink, smoothstep(0.025, 0.0, min(brow, min(eyeRing, eyePupil))));

    float jawTop    = sdSegment2(m, vec2(-0.02, -0.35), vec2(0.45, -0.05)) - 0.06;
    float jawBot    = sdSegment2(m, vec2( 0.18, -0.58), vec2(0.62, -0.22)) - 0.065;
    float mouthZone = smoothstep(0.30, 0.18, length(m - vec2(0.32, -0.28)));
    float teeth     = smoothstep(0.44, 0.58, sin(m.x * 24.0 + m.y * 8.0));
    ink = max(ink, smoothstep(0.025, 0.0, min(jawTop, jawBot)));
    ink = max(ink, mouthZone * teeth * 0.95);

    float stripe1 = sdSegment2(m, vec2(0.58, -0.55), vec2(0.88, -0.05)) - 0.055;
    float stripe2 = sdSegment2(m, vec2(0.76, -0.52), vec2(1.04, -0.10)) - 0.05;
    ink = max(ink, smoothstep(0.025, 0.0, min(stripe1, stripe2)));

    // 3. Bold ink-black concave inner spine border
    float innerSpine = smoothstep(0.17, 0.12, vRad);
    ink = max(ink, innerSpine);

    return clamp(ink, 0.0, 1.0);
}

// ----------------------------------------------------------------------------
// Lighting Rig (uLightMode: 0=hero, 1=neutral gray, 2=grazing, 3=backlit)
// ----------------------------------------------------------------------------

vec3 getLightDir() {
    if (uLightMode == 2) return normalize(vec3(-0.88, 0.24, 0.42));
    if (uLightMode == 3) return normalize(vec3(-0.18, 0.48, -0.86));
    return normalize(vec3(0.32, 0.86, 0.39));
}

float cel3(float ndl) {
    float b1 = smoothstep(0.00, 0.05, ndl);
    float b2 = smoothstep(0.42, 0.47, ndl);
    return 0.20 + 0.42 * b1 + 0.38 * b2;
}

float cel2(float ndl) {
    return mix(0.30, 1.0, smoothstep(0.08, 0.14, ndl));
}

vec3 applyLightModeTone(vec3 col) {
    if (uLightMode == 1) {
        float l = dot(col, vec3(0.2126, 0.7152, 0.0722));
        return vec3(l);
    }
    return col;
}

// ----------------------------------------------------------------------------
// Surface Shading
// ----------------------------------------------------------------------------

vec3 shadeFloor(vec3 p, vec3 N, vec3 V, vec3 L) {
    vec2 gridUV = p.xz * 1.12;
    vec2 cell   = floor(gridUV);
    vec2 f      = fract(gridUV);

    // Crisp dark ink tile grout lines
    float groutDist = min(min(f.x, 1.0 - f.x), min(f.y, 1.0 - f.y));
    float isGrout   = smoothstep(0.036, 0.015, groutDist);

    // Subtle per-tile matte charcoal variation (linear space)
    float tileVar = (hash21(cell) - 0.5) * 0.0018;

    // Subtle comic ink micro-crack lines
    float nCrack = abs(noise2(p.xz * 4.5 + hash21(cell) * 10.0) - 0.5);
    float scuff  = smoothstep(0.020, 0.007, nCrack) * step(0.66, hash21(cell + 17.3)) * 0.55;

    // Thin crimson tactical arena seam line
    float redSeam = smoothstep(0.032, 0.012, abs(p.z + 0.22)) * smoothstep(3.8, 0.6, abs(p.x));

    vec3 baseCol = max(COL_FLOOR + vec3(tileVar * 0.8, tileVar * 0.9, tileVar * 1.1), COL_GROUT);
    if (uLightMode != 1) {
        baseCol = mix(baseCol, vec3(0.14, 0.004, 0.006), redSeam * 0.55);
    }
    baseCol = mix(baseCol, COL_GROUT, max(isGrout, scuff));

    float ndl = max(dot(N, L), 0.0);
    float floorLit = mix(0.75, 1.08, smoothstep(0.15, 0.65, ndl));
    vec3 col = baseCol * floorLit;

    // Strictly localized cel-stepped crimson light pool directly beneath the cleave
    vec3 qProj = toCleaveLocal(vec3(p.x, 0.78, p.z));
    float uA, vR, dArc;
    evalCleaveArc(qProj.xy, uA, vR, dArc);
    float dPool = max(dArc, 0.0);

    float ph = loopPhase();
    float pulse = 1.0 + 0.12 * sin(ph * TAU);
    float poolInner = smoothstep(0.28 * pulse, 0.23 * pulse, dPool);
    float poolOuter = smoothstep(0.58 * pulse, 0.52 * pulse, dPool);

    // Graphic hard-stepped cast shadow
    float sh = calcGraphicShadow(p + vec3(0.0, 0.01, 0.0), L);

    if (uLightMode != 1) {
        float emitScale = (uLightMode == 3) ? 1.25 : 1.0;
        vec3 spill = vec3(0.032, 0.0010, 0.0018) * poolOuter
                   + vec3(0.085, 0.0025, 0.0035) * poolInner;
        col += spill * emitScale * (1.0 - isGrout * 0.85);
    }

    // Bold ink-black cast shadow on tiles
    col = mix(COL_INK, col, sh);

    // Subtle distance darkening beyond arena rim
    float rFloor = length(p.xz);
    float rimFade = smoothstep(4.5, 6.2, rFloor);
    col = mix(col, vec3(0.0005, 0.00045, 0.0007), rimFade);

    return applyLightModeTone(col);
}

vec3 shadeBlade(vec3 p, vec3 N, vec3 V, vec3 L) {
    vec3 q = toCleaveLocal(p);
    float uArc, vRad, d2d;
    evalCleaveArc(q.xy, uArc, vRad, d2d);

    float ph = loopPhase();
    float ndl = dot(N, L);
    float band = cel3(ndl);

    // Animated energy sweep wave traveling along the blade arc in 3s loop
    float sweepWave = exp(-pow(fract(uArc - ph) - 0.5, 2.0) * 18.0);
    float jagGfx = jaggedEdgeGraphic(uArc);
    // Razor-sharp jagged boundary for the white-hot comic leading edge
    float hotThresh = clamp(0.75 - jagGfx * 0.65 - 0.05 * sweepWave, 0.42, 0.82);

    // 3-band quantized cel shading: deep blood-black -> crimson -> blazing vermilion
    // Also force blood-black along the inner concave trough so all 3 bands always read boldly
    vec3 bladeCol;
    if (band < 0.34 || vRad < 0.25) {
        bladeCol = COL_BLOOD_DARK;
    } else if (band < 0.76 || vRad < 0.42) {
        bladeCol = COL_CRIMSON_MID;
    } else {
        bladeCol = COL_VERMILION;
    }

    // Hand-inked Oni mask & flame markings on the crimson body
    float oniInk = evalOniInkPattern(uArc, vRad);
    bladeCol = mix(bladeCol, COL_INK, oniInk);

    // Crisp black ink separator hairline right behind the jagged white-hot edge
    float edgeLine = smoothstep(0.032, 0.012, abs(vRad - (hotThresh - 0.018)));
    bladeCol = mix(bladeCol, COL_INK, edgeLine * 0.90);

    // Jagged white-hot leading comic edge (hard-stepped)
    float isHotEdge = smoothstep(hotThresh - 0.018, hotThresh + 0.014, vRad);
    float isHotCore = smoothstep(hotThresh + 0.045, hotThresh + 0.085, vRad);

    if (uLightMode == 1) {
        vec3 grayBody = mix(vec3(0.06), vec3(0.32), smoothstep(0.34, 0.76, band));
        grayBody = mix(grayBody, COL_INK, max(oniInk, edgeLine));
        bladeCol = mix(grayBody, vec3(0.68), isHotEdge);
    } else {
        vec3 hotCol = mix(vec3(0.95, 0.09, 0.04), COL_HOT_EDGE, isHotCore);
        if (uLightMode == 3) {
            hotCol *= 1.18;
        }
        bladeCol = mix(bladeCol, hotCol, isHotEdge);
    }

    // Bold black-ink contour outline (Fresnel + radial outer/inner silhouette ink)
    float inkFresnel = smoothstep(0.26, 0.16, abs(dot(N, V)));
    float outerRimInk = smoothstep(0.96, 0.995, vRad) * (1.0 - isHotCore * 0.65);
    float inkOutline = max(inkFresnel * mix(1.0, 0.35, isHotCore), outerRimInk);
    bladeCol = mix(bladeCol, COL_INK, inkOutline);

    return applyLightModeTone(bladeCol);
}

vec3 shadeTsuka(vec3 p, vec3 N, vec3 V, vec3 L) {
    vec3 q = toCleaveLocal(p);
    float openAmt = clamp(uOpen, 0.0, 1.0);
    float R_base  = 1.36 + 0.20 * openAmt;
    float angStart = 1.22;
    vec2 anchor2 = vec2(0.42, -0.04) + (R_base - 0.055) * vec2(cos(angStart), sin(angStart));
    vec2 tang = normalize(vec2(sin(angStart), -cos(angStart)));
    vec2 norm = vec2(-tang.y, tang.x);
    vec3 rel = q - vec3(anchor2, 0.0);
    vec3 h = vec3(dot(rel.xy, tang), dot(rel.xy, norm), rel.z);

    float u = h.x * 26.0;
    float v = atan(h.z, h.y) * 2.0 / PI;
    float d1 = abs(fract(u + v) - 0.5);
    float d2 = abs(fract(u - v) - 0.5);
    float isDiamond = smoothstep(0.42, 0.34, d1 + d2);

    float band = cel2(dot(N, L));
    vec3 cordCol  = mix(COL_INK, vec3(0.012, 0.012, 0.016), band);
    vec3 redPanel = mix(COL_CRIMSON_MID, COL_VERMILION, band);
    vec3 col = mix(cordCol, redPanel, isDiamond);

    float inkOutline = smoothstep(0.26, 0.16, abs(dot(N, V)));
    col = mix(col, COL_INK, inkOutline);

    return applyLightModeTone(col);
}

vec3 shadeGold(vec3 p, vec3 N, vec3 V, vec3 L) {
    float band = cel3(dot(N, L));
    vec3 col;
    if (band < 0.35) {
        col = COL_GOLD_SHAD;
    } else if (band < 0.78) {
        col = mix(COL_GOLD_SHAD, COL_GOLD_LIT, 0.62);
    } else {
        col = COL_GOLD_LIT;
    }

    vec3 H = normalize(L + V);
    float spec = smoothstep(0.86, 0.90, max(dot(N, H), 0.0));
    col = mix(col, vec3(0.95, 0.84, 0.48), spec * 0.65);

    float inkOutline = smoothstep(0.26, 0.16, abs(dot(N, V)));
    col = mix(col, COL_INK, inkOutline);

    return applyLightModeTone(col);
}

vec3 shadeSmoke(vec3 p, vec3 N, vec3 V, vec3 L) {
    float band = cel2(dot(N, L));
    vec3 col = mix(COL_SMOKE_SHAD, COL_SMOKE_LIT, band);

    // Stylized swirling comic interior ink lines
    float swirl = sin(p.x * 7.5 + p.y * 5.0 + sin(p.z * 6.0) * 1.5);
    float interiorLine = smoothstep(0.94, 0.985, abs(swirl)) * smoothstep(0.35, 0.65, abs(dot(N, V)));
    col = mix(col, COL_INK, interiorLine * 0.90);

    // Subtle warm crimson bounce tint on underside facing the cleave
    if (uLightMode != 1) {
        float bounce = smoothstep(0.18, -0.35, N.y) * 0.28;
        col = mix(col, vec3(0.45, 0.04, 0.04), bounce);
    }

    float inkOutline = smoothstep(0.28, 0.17, abs(dot(N, V)));
    col = mix(col, COL_INK, inkOutline);

    return applyLightModeTone(col);
}

vec3 shadeDiamond(vec3 p, vec3 N, vec3 V, vec3 L) {
    float facet = step(0.10, dot(N, L));
    vec3 col;
    if (uLightMode == 1) {
        col = mix(vec3(0.14), vec3(0.52), facet);
    } else {
        col = mix(vec3(0.68, 0.008, 0.015), vec3(1.08, 0.065, 0.065), facet);
    }

    float inkOutline = smoothstep(0.24, 0.14, abs(dot(N, V)));
    col = mix(col, COL_INK, inkOutline * 0.78);

    return applyLightModeTone(col);
}

vec3 shadeRim(vec3 p, vec3 N, vec3 V, vec3 L) {
    float band = cel2(dot(N, L));
    vec3 col = mix(vec3(0.0018, 0.0020, 0.0032), vec3(0.009, 0.011, 0.016), band);
    float redTop = smoothstep(0.12, 0.145, p.y);
    if (uLightMode != 1) {
        col = mix(col, vec3(0.42, 0.008, 0.014), redTop);
    }
    float inkOutline = smoothstep(0.26, 0.16, abs(dot(N, V)));
    col = mix(col, COL_INK, inkOutline);
    return applyLightModeTone(col);
}

// ----------------------------------------------------------------------------
// Filmic Stylized Tone Mapping (0% NaN, <1% clipped, <2% black)
// ----------------------------------------------------------------------------

vec3 stylizedToneMap(vec3 col) {
    col = max(col, vec3(0.0));
    col = col / (1.0 + col * 0.16);
    col = min(col, vec3(0.985));
    // Encode linear -> sRGB
    col = pow(col, vec3(1.0 / 2.2));
    // Subtle dark ink floor in sRGB (~0.016) so outlines stay rich ink without pure 0.0
    col = max(col, vec3(0.015, 0.014, 0.018));
    return clamp(col, 0.0, 0.992);
}

// ----------------------------------------------------------------------------
// Main Entry Point
// ----------------------------------------------------------------------------

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / max(uResolution.y, 1.0);

    // Camera setup (uCamMode: 0 = hero 3/4 action view, 1 = ~2.5x macro close-up)
    vec3 ro, ta;
    float focal;
    if (uCamMode == 1) {
        ro    = vec3(-0.06, 1.62, 1.48);
        ta    = vec3(-0.22, 0.78, -0.02);
        focal = 2.40;
    } else {
        ro    = vec3(0.06, 3.05, 3.25);
        ta    = vec3(-0.04, 0.48, 0.02);
        focal = 1.96;
    }

    vec3 cw = normalize(ta - ro);
    vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
    vec3 cv = cross(cu, cw);
    vec3 rd = normalize(uv.x * cu + uv.y * cv + focal * cw);

    // Tight-stepped raymarch (0.45x safety factor eliminates all overstepping/slicing)
    float t = 0.0;
    float matId = -1.0;
    float minBladeDist = 1e5;

    for (int i = 0; i < 128; i++) {
        vec3 p = ro + rd * t;
        vec2 h = map(p);
        if (h.y == MAT_BLADE || h.y == MAT_DIAMOND) {
            minBladeDist = min(minBladeDist, max(h.x, 0.0));
        }
        if (h.x < 0.0015) {
            matId = h.y;
            break;
        }
        t += clamp(h.x * 0.45, 0.003, 0.22);
        if (t > 14.0) break;
    }

    // Stylized dark matte arena backdrop (linear space)
    vec3 col = vec3(0.0006, 0.0005, 0.0009);

    if (matId > 0.0) {
        vec3 p = ro + rd * t;
        vec3 N = (matId == MAT_FLOOR) ? vec3(0.0, 1.0, 0.0) : calcNormal(p);
        vec3 V = -rd;
        vec3 L = getLightDir();

        if (matId == MAT_FLOOR)        col = shadeFloor(p, N, V, L);
        else if (matId == MAT_BLADE)   col = shadeBlade(p, N, V, L);
        else if (matId == MAT_TSUKA)   col = shadeTsuka(p, N, V, L);
        else if (matId == MAT_GOLD)    col = shadeGold(p, N, V, L);
        else if (matId == MAT_SMOKE)   col = shadeSmoke(p, N, V, L);
        else if (matId == MAT_DIAMOND) col = shadeDiamond(p, N, V, L);
        else if (matId == MAT_RIM)     col = shadeRim(p, N, V, L);
    }

    // Tight, localized crimson edge aura right next to the blade/diamonds (zero pink haze!)
    if (uLightMode != 1) {
        float emitScale = (uLightMode == 3) ? 1.25 : 0.90;
        float tightAura = exp(-minBladeDist * 28.0) * smoothstep(0.18, 0.0, minBladeDist);
        col += vec3(0.45, 0.006, 0.010) * tightAura * emitScale;
    } else {
        col = applyLightModeTone(col);
    }

    // Subtle graphic vignette framing
    float vig = 1.0 - 0.14 * dot(uv, uv);
    col *= clamp(vig, 0.76, 1.0);

    fragColor = vec4(stylizedToneMap(col), 1.0);
}

Mathematical models & shader specifications

Identifiercrimson-oni-blade-cleave
CategoryGame VFX Shaders · Stylized Cel VFX
Upstream studyvfx_stylized/lib/01_crimson_oni_blade_cleave.glsl (commit f5bfb61)
Animation cycle3.0 s seamless harmonic loop
Declared uniformsuResolution, uTime, uOpen, uLightMode, uCamMode
Source size781 lines · 29.2 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT melee sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Schlick Fresnel reflectance (1994); Beer–Lambert volumetric absorption/attenuation

Adapted upstream mathematical helpers

Inigo Quilez, distance formulation · Inigo Quilez, quadratic distance blend · Inigo Quilez, tetrahedral SDF normal

All game vfx shaders