153 lines
5.6 KiB
TypeScript
153 lines
5.6 KiB
TypeScript
import { hash, noise1 } from '../../engine/rng';
|
|
|
|
/** Fixed layout of the scene on the 320x180 canvas. */
|
|
export const W = 320;
|
|
export const H = 180;
|
|
|
|
/**
|
|
* The canyon is seen through a simple pinhole camera standing on the floor and looking straight
|
|
* down the canyon: world X runs across it (meters, + is right), z away from the camera and h up
|
|
* from the floor. Everything on the ground (plants, deer, the stream) is placed in these
|
|
* coordinates so it all shares one perspective.
|
|
*/
|
|
export const HORIZON = 98;
|
|
export const VPX = 168;
|
|
export const F = 160;
|
|
export const CAM_H = 2;
|
|
|
|
/** Nearest floor depth on screen (the bottom row). */
|
|
export const Z_NEAR = (CAM_H * F) / (H - HORIZON);
|
|
/** The canyon walls end here and the far mesas take over. */
|
|
export const Z_END = 230;
|
|
|
|
export function project(X: number, z: number, h = 0): [number, number] {
|
|
return [VPX + (X * F) / z, HORIZON + ((CAM_H - h) * F) / z];
|
|
}
|
|
|
|
/** Depth of the floor seen at a screen row (rows at or above the horizon never meet it). */
|
|
export function floorZ(y: number): number {
|
|
return (CAM_H * F) / (y + 0.5 - HORIZON);
|
|
}
|
|
|
|
/** Center of the canyon floor: it winds gently left and right as it recedes. */
|
|
export const canyonX = (z: number): number => 4.2 * Math.sin(z / 17 + 0.4) + 2.2 * Math.sin(z / 7.1 + 2) - 2.6;
|
|
const canyonSlope = (z: number): number => (4.2 / 17) * Math.cos(z / 17 + 0.4) + (2.2 / 7.1) * Math.cos(z / 7.1 + 2);
|
|
export const halfWidth = (z: number): number => 10.5 + z * 0.02;
|
|
|
|
/** Height of each wall's rim: stepped benches and buttes, sinking toward the far end. */
|
|
export function wallHeight(z: number, side: -1 | 1): number {
|
|
const seed = side < 0 ? 11 : 12;
|
|
const bench = Math.floor(noise1(z / 11, seed) * 4) * 1.6;
|
|
const rough = (noise1(z / 1.6, seed + 2) - 0.5) * 1.2;
|
|
const sink = Math.max(0, z - 150) * 0.03;
|
|
return 15 + bench + rough - sink;
|
|
}
|
|
|
|
/** 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;
|
|
|
|
/** Per screen column: which wall the view ray meets first and how far away. */
|
|
export const SIDE = new Int8Array(W);
|
|
export const ZHIT = new Float32Array(W);
|
|
/** Inward wall normal (x, z) at the hit, for lighting. */
|
|
export const NORMAL_X = new Float32Array(W);
|
|
export const NORMAL_Z = new Float32Array(W);
|
|
/** Top and bottom screen rows of the wall in each column. */
|
|
export const RIM = new Int16Array(W);
|
|
export const FOOT = new Int16Array(W);
|
|
|
|
for (let x = 0; x < W; x++) {
|
|
const dir = (x + 0.5 - VPX) / F;
|
|
let z = Z_NEAR * 0.9;
|
|
SIDE[x] = 0;
|
|
ZHIT[x] = Z_END;
|
|
while (z < Z_END) {
|
|
const X = dir * z;
|
|
const c = canyonX(z);
|
|
const w = halfWidth(z);
|
|
if (X <= c - w || X >= c + w) {
|
|
SIDE[x] = X < c ? -1 : 1;
|
|
ZHIT[x] = z;
|
|
break;
|
|
}
|
|
z += Math.max(0.02, z * 0.002);
|
|
}
|
|
const s = SIDE[x];
|
|
if (s !== 0) {
|
|
// Tangent along the wall is (canyonSlope + widening, 1); the normal points into the canyon.
|
|
const tx = canyonSlope(z) + s * 0.02;
|
|
const n = Math.hypot(tx, 1);
|
|
NORMAL_X[x] = -s / n;
|
|
NORMAL_Z[x] = (s * tx) / n;
|
|
RIM[x] = Math.round(HORIZON - ((wallHeight(z, s as -1 | 1) - CAM_H) * F) / z);
|
|
FOOT[x] = Math.min(H, Math.round(HORIZON + (CAM_H * F) / z));
|
|
} else {
|
|
RIM[x] = HORIZON + 1;
|
|
FOOT[x] = HORIZON + 1;
|
|
}
|
|
}
|
|
|
|
/** Distant mesas closing off the far end of the canyon (top row per column). */
|
|
export const MESA: readonly number[] = Array.from({ length: W }, (_, x) => {
|
|
const flat = 88 - Math.floor(noise1(x / 26, 21) * 3) * 2;
|
|
const butte = Math.abs(x - 204) < 7 ? 80 + Math.abs(x - 204) * 0.3 : 99;
|
|
return Math.round(Math.min(flat + (noise1(x / 3, 22) - 0.5) * 1.4, butte));
|
|
});
|
|
|
|
/**
|
|
* A freestanding sandstone arch on the left rim near the far end. The sun rises behind it, so at
|
|
* sunrise it first shines through the opening.
|
|
*/
|
|
export const ARCH = (() => {
|
|
const cx = 146;
|
|
const base = RIM[cx];
|
|
return {
|
|
cx,
|
|
base,
|
|
outerRx: 21,
|
|
outerRy: 25,
|
|
innerRx: 14,
|
|
innerRy: 17,
|
|
/** Center of the opening, where the sun shines through. */
|
|
openY: base - 7,
|
|
};
|
|
})();
|
|
|
|
/** A small window worn through a fin just under the rim of the left wall. */
|
|
export const WINDOW = { cx: 96, cy: RIM[96] + 9, rx: 3.5, ry: 4.5 };
|
|
|
|
/** Is this screen pixel part of the arch's rock (above the rim)? */
|
|
export function archRock(x: number, y: number): boolean {
|
|
const a = ARCH;
|
|
const dx = x + 0.5 - a.cx;
|
|
const dy = a.base - (y + 0.5);
|
|
// Below the opening the arch is a solid fin running down to the canyon floor.
|
|
if (dy < 0) return y <= HORIZON && Math.abs(dx + 1) < a.outerRx - dy * 0.15 + (hash(x, y, 32) - 0.5);
|
|
const lean = dx + dy * 0.12;
|
|
const outer = (lean / a.outerRx) ** 2 + (dy / a.outerRy) ** 2;
|
|
if (outer > 1 + (hash(x, y, 31) - 0.5) * 0.08) return false;
|
|
return (dx / a.innerRx) ** 2 + (dy / a.innerRy) ** 2 > 1;
|
|
}
|
|
|
|
export function archOpening(x: number, y: number): boolean {
|
|
const a = ARCH;
|
|
const dx = x + 0.5 - a.cx;
|
|
const dy = a.base - (y + 0.5);
|
|
return dy >= -1 && (dx / a.innerRx) ** 2 + (dy / a.innerRy) ** 2 <= 1;
|
|
}
|
|
|
|
/** Everything solid that can hide the sun: walls, the arch and the far mesas. */
|
|
export const ROCK: Uint8Array = (() => {
|
|
const m = new Uint8Array(W * H);
|
|
for (let x = 0; x < W; x++) {
|
|
for (let y = 0; y < H; y++) {
|
|
// The arch's opening is kept clear of the wall behind it, so it always frames open sky.
|
|
const wall = SIDE[x] !== 0 && y >= RIM[x] && !(y < ARCH.base && archOpening(x, y));
|
|
const mesa = y >= MESA[x];
|
|
if (wall || mesa || archRock(x, y) || y > HORIZON) m[y * W + x] = 1;
|
|
}
|
|
}
|
|
return m;
|
|
})();
|