Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

Refactor code structure for improved readability and maintainability

oversteppinged8b54270ce1
diff --git a/pf.jpg b/pf.jpg
new file mode 100644
index 000000000..11926093f
Binary files /dev/null and b/pf.jpg differ
diff --git a/scripts/aframe-origin.png b/scripts/aframe-origin.png
new file mode 100644
index 000000000..c5e12d93c
Binary files /dev/null and b/scripts/aframe-origin.png differ
diff --git a/scripts/aframe-shot.png b/scripts/aframe-shot.png
new file mode 100644
index 000000000..c5e12d93c
Binary files /dev/null and b/scripts/aframe-shot.png differ
diff --git a/scripts/aframe-strafe.png b/scripts/aframe-strafe.png
new file mode 100644
index 000000000..2cc6250ad
Binary files /dev/null and b/scripts/aframe-strafe.png differ
diff --git a/scripts/aframe-walked.png b/scripts/aframe-walked.png
new file mode 100644
index 000000000..9360a7ea5
Binary files /dev/null and b/scripts/aframe-walked.png differ
diff --git a/scripts/blank-at-surface.png b/scripts/blank-at-surface.png
new file mode 100644
index 000000000..9adeea57e
Binary files /dev/null and b/scripts/blank-at-surface.png differ
diff --git a/scripts/blank-deep-inside.png b/scripts/blank-deep-inside.png
new file mode 100644
index 000000000..88c0dd9af
Binary files /dev/null and b/scripts/blank-deep-inside.png differ
diff --git a/scripts/blank-far.png b/scripts/blank-far.png
new file mode 100644
index 000000000..a2d1ddf1c
Binary files /dev/null and b/scripts/blank-far.png differ
diff --git a/scripts/blank-just-inside.png b/scripts/blank-just-inside.png
new file mode 100644
index 000000000..79a4fe8df
Binary files /dev/null and b/scripts/blank-just-inside.png differ
diff --git a/scripts/blank-midway.png b/scripts/blank-midway.png
new file mode 100644
index 000000000..8b5f9c333
Binary files /dev/null and b/scripts/blank-midway.png differ
diff --git a/scripts/blank-out-back.png b/scripts/blank-out-back.png
new file mode 100644
index 000000000..1e1a477e5
Binary files /dev/null and b/scripts/blank-out-back.png differ
diff --git a/scripts/final-mouse-left.png b/scripts/final-mouse-left.png
new file mode 100644
index 000000000..45f95d263
Binary files /dev/null and b/scripts/final-mouse-left.png differ
diff --git a/scripts/final-mouse-right.png b/scripts/final-mouse-right.png
new file mode 100644
index 000000000..e41dd0640
Binary files /dev/null and b/scripts/final-mouse-right.png differ
diff --git a/scripts/final-spawn.png b/scripts/final-spawn.png
new file mode 100644
index 000000000..27e5dc057
Binary files /dev/null and b/scripts/final-spawn.png differ
diff --git a/scripts/final-strafe.png b/scripts/final-strafe.png
new file mode 100644
index 000000000..de3684e90
Binary files /dev/null and b/scripts/final-strafe.png differ
diff --git a/scripts/final-walked.png b/scripts/final-walked.png
new file mode 100644
index 000000000..c6c007657
Binary files /dev/null and b/scripts/final-walked.png differ
diff --git a/scripts/mesh-angle.png b/scripts/mesh-angle.png
new file mode 100644
index 000000000..aa19f538a
Binary files /dev/null and b/scripts/mesh-angle.png differ
diff --git a/scripts/mesh-spawn.png b/scripts/mesh-spawn.png
new file mode 100644
index 000000000..42829caf0
Binary files /dev/null and b/scripts/mesh-spawn.png differ
diff --git a/scripts/mesh-strafe.png b/scripts/mesh-strafe.png
new file mode 100644
index 000000000..bdc696302
Binary files /dev/null and b/scripts/mesh-strafe.png differ
diff --git a/scripts/mesh-walk.png b/scripts/mesh-walk.png
new file mode 100644
index 000000000..92b7cd697
Binary files /dev/null and b/scripts/mesh-walk.png differ
diff --git a/scripts/probe-walk-joystick.mjs b/scripts/probe-walk-joystick.mjs
new file mode 100644
index 000000000..6b7e62924
--- /dev/null
+++ b/scripts/probe-walk-joystick.mjs
@@ -0,0 +1,107 @@
+// Probe whether mouse-drag on the joystick actually wires through to
+// input.joy and ultimately moves the avatar. Stands up a headless browser,
+// loads /walk on desktop, drags the joystick, and reports both the live
+// input state and the avatar's world position before/after.
+import { chromium } from 'playwright';
+
+const URL = process.env.URL || 'http://localhost:3000/walk';
+
+const browser = await chromium.launch({
+ args: [
+ '--use-gl=swiftshader',
+ '--enable-unsafe-swiftshader',
+ '--no-sandbox',
+ '--disable-dev-shm-usage',
+ ],
+});
+const ctx = await browser.newContext({
+ viewport: { width: 1280, height: 800 },
+ hasTouch: false,
+});
+const page = await ctx.newPage();
+
+const errs = [];
+page.on('pageerror', (e) => errs.push(`pageerror: ${e.message}`));
+page.on('console', (m) => { if (m.type() === 'error') errs.push(`console.error: ${m.text()}`); });
+
+await page.goto(URL, { waitUntil: 'domcontentloaded', timeout: 20_000 });
+
+// Expose the live state to the test by stashing references on window once
+// the module has booted. We can't reach `input`/`avatarRig` directly (they
+// are module-local), so instead we synthesize the same readout from the DOM
+// plus a periodic snapshot of nipplejs internals.
+await page.waitForFunction(() => {
+ const el = document.getElementById('walk-joystick');
+ return el && el.querySelector('.front') && el.querySelector('.back');
+}, { timeout: 8000 });
+
+const before = await page.evaluate(() => {
+ const r = document.getElementById('walk-joystick').getBoundingClientRect();
+ return {
+ joyRect: { x: r.left, y: r.top, w: r.width, h: r.height, cx: r.left + r.width/2, cy: r.top + r.height/2 },
+ };
+});
+console.log('joystick rect:', before.joyRect);
+
+// Listen for all nipplejs `move` data by hooking into the front element's
+// transform — it gets mutated on each move. Read it after dragging.
+async function dragJoystick({ dx, dy, label }) {
+ const cx = before.joyRect.cx;
+ const cy = before.joyRect.cy;
+ console.log(`\n[${label}] dragging from (${cx},${cy}) to (${cx+dx},${cy+dy})`);
+ await page.mouse.move(cx, cy);
+ await page.mouse.down();
+ const steps = 12;
+ for (let i = 1; i <= steps; i++) {
+ await page.mouse.move(cx + dx * (i/steps), cy + dy * (i/steps));
+ await page.waitForTimeout(20);
+ }
+ // Hold so the avatar has time to translate.
+ for (let i = 0; i < 25; i++) {
+ await page.waitForTimeout(40);
+ }
+ const sample = await page.evaluate(() => {
+ const front = document.querySelector('#walk-joystick .front');
+ const transform = front ? getComputedStyle(front).transform : null;
+ return { transform };
+ });
+ console.log(`[${label}] front transform during drag:`, sample.transform);
+
+ await page.mouse.up();
+ await page.waitForTimeout(120);
+}
+
+// Sample avatar canvas pixel-center color before/after as a crude motion
+// witness — if the avatar moved, the center pixel changes.
+async function centerPixel() {
+ return page.evaluate(() => {
+ const c = document.getElementById('walk-canvas');
+ const tmp = document.createElement('canvas');
+ tmp.width = c.width; tmp.height = c.height;
+ const g = tmp.getContext('2d');
+ g.drawImage(c, 0, 0);
+ const d = g.getImageData(c.width/2, c.height/2, 1, 1).data;
+ return [d[0], d[1], d[2], d[3]];
+ });
+}
+
+const p0 = await centerPixel();
+console.log('center pixel before any drag:', p0);
+
+await dragJoystick({ dx: 0, dy: -45, label: 'forward (up)' });
+const p1 = await centerPixel();
+console.log('center pixel after forward:', p1);
+
+await dragJoystick({ dx: 45, dy: 0, label: 'right' });
+const p2 = await centerPixel();
+console.log('center pixel after right:', p2);
+
+if (errs.length) {
+ console.log('\nERRORS:');
+ for (const e of errs) console.log(' -', e);
+}
+
+await page.screenshot({ path: 'scripts/probe-joystick.png' });
+console.log('\nscreenshot: scripts/probe-joystick.png');
+
+await browser.close();
diff --git a/scripts/shimmer-t0.png b/scripts/shimmer-t0.png
new file mode 100644
index 000000000..50a0b492d
Binary files /dev/null and b/scripts/shimmer-t0.png differ
diff --git a/scripts/shimmer-t1.png b/scripts/shimmer-t1.png
new file mode 100644
index 000000000..b8582226a
Binary files /dev/null and b/scripts/shimmer-t1.png differ
diff --git a/src/walk.js b/src/walk.js
index d950d41a1..bb436f9d0 100644
--- a/src/walk.js
+++ b/src/walk.js
@@ -447,6 +447,15 @@ async function loadAvatar() {
CAM_LOOK_OFFSET.set(0, height * 0.6, 0);
applyCameraImmediate();
+ // On touch devices, freeze the camera so the joystick walks the avatar
+ // around in world space — the avatar then visibly approaches/recedes via
+ // natural perspective instead of being pinned to screen-center by a follow
+ // cam. AR turns this on for everyone separately.
+ if (IS_TOUCH) {
+ arFrozenCamPos = camera.position.clone();
+ arFrozenCamLook = camLookCurrent.clone();
+ }
+
animationManager.attach(avatar);
// Load only the three clips the controller actually uses — keeps startup
@@ -599,9 +608,19 @@ function disableAR() {
// Hide blob shadow.
blobShadow.material.opacity = 0;
- // Resume follow-camera behavior.
- arFrozenCamPos = null;
- arFrozenCamLook = null;
+ if (IS_TOUCH) {
+ // Touch devices keep the camera frozen even outside AR — re-derive a
+ // fresh third-person pose centered on the current avatar position so
+ // the avatar is back in frame when AR turns off.
+ const off = CAM_OFFSET.clone().multiplyScalar(camZoom);
+ off.applyAxisAngle(new Vector3(1, 0, 0), -cameraPitch);
+ off.applyAxisAngle(new Vector3(0, 1, 0), cameraYaw);
+ arFrozenCamPos = avatarRig.position.clone().add(off);
+ arFrozenCamLook = avatarRig.position.clone().add(CAM_LOOK_OFFSET);
+ } else {
+ arFrozenCamPos = null;
+ arFrozenCamLook = null;
+ }
setStatus('AR off');
}
@@ -827,6 +846,8 @@ const moveForward = new Vector3();
const moveRight = new Vector3();
const tmpQuat = new Quaternion();
const upY = new Vector3(0, 1, 0);
+const _camFwdTmp = new Vector3();
+const _camToAvatarTmp = new Vector3();
function readMoveInput() {
let ix, iy;
@@ -932,8 +953,23 @@ function tick() {
avatarLean += (targetLean - avatarLean) * LEAN_LERP;
if (avatar) avatar.rotation.x = avatarLean;
- // 2. Update camera — frozen in AR, follow-rig otherwise.
+ // 2. Update camera — frozen on touch / in AR, follow-rig on desktop non-AR.
if (arFrozenCamPos && arFrozenCamLook) {
+ // Clamp the avatar so it can't walk through (or past) the frozen camera.
+ // We project (avatar - camera) onto the ground-plane camera-forward axis
+ // and push the avatar back to a minimum forward distance.
+ _camFwdTmp.subVectors(arFrozenCamLook, arFrozenCamPos);
+ _camFwdTmp.y = 0;
+ if (_camFwdTmp.lengthSq() > 1e-6) {
+ _camFwdTmp.normalize();
+ _camToAvatarTmp.subVectors(avatarRig.position, arFrozenCamPos);
+ _camToAvatarTmp.y = 0;
+ const forwardDist = _camToAvatarTmp.dot(_camFwdTmp);
+ const MIN_FRONT_DIST = 0.8;
+ if (forwardDist < MIN_FRONT_DIST) {
+ avatarRig.position.addScaledVector(_camFwdTmp, MIN_FRONT_DIST - forwardDist);
+ }
+ }
camera.position.copy(arFrozenCamPos);
camLookCurrent.copy(arFrozenCamLook);
camera.lookAt(camLookCurrent);