BUILD REQUEST - PL-187 Eroded Worlds - please build this now I'm sending you this as my request: please build the one-file HTML app described below. Start now and do the whole build in your answer to this message. - This is a build job, not a document to summarize, review or discuss. Don't ask me anything first: if a detail is unclear, pick the most sensible option and keep going. - The spec is everything between the BEGIN SPEC and END SPEC lines. Follow all of it exactly. - If your tool can create files, also save the finished file as PL-187-eroded-worlds.html. ===== BEGIN SPEC ===== ERODED WORLDS: Raindrops Carve Rivers and Valleys into a 128 x 128 Mountain Island, with a Flyover HOOK OBJECT: window.__erode Build "ERODED WORLDS": a Three.js 3D terrain on a 128 x 128 height grid held in typed arrays. A seeded noise mountain island is carved by real droplet erosion (every droplet rolls downhill, picks up sediment where it is fast and drops it where it slows), while a camera flies a slow loop around it. Rivers show up where many droplets ran, valleys deepen, and sediment fans pile up at the foot of the slopes. It is fun to keep watching because every valley is grown by the simulation, not drawn. LOOK - Style: dusk-lit miniature diorama. Page background is a vertical gradient #1B2B4A (top) to #F0B48A (bottom) behind a transparent WebGL canvas. Terrain vertex colors by height h (0 to 40), hard bands with no blending and no baked lighting: h < 4 water-bed #1B5C8C; 4 <= h < 6 sand #D9C58C; 6 <= h < 20 grass #4F8F3C; 20 <= h < 32 rock #7A6E63; h >= 32 snow #F4F7FA. A cell whose visit count (see EROSION) is 600 or more is drawn river #3FA7D6 instead of its band color. A flat sea plane at y = 4, color #2B7FB8, opacity 0.7. HUD bar #0E1626 with text #FFE8C2. - Subject: one terrain mesh of 128 x 128 vertices (127 x 127 quads). World x and z run 0 to 127 (1 unit per cell), y is the height (0 to 40). Heights live in a Float32Array of 16384 values, index = z * 128 + x. Vertex normals are recomputed after every change. Lights: directional from (-60, 80, 40) intensity 1.1, hemisphere light sky #9FB6D6 ground #3A3028 intensity 0.5. - Initial terrain (deterministic, seed 360): 5 octaves of value noise; octave o (0 to 4) has a lattice of 12 x 2^o cells across the grid, a value in [0,1) at every lattice point from a seeded integer hash, smoothstep interpolation, and amplitude 0.5^o. Sum the octaves, multiply by max(0, 1 - 0.9 r^2) where r = distance from (63.5, 63.5) divided by 63.5 (so the corners and edges drop to the lowest level and a ring of sea and sand surrounds the mountains), then rescale linearly so the lowest cell is exactly 0 and the highest exactly 40. - Layout: a HUD bar 40 px high at the top (two lines of 14 px monospace, about 8.4 px per character: "DROPLETS 57,600 | RIVER CELLS 874" is 33 characters = 277 px, and "ERODED 27,052 | LOST 3,688" is 26 characters = 218 px, both under the 374 px available at 390 px wide). The WebGL canvas fills the window below the bar (aspect = width / (height - 40)). Two buttons RUN and RESET, each at least 56 x 56 CSS px: in landscape stacked at the right edge (x from width - 72 to width - 16, y from 80 and from 152), in portrait side by side near the bottom (y from height - 80, x from 16 and from 88). - Camera: perspective, vertical field of view 40 degrees, looking at (63.5, 12, 63.5). Position = target + D * (cos(el) sin(az), sin(el), cos(el) cos(az)) with az and el in degrees. Default az 35, el 28, D 240 when width >= height, otherwise D 560. - FIRST FRAME (t = 0, no input): the pristine terrain (no droplet has run, running() is false) seen from the default camera: a mountain island with a clear ring of sea and sand around it (about 28% of the cells are below h = 4 and 6% are sand). The projected box of the 8 corners of x 0 to 127, y 0 to 40, z 0 to 127 is about 69% of the window height at 1280x720 (493 px tall, starting 177 px from the top, so well clear of the HUD bar). On a phone in portrait the island is wide, so the same box is only about 28% of the height but 92% of the width: that is an accepted exception. The HUD bar shows all zeros and nothing overlaps the terrain. EROSION (exact rules; each droplet is simulated to its end inside one call) - A droplet starts at a seeded random position (x, z), each uniform in [0, 126.999), with direction (0, 0), speed 1, water 1, sediment 0. Then for up to 48 steps: 1. cell (cx, cz) = floor of the position, fractions (fx, fz); visits[cz * 128 + cx] += 1 (a Uint32Array of 16384). 2. Height hh and gradient (gx, gz) by bilinear interpolation of the 4 corner heights of that cell. 3. direction = direction * 0.05 - gradient * 0.95; if its length is 0 pick a random direction; normalize it; new position = position + direction. 4. If the new position leaves 0 <= x < 127 or 0 <= z < 127 the droplet is lost: lost += sediment; the droplet ends. 5. dh = (bilinear height at the new position) - hh. capacity = max(-dh, 0.01) * speed * water * 0.12. 6. If sediment > capacity or dh > 0: deposit amount = (dh > 0) ? min(dh, sediment) : (sediment - capacity) * 0.3, added to the 4 corners of the OLD cell with the bilinear weights (so the grid gains exactly that amount); sediment -= amount; deposited += amount. Otherwise erode: amount = min((capacity - sediment) * 0.3, -dh); take amount * weight from each of the 4 corners of the OLD cell but never more than that corner holds (never below 0); what was really taken is added to sediment and to eroded. 7. speed = sqrt(max(0, speed^2 + (-dh) * 4)); water = water * 0.98; position = new position. - If the droplet is still on the grid after its last step, its remaining sediment is deposited at its final position (bilinear into the 4 corners of that cell; deposited += sediment). Every finished droplet counts: droplets += 1. - So the grid never gains or loses height except through lost: sum of heights = initial sum - lost, always. MOTION / PLAY - Controls: drag (mouse or touch, pointer events on the canvas) orbits: dragging right by dx px gives az = az - 0.3 dx; dragging down by dy px gives el = el + 0.3 dy; el is clamped to 5 to 80. Mouse wheel (and two-finger pinch on touch) zooms: D = clamp(D * 1.1^(deltaY / 100), 120, 700). Space or the RUN button toggles live erosion; R or the RESET button restores the pristine terrain, all counters, the visit counts and the random sequence. - Live erosion: while running, every 1/60 s tick runs 40 droplets and then refreshes the mesh heights, vertex colors (including the river overlay) and normals. That is 2,400 droplets per second and must not stall the frame. - ATTRACT MODE: with no input, a hands-free demo starts on load and loops every 28 s: 2.0 s of the pristine island, then 24.0 s of live erosion (24.0 x 60 x 40 = 57,600 droplets), then 2.0 s of the carved island with erosion stopped (2.0 + 24.0 + 2.0 = 28.0 s). The camera flies the whole 28 s: az = 35 + 360 * t / 28, el = 28 + 10 * sin(2 pi t / 28), D at its default (t is the loop time in seconds, so the loop closes smoothly). At 28.0 s the terrain, visit counts, counters and random sequence reset to the t = 0 state and the loop restarts. Any input takes over (the schedule and the flyover stop); the demo restarts from the t = 0 state after 8 s with no input. Hook calls are not user input. - The hook also exposes: erode(n) (runs n droplets right now, then refreshes the mesh, colors and normals), reset() (same as R), running(), attract(on) (false turns the demo off), heights() (the live Float32Array), heightAt(i, j) (grid value at x = i, z = j), meshHeightAt(i, j) (the y of that vertex read from the rendered geometry's position buffer), colorAt(i, j) (the authored sRGB color of that vertex as integers [r, g, b] from 0 to 255, the hex values in LOOK, not Three.js's linear-space floats), visitsAt(i, j), camera() returning {az, el, dist}, terrainBox() returning {x, y, w, h} in CSS px (the projected box described above at the current camera), and layout() returning { buttons: { run, reset } } with each button as {x, y, w, h} in CSS px. setCameraOrbit(azimuthDeg, elevationDeg, distance) sets the camera with the same clamps. SOUND - Every 500th finished droplet -> a soft water plip (sine 700 Hz falling to 450 Hz in 0.08 s, gain 0.06). - Erosion starts (Space, RUN, or the demo's 2.0 s mark) -> a rising whoosh (noise through a band-pass sweeping 300 Hz to 1,200 Hz over 0.6 s). - Reset -> a soft low thud (sine 90 Hz, 0.2 s). Always on: quiet wind (noise through a 500 Hz low-pass, gain 0.02). - With no AudioContext (before the first tap, and in ?test=1) nothing is played, but plips are still counted. COUNTERS stats() returns { fps, grid, droplets, eroded, deposited, lost, heightSum, riverCells, plips }. - grid: always 128. - droplets: +1 each time a droplet's simulation ends. 0 at load and after a reset. - eroded: the total height really removed from the grid. deposited: the total height added to the grid. lost: sediment carried off the grid edge (see EROSION). - heightSum: the sum of all 16384 heights as a float64, computed when stats() is called. - riverCells: the number of cells whose visit count is at least 600. - plips: +1 each time the 500th, 1000th, 1500th ... droplet finishes, so it always equals floor(droplets / 500), whether or not sound is on. - On screen: droplets, riverCells, eroded and lost in the HUD bar as laid out above (numbers with thousands separators), readable in a paused frame and never over the terrain. CHECKS for ?test=1 With ?test=1, run an accelerated self-test that measures real positions and real events (not self-set flags) and logs "TEST n PASS" or "TEST n FAIL: reason" to the console and to window.__testResults: Each check starts from a fresh load; advance() steps in 1/60 s increments. 1. After attract(false) at t = 0: stats().grid is 128 and droplets, eroded, deposited, lost, riverCells and plips are 0; running() is false; heights() has 16384 values with minimum 0 and maximum 40 (each within 0.01); camera() is {az 35, el 28, dist 240} at 1280x720 and {az 35, el 28, dist 560} at 390x844 (each within 0.01); at 1280x720 terrainBox().h is at least 360 and terrainBox().y is at least 40; at 390x844 terrainBox().w is between 331 and 390; colorAt is within 3 per channel of (79,143,60) for every cell with 6.5 <= h <= 19.5, of (244,247,250) for every cell with h >= 32.5, of (27,92,140) for every cell with h <= 3.5, of (217,197,140) for every cell with 4.5 <= h <= 5.5 and of (122,110,99) for every cell with 20.5 <= h <= 31.5; between 3,500 and 8,000 cells have h < 6 (the sea and sand ring); both buttons in layout() are at least 56 x 56. 2. After attract(false): read s0 = stats().heightSum, then erode(20000): droplets is 20000, plips is 40, |heightSum - (s0 - lost)| is at most 1.0, |eroded - deposited - lost| is at most 1.0, eroded is between 4,000 and 16,000, lost is between 300 and 3,500, deposited is above 0, and heights() has a minimum of at least -0.001 and a maximum of at most 40.5. Then reset(), erode(5000), read S1 = stats().heightSum, reset(), erode(5000), read S2: |S1 - S2| is at most 1e-6 (the same seed gives the same terrain), and after the last reset() stats() droplets is 0. 3. After attract(false), save a copy h0 of heights(), then erode(100000): the 164 cells with the largest (h0 - h) have a mean (h - h0) of at most -3.0 and the 164 cells with the largest (h - h0) have a mean (h - h0) of at least +3.0 (ties broken by lower index); the single largest (h0 - h) and the single largest (h - h0) are each at least 4.0; riverCells is between 400 and 2,500; and for every one of the 16384 cells, (visitsAt >= 600) is true exactly when colorAt is within 3 per channel of (63,167,214). 4. After attract(false) and reset(): real input events on the canvas (a PointerEvent sequence pointerdown at (640, 360), pointermove to (740, 410), pointerup; then a WheelEvent with deltaY 100) give camera() az 5.0 and el 43.0 (each within 0.05) and dist 264 (within 0.5); a WheelEvent with deltaY -10000 clamps dist to 120 and one with deltaY 100000 clamps it to 700 (each within 0.01). Then a real keydown of Space on window makes running() true, advance(1.0) makes droplets exactly 2400; a real click (pointerdown and pointerup) at the centre of layout().buttons.run makes running() false and advance(1.0) leaves droplets at 2400; a real keydown of r on window gives droplets 0, riverCells 0 and a heightSum equal to the value read at load (within 1e-6). Then erode(5000): for the 12 cells (5,5) (30,100) (64,64) (90,20) (120,120) (10,60) (64,10) (100,64) (40,40) (70,90) (20,20) (110,110), meshHeightAt equals heightAt within 0.01. 5. On a fresh load with the demo running, advancing in 1/60 s ticks to each read time in turn (1.9, 2.5, 7.0, 10.0, 14.0, 26.5, 27.5, 28.5 and 30.5 s): at t = 1.9 droplets is 0, running() is false and camera().az is within 1.0 of 59.4; at t = 2.5 droplets is 1,200 (within 80); at t = 7.0 camera().az is within 1.0 of 125 and el within 1.0 of 38; at t = 10.0 droplets is 19,200 (within 80); at t = 14.0 camera().az is within 1.0 of 215 and el within 1.0 of 28; at t = 26.5 and again at t = 27.5 droplets is 57,600 (within 80), running() is false, riverCells is between 150 and 1,300, eroded is between 9,000 and 32,000, |heightSum - (s0 - lost)| is at most 1.0 (s0 = heightSum at load) and plips equals floor(droplets / 500); at t = 28.5 droplets is 0, riverCells is 0, heightSum equals s0 (within 1e-6) and camera().az is within 1.0 of 41.4; at t = 30.5 droplets is 1,200 (within 80); and the run has zero console errors. KEEP IT SHORT - Keep the whole file under about 1,500 lines (soft target) so you can write it in one pass and finish well inside 15 minutes. - Save lines with helpers, loops and instancing, not by dropping features. Everything above must really work: no placeholders, no TODOs. BUILD RULES (all required): - Deliver ONE self-contained HTML file: inline CSS and JS, no build step, no local assets. Libraries only from a CDN. If you use Three.js, use exactly this import map: {"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"}} - It must open straight from disk in desktop Chrome and on an iPhone (Safari), fill the full screen, handle resize and touch, and stay smooth (target 60 fps). - It must look stunning in the very FIRST frame: no black screen, no loading wait, no tiny subject. The main subject fills at least 50% of the screen height. - SOUND (required): all sound is made in code with Web Audio (no audio files). Show a small "🔊 TAP FOR SOUND" chip; on the first tap create ONE AudioContext, route everything through a master gain of 0.6 into a DynamicsCompressor, and hide the chip. Also expose enableAudio() on the hook object named at the top. - Expose the hook object named at the top with: setCameraOrbit(azimuthDeg, elevationDeg, distance) (or an equivalent camera setter), advance(seconds) that steps the simulation exactly that much for still captures, enableAudio(), and stats() returning {fps, ...the counters named above}. Use a fixed random seed of 360 so every run is the same. - Every on-screen counter must count a real event you can see happen. No fake numbers. - No text overlapping the subject; any label must be readable in a paused frame. - No brand logos, no real people, no copyrighted characters, brand names or logos. FAILURE MODES TO AVOID - Erosion that is only a blur, a noise pass or a painted texture: real droplets must follow the slope, pick up and drop sediment, and leave visible valleys and fans. - Mass that appears or vanishes: heights that go below 0, a heightSum that does not equal the starting sum minus lost, or droplets that wrap around the edge instead of being lost. - Heights changing in the array while the rendered mesh, vertex colors or normals stay old (the screen looks frozen or flat-shaded). - River overlay drawn from a fixed pattern instead of from the real visit counts, or riverCells not matching the cells drawn blue. - Live erosion that freezes the page (40 droplets per tick plus a mesh refresh must stay smooth), or an erode(n) that returns before the mesh is refreshed. - Counters stuck at 0, ticking on a timer, or not matching what happened (droplets, eroded, lost, plips). - A flyover that is not smooth, that jumps at the 28 s loop point, or a camera that ends up inside the terrain. - A tiny island in the middle of an empty screen, a black first frame, or the HUD drawn over the terrain. - Drag, wheel or pinch that does nothing, or touch that scrolls the page instead. - The same variable declared twice, or methods called on objects that don't exist yet. SELF-TEST LOOP - If you can run code: build the file, open it, read the console, run ?test=1, and take screenshots at 3 s, 10 s and 30 s at 1280x720 and 390x844. Look at the screenshots and fix what looks wrong. Fix and retest until all 5 checks pass with zero console errors. Mark anything you could not run as UNMEASURED; never invent results. - If you are chat-only: go through this prompt line by line, check your code against each line, and fix anything that would fail. Then write one corrected, complete file. - Final answer: ONE complete HTML file in ONE code block, then the 5 checks marked PASS, FAIL or UNMEASURED. No snippets, no "rest unchanged". ===== END SPEC ===== Now build it. Reply with ONE complete HTML file in ONE code block, then the 5 checks marked PASS, FAIL or UNMEASURED.