Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

focus ctx.lens on a point by its view-space depth and keep grain precise in long songs

[._.]/ Adam Eivy814862a87f02
diff --git a/server/services/musicVideo/documentRender.browser.test.js b/server/services/musicVideo/documentRender.browser.test.js
index 878d792a35..0dee2a1eb6 100644
--- a/server/services/musicVideo/documentRender.browser.test.js
+++ b/server/services/musicVideo/documentRender.browser.test.js
@@ -197,6 +197,38 @@ describe.skipIf(!canRun)('layered template with real Chrome and ffmpeg', () => {
const focused = await edgeEnergy();
await withLens({ grain: 0, focus: 1, aperture: 24, maxBlur: 24 });
expect(await edgeEnergy()).toBeLessThan(focused * 0.8);
+ // A Vector3 focus is view-space depth, not straight-line distance from the
+ // camera's local position: an off-axis head seen through a rig-parented
+ // camera stays as sharp as with no depth of field at all.
+ const headSharpness = (aperture) => page.evaluate(async (aperture) => {
+ window.PORTOS_MV_GENERATED.sections.world = (ctx, env) => {
+ window.authoredWorld(ctx, env);
+ const { THREE, scene, camera } = ctx;
+ const rig = new THREE.Group(); rig.position.set(2.5, 0, 4); scene.add(rig); rig.add(camera);
+ // A striped target card at the focus point: its edges blur visibly if
+ // the focal plane misses it.
+ const head = new THREE.Vector3(0, 2.2, 0.6);
+ rig.updateMatrixWorld(true);
+ const card = new THREE.Group(); card.position.copy(head); scene.add(card);
+ card.add(new THREE.Mesh(new THREE.PlaneGeometry(1, 1), new THREE.MeshBasicMaterial({ color: 0x000000 })));
+ for (let i = -2; i <= 2; i++) { const bar = new THREE.Mesh(new THREE.PlaneGeometry(0.06, 1), new THREE.MeshBasicMaterial({ color: 0xffffff })); bar.position.set(i * 0.18, 0, 0.001); card.add(bar); }
+ card.lookAt(camera.getWorldPosition(new THREE.Vector3()));
+ Object.assign(ctx.lens, { grain: 0, vignette: 0, bloom: 0, focus: head, aperture, maxBlur: 24 });
+ window.headOnScreen = head.clone().project(camera);
+ };
+ await window.portosComposition.seek(0);
+ const world = document.getElementById('world');
+ const x = Math.round((window.headOnScreen.x + 1) / 2 * world.width) - 40, y = Math.round((1 - window.headOnScreen.y) / 2 * world.height) - 40;
+ const probe = document.createElement('canvas'); probe.width = 80; probe.height = 80;
+ const g = probe.getContext('2d'); g.drawImage(world, x, y, 80, 80, 0, 0, 80, 80);
+ const { data } = g.getImageData(0, 0, 80, 80);
+ let sum = 0; for (let i = 4; i < data.length; i += 4) sum += Math.abs(data[i] - data[i - 4]);
+ return { sum, offAxis: Math.abs(window.headOnScreen.x) };
+ }, aperture);
+ const sharpHead = await headSharpness(0);
+ const focusedHead = await headSharpness(18);
+ expect(sharpHead.offAxis).toBeGreaterThan(0.1);
+ expect(focusedHead.sum).toBeGreaterThan(sharpHead.sum * 0.99); // straight-line distance from the local position gives ~0.95
expect(await page.evaluate(() => window.liveTextureCount())).toBe(textures);
expect(warnings.filter((text) => text.includes('PCFSoftShadowMap'))).toEqual([]);
expect(errors).toEqual([]);
diff --git a/server/services/musicVideo/documentTemplates/spatial/engine.js b/server/services/musicVideo/documentTemplates/spatial/engine.js
index 209ba88fda..3b21114434 100644
--- a/server/services/musicVideo/documentTemplates/spatial/engine.js
+++ b/server/services/musicVideo/documentTemplates/spatial/engine.js
@@ -156,12 +156,13 @@ function createPost(THREE, renderer) {
return mix(c, vec3(np), 1. - 1. / (.15 * (p - np) + 1.));
}
vec3 srgb(vec3 c){ return mix(c * 12.92, 1.055 * pow(c, vec3(1. / 2.4)) - .055, step(.0031308, c)); }
+ // The frame is wrapped so sin() stays in precise range late in a long song.
float hash(vec2 p){ return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }
void main(){
vec3 c = (texture2D(tColor, vUv).rgb + texture2D(tBloom, vUv).rgb * uBloom) * uExposure;
c = srgb(clamp(neutral(max(c, 0.)), 0., 1.));
vec2 q = vUv - .5; c *= 1. - dot(q, q) * uVignette * 1.8;
- c += (hash(vUv * uRes + uFrame * 7.13) - .5) * uGrain;
+ c += (hash(vUv * uRes + mod(uFrame, 251.) * 7.13) - .5) * uGrain;
gl_FragColor = vec4(clamp(c, 0., 1.), 1.);
}`, { tColor: { value: null }, tBloom: { value: null }, uRes: { value: new THREE.Vector2() }, uBloom: { value: 0 }, uExposure: { value: 1 }, uVignette: { value: 0 }, uGrain: { value: 0 }, uFrame: { value: 0 } });
let targets = [];
@@ -181,7 +182,12 @@ function createPost(THREE, renderer) {
render(world, view, lens, frame) {
const px = h / 1080;
const num = (value, fallback, lo, hi) => Number.isFinite(value) ? Math.min(hi, Math.max(lo, value)) : fallback;
- const focus = lens.focus?.isVector3 ? lens.focus.distanceTo(view.position) : num(lens.focus, LENS_DEFAULTS.focus, 0.01, 1e5);
+ // The DOF shader compares view-space depth, so a focus point is projected
+ // into camera space: off-axis subjects and rig-parented cameras both stay sharp.
+ // renderer.render has not refreshed the camera matrices yet.
+ let focus;
+ if (lens.focus?.isVector3) { view.updateMatrixWorld(); focus = Math.max(0.01, -lens.focus.clone().applyMatrix4(view.matrixWorldInverse).z); }
+ else focus = num(lens.focus, LENS_DEFAULTS.focus, 0.01, 1e5);
renderer.setRenderTarget(hdr); renderer.clear(); renderer.render(world, view);
dof.uniforms.tColor.value = hdr.texture; dof.uniforms.tDepth.value = hdr.depthTexture;
dof.uniforms.uRes.value.set(w, h);