Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

9.8 KB
Raw Blame
#pragma shader "Batch"
#pragma version 300 es

// Compile-time variant: when enabled the fragment stage treats the sampled
// alpha as a signed distance field (runtime glyph atlas) and
// derives crisp, resolution-independent coverage instead of sampling RGBA.
#pragma feature SDF

// Compile-time variants for a glyph page whose pixels are not a plain RGBA to tint (see TextGlyphMode). The tint comes from the vertices;
// the page only says how much of it each pixel covers.
//   GLYPH_MASK  coverage is the page's alpha and its colour is ignored: the silhouette of a glyph drawn with an outline baked into the page.
//   GLYPH_FILL  coverage is red x alpha: the glyph proper of that same page, where the outline ring is the part alpha covers and red does not.
// Drawn together, ring first, they are the outlined glyph with the ring in any colour, which a pre-coloured page cannot be.
#pragma feature GLYPH_MASK
#pragma feature GLYPH_FILL

// Compile-time variant: lit by the scene's 2D lights (Sprite.lit). Compiled as a
// Lit-domain shader, so applyLighting2D + LightConstants are injected.
#pragma feature LIT

// Compile-time variant: discard fragments the sprite draws as (near-)transparent.
// Selected for a STENCIL MASK draw, which runs this shader with color writes off:
// without the discard every transparent corner of the quad still writes stencil,
// so a circular mask sprite would clip a rectangle. Off for ordinary sprites, so
// they keep an unconditional (early-Z friendly) fragment path.
#pragma feature ALPHA_CLIP

// The vertex tint arrives straight; multiply coverage after color conversion.
#pragma feature PREMULTIPLIED_COLOR

#pragma vertex
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec4 a_color;
layout(location = 2) in vec2 a_texCoord;
layout(location = 3) in float a_texIndex;
// The batch layout's last lane: how far to grow a text glyph, and for a lit sprite the layer it is on (plus 129; 0 = none), which a
// light's range of layers is tested against.
#if defined(SDF) || defined(LIT)
layout(location = 4) in float a_sdfBias;
#endif

out vec4 v_color;
out vec2 v_texCoord;
flat out int v_texIndex;
#ifdef SDF
out float v_sdfBias;
#endif
#ifdef LIT
out highp vec2 v_worldPos;
flat out highp float v_lightLayer;
#endif

void main() {
    gl_Position = u_projection * vec4(a_position, 1.0);
    v_color = a_color;
    v_texCoord = a_texCoord;
    v_texIndex = int(a_texIndex);
#ifdef SDF
    v_sdfBias = a_sdfBias;
#endif
#ifdef LIT
    v_worldPos = a_position.xy;
    v_lightLayer = a_sdfBias;
#endif
}
#pragma end

#pragma fragment
// highp: the SDF text branch needs precise distance + fwidth derivatives, and
// sprite sampling is unaffected by the wider range.
precision highp float;

in vec4 v_color;
in vec2 v_texCoord;
flat in int v_texIndex;
#ifdef SDF
in float v_sdfBias;
#endif
#ifdef LIT
in highp vec2 v_worldPos;
flat in highp float v_lightLayer;
#endif

// Up to 8 textures bound per multi-texture batch. GLSL ES 3.00 forbids indexing a
// sampler array with a non-uniform expression, so the slot is selected by a constant
// branch chain (the standard WebGL2 multi-texture batching technique).
uniform sampler2D u_textures[8];

out vec4 fragColor;

