Website: 16-bit pixel-art rebuild with a live Three.js office, downloads from Gitea v1.5.1
CI / Typecheck, lint, tests, e2e (push) Failing after 3m51s
CI / Typecheck, lint, tests, e2e (push) Failing after 3m51s
- Title screen over a real-time voxel office rendered at low resolution and quantized to a rotating 32-colour palette with Bayer dithering; day part and season re-light the scene and every surface. - 16-bit UI kit: name-plate dialogs, menu cursor, inventory, XP bar, badges, NPC speech, GAME OVER with CONTINUE, hand-placed 12x12 icons, Fusion Pixel font (Latin subset preloaded). - Sections as a game: quest log (onboarding), character select (AI CEO / AI Co-Founder), inventory (features), party roster (providers), live download XP, save point (all v1.5.1 builds), FAQ as NPC dialog, credits. - Downloads and source point to the Gitea release; the download function only redirects to git.zacharyzhang.com. - Tailwind and the old components removed; unit tests for platform/downloads, rewritten e2e suite. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e374bc1e8e
commit
2d7af224dd
112 files changed
+5692
-4223
No files matched your search
@@ -0,0 +1,133 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { dayPartAt, paletteFor, seasonAt, type DayPart, type Season } from "../../lib/palette";
|
||||
import styles from "./world.module.css";
|
||||
|
||||
const DAY_PARTS: readonly DayPart[] = ["dawn", "day", "dusk", "night"];
|
||||
const SEASONS: readonly Season[] = ["spring", "summer", "autumn", "winter"];
|
||||
const TOWERS = [34, 52, 28, 70, 44, 86, 38, 58, 30, 64, 48, 76, 36, 54];
|
||||
|
||||
function attribute<T extends string>(name: string, allowed: readonly T[], clock: T): T {
|
||||
const value = document.documentElement.getAttribute(name);
|
||||
if (value === null) return clock;
|
||||
if (!allowed.includes(value as T)) throw new Error(`PixelWorld: unknown ${name} "${value}"`);
|
||||
return value as T;
|
||||
}
|
||||
|
||||
function webglAvailable(): boolean {
|
||||
const gl = document.createElement("canvas").getContext("webgl2");
|
||||
gl?.getExtension("WEBGL_lose_context")?.loseContext();
|
||||
return gl !== null;
|
||||
}
|
||||
|
||||
function Skyline() {
|
||||
return (
|
||||
<div className={styles.fallback} aria-hidden="true">
|
||||
{[0, 1, 2, 3].map((band) => (
|
||||
<div key={band} className={styles.band} style={{ background: `var(--ink-sky-${band})` }} />
|
||||
))}
|
||||
<div className={styles.skyline}>
|
||||
{TOWERS.map((height, index) => (
|
||||
<div key={index} className={styles.tower} style={{ height: `${height}%` }} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Hero diorama of the AgentCompany office, re-lit live whenever the site palette rotates. */
|
||||
export function PixelWorld({ className, focus = "center" }: { className?: string; focus?: "center" | "right" }) {
|
||||
const frameRef = useRef<HTMLDivElement>(null);
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||
const focusRef = useRef(focus);
|
||||
const reframeRef = useRef<(() => void) | null>(null);
|
||||
const [fallback, setFallback] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
focusRef.current = focus;
|
||||
reframeRef.current?.();
|
||||
}, [focus]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!webglAvailable()) {
|
||||
setFallback(true);
|
||||
return;
|
||||
}
|
||||
const frame = frameRef.current!;
|
||||
const canvas = canvasRef.current!;
|
||||
let cleanup: (() => void) | undefined;
|
||||
let cancelled = false;
|
||||
|
||||
void import("./engine").then(({ createWorld }) => {
|
||||
if (cancelled) return;
|
||||
const world = createWorld(canvas);
|
||||
const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
||||
let visible = false;
|
||||
let running = false;
|
||||
|
||||
const refresh = () => {
|
||||
running = !reduced && visible && !document.hidden;
|
||||
world.setRunning(running);
|
||||
if (!running) world.render();
|
||||
};
|
||||
const repaint = () => {
|
||||
const now = new Date();
|
||||
const part = attribute("data-daypart", DAY_PARTS, dayPartAt(now));
|
||||
world.setPalette(paletteFor(part, attribute("data-season", SEASONS, seasonAt(now))), part);
|
||||
if (!running) world.render();
|
||||
};
|
||||
const fit = () => {
|
||||
world.setFocusRight(focusRef.current === "right" && frame.clientWidth > 900);
|
||||
world.resize(frame.clientWidth, frame.clientHeight);
|
||||
};
|
||||
const resize = () => {
|
||||
fit();
|
||||
if (!running) world.render();
|
||||
};
|
||||
const point = (event: PointerEvent) => world.setPointer((event.clientX / window.innerWidth) * 2 - 1, (event.clientY / window.innerHeight) * 2 - 1);
|
||||
const scroll = () => world.setScroll(Math.min(1, Math.max(0, window.scrollY / Math.max(1, frame.clientHeight))));
|
||||
|
||||
fit();
|
||||
repaint();
|
||||
reframeRef.current = resize;
|
||||
const sizes = new ResizeObserver(resize);
|
||||
sizes.observe(frame);
|
||||
const palette = new MutationObserver(repaint);
|
||||
palette.observe(document.documentElement, { attributes: true, attributeFilter: ["data-daypart", "data-season"] });
|
||||
const sight = new IntersectionObserver(([entry]) => {
|
||||
visible = entry!.isIntersecting;
|
||||
refresh();
|
||||
});
|
||||
sight.observe(frame);
|
||||
document.addEventListener("visibilitychange", refresh);
|
||||
if (!reduced) {
|
||||
window.addEventListener("pointermove", point, { passive: true });
|
||||
window.addEventListener("scroll", scroll, { passive: true });
|
||||
}
|
||||
refresh();
|
||||
|
||||
cleanup = () => {
|
||||
reframeRef.current = null;
|
||||
sizes.disconnect();
|
||||
palette.disconnect();
|
||||
sight.disconnect();
|
||||
document.removeEventListener("visibilitychange", refresh);
|
||||
window.removeEventListener("pointermove", point);
|
||||
window.removeEventListener("scroll", scroll);
|
||||
world.dispose();
|
||||
};
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
cleanup?.();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div ref={frameRef} className={className ? `${styles.frame} ${className}` : styles.frame}>
|
||||
{fallback ? <Skyline /> : <canvas ref={canvasRef} className={styles.canvas} aria-hidden="true" />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
import { Group, Vector2, type Object3D } from "three";
|
||||
import { INK } from "../../lib/palette";
|
||||
import { CEO_DESK, DESKS, SHARED_SPOTS, deskSpot, type Point, type Spot } from "./layout";
|
||||
import { box, type Inks } from "./voxel";
|
||||
|
||||
interface Rig {
|
||||
root: Group;
|
||||
torso: Group;
|
||||
head: Group;
|
||||
legs: [Group, Group];
|
||||
arms: [Group, Group];
|
||||
}
|
||||
|
||||
interface Agent {
|
||||
rig: Rig;
|
||||
desk: Spot;
|
||||
spot: Spot;
|
||||
path: Vector2[];
|
||||
wait: number;
|
||||
stride: number;
|
||||
seed: number;
|
||||
}
|
||||
|
||||
const HIP = 0.36;
|
||||
const SEAT = 0.48;
|
||||
const SPEED = 1.7;
|
||||
const SCALE = 1.25;
|
||||
const TEAM = [
|
||||
{ shirt: INK.accent, hair: INK.woodDark },
|
||||
{ shirt: INK.sea, hair: INK.ink },
|
||||
{ shirt: INK.mint, hair: INK.brick },
|
||||
{ shirt: INK.berry, hair: INK.gold },
|
||||
{ shirt: INK.rug, hair: INK.woodDark },
|
||||
{ shirt: INK.leaf1, hair: INK.ink },
|
||||
];
|
||||
|
||||
function limb(parent: Object3D, inks: Inks, ink: number, x: number, y: number, w: number, h: number): Group {
|
||||
const pivot = new Group();
|
||||
pivot.position.set(x, y, 0);
|
||||
parent.add(pivot);
|
||||
box(pivot, inks[ink]!, w, h, w + 0.02, 0, -h, 0);
|
||||
return pivot;
|
||||
}
|
||||
|
||||
function buildRig(inks: Inks, shirt: number, hair: number, ceo: boolean): Rig {
|
||||
const root = new Group();
|
||||
root.scale.setScalar(SCALE);
|
||||
const torso = new Group();
|
||||
root.add(torso);
|
||||
const legs: [Group, Group] = [limb(root, inks, INK.inkSoft, -0.11, HIP, 0.17, HIP), limb(root, inks, INK.inkSoft, 0.11, HIP, 0.17, HIP)];
|
||||
box(torso, inks[shirt]!, 0.46, 0.42, 0.3, 0, HIP, 0);
|
||||
if (ceo) box(torso, inks[INK.gold]!, 0.08, 0.3, 0.02, 0, HIP + 0.08, 0.15);
|
||||
const arms: [Group, Group] = [limb(torso, inks, shirt, -0.3, 0.76, 0.13, 0.36), limb(torso, inks, shirt, 0.3, 0.76, 0.13, 0.36)];
|
||||
const head = new Group();
|
||||
head.position.y = 0.78;
|
||||
torso.add(head);
|
||||
box(head, inks[INK.skin]!, 0.5, 0.46, 0.46, 0, 0, 0);
|
||||
box(head, inks[hair]!, 0.54, 0.14, 0.5, 0, 0.4, 0);
|
||||
box(head, inks[hair]!, 0.54, 0.3, 0.12, 0, 0.12, -0.21);
|
||||
if (ceo) {
|
||||
box(head, inks[INK.void]!, 0.44, 0.1, 0.04, 0, 0.2, 0.24);
|
||||
box(head, inks[INK.gold]!, 0.4, 0.1, 0.4, 0, 0.54, 0);
|
||||
for (const [x, z] of [[-0.15, -0.15], [0.15, -0.15], [-0.15, 0.15], [0.15, 0.15]] as Point[]) box(head, inks[INK.gold]!, 0.1, 0.16, 0.1, x, 0.64, z);
|
||||
box(head, inks[INK.accent]!, 0.1, 0.1, 0.04, 0, 0.55, 0.21);
|
||||
} else {
|
||||
for (const x of [-0.12, 0.12]) box(head, inks[INK.void]!, 0.07, 0.1, 0.03, x, 0.18, 0.235);
|
||||
}
|
||||
return { root, torso, head, legs, arms };
|
||||
}
|
||||
|
||||
function turn(current: number, target: number, amount: number): number {
|
||||
const delta = Math.atan2(Math.sin(target - current), Math.cos(target - current));
|
||||
return current + delta * Math.min(1, amount);
|
||||
}
|
||||
|
||||
/** Seven blocky staff (CEO first, crowned) cycling desk → coffee / meeting → desk along corridor waypoints. */
|
||||
export function createAgents(inks: Inks): { root: Group; update(dt: number, time: number): void } {
|
||||
const root = new Group();
|
||||
const taken = new Set<Spot>();
|
||||
const desks = [CEO_DESK, ...DESKS].map(deskSpot);
|
||||
const agents: Agent[] = desks.map((desk, i) => {
|
||||
const look = i === 0 ? { shirt: INK.ink, hair: INK.ink } : TEAM[i - 1]!;
|
||||
const rig = buildRig(inks, look.shirt, look.hair, i === 0);
|
||||
root.add(rig.root);
|
||||
rig.root.position.set(desk.at[0], 0, desk.at[1]);
|
||||
rig.root.rotation.y = desk.face;
|
||||
return { rig, desk, spot: desk, path: [], wait: 2 + i * 1.7, stride: 0, seed: i * 1.37 };
|
||||
});
|
||||
|
||||
const route = (from: Spot, to: Spot) =>
|
||||
[...from.exit, ...[...to.exit].reverse(), to.at].map(([x, z]) => new Vector2(x, z));
|
||||
|
||||
function choose(agent: Agent): void {
|
||||
let next = agent.desk;
|
||||
if (agent.spot === agent.desk) {
|
||||
const kind = Math.random() < 0.45 ? "coffee" : "meeting";
|
||||
const free = SHARED_SPOTS.filter((s) => s.kind === kind && !taken.has(s));
|
||||
if (free.length === 0) {
|
||||
agent.wait = 3;
|
||||
return;
|
||||
}
|
||||
next = free[Math.floor(Math.random() * free.length)]!;
|
||||
taken.add(next);
|
||||
} else {
|
||||
taken.delete(agent.spot);
|
||||
}
|
||||
agent.path = route(agent.spot, next);
|
||||
agent.spot = next;
|
||||
}
|
||||
|
||||
function pose(agent: Agent, dt: number, time: number): void {
|
||||
const { rig, spot } = agent;
|
||||
const walking = agent.path.length > 0;
|
||||
const seated = !walking && spot.sit;
|
||||
const swing = walking ? Math.sin(agent.stride) * 0.7 : 0;
|
||||
rig.legs[0].rotation.x = seated ? -Math.PI / 2 : swing;
|
||||
rig.legs[1].rotation.x = seated ? -Math.PI / 2 : -swing;
|
||||
rig.root.position.y = seated ? SEAT - HIP * SCALE : walking ? Math.abs(Math.sin(agent.stride)) * 0.06 : 0;
|
||||
const tap = Math.sin(time * 16 + agent.seed * 5);
|
||||
if (walking) {
|
||||
rig.arms[0].rotation.x = -swing * 0.8;
|
||||
rig.arms[1].rotation.x = swing * 0.8;
|
||||
} else if (spot.kind === "desk") {
|
||||
rig.arms[0].rotation.x = -1.2 + tap * 0.15;
|
||||
rig.arms[1].rotation.x = -1.2 - tap * 0.15;
|
||||
} else if (spot.kind === "coffee") {
|
||||
const sip = Math.sin(time * 1.3 + agent.seed) > 0.4;
|
||||
rig.arms[0].rotation.x = 0;
|
||||
rig.arms[1].rotation.x = sip ? -2.2 : -0.9;
|
||||
} else {
|
||||
rig.arms[0].rotation.x = rig.arms[1].rotation.x = -0.7;
|
||||
}
|
||||
rig.torso.position.y = !walking && spot.kind === "desk" ? (tap > 0.6 ? 0.03 : 0) : 0;
|
||||
rig.head.rotation.x = !walking && spot.kind === "meeting" ? Math.max(0, Math.sin(time * 2 + agent.seed)) * 0.25 : 0;
|
||||
if (!walking) rig.root.rotation.y = turn(rig.root.rotation.y, spot.face, dt * 8);
|
||||
}
|
||||
|
||||
return {
|
||||
root,
|
||||
update(dt, time) {
|
||||
for (const agent of agents) {
|
||||
const { rig } = agent;
|
||||
const target = agent.path[0];
|
||||
if (target) {
|
||||
const here = new Vector2(rig.root.position.x, rig.root.position.z);
|
||||
const gap = target.clone().sub(here);
|
||||
const step = SPEED * dt;
|
||||
if (gap.length() <= step) {
|
||||
rig.root.position.x = target.x;
|
||||
rig.root.position.z = target.y;
|
||||
agent.path.shift();
|
||||
if (agent.path.length === 0) agent.wait = agent.spot === agent.desk ? 8 + Math.random() * 8 : 5 + Math.random() * 6;
|
||||
} else {
|
||||
gap.normalize();
|
||||
rig.root.position.x += gap.x * step;
|
||||
rig.root.position.z += gap.y * step;
|
||||
rig.root.rotation.y = turn(rig.root.rotation.y, Math.atan2(gap.x, gap.y), dt * 12);
|
||||
agent.stride += dt * 11;
|
||||
}
|
||||
} else {
|
||||
agent.wait -= dt;
|
||||
if (agent.wait <= 0) choose(agent);
|
||||
}
|
||||
pose(agent, dt, time);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
import {
|
||||
BasicShadowMap, Camera, HalfFloatType, Mesh, NearestFilter, OrthographicCamera, PlaneGeometry, Scene, Vector2,
|
||||
WebGLRenderer, WebGLRenderTarget,
|
||||
} from "three";
|
||||
import type { DayPart } from "../../lib/palette";
|
||||
import { createAgents } from "./agents";
|
||||
import { applyLighting, createLights } from "./lighting";
|
||||
import { buildOffice } from "./office";
|
||||
import { createQuantizeMaterial, setQuantizePalette } from "./quantize";
|
||||
import { createSky, setSkyPalette } from "./sky";
|
||||
import { UNIT_BOX, createInks, paintInks } from "./voxel";
|
||||
|
||||
export interface World {
|
||||
setPalette(colours: string[], part: DayPart): void;
|
||||
resize(width: number, height: number): void;
|
||||
setPointer(x: number, y: number): void;
|
||||
setScroll(progress: number): void;
|
||||
setFocusRight(right: boolean): void;
|
||||
setRunning(running: boolean): void;
|
||||
render(): void;
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
const ART_PIXELS = 320 * 180;
|
||||
const MAX_DPR = 2;
|
||||
const TARGET = { x: 1.6, y: 0.2, z: 0.4 };
|
||||
const RIGHT_SHARE = 0.55;
|
||||
|
||||
/** Real-time office diorama drawn at ~320×180, then palette-quantized onto the canvas. */
|
||||
export function createWorld(canvas: HTMLCanvasElement): World {
|
||||
const renderer = new WebGLRenderer({ canvas, antialias: false, alpha: false, powerPreference: "low-power" });
|
||||
renderer.setPixelRatio(1);
|
||||
renderer.shadowMap.enabled = true;
|
||||
renderer.shadowMap.type = BasicShadowMap;
|
||||
|
||||
const target = new WebGLRenderTarget(320, 180, { type: HalfFloatType, minFilter: NearestFilter, magFilter: NearestFilter });
|
||||
const quantize = createQuantizeMaterial(target.texture);
|
||||
const post = new Scene();
|
||||
const postQuad = new Mesh(new PlaneGeometry(2, 2), quantize);
|
||||
postQuad.frustumCulled = false;
|
||||
post.add(postQuad);
|
||||
const postCamera = new Camera();
|
||||
|
||||
const scene = new Scene();
|
||||
const inks = createInks();
|
||||
const office = buildOffice(inks);
|
||||
const agents = createAgents(inks);
|
||||
const lights = createLights();
|
||||
const sky = createSky();
|
||||
scene.add(sky, office.root, agents.root, lights.sun, lights.sun.target, lights.sky);
|
||||
|
||||
const camera = new OrthographicCamera(-1, 1, 1, -1, 1, 120);
|
||||
const pointer = new Vector2();
|
||||
const eased = new Vector2();
|
||||
let scroll = 0;
|
||||
let focusRight = false;
|
||||
let aspect = 16 / 9;
|
||||
let time = 0;
|
||||
let last = 0;
|
||||
let frame = 0;
|
||||
|
||||
function place(): void {
|
||||
eased.lerp(pointer, 0.06);
|
||||
const yaw = Math.PI / 4 + Math.sin(time * 0.08) * 0.16 + eased.x * 0.12;
|
||||
const pitch = 0.56 + eased.y * 0.05;
|
||||
camera.position.set(
|
||||
TARGET.x + Math.sin(yaw) * Math.cos(pitch) * 50,
|
||||
TARGET.y + Math.sin(pitch) * 50,
|
||||
TARGET.z + Math.cos(yaw) * Math.cos(pitch) * 50,
|
||||
);
|
||||
camera.lookAt(TARGET.x, TARGET.y, TARGET.z);
|
||||
const share = focusRight ? RIGHT_SHARE : 1;
|
||||
const half = Math.max(10.6, 13 / (aspect * share)) / (1 + scroll * 0.18);
|
||||
const width = half * aspect * 2;
|
||||
const centre = focusRight ? 1 - share / 2 : 0.5;
|
||||
camera.top = half;
|
||||
camera.bottom = -half;
|
||||
camera.left = -width * centre;
|
||||
camera.right = width * (1 - centre);
|
||||
camera.updateProjectionMatrix();
|
||||
(sky.material.uniforms.offset!.value as Vector2).set(-eased.x * 14 - Math.sin(time * 0.08) * 16, 0);
|
||||
sky.material.uniforms.time!.value = time;
|
||||
}
|
||||
|
||||
function render(): void {
|
||||
place();
|
||||
office.update(time);
|
||||
renderer.setRenderTarget(target);
|
||||
renderer.render(scene, camera);
|
||||
renderer.setRenderTarget(null);
|
||||
renderer.render(post, postCamera);
|
||||
}
|
||||
|
||||
function tick(now: number): void {
|
||||
const dt = last ? Math.min((now - last) / 1000, 0.1) : 0;
|
||||
last = now;
|
||||
time += dt;
|
||||
agents.update(dt, time);
|
||||
render();
|
||||
frame = requestAnimationFrame(tick);
|
||||
}
|
||||
|
||||
return {
|
||||
setPalette(colours, part) {
|
||||
paintInks(inks, colours);
|
||||
setSkyPalette(sky.material, colours, part);
|
||||
setQuantizePalette(quantize, colours);
|
||||
applyLighting(lights, office, colours, part);
|
||||
},
|
||||
resize(width, height) {
|
||||
const dpr = Math.min(window.devicePixelRatio || 1, MAX_DPR);
|
||||
const deviceWidth = width * dpr;
|
||||
const deviceHeight = height * dpr;
|
||||
const scale = Math.max(1, Math.round(Math.sqrt((deviceWidth * deviceHeight) / ART_PIXELS)));
|
||||
const artWidth = Math.ceil(deviceWidth / scale);
|
||||
const artHeight = Math.ceil(deviceHeight / scale);
|
||||
aspect = artWidth / artHeight;
|
||||
renderer.setSize(artWidth, artHeight, false);
|
||||
target.setSize(artWidth, artHeight);
|
||||
(sky.material.uniforms.resolution!.value as Vector2).set(artWidth, artHeight);
|
||||
canvas.style.width = `${(artWidth * scale) / dpr}px`;
|
||||
canvas.style.height = `${(artHeight * scale) / dpr}px`;
|
||||
},
|
||||
setPointer(x, y) {
|
||||
pointer.set(x, y);
|
||||
},
|
||||
setScroll(progress) {
|
||||
scroll = progress;
|
||||
},
|
||||
setFocusRight(right) {
|
||||
focusRight = right;
|
||||
},
|
||||
setRunning(running) {
|
||||
cancelAnimationFrame(frame);
|
||||
last = 0;
|
||||
if (running) frame = requestAnimationFrame(tick);
|
||||
},
|
||||
render() {
|
||||
eased.copy(pointer);
|
||||
render();
|
||||
},
|
||||
dispose() {
|
||||
cancelAnimationFrame(frame);
|
||||
office.dispose();
|
||||
inks.forEach((material) => material.dispose());
|
||||
UNIT_BOX.dispose();
|
||||
sky.geometry.dispose();
|
||||
sky.material.dispose();
|
||||
postQuad.geometry.dispose();
|
||||
quantize.dispose();
|
||||
lights.sun.dispose();
|
||||
lights.sky.dispose();
|
||||
target.dispose();
|
||||
renderer.dispose();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { PixelWorld } from "./PixelWorld";
|
||||
@@ -0,0 +1,51 @@
|
||||
/** Floor plan shared by the office builder and the agents: x runs right, z towards the camera, units are voxels. */
|
||||
export type Point = [x: number, z: number];
|
||||
|
||||
export interface Desk {
|
||||
x: number;
|
||||
z: number;
|
||||
/** +1 puts the chair on the +z side (screen faces the camera), -1 on the -z side. */
|
||||
facing: 1 | -1;
|
||||
}
|
||||
|
||||
export interface Spot {
|
||||
kind: "desk" | "meeting" | "coffee";
|
||||
at: Point;
|
||||
face: number;
|
||||
sit: boolean;
|
||||
exit: Point[];
|
||||
}
|
||||
|
||||
export const FLOOR = { halfX: 10, halfZ: 7, wall: 3.2 };
|
||||
export const CORRIDOR = 1.2;
|
||||
export const CEO_DESK: Desk = { x: -7, z: -4.9, facing: 1 };
|
||||
export const DESKS: Desk[] = [
|
||||
{ x: -2, z: -5, facing: 1 },
|
||||
{ x: 2, z: -5, facing: 1 },
|
||||
{ x: -5.5, z: 4.4, facing: -1 },
|
||||
{ x: -2.5, z: 4.4, facing: -1 },
|
||||
{ x: 2.5, z: 4.4, facing: -1 },
|
||||
{ x: 5.5, z: 4.4, facing: -1 },
|
||||
];
|
||||
export const MEETING_TABLE: Point = [7, -4.2];
|
||||
export const COFFEE_MACHINE: Point = [-9.5, 4.4];
|
||||
export const WINDOWS = [-8.2, -5.4, -1.8, 1.8, 5.4, 8.2];
|
||||
|
||||
export function chairOf(desk: Desk): Point {
|
||||
return [desk.x, desk.z + desk.facing * 0.85];
|
||||
}
|
||||
|
||||
export function deskSpot(desk: Desk): Spot {
|
||||
return { kind: "desk", at: chairOf(desk), face: desk.facing === 1 ? Math.PI : 0, sit: true, exit: [[desk.x, CORRIDOR]] };
|
||||
}
|
||||
|
||||
const [tx, tz] = MEETING_TABLE;
|
||||
const DOOR: Point = [tx, -2.1];
|
||||
export const SHARED_SPOTS: Spot[] = [
|
||||
{ kind: "meeting", at: [tx - 0.8, tz + 1.3], face: Math.PI, sit: true, exit: [DOOR, [tx, CORRIDOR]] },
|
||||
{ kind: "meeting", at: [tx + 0.8, tz + 1.3], face: Math.PI, sit: true, exit: [DOOR, [tx, CORRIDOR]] },
|
||||
{ kind: "meeting", at: [tx - 2.1, tz], face: Math.PI / 2, sit: true, exit: [[tx - 2.1, -2.1], DOOR, [tx, CORRIDOR]] },
|
||||
{ kind: "meeting", at: [tx + 2.1, tz], face: -Math.PI / 2, sit: true, exit: [[tx + 2.1, -2.1], DOOR, [tx, CORRIDOR]] },
|
||||
{ kind: "coffee", at: [-8.5, 4.4], face: -Math.PI / 2, sit: false, exit: [[-8.5, CORRIDOR]] },
|
||||
{ kind: "coffee", at: [-8.5, 6.1], face: -Math.PI / 2, sit: false, exit: [[-7.8, 5.3], [-7.8, CORRIDOR]] },
|
||||
];
|
||||
@@ -0,0 +1,52 @@
|
||||
import { AmbientLight, DirectionalLight, Vector3 } from "three";
|
||||
import { INK, type DayPart } from "../../lib/palette";
|
||||
import type { Office } from "./office";
|
||||
|
||||
interface Light {
|
||||
sun: [number, number, number];
|
||||
sunInk: number;
|
||||
sunPower: number;
|
||||
fill: number;
|
||||
lampInk: number;
|
||||
lamps: number;
|
||||
screens: number;
|
||||
glow: number;
|
||||
}
|
||||
|
||||
const LIGHT: Record<DayPart, Light> = {
|
||||
dawn: { sun: [-0.6, 0.5, 0.62], sunInk: INK.glow, sunPower: 1.6, fill: 2.0, lampInk: INK.glow, lamps: 0, screens: 0.85, glow: 0 },
|
||||
day: { sun: [-0.35, 0.85, 0.4], sunInk: INK.white, sunPower: 1.55, fill: 1.85, lampInk: INK.glow, lamps: 0, screens: 0.75, glow: 0 },
|
||||
dusk: { sun: [0.7, 0.45, 0.55], sunInk: INK.glow, sunPower: 1.5, fill: 2.0, lampInk: INK.glow, lamps: 4, screens: 1, glow: 0.8 },
|
||||
night: { sun: [-0.3, 0.9, 0.3], sunInk: INK.sun, sunPower: 1.2, fill: 3.2, lampInk: INK.gold, lamps: 8, screens: 1, glow: 1.6 },
|
||||
};
|
||||
|
||||
export function createLights(): { sun: DirectionalLight; sky: AmbientLight } {
|
||||
const sun = new DirectionalLight();
|
||||
sun.castShadow = true;
|
||||
sun.shadow.mapSize.set(1024, 1024);
|
||||
sun.shadow.bias = -0.0008;
|
||||
sun.shadow.normalBias = 0.03;
|
||||
Object.assign(sun.shadow.camera, { left: -16, right: 16, top: 16, bottom: -16, near: 1, far: 60 });
|
||||
return { sun, sky: new AmbientLight() };
|
||||
}
|
||||
|
||||
/** Points the sun, tints the ambient fill and switches lamps and screens for a day part. */
|
||||
export function applyLighting(lights: { sun: DirectionalLight; sky: AmbientLight }, office: Office, colours: string[], part: DayPart): void {
|
||||
const light = LIGHT[part];
|
||||
lights.sun.position.copy(new Vector3(...light.sun).normalize().multiplyScalar(24));
|
||||
lights.sun.color.set(colours[light.sunInk]!);
|
||||
lights.sun.intensity = light.sunPower;
|
||||
lights.sky.color.set(colours[INK.white]!);
|
||||
lights.sky.intensity = light.fill;
|
||||
for (const lamp of office.lamps) {
|
||||
lamp.color.set(colours[light.lampInk]!);
|
||||
lamp.intensity = light.lamps;
|
||||
}
|
||||
for (const glow of office.glows) {
|
||||
glow.color.set(colours[INK.screen]!);
|
||||
glow.intensity = light.glow;
|
||||
}
|
||||
office.bulbs.color.set(colours[light.lamps > 0 ? light.lampInk : INK.paper]!);
|
||||
for (const screen of office.screens) screen.color.set(colours[INK.screen]!).multiplyScalar(light.screens);
|
||||
office.steam.color.set(colours[INK.cloud]!);
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
import {
|
||||
BufferGeometry, DataTexture, Float32BufferAttribute, Group, Mesh, MeshBasicMaterial, NearestFilter, PlaneGeometry,
|
||||
PointLight, Points, PointsMaterial, RepeatWrapping, type Object3D,
|
||||
} from "three";
|
||||
import { INK } from "../../lib/palette";
|
||||
import { CEO_DESK, COFFEE_MACHINE, CORRIDOR, DESKS, FLOOR, MEETING_TABLE, SHARED_SPOTS, WINDOWS, chairOf, type Desk, type Point } from "./layout";
|
||||
import { box, type Inks } from "./voxel";
|
||||
|
||||
export interface Office {
|
||||
root: Group;
|
||||
screens: MeshBasicMaterial[];
|
||||
lamps: PointLight[];
|
||||
glows: PointLight[];
|
||||
bulbs: MeshBasicMaterial;
|
||||
steam: PointsMaterial;
|
||||
update(time: number): void;
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
const CODE_ROWS = 32;
|
||||
const STEAM = 7;
|
||||
const LAMPS: Point[] = [[-9.3, -4], [3.4, -1.7], [8.9, 2.4], [-0.1, 6.5]];
|
||||
|
||||
function random(seed: number): () => number {
|
||||
let state = seed;
|
||||
return () => {
|
||||
state = (state * 1664525 + 1013904223) >>> 0;
|
||||
return state / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
function codeTexture(): DataTexture {
|
||||
const width = 8;
|
||||
const data = new Uint8Array(width * CODE_ROWS * 4);
|
||||
const next = random(7);
|
||||
for (let row = 0; row < CODE_ROWS; row++) {
|
||||
const indent = Math.floor(next() * 3);
|
||||
const length = 2 + Math.floor(next() * 5);
|
||||
const level = next() > 0.7 ? 150 : 255;
|
||||
for (let x = 0; x < width; x++) {
|
||||
const on = row % 2 === 0 && x >= indent && x < indent + length;
|
||||
data.fill(on ? level : 36, (row * width + x) * 4, (row * width + x) * 4 + 3);
|
||||
data[(row * width + x) * 4 + 3] = 255;
|
||||
}
|
||||
}
|
||||
const texture = new DataTexture(data, width, CODE_ROWS);
|
||||
texture.magFilter = texture.minFilter = NearestFilter;
|
||||
texture.wrapS = texture.wrapT = RepeatWrapping;
|
||||
texture.repeat.set(1, 5 / CODE_ROWS);
|
||||
texture.needsUpdate = true;
|
||||
return texture;
|
||||
}
|
||||
|
||||
/** Builds a piece locally (facing +z) and drops it into the room at (x, z) turned to `face`. */
|
||||
function placed(parent: Object3D, x: number, z: number, face: number): Group {
|
||||
const group = new Group();
|
||||
group.position.set(x, 0, z);
|
||||
group.rotation.y = face;
|
||||
parent.add(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
function chair(parent: Object3D, inks: Inks, [x, z]: Point, face: number, seat: number = INK.accentDark): void {
|
||||
const g = placed(parent, x, z, face);
|
||||
box(g, inks[INK.inkSoft]!, 0.08, 0.38, 0.08, 0, 0, 0);
|
||||
box(g, inks[seat]!, 0.52, 0.1, 0.5, 0, 0.38, 0);
|
||||
box(g, inks[seat]!, 0.52, 0.42, 0.1, 0, 0.48, -0.24);
|
||||
}
|
||||
|
||||
function plant(parent: Object3D, inks: Inks, x: number, z: number, scale = 1): void {
|
||||
const g = placed(parent, x, z, 0);
|
||||
g.scale.setScalar(scale);
|
||||
box(g, inks[INK.brick]!, 0.5, 0.42, 0.5, 0, 0, 0);
|
||||
box(g, inks[INK.woodDark]!, 0.42, 0.04, 0.42, 0, 0.42, 0);
|
||||
box(g, inks[INK.leaf0]!, 0.64, 0.42, 0.64, 0, 0.46, 0);
|
||||
box(g, inks[INK.leaf1]!, 0.5, 0.36, 0.5, 0.06, 0.82, 0.04);
|
||||
box(g, inks[INK.leaf2]!, 0.28, 0.26, 0.28, -0.08, 1.12, 0.1);
|
||||
box(g, inks[INK.bloom]!, 0.12, 0.12, 0.12, 0.26, 0.74, 0.26);
|
||||
box(g, inks[INK.bloom]!, 0.12, 0.12, 0.12, -0.2, 1.0, 0.24);
|
||||
}
|
||||
|
||||
function desk(parent: Object3D, inks: Inks, d: Desk, screenGeometry: PlaneGeometry, screen: MeshBasicMaterial, ceo = false): void {
|
||||
const g = placed(parent, d.x, d.z, d.facing === 1 ? 0 : Math.PI);
|
||||
const width = ceo ? 2.3 : 1.6;
|
||||
const top = inks[ceo ? INK.woodDark : INK.wood]!;
|
||||
for (const sx of [-1, 1]) for (const sz of [-1, 1]) box(g, inks[INK.woodDark]!, 0.08, 0.7, 0.08, sx * (width / 2 - 0.1), 0, sz * 0.34);
|
||||
box(g, top, width, 0.08, 0.9, 0, 0.7, 0);
|
||||
if (ceo) {
|
||||
box(g, inks[INK.woodDark]!, width, 0.55, 0.06, 0, 0.15, 0.4);
|
||||
box(g, inks[INK.gold]!, width + 0.02, 0.06, 0.92, 0, 0.72, 0);
|
||||
box(g, inks[INK.gold]!, 0.14, 0.12, 0.14, 0.85, 0.78, 0.1);
|
||||
box(g, inks[INK.gold]!, 0.22, 0.14, 0.22, 0.85, 0.9, 0.1);
|
||||
box(g, inks[INK.gold]!, 0.5, 0.1, 0.06, -0.6, 0.78, 0.32);
|
||||
}
|
||||
box(g, inks[INK.inkSoft]!, 0.3, 0.03, 0.2, 0, 0.78, -0.2);
|
||||
box(g, inks[INK.inkSoft]!, 0.08, 0.16, 0.08, 0, 0.8, -0.22);
|
||||
box(g, inks[INK.ink]!, 0.84, 0.52, 0.06, 0, 0.92, -0.2);
|
||||
const panel = new Mesh(screenGeometry, screen);
|
||||
panel.position.set(0, 1.18, -0.165);
|
||||
g.add(panel);
|
||||
box(g, inks[INK.stoneLight]!, 0.5, 0.03, 0.16, 0, 0.78, 0.16);
|
||||
box(g, inks[INK.accent]!, 0.09, 0.11, 0.09, width / 2 - 0.22, 0.78, 0.12);
|
||||
chair(parent, inks, chairOf(d), d.facing === 1 ? Math.PI : 0, ceo ? INK.accent : INK.accentDark);
|
||||
}
|
||||
|
||||
function wallRun(parent: Object3D, inks: Inks, [x1, z1]: Point, [x2, z2]: Point, height: number, thick: number, body: number = INK.paper): void {
|
||||
const w = Math.max(Math.abs(x2 - x1), thick);
|
||||
const d = Math.max(Math.abs(z2 - z1), thick);
|
||||
box(parent, inks[body]!, w, height, d, (x1 + x2) / 2, 0, (z1 + z2) / 2);
|
||||
box(parent, inks[INK.inkSoft]!, w + 0.04, 0.1, d + 0.04, (x1 + x2) / 2, height, (z1 + z2) / 2);
|
||||
}
|
||||
|
||||
function shell(root: Object3D, inks: Inks): void {
|
||||
const { halfX, halfZ, wall } = FLOOR;
|
||||
const W = halfX * 2 + 0.6;
|
||||
const D = halfZ * 2 + 0.6;
|
||||
box(root, inks[INK.stone]!, W, 1.1, D, 0, -1.6, 0);
|
||||
box(root, inks[INK.brick]!, W, 0.4, D, 0, -0.5, 0);
|
||||
box(root, inks[INK.wood]!, W, 0.1, D, 0, -0.1, 0);
|
||||
for (let x = -halfX + 2; x < halfX; x += 2) box(root, inks[INK.woodDark]!, 0.1, 0.01, halfZ * 2, x, 0, 0);
|
||||
box(root, inks[INK.berry]!, 5.6, 0.03, 5.6, -7, 0, -4);
|
||||
box(root, inks[INK.stoneLight]!, 5.6, 0.03, 5.6, 7, 0, -4);
|
||||
box(root, inks[INK.rug]!, halfX * 2 - 1.4, 0.03, 1.1, 0, 0, CORRIDOR);
|
||||
|
||||
const backZ = -halfZ - 0.15;
|
||||
box(root, inks[INK.paper]!, W, 0.9, 0.3, 0, 0, backZ);
|
||||
box(root, inks[INK.paper]!, W, wall - 2.6, 0.3, 0, 2.6, backZ);
|
||||
box(root, inks[INK.inkSoft]!, W + 0.04, 0.12, 0.34, 0, wall, backZ);
|
||||
const edges = [-W / 2, ...WINDOWS.flatMap((c) => [c - 1.1, c + 1.1]), W / 2];
|
||||
for (let i = 0; i < edges.length; i += 2) box(root, inks[INK.paper]!, edges[i + 1]! - edges[i]!, 1.7, 0.3, (edges[i]! + edges[i + 1]!) / 2, 0.9, backZ);
|
||||
for (const c of WINDOWS) {
|
||||
box(root, inks[INK.white]!, 2.3, 0.1, 0.5, c, 0.82, backZ + 0.05);
|
||||
box(root, inks[INK.white]!, 0.08, 1.7, 0.14, c, 0.9, backZ);
|
||||
box(root, inks[INK.white]!, 2.2, 0.08, 0.14, c, 1.75, backZ);
|
||||
}
|
||||
const leftX = -halfX - 0.15;
|
||||
box(root, inks[INK.paper]!, 0.3, wall, D, leftX, 0, 0);
|
||||
box(root, inks[INK.inkSoft]!, 0.34, 0.12, D + 0.04, leftX, wall, 0);
|
||||
box(root, inks[INK.stone]!, 0.04, 0.16, D, leftX + 0.17, 0, 0);
|
||||
box(root, inks[INK.stone]!, W, 0.16, 0.04, 0, 0, backZ + 0.17);
|
||||
|
||||
const low = (a: Point, b: Point) => wallRun(root, inks, a, b, 0.9, 0.16, INK.stoneLight);
|
||||
low([-halfX, -1], [-7.9, -1]);
|
||||
low([-6.1, -1], [-4, -1]);
|
||||
low([-4, -halfZ], [-4, -1]);
|
||||
low([4, -1], [6.1, -1]);
|
||||
low([7.9, -1], [halfX, -1]);
|
||||
low([4, -halfZ], [4, -1]);
|
||||
}
|
||||
|
||||
function furnish(root: Object3D, inks: Inks, bulbs: MeshBasicMaterial): void {
|
||||
const shelf = placed(root, -9.72, -6.0, Math.PI / 2);
|
||||
box(shelf, inks[INK.woodDark]!, 1.8, 2.1, 0.5, 0, 0, 0);
|
||||
const books = [INK.accent, INK.sea, INK.gold, INK.berry, INK.mint, INK.rug, INK.paper, INK.accentDark];
|
||||
const next = random(3);
|
||||
for (const y of [0.12, 0.8, 1.45]) {
|
||||
let x = -0.78;
|
||||
while (x < 0.7) {
|
||||
const w = 0.12 + next() * 0.1;
|
||||
box(shelf, inks[books[Math.floor(next() * books.length)]!]!, w - 0.02, 0.36 + next() * 0.14, 0.4, x + w / 2, y, 0.08);
|
||||
x += w;
|
||||
}
|
||||
}
|
||||
const board = placed(root, -9.97, 1.6, Math.PI / 2);
|
||||
box(board, inks[INK.stone]!, 2.6, 1.4, 0.06, 0, 1.05, 0);
|
||||
box(board, inks[INK.white]!, 2.44, 1.24, 0.08, 0, 1.13, 0);
|
||||
[INK.gold, INK.bloom, INK.mint, INK.sea, INK.gold].forEach((ink, i) => box(board, inks[ink]!, 0.24, 0.24, 0.1, -0.9 + i * 0.42, 1.65 - (i % 2) * 0.4, 0));
|
||||
box(board, inks[INK.accent]!, 1.2, 0.06, 0.1, 0.3, 1.35, 0);
|
||||
box(board, inks[INK.ink]!, 0.8, 0.06, 0.1, 0.1, 1.25, 0);
|
||||
|
||||
const [cx, cz] = COFFEE_MACHINE;
|
||||
box(root, inks[INK.woodDark]!, 0.9, 0.9, 1.5, cx, 0, cz);
|
||||
box(root, inks[INK.wood]!, 0.96, 0.06, 1.56, cx, 0.9, cz);
|
||||
box(root, inks[INK.ink]!, 0.5, 0.7, 0.55, cx - 0.1, 0.96, cz - 0.1);
|
||||
box(root, inks[INK.stone]!, 0.2, 0.1, 0.24, cx + 0.2, 1.32, cz - 0.1);
|
||||
box(root, inks[INK.mint]!, 0.04, 0.08, 0.08, cx + 0.16, 1.5, cz + 0.08);
|
||||
box(root, inks[INK.white]!, 0.12, 0.14, 0.12, cx + 0.24, 0.96, cz - 0.1);
|
||||
box(root, inks[INK.accent]!, 0.12, 0.12, 0.12, cx + 0.1, 0.96, cz + 0.45);
|
||||
box(root, inks[INK.stoneLight]!, 0.44, 0.9, 0.44, cx, 0, 6.1);
|
||||
box(root, inks[INK.sea]!, 0.34, 0.46, 0.34, cx, 0.9, 6.1);
|
||||
|
||||
const [tx, tz] = MEETING_TABLE;
|
||||
for (const sx of [-1, 1]) box(root, inks[INK.woodDark]!, 0.16, 0.7, 0.6, tx + sx * 1.1, 0, tz);
|
||||
box(root, inks[INK.wood]!, 3.0, 0.1, 1.5, tx, 0.7, tz);
|
||||
box(root, inks[INK.paper]!, 0.3, 0.02, 0.4, tx - 0.6, 0.8, tz + 0.2);
|
||||
box(root, inks[INK.paper]!, 0.3, 0.02, 0.4, tx + 0.7, 0.8, tz - 0.1);
|
||||
box(root, inks[INK.gold]!, 0.12, 0.12, 0.12, tx, 0.8, tz);
|
||||
for (const spot of SHARED_SPOTS.filter((s) => s.kind === "meeting")) chair(root, inks, spot.at, spot.face, INK.sea);
|
||||
|
||||
for (const [x, z] of [[-4.6, -6.5], [-3.4, -6.5], [3.4, -6.5], [9.4, -6.5], [9.4, 6.5], [-9.4, -1.6]] as Point[]) plant(root, inks, x, z);
|
||||
|
||||
const tree = placed(root, 12.1, 5.2, 0);
|
||||
box(tree, inks[INK.woodDark]!, 0.4, 1.6, 0.4, 0, 0, 0);
|
||||
box(tree, inks[INK.leaf0]!, 2.0, 1.1, 2.0, 0, 1.2, 0);
|
||||
box(tree, inks[INK.leaf1]!, 1.6, 0.9, 1.6, 0.1, 2.1, 0.1);
|
||||
box(tree, inks[INK.leaf2]!, 0.9, 0.6, 0.9, -0.1, 2.9, 0.2);
|
||||
box(tree, inks[INK.leaf1]!, 0.8, 0.7, 0.8, 0.9, 1.5, 0.6);
|
||||
for (const [x, y, z] of [[0.7, 1.9, 0.85], [-0.6, 2.4, 0.7], [0.3, 3.0, 0.6], [-0.95, 1.5, 0.4]]) box(tree, inks[INK.bloom]!, 0.16, 0.16, 0.16, x!, y!, z!);
|
||||
const bench = placed(root, 11.9, 2.6, -Math.PI / 2);
|
||||
box(bench, inks[INK.inkSoft]!, 0.1, 0.4, 0.4, -0.6, 0, 0);
|
||||
box(bench, inks[INK.inkSoft]!, 0.1, 0.4, 0.4, 0.6, 0, 0);
|
||||
box(bench, inks[INK.wood]!, 1.5, 0.1, 0.5, 0, 0.4, 0);
|
||||
box(bench, inks[INK.wood]!, 1.5, 0.4, 0.08, 0, 0.5, -0.22);
|
||||
box(root, inks[INK.leaf1]!, 3.2, 0.12, 6.2, 11.9, -0.1, 4.2);
|
||||
box(root, inks[INK.woodDark]!, 3.2, 1.5, 6.2, 11.9, -1.6, 4.2);
|
||||
|
||||
for (const [x, z] of LAMPS) {
|
||||
const lamp = placed(root, x, z, 0);
|
||||
box(lamp, inks[INK.inkSoft]!, 0.36, 0.06, 0.36, 0, 0, 0);
|
||||
box(lamp, inks[INK.inkSoft]!, 0.06, 1.6, 0.06, 0, 0, 0);
|
||||
box(lamp, bulbs, 0.44, 0.32, 0.44, 0, 1.6, 0).castShadow = false;
|
||||
}
|
||||
}
|
||||
|
||||
export function buildOffice(inks: Inks): Office {
|
||||
const root = new Group();
|
||||
const code = codeTexture();
|
||||
const screenGeometry = new PlaneGeometry(0.74, 0.44);
|
||||
const screens: MeshBasicMaterial[] = [];
|
||||
const bulbs = new MeshBasicMaterial();
|
||||
shell(root, inks);
|
||||
for (const d of [CEO_DESK, ...DESKS]) {
|
||||
const map = code.clone();
|
||||
map.offset.y = screens.length * 6 / CODE_ROWS;
|
||||
const screen = new MeshBasicMaterial({ map });
|
||||
screens.push(screen);
|
||||
desk(root, inks, d, screenGeometry, screen, d === CEO_DESK);
|
||||
}
|
||||
furnish(root, inks, bulbs);
|
||||
|
||||
const lamps = LAMPS.map(([x, z]) => {
|
||||
const light = new PointLight(0xffffff, 0, 7, 1.4);
|
||||
light.position.set(x, 1.6, z);
|
||||
root.add(light);
|
||||
return light;
|
||||
});
|
||||
|
||||
const glows = [CEO_DESK, ...DESKS].map((d) => {
|
||||
const light = new PointLight(0xffffff, 0, 2.8, 1.6);
|
||||
light.position.set(d.x, 1.2, d.z + d.facing * 0.3);
|
||||
root.add(light);
|
||||
return light;
|
||||
});
|
||||
|
||||
const steamGeometry = new BufferGeometry();
|
||||
steamGeometry.setAttribute("position", new Float32BufferAttribute(new Float32Array(STEAM * 3), 3));
|
||||
const steam = new PointsMaterial({ size: 1, sizeAttenuation: false });
|
||||
const puffs = new Points(steamGeometry, steam);
|
||||
puffs.frustumCulled = false;
|
||||
root.add(puffs);
|
||||
const [cx, cz] = COFFEE_MACHINE;
|
||||
|
||||
return {
|
||||
root,
|
||||
screens,
|
||||
lamps,
|
||||
glows,
|
||||
bulbs,
|
||||
steam,
|
||||
update(time) {
|
||||
screens.forEach((screen, i) => {
|
||||
screen.map!.offset.y = (Math.floor(time * (2 + (i % 3)) + i * 5) * 2) / CODE_ROWS;
|
||||
});
|
||||
const position = steamGeometry.attributes.position!;
|
||||
for (let i = 0; i < STEAM; i++) {
|
||||
const life = (time * 0.45 + i / STEAM) % 1;
|
||||
position.setXYZ(i, cx + 0.24 + Math.sin(life * 9 + i) * 0.08, 1.15 + life * 0.9, cz - 0.1 + Math.cos(life * 7 + i) * 0.06);
|
||||
}
|
||||
position.needsUpdate = true;
|
||||
},
|
||||
dispose() {
|
||||
steamGeometry.dispose();
|
||||
screens.forEach((screen) => {
|
||||
screen.map!.dispose();
|
||||
screen.dispose();
|
||||
});
|
||||
code.dispose();
|
||||
screenGeometry.dispose();
|
||||
bulbs.dispose();
|
||||
steam.dispose();
|
||||
[...lamps, ...glows].forEach((light) => light.dispose());
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { ShaderMaterial, Vector3, type Texture } from "three";
|
||||
import { PALETTE_SIZE } from "../../lib/palette";
|
||||
|
||||
/** Snaps every scene pixel to the two nearest palette colours, choosing between them with a 4x4 Bayer matrix. */
|
||||
export function createQuantizeMaterial(scene: Texture): ShaderMaterial {
|
||||
return new ShaderMaterial({
|
||||
uniforms: {
|
||||
tScene: { value: scene },
|
||||
palette: { value: Array.from({ length: PALETTE_SIZE }, () => new Vector3()) },
|
||||
},
|
||||
depthTest: false,
|
||||
depthWrite: false,
|
||||
vertexShader: /* glsl */ `
|
||||
varying vec2 vUv;
|
||||
void main() {
|
||||
vUv = uv;
|
||||
gl_Position = vec4(position.xy, 0.0, 1.0);
|
||||
}
|
||||
`,
|
||||
fragmentShader: /* glsl */ `
|
||||
uniform sampler2D tScene;
|
||||
uniform vec3 palette[${PALETTE_SIZE}];
|
||||
varying vec2 vUv;
|
||||
const float BAYER[16] = float[16](0., 8., 2., 10., 12., 4., 14., 6., 3., 11., 1., 9., 15., 7., 13., 5.);
|
||||
const vec3 WEIGHT = vec3(0.55, 0.75, 0.4);
|
||||
|
||||
vec3 toSrgb(vec3 c) {
|
||||
c = clamp(c, 0.0, 1.0);
|
||||
return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c));
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 colour = toSrgb(texture2D(tScene, vUv).rgb);
|
||||
float best = 1e9;
|
||||
float next = 1e9;
|
||||
vec3 first = palette[0];
|
||||
vec3 second = palette[0];
|
||||
for (int i = 0; i < ${PALETTE_SIZE}; i++) {
|
||||
vec3 delta = (palette[i] - colour) * WEIGHT;
|
||||
float distance = dot(delta, delta);
|
||||
if (distance < best) {
|
||||
next = best; second = first;
|
||||
best = distance; first = palette[i];
|
||||
} else if (distance < next) {
|
||||
next = distance; second = palette[i];
|
||||
}
|
||||
}
|
||||
vec3 span = second - first;
|
||||
float along = clamp(dot(colour - first, span) / max(dot(span, span), 1e-5) * 2.0 - 0.45, 0.0, 1.0);
|
||||
ivec2 cell = ivec2(mod(gl_FragCoord.xy, 4.0));
|
||||
float threshold = (BAYER[cell.y * 4 + cell.x] + 0.5) / 16.0;
|
||||
gl_FragColor = vec4(along > threshold ? second : first, 1.0);
|
||||
}
|
||||
`,
|
||||
});
|
||||
}
|
||||
|
||||
export function setQuantizePalette(material: ShaderMaterial, colours: string[]): void {
|
||||
const slots = material.uniforms.palette!.value as Vector3[];
|
||||
colours.forEach((hex, index) => {
|
||||
const value = Number.parseInt(hex.slice(1), 16);
|
||||
slots[index]!.set(((value >> 16) & 255) / 255, ((value >> 8) & 255) / 255, (value & 255) / 255);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import { Color, Mesh, PlaneGeometry, ShaderMaterial, Vector2 } from "three";
|
||||
import { INK, type DayPart } from "../../lib/palette";
|
||||
|
||||
const ORB: Record<DayPart, [number, number]> = { dawn: [0.16, 0.44], day: [0.18, 0.8], dusk: [0.84, 0.4], night: [0.8, 0.8] };
|
||||
|
||||
/** Screen-space backdrop: banded sky, sun or moon, drifting clouds, stars and a two-layer skyline with lit windows. */
|
||||
export function createSky(): Mesh<PlaneGeometry, ShaderMaterial> {
|
||||
const colour = () => ({ value: new Color() });
|
||||
const material = new ShaderMaterial({
|
||||
uniforms: {
|
||||
sky0: colour(), sky1: colour(), sky2: colour(), sky3: colour(), orb: colour(), star: colour(),
|
||||
cloud: colour(), far: colour(), near: colour(), lit: colour(),
|
||||
night: { value: 0 }, moon: { value: 0 }, time: { value: 0 },
|
||||
resolution: { value: new Vector2(320, 180) }, orbPos: { value: new Vector2() }, offset: { value: new Vector2() },
|
||||
},
|
||||
depthTest: false,
|
||||
depthWrite: false,
|
||||
vertexShader: /* glsl */ `
|
||||
void main() { gl_Position = vec4(position.xy, 1.0, 1.0); }
|
||||
`,
|
||||
fragmentShader: /* glsl */ `
|
||||
uniform vec3 sky0, sky1, sky2, sky3, orb, star, cloud, far, near, lit;
|
||||
uniform float night, moon, time;
|
||||
uniform vec2 resolution, orbPos, offset;
|
||||
|
||||
const float BAYER[16] = float[16](0., 8., 2., 10., 12., 4., 14., 6., 3., 11., 1., 9., 15., 7., 13., 5.);
|
||||
|
||||
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
|
||||
|
||||
vec3 band(vec3 low, vec3 high, float t, float threshold) {
|
||||
return clamp((t - 0.5) * 2.2 + 0.5, 0.0, 1.0) > threshold ? high : low;
|
||||
}
|
||||
|
||||
bool cloudAt(vec2 px, float k) {
|
||||
float span = resolution.x + 80.0;
|
||||
vec2 c = vec2(mod(k * 137.0 + time * (1.5 + k * 0.6), span) - 40.0, resolution.y * (0.6 + 0.09 * k));
|
||||
if (px.y < c.y - 1.0) return false;
|
||||
vec2 d = px - c;
|
||||
return length(d * vec2(0.55, 1.0)) < 5.0 + k || length(d - vec2(8.0, -1.0)) < 4.0 || length(d + vec2(9.0, 1.0)) < 3.5;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 px = floor(gl_FragCoord.xy);
|
||||
float v = px.y / resolution.y;
|
||||
ivec2 cell4 = ivec2(mod(px, 4.0));
|
||||
float threshold = (BAYER[cell4.y * 4 + cell4.x] + 0.5) / 16.0;
|
||||
vec3 c = v > 0.74 ? band(sky1, sky0, (v - 0.74) / 0.26, threshold)
|
||||
: v > 0.48 ? band(sky2, sky1, (v - 0.48) / 0.26, threshold)
|
||||
: band(sky3, sky2, clamp((v - 0.2) / 0.28, 0.0, 1.0), threshold);
|
||||
|
||||
float h = hash(px + floor(offset * 0.3));
|
||||
if (moon > 0.5 && v > 0.42 && h > 0.996) c = mix(c, star, step(0.35, fract(h * 97.0 + time * 0.4)));
|
||||
|
||||
for (float k = 0.0; k < 3.0; k++) if (cloudAt(px + floor(offset * 0.25), k)) c = cloud;
|
||||
|
||||
vec2 o = orbPos * resolution + floor(offset * 0.2);
|
||||
float d = length(px + 0.5 - o);
|
||||
if (moon > 0.5) {
|
||||
if (d < 7.0 && length(px + 0.5 - o - vec2(3.0, 2.0)) > 6.0) c = orb;
|
||||
} else {
|
||||
if (d < 14.0 && threshold < 0.3) c = orb;
|
||||
if (d < 9.0) c = orb;
|
||||
}
|
||||
|
||||
float unit = min(resolution.y, resolution.x * 0.9);
|
||||
float fx = px.x + floor(offset.x * 0.35);
|
||||
float fid = floor(fx / 9.0);
|
||||
if (px.y < unit * (0.2 + hash(vec2(fid, 3.0)) * 0.16)) {
|
||||
c = far;
|
||||
vec2 cell = vec2(mod(fx, 9.0), mod(px.y, 5.0));
|
||||
if (night > 0.5 && cell.x > 2.0 && cell.x < 7.0 && cell.y == 2.0 && hash(vec2(fid, floor(px.y / 5.0))) > 0.7) c = lit;
|
||||
}
|
||||
float nx = px.x + floor(offset.x * 0.6);
|
||||
float nid = floor(nx / 13.0);
|
||||
float top = unit * (0.08 + hash(vec2(nid, 9.0)) * 0.15);
|
||||
if (px.y < top) {
|
||||
c = near;
|
||||
vec2 cell = vec2(mod(nx, 13.0), mod(px.y, 4.0));
|
||||
bool window = cell.x > 1.0 && cell.x < 11.0 && mod(cell.x, 3.0) == 2.0 && cell.y == 1.0 && px.y < top - 2.0;
|
||||
if (window) c = night > 0.5 && hash(vec2(nx, floor(px.y / 4.0))) > 0.45 ? lit : mix(near, sky3, 0.35);
|
||||
}
|
||||
gl_FragColor = vec4(c, 1.0);
|
||||
}
|
||||
`,
|
||||
});
|
||||
const mesh = new Mesh(new PlaneGeometry(2, 2), material);
|
||||
mesh.frustumCulled = false;
|
||||
mesh.renderOrder = -1;
|
||||
return mesh;
|
||||
}
|
||||
|
||||
export function setSkyPalette(material: ShaderMaterial, colours: string[], part: DayPart): void {
|
||||
const u = material.uniforms;
|
||||
const set = (name: string, index: number) => (u[name]!.value as Color).set(colours[index]!);
|
||||
set("sky0", INK.sky0); set("sky1", INK.sky1); set("sky2", INK.sky2); set("sky3", INK.sky3);
|
||||
set("orb", INK.sun); set("star", INK.star); set("cloud", INK.cloud); set("lit", INK.gold);
|
||||
(u.far!.value as Color).set(colours[INK.sky1]!).lerp(new Color(colours[INK.stone]!), 0.5);
|
||||
(u.near!.value as Color).set(colours[INK.inkSoft]!).lerp(new Color(colours[INK.sky0]!), 0.3);
|
||||
u.night!.value = part === "night" || part === "dusk" ? 1 : 0;
|
||||
u.moon!.value = part === "night" ? 1 : 0;
|
||||
(u.orbPos!.value as Vector2).set(...ORB[part]);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { BoxGeometry, Mesh, MeshLambertMaterial, type Material, type Object3D } from "three";
|
||||
import { PALETTE_SIZE } from "../../lib/palette";
|
||||
|
||||
export const UNIT_BOX = new BoxGeometry(1, 1, 1);
|
||||
|
||||
/** One Lambert material per palette index, recoloured in place whenever the palette rotates. */
|
||||
export type Inks = MeshLambertMaterial[];
|
||||
|
||||
export function createInks(): Inks {
|
||||
return Array.from({ length: PALETTE_SIZE }, () => new MeshLambertMaterial());
|
||||
}
|
||||
|
||||
export function paintInks(inks: Inks, colours: string[]): void {
|
||||
inks.forEach((material, index) => material.color.set(colours[index]!));
|
||||
}
|
||||
|
||||
/** Adds a w×h×d box whose bottom sits at y. */
|
||||
export function box(parent: Object3D, material: Material, w: number, h: number, d: number, x: number, y: number, z: number): Mesh {
|
||||
const mesh = new Mesh(UNIT_BOX, material);
|
||||
mesh.scale.set(w, h, d);
|
||||
mesh.position.set(x, y + h / 2, z);
|
||||
mesh.castShadow = true;
|
||||
mesh.receiveShadow = true;
|
||||
parent.add(mesh);
|
||||
return mesh;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
.frame {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
background: var(--ink-sky-2);
|
||||
}
|
||||
|
||||
.canvas {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
display: block;
|
||||
transform: translate(-50%, -50%);
|
||||
image-rendering: pixelated;
|
||||
}
|
||||
|
||||
.fallback {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.band {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.skyline {
|
||||
position: absolute;
|
||||
inset: auto 0 0;
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
height: 60%;
|
||||
}
|
||||
|
||||
.tower {
|
||||
--tower: var(--ink-ink-soft);
|
||||
flex: 1;
|
||||
background-color: var(--tower);
|
||||
background-image: repeating-linear-gradient(0deg, var(--tower) 0 10px, transparent 10px 16px),
|
||||
repeating-linear-gradient(90deg, var(--tower) 0 8px, var(--ink-gold) 8px 14px, var(--tower) 14px 20px);
|
||||
box-shadow: inset 0 6px 0 var(--ink-ink);
|
||||
}
|
||||
|
||||
.tower:nth-child(odd) {
|
||||
--tower: var(--ink-stone);
|
||||
}
|
||||
Reference in new issue
Block a user