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.

4.5 KB
Raw Blame
#pragma shader "Sky"
#pragma version 300 es
#pragma domain Lit

// The background half of an environment. The reflection in a metal ball and the
// sky behind it are the same baked panorama; without this only one of them was
// ever visible, and a scene lit by a sky sat in front of a flat clear colour.
//
// Geometry is a quad over the whole screen, so the vertex stage needs no matrix of its
// own — the fragment's direction is the one from the eye through it, which is
// what viewDirection() already answers (negated: it points AT the viewer).

#pragma vertex
layout(location = 0) in vec3 a_position;

out highp vec3 v_worldPos;

void main() {
    gl_Position = u_projection * vec4(a_position, 1.0);
    v_worldPos = a_position;
}
#pragma end

#pragma fragment
precision highp float;

in highp vec3 v_worldPos;
out vec4 fragColor;

#ifdef ES_SKY_PANORAMA
uniform sampler2D u_texture;

// Decoded before it is blended: RGBM filtered by the hardware mixes multipliers
// and colours apart, which blots every high-contrast edge of the sky.
highp vec3 skyTexel(in ivec2 p, in ivec2 size) {
    ivec2 q = ivec2(((p.x % size.x) + size.x) % size.x, clamp(p.y, 0, size.y - 1));
    highp vec4 t = texelFetch(u_texture, q, 0);
    return t.rgb * t.rgb * (t.a * t.a * u_envParams.y);
}

// panoramaUV's mapping (sdk/src/lightmap/panorama.ts): +Z at the centre, u toward
// -X, row 0 up.
highp vec3 skyPanorama(in highp vec3 d) {
    ivec2 size = textureSize(u_texture, 0);
    highp float u = 0.5 - atan(d.x, d.z) / 6.28318530718;
    highp float v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
    highp vec2 f = vec2(u * float(size.x), v * float(size.y)) - 0.5;
    ivec2 i = ivec2(floor(f));
    highp vec2 t = f - floor(f);
    return mix(mix(skyTexel(i, size), skyTexel(i + ivec2(1, 0), size), t.x),
               mix(skyTexel(i + ivec2(0, 1), size), skyTexel(i + ivec2(1, 1), size), t.x), t.y);
}
#endif

void main() {
#ifdef ES_SKY_PANORAMA
    fragColor = vec4(skyPanorama(envDirection(-viewDirection(v_worldPos))), 1.0);
#else
    // Mip 0: the sharpest level of the prefiltered atlas, the fallback for an
    // environment imported before it carried its panorama.
    fragColor = vec4(envSampleMip(-viewDirection(v_worldPos), 0.0), 1.0);
#endif
}
#pragma end

#pragma vertex wgsl
struct VSIn {
    @location(0) a_position : vec3f,
};
struct VSOut {
    @builtin(position) pos : vec4f,
    @location(0) v_worldPos : vec3f,
};

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

#pragma fragment wgsl
// Slot 0 is bound and never read — a draw fills it so the walk that pins the
// atlas one slot on has somewhere to start. The atlas itself is on 3, and
// without these two the injected sampler names identifiers this twin never
// declared: an invalid pipeline that does not mention a shader.
@group(1) @binding(0) var t0 : texture_2d<f32>;
@group(1) @binding(8) var s0 : sampler;
@group(1) @binding(3) var t3 : texture_2d<f32>;
@group(1) @binding(11) var s3 : sampler;
// The 2D shadow mask, which the injected lighting reads for every Lit shader.
@group(1) @binding(7) var t7 : texture_2d<f32>;
@group(1) @binding(15) var s7 : sampler;

// Declared again: each WGSL block is compiled on its own, so a struct defined in
// the vertex one is an undeclared name here — and that reaches anyone as an
// invalid pipeline with nothing in the log about a shader.
struct VSOut {
    @builtin(position) pos : vec4f,
    @location(0) v_worldPos : vec3f,
};

#ifdef ES_SKY_PANORAMA
fn skyTexel(p : vec2i, size : vec2i) -> vec3f {
    let q = vec2i(((p.x % size.x) + size.x) % size.x, clamp(p.y, 0, size.y - 1));
    let t = textureLoad(t0, q, 0);
    return t.rgb * t.rgb * (t.a * t.a * lc.u_envParams.y);
}

fn skyPanorama(d : vec3f) -> vec3f {
    let size = vec2i(textureDimensions(t0, 0));
    let u = 0.5 - atan2(d.x, d.z) / 6.28318530718;
    let v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
    let f = vec2f(u * f32(size.x), v * f32(size.y)) - 0.5;
    let i = vec2i(floor(f));
    let t = f - floor(f);
    return mix(mix(skyTexel(i, size), skyTexel(i + vec2i(1, 0), size), t.x),
               mix(skyTexel(i + vec2i(0, 1), size), skyTexel(i + vec2i(1, 1), size), t.x), t.y);
}
#endif

@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
#ifdef ES_SKY_PANORAMA
    return vec4f(skyPanorama(envDirection(-viewDirection(v.v_worldPos))), 1.0);
#else
    return vec4f(envSampleMip(-viewDirection(v.v_worldPos), 0.0), 1.0);
#endif
}
#pragma end