void main() {
    vec4 texColor;
    if (v_texIndex == 0) texColor = texture(u_textures[0], v_texCoord);
    else if (v_texIndex == 1) texColor = texture(u_textures[1], v_texCoord);
    else if (v_texIndex == 2) texColor = texture(u_textures[2], v_texCoord);
    else if (v_texIndex == 3) texColor = texture(u_textures[3], v_texCoord);
    else if (v_texIndex == 4) texColor = texture(u_textures[4], v_texCoord);
    else if (v_texIndex == 5) texColor = texture(u_textures[5], v_texCoord);
    else if (v_texIndex == 6) texColor = texture(u_textures[6], v_texCoord);
    else texColor = texture(u_textures[7], v_texCoord);
#ifdef ES_LINEAR
    // Vertex colors are authored sRGB; the sampled texel is already linear
    // (sRGB texture formats decode in hardware). Alpha is coverage — never encoded.
    vec4 tint = vec4(srgbToLinear(v_color.rgb), v_color.a);
#else
    vec4 tint = v_color;
#endif
#ifdef PREMULTIPLIED_COLOR
    tint.rgb *= tint.a;
#endif
#ifdef SDF
    // The glyph atlas stores a signed distance in the alpha channel (RGB = 1).
    // Distance ÷ fwidth = screen px from the edge; the ±0.5px clamp is exactly
    // one pixel of linear coverage ramp (the msdfgen standard).
    // v_sdfBias moves the edge outward before the ramp, which grows the glyph's
    // own shape by that many distance units — an outline pass draws the same
    // quads with it raised and its colour in the vertices.
    float dist = texColor.a;
    float screenPxDist = (dist - 0.5 + v_sdfBias) / max(fwidth(dist), 1e-6);
    float coverage = clamp(screenPxDist + 0.5, 0.0, 1.0);
    fragColor = vec4(tint.rgb, tint.a * coverage);
#elif defined(GLYPH_MASK)
    fragColor = vec4(tint.rgb, tint.a * texColor.a);
#elif defined(GLYPH_FILL)
    fragColor = vec4(tint.rgb, tint.a * texColor.r * texColor.a);
#elif defined(LIT)
    // Flat normal; the tinted color is the albedo. Normal maps need a material.
    vec4 base = texColor * tint;
    fragColor = vec4(applyLighting2DLayer(base.rgb, vec3(0.0, 0.0, 1.0), v_worldPos, v_lightLayer), base.a);
#else
  #ifdef ALPHA_CLIP
    // The SPRITE's alpha, not the tinted result: a mask graphic is routinely
    // tinted to near-zero alpha so it masks without being seen, and testing the
    // tinted value would discard the whole mask and clip everything away.
    if (texColor.a < 0.5) discard;
  #endif
    fragColor = texColor * tint;
#endif
}
#pragma end

#pragma vertex wgsl
struct VSIn {
    @location(0) a_position : vec3f,
    @location(1) a_color : vec4f,
    @location(2) a_texCoord : vec2f,
    @location(3) a_texIndex : f32,
#ifdef SDF
    @location(4) a_sdfBias : f32,
#elif defined(LIT)
    @location(4) a_sdfBias : f32,
#endif
};
struct VSOut {
    @builtin(position) pos : vec4f,
    @location(0) v_color : vec4f,
    @location(1) v_texCoord : vec2f,
    @location(2) v_texIndex : f32,
#ifdef LIT
    @location(3) v_worldPos : vec2f,
    @location(5) @interpolate(flat) v_lightLayer : f32,
#endif
#ifdef SDF
    @location(4) v_sdfBias : f32,
#endif
};

@vertex fn vs_main(v : VSIn) -> VSOut {
    var out : VSOut;
    out.pos = frame.projection * vec4f(v.a_position, 1.0);
    out.v_color = v.a_color;
    out.v_texCoord = v.a_texCoord;
    out.v_texIndex = v.a_texIndex;
#ifdef SDF
    out.v_sdfBias = v.a_sdfBias;
#endif
#ifdef LIT
    out.v_worldPos = v.a_position.xy;
    out.v_lightLayer = v.a_sdfBias;
#endif
    return out;
}
#pragma end

