diff --git a/src/meditations/desert-sunrise/deer.ts b/src/meditations/desert-sunrise/deer.ts index 9128960..75424df 100644 --- a/src/meditations/desert-sunrise/deer.ts +++ b/src/meditations/desert-sunrise/deer.ts @@ -5,7 +5,7 @@ import type { Surface } from '../../engine/Surface'; import { clamp01, ease, pathTrack, track } from '../../engine/Timeline'; import { floorSunAt } from './stream'; import { sunlit, TIMES, type Frame, type Thing } from './timeline'; -import { F, project, streamHalf, streamX } from './world'; +import { F, groundHeight, project, streamHalf, streamX } from './world'; /** * A mule deer doe and her family come down the canyon in the blue before dawn. They stop at the @@ -374,7 +374,7 @@ function drawDeer(s: Surface, f: Frame, d: Deer): void { const p = place(d, t); const k = F / p.z; const sc = (k / 10) * d.size; - const [x, y] = project(p.X, p.z, 0.85 * d.size); + const [x, y] = project(p.X, p.z, groundHeight(p.X, p.z) + 0.85 * d.size); if (x < -40 || x > 360) return; const sun = Math.max(floorSunAt(f, p.X, p.z), 0); const L = (c: RGB) => sunlit(f, c, sun); diff --git a/src/meditations/desert-sunrise/flora.ts b/src/meditations/desert-sunrise/flora.ts index 5f733da..9008a48 100644 --- a/src/meditations/desert-sunrise/flora.ts +++ b/src/meditations/desert-sunrise/flora.ts @@ -4,7 +4,7 @@ import { hash, noise2, Rng } from '../../engine/rng'; import type { Surface } from '../../engine/Surface'; import { sunReach, floorSunAt } from './stream'; import { floorSunProgress, sunlit, type Frame, type Thing } from './timeline'; -import { canyonX, F, halfWidth, project, streamHalf, streamX, Z_NEAR } from './world'; +import { canyonX, F, groundHeight, halfWidth, project, streamHalf, streamX, Z_NEAR } from './world'; type Kind = 'sage' | 'rabbitbrush' | 'grass' | 'yucca' | 'pear' | 'primrose' | 'juniper' | 'cottonwood' | 'boulder' | 'willow'; @@ -269,7 +269,7 @@ function drawWillow(s: Surface, f: Frame, p: Plant, bx: number, by: number, k: n function drawPlant(s: Surface, f: Frame, p: Plant): void { const k = F / p.z; - const [fx, fy] = project(p.X, p.z); + const [fx, fy] = project(p.X, p.z, groundHeight(p.X, p.z)); const bx = Math.round(fx); const by = Math.round(fy); const sun = floorSunAt(f, p.X, p.z); diff --git a/src/meditations/desert-sunrise/stream.ts b/src/meditations/desert-sunrise/stream.ts index 2f3d822..c2ef00f 100644 --- a/src/meditations/desert-sunrise/stream.ts +++ b/src/meditations/desert-sunrise/stream.ts @@ -3,7 +3,27 @@ import { clamp01, progress } from '../../engine/Timeline'; import { hash, noise1, noise2 } from '../../engine/rng'; import type { Surface } from '../../engine/Surface'; import { sunlit, type Frame, type Thing } from './timeline'; -import { canyonX, F, FOOT, H, halfWidth, HORIZON, floorZ, LEDGES, project, SIDE, STREAM_ROCKS, streamHalf, streamX, VPX, W } from './world'; +import { + APRON_H, + CAM_H, + canyonX, + F, + floorZ, + groundHeight, + H, + halfWidth, + HORIZON, + LEDGES, + project, + SIDE, + STREAM_ROCKS, + streamHalf, + streamX, + VPX, + W, + Z_NEAR, + ZHIT, +} from './world'; const SAND = hex('#d4a47c'); const SAND_DARK = hex('#c08e68'); @@ -17,6 +37,10 @@ const FOAM = hex('#e8f0f4'); const WHITE_WATER = hex('#f4f8fa'); const MIST = hex('#c8d4e8'); const MIST_GOLD = hex('#ffd6a0'); +const TALUS = hex('#b07258'); +const TALUS_DARK = hex('#94604c'); +const BLOCK = hex('#7e4e3e'); +const BLOCK_LIT = hex('#c48a68'); /** * How much sun reaches a spot on the floor (0–1) once the sweep has got `reach` (0–1) of the way @@ -40,40 +64,67 @@ export function floorSunAt(f: Frame, X: number, z: number): number { const KIND = new Uint8Array(W * H); const FX = new Float32Array(W * H); const FZ = new Float32Array(W * H); +/** Height of the ground (m), above zero only on the talus aprons at the foot of the walls. */ +const FH = new Float32Array(W * H); /** Distance (m) from the water's edge, negative inside the stream. */ const EDGE = new Float32Array(W * H); const GROUND = new Uint8ClampedArray(W * H * 3); -for (let y = HORIZON + 1; y < H; y++) { - const z = floorZ(y); - for (let x = 0; x < W; x++) { - if (SIDE[x] !== 0 && y < FOOT[x]) continue; - const i = y * W + x; - const X = ((x + 0.5 - VPX) * z) / F; - FX[i] = X; - FZ[i] = z; - // Far rows are thinner than a pixel, so the stream gets a minimum width there. - const half = Math.max(streamHalf(z), (0.55 * z) / F); - const e = Math.abs(X - streamX(z)) - half; - EDGE[i] = e; - KIND[i] = e < 0 ? 2 : 1; - if (e < 0) continue; - const n = noise2(X * 0.9, z * 0.6, 52); - let c: RGB = n > 0.62 ? SAND_PALE : n < 0.32 ? SAND_DARK : SAND; - // Scattered pebbles, each a little disc somewhere in its own cell. - const cx = Math.floor(X * 4); - const cz = Math.floor(z * 4); - if (hash(cx, cz, 53) > 0.8) { - const px = (cx + 0.2 + hash(cx, cz, 57) * 0.6) / 4; - const pz = (cz + 0.2 + hash(cx, cz, 58) * 0.6) / 4; - if (Math.hypot(X - px, (z - pz) * 0.8) < 0.035 + hash(cx, cz, 59) * 0.04) c = PEBBLE; +function groundColor(x: number, y: number, X: number, z: number, h: number, e: number): RGB { + const n = noise2(X * 0.9, z * 0.6, 52); + let c: RGB = n > 0.62 ? SAND_PALE : n < 0.32 ? SAND_DARK : SAND; + // Scattered pebbles, each a little disc somewhere in its own cell. + const cx = Math.floor(X * 4); + const cz = Math.floor(z * 4); + if (hash(cx, cz, 53) > 0.8) { + const px = (cx + 0.2 + hash(cx, cz, 57) * 0.6) / 4; + const pz = (cz + 0.2 + hash(cx, cz, 58) * 0.6) / 4; + if (Math.hypot(X - px, (z - pz) * 0.8) < 0.035 + hash(cx, cz, 59) * 0.04) c = PEBBLE; + } else if (noise2(X * 0.5, z * 0.5, 54) > 0.72 && e > 1.5) c = mix(c, CRUST, 0.55); + // Damp sand and a fringe of grass along the banks. + if (e < 0.35) c = mix(c, DAMP, 1 - e / 0.35); + else if (e < 1.1 && noise2(X * 3, z * 3, 55) > 0.5 && hash(x, y, 56) > 0.35) c = mix(c, GRASS, 0.7); + if (h <= 0.01) return c; + // Up the talus apron the sand gives way to reddish scree and fallen blocks that grow larger + // and more frequent toward the wall, and the steeper slope turns a little away from the sky. + const up = Math.min(1, h / APRON_H); + c = mix(c, mix(TALUS, TALUS_DARK, noise2(X * 1.3, z * 1.3, 37)), Math.min(1, up * 1.6)); + const cell = 2.6 - up * 1.4; + const bx = Math.floor(X * cell); + const bz = Math.floor(z * cell); + if (hash(bx, bz, 38) < up * 0.75) { + const ox = (bx + 0.25 + hash(bx, bz, 39) * 0.5) / cell; + const oz = (bz + 0.25 + hash(bx, bz, 40) * 0.5) / cell; + const r = (0.06 + hash(bx, bz, 41) * 0.12) * (0.6 + up); + const dx = X - ox; + const dz = (z - oz) * 0.7; + if (dx * dx + dz * dz < r * r) c = dz < -r * 0.2 ? BLOCK_LIT : BLOCK; + } + return scale(c, 1 - 0.14 * up); +} + +// Each screen column marches out over the ground; wherever the ground rises into view (the +// aprons climbing toward the walls) it covers the foot of the wall behind. +for (let x = 0; x < W; x++) { + const dir = (x + 0.5 - VPX) / F; + const zMax = SIDE[x] !== 0 ? ZHIT[x] : 320; + let top = H; + for (let z = Z_NEAR * 0.95; z < zMax && top > HORIZON + 1; z += Math.max(0.008, z * 0.003)) { + const X = dir * z; + const h = groundHeight(X, z); + const row = Math.max(HORIZON + 1, Math.ceil(HORIZON + ((CAM_H - h) * F) / z - 0.5)); + for (let y = row; y < top; y++) { + const i = y * W + x; + FX[i] = X; + FZ[i] = z; + FH[i] = h; + // Far rows are thinner than a pixel, so the stream gets a minimum width there. + const e = Math.abs(X - streamX(z)) - Math.max(streamHalf(z), (0.55 * z) / F); + EDGE[i] = e; + KIND[i] = e < 0 && h <= 0.01 ? 2 : 1; + if (KIND[i] === 1) GROUND.set(groundColor(x, y, X, z, h, e), i * 3); } - else if (noise2(X * 0.5, z * 0.5, 54) > 0.72 && e > 1.5) c = mix(c, CRUST, 0.55); - // Damp sand and a fringe of grass along the banks. - if (e < 0.35) c = mix(c, DAMP, 1 - e / 0.35); - else if (e < 1.1 && noise2(X * 3, z * 3, 55) > 0.5 && hash(x, y, 56) > 0.35) c = mix(c, GRASS, 0.7); - // Ordered dither between neighbouring tones keeps the far floor from shimmering. - GROUND.set(c, i * 3); + top = Math.min(top, row); } } @@ -134,7 +185,7 @@ export function drawFloor(s: Surface, f: Frame): void { let g = GROUND[b + 1] * amb[1]; let bl = GROUND[b + 2] * amb[2]; if (sunny) { - const u = (FX[i] - edge + 0.4) / 0.8; + const u = (FX[i] - (FH[i] > 0.01 ? sunEdge(f.floorSun, FZ[i]) : edge) + 0.4) / 0.8; const k = (u <= 0 ? 0 : u >= 1 ? 1 : ditherStep(u, x, y, 3)) * day; r += GROUND[b] * (sun[0] / 255) * k; g += GROUND[b + 1] * (sun[1] / 255) * k; diff --git a/src/meditations/desert-sunrise/world.ts b/src/meditations/desert-sunrise/world.ts index 0db332c..3c83ca4 100644 --- a/src/meditations/desert-sunrise/world.ts +++ b/src/meditations/desert-sunrise/world.ts @@ -43,6 +43,26 @@ export function wallHeight(z: number, side: -1 | 1): number { return 15 + bench + rough - sink; } +/** Height and reach (m) of the talus aprons where the floor sweeps up to meet each wall. */ +export const APRON_H = 2.6; +const APRON_W = 4.5; + +/** + * Height of the ground above the flat canyon floor: zero across the middle, rising in a concave + * sweep of sand and rubble over the last few meters to the foot of each wall. + */ +export function groundHeight(X: number, z: number): number { + const c = canyonX(z); + const d = halfWidth(z) - Math.abs(X - c); + if (d >= APRON_W) return 0; + const u = 1 - Math.max(0, d) / APRON_W; + // Lumpy along the wall, where rockfall has piled up more in some places than others. + const side = X > c ? 50 : 0; + // Lumpy along the wall, where rockfall has piled up more in some places than others, with a + // ragged crest of fallen blocks. + return APRON_H * u * u * (0.75 + 0.5 * noise1(z * 0.45 + side, 36) + 0.22 * (noise1(z * 3.2 + side, 37) - 0.5)); +} + /** The stream meanders inside the canyon floor. */ export const streamX = (z: number): number => canyonX(z) + 1.1 * Math.sin(z / 5.3 + 2.4) + 0.8 * Math.sin(z / 13 + 0.6) - 1.4; export const streamHalf = (z: number): number => 0.62 + z * 0.006;