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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat: add shared toon-world materials, shells and visible ink (#10713)
Adam Eivy22a0e2f5727f
diff --git a/.claude/skills/portos-music-video-code/SKILL.md b/.claude/skills/portos-music-video-code/SKILL.mdindex 71096ea1e6..d04ca648ca 100644--- a/.claude/skills/portos-music-video-code/SKILL.md+++ b/.claude/skills/portos-music-video-code/SKILL.md@@ -45,3 +45,41 @@ In **Compose → Document**, choose **Three.js authored worlds → Generate authPortOS packages installed Three.js, license, local fonts, validated author functions and a dependency hash manifest in the immutable document version. It does not install packages, load CDNs or grant network access. Preview supports acyclic local static ES-module graphs (including inline module entrypoints); remote/bare/dynamic imports are unsupported. The same `portosComposition.seek(t)` drives preview, excerpt and final capture, normally 1080p24. Finish every `await` (image decode; video `seeked` plus a presented frame, as the layered template's `seekVideo` waits for) before clearing, drawing or showing a layer: the render waits for the whole seek, but the live preview paints at each await, so clear-then-await flashes in the preview only. Preview media loads on demand when a media `src` first names it, so a far scrub waits for that file inside `seek`; keep decoded-image caches to a few seconds around the playhead (a 2048×1152 frame decodes to about 9 MB), or iPhone Safari reloads the tab. Imported documents may declare supported motion blur; export honors that contract. A valid contract proves execution, not art quality: inspect authored characters/action and changing frames, then the existing treatment/proof workflow decides readiness.Code-only autopilot preserves text direction and defaults to this document authoring path, retaining explicitly saved legacy Code choices. It never requests image guides or an image/video provider. Code-rendered prototypes and visual planning remain required by the production review workflow; skipping raster moodboard assets is not approval to skip art direction.++## Toon worlds++Use the shared `toonWorld.js` kit for cel worlds. Import it at the document root+(`import * as kit from './toonWorld.js'`); PortOS copies missing kit and local+Three.js vendor files into the immutable document. Both spatial and layered+manual documents can import it. Generated spatial sections receive+`ctx.toonWorld`, with no imports or globals in the authored function.++- `toonMaterial(ctx.THREE, { lit, mid, shadow, bands: 3 })` uses explicit palette+ colors, including tinted shadow-map receiving. Lighting selects discrete bands.+- `shellLathe(profile, thickness, segments = 64)` takes base-to-rim+ `[radius, height]` points and closes the inner/outer contour, rim and base.+ Keep thickness smaller than the local radius of curvature. Never use a+ single-surface bowl, cup or shell.+- `solidify(geometry, thickness)` closes the boundary of an orientable triangle+ sheet; vertex normals define its front. It preserves the source geometry.+- `layoutRow({ count, footprint, gap, curve })` and `layoutGrid({...,+ columns})` return centered `{x,y,z}` positions. Footprint is the full,+ axis-aligned width/depth (a number or `{x,z}`), including rim thickness,+ rotation and animation excursions. Curve is `(x, row) => z`; grid rows+ expand around its excursions. Call `warnOverlaps(THREE, placedMeshes)` at+ author time to inspect actual transformed bounding boxes.+- Set `ctx.lens.ink = true` or `{ color, width, depthThreshold,+ normalThreshold }` in spatial sections. Width is in render pixels; depth+ threshold is relative view depth; normal threshold is one minus absolute+ normal alignment. Ink uses the nearest visible depth surface, after+ DOF/bloom and before grade/grain; the optical passes can be on or off.+ Never use inverted-hull outlines on thin or concave meshes.+- For a manual post stack, `createInkPass(THREE, renderer, options)` exposes+ `render(depthTexturedInputTarget, camera, outputTarget)` in linear color+ before the caller's grade/grain. Keep input and output distinct. Call+ `dispose()` when done. The spatial host fuses ink into its existing+ composite pass to avoid a second geometry render.++Render a seven-dish parabolic row at dusk from inside, outside and rim-level+views. Verify visible rim thickness, clear footprints, hidden-edge occlusion,+and ink with DOF/bloom/grade enabled and disabled before accepting a candidate.diff --git a/scripts/ci-test-plan.js b/scripts/ci-test-plan.jsindex 06186a28f7..0b64a76514 100644--- a/scripts/ci-test-plan.js+++ b/scripts/ci-test-plan.js@@ -42,6 +42,7 @@ export const BROWSER_SUITES = ['server/routes/musicVideoRichAuthoring.browser.test.js','server/routes/musicVideoSongRevision.browser.test.js','server/services/musicVideo/documentRender.browser.test.js',+ 'server/services/musicVideo/documentTemplates/shared/toonWorld.browser.test.js',];const browserSuitesIn = (paths) => uniqueSorted(paths.filter((path) => BROWSER_SUITES.includes(path)));diff --git a/server/services/codeAnimation/prompt.js b/server/services/codeAnimation/prompt.jsindex 3d88b07669..3a805029c5 100644--- a/server/services/codeAnimation/prompt.js+++ b/server/services/codeAnimation/prompt.js@@ -459,7 +459,7 @@ export function buildMixedMediaDocumentPrompt({ title, song, palette, treatment,const sections = (song.sections || []).filter((section) => !onlySectionId || section.id === onlySectionId);return [`MEDIA POLICY: ${mediaMode}. Code always authors composition, staging, characters, camera, text, motion and timing. Never generate guide images in code-only mode.`,- renderer === 'three' ? `Write complete authored Three.js worlds, one function render(ctx, env) per section. ctx = { THREE, scene, camera, text, lens }; THREE is the installed locally packaged library; scene is a fresh Scene on EACH seek, camera a PerspectiveCamera, text a transparent Canvas2D overlay with local MV Mono font, lens a per-frame object you set to direct the host post stack. LENS: every lens setting starts off; once any is set the host renders HDR then applies depth of field, bloom, a PBR Neutral highlight shoulder, vignette and grain. For a finished look set ctx.lens.bloom 0.3, vignette 0.3 and grain 0.02 alongside your focus. Set ctx.lens.focus (camera distance, or a THREE.Vector3 such as the subject's head) and ctx.lens.aperture (blur pixels at 1080p; 0 is sharp, 3-6 for wide shots, 10-18 for character close-ups) and animate focus from env.t for rack focus; ctx.lens.maxBlur caps it (default 16). Only colours above ctx.lens.bloomThreshold (default 1) glow, so drive emissive screens, lamps and sparks above 1 (e.g. new THREE.Color(3, 1.6, 0.8)) and raise the threshold above a bright daytime sky so daylight does not haze; exposure is also settable (default 1). Light a character with a key that casts shadows plus a soft fill near the camera so faces stay readable in night or backlit shots, and keep additive particles small and away from the lens. Build modeled characters with articulated limbs and expressive poses, environments with depth/lighting/shadows, props, narrative action and camera choreography. Position every object analytically from env.t/localT; no simulation accumulation. Add geometry/lights to ctx.scene and set ctx.camera explicitly. Use ctx.text for designed typography. Render fills the whole frame; a particle field or text overlays alone are not a scene. Do not create a renderer, DOM elements, textures loaded from files, or another clock. env provides t, localT, frame, width, height, song, section, palette, safe, events and reactiveGain. No imports, require, fetch, Math.random, Date, performance, globalThis, window, document or external assets. The host draws aligned subtitles after your text; reserve lower title-safe space. Use reusable local functions within each section for anatomy and world construction.` : `Write original Canvas 2D section functions for a mixed-media music-video document titled ${JSON.stringify(trimTo(title, 200))}. The host owns the document, song clock, selected local media and lyric pass. Return code functions only; do not request or generate image/video assets.`,+ renderer === 'three' ? `Write complete authored Three.js worlds, one function render(ctx, env) per section. ctx = { THREE, scene, camera, text, lens, toonWorld }; THREE is the installed locally packaged library; scene is a fresh Scene on EACH seek, camera a PerspectiveCamera, text a transparent Canvas2D overlay with local MV Mono font, lens a per-frame object you set to direct the host post stack. LENS: every lens setting starts off; once any is set the host renders HDR then applies depth of field, bloom, a PBR Neutral highlight shoulder, vignette and grain. For a finished look set ctx.lens.bloom 0.3, vignette 0.3 and grain 0.02 alongside your focus. Set ctx.lens.focus (camera distance, or a THREE.Vector3 such as the subject's head) and ctx.lens.aperture (blur pixels at 1080p; 0 is sharp, 3-6 for wide shots, 10-18 for character close-ups) and animate focus from env.t for rack focus; ctx.lens.maxBlur caps it (default 16). Only colours above ctx.lens.bloomThreshold (default 1) glow, so drive emissive screens, lamps and sparks above 1 (e.g. new THREE.Color(3, 1.6, 0.8)) and raise the threshold above a bright daytime sky so daylight does not haze; exposure is also settable (default 1). Light a character with a key that casts shadows plus a soft fill near the camera so faces stay readable in night or backlit shots, and keep additive particles small and away from the lens. For toon worlds use ctx.toonWorld.toonMaterial(THREE, { lit, mid, shadow, bands: 3 }), shellLathe(profile, thickness, segments) for closed bowls/shells, solidify(geometry, thickness) for sheets, and layoutRow/layoutGrid({ count, footprint, gap, curve }) for spaced placements. These helpers are members of ctx.toonWorld. Footprint is full axis-aligned width/depth including thickness and posed excursions; curve(x, row) adds Z. Set ctx.lens.ink = true or { color, width, depthThreshold, normalThreshold } for visible-depth silhouettes/creases before grade/grain. Never use single-surface bowls or inverted-hull ink on thin/concave geometry. Build modeled characters with articulated limbs and expressive poses, environments with depth/lighting/shadows, props, narrative action and camera choreography. Position every object analytically from env.t/localT; no simulation accumulation. Add geometry/lights to ctx.scene and set ctx.camera explicitly. Use ctx.text for designed typography. Render fills the whole frame; a particle field or text overlays alone are not a scene. Do not create a renderer, DOM elements, textures loaded from files, or another clock. env provides t, localT, frame, width, height, song, section, palette, safe, events and reactiveGain. No imports, require, fetch, Math.random, Date, performance, globalThis, window, document or external assets. The host draws aligned subtitles after your text; reserve lower title-safe space. Use reusable local functions within each section for anatomy and world construction.` : `Write original Canvas 2D section functions for a mixed-media music-video document titled ${JSON.stringify(trimTo(title, 200))}. The host owns the document, song clock, selected local media and lyric pass. Return code functions only; do not request or generate image/video assets.`,renderer === 'three' ? '' : CODE_VIDEO_RULES.replace('- Canvas 2D only. No external assets, fonts, or network.', '- Canvas 2D only. No network, remote URLs, filesystem paths or font loading. The host binds only the listed selected project assets; draw over footage/stills without obscuring them, and draw the entire authored world for card scenes, including characters, environments, camera staging, lighting and narrative actions. Local packaged fonts MV Mono, MV Cond and MV Stencil are available; authored titles are allowed, but do not duplicate host subtitles.'),MUSIC_VIDEO_DIRECTION,MUSIC_VIDEO_CHOREOGRAPHY,diff --git a/server/services/codeAnimation/prompt.test.js b/server/services/codeAnimation/prompt.test.jsindex f8aff7dc65..0d882254d3 100644--- a/server/services/codeAnimation/prompt.test.js+++ b/server/services/codeAnimation/prompt.test.js@@ -237,7 +237,7 @@ describe('music-video craft and measured choreography', () => {it('teaches the Three.js author the host lens contract', () => {const prompt = buildMixedMediaDocumentPrompt({ title: 'Lens', song: buildSongDocument(project), palette: {}, scenes: [], renderer: 'three' });- expect(prompt).toContain('ctx = { THREE, scene, camera, text, lens }');+ expect(prompt).toContain('ctx = { THREE, scene, camera, text, lens, toonWorld }');expect(prompt).toContain('ctx.lens.focus');expect(prompt).toContain('ctx.lens.bloomThreshold');});diff --git a/server/services/musicVideo/compositionDocument.js b/server/services/musicVideo/compositionDocument.jsindex 71e70178de..04a9afc506 100644--- a/server/services/musicVideo/compositionDocument.js+++ b/server/services/musicVideo/compositionDocument.js@@ -177,13 +177,39 @@ async function withSharedLyricType(files) {}if (!referenced) return files;const { files: shared } = await collectTree(SHARED_ROOT, { excludeTests: true });- const missing = shared.filter((file) => !rels.has(file.rel));+ const missing = shared.filter((file) => file.rel !== 'toonWorld.js' && !rels.has(file.rel));return missing.length ? [...files, ...missing] : files;}+// Shared toon kit remains a local static graph, including its vendored geometry+// dependency. Authored copies win; unrelated documents gain no dependencies.+async function withSharedToonWorld(files) {+ let referenced = false;+ for (const file of files) {+ if (!/\.(?:html?|m?js)$/i.test(file.rel) || (file.size ?? file.data?.length ?? 0) > LYRIC_TYPE_SCAN_MAX) continue;+ const text = file.data ? file.data.toString('utf8') : await readFile(file.abs, 'utf8');+ if (/\btoonWorld\.js\b/.test(text)) { referenced = true; break; }+ }+ if (!referenced) return files;+ const rels = new Set(files.map(file => file.rel));+ const added = [];+ if (!rels.has('toonWorld.js')) added.push({ rel: 'toonWorld.js', data: await readFile(join(SHARED_ROOT, 'toonWorld.js')) });+ const require = createRequire(import.meta.url);+ const packageRoot = dirname(dirname(require.resolve('three')));+ for (const name of ['three.module.js', 'three.core.js', 'LICENSE']) {+ const rel = `vendor/${name}`;+ if (!rels.has(rel)) added.push({ rel, data: await readFile(join(packageRoot, name === 'LICENSE' ? name : `build/${name}`)) });+ }+ return [...files, ...added];+}+async function storeVersionNow(projectId, inputFiles, source, { draft = false, verifyCurrent = () => {} } = {}) {assertDocumentShape(inputFiles.map((file) => file.rel));- const files = await withSharedLyricType(inputFiles);+ const files = await withSharedToonWorld(await withSharedLyricType(inputFiles));+ const totalBytes = files.reduce((sum, file) => sum + (file.data?.length ?? file.size), 0);+ if (files.length > DOCUMENT_MAX_FILES || totalBytes > DOCUMENT_MAX_BYTES) {+ throw refuse('Shared document assets exceed the document file or byte limit', 'COMPOSITION_DOCUMENT_TOO_LARGE', 413);+ }const initial = await getProject(projectId);await assertDocumentMediaPolicy(initial, files);if (!initial) throw new ServerError('Project not found', { status: 404, code: 'NOT_FOUND' });diff --git a/server/services/musicVideo/compositionDocument.lyricType.test.js b/server/services/musicVideo/compositionDocument.lyricType.test.jsindex 52685bcb83..16a196cdb3 100644--- a/server/services/musicVideo/compositionDocument.lyricType.test.js+++ b/server/services/musicVideo/compositionDocument.lyricType.test.js@@ -9,7 +9,7 @@ vi.mock('../../lib/paths.js', async (importOriginal) => makePathsProxy(await impconst { PATHS } = await import('../../lib/paths.js');const projects = await import('./projects.js');-const { importDocumentTemplate, importDocumentDirectory } = await import('./compositionDocument.js');+const { importDocumentTemplate, importDocumentDirectory, stageGeneratedDocument } = await import('./compositionDocument.js');afterAll(() => cleanupTempDataRoots());@@ -70,4 +70,39 @@ describe('shared lyric-type assets in composition documents', () => {const plain = await importDocumentDirectory(id, await uploaded('no-type', { 'index.html': '<canvas></canvas>' }));expect((await listed(plain.document)).files).toEqual(['index.html']);});+ it('copies the local toon module and vendor graph for imports, preserves authored copies, and omits tests', async () => {+ const { id } = await projects.createProject({ name: 'Example Toon Song' });+ const imported = await importDocumentDirectory(id, await uploaded('toon-import', {+ 'index.html': '<script type="module" src="world.js"></script>',+ 'world.js': "import { shellLathe } from './toonWorld.js';",+ }));+ const { root, files } = await listed(imported.document);+ const { buildDocumentPreview } = await import('./documentPreview.js');+ const preview = await buildDocumentPreview(await projects.getProject(id));+ expect(preview.html).toContain('data:text/javascript;base64,');+ expect(files).toEqual(['index.html', 'toonWorld.js', 'vendor/LICENSE', 'vendor/three.core.js', 'vendor/three.module.js', 'world.js']);+ expect(await readFile(join(root, 'toonWorld.js'), 'utf8')).toContain('export function shellLathe');+ const own = await importDocumentDirectory(id, await uploaded('toon-own', {+ 'index.html': '<script type="module" src="toonWorld.js"></script>',+ 'toonWorld.js': 'export const customKit = true;',+ 'vendor/three.module.js': 'export const customThree = true;',+ }));+ const kept = await listed(own.document);+ expect(await readFile(join(kept.root, 'toonWorld.js'), 'utf8')).toBe('export const customKit = true;');+ expect(await readFile(join(kept.root, 'vendor/three.module.js'), 'utf8')).toBe('export const customThree = true;');+ const layered = await listed((await importDocumentTemplate(id, 'layered')).document);+ const layeredIndex = await readFile(join(layered.root, 'index.html'), 'utf8');+ const layeredWithKit = await importDocumentDirectory(id, await uploaded('layered-toon', {+ 'index.html': layeredIndex.replace('</body>', '<script type="module" src="toonWorld.js"></script></body>'),+ 'engine.js': await readFile(join(layered.root, 'engine.js'), 'utf8'),+ 'cameraRig.js': await readFile(join(layered.root, 'cameraRig.js'), 'utf8'),+ }));+ expect((await listed(layeredWithKit.document)).files).toContain('toonWorld.js');+ expect((await buildDocumentPreview(await projects.getProject(id))).html).toContain('data:text/javascript;base64,');+ const staged = await stageGeneratedDocument(id, [{ rel: 'generated.js', data: Buffer.from('window.PORTOS_MV_GENERATED = {};') }], { renderer: 'three' });+ const spatial = await listed(staged.document);+ expect(spatial.files).toEqual(expect.arrayContaining(['toonWorld.js', 'vendor/three.module.js', 'vendor/three.core.js']));+ expect((await buildDocumentPreview({ ...(await projects.getProject(id)), composition: { mode: 'document', document: staged.document } })).html).toContain('data:text/javascript;base64,');+ });+});diff --git a/server/services/musicVideo/documentTemplates/shared/toonWorld.browser.test.js b/server/services/musicVideo/documentTemplates/shared/toonWorld.browser.test.jsnew file mode 100644index 0000000000..744fd06d5e--- /dev/null+++ b/server/services/musicVideo/documentTemplates/shared/toonWorld.browser.test.js@@ -0,0 +1,137 @@+import { afterAll, beforeAll, describe, expect, it } from 'vitest';+import { existsSync } from 'node:fs';+import { readFile } from 'node:fs/promises';+import { dirname, join } from 'node:path';+import { createRequire } from 'node:module';+import { fileURLToPath } from 'node:url';+import { chromium } from 'playwright-core';+import { browserSuiteCanRun } from '../../../../lib/browserSuiteGate.js';++const require = createRequire(import.meta.url);+const chrome = [process.env.CHROME_PATH, chromium.executablePath(),+ '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/usr/bin/google-chrome', '/usr/bin/chromium',+].find(p => p && existsSync(p));+const canRun = browserSuiteCanRun('toon world browser', { Chrome: chrome });+const root = dirname(fileURLToPath(import.meta.url));+const threeRoot = dirname(require.resolve('three'));++describe.skipIf(!canRun)('toon world headless dish proof', () => {+ let browser, page;+ const errors = [];+ beforeAll(async () => {+ browser = await chromium.launch({ executablePath: chrome, headless: true,+ args: ['--mute-audio', '--use-angle=swiftshader', '--enable-unsafe-swiftshader',+ '--disable-frame-rate-limit', '--disable-background-timer-throttling', '--disable-renderer-backgrounding'] });+ page = await browser.newPage({ viewport: { width: 640, height: 360 } });+ page.on('pageerror', error => errors.push(error.message));+ page.on('console', message => { if (message.type() === 'error') errors.push(message.text()); });+ await page.route('http://toon.test/**', async route => {+ const name = new URL(route.request().url()).pathname.slice(1);+ if (name === '') return route.fulfill({ contentType: 'text/html', body: '<canvas id="world"></canvas><script type="module" src="fixture.js"></script>' });+ if (name === 'fixture.js') return route.fulfill({ contentType: 'text/javascript', body: fixture });+ const files = { 'toonWorld.js': join(root, 'toonWorld.js'), 'engine.js': join(root, '../spatial/engine.js'),+ 'vendor/three.module.js': join(threeRoot, 'three.module.js'), 'vendor/three.core.js': join(threeRoot, 'three.core.js') };+ if (!files[name]) return route.abort();+ return route.fulfill({ contentType: 'text/javascript', body: await readFile(files[name]) });+ });+ await page.goto('http://toon.test/');+ await page.waitForFunction(() => window.proof);+ }, 30000);+ afterAll(async () => { await browser?.close(); });++ it('renders thick, separated dishes and refuses hidden ink with optical passes on/off', async () => {+ const results = await page.evaluate(() => window.proof());+ expect(results.overlaps).toEqual([]);+ expect(results.inkChanged).toBeGreaterThan(300);+ expect(results.hiddenChanged).toBe(0);+ expect(results.flatChanged).toBe(0);+ expect(new Set(results.views).size).toBe(3);+ expect(results.postChanged).toBeGreaterThan(1000);+ expect(results.repeat).toBe(true);+ expect(errors).toEqual([]);+ console.log(`🎨 Toon dish proof: baseline ${results.baselineMs.toFixed(1)}ms, ink ${results.inkMs.toFixed(1)}ms, ratio ${results.ratio.toFixed(2)}`);+ if (process.env.PORTOS_TOON_PROOF_IMAGE) await page.screenshot({ path: process.env.PORTOS_TOON_PROOF_IMAGE });+ expect(results.ratio).toBeLessThanOrEqual(1.5);+ }, 60000);++ it('supports a manual/layered post stack and restores the caller target', async () => {+ const result = await page.evaluate(() => window.manualProof());+ expect(result.changed).toBeGreaterThan(300);+ expect(result.restored).toBe(true);+ expect(result.feedbackRefused).toBe(true);+ expect(errors).toEqual([]);+ });+});++const fixture = `+import * as THREE from './vendor/three.module.js';+import * as kit from './toonWorld.js';+import { createPost } from './engine.js';+const renderer = new THREE.WebGLRenderer({canvas:document.getElementById('world'),preserveDrawingBuffer:true});+renderer.setSize(640,360,false); renderer.shadowMap.enabled=true;+const post=createPost(THREE,renderer);post.resize(640,360);+const scene=new THREE.Scene();scene.background=new THREE.Color('#65556e');+const camera=new THREE.PerspectiveCamera(40,640/360,.1,100);+camera.position.set(9,8,17);camera.lookAt(0,1,0);+const sun=new THREE.DirectionalLight('#ffe2ad',2);sun.position.set(-7,8,4);sun.castShadow=true;+sun.shadow.mapSize.set(512,512);sun.shadow.camera.left=-14;sun.shadow.camera.right=14;+sun.shadow.camera.top=10;sun.shadow.camera.bottom=-10;scene.add(sun);+scene.add(new THREE.AmbientLight('#927fa5',.15));+const material=kit.toonMaterial(THREE,{lit:'#f6d89e',mid:'#b98c88',shadow:'#65518b'});+const profile=Array.from({length:25},(_,i)=>[i/24,(i/24)**2*.6]);+const dishes=kit.layoutRow({count:7,footprint:2.3,gap:.5,curve:x=>Math.cos(x*.2)}).map(p=>{+ const dish=new THREE.Mesh(kit.shellLathe(profile,.12,64),material);+ dish.position.set(p.x,1.2,p.z);dish.castShadow=true;dish.receiveShadow=true;scene.add(dish);+ const stand=new THREE.Mesh(new THREE.CylinderGeometry(.1,.16,1.2,12),material);+ stand.position.set(p.x,.6,p.z);stand.castShadow=true;scene.add(stand);return dish;+});+const ground=new THREE.Mesh(new THREE.PlaneGeometry(80,80),kit.toonMaterial(THREE,{lit:'#cf987b',mid:'#a57280',shadow:'#58436a'}));+ground.rotation.x=-Math.PI/2;ground.receiveShadow=true;scene.add(ground);+const pixels=()=>{const gl=renderer.getContext(),p=new Uint8Array(640*360*4);gl.readPixels(0,0,640,360,gl.RGBA,gl.UNSIGNED_BYTE,p);return p;};+const changed=(a,b,region=()=>true)=>{let count=0;for(let i=0;i<a.length;i+=4)if(region(i/4)&&a.slice(i,i+3).some((v,j)=>Math.abs(v-b[i+j])>3))count++;return count;};+const draw=(lens={})=>{post.render(scene,camera,lens,0);renderer.getContext().finish();return pixels();};+window.manualProof=()=>{+ const input=new THREE.WebGLRenderTarget(640,360,{type:THREE.HalfFloatType,depthTexture:new THREE.DepthTexture(640,360,THREE.FloatType)});+ const output=new THREE.WebGLRenderTarget(640,360,{depthBuffer:false});+ const ink=kit.createInkPass(THREE,renderer);+ renderer.setRenderTarget(input);renderer.render(scene,camera);+ const read=()=>{const p=new Uint8Array(640*360*4);renderer.readRenderTargetPixels(output,0,0,640,360,p);return p;};+ ink.render(input,camera,output,{enabled:false});const baseline=read();+ ink.render(input,camera,output);const outlined=read();+ const restored=renderer.getRenderTarget()===input;+ let feedbackRefused=false;try{ink.render(input,camera,input);}catch{feedbackRefused=true;}+ renderer.setRenderTarget(null);ink.dispose();input.dispose();output.dispose();+ return {changed:changed(baseline,outlined),restored,feedbackRefused};+};+window.proof=()=>{+ const baseline=draw(), ink=draw({ink:true});+ const inkChanged=changed(baseline,ink);+ // Empty ground in the lower left has no silhouette or crease.+ const flatChanged=changed(baseline,ink,index=>{const x=index%640,y=Math.floor(index/640);return x>30&&x<100&&y>30&&y<100;});+ const views=[];+ for(const p of [[9,8,17],[8,.7,14],[0,2,19]]){camera.position.set(...p);camera.lookAt(0,1.2,0);draw({ink:true});views.push(document.getElementById('world').toDataURL());}+ camera.position.set(9,8,17);camera.lookAt(0,1,0);+ const before=draw({ink:true});+ const withPost=draw({ink:true,aperture:6,focus:10,maxBlur:8,bloom:.3,bloomThreshold:.2,exposure:1.1,grain:.03});+ const postChanged=changed(before,withPost);+ const repeat=changed(withPost,draw({ink:true,aperture:6,focus:10,maxBlur:8,bloom:.3,bloomThreshold:.2,exposure:1.1,grain:.03}))===0;+ // A solid flat blocker fills the central screen; dishes remain behind it.+ const blocker=new THREE.Mesh(new THREE.PlaneGeometry(8,8),new THREE.MeshBasicMaterial({color:'#63aa85'}));+ blocker.position.copy(camera.position).multiplyScalar(.65);blocker.lookAt(camera.position);scene.add(blocker);+ const noInk=draw(), yesInk=draw({ink:true});+ const hiddenChanged=changed(noInk,yesInk,index=>{const x=index%640,y=Math.floor(index/640);return x>260&&x<380&&y>130&&y<230;});+ scene.remove(blocker);+ // Alternate measured runs, finish the GPU, and use medians after warmup.+ for(let i=0;i<4;i++){draw();draw({ink:true});}+ const a=[],b=[];+ for(let i=0;i<15;i++){+ for(const enabled of (i%2?[true,false]:[false,true])){+ const start=performance.now();post.render(scene,camera,enabled?{ink:true}:{},0);pixels();+ (enabled?b:a).push(performance.now()-start);+ }+ }+ const median=values=>values.sort((x,y)=>x-y)[Math.floor(values.length/2)];+ const baselineMs=median(a),inkMs=median(b);+ return {overlaps:kit.warnOverlaps(THREE,dishes),inkChanged,hiddenChanged,flatChanged,views,postChanged,repeat,baselineMs,inkMs,ratio:inkMs/baselineMs};+};+`;diff --git a/server/services/musicVideo/documentTemplates/shared/toonWorld.js b/server/services/musicVideo/documentTemplates/shared/toonWorld.jsnew file mode 100644index 0000000000..0c3258f014--- /dev/null+++ b/server/services/musicVideo/documentTemplates/shared/toonWorld.js@@ -0,0 +1,246 @@+// Copied beside a document's local vendor/ directory; never loads a CDN.+import * as GeometryTHREE from './vendor/three.module.js';++/** Cel bands from accumulated illumination, including Three's shadow maps. */+export function toonMaterial(THREE, { lit = '#f6ce86', mid = '#ba817b', shadow = '#57456f', bands = 3 } = {}) {+ if (!Number.isInteger(bands) || bands < 2 || bands > 16) throw new Error('Toon bands must be an integer from 2 to 16');+ const material = new THREE.MeshToonMaterial({ color: '#ffffff' });+ material.onBeforeCompile = shader => {+ Object.assign(shader.uniforms, {+ toonLit: { value: new THREE.Color(lit) }, toonMid: { value: new THREE.Color(mid) },+ toonShadow: { value: new THREE.Color(shadow) }, toonBands: { value: bands },+ });+ shader.fragmentShader = 'uniform vec3 toonLit, toonMid, toonShadow; uniform float toonBands;\n' + shader.fragmentShader;+ shader.fragmentShader = shader.fragmentShader.replace(+ 'vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;',+ `vec3 illumination = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse;+ float level = clamp(max(illumination.r, max(illumination.g, illumination.b)) * PI, 0., 1.);+ level = min(floor(level * toonBands), toonBands - 1.) / (toonBands - 1.);+ vec3 outgoingLight = (level < .5 ? mix(toonShadow, toonMid, level * 2.) : mix(toonMid, toonLit, level * 2. - 1.)) + totalEmissiveRadiance;`);+ };+ material.customProgramCacheKey = () => 'portos-toon-world-v1';+ return material;+}++const positive = (n, name) => {+ if (!Number.isFinite(n) || n <= 0) throw new Error(`${name} must be positive and finite`);+ return n;+};++/** Profile runs from base to rim, as [radius, height] pairs or Vector2s.+ * Thickness offsets inward along the profile normal; the rim and base close.+ * Use a non-self-intersecting profile and thickness smaller than its curvature.+ */+export function shellLathe(profile, thickness, segments = 64) {+ positive(thickness, 'Thickness');+ if (!Number.isInteger(segments) || segments < 3) throw new Error('Lathe needs at least three segments');+ if (!Array.isArray(profile) || profile.length < 2) throw new Error('Lathe needs at least two profile points');+ const points = profile.map(p => new GeometryTHREE.Vector2(p.x ?? p[0], p.y ?? p[1]));+ if (points.some(p => !Number.isFinite(p.x) || !Number.isFinite(p.y) || p.x < 0)) throw new Error('Profile radii must be nonnegative and coordinates finite');+ const inner = points.map((p, i) => {+ const before = points[Math.max(0, i - 1)], after = points[Math.min(points.length - 1, i + 1)];+ const tangent = after.clone().sub(before);+ if (!tangent.length()) throw new Error('Profile points must be distinct');+ tangent.normalize();+ const radius = p.x === 0 ? 0 : p.x - tangent.y * thickness;+ if (radius < 0) throw new Error('Thickness crosses the lathe axis');+ return new GeometryTHREE.Vector2(radius, p.y + (p.x === 0 ? Math.sign(tangent.x) * thickness : tangent.x * thickness));+ });+ return new GeometryTHREE.LatheGeometry([...points, ...inner.reverse(), points[0].clone()], segments);+}++/** Thicken an orientable triangle sheet without modifying the source.+ * Coincident vertices (UV seams / non-indexed triangles) are welded for walls.+ * Input vertex normals define the front; front/back are +/- thickness/2.+ */+export function solidify(geometry, thickness) {+ positive(thickness, 'Thickness');+ const source = geometry.clone();+ if (!source.getAttribute('normal')) source.computeVertexNormals();+ const p = source.getAttribute('position'), n = source.getAttribute('normal');+ if (!p || p.count < 3) throw new Error('Solidify needs triangle positions');+ const indices = source.index ? Array.from(source.index.array) : Array.from({ length: p.count }, (_, i) => i);+ if (indices.length % 3) throw new Error('Solidify needs triangles');+ const positions = [], normals = [], uvs = [], uv = source.getAttribute('uv');+ const weld = new Map(), canonical = [];+ for (let i = 0; i < p.count; i++) {+ const key = [p.getX(i), p.getY(i), p.getZ(i)].map(v => Math.round(v * 1e6)).join(',');+ if (!weld.has(key)) weld.set(key, i);+ canonical[i] = weld.get(key);+ for (const sign of [1, -1]) {+ positions.push(p.getX(i) + sign * n.getX(i) * thickness / 2, p.getY(i) + sign * n.getY(i) * thickness / 2, p.getZ(i) + sign * n.getZ(i) * thickness / 2);+ normals.push(sign * n.getX(i), sign * n.getY(i), sign * n.getZ(i));+ uvs.push(uv?.getX(i) ?? 0, uv?.getY(i) ?? 0);+ }+ }+ const faces = [], edges = new Map();+ for (let i = 0; i < indices.length; i += 3) {+ const [a, b, c] = indices.slice(i, i + 3);+ faces.push(a * 2, b * 2, c * 2, c * 2 + 1, b * 2 + 1, a * 2 + 1);+ for (const [start, end] of [[a, b], [b, c], [c, a]]) {+ const key = [canonical[start], canonical[end]].sort((x, y) => x - y).join(',');+ const edge = edges.get(key);+ if (edge) edge.count++;+ else edges.set(key, { start, end, count: 1 });+ }+ }+ for (const { start, end, count } of edges.values()) {+ if (count > 2) throw new Error('Solidify needs a manifold sheet');+ if (count !== 1) continue;+ // Duplicate wall vertices for a crisp rim and geometric wall normals.+ const offset = positions.length / 3;+ for (const index of [start * 2, start * 2 + 1, end * 2, end * 2 + 1]) {+ positions.push(...positions.slice(index * 3, index * 3 + 3));+ normals.push(0, 0, 0); uvs.push(0, 0);+ }+ faces.push(offset, offset + 1, offset + 2, offset + 2, offset + 1, offset + 3);+ const a = new GeometryTHREE.Vector3().fromArray(positions, offset * 3);+ const b = new GeometryTHREE.Vector3().fromArray(positions, (offset + 1) * 3);+ const c = new GeometryTHREE.Vector3().fromArray(positions, (offset + 2) * 3);+ const normal = b.sub(a).cross(c.sub(a)).normalize();+ for (let j = 0; j < 4; j++) normals.splice((offset + j) * 3, 3, ...normal.toArray());+ }+ source.dispose();+ const result = new GeometryTHREE.BufferGeometry();+ result.setAttribute('position', new GeometryTHREE.Float32BufferAttribute(positions, 3));+ result.setAttribute('normal', new GeometryTHREE.Float32BufferAttribute(normals, 3));+ result.setAttribute('uv', new GeometryTHREE.Float32BufferAttribute(uvs, 2));+ result.setIndex(faces);+ return result;+}++/** Conservative, axis-aligned X/Z footprints; curve(x, row) adds Z only,+ * so it cannot reduce X clearance. Grid rows expand around curve excursions.+ */+export function layoutGrid({ count, footprint, gap = 0, curve = () => 0, columns = Math.ceil(Math.sqrt(count)) }) {+ if (!Number.isInteger(count) || count < 0 || !Number.isInteger(columns) || columns < 1) {+ if (count === 0) return [];+ throw new Error('Layout count and columns must be nonnegative/positive integers');+ }+ const xSize = positive(typeof footprint === 'number' ? footprint : footprint?.x, 'Footprint x');+ const zSize = positive(typeof footprint === 'number' ? footprint : footprint?.z, 'Footprint z');+ if (!Number.isFinite(gap) || gap < 0) throw new Error('Gap must be nonnegative and finite');+ const out = [];+ let previousMax = -Infinity;+ for (let row = 0; out.length < count; row++) {+ const size = Math.min(columns, count - out.length);+ const offsets = Array.from({ length: size }, (_, col) => {+ const x = (col - (size - 1) / 2) * (xSize + gap);+ const z = curve(x, row);+ if (!Number.isFinite(z)) throw new Error('Curve must return a finite Z offset');+ return { x, y: 0, z };+ });+ const minimum = Math.min(...offsets.map(p => p.z));+ const shift = row ? previousMax + zSize + gap - minimum : 0;+ for (const p of offsets) out.push({ ...p, z: p.z + shift });+ previousMax = Math.max(...offsets.map(p => p.z + shift));+ }+ const center = out.length ? (Math.min(...out.map(p => p.z)) + Math.max(...out.map(p => p.z))) / 2 : 0;+ return out.map(p => ({ ...p, z: p.z - center }));+}+export function layoutRow(options) { return layoutGrid({ ...options, columns: Math.max(1, options.count) }); }++/** Explicit author-time check on placed, transformed world bounding boxes. */+export function warnOverlaps(THREE, instances, { warn = message => console.warn(message) } = {}) {+ const boxes = instances.map(object => { object.updateWorldMatrix(true, true); return new THREE.Box3().setFromObject(object); });+ const overlaps = [];+ for (let i = 0; i < boxes.length; i++) for (let j = i + 1; j < boxes.length; j++) {+ const a = boxes[i], b = boxes[j];+ if (Math.min(a.max.x, b.max.x) > Math.max(a.min.x, b.min.x) &&+ Math.min(a.max.y, b.max.y) > Math.max(a.min.y, b.min.y) &&+ Math.min(a.max.z, b.max.z) > Math.max(a.min.z, b.min.z)) overlaps.push([i, j]);+ }+ if (overlaps.length) warn(`⚠️ Toon world: ${overlaps.length} overlapping instance bounds; increase footprint or gap`);+ return overlaps;+}++// Surface normals reconstructed from the nearest visible depth surface. No+// inverted hulls, hidden geometry or extra scene/normal render. Orthographic+// and perspective cameras both use their actual inverse projection.+export const inkShader = /* glsl */`+uniform sampler2D tInkDepth; uniform mat4 uInkInverseProjection;+uniform vec2 uInkRes; uniform vec3 uInkColor;+uniform float uInkWidth, uInkDepthThreshold, uInkNormalThreshold, uInkEnabled;+vec3 inkPosition(vec2 uv){+ float d = texture2D(tInkDepth, uv).x;+ vec4 p = uInkInverseProjection * vec4(uv * 2. - 1., d * 2. - 1., 1.);+ return p.xyz / p.w;+}+float inkEdge(vec3 delta, vec3 nn, vec3 n, float planeThreshold2, float alignmentThreshold){+ float distanceToPlane = dot(delta, n), alignment = dot(n, nn);+ float discontinuity = step(planeThreshold2, distanceToPlane * distanceToPlane);+ float crease = step(alignment * alignment, alignmentThreshold * dot(nn, nn));+ return step(delta.z, 1e-5) * max(discontinuity, crease);+}+vec3 applyInk(vec3 color, vec2 uv){+ if (uInkEnabled < .5 || uInkWidth <= 0.) return color;+ float depth = texture2D(tInkDepth, uv).x;+ if (depth >= .999999) return color;+ vec2 dx = vec2(uInkWidth / uInkRes.x, 0.), dy = vec2(0., uInkWidth / uInkRes.y);+ vec3 p = inkPosition(uv);+ vec3 r = inkPosition(clamp(uv + dx, 0., 1.)) - p;+ vec3 l = p - inkPosition(clamp(uv - dx, 0., 1.));+ vec3 u = inkPosition(clamp(uv + dy, 0., 1.)) - p;+ vec3 d = p - inkPosition(clamp(uv - dy, 0., 1.));+ // Five visible-depth taps supply all four one-sided normals. Choose+ // derivatives on the nearest surface so silhouettes don't tilt its normal.+ vec3 nx = abs(r.z) < abs(l.z) ? r : l;+ vec3 ny = abs(u.z) < abs(d.z) ? u : d;+ vec3 n = cross(nx, ny);+ float normalLength2 = max(dot(n, n), 1e-20);+ float planeThreshold2 = uInkDepthThreshold * uInkDepthThreshold * max(dot(p, p), 1e-6) * normalLength2;+ float alignment2 = (1. - uInkNormalThreshold) * (1. - uInkNormalThreshold);+ float alignmentThreshold = alignment2 * normalLength2;+ float edge = max(+ max(inkEdge(r, cross(r, ny), n, planeThreshold2, alignmentThreshold),+ inkEdge(-l, cross(l, ny), n, planeThreshold2, alignmentThreshold)),+ max(inkEdge(u, cross(nx, u), n, planeThreshold2, alignmentThreshold),+ inkEdge(-d, cross(nx, d), n, planeThreshold2, alignmentThreshold)));+ return mix(color, uInkColor, edge);+}`;++export function createInkPass(THREE, renderer, { color = '#181325', width = 1.5, depthThreshold = .015, normalThreshold = .35 } = {}) {+ const uniforms = {+ tInkDepth: { value: null }, uInkInverseProjection: { value: new THREE.Matrix4() },+ uInkRes: { value: new THREE.Vector2(1, 1) }, uInkColor: { value: new THREE.Color(color) },+ uInkWidth: { value: width }, uInkDepthThreshold: { value: depthThreshold },+ uInkNormalThreshold: { value: normalThreshold }, uInkEnabled: { value: 0 },+ };+ let material, quad, stage, camera;+ const configure = (depth, view, w, h, options = {}) => {+ uniforms.tInkDepth.value = depth; uniforms.uInkInverseProjection.value.copy(view.projectionMatrixInverse);+ uniforms.uInkRes.value.set(w, h); uniforms.uInkEnabled.value = options.enabled === false ? 0 : 1;+ uniforms.uInkColor.value.set(options.color ?? color);+ uniforms.uInkWidth.value = options.width ?? width;+ uniforms.uInkDepthThreshold.value = options.depthThreshold ?? depthThreshold;+ uniforms.uInkNormalThreshold.value = options.normalThreshold ?? normalThreshold;+ };+ return {+ uniforms, configure,+ // Caller renders its scene into a depth-textured target, then invokes this+ // before its own grade/grain. The spatial host fuses it into existing passes.+ render(input, view, output = null, options = {}) {+ if (input === output) throw new Error('Ink input and output targets must be distinct');+ if (!input.depthTexture) throw new Error('Ink requires a depth-textured render target');+ configure(input.depthTexture, view, input.width, input.height, options);+ if (!material) {+ material = new THREE.ShaderMaterial({ uniforms: { ...uniforms, tColor: { value: null } },+ vertexShader: 'varying vec2 vUv; void main(){vUv=uv;gl_Position=vec4(position.xy,0.,1.);}',+ fragmentShader: `varying vec2 vUv; uniform sampler2D tColor; ${inkShader}+ void main(){gl_FragColor=vec4(applyInk(texture2D(tColor,vUv).rgb,vUv),1.);}`,+ depthTest: false, depthWrite: false, toneMapped: false });+ quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), material);+ stage = new THREE.Scene(); stage.add(quad);+ camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);+ }+ material.uniforms.tColor.value = input.texture;+ const previous = renderer.getRenderTarget();+ try { renderer.setRenderTarget(output); renderer.render(stage, camera); }+ finally { renderer.setRenderTarget(previous); }+ },+ dispose() { material?.dispose(); quad?.geometry.dispose(); material = null; },+ };+}++// Imported manual/layered documents can use these directly. Generated section+// functions receive the same exports through ctx.toonWorld.diff --git a/server/services/musicVideo/documentTemplates/shared/toonWorld.test.js b/server/services/musicVideo/documentTemplates/shared/toonWorld.test.jsnew file mode 100644index 0000000000..7dd3528f2d--- /dev/null+++ b/server/services/musicVideo/documentTemplates/shared/toonWorld.test.js@@ -0,0 +1,81 @@+import { describe, expect, it, vi } from 'vitest';+import { readFile } from 'node:fs/promises';+import { createRequire } from 'node:module';+import { dirname, join } from 'node:path';+import { pathToFileURL } from 'node:url';++const require = createRequire(import.meta.url);+const threeUrl = pathToFileURL(join(dirname(require.resolve('three')), 'three.module.js')).href;+const THREE = await import(threeUrl);+// The shipped source resolves beside a stored document's vendor/ directory.+const source = (await readFile(new URL('./toonWorld.js', import.meta.url), 'utf8')).replace("'./vendor/three.module.js'", JSON.stringify(threeUrl));+const { layoutRow, layoutGrid, shellLathe, solidify, warnOverlaps } = await import(`data:text/javascript;base64,${Buffer.from(source).toString('base64')}`);++describe('toon world geometry and placement', () => {+ it('keeps footprint clearance for empty, single, long, and curved rows/grids', () => {+ for (const count of [0, 1, 2, 7, 101]) for (const footprint of [0.01, 1, 23]) for (const gap of [0, 0.3]) {+ for (const layout of [layoutRow, layoutGrid]) {+ const points = layout({ count, footprint, gap, curve: x => Math.sin(x) * 4 });+ expect(points).toHaveLength(count);+ for (let i = 0; i < points.length; i++) for (let j = i + 1; j < points.length; j++) {+ expect(Math.max(Math.abs(points[i].x - points[j].x), Math.abs(points[i].z - points[j].z)) + 1e-8).toBeGreaterThanOrEqual(footprint + gap);+ }+ }+ }+ expect(() => layoutRow({ count: 7, footprint: 0 })).toThrow();+ expect(() => layoutRow({ count: 7, footprint: 2, gap: -1 })).toThrow();+ });++ it('closes a bowl with normals facing out of the solid on outer and inner surfaces', () => {+ const profile = Array.from({ length: 17 }, (_, i) => [i / 16, (i / 16) ** 2 * .5]);+ const shell = shellLathe(profile, .08, 32);+ const n = shell.getAttribute('normal'), p = shell.getAttribute('position');+ const stride = profile.length * 2 + 1;+ const outer = 8, inner = profile.length + 8;+ expect(n.getY(outer)).toBeLessThan(-.5); // outside/bottom+ expect(n.getY(inner)).toBeGreaterThan(.5); // inside/top+ expect(n.getZ(outer)).toBeGreaterThan(.1);+ expect(n.getZ(inner)).toBeLessThan(-.1);+ expect(p.getY(profile.length * 2 - 1) - p.getY(0)).toBeCloseTo(.08);+ // Radial seam and contour seam close geometrically.+ for (let i = 0; i < stride; i++) {+ expect(p.getX(i)).toBeCloseTo(p.getX(i + 32 * stride));+ expect(p.getZ(i)).toBeCloseTo(p.getZ(i + 32 * stride));+ }+ expect(() => shellLathe([[0, 0], [.01, 1]], .5)).toThrow();+ shell.dispose();+ const dome = shellLathe([[1, 0], [.7, .7], [0, 1]], .08, 32);+ const dp = dome.getAttribute('position'), dn = dome.getAttribute('normal');+ expect(dp.getY(3)).toBeLessThan(dp.getY(2)); // tip offset goes into a dome+ expect(dn.getY(1)).toBeGreaterThan(0);+ expect(dn.getY(4)).toBeLessThan(0);+ dome.dispose();+ });++ it('solidifies indexed and unindexed sheets with only boundary walls and preserved front/back normals', () => {+ const sheet = new THREE.PlaneGeometry(2, 2, 2, 2);+ for (const input of [sheet, sheet.toNonIndexed()]) {+ const result = solidify(input, .2);+ expect(result.index.count).toBe(sheet.index.count * 2 + 8 * 6);+ const n = result.getAttribute('normal'), p = result.getAttribute('position');+ expect(n.getZ(0)).toBe(1); expect(n.getZ(1)).toBe(-1);+ expect(p.getZ(0)).toBeCloseTo(.1); expect(p.getZ(1)).toBeCloseTo(-.1);+ result.dispose();+ }+ expect(sheet.getAttribute('position').getZ(0)).toBe(0);+ sheet.dispose();+ });++ it('warns for actual transformed bounds, while touching or spaced bounds are allowed', () => {+ const objects = layoutRow({ count: 7, footprint: 2, gap: .1 }).map(p => {+ const mesh = new THREE.Mesh(new THREE.BoxGeometry(2, 1, 2));+ mesh.position.set(p.x, p.y, p.z); return mesh;+ });+ const warn = vi.fn();+ expect(warnOverlaps(THREE, objects, { warn })).toEqual([]);+ objects[1].position.copy(objects[0].position);+ expect(warnOverlaps(THREE, objects, { warn })).toEqual([[0, 1]]);+ expect(warn).toHaveBeenCalledOnce();+ objects.forEach(o => { o.geometry.dispose(); o.material.dispose(); });+ });+});diff --git a/server/services/musicVideo/documentTemplates/spatial/engine.js b/server/services/musicVideo/documentTemplates/spatial/engine.jsindex 6748ca6adb..0991f3d834 100644--- a/server/services/musicVideo/documentTemplates/spatial/engine.js+++ b/server/services/musicVideo/documentTemplates/spatial/engine.js@@ -1,3 +1,5 @@+import * as toonWorld from './toonWorld.js';+export function mount(THREE) {const mv = window.PORTOS_MV;@@ -67,7 +69,7 @@ globalThis.portosComposition = {text.reset();try {text.save();- fn({ THREE, scene, camera, text, lens }, { t, localT: t - section.startSec, frame: Math.floor(t * mv.render.fps),+ fn({ THREE, scene, camera, text, lens, toonWorld }, { t, localT: t - section.startSec, frame: Math.floor(t * mv.render.fps),width, height, song: authored.song, section, palette: authored.palette,safe: { x: width * 0.1, y: height * 0.1, w: width * 0.8, h: height * 0.8 },events: state.activeEvents || [], reactiveGain: state.reactiveGain ?? 1 });@@ -105,7 +107,8 @@ const LENS_DEFAULTS = Object.freeze({ focus: 10, aperture: 0, maxBlur: 16, bloom// and a composite that compresses highlights (PBR Neutral shoulder), encodes sRGB and adds// a vignette and frame-keyed grain. Every pass runs every frame so GPU// resources are allocated once and seeks stay deterministic.-function createPost(THREE, renderer) {+export function createPost(THREE, renderer) {+ const ink = toonWorld.createInkPass(THREE, renderer);const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);const quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2));const stage = new THREE.Scene(); stage.add(quad);@@ -149,6 +152,7 @@ function createPost(THREE, renderer) {gl_FragColor = vec4(c, 1.);}`, { tColor: { value: null }, uStep: { value: new THREE.Vector2() } });const composite = pass(/* glsl */`+ ${toonWorld.inkShader}varying vec2 vUv; uniform sampler2D tColor, tBloom; uniform vec2 uRes;uniform float uBloom, uExposure, uVignette, uGrain, uFrame;// Khronos PBR Neutral's highlight shoulder without its toe offset, so@@ -162,12 +166,12 @@ function createPost(THREE, renderer) {// 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;+ vec3 c = (applyInk(texture2D(tColor, vUv).rgb, vUv) + 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 + 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 } });+ }`, { 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 }, ...ink.uniforms });let targets = [];let hdr, hdrDepth, colour, small, smallB, w = 1, h = 1;// A software rasterizer (CI, GPU-less hosts) multiplies every MSAA sample, so@@ -182,7 +186,7 @@ function createPost(THREE, renderer) {for (const target of targets) { target.depthTexture?.dispose(); target.dispose(); }w = width; h = height;const linear = { type: THREE.HalfFloatType, depthBuffer: false };- // Depth is only sampled for depth of field, so only that target carries a+ // Depth is sampled for depth of field and visible-surface ink, so only that target carries a// depth texture (resolving one is a full extra copy every frame).hdr = new THREE.WebGLRenderTarget(w, h, { type: THREE.HalfFloatType, samples });hdrDepth = new THREE.WebGLRenderTarget(w, h, { type: THREE.HalfFloatType, samples, depthTexture: new THREE.DepthTexture(w, h, THREE.FloatType) });@@ -210,11 +214,13 @@ function createPost(THREE, renderer) {// that does nothing skips its pass outright, and an untouched lens skips// the HDR path entirely.const focusing = aperture > 0 && maxBlur >= 0.5;- if (!focusing && !bloom && !vignette && !grain && exposure === 1) {+ const inking = Boolean(lens.ink);+ if (!inking && !focusing && !bloom && !vignette && !grain && exposure === 1) {renderer.setRenderTarget(null); renderer.render(world, view);return;}- const sceneTarget = focusing ? hdrDepth : hdr;+ const sceneTarget = focusing || inking ? hdrDepth : hdr;+ ink.configure(sceneTarget.depthTexture, view, w, h, { ...(typeof lens.ink === 'object' ? lens.ink : {}), enabled: inking });renderer.setRenderTarget(sceneTarget); renderer.clear(); renderer.render(world, view);let sharp = sceneTarget.texture;if (focusing) {