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.

1.8 KB
Raw Blame
#pragma shader "Shadow2D"
#pragma version 300 es

// The 2D shadow mask: one channel per light that casts, drawn as the region each
// occluder's silhouette hides from it. The pass ADDS, which is what makes a
// source with width soft: its silhouette is drawn once per sample across the
// source, and a fragment collects a share for every sample hidden from it.
//
// `a_shadow` is the share this piece of geometry hides. `a_channel` is the
// light's own channel as a one-hot vector, which lets every light ride in one
// draw: the channels a vertex does not name receive zero.

#pragma vertex
layout(location = 0) in vec2 a_position;
layout(location = 1) in float a_shadow;
layout(location = 2) in vec4 a_channel;

out float v_shadow;
out vec4 v_channel;

void main() {
    gl_Position = u_projection * vec4(a_position, 0.0, 1.0);
    v_shadow = a_shadow;
    v_channel = a_channel;
}
#pragma end

#pragma fragment
precision highp float;

in float v_shadow;
in vec4 v_channel;

out vec4 fragColor;

void main() {
    fragColor = v_channel * v_shadow;
}
#pragma end

#pragma vertex wgsl
struct VSIn {
    @location(0) a_position : vec2f,
    @location(1) a_shadow : f32,
    @location(2) a_channel : vec4f,
};
struct VSOut {
    @builtin(position) pos : vec4f,
    @location(0) v_shadow : f32,
    @location(1) v_channel : vec4f,
};

@vertex fn vs_main(v : VSIn) -> VSOut {
    var out : VSOut;
    out.pos = frame.projection * vec4f(v.a_position, 0.0, 1.0);
    out.v_shadow = v.a_shadow;
    out.v_channel = v.a_channel;
    return out;
}
#pragma end

#pragma fragment wgsl
struct VSOut {
    @builtin(position) pos : vec4f,
    @location(0) v_shadow : f32,
    @location(1) v_channel : vec4f,
};

@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
    return v.v_channel * v.v_shadow;
}
#pragma end