330 lines
15 KiB
WebGPU Shading Language
330 lines
15 KiB
WebGPU Shading Language
// Terrain, sky and outline shaders, ported from the pinned vanilla
|
|
// terrain.vsh/fsh, fog.glsl and sample_lightmap.glsl.
|
|
struct Globals {
|
|
view_proj: mat4x4<f32>,
|
|
fog_color: vec4<f32>,
|
|
// environmental start/end, render-distance start/end
|
|
fog: vec4<f32>,
|
|
sky_color: vec4<f32>,
|
|
// x: sky darkening, y: game time, z: aspect, w: tan(fov/2)
|
|
misc: vec4<f32>,
|
|
inverse_view: mat4x4<f32>,
|
|
sunrise: vec4<f32>,
|
|
// xyz: sun direction, w: star brightness
|
|
sun: vec4<f32>,
|
|
// x: sky fog end, y: cloud fog end, z: dark disc flag
|
|
fog_ends: vec4<f32>,
|
|
// Atlas rects (u0, v0, width, height) of end_sky.png and end_portal.png.
|
|
end_portal_sprites: array<vec4<f32>, 2>,
|
|
// x: vanilla's GameTime ((game time % 24000 + partial tick) / 24000), z: how many
|
|
// dynamic_lights are set
|
|
time: vec4<f32>,
|
|
// Atlas rect of the current destroy stage (read by entity.wgsl).
|
|
crumbling_sprite: vec4<f32>,
|
|
outline_params: vec4<f32>,
|
|
// Horizon Extras' dynamic lights: camera-relative position and level (dynamic_light.rs).
|
|
dynamic_lights: array<vec4<f32>, 16>,
|
|
}
|
|
@group(0) @binding(0) var<uniform> globals: Globals;
|
|
@group(0) @binding(1) var sprites: texture_2d_array<f32>;
|
|
@group(0) @binding(2) var sprite_sampler: sampler;
|
|
@group(0) @binding(3) var lightmap: texture_2d<f32>;
|
|
@group(0) @binding(4) var lightmap_sampler: sampler;
|
|
// Layers past the first array's SPRITE_SPLIT (2048, gfx.rs): mods' sprites.
|
|
@group(0) @binding(5) var sprites_1: texture_2d_array<f32>;
|
|
@group(0) @binding(6) var sprites_2: texture_2d_array<f32>;
|
|
@group(0) @binding(7) var sprites_3: texture_2d_array<f32>;
|
|
@group(0) @binding(8) var sprites_4: texture_2d_array<f32>;
|
|
@group(0) @binding(9) var sprites_5: texture_2d_array<f32>;
|
|
@group(0) @binding(10) var sprites_6: texture_2d_array<f32>;
|
|
@group(0) @binding(11) var sprites_7: texture_2d_array<f32>;
|
|
// How many sprite arrays hold layers (a pipeline constant): 1 for vanilla,
|
|
// which then samples exactly as before.
|
|
override SPRITE_ARRAYS: u32 = 1u;
|
|
override SPRITE_SPLIT: u32 = 2048u;
|
|
fn sprite(uv: vec2<f32>, layer: u32) -> vec4<f32> {
|
|
if SPRITE_ARRAYS == 1u {
|
|
return textureSample(sprites, sprite_sampler, uv, i32(layer));
|
|
}
|
|
// The array is chosen per fragment: sample with the derivatives taken here.
|
|
let dx = dpdx(uv);
|
|
let dy = dpdy(uv);
|
|
let l = i32(layer % SPRITE_SPLIT);
|
|
switch layer / SPRITE_SPLIT {
|
|
case 0u: { return textureSampleGrad(sprites, sprite_sampler, uv, l, dx, dy); }
|
|
case 1u: { return textureSampleGrad(sprites_1, sprite_sampler, uv, l, dx, dy); }
|
|
case 2u: { return textureSampleGrad(sprites_2, sprite_sampler, uv, l, dx, dy); }
|
|
case 3u: { return textureSampleGrad(sprites_3, sprite_sampler, uv, l, dx, dy); }
|
|
case 4u: { return textureSampleGrad(sprites_4, sprite_sampler, uv, l, dx, dy); }
|
|
case 5u: { return textureSampleGrad(sprites_5, sprite_sampler, uv, l, dx, dy); }
|
|
case 6u: { return textureSampleGrad(sprites_6, sprite_sampler, uv, l, dx, dy); }
|
|
default: { return textureSampleGrad(sprites_7, sprite_sampler, uv, l, dx, dy); }
|
|
}
|
|
}
|
|
|
|
struct TerrainOut {
|
|
@builtin(position) clip: vec4<f32>,
|
|
@location(0) uv: vec2<f32>,
|
|
@location(1) @interpolate(flat) layer: u32,
|
|
@location(2) color: vec4<f32>,
|
|
@location(3) spherical: f32,
|
|
@location(4) cylindrical: f32,
|
|
// The light the face emits, 0 to 15 (mesher.rs's `EMISSION_BITS`).
|
|
@location(5) @interpolate(flat) emission: u32,
|
|
}
|
|
// Horizon Extras' dynamic lights (dynamic_light.rs): the block light, in the vertex's units
|
|
// (16 per level), a vertex at camera-relative `pos` gets from the frame's light sources, one
|
|
// level less per block. time.z of them are set: none when the extra is off, so no loop runs.
|
|
fn dynamic_block_light(pos: vec3<f32>) -> f32 {
|
|
let count = min(u32(globals.time.z), 16u);
|
|
var level = 0.0;
|
|
for (var i = 0u; i < count; i++) {
|
|
let l = globals.dynamic_lights[i];
|
|
level = max(level, l.w - distance(pos, l.xyz));
|
|
}
|
|
return level * 16.0;
|
|
}
|
|
fn sample_lightmap(sky: f32, block: f32) -> vec4<f32> {
|
|
let uv = vec2(block, sky) * 16.0;
|
|
return textureSampleLevel(lightmap, lightmap_sampler,
|
|
clamp(uv / 256.0 + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0);
|
|
}
|
|
// mesher.rs's TerrainVertex holds the UV in fixed point; the instance's
|
|
// origin.w is the step: 1/32768 per unit for sections (a power of two on an
|
|
// integer below 2^24, so it decodes exactly), 1/32 per block for distant
|
|
// terrain tiles (TerrainVertex::tiled).
|
|
fn terrain_uv(packed: vec2<u32>, step: f32) -> vec2<f32> {
|
|
return vec2<f32>(packed) * step;
|
|
}
|
|
@vertex fn vs_terrain(
|
|
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
|
|
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>,
|
|
@location(5) origin: vec4<f32>) -> TerrainOut {
|
|
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz);
|
|
}
|
|
// Flat section draws (gfx/flat.rs): one draw per arena page over the frame's
|
|
// index list. A section's vertices fill whole 256-vertex blocks of its page:
|
|
// the block gives its resident slot, the slot its camera-relative origin.
|
|
@group(1) @binding(0) var<storage, read> block_slots: array<u32>;
|
|
@group(1) @binding(1) var<storage, read> slot_origins: array<vec4<f32>>;
|
|
@vertex fn vs_terrain_flat(
|
|
@builtin(vertex_index) vertex: u32,
|
|
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
|
|
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>
|
|
) -> TerrainOut {
|
|
let origin = slot_origins[block_slots[vertex >> 8u]];
|
|
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz);
|
|
}
|
|
fn terrain_vertex(position: vec3<f32>, uv: vec2<f32>, layer: u32, color: vec4<f32>,
|
|
light: vec2<u32>, origin: vec3<f32>) -> TerrainOut {
|
|
let pos = position + origin;
|
|
var out: TerrainOut;
|
|
out.clip = globals.view_proj * vec4(pos, 1.0);
|
|
out.uv = uv;
|
|
out.layer = layer;
|
|
let block = max(f32(light.y), dynamic_block_light(pos));
|
|
out.color = vec4(color.rgb, 1.0) * sample_lightmap(f32(light.x) / 16.0, block / 16.0);
|
|
out.emission = 15u - (u32(round(color.a * 255.0)) & 15u);
|
|
out.spherical = length(pos);
|
|
out.cylindrical = max(length(pos.xz), abs(pos.y));
|
|
return out;
|
|
}
|
|
fn linear_fog(d: f32, start: f32, end: f32) -> f32 {
|
|
if d <= start { return 0.0; }
|
|
if d >= end { return 1.0; }
|
|
return (d - start) / (end - start);
|
|
}
|
|
fn apply_fog(color: vec4<f32>, spherical: f32, cylindrical: f32) -> vec4<f32> {
|
|
let f = max(linear_fog(spherical, globals.fog.x, globals.fog.y),
|
|
linear_fog(cylindrical, globals.fog.z, globals.fog.w));
|
|
return vec4(mix(color.rgb, globals.fog_color.rgb, f * globals.fog_color.a), color.a);
|
|
}
|
|
@fragment fn fs_solid(in: TerrainOut) -> @location(0) vec4<f32> {
|
|
// Bit 15 of the layer marks opaque leaves (forceOpaque): no alpha cutout.
|
|
let color = sprite(in.uv, in.layer & 0x7fffu) * in.color;
|
|
if color.a < 0.5 && (in.layer & 0x8000u) == 0u { discard; }
|
|
// Alpha 0 tells HDR output (hdr.wgsl) a light-emitting block's bright pixels
|
|
// may shine. Nothing else reads the frame's alpha.
|
|
let emissive = select(1.0, 0.0, in.emission > 0u);
|
|
return apply_fog(vec4(color.rgb, emissive), in.spherical, in.cylindrical);
|
|
}
|
|
@fragment fn fs_translucent(in: TerrainOut) -> @location(0) vec4<f32> {
|
|
let color = sprite(in.uv, in.layer & 0x7fffu) * in.color;
|
|
if color.a < 0.01 { discard; }
|
|
return apply_fog(color, in.spherical, in.cylindrical);
|
|
}
|
|
|
|
// Sky (SkyRenderer): the 10-vertex sky disc fan at y = 16 (radius 512) in
|
|
// the sky colour with sky fog (sky.fsh: fog 0..FogSkyEnd on the spherical
|
|
// distance, a step at FogSkyEnd on the cylindrical one), evaluated per pixel
|
|
// along the view ray with the fan's interpolated vertex distances; the
|
|
// clear colour (fog colour) elsewhere; the sunrise/sunset fan blended over
|
|
// it; the dark disc (y = -4) below the horizon when the camera is low.
|
|
struct SkyOut {
|
|
@builtin(position) clip: vec4<f32>,
|
|
@location(0) ndc: vec2<f32>,
|
|
}
|
|
@vertex fn vs_sky(@builtin(vertex_index) i: u32) -> SkyOut {
|
|
let p = vec2(f32(i32(i & 1u) * 4 - 1), f32(i32(i >> 1u) * 4 - 1));
|
|
var out: SkyOut;
|
|
out.clip = vec4(p, 0.0, 1.0);
|
|
out.ndc = p;
|
|
return out;
|
|
}
|
|
fn view_ray(ndc: vec2<f32>) -> vec3<f32> {
|
|
return normalize((globals.inverse_view * vec4(ndc.x * globals.misc.z * globals.misc.w,
|
|
ndc.y * globals.misc.w, -1.0, 0.0)).xyz);
|
|
}
|
|
const PI: f32 = 3.14159265;
|
|
// Interpolated (spherical, cylindrical) distance where the ray meets a disc
|
|
// fan at height y, or negative when it misses.
|
|
fn disc_distance(d: vec3<f32>, y: f32) -> vec2<f32> {
|
|
if d.y * y <= 0.0 { return vec2(-1.0); }
|
|
let p = d * (y / d.y);
|
|
let h = length(p.xz);
|
|
let phi = atan2(p.z, p.x);
|
|
let sector = PI / 4.0;
|
|
let delta = phi - (floor(phi / sector) * sector + sector * 0.5);
|
|
let edge = 512.0 * cos(sector * 0.5) / cos(delta);
|
|
if h > edge { return vec2(-1.0); }
|
|
let s = h / edge;
|
|
let ay = abs(y);
|
|
let rim = sqrt(512.0 * 512.0 + y * y);
|
|
return vec2(ay + s * (rim - ay), ay + s * (512.0 - ay));
|
|
}
|
|
fn sky_fog(color: vec3<f32>, dist: vec2<f32>) -> vec3<f32> {
|
|
let end = globals.fog_ends.x;
|
|
let f = max(linear_fog(dist.x, 0.0, end), linear_fog(dist.y, end, end));
|
|
return mix(color, globals.fog_color.rgb, f * globals.fog_color.a);
|
|
}
|
|
fn rotate_x90(v: vec3<f32>) -> vec3<f32> { return vec3(v.x, -v.z, v.y); }
|
|
// Weight of the fan centre where the ray crosses the sunrise fan (0 when missed).
|
|
fn sunrise_weight(d: vec3<f32>) -> f32 {
|
|
let alpha = globals.sunrise.a;
|
|
if alpha <= 0.001 { return 0.0; }
|
|
// sin(sunAngle) < 0 flips the fan to the other horizon; sun.z = -sin(sunAngle).
|
|
let flip = select(0.0, 180.0, -globals.sun.z < 0.0);
|
|
let theta = radians(flip + 90.0);
|
|
let c = cos(theta);
|
|
let s = sin(theta);
|
|
var best = 0.0;
|
|
let apex = rotate_x90(vec3(-100.0 * s, 100.0 * c, 0.0));
|
|
for (var i = 0; i < 16; i++) {
|
|
let a0 = f32(i) * 2.0 * PI / 16.0;
|
|
let a1 = f32(i + 1) * 2.0 * PI / 16.0;
|
|
let l0 = vec3(sin(a0) * 120.0, cos(a0) * 120.0, -cos(a0) * 40.0 * alpha);
|
|
let l1 = vec3(sin(a1) * 120.0, cos(a1) * 120.0, -cos(a1) * 40.0 * alpha);
|
|
let r0 = rotate_x90(vec3(l0.x * c - l0.y * s, l0.x * s + l0.y * c, l0.z));
|
|
let r1 = rotate_x90(vec3(l1.x * c - l1.y * s, l1.x * s + l1.y * c, l1.z));
|
|
// Moller-Trumbore from the origin along d.
|
|
let e1 = r0 - apex;
|
|
let e2 = r1 - apex;
|
|
let pv = cross(d, e2);
|
|
let det = dot(e1, pv);
|
|
if abs(det) < 1e-6 { continue; }
|
|
let tv = -apex;
|
|
let u = dot(tv, pv) / det;
|
|
let qv = cross(tv, e1);
|
|
let v = dot(d, qv) / det;
|
|
let t = dot(e2, qv) / det;
|
|
if u >= 0.0 && v >= 0.0 && u + v <= 1.0 && t > 0.0 {
|
|
best = max(best, 1.0 - u - v);
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
// The sky's colour along the view ray `d` (a shader pack's sky stage gets it as its input:
|
|
// gfx/native_sky.rs builds that stage from this section).
|
|
fn sky_along_ray(d: vec3<f32>) -> vec3<f32> {
|
|
var color = globals.fog_color.rgb;
|
|
let dist = disc_distance(d, 16.0);
|
|
if dist.x >= 0.0 {
|
|
color = sky_fog(globals.sky_color.rgb, dist);
|
|
}
|
|
let w = sunrise_weight(d);
|
|
if w > 0.0 {
|
|
color = mix(color, globals.sunrise.rgb, w * globals.sunrise.a);
|
|
}
|
|
return color;
|
|
}
|
|
@fragment fn fs_sky(in: SkyOut) -> @location(0) vec4<f32> {
|
|
return vec4(sky_along_ray(view_ray(in.ndc)), 1.0);
|
|
}
|
|
@fragment fn fs_dark_disc(in: SkyOut) -> @location(0) vec4<f32> {
|
|
let d = view_ray(in.ndc);
|
|
let dist = disc_distance(d, -4.0);
|
|
if dist.x < 0.0 { discard; }
|
|
return vec4(sky_fog(vec3(0.0), dist), 1.0);
|
|
}
|
|
|
|
// rendertype_lines.vsh: screen-space stroke and camera-space depth separation.
|
|
fn outline_clip(vertex: u32, start: vec3<f32>, end: vec3<f32>, width: f32) -> vec4<f32> {
|
|
let position = select(start, end, vertex >= 2u);
|
|
let normal = normalize(end - start);
|
|
let view_shrink = 1.0 - 1.0 / 256.0;
|
|
let line_start = globals.view_proj * vec4((position * globals.outline_params.w) * view_shrink, 1.0);
|
|
let line_end = globals.view_proj * vec4(((position + normal) * globals.outline_params.w) * view_shrink, 1.0);
|
|
let ndc1 = line_start.xyz / line_start.w;
|
|
let ndc2 = line_end.xyz / line_end.w;
|
|
let screen = globals.outline_params.xy;
|
|
let direction = normalize((ndc2.xy - ndc1.xy) * screen);
|
|
var offset = vec2(-direction.y, direction.x) * width / screen;
|
|
if offset.x < 0.0 { offset = -offset; }
|
|
if vertex % 2u != 0u { offset = -offset; }
|
|
return vec4((ndc1 + vec3(offset, 0.0)) * line_start.w, line_start.w);
|
|
}
|
|
struct LineOut {
|
|
@builtin(position) clip: vec4<f32>,
|
|
@location(0) spherical: f32,
|
|
@location(1) cylindrical: f32,
|
|
@location(2) color: vec4<f32>,
|
|
}
|
|
@vertex fn vs_line(@builtin(vertex_index) vertex: u32,
|
|
@location(0) start: vec3<f32>, @location(1) end: vec3<f32>,
|
|
@location(2) argb: vec4<f32>, @location(3) width: f32) -> LineOut {
|
|
let position = select(start, end, vertex >= 2u);
|
|
var out: LineOut;
|
|
out.clip = outline_clip(vertex, start, end, width);
|
|
out.spherical = length(position);
|
|
out.cylindrical = max(length(position.xz), abs(position.y));
|
|
// ARGB in a little-endian u32 reads as B, G, R, A.
|
|
out.color = argb.zyxw;
|
|
return out;
|
|
}
|
|
@fragment fn fs_line(in: LineOut) -> @location(0) vec4<f32> {
|
|
return apply_fog(in.color, in.spherical, in.cylindrical);
|
|
}
|
|
// Gizmo lines (rendertype_lines with a per-vertex colour and LineWidth in
|
|
// pixels; no layering transform).
|
|
struct DebugLineOut {
|
|
@builtin(position) clip: vec4<f32>,
|
|
@location(0) spherical: f32,
|
|
@location(1) cylindrical: f32,
|
|
@location(2) color: vec4<f32>,
|
|
}
|
|
@vertex fn vs_debug_line(@builtin(vertex_index) vertex: u32,
|
|
@location(0) start: vec3<f32>, @location(1) end: vec3<f32>,
|
|
@location(2) argb: vec4<f32>, @location(3) width: f32) -> DebugLineOut {
|
|
let position = select(start, end, vertex >= 2u);
|
|
let view_shrink = 1.0 - 1.0 / 256.0;
|
|
let line_start = globals.view_proj * vec4(position * view_shrink, 1.0);
|
|
let line_end = globals.view_proj * vec4((position + (end - start)) * view_shrink, 1.0);
|
|
let ndc1 = line_start.xyz / line_start.w;
|
|
let ndc2 = line_end.xyz / line_end.w;
|
|
let screen = globals.outline_params.xy;
|
|
let direction = normalize((ndc2.xy - ndc1.xy) * screen);
|
|
var offset = vec2(-direction.y, direction.x) * width / screen;
|
|
if offset.x < 0.0 { offset = -offset; }
|
|
if vertex % 2u != 0u { offset = -offset; }
|
|
var out: DebugLineOut;
|
|
out.clip = vec4((ndc1 + vec3(offset, 0.0)) * line_start.w, line_start.w);
|
|
out.spherical = length(position);
|
|
out.cylindrical = max(length(position.xz), abs(position.y));
|
|
// ARGB in a little-endian u32 reads as B, G, R, A.
|
|
out.color = argb.zyxw;
|
|
return out;
|
|
}
|
|
@fragment fn fs_debug_line(in: DebugLineOut) -> @location(0) vec4<f32> {
|
|
return apply_fog(in.color, in.spherical, in.cylindrical);
|
|
}
|