#pragma fragment wgsl
@group(1) @binding(0) var t0 : texture_2d<f32>;
@group(1) @binding(1) var t1 : texture_2d<f32>;
@group(1) @binding(2) var t2 : texture_2d<f32>;
@group(1) @binding(3) var t3 : texture_2d<f32>;
@group(1) @binding(4) var t4 : texture_2d<f32>;
@group(1) @binding(5) var t5 : texture_2d<f32>;
@group(1) @binding(6) var t6 : texture_2d<f32>;
@group(1) @binding(7) var t7 : texture_2d<f32>;
@group(1) @binding(8) var s0 : sampler;
@group(1) @binding(9) var s1 : sampler;
@group(1) @binding(10) var s2 : sampler;
@group(1) @binding(11) var s3 : sampler;
@group(1) @binding(12) var s4 : sampler;
@group(1) @binding(13) var s5 : sampler;
@group(1) @binding(14) var s6 : sampler;
@group(1) @binding(15) var s7 : sampler;

struct VSOut {
    @builtin(position) pos : vec4f,
    @location(0) v_color : vec4f,
    @location(1) v_texCoord : vec2f,
    @location(2) v_texIndex : f32,
#ifdef LIT
    @location(3) v_worldPos : vec2f,
    @location(5) @interpolate(flat) v_lightLayer : f32,
#endif
#ifdef SDF
    @location(4) v_sdfBias : f32,
#endif
};

// The gradients are taken before the branch chain, where every fragment of the quad runs, and each branch samples through the mip
// chain with them, as the GLSL twin's texture() does: a sprite drawn smaller than its texture reads the level that fits instead of
// shimmering from level 0, and one drawn at its size reads level 0 as before.
@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
    let idx = i32(v.v_texIndex + 0.5);
    let duvx = dpdx(v.v_texCoord);
    let duvy = dpdy(v.v_texCoord);
    var texColor : vec4f;
    if (idx == 0) { texColor = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy); }
    else if (idx == 1) { texColor = textureSampleGrad(t1, s1, v.v_texCoord, duvx, duvy); }
    else if (idx == 2) { texColor = textureSampleGrad(t2, s2, v.v_texCoord, duvx, duvy); }
    else if (idx == 3) { texColor = textureSampleGrad(t3, s3, v.v_texCoord, duvx, duvy); }
    else if (idx == 4) { texColor = textureSampleGrad(t4, s4, v.v_texCoord, duvx, duvy); }
    else if (idx == 5) { texColor = textureSampleGrad(t5, s5, v.v_texCoord, duvx, duvy); }
    else if (idx == 6) { texColor = textureSampleGrad(t6, s6, v.v_texCoord, duvx, duvy); }
    else { texColor = textureSampleGrad(t7, s7, v.v_texCoord, duvx, duvy); }
#ifdef ES_LINEAR
    var tint = vec4f(srgbToLinear(v.v_color.rgb), v.v_color.a);
#else
    var tint = v.v_color;
#endif
#ifdef PREMULTIPLIED_COLOR
    tint = vec4f(tint.rgb * tint.a, tint.a);
#endif
#ifdef SDF
    // Same coverage math as the GLSL stage; fwidth here is in uniform control
    // flow (main scope), which WGSL requires of derivative builtins.
    let dist = texColor.a;
    let screenPxDist = (dist - 0.5 + v.v_sdfBias) / max(fwidth(dist), 1e-6);
    let coverage = clamp(screenPxDist + 0.5, 0.0, 1.0);
    return vec4f(tint.rgb, tint.a * coverage);
#elif defined(GLYPH_MASK)
    return vec4f(tint.rgb, tint.a * texColor.a);
#elif defined(GLYPH_FILL)
    return vec4f(tint.rgb, tint.a * texColor.r * texColor.a);
#elif defined(LIT)
    let base = texColor * tint;
    return vec4f(applyLighting2DLayer(base.rgb, vec3f(0.0, 0.0, 1.0), v.v_worldPos, v.v_lightLayer), base.a);
#else
  #ifdef ALPHA_CLIP
    // See the GLSL stage: test the sprite's alpha, not the tinted result.
    if (texColor.a < 0.5) { discard; }
  #endif
    return texColor * tint;
#endif
}
#pragma end