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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
refactor(ar): extract room-light read into a pure, tested function
nirholas2e34b8960a5e
diff --git a/src/ar-studio.js b/src/ar-studio.jsindex 1c7cbbf50..74f44884a 100644--- a/src/ar-studio.js+++ b/src/ar-studio.js@@ -39,8 +39,9 @@ import {} from './ar/pinch-scale.js';import {deserializeScene, fitTransform, MAX_PLACEMENTS, normalizeGlbUrl,- parseSrcParams, sceneFromHashParam, serializeScene, SPAWN_DISTANCE_M,- spawnPointInFront, studioSceneUrl, studioShareUrl, touchAngle, twistDelta,+ parseSrcParams, roomLightFromPixels, sceneFromHashParam, serializeScene,+ SPAWN_DISTANCE_M, spawnPointInFront, studioSceneUrl, studioShareUrl,+ touchAngle, twistDelta,} from './ar/studio-scene.js';import { renderQRToSVG } from './erc8004/qr.js';import { deriveVerticalFovDeg, DEFAULT_DIAG_FOV_DEG } from './irl/camera-fov.js';@@ -603,30 +604,14 @@ function sampleCameraLight() {} catch {return; // frame not readable yet — try again next tick}- let rSum = 0, gSum = 0, bSum = 0, lumaSum = 0;- const px = data.length / 4;- for (let i = 0; i < data.length; i += 4) {- const r = data[i], g = data[i + 1], b = data[i + 2];- rSum += r; gSum += g; bSum += b;- lumaSum += 0.2126 * r + 0.7152 * g + 0.0722 * b;- }- const luma = lumaSum / px / 255; // 0 (black room) → 1 (blown out)- const target = Math.min(1.35, Math.max(0.4, 0.35 + luma * 1.25));- hemi.intensity += (target * HEMI_BASE - hemi.intensity) * 0.4;- sun.intensity += (target * SUN_BASE - sun.intensity) * 0.4;-- // White-balance tint: normalize the mean colour to its own brightness, then- // pull it a third of the way toward the room's cast (never a full tint — the- // PBR albedo should still read as itself). Guard a black frame (mean ~0).- const mean = (rSum + gSum + bSum) / (px * 3);- if (mean > 6) {- const tr = 1 + ((rSum / px / mean) - 1) * 0.33;- const tg = 1 + ((gSum / px / mean) - 1) * 0.33;- const tb = 1 + ((bSum / px / mean) - 1) * 0.33;- roomTint.setRGB(tr, tg, tb);- hemi.color.lerp(roomTint, 0.4);- sun.color.lerp(roomTint, 0.4);- }+ // Pure read (tested in studio-scene.test.js); ease the result onto the lights+ // so it adapts over a couple of seconds instead of popping frame to frame.+ const { intensity, tint } = roomLightFromPixels(data);+ hemi.intensity += (intensity * HEMI_BASE - hemi.intensity) * 0.4;+ sun.intensity += (intensity * SUN_BASE - sun.intensity) * 0.4;+ roomTint.setRGB(tint.r, tint.g, tint.b);+ hemi.color.lerp(roomTint, 0.4);+ sun.color.lerp(roomTint, 0.4);}function startLightMatching() {diff --git a/src/ar/studio-scene.js b/src/ar/studio-scene.jsindex 2b27674ac..4193f257f 100644--- a/src/ar/studio-scene.js+++ b/src/ar/studio-scene.js@@ -24,6 +24,40 @@ export const MAX_PLACEMENTS = 20;/** How far (m) in front of the camera a newly added model lands. */export const SPAWN_DISTANCE_M = 1.6;+/**+ * Read the room's light from a downsampled camera frame (RGBA bytes). Returns a+ * light INTENSITY multiplier (dim room → <1, bright room → >1) and a white-+ * balance TINT (a warm-lamp room casts warm, daylight casts cool) so a placed+ * model belongs to the room instead of glowing neutral on top of it. Pure — the+ * caller does the canvas read and applies the result to the scene lights.+ *+ * @param {Uint8ClampedArray|number[]} data RGBA pixels (length divisible by 4).+ * @returns {{ intensity: number, tint: { r: number, g: number, b: number } }}+ * intensity in [0.4, 1.35]; tint near {1,1,1} for neutral light, each channel+ * pulled a third of the way toward the room's cast. A black/empty frame+ * returns neutral tint and the floor intensity.+ */+export function roomLightFromPixels(data) {+ const px = data && data.length ? data.length / 4 : 0;+ if (!px) return { intensity: 0.4, tint: { r: 1, g: 1, b: 1 } };+ let rSum = 0, gSum = 0, bSum = 0, lumaSum = 0;+ for (let i = 0; i < data.length; i += 4) {+ const r = data[i], g = data[i + 1], b = data[i + 2];+ rSum += r; gSum += g; bSum += b;+ lumaSum += 0.2126 * r + 0.7152 * g + 0.0722 * b;+ }+ const luma = lumaSum / px / 255; // 0 (black) → 1 (blown out)+ const intensity = Math.min(1.35, Math.max(0.4, 0.35 + luma * 1.25));++ // Normalize the mean colour to its own brightness so only the CAST remains,+ // then pull a third of the way toward it (full tint would recolour the+ // model's own albedo). A near-black frame has no reliable cast → stay neutral.+ const mean = (rSum + gSum + bSum) / (px * 3);+ if (mean <= 6) return { intensity, tint: { r: 1, g: 1, b: 1 } };+ const pull = (sum) => 1 + ((sum / px / mean) - 1) * 0.33;+ return { intensity, tint: { r: pull(rSum), g: pull(gSum), b: pull(bSum) } };+}+/*** Normalize a raw model bounding box into a floor-resting placement transform.*diff --git a/tests/ar-studio-scene.test.js b/tests/ar-studio-scene.test.jsindex c4ec9ee0b..68f7989fa 100644--- a/tests/ar-studio-scene.test.js+++ b/tests/ar-studio-scene.test.js@@ -15,6 +15,7 @@ import {normalizeGlbUrl,parseSrcParams,PROP_TARGET_SIZE_M,+ roomLightFromPixels,SCALE_MAX,SCALE_MIN,sceneFromHashParam,@@ -231,6 +232,54 @@ describe('studioSceneUrl', () => {});});+describe('roomLightFromPixels', () => {+ // Build a downsampled RGBA buffer of a single flat colour.+ const flat = (r, g, b, n = 16) => {+ const a = new Uint8ClampedArray(n * 4);+ for (let i = 0; i < a.length; i += 4) { a[i] = r; a[i + 1] = g; a[i + 2] = b; a[i + 3] = 255; }+ return a;+ };++ it('reads a dim room as low intensity, a bright room as high', () => {+ expect(roomLightFromPixels(flat(20, 20, 20)).intensity).toBeLessThan(0.6);+ expect(roomLightFromPixels(flat(240, 240, 240)).intensity).toBeGreaterThan(1.2);+ });++ it('clamps intensity to the [0.4, 1.35] band', () => {+ expect(roomLightFromPixels(flat(0, 0, 0)).intensity).toBe(0.4);+ expect(roomLightFromPixels(flat(255, 255, 255)).intensity).toBeLessThanOrEqual(1.35);+ });++ it('stays neutral for grey light (no false tint)', () => {+ const { tint } = roomLightFromPixels(flat(128, 128, 128));+ expect(tint.r).toBeCloseTo(1, 5);+ expect(tint.g).toBeCloseTo(1, 5);+ expect(tint.b).toBeCloseTo(1, 5);+ });++ it('casts warm for a warm-lamp room and cool for daylight', () => {+ const warm = roomLightFromPixels(flat(220, 160, 90)).tint;+ expect(warm.r).toBeGreaterThan(1);+ expect(warm.b).toBeLessThan(1);+ const cool = roomLightFromPixels(flat(120, 150, 210)).tint;+ expect(cool.b).toBeGreaterThan(1);+ expect(cool.r).toBeLessThan(1);+ });++ it('never over-tints — the cast is a partial pull, not a full recolour', () => {+ // Pure red light: a full tint would zero G and B. The 1/3 pull keeps them well above 0.+ const { tint } = roomLightFromPixels(flat(255, 0, 0));+ expect(tint.g).toBeGreaterThan(0.5);+ expect(tint.b).toBeGreaterThan(0.5);+ expect(tint.r).toBeGreaterThan(1);+ });++ it('degrades an empty or near-black frame to neutral', () => {+ expect(roomLightFromPixels(new Uint8ClampedArray(0))).toEqual({ intensity: 0.4, tint: { r: 1, g: 1, b: 1 } });+ expect(roomLightFromPixels(flat(1, 1, 1)).tint).toEqual({ r: 1, g: 1, b: 1 });+ });+});+describe('studioShareUrl', () => {it('embeds unique sources up to the cap', () => {const url = studioShareUrl('https://three.ws', [