Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Animate homepage model on a transparent canvas without controls

Nguyen Xuan An8f904b3efa88
diff --git a/index.html b/index.html
index d9e0d40c..45f4a43c 100644
--- a/index.html
+++ b/index.html
@@ -24,10 +24,8 @@
<header class="introduction"><div><h1>Your 3D scene,<br />from browser to film</h1><p>67.js brings browser 3D, animation and image/video export into one workflow. Create interactive experiences and export the same scene as images or video</p><div class="hero-actions"><a href="/downloads.html" data-icon="download">Download 67.js</a></div></div></header>
<section class="feature-showcase live-showcase" aria-label="Interactive 3D preview">
- <canvas id="home-preview" tabindex="0" aria-label="Clockwork 3D model. Drag or use arrow keys to rotate. Use plus and minus to zoom."></canvas>
+ <canvas id="home-preview" tabindex="0" aria-label="Clockwork 3D model. Drag or use arrow keys to rotate. Use plus and minus to zoom. Press Space to pause or resume animation."></canvas>
<p id="home-preview-status" role="status">Loading 3D preview…</p>
- <div class="preview-tools" role="group" aria-label="3D preview controls"><button id="preview-play" aria-pressed="false">Animate</button><button id="preview-zoom-in" aria-label="Zoom in">+</button><button id="preview-zoom-out" aria-label="Zoom out">−</button><button id="preview-reset">Reset view</button></div>
- <div class="showcase-caption"><div><h2>Clockwork orbit</h2><p>Live 3D in your browser. Drag to rotate, zoom in and animate the mechanism.</p></div><a href="./motion.html" data-icon="cube">Open the full studio</a></div>
</section>
</section>
<section class="workflow" aria-label="What you can build"><div><span data-icon="cube"></span><h3>Build and inspect</h3><p>Shared meshes, materials and cameras. Pull a machine apart, explore a world, or bring your own GLB.</p></div><div><span data-icon="film"></span><h3>Make it move</h3><p>Sample keyframes at any time. Give visitors controls and keep the same timeline for your film.</p></div><div><span data-icon="image"></span><h3>Export the result</h3><p>Produce images and videos on GPU or CPU with explicit render budgets and one capture frame at a time.</p></div></section>
diff --git a/scripts/smoke-site.mjs b/scripts/smoke-site.mjs
index ce58d6d4..a9bdbe35 100644
--- a/scripts/smoke-site.mjs
+++ b/scripts/smoke-site.mjs
@@ -62,17 +62,29 @@ try {
await page.locator('#home-preview').scrollIntoViewIfNeeded();
await page.waitForFunction(() => window.__homePreview?.renderer && window.__homePreview.state.visible);
const initialDistance = await page.evaluate(() => window.__homePreview.state.distance);
- await page.locator('#preview-zoom-in').click();
+ await page.locator('#home-preview').focus();
+ await page.keyboard.press('+');
assert.ok(await page.evaluate(() => window.__homePreview.state.distance) < initialDistance);
await page.locator('#home-preview').focus();
await page.keyboard.press('ArrowRight');
assert.ok(await page.evaluate(() => window.__homePreview.state.yaw) > 0.6);
- await page.locator('#preview-reset').click();
+ await page.keyboard.press('Home');
assert.equal(await page.evaluate(() => window.__homePreview.state.yaw), 0.6);
- await page.locator('#preview-play').click();
+ assert.equal(await page.locator('.preview-tools, .showcase-caption').count(), 0);
+ assert.equal(await page.evaluate(() => window.__homePreview.state.playing), false);
+ await page.emulateMedia({ reducedMotion:'no-preference' });
await page.waitForFunction(() => window.__homePreview.state.time > 3.1);
- await page.locator('#preview-play').click();
+ await page.emulateMedia({ reducedMotion:'reduce' });
+ await page.waitForFunction(() => !window.__homePreview.state.playing);
assert.equal(await page.evaluate(() => window.__homePreview.state.playing), false);
+ const backgroundAlpha = await page.evaluate(() => {
+ window.__homePreview.draw();
+ const context = window.__homePreview.renderer.context;
+ const pixel = new Uint8Array(4);
+ context.readPixels(0, 0, 1, 1, context.RGBA, context.UNSIGNED_BYTE, pixel);
+ return pixel[3];
+ });
+ assert.equal(backgroundAlpha, 0, 'Preview background is opaque');
assert.equal(await page.evaluate(() => window.__homePreview.renderer.context.getError()), 0);
assert.ok(await page.evaluate(() => window.__homePreview.renderer.memoryPlan.estimatedBytes) <= 192 * 1048576);
await page.screenshot({ path:'artifacts/home-live-preview.png' });
diff --git a/src/home-preview.js b/src/home-preview.js
index 8cb04895..6d1322b8 100644
--- a/src/home-preview.js
+++ b/src/home-preview.js
@@ -3,9 +3,8 @@ import { createMotionScene } from './motion.js';
const canvas = document.querySelector('#home-preview');
const status = document.querySelector('#home-preview-status');
-const playButton = document.querySelector('#preview-play');
const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)');
-const state = { yaw:0.6, pitch:0.28, distance:8, time:3, playing:false, visible:false };
+const state = { yaw:0.6, pitch:0.28, distance:8, time:3, playing:!reducedMotion.matches, visible:false };
let renderer;
let scene;
let scheduledFrame = 0;
@@ -15,10 +14,8 @@ let disposed = false;
function showError(error) {
state.playing = false;
- playButton.setAttribute('aria-pressed', 'false');
- playButton.textContent = 'Animate';
status.hidden = false;
- status.textContent = `3D preview unavailable: ${error.message}. Open the full studio to retry.`;
+ status.textContent = `3D preview unavailable: ${error.message}. Reload the page to retry.`;
cancelAnimationFrame(scheduledFrame);
scheduledFrame = 0;
}
@@ -31,7 +28,7 @@ function draw() {
const distance = state.distance * (canvas.clientWidth < 500 ? 1.25 : 1);
renderer.render({
eye:[Math.sin(state.yaw) * Math.cos(state.pitch) * distance, Math.sin(state.pitch) * distance, Math.cos(state.yaw) * Math.cos(state.pitch) * distance],
- target:[0,0.1,0], fov:40, time:state.time, exposure:1, effects:frame.effects,
+ target:[0,0.1,0], fov:40, time:state.time, exposure:1, effects:{ ...frame.effects, vignette:0, grain:0 },
});
} catch (error) {
showError(error);
@@ -63,7 +60,7 @@ function initialize() {
try {
scene = createMotionScene();
renderer = new Renderer67(canvas, scene.parts, {
- ground:false, hdr:true, samples:4, pixelRatio:1.25, memoryBudgetMiB:192,
+ ground:false, transparent:true, hdr:true, samples:4, pixelRatio:1.25, memoryBudgetMiB:192,
});
window.__homePreview = { renderer, scene, state, draw };
status.hidden = true;
@@ -83,16 +80,6 @@ function reset() {
draw();
}
-playButton.addEventListener('click', () => {
- if (!renderer) return;
- state.playing = !state.playing;
- playButton.setAttribute('aria-pressed', String(state.playing));
- playButton.textContent = state.playing ? 'Pause' : 'Animate';
- synchronizePlayback();
-});
-document.querySelector('#preview-zoom-in').addEventListener('click', () => zoom(0.85));
-document.querySelector('#preview-zoom-out').addEventListener('click', () => zoom(1.15));
-document.querySelector('#preview-reset').addEventListener('click', reset);
canvas.addEventListener('pointerdown', event => {
if (event.button !== 0 || pointer) return;
pointer = { id:event.pointerId, x:event.clientX, y:event.clientY };
@@ -120,6 +107,12 @@ canvas.addEventListener('wheel', event => {
zoom(Math.exp(Math.max(-100, Math.min(100, event.deltaY)) * 0.002));
}, { passive:false });
canvas.addEventListener('keydown', event => {
+ if (event.code === 'Space') {
+ event.preventDefault();
+ state.playing = !state.playing;
+ synchronizePlayback();
+ return;
+ }
if (!['ArrowLeft','ArrowRight','ArrowUp','ArrowDown','+','=','-','Home'].includes(event.key)) return;
event.preventDefault();
if (event.key === 'ArrowLeft') state.yaw -= 0.12;
@@ -165,10 +158,7 @@ document.addEventListener('visibilitychange', () => {
synchronizePlayback();
});
reducedMotion.addEventListener('change', () => {
- if (!reducedMotion.matches) return;
- state.playing = false;
- playButton.setAttribute('aria-pressed', 'false');
- playButton.textContent = 'Animate';
+ state.playing = !reducedMotion.matches;
synchronizePlayback();
});
window.addEventListener('pagehide', event => {
diff --git a/src/home.css b/src/home.css
index 9d4e6b63..6f6cd55e 100644
--- a/src/home.css
+++ b/src/home.css
@@ -132,14 +132,11 @@ button:hover { background:var(--border); }
.hero-actions { display:flex; flex-wrap:wrap; gap:12px; align-items:center; }
.hero-actions a { padding:12px 18px; border-radius:var(--radius); background:var(--text); color:var(--window); font-size:14px; }
.hero-actions a+ a { background:var(--bg); color:var(--text); border:1px solid var(--border); }
-.feature-showcase { margin:26px 0 42px; border-radius:var(--panel-radius); background:#171717; overflow:hidden; position:relative; }
+.feature-showcase { margin:26px 0 42px; border-radius:var(--panel-radius); background:transparent; overflow:hidden; position:relative; }
.live-showcase canvas { display:block; width:100%; height:clamp(280px,42vw,540px); touch-action:pan-y; cursor:grab; }
.live-showcase canvas:active { cursor:grabbing; }
.live-showcase canvas:focus-visible { outline-offset:-4px; }
-.preview-tools { display:flex; flex-wrap:wrap; justify-content:center; gap:8px; padding:16px; }
-.preview-tools button { min-height:40px; background:#2f2f2f; color:#ececec; }
-.preview-tools button[aria-pressed=true] { background:#ececec; color:#171717; }
-#home-preview-status { position:absolute; top:20px; left:20px; right:20px; margin:0; font-size:13px; color:#ececec; pointer-events:none; }
+#home-preview-status { position:absolute; top:20px; left:20px; right:20px; margin:0; font-size:13px; color:var(--muted); pointer-events:none; }
.downloads-page { max-width:1000px; }
.downloads-page > .downloads { margin:20px 0; }
.downloads-page .section-heading h1 { margin-bottom:16px; }
@@ -161,9 +158,6 @@ body:has(.navigation) { --navigation-height:80px; }
.navigation nav a { padding:10px 8px; gap:6px; font-size:12px; white-space:nowrap; }
.package-managers { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); }
}
-.showcase-caption { display:flex; justify-content:space-between; flex-wrap:wrap; gap:16px; padding:22px 24px; color:#ececec; }
-.showcase-caption p { margin:6px 0 0; color:#b4b4b4; }
-.showcase-caption a { align-self:center; border-radius:var(--radius); padding:12px 18px; background:#ececec; color:#171717; font-size:14px; }
.workflow { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:24px; margin-bottom:46px; }
.workflow h3 { margin:12px 0 8px; }
.workflow p { font-size:14px; margin:0; }
@@ -195,4 +189,4 @@ td, th { text-align:left; padding:12px; border-bottom:0; vertical-align:top; }
.release-feedback p, .download > span { color:var(--muted); }
}
@media (max-width:960px) { .docs-layout { grid-template-columns:1fr; gap:0; } #doc-toc { position:static; max-height:none; display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); } }
-@media (max-width:760px) { .workflow { grid-template-columns:1fr; gap:24px; } .introduction h1 { font-size:38px; letter-spacing:-1.3px; } .showcase-caption { padding:18px; } .navigation { border-right:0; border-bottom:1px solid var(--border); } .docs-page h1 { font-size:32px; } .docs-search input { font-size:16px; } }
+@media (max-width:760px) { .workflow { grid-template-columns:1fr; gap:24px; } .introduction h1 { font-size:38px; letter-spacing:-1.3px; } .docs-page h1 { font-size:32px; } .docs-search input { font-size:16px; } }
diff --git a/src/post.js b/src/post.js
index f800a50e..7662f21f 100644
--- a/src/post.js
+++ b/src/post.js
@@ -11,6 +11,7 @@ export const postFragment = `#version 300 es
precision highp float;
in vec2 textureCoordinate;
uniform sampler2D frameTexture;
+uniform float transparent;
uniform sampler2D bloomTexture;
uniform float bloomStrength;
uniform float hdrEnabled;
@@ -99,6 +100,8 @@ void main() {
float secondLuma = luminance(second);
vec3 color = secondLuma < minimumLuma || secondLuma > maximumLuma ? first : second;
if (maximumLuma - minimumLuma < max(0.03, maximumLuma * 0.125)) color = center;
+ float coverage = texture(frameTexture, textureCoordinate).a;
+ if (transparent > 0.5) color = center / max(coverage, 0.00001);
if (bloomStrength > 0.0) color += texture(bloomTexture, textureCoordinate).rgb * bloomStrength;
if (hdrEnabled > 0.5) {
color = clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), 0.0, 1.0);
@@ -113,7 +116,8 @@ void main() {
color *= max(0.0, 1.0 - (cinematic * 0.20 + vignette) * dot(centered, centered));
float grain = fract(sin(dot(gl_FragCoord.xy + floor(time * 24.0), vec2(12.9898, 78.233))) * 43758.5453) - 0.5;
color += grain * (0.018 * cinematic + grainAmount);
- outputColor = vec4(color * (1.0 - fade), 1.0);
+ float alpha = transparent > 0.5 ? coverage : 1.0;
+ outputColor = vec4(color * (1.0 - fade) * alpha, alpha);
}`;
export const skyFragment = `#version 300 es
diff --git a/src/renderer.js b/src/renderer.js
index 9d756733..f39322a6 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -108,12 +108,13 @@ function sampleCount(context, format, requested) {
}
export class Renderer67 {
- constructor(canvas, parts, { batching = true, pixelRatio = 1.5, benchmark = false, world = false, ground = true, samples = 4, hdr = false, memoryBudgetMiB = 256, environment = {}, shadowBounds = {} } = {}) {
+ constructor(canvas, parts, { batching = true, pixelRatio = 1.5, benchmark = false, world = false, ground = true, transparent = false, samples = 4, hdr = false, memoryBudgetMiB = 256, environment = {}, shadowBounds = {} } = {}) {
this.canvas = canvas;
if (!Number.isInteger(samples) || samples < 1) throw new Error('MSAA sample count must be a positive integer.');
if (new Set(parts).size !== parts.length) throw new Error('Scene parts must have distinct object identities for animation.');
- this.context = canvas.getContext('webgl2', { antialias: true, alpha: false, powerPreference: 'high-performance' });
+ this.context = canvas.getContext('webgl2', { antialias: true, alpha: Boolean(transparent), premultipliedAlpha: true, powerPreference: 'high-performance' });
if (!this.context) throw new Error('WebGL2 is unavailable. Enable hardware acceleration or use a supported browser.');
+ this.transparent = Boolean(transparent);
this.pixelRatio = pixelRatio;
this.benchmark = benchmark;
this.world = world;
@@ -154,7 +155,7 @@ export class Renderer67 {
if (!benchmark) {
this.postProgram = compileProgram(this.context, fullscreenVertex, postFragment);
this.skyProgram = compileProgram(this.context, fullscreenVertex, skyFragment);
- this.postUniforms = Object.fromEntries(['frameTexture', 'bloomTexture', 'bloomStrength', 'hdrEnabled', 'grade', 'vignette', 'grainAmount', 'resolution', 'monochrome', 'fade', 'cinematic', 'time', 'volumeExplosion', 'explosionProgress', 'inverseViewProjection', 'eye'].map((name) => [name, this.context.getUniformLocation(this.postProgram, name)]));
+ this.postUniforms = Object.fromEntries(['transparent', 'frameTexture', 'bloomTexture', 'bloomStrength', 'hdrEnabled', 'grade', 'vignette', 'grainAmount', 'resolution', 'monochrome', 'fade', 'cinematic', 'time', 'volumeExplosion', 'explosionProgress', 'inverseViewProjection', 'eye'].map((name) => [name, this.context.getUniformLocation(this.postProgram, name)]));
this.bloomProgram = compileProgram(this.context, fullscreenVertex, bloomFragment);
this.bloomUniforms = Object.fromEntries(['sourceTexture', 'texelDirection', 'threshold', 'extract'].map(name => [name, this.context.getUniformLocation(this.bloomProgram, name)]));
this.skyUniforms = Object.fromEntries(['skyTop', 'skyBottom', 'sunDirection', 'inverseViewProjection', 'eye', 'time', 'daylight', 'hdrEnabled'].map((name) => [name, this.context.getUniformLocation(this.skyProgram, name)]));
@@ -445,6 +446,7 @@ export class Renderer67 {
context.bindTexture(context.TEXTURE_2D, effects.bloom.strength > 0 ? this.bloomTargets[1].texture : this.whiteTexture);
context.uniform1i(this.postUniforms.bloomTexture, 2);
context.uniform1f(this.postUniforms.bloomStrength, effects.bloom.strength);
+ context.uniform1f(this.postUniforms.transparent, this.transparent ? 1 : 0);
context.uniform1f(this.postUniforms.hdrEnabled, this.hdr ? 1 : 0);
context.uniform3f(this.postUniforms.grade, effects.saturation, effects.contrast, effects.temperature);
context.uniform1f(this.postUniforms.vignette, effects.vignette);
@@ -507,7 +509,11 @@ export class Renderer67 {
mat4.invert(this.inverseViewProjection, this.viewProjection);
context.bindFramebuffer(context.FRAMEBUFFER, this.benchmark ? null : this.multisampleFramebuffer);
context.viewport(0, 0, this.canvas.width, this.canvas.height);
- context.clearColor(0.145, 0.162, 0.177, 1);
+ if (this.transparent) {
+ context.clearColor(0, 0, 0, 0);
+ } else {
+ context.clearColor(0.145, 0.162, 0.177, 1);
+ }
context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT);
if (this.world && !this.benchmark) this.drawSky(frame);
context.enable(context.DEPTH_TEST);