3D engine that AI agents drive
Add deterministic CRITICAL film replay and export page
Nguyen Xuan Anf9f5d6de4e29
diff --git a/film.html b/film.htmlnew file mode 100644index 00000000..48c856b2--- /dev/null+++ b/film.html@@ -0,0 +1,43 @@+<!doctype html>+<html lang="en">+ <head>+ <meta charset="UTF-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <meta name="theme-color" content="#080909" />+ <meta name="description" content="CRITICAL. An original 32-second cinematic short rendered in 3D by 67.js." />+ <title>CRITICAL · A 67.js short</title>+ </head>+ <body>+ <main class="film-stage" aria-label="CRITICAL, an original 3D short film">+ <canvas id="film-canvas" aria-label="Procedural 3D film"></canvas>+ <div id="film-flash" class="film-flash" aria-hidden="true"></div>+ <div id="film-title" class="film-title" aria-hidden="true">+ <h1 id="title-text"></h1>+ <p id="subtitle-text"></p>+ </div>+ <header class="film-header player-interface">+ <a class="brand" href="./">67<span>.js</span></a>+ <span class="film-name">CRITICAL</span>+ <nav aria-label="Explore 67.js">+ <a href="./nature.html">Nature</a>+ <a href="./">Object</a>+ </nav>+ </header>+ <section class="transport player-interface" aria-label="Film playback">+ <div class="transport-actions">+ <button id="film-play" class="play-button" type="button" aria-label="Play film"><span id="play-symbol" aria-hidden="true">▶</span><span id="play-label">Play film</span></button>+ <button id="film-replay" type="button"><span aria-hidden="true">↺</span> Replay</button>+ <a class="download" href="./media/critical.mp4" download="critical.mp4">Download film <span aria-hidden="true">↓</span></a>+ </div>+ <div class="scrubber">+ <label for="film-time" class="visually-hidden">Film position in seconds</label>+ <input id="film-time" type="range" min="0" max="32" step="0.01" value="0" />+ <output id="film-clock" for="film-time">0:00 / 0:32</output>+ </div>+ <p class="film-caption">An original 3D short. Rendered live in your browser.</p>+ </section>+ <p id="film-error" class="film-error player-interface" role="alert" hidden></p>+ </main>+ <script type="module" src="/src/film-player.js"></script>+ </body>+</html>diff --git a/src/film-player.js b/src/film-player.jsnew file mode 100644index 00000000..9bdb4eaf--- /dev/null+++ b/src/film-player.js@@ -0,0 +1,164 @@+import './film-style.css';+import { Renderer67 } from './renderer.js';+import { createFilmScenes, FILM_DURATION, sampleFilm } from './film.js';++const query = new URLSearchParams(window.location.search);+const exporting = query.get('export') === '1';+function numberOption(name, fallback, minimum, maximum) {+ const raw = query.get(name);+ const value = raw === null || raw.trim() === '' ? fallback : Number(raw);+ return Number.isFinite(value) ? Math.round(Math.max(minimum, Math.min(maximum, value))) : fallback;+}+const width = numberOption('width', 1280, 320, 7680);+const height = numberOption('height', 720, 240, 4320);+const samples = numberOption('samples', 4, 1, 8);+const memoryBudgetMiB = numberOption('memory', 384, 64, 1024);+const canvas = document.querySelector('#film-canvas');+const stage = document.querySelector('.film-stage');+const slider = document.querySelector('#film-time');+const playButton = document.querySelector('#film-play');+const playLabel = document.querySelector('#play-label');+const playSymbol = document.querySelector('#play-symbol');+const clock = document.querySelector('#film-clock');+const title = document.querySelector('#film-title');+const titleText = document.querySelector('#title-text');+const subtitleText = document.querySelector('#subtitle-text');+const flash = document.querySelector('#film-flash');+const scenes = createFilmScenes();+let renderer = null;+let activeScene = null;+let currentTime = 0;+let playing = false;+let animationFrame = 0;+let playbackOrigin = 0;++if (exporting) {+ document.body.classList.add('export-mode');+ stage.style.width = `${width}px`;+ stage.style.height = `${height}px`;+}++function displayTime(seconds) {+ return `${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, '0')}`;+}++function renderAt(time) {+ const sample = sampleFilm(time);+ const scene = scenes[sample.scene];+ if (activeScene !== sample.scene) {+ renderer?.dispose();+ renderer = null;+ activeScene = null;+ renderer = new Renderer67(canvas, scene.parts, {+ world: true,+ ground: scene.ground,+ environment: scene.environment,+ shadowBounds: scene.shadowBounds,+ pixelRatio: 1,+ samples,+ memoryBudgetMiB,+ });+ activeScene = sample.scene;+ }+ renderer.render({+ ...sample.camera,+ time: sample.sceneTime,+ exposure: sample.exposure,+ monochrome: sample.monochrome,+ fade: sample.fade,+ cinematic: true,+ explosionProgress: sample.explosionProgress,+ environment: scene.environment,+ });+ currentTime = sample.time;+ slider.value = String(currentTime);+ slider.setAttribute('aria-valuetext', `${currentTime.toFixed(1)} of ${FILM_DURATION} seconds`);+ clock.value = `${displayTime(currentTime)} / ${displayTime(FILM_DURATION)}`;+ titleText.textContent = sample.title;+ subtitleText.textContent = sample.subtitle;+ title.style.opacity = String(sample.titleOpacity);+ flash.style.opacity = String(sample.flash);+ return sample;+}++function setPlaying(value) {+ playing = value;+ playLabel.textContent = playing ? 'Pause' : currentTime >= FILM_DURATION ? 'Play again' : 'Play film';+ playSymbol.textContent = playing ? 'Ⅱ' : '▶';+ playButton.setAttribute('aria-label', playing ? 'Pause film' : 'Play film');+ cancelAnimationFrame(animationFrame);+ animationFrame = 0;+ if (playing) {+ if (currentTime >= FILM_DURATION) renderAt(0);+ playbackOrigin = performance.now() - currentTime * 1000;+ animationFrame = requestAnimationFrame(advance);+ }+}++function showError(error) {+ setPlaying(false);+ const notice = document.querySelector('#film-error');+ notice.textContent = `⚠ ${error.message}`;+ notice.hidden = false;+ window.__filmError = error.message;+}++function advance(timestamp) {+ if (!playing) return;+ try {+ renderAt((timestamp - playbackOrigin) / 1000);+ if (currentTime >= FILM_DURATION) setPlaying(false);+ else animationFrame = requestAnimationFrame(advance);+ } catch (error) {+ showError(error);+ }+}++playButton.addEventListener('click', () => setPlaying(!playing));+document.querySelector('#film-replay').addEventListener('click', () => {+ setPlaying(false);+ renderAt(0);+ setPlaying(true);+});+slider.addEventListener('input', () => {+ setPlaying(false);+ renderAt(Number(slider.value));+});+document.addEventListener('keydown', event => {+ if (event.code !== 'Space' || event.target.closest('input, button, a')) return;+ event.preventDefault();+ setPlaying(!playing);+});+document.addEventListener('visibilitychange', () => {+ if (document.hidden) setPlaying(false);+});+window.addEventListener('resize', () => {+ if (!renderer || exporting) return;+ renderer.resize();+ renderAt(currentTime);+});+window.addEventListener('pagehide', () => {+ setPlaying(false);+ renderer?.dispose();+ renderer = null;+ activeScene = null;+});++window.__filmReady = false;+window.renderAt = renderAt;+window.__film = {+ scenes,+ get renderer() { return renderer; },+ sampleFilm,+ renderAt,+ duration: FILM_DURATION,+ exporting,+ options: { width, height, samples, memoryBudgetMiB },+};+try {+ // Playback waits for a click for every motion preference, including reduced motion.+ renderAt(exporting ? 0 : 0.8);+ window.__filmReady = true;+} catch (error) {+ showError(error);+}diff --git a/src/film-style.css b/src/film-style.cssnew file mode 100644index 00000000..ad57a8c9--- /dev/null+++ b/src/film-style.css@@ -0,0 +1,65 @@+:root {+ color-scheme: dark;+ font-family: Inter, "Segoe UI", Arial, sans-serif;+ color: #f3f1eb;+ background: #080909;+ font-synthesis: none;+}+* { box-sizing: border-box; }+body { margin: 0; }+button, input { font: inherit; }+a { color: inherit; text-decoration: none; }+button, a, input { -webkit-tap-highlight-color: transparent; }+button, .player-interface { user-select: none; }+button, a { touch-action: manipulation; }+button:focus-visible, a:focus-visible, input:focus-visible {+ outline: 2px solid #e7c89b;+ outline-offset: 5px;+}+.film-stage { position: relative; width: 100%; height: 100dvh; min-height: 400px; overflow: hidden; }+#film-canvas { display: block; width: 100%; height: 100%; }+.film-flash { position: absolute; inset: 0; background: white; opacity: 0; pointer-events: none; }+.film-title { position: absolute; inset: 0; display: grid; align-content: center; justify-items: center; opacity: 0; pointer-events: none; }+.film-title h1 { margin: 0; padding-left: .24em; font-size: clamp(42px, 8vw, 106px); font-weight: 450; letter-spacing: .24em; }+.film-title p { margin: 18px 0 0; font-size: clamp(14px, 1.6vw, 21px); letter-spacing: .12em; }+.film-header {+ position: absolute; inset: 0 0 auto; display: flex; align-items: center; gap: 22px;+ padding: 24px 32px 55px; background: linear-gradient(#080909d9, #08090900);+}+.brand { font-size: 24px; font-weight: 750; letter-spacing: -.08em; min-width: 52px; }+.brand span { font-weight: 400; }+.film-name { font-size: 15px; letter-spacing: .1em; }+.film-header nav { margin-left: auto; display: flex; gap: 26px; font-size: 14px; }+.film-header nav a { min-height: 44px; display: inline-flex; align-items: center; color: #d6d5cf; }+.transport {+ position: absolute; inset: auto 0 0; padding: 55px 32px 24px;+ background: linear-gradient(#08090900, #080909ed);+}+.transport-actions { display: flex; align-items: center; gap: 16px; }+button { min-height: 44px; padding: 10px 14px; cursor: pointer; border: 0; border-radius: 7px; color: #f3f1eb; background: #ffffff0d; }+button:hover { background: #ffffff1c; }+.play-button { display: flex; align-items: center; gap: 10px; min-width: 128px; background: #eeece4; color: #191a18; }+.play-button:hover { background: white; }+.download { margin-left: auto; font-size: 14px; min-height: 44px; display: inline-flex; align-items: center; gap: 8px; }+.scrubber { display: flex; align-items: center; gap: 18px; margin-top: 17px; }+#film-time { flex: 1; width: 100%; min-width: 0; accent-color: #e7c89b; cursor: pointer; height: 28px; }+#film-clock { font-variant-numeric: tabular-nums; font-size: 13px; color: #d6d5cf; white-space: nowrap; }+.film-caption { margin: 8px 0 0; font-size: 12px; color: #bdbdb5; }+.film-error { position: absolute; top: 100px; left: 32px; right: 32px; padding: 16px; border-radius: 8px; background: #181817; color: #ffbfaf; }+.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }+.export-mode .player-interface { display: none !important; }+.export-mode .film-stage { min-height: 0; }+@media (max-width: 600px) {+ .film-header { padding: 16px 20px 50px; gap: 16px; }+ .brand { font-size: 22px; }+ .film-name { font-size: 12px; }+ .film-header nav { gap: 16px; font-size: 13px; }+ .transport { padding: 60px 20px 24px; }+ .transport-actions { flex-wrap: wrap; gap: 10px; }+ .download { font-size: 12px; }+ .play-button { min-width: 118px; }+ button { padding: 10px 12px; font-size: 13px; }+ .film-caption { max-width: 270px; line-height: 1.5; }+ .film-title h1 { font-size: 42px; letter-spacing: .17em; padding-left: .17em; }+ .film-error { left: 20px; right: 20px; }+}