diff --git a/src/components/editors/Nodes.svelte b/src/components/editors/Nodes.svelte index b896f13fb..4dc31ac89 100644 --- a/src/components/editors/Nodes.svelte +++ b/src/components/editors/Nodes.svelte @@ -110,7 +110,7 @@ import { lintFlowGraph, type FlowLintFinding } from '$lib/flowLint.js'; // 78 F24 import { fittingNodes, wireMenuRows, candidateOutputs, type WireEnd } from '$lib/wireSearch.js'; // 78 F1 import { animateInEditor, toggleAnimateInEditor } from '$lib/editorMotion'; // 78 F6 - import { liveStateGraph, nodeLiveStates, clearLiveStates } from '$lib/nodeLiveState'; // 78 F7 + import { liveStateGraph, nodeLiveStates, clearLiveStates, INERT_LINE } from '$lib/nodeLiveState'; // 78 F7 import { variadicSocketExists, switcherItems, MAX_SWITCHER_ITEMS, SWITCHER_TYPES, switcherVType } from '$lib/variadicNodes.js'; // 37 (R6) import { removeVariadicSocket, setSwitcherType, addSwitcherItem } from '$lib/variadicEdit.js'; // 37 (R6) import KitButton from '../ui/Button.svelte'; // 37: the Switcher ⓘ rows on the redesign kit @@ -2293,7 +2293,7 @@ {#each liveChips as chip (chip.id)} -
{chip.text}
+
{chip.text}
{/each} @@ -2527,6 +2527,10 @@ overflow: hidden; text-overflow: ellipsis; } + /* 102b-papercuts: an effect that acts on nothing (the words say it too: not colour alone) */ + .flow-live-state.flow-live-warn { + color: var(--warn-text); + } /* 78 F6: the motion switch reads as OFF at a glance (not by colour alone: the glyph is a pause) */ :global(.svelte-flow__controls-button.flow-motion-off) { color: var(--accent); diff --git a/src/components/editors/nodes/EffectNode.svelte b/src/components/editors/nodes/EffectNode.svelte index 71459c4ac..7b0e3e474 100644 --- a/src/components/editors/nodes/EffectNode.svelte +++ b/src/components/editors/nodes/EffectNode.svelte @@ -22,6 +22,12 @@ + {#if data.type === 'setuniform'} + + + + {/if}
{#if data.type === 'lookat'} @@ -42,6 +48,8 @@

the name shown beside that param in the Shader editor

+ color + vector {:else if data.type === 'visibility'}
+ {:else if spec.optional} + +
{@render geoSlider(spec, 'geo-' + spec.key)}
{:else} - editGeometry(spec.key, spec.kind === 'int' ? Math.round(v) : v)} - /> + {@render geoSlider(spec, undefined)} {/if} {/each} {/if} + {:else if roundOffer && !multiCount} + + {#await import('./RoundedBoxOffer.svelte') then m} geoTick++} />{/await} {/if} {#if $selectedObject?.userData?.terrain && !multiCount} diff --git a/src/components/menu/RoundedBoxOffer.svelte b/src/components/menu/RoundedBoxOffer.svelte new file mode 100644 index 000000000..5a9b5133a --- /dev/null +++ b/src/components/menu/RoundedBoxOffer.svelte @@ -0,0 +1,26 @@ + + +
+

+ This mesh is a rounded box ({offer.width} x {offer.height} x {offer.depth} m, corner {offer.radius} m). +

+ { + void import('$lib/roundedBoxConvert.js').then((m) => { + if (m.convertToRoundedBox(uuid)) onconverted(); + }); + }}>Convert to a rounded Box +
diff --git a/src/components/menu/scene/FogKindRows.svelte b/src/components/menu/scene/FogKindRows.svelte new file mode 100644 index 000000000..646b55a2a --- /dev/null +++ b/src/components/menu/scene/FogKindRows.svelte @@ -0,0 +1,45 @@ + + + + {#snippet control()} + editEnvSky({ fog: fogKindPatch(fog, v) })} + /> + {/snippet} + +{#if kind !== 'linear'} + editEnvSky({ fog: { density: v } })} /> +{/if} +{#if layer} + editEnvSky({ fog: { base: v } })} /> + editEnvSky({ fog: { thickness: v } })} /> +{/if} diff --git a/src/components/menu/scene/HdriSection.svelte b/src/components/menu/scene/HdriSection.svelte index be56500b1..e68f01409 100644 --- a/src/components/menu/scene/HdriSection.svelte +++ b/src/components/menu/scene/HdriSection.svelte @@ -41,20 +41,25 @@ } /** @param {any} event */ async function onUpload(event) { - const file = event.currentTarget.files?.[0]; + const picked = [...(event.currentTarget.files ?? [])]; event.currentTarget.value = ''; - if (!file) return; - if (!/\.(hdr|exr)$/i.test(file.name)) { - showToast('Pick a .hdr or .exr file'); + if (!picked.length) return; + // 102b-papercuts: a cube map (a cross, a strip, or six face files) or a plain image is converted to an + // equirectangular .hdr first; a 2:1 .hdr / .exr goes in untouched, as before + const { prepareSky } = await import('$lib/hdri/cubeImport.js'); + const ready = await prepareSky(await Promise.all(picked.map(async (f) => ({ name: f.name, buffer: await f.arrayBuffer() })))); + if ('error' in ready) { + showToast(ready.error); return; } - if (file.size > MAX_UPLOAD) { + if (ready.bytes.byteLength > MAX_UPLOAD) { showToast('That HDRI is over 25 MB — peers could not receive it. A 1k or 2k file is plenty.'); return; } try { - const item = await addItemFromBytes(await file.arrayBuffer(), file.name, null, { imported: true, kind: 'hdri' }); + const item = await addItemFromBytes(/** @type {ArrayBuffer} */ (ready.bytes.buffer), ready.name, null, { imported: true, kind: 'hdri' }); if (item?.hash) pickCustom(item); + if (ready.converted && ready.converted !== 'image') showToast('Cube map converted to a sky image: ' + ready.name); } catch (err) { showToast('Could not add that HDRI'); console.warn('[hdri] upload failed', err); @@ -77,7 +82,7 @@ ); -
+
- +
{#if hdri} target.input. End at a "' + cat.outputNodeFor(domain) + '" node.', - uniforms: 'a param marked uniform compiles to u__; a float one can be driven by a flow "setuniform" node (data.uniform = that name) on the same object.' + uniforms: 'a param marked uniform compiles to u__; a flow "setuniform" node (data.uniform = that name) on the same object drives it: data.value for a float, data.color ("#rrggbb") or data.vector ([x, y, z]) for a vec3/vec2/vec4.' }; } @@ -1474,7 +1474,7 @@ export const LOOK_AI_TOOLS = [ function: { name: 'create_shader', description: - 'Give objects a node shader, written as text like flow graphs: one line per node (id = type {param: value}), one per edge (source[.output] -> target.input), ending at a "surface" node. Example: c = color {value: "#ff8800"}\\nout = surface\\nc -> out.emissive. Call describe_shader_nodes for the types. Each object gets its own copy. Compiler errors come back in the result and nothing is written. Returns the graph key and its uniforms: a float one (u__) can be driven by a flow "setuniform" node on that object. scene: true makes the default for every object, which asks the host first.', + 'Give objects a node shader, written as text like flow graphs: one line per node (id = type {param: value}), one per edge (source[.output] -> target.input), ending at a "surface" node. Example: c = color {value: "#ff8800"}\\nout = surface\\nc -> out.emissive. Call describe_shader_nodes for the types. Each object gets its own copy. Compiler errors come back in the result and nothing is written. Returns the graph key and its uniforms (u__): a flow "setuniform" node on that object drives a float, colour or vector one. scene: true makes the default for every object, which asks the host first.', parameters: { type: 'object', properties: { diff --git a/src/lib/ai/runtimeCore.js b/src/lib/ai/runtimeCore.js index 9929cd6f6..5340ece07 100644 --- a/src/lib/ai/runtimeCore.js +++ b/src/lib/ai/runtimeCore.js @@ -39,6 +39,7 @@ const r1 = (n) => Math.round(n * 10) / 10; * behaviours?: {name: string, issues: {line?: number, message: string}[]}[], * isSource: (type: string) => boolean, * isAction: (type: string) => boolean, + * isObjectEffect?: (type: string) => boolean, * inputsOf: (type: string) => {name: string, type: string}[], * edgeTypeProblem?: (source: any, target: any, sourceHandle?: string, targetHandle?: string) => ({refuse?: string, warn?: string} | null), * edgeHandleProblems?: (source: any, target: any, sourceHandle?: string, targetHandle?: string) => string[] @@ -175,6 +176,25 @@ export function validateScene(w) { if (n.type === 'onclick' && w.play?.interaction === 'off') add({ code: 'clicks-off', level: 'warning', node: n.id, message: name + ': clicking is switched off in Play (scene physics ▸ play interaction "off"), so a player cannot click it.', fix: 'Set the play interaction to click or grab.' }); } + // 102b-papercuts (scout row 18): a per-frame EFFECT in the scene graph that reaches no object (no Object + // Selector picking one, no target of its own) does nothing — the node editor card says so too + if ( + w.isObjectEffect?.(n.type) && + !(n.type === 'script' && Array.isArray(n.data?.outputs) && n.data.outputs.length) && + (!n.__graph || n.__graph === 'scene') && + !(typeof n.data?.target === 'string' && n.data.target && n.data.target !== '-None-') && + !ix.out(n.id).some((/** @type {any} */ e) => { + const t = ix.byId.get(e.target); + return t?.type === 'objectselector' && t.data?.selected && t.data.selected !== '-None-'; + }) + ) + add({ + code: 'effect-no-object', + level: 'warning', + node: n.id, + message: name + ' is in the scene graph and is not wired into an Object Selector that picks an object, so it acts on nothing.', + fix: 'Wire it into an Object Selector naming the object, or move it into that object\'s own graph.' + }); // an ACTION with no source upstream never runs (kit actions, setgamestate, travel, spawn …) if (w.isAction(n.type) && !w.isSource(n.type)) { const triggerWired = ix.into(n.id).length > 0; diff --git a/src/lib/ai/runtimeTools.js b/src/lib/ai/runtimeTools.js index bb0328088..be5a4574f 100644 --- a/src/lib/ai/runtimeTools.js +++ b/src/lib/ai/runtimeTools.js @@ -52,10 +52,10 @@ const triggerNow = () => (sessionNow() % 86400000) / 1000; const objectOf = (uuid) => /** @type {any} */ (get(objectsGroup))?.getObjectByProperty?.('uuid', uuid); /** the socket tables, loaded once (flowSockets is a leaf; the kit's action set needs the kit catalog) */ -/** @type {Promise<{isSource: (t: string) => boolean, isAction: (t: string) => boolean, inputsOf: (t: string) => {name: string, type: string}[]}> | null} */ +/** @type {Promise<{isSource: (t: string) => boolean, isAction: (t: string) => boolean, isObjectEffect: (t: string) => boolean, inputsOf: (t: string) => {name: string, type: string}[]}> | null} */ let typeInfoP = null; function typeInfo() { - typeInfoP ??= Promise.all([import('$lib/flowSockets'), import('$lib/kit/nodes.js').catch(() => null), import('./edgeHandles.js')]).then(([s, kit, eh]) => { + typeInfoP ??= Promise.all([import('$lib/flowSockets'), import('$lib/kit/nodes.js').catch(() => null), import('./edgeHandles.js'), import('$lib/nodeCatalog'), import('$lib/moduleSDK')]).then(([s, kit, eh, cat, sdk]) => { /** @param {string} t */ const inputsOf = (t) => s.inputHandles(t).map((name) => ({ name, type: s.inputType(t, name) })); /** @param {string} t */ @@ -64,7 +64,10 @@ function typeInfo() { /** @param {string} t */ const isAction = (t) => TRIGGERED_ACTIONS.has(t) || kitActions.has(t); // 2.0-n3b step 2: the same edge checks create_flow_nodes runs, over the graph as it stands - return { isSource, isAction, inputsOf, edgeTypeProblem: eh.edgeTypeProblem, edgeHandleProblems: eh.edgeHandleProblems }; + // 102b-papercuts: the per-frame effects that act only through an Object Selector (or their own graph's object) + /** @param {string} t */ + const isObjectEffect = (t) => cat.animationTypes.includes(t) || !!sdk.moduleEffects[t] || t === 'script' || t === 'customnode'; + return { isSource, isAction, inputsOf, isObjectEffect, edgeTypeProblem: eh.edgeTypeProblem, edgeHandleProblems: eh.edgeHandleProblems }; }); return typeInfoP; } @@ -478,7 +481,7 @@ export const RUNTIME_AI_TOOLS = [ function: { name: 'validate_scene', description: - 'Check the scene for what would not work when someone presses Play: actions nothing fires, an Object Selector wired the wrong way round, a scene-graph On Click that reaches no object, Game Start wired as an event, HUD nodes pointing at elements that do not exist, walk mode with no spawn, a scene with no light, behaviour code problems. Each issue says what to fix. Run it after building a game, then playtest.', + 'Check the scene for what would not work when someone presses Play: actions nothing fires, effects that act on no object, an Object Selector wired the wrong way round, a scene-graph On Click that reaches no object, Game Start wired as an event, HUD nodes pointing at elements that do not exist, walk mode with no spawn, a scene with no light, behaviour code problems. Each issue says what to fix. Run it after building a game, then playtest.', parameters: { type: 'object', properties: {} } } }, diff --git a/src/lib/ai/sceneSetupCore.js b/src/lib/ai/sceneSetupCore.js index 4f5392f51..97fcb1f34 100644 --- a/src/lib/ai/sceneSetupCore.js +++ b/src/lib/ai/sceneSetupCore.js @@ -157,22 +157,47 @@ export function planSky(args, now) { if (!c) return { error: 'fog.color: a colour' }; fog.color = c; } + // 102b-papercuts: a density is REAL exponential fog now (kind distance), or the ground layer (kind height); + // near / far are still written, as the linear fog an older build falls back to + if (f.kind !== undefined && !['linear', 'distance', 'height'].includes(f.kind)) return { error: 'fog.kind: linear (near / far), distance (density) or height (density, base, thickness)' }; if (f.density !== undefined) { if (!finite(f.density) || f.density <= 0) return { error: 'fog.density: a number above 0 (0.01 thin, 0.1 thick)' }; + fog.density = r3(clamp(f.density, 0.0005, 1)) || 0.001; fog.near = 0; fog.far = r3(clamp(DENSITY_FAR / f.density, 1, 20000)); fog.fit = false; - warnings.push('density ' + f.density + ' became linear fog from 0 to ' + fog.far + ' m (the scene renders linear fog)'); + if (f.kind === undefined && fog.kind !== 'height') fog.kind = 'distance'; + } + if (f.kind !== undefined) { + fog.kind = f.kind; + if (f.kind !== 'linear' && !(fog.density > 0)) fog.density = 0.02; + } + for (const k of ['base', 'thickness']) { + if (f[k] === undefined) continue; + if (!finite(f[k]) || (k === 'thickness' && f[k] <= 0)) return { error: 'fog.' + k + (k === 'base' ? ' is the ground height in metres' : ' is metres above 0') }; + fog[k] = r3(clamp(f[k], k === 'base' ? -10000 : 0.1, 10000)); + if (f.kind === undefined) fog.kind = 'height'; + } + if (fog.kind === 'height') { + if (!finite(fog.base)) fog.base = 0; + if (!(fog.thickness > 0)) fog.thickness = 6; } for (const k of ['near', 'far']) { if (f[k] === undefined) continue; + if (f.kind === undefined && f.density === undefined) fog.kind = 'linear'; if (!finite(f[k]) || f[k] < 0) return { error: 'fog.' + k + ' is metres (0 or more)' }; fog[k] = r3(clamp(f[k], 0, 20000)); fog.fit = false; } if (fog.far <= fog.near) return { error: 'fog: far must be beyond near' }; payload.fog = fog; - words.push('fog ' + fog.color + ' ' + fog.near + '-' + fog.far + ' m'); + words.push( + fog.kind === 'height' + ? 'ground fog ' + fog.color + ' density ' + fog.density + ', ' + fog.thickness + ' m thick from ' + fog.base + ' m' + : fog.kind === 'distance' + ? 'fog ' + fog.color + ' density ' + fog.density + : 'fog ' + fog.color + ' ' + fog.near + '-' + fog.far + ' m' + ); } custom = true; } diff --git a/src/lib/ai/sceneSetupTools.js b/src/lib/ai/sceneSetupTools.js index 78836e6c2..fda2d04cb 100644 --- a/src/lib/ai/sceneSetupTools.js +++ b/src/lib/ai/sceneSetupTools.js @@ -138,7 +138,13 @@ function describeEnvironment() { minQuality: hdri.minQuality } : null, - fog: p.fog ? { color: p.fog.color, near: p.fog.near, far: p.fog.far } : null, + fog: p.fog + ? p.fog.kind === 'height' + ? { kind: 'height', color: p.fog.color, density: p.fog.density, base: p.fog.base, thickness: p.fog.thickness } + : p.fog.kind === 'distance' + ? { kind: 'distance', color: p.fog.color, density: p.fog.density } + : { color: p.fog.color, near: p.fog.near, far: p.fog.far } + : null, sun: p.sun ? { color: p.sun.color, intensity: p.sun.intensity, position: p.sun.position } : null, ambient: p.hemi ? { sky: p.hemi.sky, ground: p.hemi.ground, intensity: p.hemi.intensity } : null, envLights: (s.lights ?? []).map((/** @type {any} */ l) => ({ id: l.id, kind: l.kind, color: l.color, intensity: l.intensity, position: l.position, ...(l.groundColor ? { groundColor: l.groundColor } : {}) })) @@ -315,7 +321,7 @@ export const SCENE_SETUP_AI_TOOLS = [ PRESET_NAMES + '), a sky image by name (' + SKY_NAMES + - '; a search_assets HDRI id "cc0:polyhaven/" (fetched into the library once) or "lib:"; or {hash} of an HDR in the library — upload your own .hdr / .exr with assets upload; never a URL) with rotation, intensity, blur and Sky quality (auto / standard / high / max, plus a minimum), a background colour or {top, bottom} gradient, fog ({color, near, far} in metres, or {color, density}; null = none) and exposure. "Make it night": preset night or hdri starlight + a dark fog. Changes the whole scene\'s look, so the host is asked first: it answers needsConfirm, then repeat with confirmId. One undo step.', + '; a search_assets HDRI id "cc0:polyhaven/" (fetched into the library once) or "lib:"; or {hash} of an HDR in the library — upload your own .hdr / .exr with assets upload; never a URL) with rotation, intensity, blur and Sky quality (auto / standard / high / max, plus a minimum), a background colour or {top, bottom} gradient, fog ({color, near, far} in metres; {color, density} for distance fog; {color, density, base, thickness} for ground fog lying from base up over thickness metres; null = none) and exposure. "Make it night": preset night or hdri starlight + a dark fog. Changes the whole scene\'s look, so the host is asked first: it answers needsConfirm, then repeat with confirmId. One undo step.', parameters: { type: 'object', properties: { @@ -338,7 +344,15 @@ export const SCENE_SETUP_AI_TOOLS = [ gradient: { type: ['object', 'null'], properties: { top: { type: 'string' }, bottom: { type: 'string' } } }, fog: { type: ['object', 'null'], - properties: { color: { type: 'string' }, near: { type: 'number', minimum: 0 }, far: { type: 'number', minimum: 0 }, density: { type: 'number', exclusiveMinimum: 0 } } + properties: { + kind: { type: 'string', enum: ['linear', 'distance', 'height'], description: 'linear = near to far; distance = thickens with distance (density); height = ground fog (density, base, thickness)' }, + color: { type: 'string' }, + near: { type: 'number', minimum: 0 }, + far: { type: 'number', minimum: 0 }, + density: { type: 'number', exclusiveMinimum: 0 }, + base: { type: 'number', description: 'ground fog: the height (m) where it is thickest' }, + thickness: { type: 'number', exclusiveMinimum: 0, description: 'ground fog: how far up (m) it thins out' } + } }, exposure: { type: 'number', minimum: 0.1, maximum: 4 }, ...CONFIRM diff --git a/src/lib/ai/tools.js b/src/lib/ai/tools.js index 52093134d..2d67ec345 100644 --- a/src/lib/ai/tools.js +++ b/src/lib/ai/tools.js @@ -1457,7 +1457,7 @@ export const AI_TOOLS = [ type: 'array', items: { type: 'number' }, description: - 'up to 4 constructor args. Box=[w,h,d], Sphere=[radius], Cylinder=[rTop,rBottom,height], Cone=[radius,height], Plane=[w,h], Torus=[radius,tube]. Omit for sensible defaults. Sizes go down to 0.001 m.' + 'up to 4 constructor args. Box=[w,h,d], Sphere=[radius], Cylinder=[rTop,rBottom,height], Cone=[radius,height], Plane=[w,h], Torus=[radius,tube]. Omit for sensible defaults. For a rounded box, create a Box and then set_geometry {radius}. Sizes go down to 0.001 m.' }, name: { type: 'string' }, position: { type: 'array', items: { type: 'number' }, description: '[x,y,z]' }, diff --git a/src/lib/ai/uploadAsset.js b/src/lib/ai/uploadAsset.js index a22f2385b..beea98172 100644 --- a/src/lib/ai/uploadAsset.js +++ b/src/lib/ai/uploadAsset.js @@ -52,7 +52,19 @@ export async function uploadAssetTool(args) { } } } - const name = uploadName(args.name, type.kind === 'sound' ? 'sound' : 'sky', type.ext); + let name = uploadName(args.name, type.kind === 'sound' ? 'sound' : 'sky', type.ext); + // 102b-papercuts: a sky in a cube layout (a cross or a strip of six faces) becomes an equirectangular .hdr + /** @type {string | null} */ let converted = null; + if (type.kind === 'hdri') { + const { prepareSky } = await import('../hdri/cubeImport.js'); + const ready = await prepareSky([{ name, buffer: /** @type {ArrayBuffer} */ (bytes.slice().buffer) }]); + // anything else (a 2:1 sky, a file it cannot read) is filed exactly as before + if (!('error' in ready) && ready.converted) { + bytes = ready.bytes; + name = ready.name; + converted = ready.converted; + } + } const { fileIntoLibrary } = await import('./importFile.js'); try { const filed = await fileIntoLibrary(bytes, name, args.folder ?? (type.kind === 'sound' ? 'Sounds' : 'Skies')); @@ -63,6 +75,7 @@ export async function uploadAssetTool(args) { bytes: bytes.length, folder: filed.folder, ...(filed.created ? {} : { note: 'the library already had this file' }), + ...(converted ? { converted: 'cube map (' + converted + ') -> equirectangular .hdr' } : {}), use: uploadUse(type.kind) }; } catch (e) { @@ -75,7 +88,7 @@ export const UPLOAD_ASSET_TOOL = { function: { name: 'upload_asset', description: - 'Put a SOUND or an HDR SKY you hold as bytes into the library and get its hash: your own generated music or ambience, or a file you fetched yourself (any site — the bytes come from you, so nothing needs the site to allow cross-origin reads). data = base64; a big file goes as chunk: {id, index, total} parts (each a multiple of 4 base64 characters). Sounds: WAV, OGG, MP3, FLAC, M4A or WebM up to 16 MB (decoded once to prove it is audio) → place it with effects sound {asset: hash} or music {asset: hash}. Skies: Radiance .hdr or OpenEXR up to 25 MB → set_look sky {hdri: {hash}}. The type comes from the bytes, never the name. Images go through image instead. Everyone in the session can pull it by hash.', + 'Put a SOUND or an HDR SKY you hold as bytes into the library and get its hash: your own generated music or ambience, or a file you fetched yourself (any site — the bytes come from you, so nothing needs the site to allow cross-origin reads). data = base64; a big file goes as chunk: {id, index, total} parts (each a multiple of 4 base64 characters). Sounds: WAV, OGG, MP3, FLAC, M4A or WebM up to 16 MB (decoded once to prove it is audio) → place it with effects sound {asset: hash} or music {asset: hash}. Skies: Radiance .hdr or OpenEXR up to 25 MB, equirectangular (2:1) or a cube map as a cross (4:3, 3:4) or a strip of six faces (6:1, 1:6), which is converted → set_look sky {hdri: {hash}}. The type comes from the bytes, never the name. Images go through image instead. Everyone in the session can pull it by hash.', parameters: { type: 'object', properties: { diff --git a/src/lib/cinematic/cinematicCore.js b/src/lib/cinematic/cinematicCore.js index c200d5215..7889e2666 100644 --- a/src/lib/cinematic/cinematicCore.js +++ b/src/lib/cinematic/cinematicCore.js @@ -117,7 +117,8 @@ export const STAND_IN_LABELS = Object.freeze({ shader: 'Shader graph and code materials', terrain: 'Terrain paint layers', deform: 'Jiggle and sway', - custom: 'Custom materials' + custom: 'Custom materials', + heightfog: 'Height fog' // 102b-papercuts: the WebGPU view draws distance fog, not the ground layer }); /** @typedef {keyof typeof STAND_IN_LABELS} StandInKind */ diff --git a/src/lib/cinematic/cinematicView.js b/src/lib/cinematic/cinematicView.js index 878e9b2c3..674125d2e 100644 --- a/src/lib/cinematic/cinematicView.js +++ b/src/lib/cinematic/cinematicView.js @@ -25,6 +25,10 @@ import { compileAsync } from '../render/compile.js'; import { captureEnvironment, useCopies, restoreEnvironment } from './envHandover.js'; import { buildPipeline } from './pipeline.js'; import { classifyMaterial, notShownText, effectiveQuality, createDropWatch, quantile } from './cinematicCore.js'; +import { HEIGHT_FOG } from '../fogModes.js'; + +/** 102b-papercuts: what the chip names — the stand-ins, plus a height fog (WebGL-only shader chunk) @param {any} swap */ +const shownKinds = (swap) => (HEIGHT_FOG[2] > 0.5 ? [...swap.kinds, 'heightfog'] : swap.kinds); // the STORE leaf, never ./cinematicState.js (that one reaches the app's stores: this chunk's preload list) import { cinematicState, cinematicPrefs, disableCinematic, cinematicDisabled } from './cinematicStore.js'; import { deformersOf } from '../vertexDeform.js'; @@ -359,7 +363,7 @@ async function enterSteps() { live.labels.tick(); live.swap = createSwapper(W, scene); live.swap.scan(); - patchState({ notShown: notShownText(live.swap.kinds) }); + patchState({ notShown: notShownText(shownKinds(live.swap)) }); try { live.built = await build(targetQuality()); await compileAsync(r, scene, cam); @@ -468,10 +472,10 @@ function draw(_delta) { /** @param {any} g */ function rescan(g) { - const before = notShownText(g.swap.kinds); + const before = notShownText(shownKinds(g.swap)); g.swap.scan(); g.labels?.scan(g.scene); - const after = notShownText(g.swap.kinds); + const after = notShownText(shownKinds(g.swap)); if (after !== before) patchState({ notShown: after }); } diff --git a/src/lib/environment.js b/src/lib/environment.js index 6c2050831..c8a080c7d 100644 --- a/src/lib/environment.js +++ b/src/lib/environment.js @@ -8,6 +8,19 @@ import { globalScene, globalRenderer, objectsGroup, backgroundColor, TControls, import { peers } from '../stores/appStore'; import { sceneRadius } from './sceneBounds'; import { fogFarFor, mergeFogPatch } from './fogReach'; +// 102b-papercuts: distance + height fog (the height layer's chunk patch, fogShader.js, loads with the first height fog) +import { fogKindOf, fogDensityOf, heightFogParams, setHeightFog } from './fogModes.js'; + +/** 102b: the height-fog chunk patch is a lazy chunk; the first height fog installs it and recompiles what is live + * @param {any} scene */ +function ensureHeightFogShader(scene) { + void import('./fogShader.js').then((m) => { + if (!m.installHeightFog()) return; + scene?.traverse?.((/** @type {any} */ o) => { + for (const mat of Array.isArray(o.material) ? o.material : o.material ? [o.material] : []) mat.needsUpdate = true; + }); + }); +} import { registerSystemGroup } from './moduleSDK'; import { createLight } from './geometries.svelte'; import { cappedShadowSize, shadowsDisabled } from './lightParams'; @@ -458,6 +471,7 @@ export function applyEnvironment() { // compositor blends the camera feed under); 'room' leaves nothing (three clears to clear) scene.background = get(roomLook) === 'dim' ? dimBackdrop() : null; scene.fog = null; + setHeightFog([0, 0, 0, 0]); } else { // 30 author-kit: a gradient sky when the payload carries one, else the flat colour const gradient = skyGradientOf(preset); @@ -469,9 +483,17 @@ export function applyEnvironment() { } // fog never swallows a big scene: a PRESET's reach grows with the scene bounds; a fog the // user set by hand (`fit: false`) keeps its own (36 A5, fogReach.js) - scene.fog = preset.fog - ? new THREE.Fog(preset.fog.color, preset.fog.near, fogFarFor(preset.fog, sceneRadius())) - : null; + // 102b-papercuts: `kind` distance / height = exponential fog (height adds the ground layer, fogShader.js); + // absent = the linear fog every older document and build has + const kind = preset.fog ? fogKindOf(preset.fog) : 'linear'; + scene.fog = !preset.fog + ? null + : kind === 'linear' + ? new THREE.Fog(preset.fog.color, preset.fog.near, fogFarFor(preset.fog, sceneRadius())) + : new THREE.FogExp2(preset.fog.color, fogDensityOf(preset.fog)); + const layer = heightFogParams(preset.fog); + setHeightFog(layer); + if (layer[2] > 0.5) ensureHeightFogShader(scene); } backgroundColor.set(preset.background); @@ -630,7 +652,7 @@ function detachedPayload() { * 37-hdri: `hdri` is the third sky field — a partial patch merged into the payload's hdri * (hdriCore.mergeHdriPatch), `null` removes it. It rides the same commit, so an HDRI pick, * rotation or exposure drag persists and replicates like a background colour does. - * @param {{background?: string, fog?: {color?: string, near?: number, far?: number} | null, hdri?: any}} patch + * @param {{background?: string, fog?: {color?: string, near?: number, far?: number, kind?: string, density?: number, base?: number, thickness?: number} | null, hdri?: any}} patch * @param {{replicate?: boolean}} [opts] */ export function editEnvSky(patch, opts = {}) { diff --git a/src/lib/flowRuntime.js b/src/lib/flowRuntime.js index 9fc60386d..f16748210 100644 --- a/src/lib/flowRuntime.js +++ b/src/lib/flowRuntime.js @@ -1,3 +1,4 @@ +import { writeUniformSlot } from './uniformWrite.js'; import * as THREE from 'three'; // `{v}` labels for the HUD — 'Gems: {v}' needs no string node and no string socket type import { hudFormat } from './hudFormat.js'; @@ -2527,6 +2528,8 @@ function stampOfSource(edge, ctx, seen) { // 36 (U10): a NAMED event output keeps its own log entry, `#` (a behaviour's // `this.emit('holeSunk')`) — several events on one node, each its own stamp if (edge.sourceHandle && source?.type === 'behaviour') return ctx?.triggers?.[edge.source + '#' + edge.sourceHandle]?.lastT ?? null; + // 102b-papercuts: a Script node's EVENT output is its own entry too (updateScriptEvents) + if (edge.sourceHandle && source?.type === 'script') return ctx?.triggers?.[edge.source + '#' + edge.sourceHandle]?.lastT ?? null; // 78 F3: a Toggle Animation's opened / closed are its own entries too (derived on every peer) if (source?.type === 'toggleanim') return edge.sourceHandle === 'opened' || edge.sourceHandle === 'closed' ? (ctx?.triggers?.[edge.source + '#' + edge.sourceHandle]?.lastT ?? null) : null; return ctx?.triggers?.[edge.source]?.lastT ?? null; @@ -2680,6 +2683,44 @@ function updateDerivedPulses(ctx, fired = scheduledFired) { fired.delete(key); } +/** 102b-papercuts: the last truthiness of each Script event output, keyed `#`. LOCAL: + * every peer runs the same pure code against the same replicated inputs and clock. @type {Map} */ +const scriptEventLevels = new Map(); + +/** + * 102b-papercuts (scout row 18, "a script cannot fire an event"): a Script node with an output of + * type `event` fires it when the value its code returns for that output turns true. The pulse is + * stamped as `#` (the behaviour emit shape, so stampOfSource and the PUSH walk in + * nextTriggerLog already know it) with `replicate: false` — the Delay / Once rule: the code is a + * pure function of replicated inputs and the synced clock, so each peer sees the same edge and a + * broadcast would count it once per peer. The FIRST reading only seeds (a joiner, or a load, must + * not fire an event that is merely already true). Costs nothing while no script declares one. + * @param {number} time @param {any} ctx + */ +function updateScriptEvents(time, ctx) { + let any = false; + for (const node of nodes) { + if (node.type !== 'script' || !Array.isArray(node.data?.outputs)) continue; + const events = scriptOutputs(node.data).filter((o) => o.type === 'event'); + if (!events.length) continue; + any = true; + const value = /** @type {any} */ (evalNode(node, nodes, edges, time, new Set(), ctx)); + const handles = value && typeof value === 'object' ? value.__handles : null; + for (const o of events) { + const key = node.id + '#' + o.name; + const level = !!handles?.[o.name]; + const prev = scriptEventLevels.get(key); + scriptEventLevels.set(key, level); + if (prev === false && level) applyNodeTrigger(key, time, false); + } + } + if (!any && !scriptEventLevels.size) return; + for (const key of [...scriptEventLevels.keys()]) { + const id = key.slice(0, key.indexOf('#')); + if (!nodes.some((/** @type {any} */ n) => n.id === id)) scriptEventLevels.delete(key); + } +} + /** The shared timestamp of whatever event is wired into this node's `trigger` * (the newest, with several sources fanned in). `alias` = a second handle name that counts as * the trigger (HUD Timer's `start`). @param {string} nodeId @param {any} ctx @param {string} [alias] */ @@ -2748,6 +2789,21 @@ function implicitOwnerOf(node) { return wired ? null : graph; } +/** + * 102b-papercuts (scout row 18): an EFFECT node with nothing to act on — a scene-graph node (an object graph + * targets its owner) with no target of its own and no wire into an Object Selector that picks an object. Read + * only for the card line (4 Hz, the one graph the node editor shows), so it may walk the edges. + * @param {any} node @returns {boolean} + */ +export function effectActsOnNothing(node) { + if (!node || node.data?.muted) return false; + const effect = animationTypes.includes(node.type) || !!moduleEffects[node.type] || (node.type === 'script' && !isScriptValue(node.data)) || node.type === 'customnode'; + if (!effect) return false; + if (node.__graph && node.__graph !== SCENE_GRAPH) return false; // the owner is its object + if (directTargetOf(node)) return false; + return !edges.some((/** @type {any} */ e) => e.source === node.id && targetUuidOf(e)); +} + function applyColors() { if (!sceneObjects) return; edges.forEach((edge) => { @@ -4672,11 +4728,8 @@ function applyAnimation(object, base, anim, time, ctx) { // uniform also needs no recompile — that is what the live uniform record is for. const name = typeof data.uniform === 'string' ? data.uniform.trim() : ''; if (name && shaderRef?.shaderUniform) { - const slot = shaderRef.shaderUniform(object.uuid, name); - // NUMBERS only in v1: a flow number socket is what drives this, and a vecN would - // need an array that socket cannot carry - const value = Number(data.value); - if (slot && Number.isFinite(value)) slot.value = value; + // 102b-papercuts: a vecN slot now takes a vector or a colour too (uniformWrite.js) + writeUniformSlot(shaderRef.shaderUniform(object.uuid, name), data); } } } @@ -5588,6 +5641,8 @@ function runTick(now) { // the bump on the NEXT tick - one frame, the same latency every derived reaction // in this file has. updateDerivedPulses(ctx, paused ? viewScheduled : scheduledFired); + // 102b-papercuts: a Script node's event outputs (never while the debugger holds a pause) + if (!paused) updateScriptEvents(time, ctx); // ONE prune for the shared stale-stamp cutoffs, after every family has had its turn // to register this tick's nodes (physics runs first, then game, then character) pruneActionSeenAt(); @@ -5668,7 +5723,8 @@ function runTick(now) { value: (node) => evalNode(node, nodes, edges, time, new Set(), ctx), pulseAt: (id) => ctx.triggers?.[id]?.lastT ?? null, // 78 F26: the trigger nodes' "why not" line - why: (node) => (WHY_TYPES.has(node.type) ? whyCardLine(lastCheck(node.id), standingWhy(node, time, ctx)) : null) + why: (node) => (WHY_TYPES.has(node.type) ? whyCardLine(lastCheck(node.id), standingWhy(node, time, ctx)) : null), + inert: effectActsOnNothing // 102b-papercuts }); if (paused) toLive(); phaseEnd(PHASE_FLOW); diff --git a/src/lib/fogEdit.js b/src/lib/fogEdit.js new file mode 100644 index 000000000..5d8fd6e8e --- /dev/null +++ b/src/lib/fogEdit.js @@ -0,0 +1,44 @@ +// 102b-papercuts: the EDITING half of fogModes.js (the boot keeps only what drawing a fog needs). Loaded by the +// lazy Fog kind rows and the unit tests. +import { fogKindOf, fogDensityOf, heightFogOf, FOG_DENSITY_DEFAULT, FOG_THICKNESS_DEFAULT } from './fogModes.js'; + +/** @typedef {import('./fogModes.js').FogKind} FogKind */ + +/** + * A patch that switches the kind, with the defaults the new kind needs (a density, a layer). Merged by + * mergeFogPatch like any fog edit; near / far are left as they were (they are an older build's fog). + * @param {any} fog @param {FogKind} kind + */ +export function fogKindPatch(fog, kind) { + if (kind === 'linear') return { kind: 'linear' }; + /** @type {Record} */ + const patch = { kind }; + if (!(Number(fog?.density) > 0)) patch.density = FOG_DENSITY_DEFAULT; + if (kind === 'height') { + if (!Number.isFinite(Number(fog?.base))) patch.base = 0; + if (!(Number(fog?.thickness) > 0)) patch.thickness = FOG_THICKNESS_DEFAULT; + } + return patch; +} + +/** + * The fraction of fog colour one point gets — the same maths the shader runs, for tests and probes. + * @param {any} fog @param {number} dist metres from the camera @param {number} camY @param {number} pointY + */ +export function fogFactorAt(fog, dist, camY, pointY) { + const kind = fogKindOf(fog); + if (kind === 'linear') { + const near = Number(fog?.near) || 0; + const far = Number(fog?.far) || 0; + const t = Math.min(1, Math.max(0, (dist - near) / Math.max(1e-6, far - near))); + return t * t * (3 - 2 * t); // smoothstep, as three + } + const d = fogDensityOf(fog); + if (kind === 'distance') return 1 - Math.exp(-d * d * dist * dist); + const h = /** @type {{base: number, thickness: number}} */ (heightFogOf(fog)); + const k = 1 / h.thickness; + const h0 = Math.max(camY - h.base, 0); + const h1 = Math.max(pointY - h.base, 0); + const f = Math.abs(h1 - h0) > 1e-3 ? (Math.exp(-k * h0) - Math.exp(-k * h1)) / (k * (h1 - h0)) : Math.exp(-k * h0); + return 1 - Math.exp(-d * dist * f); +} diff --git a/src/lib/fogModes.js b/src/lib/fogModes.js new file mode 100644 index 000000000..50b1d5751 --- /dev/null +++ b/src/lib/fogModes.js @@ -0,0 +1,55 @@ +// 102b-papercuts (scout row 18): REAL DISTANCE AND HEIGHT FOG. A LEAF (imports nothing), so the rules are +// testable with no scene. The fog stays inside the environment preset payload, ADDITIVELY: +// +// {color, near, far, fit?} linear fog, exactly as before (no `kind`) +// {color, near, far, kind: 'distance', density} exponential fog that thickens with distance +// {color, near, far, kind: 'height', density, base, thickness} +// fog that lies on the ground: densest at `base` +// (m), thinning upward over `thickness` (m), and +// integrated along each view ray, so looking down +// into a valley fogs more than looking across it +// +// near / far stay in the document, so an older build (which only knows linear fog) still draws a fog. +// The editing half (switching kind, the shader maths for tests) is fogEdit.js, which only the lazy Fog rows load. + +/** @typedef {'linear' | 'distance' | 'height'} FogKind */ +export const FOG_KINDS = /** @type {const} */ (['linear', 'distance', 'height']); +export const FOG_DENSITY_DEFAULT = 0.02; +export const FOG_THICKNESS_DEFAULT = 6; + +/** @param {any} fog @returns {FogKind} */ +export function fogKindOf(fog) { + return fog?.kind === 'distance' || fog?.kind === 'height' ? fog.kind : 'linear'; +} + +/** a density in (0, 1], else the default @param {any} fog */ +export function fogDensityOf(fog) { + const d = Number(fog?.density); + return Number.isFinite(d) && d > 0 ? Math.min(d, 1) : FOG_DENSITY_DEFAULT; +} + +/** the height layer, or null for a fog that has none @param {any} fog @returns {{base: number, thickness: number} | null} */ +export function heightFogOf(fog) { + if (fogKindOf(fog) !== 'height') return null; + const base = Number(fog.base); + const thickness = Number(fog.thickness); + return { + base: Number.isFinite(base) ? Math.max(-10000, Math.min(10000, base)) : 0, + thickness: Number.isFinite(thickness) && thickness > 0.05 ? Math.min(thickness, 10000) : FOG_THICKNESS_DEFAULT + }; +} + +/** the shared shader uniform's four numbers: [base, 1 / thickness, on, 0] @param {any} fog */ +export function heightFogParams(fog) { + const h = heightFogOf(fog); + return h ? [h.base, 1 / h.thickness, 1, 0] : [0, 0, 0, 0]; +} + +/** 102b: the shared height-fog uniform value [base, 1/thickness, on, 0] (fogShader.js puts it in every fog + * material; a leaf here so the Cinematic view and environment.js can read and write it without the patch) */ +export const HEIGHT_FOG = new Float32Array(4); + +/** write the layer (from heightFogParams) @param {number[]} params */ +export function setHeightFog(params) { + for (let i = 0; i < 4; i++) HEIGHT_FOG[i] = Number(params[i]) || 0; +} diff --git a/src/lib/fogShader.js b/src/lib/fogShader.js new file mode 100644 index 000000000..adf5af42f --- /dev/null +++ b/src/lib/fogShader.js @@ -0,0 +1,55 @@ +// 102b-papercuts (scout row 18): HEIGHT FOG in three's own fog chunks. Installed ONCE at import, before any +// program compiles (environment.js imports it), so every material — built-in, ShaderMaterial (water, +// lines) — gets the same code. One shared uniform carries the layer: `fogHeight` = [base, 1/thickness, +// on, 0], a Float32Array that UniformsUtils.clone passes by REFERENCE (it copies three objects and real +// arrays only), so writing it reaches every material with no recompile. While it is [0,0,0,0] the +// shader takes the stock branch, so a scene without height fog renders exactly as before. +// +// Height fog rides FOG_EXP2 (scene.fog is a FogExp2 carrying the density); linear fog is untouched. +// World height comes from the view-space position the stock chunk already has (viewMatrix is rigid, so +// its inverse is a transpose: world y = column 1 of the rotation . (view position - translation)) — no new +// attribute, no worldpos chunk dependency. + +// A LAZY chunk since the boot pass: environment.js imports it the first time a scene asks for height fog, +// and then asks every live material to recompile once (installHeightFog returns true on that first call). +import * as THREE from 'three'; +import { HEIGHT_FOG } from './fogModes.js'; + +export { HEIGHT_FOG }; + +let installed = false; + +/** @returns {boolean} true when this call installed the patch (materials compiled before it need a recompile) */ +export function installHeightFog() { + if (installed) return false; + installed = true; + const C = /** @type {any} */ (THREE.ShaderChunk); + C.fog_pars_vertex = C.fog_pars_vertex.replace('varying float vFogDepth;', 'varying float vFogDepth;\n\tvarying float vFogWorldY;'); + C.fog_vertex = C.fog_vertex.replace( + 'vFogDepth = - mvPosition.z;', + 'vFogDepth = - mvPosition.z;\n\tvFogWorldY = dot( viewMatrix[ 1 ].xyz, mvPosition.xyz - viewMatrix[ 3 ].xyz );' + ); + C.fog_pars_fragment = C.fog_pars_fragment.replace( + 'varying float vFogDepth;', + 'varying float vFogDepth;\n\tvarying float vFogWorldY;\n\tuniform vec4 fogHeight;' + ); + C.fog_fragment = C.fog_fragment.replace( + 'float fogFactor = 1.0 - exp( - fogDensity * fogDensity * vFogDepth * vFogDepth );', + [ + 'float fogFactor = 1.0 - exp( - fogDensity * fogDensity * vFogDepth * vFogDepth );', + '\t\tif ( fogHeight.z > 0.5 ) {', + '\t\t\tfloat fogK = fogHeight.y;', + '\t\t\tfloat fogH0 = max( cameraPosition.y - fogHeight.x, 0.0 );', + '\t\t\tfloat fogH1 = max( vFogWorldY - fogHeight.x, 0.0 );', + '\t\t\tfloat fogDh = fogH1 - fogH0;', + '\t\t\tfloat fogF = abs( fogDh ) > 1e-3 ? ( exp( - fogK * fogH0 ) - exp( - fogK * fogH1 ) ) / ( fogK * fogDh ) : exp( - fogK * fogH0 );', + '\t\t\tfogFactor = 1.0 - exp( - fogDensity * vFogDepth * fogF );', + '\t\t}' + ].join('\n') + ); + // every built-in material that takes fog gets the shared slot (cloned by reference when it compiles) + for (const lib of Object.values(/** @type {any} */ (THREE.ShaderLib))) + if (lib?.uniforms?.fogColor && !lib.uniforms.fogHeight) lib.uniforms.fogHeight = { value: HEIGHT_FOG }; + /** @type {any} */ (THREE.UniformsLib.fog).fogHeight = { value: HEIGHT_FOG }; + return true; +} diff --git a/src/lib/geometryEdit.js b/src/lib/geometryEdit.js index 90280c7cc..5b4bc5381 100644 --- a/src/lib/geometryEdit.js +++ b/src/lib/geometryEdit.js @@ -4,6 +4,7 @@ import { objectsGroup, pokeScene } from '../stores/sceneStore'; import { peers } from '../stores/appStore'; import { recordEntry, registerHistoryKind } from './history'; import { GEOMETRY_PARAMS, geometrySpec, boundGeometryParams } from './geometryParams'; +import { loadRoundedBox } from './roundedBoxGate.js'; // Live geometry parameter editing (phase 78). Params live in // object.userData.geometryParams = { gtype, params } — userData survives the @@ -63,7 +64,7 @@ function terrainParamsOf(object) { /** 37 R3: a spec with a `clean` hook stores only what its builder reads (clamped, known keys) * @param {string} gtype @param {any} params */ -function cleanFor(gtype, params) { +export function cleanFor(gtype, params) { const clean = geometrySpec(gtype)?.clean; return clean ? clean(params) : boundGeometryParams(gtype, params); } @@ -84,8 +85,11 @@ export function geometryParamsOf(object) { if (!spec || !object.geometry.parameters) return null; /** @type {any} */ const params = {}; - for (const p of spec.params) + for (const p of spec.params) { + // 102b: an optional row the geometry does not carry stays out (a square box keeps its six params) + if (p.optional && object.geometry.parameters[p.key] === undefined) continue; params[p.key] = object.geometry.parameters[p.key] ?? p.def; + } return { gtype, params }; } @@ -99,7 +103,7 @@ export function stampGeometryParams(object) { export function defaultGeometryParams(gtype) { /** @type {any} */ const params = {}; - for (const p of geometrySpec(gtype)?.params ?? []) params[p.key] = p.def; + for (const p of geometrySpec(gtype)?.params ?? []) if (!p.optional) params[p.key] = p.def; return params; } @@ -150,6 +154,18 @@ export function applyRemoteGeometry(data) { const group = get(objectsGroup); const object = group?.getObjectByProperty('uuid', data.uuid); if (!object?.geometry) return; + // 102b: an undone Convert goes back to the BAKED mesh (`baked` is additive: an older peer builds the box) + if (data.baked) { + // (through the gate's small chunk: importing roundedBoxConvert here would put its preload list on boot) + void loadRoundedBox().then((m) => { + const baked = m.bakedRoundedBox(cleanFor('Box', { ...data.params })); + object.geometry.dispose(); + object.geometry = baked; + delete object.userData.geometryParams; + pokeScene(); + }); + return; + } // 85 G2/G4: a profile / outline rides `params` — the builder gets the cleaned set, never the raw wire const fresh = buildGeometry(data.gtype, cleanFor(data.gtype, { ...data.params })); if (!fresh) return; diff --git a/src/lib/geometryParams.js b/src/lib/geometryParams.js index baeea616b..c9cf03fb9 100644 --- a/src/lib/geometryParams.js +++ b/src/lib/geometryParams.js @@ -16,12 +16,16 @@ import { terrainGeometry, TERRAIN_FALLOFFS, latheGeometry, cleanLathe, extrudeGe // `build`-hook shape as Terrain, plus `fromArgs` (createGeometry maps `/create` numbers to // params with it) and the collider hint stamped at creation import { ARCH_SPECS } from './arch/archGeometry.js'; +// 102b-papercuts: a Box may be ROUNDED (corner radius + segments), still a Box in the file +import { boxGeometryFor, MAX_RADIUS_SEGMENTS, DEFAULT_RADIUS_SEGMENTS } from './roundedBoxGate.js'; const TAU = Math.PI * 2; /** `show` (37 R3): the Inspector hides the row while it returns false for the current params * (a door width with no doors, a spiral's column on a straight stair). - * @typedef {{key: string, label: string, kind: 'slider'|'int'|'angle'|'bool'|'choice', min?: number, max?: number, step?: number, options?: string[], def: number|boolean|string, show?: (params: any) => boolean}} ParamSpec */ + * `optional` (102b): the row is stored only once someone sets it, so an object that never uses it keeps + * exactly the params it always had (a Box's corner radius). `title` = the row's tooltip. + * @typedef {{key: string, label: string, kind: 'slider'|'int'|'angle'|'bool'|'choice', min?: number, max?: number, step?: number, options?: string[], def: number|boolean|string, show?: (params: any) => boolean, optional?: boolean, title?: string}} ParamSpec */ /** @type {Record any, fromArgs?: (args: any[]) => Record, clean?: (params: any) => Record, colliderHint?: string}>} */ export const GEOMETRY_PARAMS = { @@ -31,10 +35,15 @@ export const GEOMETRY_PARAMS = { { key: 'width', label: 'Width', kind: 'slider', min: 0.1, max: 20, step: 0.1, def: 1 }, { key: 'height', label: 'Height', kind: 'slider', min: 0.1, max: 20, step: 0.1, def: 1 }, { key: 'depth', label: 'Depth', kind: 'slider', min: 0.1, max: 20, step: 0.1, def: 1 }, - { key: 'widthSegments', label: 'Width segs', kind: 'int', min: 1, max: 10, def: 1 }, - { key: 'heightSegments', label: 'Height segs', kind: 'int', min: 1, max: 10, def: 1 }, - { key: 'depthSegments', label: 'Depth segs', kind: 'int', min: 1, max: 10, def: 1 } - ] + // a rounded box builds its own grid, so the segment rows only apply to a square one + { key: 'widthSegments', label: 'Width segs', kind: 'int', min: 1, max: 10, def: 1, show: (p) => !(Number(p?.radius) > 0) }, + { key: 'heightSegments', label: 'Height segs', kind: 'int', min: 1, max: 10, def: 1, show: (p) => !(Number(p?.radius) > 0) }, + { key: 'depthSegments', label: 'Depth segs', kind: 'int', min: 1, max: 10, def: 1, show: (p) => !(Number(p?.radius) > 0) }, + // 102b-papercuts: corner rounding, saved as two numbers (not 270 KB of vertices) + { key: 'radius', label: 'Corner radius', kind: 'slider', min: 0, max: 2, step: 0.01, def: 0, optional: true, title: 'Rounds every edge and corner of the box (0 = square corners). Stops at half the shortest side.' }, + { key: 'radiusSegments', label: 'Corner segs', kind: 'int', min: 1, max: MAX_RADIUS_SEGMENTS, def: DEFAULT_RADIUS_SEGMENTS, optional: true, show: (p) => Number(p?.radius) > 0, title: 'How smooth the rounding is: more segments, rounder corners, more triangles.' } + ], + build: boxGeometryFor }, Sphere: { order: ['radius', 'widthSegments', 'heightSegments', 'phiStart', 'phiLength', 'thetaStart', 'thetaLength'], diff --git a/src/lib/hdri/cubeImport.js b/src/lib/hdri/cubeImport.js new file mode 100644 index 000000000..b924dc61a --- /dev/null +++ b/src/lib/hdri/cubeImport.js @@ -0,0 +1,129 @@ +// 102b-papercuts (scout row 18): turn what someone uploads as a sky into the ONE thing the sky pipeline +// takes — an equirectangular .hdr / .exr. A lazy chunk (the Sky image section and the agents' upload +// import it on demand); the geometry is cubeToEquirect.js. +// +// one .hdr / .exr, twice as wide as tall -> the original bytes, untouched (the old path) +// one image in a cube layout (cross / strip) -> converted to an equirectangular .hdr +// six face files (px nx py ny pz nz …) -> converted to an equirectangular .hdr +// one .png / .jpg / .webp, twice as wide -> converted to .hdr (sRGB -> linear), so it lights too +// +// The converted file is an ordinary Radiance .hdr: it rides the Explorer, the hash pull, .tpscene saves +// and exports with no new field, so a peer on an older build sees the same sky. + +import { cubeLayoutOf, isEquirectShape, splitCubeImage, cubeToEquirect, encodeRGBE, facesFromFiles, fitEquirect } from './cubeToEquirect.js'; + +/** the most pixels one decoded input may hold (an 8K cross is 50 M; floats are 16 bytes a pixel) */ +export const MAX_INPUT_PIXELS = 64 * 1024 * 1024; + +/** @param {string} name */ +const isFloatName = (name) => /\.(hdr|exr)$/i.test(name); +/** @param {string} name */ +const isImageName = (name) => /\.(png|jpe?g|webp)$/i.test(name); +/** the accept list for a file input */ +export const SKY_ACCEPT = '.hdr,.exr,.png,.jpg,.jpeg,.webp'; + +/** @param {number} c */ +const srgbToLinear = (c) => (c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4)); + +/** + * Decode one file to linear RGBA floats, top row first. + * @param {ArrayBuffer} buffer @param {string} name + * @returns {Promise} + */ +export async function decodeSkyImage(buffer, name) { + if (/\.exr$/i.test(name)) { + const THREE = await import('three'); + const { EXRLoader } = await import('three/addons/loaders/EXRLoader.js'); + const t = /** @type {any} */ (new EXRLoader().setDataType(THREE.FloatType).parse(buffer)); + if (t.format !== THREE.RGBAFormat) throw new Error('only RGB or RGBA .exr files are supported'); + guard(t.width, t.height); + // EXRLoader hands the rows bottom first (flipY false): turn them top first + return flipRows({ data: t.data, width: t.width, height: t.height }); + } + if (/\.hdr$/i.test(name)) { + const THREE = await import('three'); + const { HDRLoader } = await import('three/addons/loaders/HDRLoader.js'); + const t = /** @type {any} */ (new HDRLoader().setDataType(THREE.FloatType).parse(buffer)); + guard(t.width, t.height); + return { data: t.data, width: t.width, height: t.height }; + } + const bitmap = await createImageBitmap(new Blob([buffer])); + try { + guard(bitmap.width, bitmap.height); + const canvas = new OffscreenCanvas(bitmap.width, bitmap.height); + const ctx = /** @type {OffscreenCanvasRenderingContext2D} */ (canvas.getContext('2d')); + ctx.drawImage(bitmap, 0, 0); + const px = ctx.getImageData(0, 0, bitmap.width, bitmap.height).data; + const data = new Float32Array(px.length); + for (let i = 0; i < px.length; i += 4) { + data[i] = srgbToLinear(px[i] / 255); + data[i + 1] = srgbToLinear(px[i + 1] / 255); + data[i + 2] = srgbToLinear(px[i + 2] / 255); + data[i + 3] = px[i + 3] / 255; + } + return { data, width: bitmap.width, height: bitmap.height }; + } finally { + bitmap.close?.(); + } +} + +/** @param {number} w @param {number} h */ +function guard(w, h) { + if (w * h > MAX_INPUT_PIXELS) throw new Error('the image is too large to convert (' + w + ' x ' + h + ')'); +} + +/** @param {import('./cubeToEquirect.js').RGBAImage} img */ +function flipRows(img) { + const row = img.width * 4; + const out = new Float32Array(img.data.length); + for (let y = 0; y < img.height; y++) out.set(img.data.subarray(y * row, (y + 1) * row), (img.height - 1 - y) * row); + return { data: out, width: img.width, height: img.height }; +} + +/** a library name: the file's own (or the faces' shared prefix) with .hdr @param {string[]} names @param {string} suffix */ +function outName(names, suffix) { + const bases = names.map((n) => n.replace(/^.*[\\/]/, '').replace(/\.[a-z0-9]+$/i, '')); + let base = bases[0] ?? 'sky'; + if (bases.length > 1) { + let p = bases[0]; + for (const b of bases) while (p && !b.startsWith(p)) p = p.slice(0, -1); + base = p.replace(/[-_ .]+$/, '') || 'sky'; + } + return base + suffix + '.hdr'; +} + +/** + * What to put into the library for these files. + * @param {{name: string, buffer: ArrayBuffer}[]} files + * @returns {Promise<{bytes: Uint8Array, name: string, converted: null | 'cross' | 'strip' | 'faces' | 'image'} | {error: string}>} + */ +export async function prepareSky(files) { + if (!files.length) return { error: 'No file.' }; + const bad = files.find((f) => !isFloatName(f.name) && !isImageName(f.name)); + if (bad) return { error: 'Pick a .hdr, .exr, .png, .jpg or .webp file (' + bad.name + ' is none of these).' }; + try { + if (files.length > 1) { + const decoded = []; + for (const f of files) decoded.push({ name: f.name, image: await decodeSkyImage(f.buffer, f.name) }); + const got = facesFromFiles(decoded); + if ('error' in got) return got; + return { bytes: encodeRGBE(cubeToEquirect(got.faces)), name: outName(files.map((f) => f.name), '-cube'), converted: 'faces' }; + } + const f = files[0]; + const img = await decodeSkyImage(f.buffer, f.name); + if (isEquirectShape(img.width, img.height)) { + if (isFloatName(f.name)) return { bytes: new Uint8Array(f.buffer), name: f.name, converted: null }; + return { bytes: encodeRGBE(fitEquirect(img)), name: outName([f.name], ''), converted: 'image' }; + } + const layout = cubeLayoutOf(img.width, img.height); + const faces = layout ? splitCubeImage(img) : null; + if (!layout || !faces) + return { + error: + 'This image is ' + img.width + ' x ' + img.height + ': a sky is twice as wide as it is tall (equirectangular), a cube-map cross (4:3 or 3:4), a strip of six faces (6:1 or 1:6), or six separate face files.' + }; + return { bytes: encodeRGBE(cubeToEquirect(faces)), name: outName([f.name], '-equirect'), converted: layout.endsWith('cross') ? 'cross' : 'strip' }; + } catch (err) { + return { error: 'Could not read that sky: ' + String(/** @type {any} */ (err)?.message ?? err).slice(0, 120) }; + } +} diff --git a/src/lib/hdri/cubeToEquirect.js b/src/lib/hdri/cubeToEquirect.js new file mode 100644 index 000000000..44a6217bf --- /dev/null +++ b/src/lib/hdri/cubeToEquirect.js @@ -0,0 +1,283 @@ +// 102b-papercuts (scout row 18): CUBE-MAP SKIES. camera.md said "cube-map files are not supported". +// The whole sky pipeline (PMREM from an equirect texture, Rotation, the sun placed from the image, +// the hash a peer pulls, the .tpscene and export bundling) takes ONE equirectangular image, so a cube +// map is CONVERTED on upload into an equirectangular Radiance .hdr and stored like any uploaded sky. +// Nothing new is saved or sent: an older build opens the result as an ordinary HDRI. +// +// PURE (no DOM, no three): layouts, the face lookup, the resample and the .hdr writer, so the +// geometry is unit-tested. Decoding the files is cubeImport.js's job. +// +// CONVENTIONS. Faces are named the way cube-map files are: px nx py ny pz nz, with the OpenGL face +// axes (images top row first). Unfolded as a cross, the centre face is +Z and the face to its RIGHT is +// +X — which, standing inside and turning right, is world -X in three's right-handed frame; three +// flips x for every cube texture it draws (`flipEnvMap`), and the lookup below does the same, so a +// cross shows exactly as it would as a three CubeTexture and is never mirrored. + +/** @typedef {'px' | 'nx' | 'py' | 'ny' | 'pz' | 'nz'} Face */ +/** @typedef {{ data: Float32Array, width: number, height: number }} RGBAImage linear RGBA floats, top row first */ + +export const FACES = /** @type {const} */ (['px', 'nx', 'py', 'ny', 'pz', 'nz']); + +/** the largest equirect we write: 2048 x 1024 RGBE is 8 MB, well under the 25 MB a peer can receive */ +export const MAX_EQUIRECT_WIDTH = 2048; + +/** + * Which cube layout an image's shape is, or null. A cross is 4:3 (or 3:4), a strip 6:1 (or 1:6). + * @param {number} w @param {number} h @returns {'hcross' | 'vcross' | 'hstrip' | 'vstrip' | null} + */ +export function cubeLayoutOf(w, h) { + if (!(w > 0 && h > 0)) return null; + const near = (/** @type {number} */ a, /** @type {number} */ b) => Math.abs(a - b) <= 0.01 * b; + if (near(w * 3, h * 4) && w % 4 === 0) return 'hcross'; + if (near(w * 4, h * 3) && h % 4 === 0) return 'vcross'; + if (near(w, h * 6) && w % 6 === 0) return 'hstrip'; + if (near(h, w * 6) && h % 6 === 0) return 'vstrip'; + return null; +} + +/** an equirectangular image is twice as wide as it is tall @param {number} w @param {number} h */ +export function isEquirectShape(w, h) { + return w > 0 && h > 0 && Math.abs(w - 2 * h) <= 0.01 * w; +} + +/** + * Where each face sits in a layout, in cells of the face size, and whether it is stored turned 180 + * degrees (the -Z face at the bottom of a vertical cross is). + * +Y +Y + * -X +Z +X -Z -X +Z +X strips: +X -X +Y -Y +Z -Z + * -Y -Y + * -Z (turned) + * @param {'hcross' | 'vcross' | 'hstrip' | 'vstrip'} layout + * @returns {Record} + */ +export function faceCells(layout) { + if (layout === 'hcross') + return { py: { cx: 1, cy: 0, turned: false }, nx: { cx: 0, cy: 1, turned: false }, pz: { cx: 1, cy: 1, turned: false }, px: { cx: 2, cy: 1, turned: false }, nz: { cx: 3, cy: 1, turned: false }, ny: { cx: 1, cy: 2, turned: false } }; + if (layout === 'vcross') + return { py: { cx: 1, cy: 0, turned: false }, nx: { cx: 0, cy: 1, turned: false }, pz: { cx: 1, cy: 1, turned: false }, px: { cx: 2, cy: 1, turned: false }, ny: { cx: 1, cy: 2, turned: false }, nz: { cx: 1, cy: 3, turned: true } }; + /** @type {any} */ const out = {}; + FACES.forEach((f, i) => (out[f] = layout === 'hstrip' ? { cx: i, cy: 0, turned: false } : { cx: 0, cy: i, turned: false })); + return out; +} + +/** + * Cut a single cube-layout image into its six square faces. + * @param {RGBAImage} img @returns {Record | null} + */ +export function splitCubeImage(img) { + const layout = cubeLayoutOf(img.width, img.height); + if (!layout) return null; + const size = layout === 'hcross' ? img.width / 4 : layout === 'vcross' ? img.width / 3 : layout === 'hstrip' ? img.height : img.width; + const cells = faceCells(layout); + /** @type {any} */ const faces = {}; + for (const f of FACES) { + const { cx, cy, turned } = cells[f]; + const data = new Float32Array(size * size * 4); + for (let y = 0; y < size; y++) + for (let x = 0; x < size; x++) { + const sx = cx * size + (turned ? size - 1 - x : x); + const sy = cy * size + (turned ? size - 1 - y : y); + const si = (sy * img.width + sx) * 4; + const di = (y * size + x) * 4; + data[di] = img.data[si]; + data[di + 1] = img.data[si + 1]; + data[di + 2] = img.data[si + 2]; + data[di + 3] = img.data[si + 3]; + } + faces[f] = { data, width: size, height: size }; + } + return faces; +} + +/** + * Which face a cube-map lookup direction hits, and where (s, t in 0..1, t from the TOP of the face + * image) — the OpenGL major-axis rule. + * @param {number} x @param {number} y @param {number} z @returns {{face: Face, s: number, t: number}} + */ +export function cubeLookup(x, y, z) { + const ax = Math.abs(x); + const ay = Math.abs(y); + const az = Math.abs(z); + /** @type {Face} */ let face; + let sc; + let tc; + let ma; + if (ax >= ay && ax >= az) { + ma = ax; + face = x > 0 ? 'px' : 'nx'; + sc = x > 0 ? -z : z; + tc = -y; + } else if (ay >= az) { + ma = ay; + face = y > 0 ? 'py' : 'ny'; + sc = x; + tc = y > 0 ? z : -z; + } else { + ma = az; + face = z > 0 ? 'pz' : 'nz'; + sc = z > 0 ? x : -x; + tc = -y; + } + return { face, s: (sc / ma + 1) / 2, t: (tc / ma + 1) / 2 }; +} + +/** bilinear sample of a face at (s, t), clamped to the face @param {RGBAImage} f @param {number} s @param {number} t @param {Float32Array} out @param {number} o */ +function sampleFace(f, s, t, out, o) { + const fx = Math.min(f.width - 1, Math.max(0, s * f.width - 0.5)); + const fy = Math.min(f.height - 1, Math.max(0, t * f.height - 0.5)); + const x0 = Math.floor(fx); + const y0 = Math.floor(fy); + const x1 = Math.min(f.width - 1, x0 + 1); + const y1 = Math.min(f.height - 1, y0 + 1); + const wx = fx - x0; + const wy = fy - y0; + const d = f.data; + const i00 = (y0 * f.width + x0) * 4; + const i10 = (y0 * f.width + x1) * 4; + const i01 = (y1 * f.width + x0) * 4; + const i11 = (y1 * f.width + x1) * 4; + for (let c = 0; c < 4; c++) + out[o + c] = (d[i00 + c] * (1 - wx) + d[i10 + c] * wx) * (1 - wy) + (d[i01 + c] * (1 - wx) + d[i11 + c] * wx) * wy; +} + +/** + * The equirect width for a face size: four faces round the equator, capped, even. + * @param {number} faceSize + */ +export function equirectWidthFor(faceSize) { + return Math.max(64, Math.min(MAX_EQUIRECT_WIDTH, Math.round((faceSize * 4) / 2) * 2)); +} + +/** + * Resample six faces into an equirectangular image (three's mapping: u = atan2(z, x) / 2π + 0.5, + * v = asin(y) / π + 0.5, top row = straight up). + * @param {Record} faces @param {number} [width] + * @returns {RGBAImage} + */ +export function cubeToEquirect(faces, width) { + const size = faces.px.width; + const W = width ?? equirectWidthFor(size); + const H = W / 2; + const data = new Float32Array(W * H * 4); + for (let r = 0; r < H; r++) { + const lat = (0.5 - (r + 0.5) / H) * Math.PI; + const cl = Math.cos(lat); + const y = Math.sin(lat); + for (let c = 0; c < W; c++) { + const phi = ((c + 0.5) / W - 0.5) * 2 * Math.PI; + const x = Math.cos(phi) * cl; + const z = Math.sin(phi) * cl; + // three's flipEnvMap: a cube texture is looked up with x negated + const hit = cubeLookup(-x, y, z); + sampleFace(faces[hit.face], hit.s, hit.t, data, (r * W + c) * 4); + } + } + return { data, width: W, height: H }; +} + +/** + * Downsample an equirect (box filter) so the written file stays within MAX_EQUIRECT_WIDTH. + * @param {RGBAImage} img @returns {RGBAImage} + */ +export function fitEquirect(img) { + if (img.width <= MAX_EQUIRECT_WIDTH) return img; + const W = MAX_EQUIRECT_WIDTH; + const H = W / 2; + const data = new Float32Array(W * H * 4); + const kx = img.width / W; + const ky = img.height / H; + for (let r = 0; r < H; r++) + for (let c = 0; c < W; c++) { + const x0 = Math.floor(c * kx); + const x1 = Math.max(x0 + 1, Math.floor((c + 1) * kx)); + const y0 = Math.floor(r * ky); + const y1 = Math.max(y0 + 1, Math.floor((r + 1) * ky)); + const o = (r * W + c) * 4; + let n = 0; + for (let y = y0; y < y1; y++) + for (let x = x0; x < x1; x++) { + const i = (y * img.width + x) * 4; + for (let k = 0; k < 4; k++) data[o + k] += img.data[i + k]; + n++; + } + for (let k = 0; k < 4; k++) data[o + k] /= n; + } + return { data, width: W, height: H }; +} + +/** + * Write linear RGB floats as a Radiance .hdr (flat RGBE scanlines, which three's HDRLoader reads). + * @param {RGBAImage} img @returns {Uint8Array} + */ +export function encodeRGBE(img) { + const header = `#?RADIANCE\n# made by theprototype.app from a cube map\nFORMAT=32-bit_rle_rgbe\n\n-Y ${img.height} +X ${img.width}\n`; + const head = new TextEncoder().encode(header); + const out = new Uint8Array(head.length + img.width * img.height * 4); + out.set(head, 0); + let o = head.length; + const d = img.data; + for (let i = 0; i < img.width * img.height; i++) { + const r = Math.max(0, d[i * 4] || 0); + const g = Math.max(0, d[i * 4 + 1] || 0); + const b = Math.max(0, d[i * 4 + 2] || 0); + const m = Math.max(r, g, b); + if (m < 1e-32) { + o += 4; // already zero + continue; + } + const e = Math.ceil(Math.log2(m) + 1e-9); + // three's HDRLoader decodes byte * 2^(e - 128) / 255 (not the classic / 256), so encode to match + const scale = 255 / Math.pow(2, e); + out[o] = Math.min(255, Math.round(r * scale)); + out[o + 1] = Math.min(255, Math.round(g * scale)); + out[o + 2] = Math.min(255, Math.round(b * scale)); + out[o + 3] = e + 128; + o += 4; + } + return out; +} + +/** + * Which face a separate file is, from its name: px / posx / pos-x / right … Order matters: the + * three-letter axis forms win over the words. Null when the name says nothing. + * @param {string} name @returns {Face | null} + */ +export function faceOfFileName(name) { + const base = String(name ?? '') + .toLowerCase() + .replace(/^.*[\\/]/, '') + .replace(/\.[a-z0-9]+$/, ''); + // the face word ENDS the name, after a separator (or is the whole name): sky_px, negz, cloudy-back + const m = /(?:^|[^a-z0-9])(pos-?[xyz]|neg-?[xyz]|[pn][xyz]|[+-][xyz]|right|left|top|up|bottom|down|front|back|rt|lf|ft|bk|dn)$/.exec(base) ?? /^(pos[xyz]|neg[xyz])$/.exec(base); + if (!m) return null; + const t = m[1].replace('-', ''); + /** @type {Record} */ + const map = { + px: 'px', nx: 'nx', py: 'py', ny: 'ny', pz: 'pz', nz: 'nz', + posx: 'px', negx: 'nx', posy: 'py', negy: 'ny', posz: 'pz', negz: 'nz', + '+x': 'px', x: 'nx', '+y': 'py', y: 'ny', '+z': 'pz', z: 'nz', + right: 'px', left: 'nx', top: 'py', up: 'py', bottom: 'ny', down: 'ny', front: 'pz', back: 'nz', + rt: 'px', lf: 'nx', ft: 'pz', bk: 'nz', dn: 'ny' + }; + return map[t] ?? null; +} + +/** + * Six named files -> the faces in order, or the reason they cannot be one cube. + * @param {{name: string, image: RGBAImage}[]} files + * @returns {{faces: Record} | {error: string}} + */ +export function facesFromFiles(files) { + if (files.length !== 6) return { error: 'A cube map in separate files needs all six faces (px, nx, py, ny, pz, nz).' }; + /** @type {any} */ const faces = {}; + for (const f of files) { + const face = faceOfFileName(f.name); + if (!face) return { error: 'Could not tell which face "' + f.name + '" is: name the files px, nx, py, ny, pz, nz (or right, left, top, bottom, front, back).' }; + if (faces[face]) return { error: 'Two files are the ' + face + ' face.' }; + faces[face] = f.image; + } + const size = faces.px.width; + for (const face of FACES) + if (faces[face].width !== size || faces[face].height !== size) return { error: 'The six faces must be square and the same size.' }; + return { faces }; +} diff --git a/src/lib/loadGuard.js b/src/lib/loadGuard.js index d503e2816..7a36afe49 100644 --- a/src/lib/loadGuard.js +++ b/src/lib/loadGuard.js @@ -1,6 +1,8 @@ // The reporting half of loadGuardCore.js: every place that rebuilds an object or a material set // from JSON it did not just make itself goes through here, so the check, the count and the one // toast are the same everywhere. See loadGuardCore.js for the rules. +// 102b: importing it teaches ObjectLoader to read a Box's corner radius +import { healRoundedBoxes } from './roundedBoxGate.js'; import * as THREE from 'three'; import { guardChanged, isEmbeddedUrl, isLocalImageUrl, stlWithinLimits } from './loadGuardCore.js'; // 101 F2: the full object / material schema runs here, on top of loadGuardCore's rules @@ -159,7 +161,10 @@ export function refusal(what) { */ export function parseCheckedObject(json, from, what) { if (!checkObjectJson(json, from, what)) throw refusal('object'); - return new THREE.ObjectLoader(embeddedOnlyManager(from, what)).parse(json); + const object = new THREE.ObjectLoader(embeddedOnlyManager(from, what)).parse(json); + // 102b: a rounded box an older build wrote back square (its radius kept in userData) is rounded again + healRoundedBoxes(object); + return object; } /** diff --git a/src/lib/nodeDocs.js b/src/lib/nodeDocs.js index 35436070f..01941230c 100644 --- a/src/lib/nodeDocs.js +++ b/src/lib/nodeDocs.js @@ -92,7 +92,7 @@ export const NODE_DOCS = { lookat: "Keeps the connected object facing a target - another object or a fixed point - every frame.", setcolor: "Paints the connected object's material with a color, re-applied every frame.", visibility: "Shows or hides the connected object based on a boolean.", - setuniform: "Writes one number inside an object's shader graph, so a trigger, a", + setuniform: "Writes a number, a colour or a vector inside an object's shader graph, so a trigger, a timer or a counter can drive how the material looks.", onclick: "Fires a short pulse when its object is clicked - the bridge from user input into the graph.", keypress: "Fires a pulse while a keyboard key is pressed - the bridge from your keyboard into the graph.", onimpact: "Fires a pulse when the connected object hits something in a physics simulation - from any side (a wall, a target), or only when it lands with From: landing only.", diff --git a/src/lib/nodeLiveState.js b/src/lib/nodeLiveState.js index f6d69adee..5f1e4707e 100644 --- a/src/lib/nodeLiveState.js +++ b/src/lib/nodeLiveState.js @@ -132,6 +132,10 @@ registerLiveState('pathpatrol', (node, api) => { return to === null ? null : `to point ${to}`; }); +/** 102b-papercuts (scout row 18): the line an EFFECT node shows when nothing it could act on is wired — + * node-system.md used to call such a node "silently inert". The card renders it in the warning ink. */ +export const INERT_LINE = 'acts on nothing: add an Object Selector'; + let publishedAt = -Infinity; let lastJson = ''; @@ -139,7 +143,8 @@ let lastJson = ''; * Called by flowRuntime at the end of its tick. Throttled to ~4 Hz, only for the wanted graph's * nodes, and the store is written only when a line changed. * @param {{ nodes: any[], now: number, motionTime: number, motionRests: boolean, capturing: string | null, - * value: (node: any) => any, pulseAt: (id: string) => number | null, time: number, why?: (node: any) => string | null }} tick + * value: (node: any) => any, pulseAt: (id: string) => number | null, time: number, why?: (node: any) => string | null, + * inert?: (node: any) => boolean }} tick */ export function publishLiveStates(tick) { if (!wantedGraph) return; @@ -152,7 +157,9 @@ export function publishLiveStates(tick) { const reader = readers.get(node.type); /** @type {string | null | undefined} */ let text = null; - if (reader) { + // 102b-papercuts: an effect that reaches no object says so before anything else it could say + if (tick.inert?.(node)) text = INERT_LINE; + else if (reader) { try { text = reader(node, { value: () => tick.value(node), diff --git a/src/lib/nodes/families/logic.js b/src/lib/nodes/families/logic.js index 2d8ca8411..c3f562df8 100644 --- a/src/lib/nodes/families/logic.js +++ b/src/lib/nodes/families/logic.js @@ -460,8 +460,10 @@ export default { // editor shows beside a uniform-backed param; the value rides a number socket, so // no recompile and no message of its own (the flow value is already replicated). { + // 102b-papercuts: `color` and `vector` reach a vec3/vec2/vec4 uniform (uniformWrite.js); + // both optional, so an old node's data is unchanged type: 'setuniform', label: 'Set Shader Uniform', defaults: { uniform: '', value: 0 }, - sockets: { out: 'effect', in: { value: 'number' } }, + sockets: { out: 'effect', in: { value: 'number', color: 'color', vector: 'vector3' } }, card: 'EffectNode' } ] diff --git a/src/lib/projectFile.js b/src/lib/projectFile.js index 6e0069b4d..dc51993de 100644 --- a/src/lib/projectFile.js +++ b/src/lib/projectFile.js @@ -24,6 +24,7 @@ // A project with five scenes has no opinion about which one you want open, and // auto-loading one would replace the world someone is standing in. +import { inflateSharedGeometries } from './sharedGeometry.js'; import { get } from 'svelte/store'; import { showToast, explorerClose, armExplorerSceneSave } from '../stores/appStore'; import { bottomDockActive } from './bottomDock'; @@ -1158,7 +1159,7 @@ export async function importProjectAsSession(buffer, opts = {}) { try { const inner = unzipSync(sceneBytes); const json = inner['session.json']; - if (json) payload = JSON.parse(strFromU8(json)); + if (json) payload = inflateSharedGeometries(JSON.parse(strFromU8(json))); } catch { payload = null; } diff --git a/src/lib/roundedBox.js b/src/lib/roundedBox.js new file mode 100644 index 000000000..8e9db68e6 --- /dev/null +++ b/src/lib/roundedBox.js @@ -0,0 +1,133 @@ +// 102b-papercuts (scout row 15): A ROUNDED BOX IS A BOX SETTING. The official scenes carried 147 rounded boxes +// BAKED into plain meshes (23.6 MB of their 30 MB: "Core's Box params carry no bevel", author-templates.cjs), +// about 270 KB each. Now a Box has a corner radius, and the file keeps only its numbers. +// +// THE FILE TRICK: `RoundedBox` is three's RoundedBoxGeometry (MIT, examples/jsm — the generator's port is of +// the same code, so a baked box rebuilds to the same vertices) whose `type` says 'BoxGeometry' and whose +// `parameters` are a Box's plus `radius` / `radiusSegments`. BufferGeometry.toJSON writes a typed geometry as +// its parameters only, so a saved rounded box is a few numbers. On load, `BoxGeometry.fromJSON` (patched +// below, once, at import) builds the rounded one when the JSON names a radius; an OLDER build reads the same +// JSON as a plain box of the same size — the one thing it cannot show is the rounding. +// +// What is NOT parametric: GLTF (autosave, the join sync) writes vertices, as it always did; the object's +// `userData.geometryParams` carries the radius there, so its Geometry rows stay editable. +// +// A LAZY CHUNK: roundedBoxGate.js is the boot half (the Box builder, the ObjectLoader hook, the cheap tests) +// and loads this the first time a radius or a candidate mesh shows up. + +import * as THREE from 'three'; +import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js'; +import { MAX_RADIUS_SEGMENTS, DEFAULT_RADIUS_SEGMENTS } from './roundedBoxGate.js'; + +export { MAX_RADIUS_SEGMENTS, DEFAULT_RADIUS_SEGMENTS }; + +export class RoundedBox extends RoundedBoxGeometry { + /** @param {number} [width] @param {number} [height] @param {number} [depth] @param {number} [segments] @param {number} [radius] */ + constructor(width = 1, height = 1, depth = 1, segments = DEFAULT_RADIUS_SEGMENTS, radius = 0.1) { + const segs = Math.max(1, Math.min(MAX_RADIUS_SEGMENTS, Math.round(Number(segments) || DEFAULT_RADIUS_SEGMENTS))); + super(width, height, depth, segs, radius); + // a BOX, as far as every reader (toJSON, ObjectLoader, geometryParamsOf, an older build) is concerned + this.type = 'BoxGeometry'; + this.parameters = { + width, + height, + depth, + widthSegments: 1, + heightSegments: 1, + depthSegments: 1, + radius: /** @type {any} */ (this).parameters.radius, + radiusSegments: segs + }; + } +} + +/** + * A Box's geometry from its params: rounded when `radius` > 0, else exactly the BoxGeometry it always was. + * @param {any} p + */ +export function boxGeometryFor(p) { + const radius = Number(p?.radius); + if (Number.isFinite(radius) && radius > 0) return new RoundedBox(p.width, p.height, p.depth, p.radiusSegments ?? DEFAULT_RADIUS_SEGMENTS, radius); + return new THREE.BoxGeometry(p?.width, p?.height, p?.depth, p?.widthSegments, p?.heightSegments, p?.depthSegments); +} + +/** the rounded box for these params (the gate's builder, once this chunk is here) @param {any} p */ +export function roundedBoxGeometry(p) { + return new RoundedBox(p.width, p.height, p.depth, p.radiusSegments ?? DEFAULT_RADIUS_SEGMENTS, p.radius); +} + +/** + * A plain box an older build wrote back (it read our rounded box as a Box and dropped the radius) still carries + * the radius in `userData.geometryParams`: rebuild it. Only for an unedited Box whose size still matches. + * @param {any} root a parsed object tree @returns {number} how many were rebuilt + */ +export function restoreRoundedBoxes(root) { + let n = 0; + root?.traverse?.((/** @type {any} */ o) => { + const gp = o.userData?.geometryParams; + const g = o.geometry; + if (!o.isMesh || gp?.gtype !== 'Box' || !(Number(gp.params?.radius) > 0) || o.userData.faceEdited || o.userData.vertexEdited) return; + if (g?.type !== 'BoxGeometry' || g instanceof RoundedBox || !g.parameters) return; + const same = ['width', 'height', 'depth'].every((k) => Math.abs(Number(g.parameters[k]) - Number(gp.params[k])) < 1e-6); + if (!same) return; + const fresh = boxGeometryFor(gp.params); + g.dispose(); + o.geometry = fresh; + n++; + }); + return n; +} + +/** a rounded box as a plain BufferGeometry (what a baked one is) @param {any} p */ +export function bakedRoundedBox(p) { + const g = new RoundedBox(p.width, p.height, p.depth, p.radiusSegments ?? DEFAULT_RADIUS_SEGMENTS, p.radius); + const out = new THREE.BufferGeometry(); + for (const name of ['position', 'normal', 'uv']) out.setAttribute(name, g.attributes[name].clone()); + g.dispose(); + return out; +} + +/** the vertex count a rounded box of `segments` has (non-indexed: 6 faces x (2s+1)^2 quads x 6) @param {number} segments */ +export function roundedBoxVertexCount(segments) { + const t = segments * 2 + 1; + return 36 * t * t; +} + +/** + * Is this baked mesh exactly a rounded box? Returns its Box params (with radius) or null. Never changes + * anything: the Inspector OFFERS a Convert for what this recognises. + * @param {any} geometry a BufferGeometry @param {number} [eps] + * @returns {{width: number, height: number, depth: number, radius: number, radiusSegments: number} | null} + */ +export function matchRoundedBox(geometry, eps = 1e-4) { + if (!geometry?.isBufferGeometry || geometry instanceof RoundedBox || geometry.index) return null; + if (geometry.parameters) return null; // a typed geometry is not baked + const pos = geometry.attributes?.position; + if (!pos || pos.itemSize !== 3) return null; + let segments = 0; + for (let s = 1; s <= MAX_RADIUS_SEGMENTS; s++) if (roundedBoxVertexCount(s) === pos.count) segments = s; + if (!segments) return null; + geometry.computeBoundingBox(); + const b = geometry.boundingBox; + const width = b.max.x - b.min.x; + const height = b.max.y - b.min.y; + const depth = b.max.z - b.min.z; + // centred, as the builder makes it + if (Math.abs(b.max.x + b.min.x) > eps || Math.abs(b.max.y + b.min.y) > eps || Math.abs(b.max.z + b.min.z) > eps) return null; + // the radius: on the +X face's flat part, y reaches height / 2 - radius + const a = pos.array; + let maxY = -Infinity; + for (let i = 0; i < a.length; i += 3) if (Math.abs(a[i] - b.max.x) < eps) maxY = Math.max(maxY, Math.abs(a[i + 1])); + const radius = height / 2 - maxY; + if (!(radius > eps)) return null; + const test = new RoundedBox(width, height, depth, segments, radius); + const t = test.attributes.position.array; + let ok = t.length === a.length; + const tol = Math.max(eps, 1e-5 * Math.max(width, height, depth)); + for (let i = 0; ok && i < a.length; i++) if (Math.abs(t[i] - a[i]) > tol) ok = false; + test.dispose(); + return ok ? { width: round6(width), height: round6(height), depth: round6(depth), radius: round6(radius), radiusSegments: segments } : null; +} + +/** @param {number} v */ +const round6 = (v) => Math.round(v * 1e6) / 1e6; diff --git a/src/lib/roundedBoxConvert.js b/src/lib/roundedBoxConvert.js new file mode 100644 index 000000000..03ae2c1fa --- /dev/null +++ b/src/lib/roundedBoxConvert.js @@ -0,0 +1,64 @@ +// 102b-papercuts: CONVERT a baked rounded box into a Box with a corner radius, and its undo. A lazy chunk: the +// Inspector's button and an undo (or a peer's `baked` geometry message) load it; nothing on boot needs it. +import { get } from 'svelte/store'; +import { objectsGroup, pokeScene } from '../stores/sceneStore'; +import { peers } from '../stores/appStore'; +import { recordEntry, registerHistoryKind } from './history'; +import { buildGeometry, cleanFor, defaultGeometryParams } from './geometryEdit'; +import { roundedBoxOffer } from './roundedBoxGate.js'; +import { bakedRoundedBox } from './roundedBox.js'; + +/** + * Turn a baked rounded box into a Box with a corner radius: same vertices, now two numbers in the file. + * One undo step (undo puts the baked mesh back), replicated as the ordinary `geometry` message. + * @param {string} uuid @param {{replicate?: boolean, record?: boolean}} [options] @returns {boolean} + */ +export function convertToRoundedBox(uuid, options = {}) { + const { replicate = true, record = true } = options; + const object = get(objectsGroup)?.getObjectByProperty('uuid', uuid); + const params = roundedBoxOffer(object); + if (!params) return false; + const full = cleanFor('Box', { ...defaultGeometryParams('Box'), ...params }); + const fresh = buildGeometry('Box', full); + if (!fresh) return false; + const g = /** @type {any} */ (object).geometry; + // what the mesh carried that a rebuild would not (material groups are refused above; keep its userData) + fresh.userData = { ...(g.userData ?? {}) }; + g.dispose(); + /** @type {any} */ (object).geometry = fresh; + /** @type {any} */ (object).userData.geometryParams = { gtype: 'Box', params: full }; + pokeScene(); + if (record) recordEntry({ kind: 'roundconvert', uuid, params: full, before: { baked: true }, after: { baked: false } }); + if (replicate) /** @type {any} */ (get(peers))?.send({ type: 'geometry', uuid, gtype: 'Box', params: full }); + return true; +} + +/** back to the baked mesh (undo of a Convert, here or from a peer) @param {any} object @param {any} params */ +export function unconvertLocal(object, params) { + const baked = bakedRoundedBox(cleanFor('Box', { ...params })); + if (!baked) return; + object.geometry.dispose(); + object.geometry = baked; + delete object.userData.geometryParams; + pokeScene(); +} + +registerHistoryKind('roundconvert', (entry, state) => { + const object = get(objectsGroup)?.getObjectByProperty('uuid', entry.uuid); + if (!object) return false; + if (!state?.baked) { + const params = entry.params; + const fresh = buildGeometry('Box', params); + if (!fresh) return false; + /** @type {any} */ (object).geometry.dispose(); + /** @type {any} */ (object).geometry = fresh; + /** @type {any} */ (object).userData.geometryParams = { gtype: 'Box', params }; + pokeScene(); + /** @type {any} */ (get(peers))?.send({ type: 'geometry', uuid: entry.uuid, gtype: 'Box', params }); + return true; + } + unconvertLocal(object, entry.params); + /** @type {any} */ (get(peers))?.send({ type: 'geometry', uuid: entry.uuid, gtype: 'Box', params: entry.params, baked: true }); + return true; +}); + diff --git a/src/lib/roundedBoxGate.js b/src/lib/roundedBoxGate.js new file mode 100644 index 000000000..104be84ce --- /dev/null +++ b/src/lib/roundedBoxGate.js @@ -0,0 +1,106 @@ +// 102b-papercuts: the BOOT half of the rounded box (roundedBox.js is a lazy chunk). Everything that must be +// synchronous lives here and is small: the Box builder the Geometry rows call, the ObjectLoader hook that +// reads a radius back, and the cheap "could this be one?" tests. When a radius is asked for before the chunk +// has arrived, the box is built SQUARE for that moment and the chunk then rounds every such box in the scene +// (`restoreRoundedBoxes`, keyed on the object's userData.geometryParams, which every rounded box carries). + +import * as THREE from 'three'; +import { get } from 'svelte/store'; +import { objectsGroup, pokeScene } from '../stores/sceneStore'; + +/** the most corner segments a radius may ask for (each one is 4x the vertices of the last) */ +export const MAX_RADIUS_SEGMENTS = 6; +/** the default corner segments (three's RoundedBoxGeometry default) */ +export const DEFAULT_RADIUS_SEGMENTS = 2; + +/** @type {any} */ let mod = null; +/** @type {Promise | null} */ let loading = null; + +/** the loaded chunk, or null */ +export function roundedBoxModule() { + return mod; +} + +/** load the chunk once; when it lands, round every box that was built square while waiting */ +export function loadRoundedBox() { + loading ??= import('./roundedBox.js').then((m) => { + mod = m; + const group = get(objectsGroup); + if (group && m.restoreRoundedBoxes(group)) pokeScene(); + return m; + }); + return loading; +} + +/** @param {any} v */ +const hasRadius = (v) => Number(v) > 0 && Number.isFinite(Number(v)); + +/** + * A Box's geometry from its params: rounded when `radius` > 0, else exactly the BoxGeometry it always was. + * @param {any} p + */ +export function boxGeometryFor(p) { + if (hasRadius(p?.radius)) { + if (mod) return mod.roundedBoxGeometry(p); + void loadRoundedBox(); + } + return new THREE.BoxGeometry(p?.width, p?.height, p?.depth, p?.widthSegments, p?.heightSegments, p?.depthSegments); +} + +// teach ObjectLoader (every parse path: files, peers, undo, prefabs) to read a radius back +{ + const Box = /** @type {any} */ (THREE.BoxGeometry); + const stock = Box.fromJSON; + Box.fromJSON = function (/** @type {any} */ data) { + if (hasRadius(data?.radius)) { + if (mod) return mod.roundedBoxGeometry({ ...data, radiusSegments: data.radiusSegments ?? DEFAULT_RADIUS_SEGMENTS }); + void loadRoundedBox(); + } + return stock.call(this, data); + }; +} + +/** a parsed tree: round any box that should be (the chunk's job; loads it only when a box asks) @param {any} root */ +export function healRoundedBoxes(root) { + let wants = false; + root?.traverse?.((/** @type {any} */ o) => { + if (o.userData?.geometryParams?.gtype === 'Box' && hasRadius(o.userData.geometryParams.params?.radius)) wants = true; + }); + if (!wants) return; + if (mod) mod.restoreRoundedBoxes(root); + else void loadRoundedBox(); +} + +/** vertex counts a rounded box can have (non-indexed: 36 (2s+1)^2), for the cheap pre-test */ +const COUNTS = new Set(Array.from({ length: MAX_RADIUS_SEGMENTS }, (_, i) => 36 * (2 * i + 3) * (2 * i + 3))); + +/** + * Could this geometry be a baked rounded box? The exact test lives in the chunk: null while it loads (the + * scene is poked when it lands, so an Inspector reading this asks again). + * @param {any} geometry @returns {any} + */ +export function matchRoundedBoxLazy(geometry) { + const count = geometry?.attributes?.position?.count; + if (!geometry?.isBufferGeometry || geometry.index || geometry.parameters || !COUNTS.has(count)) return null; + if (mod) return mod.matchRoundedBox(geometry); + void loadRoundedBox().then(() => pokeScene()); + return undefined; +} + +/** + * 102b-papercuts: is this baked mesh a rounded box the Inspector can OFFER to convert? The params, or null. + * Cached per geometry (the check rebuilds one box to compare). Never converts anything. + * @type {WeakMap} */ +const matchCache = new WeakMap(); +/** @param {any} object */ +export function roundedBoxOffer(object) { + const g = object?.geometry; + if (!object?.isMesh || !g || object.userData?.geometryParams || object.userData?.terrain || Array.isArray(object.material)) return null; + if (g.type !== 'BufferGeometry') return null; + if (!matchCache.has(g)) { + const found = matchRoundedBoxLazy(g); + if (found === undefined) return null; // the exact test is still loading (the scene is poked when it lands) + matchCache.set(g, found); + } + return matchCache.get(g); +} diff --git a/src/lib/scriptIO.js b/src/lib/scriptIO.js index 1328104d9..cafb9a06b 100644 --- a/src/lib/scriptIO.js +++ b/src/lib/scriptIO.js @@ -29,9 +29,10 @@ export const SCRIPT_INPUT_TYPES = ['number', 'boolean', 'vector3', 'color', 'obj // nothing is wired and nothing is dialled — what a converted HUD Text's value and a converted // Math / Gate's extra sockets need to read exactly what the built-in read. An older build reads an // 'any' input as 'number' (normalizeSockets' fallback). -/** an output carries a value; an EVENT output would need a stamp, which a pure function - * of (inputs, time) cannot mint — so outputs are values only */ -export const SCRIPT_OUTPUT_TYPES = ['number', 'boolean', 'vector3', 'color', 'object', 'any']; +/** an output carries a value. 102b-papercuts: an EVENT output fires when the value the code + * returns for it turns TRUE (a rising edge, seen by every peer from the same pure code, so it + * fires locally on each with no message — the Delay rule). An older build reads it as 'number'. */ +export const SCRIPT_OUTPUT_TYPES = ['number', 'boolean', 'vector3', 'color', 'object', 'any', 'event']; // 36 (U10): 'any' passes the returned value through unchanged — TEXT for a HUD Text's format, // a list — there is no string socket type, and 'object' only carries an object reference @@ -162,7 +163,8 @@ export function harvestOutputs(result, outputs) { const bag = result && typeof result === 'object' && !Array.isArray(result) ? result : null; if (!bag) problems.push('return an object like { ' + outputs.map((o) => o.name + ': …').join(', ') + ' }'); for (const o of outputs) { - if (bag && !(o.name in bag)) problems.push('missing output `' + o.name + '`'); + // an event output is quiet most of the time: leaving it out of the return means "not now" + if (bag && !(o.name in bag) && o.type !== 'event') problems.push('missing output `' + o.name + '`'); handles[o.name] = coerceOutput(o.type, bag?.[o.name]); } return { value: { __handles: handles, __default: outputs.length ? handles[outputs[0].name] : undefined }, problems }; diff --git a/src/lib/sessions.js b/src/lib/sessions.js index e0ac2b479..2a7853de9 100644 --- a/src/lib/sessions.js +++ b/src/lib/sessions.js @@ -1,4 +1,5 @@ import { parseCheckedObject } from './loadGuard.js'; +import { inflateSharedGeometries } from './sharedGeometry.js'; import * as THREE from 'three'; import { renderOffscreen } from './render/offscreen.js'; import './threeFixes.js'; // 36-fb-water F14: transmissive materials survive a JSON trip (the export runtime loads scenes here too) @@ -666,13 +667,17 @@ export const SESSION_FORMAT = 1; // 2.0-n3c: 3 = custom node definitions are saved (`nodeDefs`), a graph node may carry `v` (its // definition's version; absent = 1) and a Script node `data.convertedFrom` (Convert to code). An older // reader ignores all three: the scene opens, its custom nodes without definitions (as before). -export const SESSION_FORMAT_MINOR = 3; +// 102b-papercuts: 4 = a Box geometry may carry `radius` / `radiusSegments` (a rounded box; an older reader +// draws the plain box of the same size) and a file may carry `sharedGeometries` (repeated meshes saved once, +// see sharedGeometry.js; an older reader draws each repeat as a plain box of its size). +export const SESSION_FORMAT_MINOR = 4; /** Parse + shape-validate a session JSON string. @param {string} json */ function parseSessionJson(json) { const payload = JSON.parse(json); if (!payload || !Array.isArray(payload.objects)) throw new Error('not a session file'); - return payload; + // 102b: a file's repeated meshes come back where they were used before anything reads the objects + return inflateSharedGeometries(payload); } /** V4: true when the payload's format is loadable; a NEWER format asks first. @@ -1014,7 +1019,9 @@ export async function exportSessionZip(payload, opts = { assets: true, packs: fa } wire = { ...payload, library: { folders: payload.library.folders ?? [], items: libIndex } }; } - files['session.json'] = strToU8(JSON.stringify(wire)); + // 102b: equal shapes are written once (sharedGeometry.js); the live payload is not changed + const { shareGeometries } = await import('./sharedGeometryWrite.js'); + files['session.json'] = strToU8(JSON.stringify(shareGeometries(wire))); Object.assign(files, presetFiles); /** @type {Array<{hash: string, name: string, kind: string, file: string}>} */ const index = []; diff --git a/src/lib/sharedGeometry.js b/src/lib/sharedGeometry.js new file mode 100644 index 000000000..15fccdfd6 --- /dev/null +++ b/src/lib/sharedGeometry.js @@ -0,0 +1,47 @@ +// 102b-papercuts (scout row 3): EQUAL SHAPES ARE SAVED ONCE. Every top-level object is toJSON'd on its own, +// so ten crates of one mesh wrote that mesh ten times (14.4 MB of the official scenes' 30 MB were +// byte-identical copies). A LEAF (no imports): the .tpscene writer calls `shareGeometries` (sharedGeometryWrite.js, +// a lazy chunk) on the way OUT +// and every reader calls `inflateSharedGeometries` first thing on the way IN, so nothing between (the +// loader, the budget check, the optimiser, the signature) ever sees the shared form. +// +// THE FILE: `sharedGeometries: [{type, data, userData?}]` beside `objects`, and a repeated geometry inside +// an object becomes a STUB that an OLDER build can still read: +// {uuid, type: 'BoxGeometry', width, height, depth, sharedGeometry: } +// An older build draws a plain box of the mesh's size there (three ignores the unknown number); this build +// puts the real mesh back. Only a geometry that appears TWICE or more is shared, so a scene without repeats +// is written byte-for-byte as before. + +/** a buffer geometry entry worth sharing @param {any} g */ +function isBuffer(g) { + return g && typeof g === 'object' && g.type === 'BufferGeometry' && g.data && typeof g.data === 'object' && g.data.attributes?.position; +} + +/** + * Put every shared geometry back where it was used (in place) and drop the table. Safe on any payload: + * one without a table is left alone, a stub naming a missing entry stays the older build's stand-in box. + * @param {any} payload @returns {any} the same payload + */ +export function inflateSharedGeometries(payload) { + const table = payload?.sharedGeometries; + if (!Array.isArray(table)) return payload; + for (const el of Array.isArray(payload.objects) ? payload.objects : []) { + if (!Array.isArray(el?.geometries)) continue; + for (let k = 0; k < el.geometries.length; k++) { + const g = el.geometries[k]; + const i = g?.sharedGeometry; + if (!Number.isInteger(i) || !isBuffer(table[i])) continue; + const entry = table[i]; + // a copy per use: each mesh owns its geometry (an edit to one must not reach the others) + el.geometries[k] = { + uuid: g.uuid, + type: entry.type, + ...('name' in g ? { name: g.name } : {}), + data: structuredClone(entry.data), + ...(entry.userData !== undefined ? { userData: structuredClone(entry.userData) } : {}) + }; + } + } + delete payload.sharedGeometries; + return payload; +} diff --git a/src/lib/sharedGeometryWrite.js b/src/lib/sharedGeometryWrite.js new file mode 100644 index 000000000..a5a188892 --- /dev/null +++ b/src/lib/sharedGeometryWrite.js @@ -0,0 +1,74 @@ +// 102b-papercuts: the WRITE half of sharedGeometry.js (see it for the file shape). A lazy chunk: only the +// .tpscene writer (exportSessionZip, already async) loads it, so the boot carries the small read half only. + +/** @param {any} g a toJSON geometry entry -> the key two equal shapes share (no uuid, no name) */ +function shapeKey(g) { + return JSON.stringify([g.type, g.data, g.userData ?? null]); +} + +/** a buffer geometry entry worth sharing @param {any} g */ +function isBuffer(g) { + return g && typeof g === 'object' && g.type === 'BufferGeometry' && g.data && typeof g.data === 'object' && g.data.attributes?.position; +} + +/** the size of a position attribute, for the older build's stand-in box @param {any} data */ +function sizeOf(data) { + const a = data?.attributes?.position?.array; + const lo = [Infinity, Infinity, Infinity]; + const hi = [-Infinity, -Infinity, -Infinity]; + if (Array.isArray(a)) + for (let i = 0; i + 2 < a.length; i += 3) + for (let k = 0; k < 3; k++) { + const v = a[i + k]; + if (v < lo[k]) lo[k] = v; + if (v > hi[k]) hi[k] = v; + } + const d = (/** @type {number} */ k) => (hi[k] > lo[k] ? Math.round((hi[k] - lo[k]) * 1e4) / 1e4 : 0.01); + return { width: d(0), height: d(1), depth: d(2) }; +} + +/** + * The payload as it goes into the file: repeated geometries moved into `sharedGeometries`. Returns the SAME + * object when nothing repeats; otherwise a shallow copy (the live payload is never changed). + * @param {any} payload @returns {any} + */ +export function shareGeometries(payload) { + const objects = Array.isArray(payload?.objects) ? payload.objects : null; + if (!objects || Array.isArray(payload.sharedGeometries)) return payload; + /** @type {Map} */ + const seen = new Map(); + for (const el of objects) + for (const g of Array.isArray(el?.geometries) ? el.geometries : []) + if (isBuffer(g)) { + const key = shapeKey(g); + seen.set(key, (seen.get(key) ?? 0) + 1); + } + /** @type {Map} key -> table index */ + const index = new Map(); + /** @type {any[]} */ + const table = []; + for (const [key, n] of seen) if (n > 1) index.set(key, -1); + if (!index.size) return payload; + const out = objects.map((/** @type {any} */ el) => { + if (!Array.isArray(el?.geometries) || !el.geometries.some(isBuffer)) return el; + let changed = false; + const geometries = el.geometries.map((/** @type {any} */ g) => { + if (!isBuffer(g)) return g; + const key = shapeKey(g); + let i = index.get(key); + if (i === undefined) return g; + if (i < 0) { + i = table.length; + table.push({ type: g.type, data: g.data, ...(g.userData !== undefined ? { userData: g.userData } : {}) }); + index.set(key, i); + } + changed = true; + return { uuid: g.uuid, type: 'BoxGeometry', ...('name' in g ? { name: g.name } : {}), ...sizeOf(g.data), sharedGeometry: i }; + }); + return changed ? { ...el, geometries } : el; + }); + return { ...payload, objects: out, sharedGeometries: table }; +} + + +export { inflateSharedGeometries } from './sharedGeometry.js'; diff --git a/src/lib/uniformWrite.js b/src/lib/uniformWrite.js new file mode 100644 index 000000000..896c489b3 --- /dev/null +++ b/src/lib/uniformWrite.js @@ -0,0 +1,80 @@ +// 102b-papercuts (row 18): what a Set Shader Uniform node writes into a live uniform slot. +// A LEAF (imports nothing) so the flow runtime stays free of the shader compiler (a lazy +// chunk) and the rule is testable with no GL context. +// +// A float slot takes the number, as SH7 always did. A vec2/vec3/vec4 slot holds an ARRAY +// (shaderCompile.uniformValue), and it takes, in this order: +// 1. a wired or typed VECTOR (`vector`, a [x, y, z] array the vector3 socket carries), +// 2. a COLOUR (`color`, '#rrggbb', converted sRGB -> linear exactly as the compiler does +// for an authored colour, or the driven colour and the authored one would not match), +// 3. the NUMBER, written into every component (a grey, or a uniform scale). +// A vec4 keeps its own 4th component unless the vector names one. Writes are IN PLACE on +// the array three already uploads from, so nothing recompiles. Anything unreadable writes +// nothing: the node "does nothing" rather than breaking the material, as the docs promise. + +/** @param {number} c */ +function srgbToLinear(c) { + return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4); +} + +/** '#rrggbb' -> linear [r, g, b], or null when it is not a colour @param {any} hex */ +export function hexToLinear(hex) { + if (typeof hex !== 'string') return null; + const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim()); + if (!m) return null; + const n = parseInt(m[1], 16); + return [(n >> 16) & 255, (n >> 8) & 255, n & 255].map((c) => srgbToLinear(c / 255)); +} + +/** a vector socket value as numbers, or null @param {any} v @returns {number[] | null} */ +function vectorOf(v) { + if (Array.isArray(v)) { + const out = v.slice(0, 4).map(Number); + return out.length && out.every(Number.isFinite) ? out : null; + } + if (v && typeof v === 'object' && Number.isFinite(+v.x)) { + const out = [+v.x, +v.y, +v.z, v.w].filter((n, i) => i < 3 || n !== undefined).map(Number); + return out.every(Number.isFinite) ? out : null; + } + return null; +} + +/** + * Write a node's resolved data into a uniform slot. Returns what kind of value was written + * ('number' | 'vector' | 'color') or null when nothing was. + * @param {{value: any} | null | undefined} slot + * @param {{value?: any, color?: any, vector?: any}} data + * @returns {'number' | 'vector' | 'color' | null} + */ +export function writeUniformSlot(slot, data) { + if (!slot) return null; + const cur = slot.value; + if (Array.isArray(cur) || (cur && typeof cur === 'object' && typeof cur.fromArray === 'function')) { + /** @type {any} */ const target = cur; + const n = Array.isArray(cur) ? cur.length : typeof cur.w === 'number' ? 4 : typeof cur.z === 'number' || typeof cur.b === 'number' ? 3 : 2; + /** @type {number[] | null} */ let src = vectorOf(data.vector); + /** @type {'vector' | 'color' | 'number'} */ let kind = 'vector'; + if (!src) { + src = hexToLinear(data.color); + kind = 'color'; + } + if (!src) { + const num = Number(data.value); + if (!Number.isFinite(num)) return null; + src = Array.from({ length: Math.min(n, 3) }, () => num); + kind = 'number'; + } + const width = Math.min(n, src.length); + if (Array.isArray(target)) for (let i = 0; i < width; i++) target[i] = src[i]; + else { + // a THREE.Vector2/3/4 or Color, should a backend hand one over + const keys = typeof target.r === 'number' ? ['r', 'g', 'b'] : ['x', 'y', 'z', 'w']; + for (let i = 0; i < width && i < keys.length; i++) target[keys[i]] = src[i]; + } + return kind; + } + const value = Number(data.value); + if (!Number.isFinite(value)) return null; + slot.value = value; + return 'number'; +} diff --git a/tests/e2e/battery.json b/tests/e2e/battery.json index 0dc5837fc..005870d33 100644 --- a/tests/e2e/battery.json +++ b/tests/e2e/battery.json @@ -304,6 +304,7 @@ "import-file": "agent" }, "suites": { + "papercuts-102b": 300, "render-seam": 180, "load-check": 240, "schema-two-peers": 360, @@ -1037,6 +1038,7 @@ "context-menu-overflow": false, "context-menu-redesign": false, "context-menu-v2": false, + "convert-to-code-sync": false, "csp-build": false, "deform-stack": false, "diagnostics": false, @@ -1092,6 +1094,7 @@ "gamepad-input": false, "gem-arena-nodes": false, "gltf-export-selection": false, + "guard-patrol-template": false, "hdri-drop-toast": false, "hit-slop": false, "hotkeys-layout": false, diff --git a/tests/e2e/papercuts-102b.test.cjs b/tests/e2e/papercuts-102b.test.cjs new file mode 100644 index 000000000..456f102e8 --- /dev/null +++ b/tests/e2e/papercuts-102b.test.cjs @@ -0,0 +1,408 @@ +// Roadmap 102b — the paper cuts, end to end on two real peers (A hosts, B joins): +// 1 a Box gets a corner radius (agent set_geometry): B rebuilds the same rounded mesh; the Inspector +// shows Corner radius / Corner segs with their tooltips and hides the square segment rows +// 2 a BAKED rounded box is OFFERED a Convert (never automatic): the button by keyboard, B follows, one +// undo puts the baked mesh back on both +// 3 the file: a rounded box saves as a Box with two numbers; two equal baked meshes are written once +// (sharedGeometries); the file reads back to the same vertices; B reloads and rejoins with the +// rounded box intact (two-peer + reload) +// 4 an OLD scene (an official .tpscene, OLD_SCENE=) loads with the same objects and vertices +// 5 Set Shader Uniform drives a COLOUR and a VECTOR uniform (sRGB -> linear, like an authored colour) +// 6 a Script node's event output fires once per rising edge, and a Counter counts it on BOTH peers +// 7 an effect that reaches no object says "acts on nothing" on its card and in validate_scene; wired, the +// line goes +// 8 fog kinds: set_sky {kind: 'height'} reaches B as FogExp2 + the height layer; the Inspector's Kind +// row switches it back to Linear by keyboard +// 9 a cube-map cross (PNG) uploaded through Upload HDRI… becomes an equirectangular .hdr library item +// 10 feature unused = zero change: a plain box saves exactly the params it did, a scene with no repeats +// writes no sharedGeometries, a linear fog adds no keys +// OLD_URL= adds 3b: the file this build wrote opens there (objects + rounded box size). +// Run: e2e-slot --dev 7106 -- node tests/e2e/papercuts-102b.test.cjs +const fs = require('fs'); +const zlib = require('zlib'); +const h = require('./helpers.cjs'); + +const exec = (peer, tool, args) => peer.page.evaluate(([x, a]) => window.__tp.test.v1.aiTools.executeAiTool(x, a), [tool, args]); +const short = (v) => JSON.stringify(v ?? null).slice(0, 220); + +/** one object's geometry facts on a page */ +const geoOf = (page, uuid) => + page.evaluate((u) => { + /** @type {any} */ let g; + window.__tp.test.v1.objectsGroup.subscribe((x) => (g = x))(); + const o = g?.getObjectByProperty('uuid', u); + if (!o?.geometry) return null; + o.geometry.computeBoundingBox(); + const b = o.geometry.boundingBox; + return { + type: o.geometry.type, + verts: o.geometry.attributes.position.count, + typed: !!o.geometry.parameters, + radius: o.geometry.parameters?.radius ?? null, + gp: JSON.parse(JSON.stringify(o.userData.geometryParams ?? null)), + size: [b.max.x - b.min.x, b.max.y - b.min.y, b.max.z - b.min.z].map((v) => Math.round(v * 1000) / 1000) + }; + }, uuid); + +/** a tiny RGB PNG from a pixel function (node zlib; no image library) */ +function png(w, h, px) { + const raw = Buffer.alloc((w * 3 + 1) * h); + for (let y = 0; y < h; y++) { + raw[y * (w * 3 + 1)] = 0; + for (let x = 0; x < w; x++) raw.set(px(x, y), y * (w * 3 + 1) + 1 + x * 3); + } + const crcTable = Array.from({ length: 256 }, (_, n) => { + let c = n; + for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; + return c >>> 0; + }); + const crc = (buf) => { + let c = 0xffffffff; + for (const b of buf) c = crcTable[(c ^ b) & 255] ^ (c >>> 8); + return (c ^ 0xffffffff) >>> 0; + }; + const chunk = (type, data) => { + const len = Buffer.alloc(4); + len.writeUInt32BE(data.length); + const td = Buffer.concat([Buffer.from(type), data]); + const c = Buffer.alloc(4); + c.writeUInt32BE(crc(td)); + return Buffer.concat([len, td, c]); + }; + const ihdr = Buffer.alloc(13); + ihdr.writeUInt32BE(w, 0); + ihdr.writeUInt32BE(h, 4); + ihdr[8] = 8; + ihdr[9] = 2; + return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ihdr), chunk('IDAT', zlib.deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]); +} + +/** the scene as a .tpscene (the real writer), base64 */ +const saveFile = (page) => + page.evaluate(async () => { + const s = window.__tp.test.v1.sessions; + const bytes = await s.exportSessionZip(s.buildSessionPayload('102b'), { assets: false, packs: false }); + let bin = ''; + for (let i = 0; i < bytes.length; i += 0x8000) bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000)); + return btoa(bin); + }); +/** session.json inside a base64 .tpscene, as the file holds it (no inflate) */ +async function sessionJsonOf(b64) { + const { unzipSync, strFromU8 } = await import('fflate'); + return JSON.parse(strFromU8(unzipSync(new Uint8Array(Buffer.from(b64, 'base64')))['session.json'])); +} + +h.run(async () => { + const browser = await h.launch(); + const A = await h.setupPage(browser, 'A'); + const B = await h.setupPage(browser, 'B'); + await h.connect(B, A); + + const make = async (name, position, params = [1, 1, 1]) => { + const r = await exec(A, 'create_objects', { objects: [{ kind: 'primitive', primitive: 'Box', params, name, position }] }); + const id = r?.created?.[0]?.uuid; + if (!id) throw new Error('create_objects: ' + short(r)); + await h.eventually(() => geoOf(B.page, id), (o) => !!o, 'premise: B has ' + name, 20000); + return id; + }; + + // ---- 10a (first, on a clean scene): a plain box keeps exactly its six params + const plain = await make('Plain', [-6, 0.5, 0]); + const plainGeo = await geoOf(A.page, plain); + h.check( + JSON.stringify(Object.keys(plainGeo.gp?.params ?? {})) === JSON.stringify(['width', 'height', 'depth', 'widthSegments', 'heightSegments', 'depthSegments']) && plainGeo.verts === 24, + '10. a box that never uses a radius stores the six params it always did: ' + short(plainGeo.gp) + ); + const plainFile = await sessionJsonOf(await saveFile(A.page)); + h.check(!('sharedGeometries' in plainFile), '10. a scene with no repeated mesh writes no sharedGeometries'); + const plainBoxJson = plainFile.objects.flatMap((e) => e.geometries ?? []).find((g) => g.type === 'BoxGeometry'); + h.check(plainBoxJson && !('radius' in plainBoxJson) && !('radiusSegments' in plainBoxJson), '10. ...and its Box JSON has no radius keys: ' + short(plainBoxJson)); + + // ---- 1. corner radius + console.log('\n=== 1. rounded box ==='); + const crate = await make('Crate', [0, 0.5, 0], [2, 1, 1]); + const set = await exec(A, 'set_geometry', { updates: [{ uuid: crate, params: { radius: 0.2, radiusSegments: 3 } }] }); + h.check(!set?.error, '1. set_geometry takes {radius, radiusSegments}: ' + short(set)); + await h.eventually(() => geoOf(B.page, crate), (o) => o?.verts === 1764 && o.gp?.params?.radius === 0.2, '1. B rebuilds the same rounded box (1764 vertices, radius 0.2)', 20000); + const a1 = await geoOf(A.page, crate); + h.check(a1.verts === 1764 && a1.type === 'BoxGeometry' && a1.radius === 0.2 && a1.size.join() === '2,1,1', '1. A: a Box with radius 0.2, size 2 x 1 x 1: ' + short(a1)); + const listed = await exec(A, 'list_scene', {}); + h.check(JSON.stringify(listed).includes('"radius":0.2'), '1. list_scene reports the radius under geometry'); + await A.page.evaluate((u) => { + const s = window.__tp.test.v1; + let g; + s.objectsGroup.subscribe((x) => (g = x))(); + s.selectedObject.set(g.getObjectByProperty('uuid', u)); + s.showSidebar('properties'); + }, crate); + await A.page.waitForSelector('#inspector-geometry', { timeout: 15000 }).catch(() => {}); + const rows = await A.page.evaluate(() => { + const box = document.getElementById('inspector-geometry'); + const txt = box?.textContent ?? ''; + return { + radius: !!document.getElementById('geo-radius'), + segs: !!document.getElementById('geo-radiusSegments'), + tip: document.getElementById('geo-radius')?.closest('[title^="Rounds"]')?.getAttribute('title') ?? '', + squareSegs: /Width segs/.test(txt) + }; + }); + h.check(rows.radius && rows.segs && /Rounds every edge/.test(rows.tip), '1. the Inspector shows Corner radius + Corner segs with a tooltip: ' + short(rows)); + h.check(!rows.squareSegs, '1. ...and hides the square box segment rows while it is rounded'); + + // ---- 2. Convert a baked one + console.log('\n=== 2. Convert ==='); + const baked = await make('Baked', [3, 0.5, 0], [1.2, 0.8, 0.6]); + await exec(A, 'set_geometry', { updates: [{ uuid: baked, params: { radius: 0.08, radiusSegments: 2 } }] }); + await h.eventually(() => geoOf(B.page, baked), (o) => o?.verts === 900, 'premise: B has the rounded box', 20000); + // bake it on both pages, as the template generator did (a plain mesh of vertices, no params) + for (const P of [A, B]) + await P.page.evaluate((u) => { + const s = window.__tp.test.v1; + let g; + s.objectsGroup.subscribe((x) => (g = x))(); + const o = g.getObjectByProperty('uuid', u); + const out = new s.THREE.BufferGeometry(); + for (const n of ['position', 'normal', 'uv']) out.setAttribute(n, o.geometry.attributes[n].clone()); + o.geometry.dispose(); + o.geometry = out; + delete o.userData.geometryParams; + s.pokeScene?.(); + }, baked); + const bakedA = await geoOf(A.page, baked); + h.check(!bakedA.typed && !bakedA.gp && bakedA.verts === 900, 'premise: the box is baked (a plain mesh, no params): ' + short(bakedA)); + await A.page.evaluate((u) => { + const s = window.__tp.test.v1; + let g; + s.objectsGroup.subscribe((x) => (g = x))(); + s.selectedObject.set(g.getObjectByProperty('uuid', u)); + }, baked); + await A.page.waitForSelector('#geometry-convert-rounded', { timeout: 15000 }).catch(() => {}); + const offer = await A.page.evaluate(() => ({ + text: document.getElementById('geometry-rounded-offer')?.textContent?.trim() ?? '', + tip: document.getElementById('geometry-convert-rounded')?.getAttribute('title') ?? '' + })); + h.check(/rounded box \(1\.2 x 0\.8 x 0\.6 m, corner 0\.08 m\)/.test(offer.text) && /looks the same/.test(offer.tip), '2. the Inspector OFFERS a Convert, with the box it found and a tooltip: ' + short(offer)); + await new Promise((r) => setTimeout(r, 1200)); + h.check((await geoOf(A.page, baked)).gp === null, '2. ...and converts nothing by itself'); + await A.page.focus('#geometry-convert-rounded'); + await A.page.keyboard.press('Enter'); + await h.eventually(() => geoOf(A.page, baked), (o) => o?.typed && o.gp?.params?.radius === 0.08, '2. Enter on the button converts it (a Box with radius 0.08)', 10000); + await h.eventually(() => geoOf(B.page, baked), (o) => o?.typed && o.gp?.params?.radius === 0.08 && o.verts === 900, '2. B follows (the same 900 vertices, now a Box)', 20000); + await A.page.evaluate(() => window.__tp.test.v1.history.undo()); + await h.eventually(() => geoOf(A.page, baked), (o) => o && !o.typed && !o.gp && o.verts === 900, '2. one undo puts the baked mesh back on A', 10000); + await h.eventually(() => geoOf(B.page, baked), (o) => o && !o.typed && !o.gp && o.verts === 900, '2. ...and on B', 20000); + + // ---- 3. the file + console.log('\n=== 3. the file ==='); + // a second baked copy of the same mesh: two equal shapes + const twin = await A.page.evaluate((u) => { + const s = window.__tp.test.v1; + let g; + s.objectsGroup.subscribe((x) => (g = x))(); + const o = g.getObjectByProperty('uuid', u); + const copy = new s.THREE.Mesh(o.geometry.clone(), o.material); + copy.name = 'Baked twin'; + copy.position.set(5, 0.5, 0); + g.add(copy); + return copy.uuid; + }, baked); + const b64 = await saveFile(A.page); + const file = await sessionJsonOf(b64); + const crateJson = file.objects.find((e) => e.object?.uuid === crate)?.geometries?.[0]; + h.check(crateJson?.type === 'BoxGeometry' && crateJson.radius === 0.2 && crateJson.radiusSegments === 3 && !crateJson.data, '3. the rounded box is a Box with two numbers in the file: ' + short(crateJson)); + h.check(JSON.stringify(crateJson).length < 400, '3. ...a few hundred bytes, not ' + 1764 * 8 * 4 + '+ of vertices (' + JSON.stringify(crateJson).length + ' bytes)'); + const stubs = file.objects.filter((e) => [baked, twin].includes(e.object?.uuid)).map((e) => e.geometries?.[0]); + h.check(Array.isArray(file.sharedGeometries) && file.sharedGeometries.length === 1, '3. two equal baked meshes are written once (sharedGeometries: ' + (file.sharedGeometries?.length ?? 0) + ')'); + h.check(stubs.length === 2 && stubs.every((g) => g.type === 'BoxGeometry' && g.sharedGeometry === 0 && g.width === 1.2), '3. ...each use is a plain-box stand-in an older build can draw: ' + short(stubs[0])); + h.check(file.formatMinor === 4 && file.format === 1, '3. format 1, minor 4 (additive: an older build loads it): ' + file.format + '.' + file.formatMinor); + const back = await A.page.evaluate(async (b) => { + const bytes = Uint8Array.from(atob(b), (c) => c.charCodeAt(0)); + const payload = await window.__tp.test.v1.sessions.readSessionZip(bytes.buffer); + const out = {}; + for (const e of payload.objects) { + const o = new (window.__tp.test.v1.THREE.ObjectLoader)().parse(e); + out[o.name] = { type: o.geometry?.type, verts: o.geometry?.attributes?.position?.count, radius: o.geometry?.parameters?.radius ?? null }; + } + return { out, shared: 'sharedGeometries' in payload }; + }, b64); + h.check(!back.shared && back.out.Crate?.verts === 1764 && back.out.Crate.radius === 0.2 && back.out.Baked?.verts === 900 && back.out['Baked twin']?.verts === 900, '3. the file reads back to the same meshes: ' + short(back.out)); + if (process.env.OUT) fs.writeFileSync(process.env.OUT + '/102b-saved.tpscene', Buffer.from(b64, 'base64')); + + // two-peer + reload: B reloads, rejoins, and has the rounded box (GLTF sync carries vertices + params) + // a loaded machine can take longer than the helper's 30 s to boot the reloaded page + await h.freshReload(B).catch(() => B.page.waitForFunction(() => window.__tp?.test.v1 && !!window.__tp.test.v1.moduleSDK, null, { timeout: 120000 })); + await h.answerRestore(B).catch(() => false); + await h.eventually(() => B.page.evaluate(() => { let p; window.__tp.test.v1.peers.subscribe((x) => (p = x))(); const o = p?.openedPeers; return o ? (o.size ?? o.length ?? Object.keys(o).length) : 0; }), (n) => n >= 1, '3. B is back in the session after its reload', 60000); + await h.eventually(() => geoOf(B.page, crate), (o) => o?.verts === 1764 && o.gp?.params?.radius === 0.2, '3. after the reload B has the rounded box with its radius (rows editable)', 60000); + await exec(A, 'set_geometry', { updates: [{ uuid: crate, params: { radius: 0.4 } }] }); + await h.eventually(() => geoOf(B.page, crate), (o) => o?.gp?.params?.radius === 0.4, '3. ...and a later radius edit still reaches it', 20000); + + // ---- 3b. the newest release opens this file + if (process.env.OLD_URL) { + console.log('\n=== 3b. the newest release ==='); + const ctx = await browser.newContext({ ignoreHTTPSErrors: true, viewport: { width: 1280, height: 800 } }); + await ctx.addInitScript(() => { + localStorage.setItem('debugStores', 'true'); + localStorage.setItem('errorReports:send', 'false'); + localStorage.setItem('perfReports:offered', 'no'); + }); + const old = await ctx.newPage(); + await old.goto(process.env.OLD_URL, { waitUntil: 'domcontentloaded', timeout: 120000 }); + await old.waitForFunction(() => !!window.__tp?.test?.v1?.sessions, null, { timeout: 120000 }); + const there = await old.evaluate(async (b) => { + const bytes = Uint8Array.from(atob(b), (c) => c.charCodeAt(0)); + const s = window.__tp.test.v1.sessions; + const payload = await s.readSessionZip(bytes.buffer); + if (!payload) return { refused: true }; + const out = {}; + for (const e of payload.objects) { + const o = new (window.__tp.test.v1.THREE.ObjectLoader)().parse(e); + o.geometry?.computeBoundingBox?.(); + const bb = o.geometry?.boundingBox; + out[o.name] = bb ? [bb.max.x - bb.min.x, bb.max.y - bb.min.y, bb.max.z - bb.min.z].map((v) => Math.round(v * 100) / 100).join('x') : null; + } + return { out }; + }, b64); + h.check(!there.refused && there.out?.Crate === '2x1x1' && there.out?.Baked === '1.2x0.8x0.6' && there.out?.['Baked twin'] === '1.2x0.8x0.6' && there.out?.Plain === '1x1x1', + '3b. the newest release opens the file: every object, each the right size (rounded box and repeats as plain boxes): ' + short(there)); + await ctx.close(); + } else console.log(' (3b skipped: set OLD_URL to the newest release)'); + + // remove the local-only twin (B never had it) + await A.page.evaluate((u) => { + let g; + window.__tp.test.v1.objectsGroup.subscribe((x) => (g = x))(); + const o = g.getObjectByProperty('uuid', u); + o?.parent?.remove(o); + }, twin); + + // ---- 4. an old scene loads unchanged + if (process.env.OLD_SCENE && fs.existsSync(process.env.OLD_SCENE)) { + console.log('\n=== 4. an old scene ==='); + const { unzipSync, strFromU8 } = await import('fflate'); + const raw = fs.readFileSync(process.env.OLD_SCENE); + const orig = JSON.parse(strFromU8(unzipSync(new Uint8Array(raw))['session.json'])); + const want = { objects: orig.objects.length, floats: orig.objects.flatMap((e) => e.geometries ?? []).reduce((n, g) => n + (g.data?.attributes?.position?.array?.length ?? 0), 0) }; + const got = await A.page.evaluate(async (b) => { + const bytes = Uint8Array.from(atob(b), (c) => c.charCodeAt(0)); + const payload = await window.__tp.test.v1.sessions.readSessionZip(bytes.buffer); + return { + objects: payload.objects.length, + floats: payload.objects.flatMap((e) => e.geometries ?? []).reduce((n, g) => n + (g.data?.attributes?.position?.array?.length ?? 0), 0), + shared: 'sharedGeometries' in payload, + minor: payload.formatMinor ?? 0 + }; + }, raw.toString('base64')); + h.check(got.objects === want.objects && got.floats === want.floats && !got.shared, '4. an old scene (' + process.env.OLD_SCENE.split('/').slice(-2).join('/') + ', minor ' + got.minor + ') reads back unchanged: ' + short(got) + ' vs ' + short(want)); + } else console.log(' (4 skipped: set OLD_SCENE to an official .tpscene)'); + + // ---- 5. Set Shader Uniform: colour + vector + console.log('\n=== 5. Set Shader Uniform ==='); + const sign = await make('Sign', [8, 0.5, 0]); + const shader = await exec(A, 'create_shader', { objects: [sign], text: 'c = color {value: "#000000"}\nv = vector3 {value: [0, 0, 0]}\nout = surface\nc -> out.emissive\nv -> out.position' }); + console.log(' create_shader: ' + short(shader?.created?.[0]?.uniforms ?? shader)); + const uniforms = (shader?.created?.[0]?.uniforms ?? []).map((u) => u.uniform); + const cu = uniforms.find((u) => /^u_c_/.test(u)); + const vu = uniforms.find((u) => /^u_v_/.test(u)); + const uni = (page, name) => page.evaluate(([u, n]) => { const v = window.__tp.test.v1.shaderGraph.shaderUniform(u, n)?.value; return v == null ? null : Array.from(v); }, [sign, name]); + await h.eventually(() => uni(A.page, cu), (v) => Array.isArray(v) && v.length === 3, 'premise: the colour uniform compiled on A (' + cu + ')', 30000); + const flow = await exec(A, 'create_flow_nodes', { + graph: sign, + text: 'pick = colorpicker {color: "#ff8000"}\nset = setuniform {uniform: "' + cu + '"}\npick -> set.color\nvec = vector3 {x: 0, y: 0.25, z: 0}\nsetv = setuniform {uniform: "' + vu + '"}\nvec -> setv.vector' + }); + h.check(!flow?.error && !flow?.partial, '5. colour and vector wires into Set Shader Uniform are accepted: ' + short(flow?.errors ?? flow?.error ?? 'ok')); + const lin = (c) => (c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4)); + const want5 = [1, lin(128 / 255), 0]; + for (const P of [A, B]) { + await h.eventually(() => uni(P.page, cu), (v) => Array.isArray(v) && v.every((x, i) => Math.abs(x - want5[i]) < 1e-3), '5. ' + P.name + ': the colour drives the uniform, sRGB -> linear (' + want5.map((x) => x.toFixed(3)) + ')', 30000); + await h.eventually(() => uni(P.page, vu), (v) => Array.isArray(v) && Math.abs(v[1] - 0.25) < 1e-6 && v[0] === 0, '5. ' + P.name + ': the vector drives the vec3 uniform (0, 0.25, 0)', 30000); + } + + // ---- 6. Script event output + console.log('\n=== 6. Script event ==='); + const scr = await exec(A, 'create_flow_nodes', { + graph: 'scene', + nodes: [ + { ref: 'tick', type: 'script', data: { code: 'return { hit: Math.floor(time / 2) % 2 === 0 };', outputs: [{ name: 'hit', type: 'event' }] } }, + { ref: 'count', type: 'counter' } + ], + edges: [{ from: 'tick', fromHandle: 'hit', to: 'count', toHandle: 'pulse' }] + }); + console.log(" create_flow_nodes: " + JSON.stringify(scr).slice(0, 900)); + const countId = scr?.created?.find?.((c) => c.ref === 'count')?.id ?? scr?.created?.[1]?.id; + const counterOf = (page) => page.evaluate((id) => { let t; window.__tp.test.v1.flowTriggers.subscribe((x) => (t = x))(); return t?.[id]?.count ?? 0; }, countId); + // the first Script on a page loads the sandbox guest (lazy): wait for the first pulse on each peer, then measure one window + for (const P of [A, B]) await h.eventually(() => counterOf(P.page), (n) => n >= 1, 'premise: ' + P.name + ' runs the Script (sandbox loaded, first pulse)', 60000); + // a 4 s square wave (2 s halves): the flow tick rides rAF, and a loaded headless page draws ~1-4 frames a + // second, so a faster wave would be sampled too coarsely to see every edge (that is aliasing, not a miss) + const fps = await A.page.evaluate(() => new Promise((r) => { let n = 0; const t0 = performance.now(); const f = () => { n++; if (performance.now() - t0 < 2000) requestAnimationFrame(f); else r(Math.round((n / 2) * 10) / 10); }; requestAnimationFrame(f); })); + console.log(' A draws ' + fps + ' frames/s'); + const [s0, t0] = [await counterOf(A.page), await counterOf(B.page)]; + await new Promise((r) => setTimeout(r, 12200)); + const [s1, t1] = [await counterOf(A.page), await counterOf(B.page)]; + h.check(s1 - s0 >= 2 && s1 - s0 <= 4, '6. the Script event fires once per rising edge (a 4 s square wave over 12.2 s: +' + (s1 - s0) + ', 2..4)'); + h.check(t1 - t0 >= 2 && t1 - t0 <= 4, '6. B counts it too (its own run of the same code: +' + (t1 - t0) + ')'); + + // ---- 7. an effect that acts on nothing + console.log('\n=== 7. acts on nothing ==='); + const spin = await exec(A, 'create_flow_nodes', { graph: 'scene', nodes: [{ ref: 's', type: 'spin' }] }); + const spinId = spin?.created?.[0]?.id; + await A.page.evaluate(() => window.__tp.test.v1.nodeLiveState.liveStateGraph.set('scene')); + const lineOf = () => A.page.evaluate((id) => { let m; window.__tp.test.v1.nodeLiveState.nodeLiveStates.subscribe((x) => (m = x))(); return m?.[id] ?? null; }, spinId); + await h.eventually(lineOf, (t) => t === 'acts on nothing: add an Object Selector', '7. an unwired Spin in the scene graph says "acts on nothing" on its card', 10000); + const lint = await exec(A, 'validate_scene', {}); + h.check(JSON.stringify(lint).includes('effect-no-object'), '7. validate_scene lists it (effect-no-object)'); + const wire = await exec(A, 'create_flow_nodes', { graph: 'scene', nodes: [{ ref: 'sel', type: 'objectselector', data: { selected: plain } }], edges: [{ from: spinId, to: 'sel' }] }); + h.check(!wire?.error, '7. wiring it to an Object Selector: ' + short(wire?.error ?? 'ok')); + await h.eventually(lineOf, (t) => t !== 'acts on nothing: add an Object Selector', '7. ...and the line goes', 10000); + await A.page.evaluate(() => window.__tp.test.v1.nodeLiveState.liveStateGraph.set(null)); + + // ---- 8. fog kinds + console.log('\n=== 8. fog ==='); + const fogSet = await exec(A, 'set_sky', { fog: { color: '#a0b0c0', kind: 'height', density: 0.05, base: 0, thickness: 4 } }); + h.check(!fogSet?.error, '8. set_sky takes a height fog: ' + short(fogSet?.error ?? 'ok')); + const fogOn = (page) => page.evaluate(() => { + let sc; + window.__tp.test.v1.globalScene.subscribe((v) => (sc = v))(); + const f = sc?.fog ?? null; + return { exp2: !!f?.isFogExp2, linear: !!f?.isFog, density: f?.density ?? null }; + }); + const envOf = (P) => exec(P, 'get_environment', {}); + await h.eventually(() => envOf(B), (e) => JSON.stringify(e).includes('"kind":"height"'), '8. B reads a height fog back (get_environment)', 20000); + const fb = await fogOn(B.page); + h.check(fb.exp2 && Math.abs(fb.density - 0.05) < 1e-6, '8. B draws it as a distance-integrated fog (FogExp2, density 0.05): ' + short(fb)); + // the Fog rows are a lazy chunk: under load the section can need a second ask and longer than 15 s + for (let i = 0; i < 4; i++) { + await A.page.evaluate(() => window.__tp.test.v1.openSceneSection('Fog')); + if (await A.page.waitForSelector('#fog-kind', { timeout: 15000 }).then(() => true, () => false)) break; + } + const kindRow = await A.page.evaluate(() => ({ there: !!document.getElementById('fog-kind'), tip: document.getElementById('fog-kind')?.getAttribute('title') ?? document.getElementById('fog-kind')?.closest('[title]')?.getAttribute('title') ?? '', base: !!document.getElementById('fog-base'), near: !!document.getElementById('fog-near') })); + h.check(kindRow.there && kindRow.base && !kindRow.near, '8. the Fog rows show Kind + Ground height, and hide Near/Far for a height fog: ' + short(kindRow)); + const picked = await exec(A, 'set_sky', { fog: { kind: 'linear', near: 5, far: 60 } }); + h.check(!picked?.error, '8. back to a linear fog'); + await h.eventually(() => envOf(B), (e) => !JSON.stringify(e).includes('"kind":"height"'), '8. B follows back to linear', 20000); + const fl = await fogOn(B.page); + h.check(fl.linear && !fl.exp2, '8. ...as THREE.Fog again (the fog every older build draws): ' + short(fl)); + + // ---- 9. a cube-map cross + console.log('\n=== 9. cube-map sky ==='); + const face = [[220, 60, 60], [60, 220, 60], [60, 60, 220], [220, 220, 60], [60, 220, 220], [220, 60, 220]]; + // a 4:3 horizontal cross, 16 px faces: row 1 = -X +Z +X -Z, +Y above +Z, -Y below + const cell = (x, y) => { + const cx = Math.floor(x / 16), cy = Math.floor(y / 16); + if (cy === 0 && cx === 1) return face[2]; + if (cy === 2 && cx === 1) return face[3]; + if (cy === 1) return face[[1, 4, 0, 5][cx]]; + return [0, 0, 0]; + }; + const cross = png(64, 48, cell); + await A.page.evaluate(() => window.__tp.test.v1.openSceneSection('Environment')); + await A.page.waitForSelector('#hdri-upload-file', { state: 'attached', timeout: 15000 }); + const toastSeen = A.page.waitForFunction(() => /Cube map converted to a sky image/.test(document.body.textContent ?? ''), null, { timeout: 30000 }).then(() => true, () => false); + await A.page.setInputFiles('#hdri-upload-file', { name: 'test-sky-cross.png', mimeType: 'image/png', buffer: cross }); + const items = () => A.page.evaluate(() => { let x; window.__tp.test.v1.explorer.explorerItems.subscribe((v) => (x = v))(); return (x ?? []).filter((i) => /test-sky-cross/.test(i.name)).map((i) => ({ name: i.name, kind: i.kind })); }); + await h.eventually(items, (l) => l.some((i) => i.name === 'test-sky-cross-equirect.hdr' && i.kind === 'hdri'), '9. the cross becomes test-sky-cross-equirect.hdr in the library (kind hdri)', 30000); + h.check(await toastSeen, '9. a toast names the conversion ("Cube map converted to a sky image")'); + + await h.finish(browser); +}); diff --git a/tests/unit/cubeToEquirect.test.js b/tests/unit/cubeToEquirect.test.js new file mode 100644 index 000000000..5bf4a2537 --- /dev/null +++ b/tests/unit/cubeToEquirect.test.js @@ -0,0 +1,154 @@ +// 102b-papercuts (scout row 18): cube-map skies are converted to an equirectangular .hdr on upload +import { describe, it, expect } from 'vitest'; +import * as THREE from 'three'; +import { HDRLoader } from 'three/addons/loaders/HDRLoader.js'; +import { + FACES, cubeLayoutOf, isEquirectShape, faceCells, splitCubeImage, cubeLookup, cubeToEquirect, + encodeRGBE, faceOfFileName, facesFromFiles, fitEquirect, equirectWidthFor +} from '../../src/lib/hdri/cubeToEquirect.js'; + +/** the lookup direction through a face pixel centre (the inverse of cubeLookup) */ +function faceDir(/** @type {string} */ face, /** @type {number} */ s, /** @type {number} */ t) { + const sc = s * 2 - 1; + const tc = t * 2 - 1; + /** @type {Record} */ + const d = { + px: [1, -tc, -sc], nx: [-1, -tc, sc], py: [sc, 1, tc], ny: [sc, -1, -tc], pz: [sc, -tc, 1], nz: [-sc, -tc, -1] + }; + const v = d[face]; + const l = Math.hypot(v[0], v[1], v[2]); + return v.map((x) => x / l); +} +/** six faces painted with their own lookup direction (rgb = dir + 1) */ +function paintedFaces(size = 16) { + /** @type {any} */ const faces = {}; + for (const f of FACES) { + const data = new Float32Array(size * size * 4); + for (let y = 0; y < size; y++) + for (let x = 0; x < size; x++) { + const dir = faceDir(f, (x + 0.5) / size, (y + 0.5) / size); + data.set([dir[0] + 1, dir[1] + 1, dir[2] + 1, 1], (y * size + x) * 4); + } + faces[f] = { data, width: size, height: size }; + } + return faces; +} +/** pack faces into a layout image */ +function packed(/** @type {any} */ faces, /** @type {any} */ layout) { + const size = faces.px.width; + const [cw, ch] = { hcross: [4, 3], vcross: [3, 4], hstrip: [6, 1], vstrip: [1, 6] }[/** @type {'hcross'} */ (layout)]; + const W = cw * size; + const data = new Float32Array(W * ch * size * 4); + const cells = faceCells(layout); + for (const f of FACES) { + const { cx, cy, turned } = /** @type {any} */ (cells)[f]; + for (let y = 0; y < size; y++) + for (let x = 0; x < size; x++) { + const sx = turned ? size - 1 - x : x; + const sy = turned ? size - 1 - y : y; + const si = (sy * size + sx) * 4; + data.set(faces[f].data.subarray(si, si + 4), ((cy * size + y) * W + cx * size + x) * 4); + } + } + return { data, width: W, height: ch * size }; +} + +describe('cube layouts', () => { + it('recognises cross and strip shapes, and nothing else', () => { + expect(cubeLayoutOf(4096, 3072)).toBe('hcross'); + expect(cubeLayoutOf(3072, 4096)).toBe('vcross'); + expect(cubeLayoutOf(6144, 1024)).toBe('hstrip'); + expect(cubeLayoutOf(1024, 6144)).toBe('vstrip'); + expect(cubeLayoutOf(2048, 1024)).toBe(null); + expect(isEquirectShape(2048, 1024)).toBe(true); + expect(isEquirectShape(4096, 3072)).toBe(false); + }); + it('every layout splits back into the same six faces', () => { + const faces = paintedFaces(8); + for (const layout of ['hcross', 'vcross', 'hstrip', 'vstrip']) { + const split = /** @type {any} */ (splitCubeImage(packed(faces, layout))); + for (const f of FACES) expect(Array.from(split[f].data)).toEqual(Array.from(faces[f].data)); + } + }); + it('the major-axis lookup lands in the middle of each face', () => { + expect(cubeLookup(1, 0, 0)).toEqual({ face: 'px', s: 0.5, t: 0.5 }); + expect(cubeLookup(0, -1, 0).face).toBe('ny'); + expect(cubeLookup(0, 0, -1).face).toBe('nz'); + // +Y's bottom row meets +Z's top row (the cross is continuous) + expect(cubeLookup(0, 1, 0.999).t).toBeGreaterThan(0.99); + expect(cubeLookup(0, 0.999, 1).face).toBe('pz'); + expect(cubeLookup(0, 0.999, 1).t).toBeLessThan(0.01); + }); +}); + +describe('cubeToEquirect', () => { + it('each equirect pixel holds the cube texel three would show in that direction (x flipped)', () => { + const eq = cubeToEquirect(paintedFaces(32), 128); + expect([eq.width, eq.height]).toEqual([128, 64]); + let worst = 0; + for (let r = 0; r < eq.height; r++) + for (let c = 0; c < eq.width; c++) { + const lat = (0.5 - (r + 0.5) / 64) * Math.PI; + const phi = ((c + 0.5) / 128 - 0.5) * 2 * Math.PI; + const want = [-Math.cos(phi) * Math.cos(lat), Math.sin(lat), Math.sin(phi) * Math.cos(lat)]; + const i = (r * 128 + c) * 4; + for (let k = 0; k < 3; k++) worst = Math.max(worst, Math.abs(eq.data[i + k] - 1 - want[k])); + } + expect(worst).toBeLessThan(0.06); + }); + it('the cross centre (+Z) is in front of the default camera direction after the flip rule: three looks down -Z, so +Z is behind it and the up face is up', () => { + const eq = cubeToEquirect(paintedFaces(8), 64); + // top row = straight up: +Y face (green channel ~2) + expect(eq.data[(0 * 64 + 10) * 4 + 1]).toBeGreaterThan(1.9); + }); + it('the width follows the face size and is capped', () => { + expect(equirectWidthFor(256)).toBe(1024); + expect(equirectWidthFor(2048)).toBe(2048); + const big = { data: new Float32Array(4096 * 2048 * 4).fill(0.5), width: 4096, height: 2048 }; + const fit = fitEquirect(big); + expect([fit.width, fit.height]).toEqual([2048, 1024]); + expect(fit.data[100]).toBeCloseTo(0.5); + }); +}); + +describe('encodeRGBE', () => { + it('three\'s HDRLoader reads it back (values within RGBE precision)', () => { + const W = 16; + const H = 8; + const data = new Float32Array(W * H * 4); + for (let i = 0; i < W * H; i++) data.set([i / 10, 1, 0.01 * i, 1], i * 4); + const bytes = encodeRGBE({ data, width: W, height: H }); + const t = /** @type {any} */ (new HDRLoader().setDataType(THREE.FloatType).parse(/** @type {ArrayBuffer} */ (bytes.buffer))); + expect([t.width, t.height]).toEqual([W, H]); + for (let i = 0; i < W * H; i++) + for (let k = 0; k < 3; k++) { + const want = data[i * 4 + k]; + // RGBE shares one exponent per pixel: the error scales with the pixel's brightest channel + const peak = Math.max(data[i * 4], data[i * 4 + 1], data[i * 4 + 2]); + expect(Math.abs(t.data[i * 4 + k] - want)).toBeLessThanOrEqual(peak / 200); + } + }); +}); + +describe('separate face files', () => { + it('names say which face', () => { + expect(faceOfFileName('sky_px.hdr')).toBe('px'); + expect(faceOfFileName('Bridge2/negz.jpg')).toBe('nz'); + expect(faceOfFileName('skybox_right.png')).toBe('px'); + expect(faceOfFileName('cloudy-Back.jpg')).toBe('nz'); + expect(faceOfFileName('city_dn.png')).toBe('ny'); + expect(faceOfFileName('photo.jpg')).toBe(null); + expect(faceOfFileName('sky_-x.png')).toBe('nx'); + expect(faceOfFileName('sky_+x.png')).toBe('px'); + expect(faceOfFileName('posy.hdr')).toBe('py'); + expect(faceOfFileName('desktop.png')).toBe(null); + }); + it('six square, same-size, distinct faces or the reason', () => { + const f = paintedFaces(4); + const files = FACES.map((face) => ({ name: 'a_' + face + '.png', image: f[face] })); + expect('faces' in facesFromFiles(files)).toBe(true); + expect(facesFromFiles(files.slice(0, 5))).toHaveProperty('error'); + expect(facesFromFiles([...files.slice(0, 5), { ...files[5], name: 'a_px.png' }])).toHaveProperty('error'); + expect(facesFromFiles([...files.slice(0, 5), { name: 'a_nz.png', image: paintedFaces(8).nz }])).toHaveProperty('error'); + }); +}); diff --git a/tests/unit/effectActsOnNothing.test.js b/tests/unit/effectActsOnNothing.test.js new file mode 100644 index 000000000..2b0de96b2 --- /dev/null +++ b/tests/unit/effectActsOnNothing.test.js @@ -0,0 +1,65 @@ +// 102b-papercuts (scout row 18): an effect that reaches no object is no longer silently inert +import { describe, it, expect } from 'vitest'; +import { validateScene } from '../../src/lib/ai/runtimeCore.js'; +import { publishLiveStates, nodeLiveStates, liveStateGraph, clearLiveStates, INERT_LINE } from '../../src/lib/nodeLiveState.js'; +import { get } from 'svelte/store'; + +const EFFECTS = new Set(['spin', 'setcolor', 'script']); +const world = (/** @type {any} */ w) => ({ + nodes: [], + edges: [], + objects: { box: { name: 'Box' } }, + envLit: true, + isSource: () => false, + isAction: () => false, + inputsOf: () => [], + isObjectEffect: (/** @type {string} */ t) => EFFECTS.has(t), + ...w +}); +const codes = (/** @type {any} */ r) => r.issues.map((/** @type {any} */ i) => i.code); + +describe('validate_scene: effect-no-object', () => { + it('a scene-graph Spin with no selector is flagged (warning)', () => { + const r = validateScene(world({ nodes: [{ id: 's', type: 'spin', data: {} }] })); + expect(codes(r)).toContain('effect-no-object'); + expect(r.issues.find((/** @type {any} */ i) => i.code === 'effect-no-object').level).toBe('warning'); + }); + it('a selector with no object picked still acts on nothing', () => { + const r = validateScene(world({ nodes: [{ id: 's', type: 'spin' }, { id: 'o', type: 'objectselector', data: { selected: '' } }], edges: [{ source: 's', target: 'o' }] })); + expect(codes(r)).toContain('effect-no-object'); + }); + it('wired to a picked object, in an object graph, or with its own target: fine', () => { + expect(codes(validateScene(world({ nodes: [{ id: 's', type: 'spin' }, { id: 'o', type: 'objectselector', data: { selected: 'box' } }], edges: [{ source: 's', target: 'o' }] })))).not.toContain('effect-no-object'); + expect(codes(validateScene(world({ nodes: [{ id: 's', type: 'spin', __graph: 'box' }] })))).not.toContain('effect-no-object'); + expect(codes(validateScene(world({ nodes: [{ id: 's', type: 'setcolor', data: { target: 'box' } }] })))).not.toContain('effect-no-object'); + }); + it('a Script with outputs is a value, not an effect', () => { + expect(codes(validateScene(world({ nodes: [{ id: 's', type: 'script', data: { outputs: [{ name: 'n', type: 'number' }] } }] })))).not.toContain('effect-no-object'); + }); + it('a World without the probe (an older caller) is unchanged', () => { + const w = world({ nodes: [{ id: 's', type: 'spin' }] }); + delete w.isObjectEffect; + expect(codes(validateScene(w))).not.toContain('effect-no-object'); + }); +}); + +describe('the card line', () => { + it('an inert effect shows INERT_LINE ahead of its own reader', () => { + liveStateGraph.set('scene'); + clearLiveStates(); + publishLiveStates({ + nodes: [{ id: 'a', type: 'counter' }, { id: 'b', type: 'spin' }], + now: 1000, + time: 1, + motionTime: 1, + motionRests: false, + capturing: null, + value: () => 2, + pulseAt: () => null, + inert: (/** @type {any} */ n) => n.id === 'b' + }); + expect(get(nodeLiveStates)).toEqual({ a: 'count 2', b: INERT_LINE }); + expect(INERT_LINE.length).toBeLessThanOrEqual(40); // the card clips at 40 + liveStateGraph.set(null); + }); +}); diff --git a/tests/unit/fogModes.test.js b/tests/unit/fogModes.test.js new file mode 100644 index 000000000..798f66121 --- /dev/null +++ b/tests/unit/fogModes.test.js @@ -0,0 +1,89 @@ +// 102b-papercuts (scout row 18): real distance and height fog +import { describe, it, expect } from 'vitest'; +import * as THREE from 'three'; +import { fogKindOf, fogDensityOf, heightFogOf, heightFogParams } from '../../src/lib/fogModes.js'; +import { fogKindPatch, fogFactorAt } from '../../src/lib/fogEdit.js'; +import { HEIGHT_FOG, setHeightFog } from '../../src/lib/fogModes.js'; +import { installHeightFog } from '../../src/lib/fogShader.js'; +import { mergeFogPatch } from '../../src/lib/fogReach.js'; + +describe('fog kinds', () => { + it('a fog with no kind is the linear fog every older document has', () => { + const old = { color: '#fff', near: 1, far: 50 }; + expect(fogKindOf(old)).toBe('linear'); + expect(heightFogOf(old)).toBe(null); + expect(heightFogParams(old)).toEqual([0, 0, 0, 0]); + expect(heightFogParams(null)).toEqual([0, 0, 0, 0]); + expect(fogKindOf({ kind: 'nonsense' })).toBe('linear'); + }); + it('switching kind fills in what the new kind needs and keeps near / far', () => { + const fog = { color: '#fff', near: 1, far: 50 }; + const d = mergeFogPatch(fog, fogKindPatch(fog, 'distance')); + expect(d).toMatchObject({ kind: 'distance', density: 0.02, near: 1, far: 50 }); + const h = mergeFogPatch(d, fogKindPatch(d, 'height')); + expect(h).toMatchObject({ kind: 'height', density: 0.02, base: 0, thickness: 6, near: 1, far: 50 }); + expect(fogKindOf(mergeFogPatch(h, fogKindPatch(h, 'linear')))).toBe('linear'); + }); + it('values are clamped', () => { + expect(fogDensityOf({ density: -1 })).toBe(0.02); + expect(fogDensityOf({ density: 5 })).toBe(1); + expect(heightFogOf({ kind: 'height', base: 'x', thickness: 0 })).toEqual({ base: 0, thickness: 6 }); + expect(heightFogParams({ kind: 'height', base: 2, thickness: 4 })).toEqual([2, 0.25, 1, 0]); + }); +}); + +describe('fogFactorAt (the shader maths)', () => { + const ground = { kind: 'height', density: 0.1, base: 0, thickness: 2 }; + it('distance fog thickens with distance', () => { + const f = { kind: 'distance', density: 0.05 }; + expect(fogFactorAt(f, 10, 0, 0)).toBeLessThan(fogFactorAt(f, 40, 0, 0)); + expect(fogFactorAt(f, 0, 0, 0)).toBe(0); + }); + it('height fog: on the ground it fogs, high up the same distance barely does', () => { + const low = fogFactorAt(ground, 30, 0.5, 0.5); + const high = fogFactorAt(ground, 30, 20, 20); + expect(low).toBeGreaterThan(0.8); + expect(high).toBeLessThan(0.01); + }); + it('looking down into it fogs more than looking across above it', () => { + expect(fogFactorAt(ground, 30, 10, 0)).toBeGreaterThan(fogFactorAt(ground, 30, 10, 10)); + }); + it('linear fog is smoothstep(near, far), as three', () => { + expect(fogFactorAt({ near: 10, far: 20 }, 15, 0, 0)).toBeCloseTo(0.5); + }); +}); + +describe('the shader patch', () => { + it('three\'s fog chunks carry the layer; the stock branches are still there', () => { + expect(THREE.ShaderChunk.fog_fragment).not.toContain('fogHeight'); // nothing until a height fog asks + expect(installHeightFog()).toBe(true); + expect(installHeightFog()).toBe(false); // once + expect(THREE.ShaderChunk.fog_fragment).toContain('fogHeight.z > 0.5'); + expect(THREE.ShaderChunk.fog_fragment).toContain('smoothstep( fogNear, fogFar, vFogDepth )'); + expect(THREE.ShaderChunk.fog_vertex).toContain('vFogWorldY'); + expect(THREE.ShaderChunk.fog_pars_fragment).toContain('uniform vec4 fogHeight;'); + }); + it('every built-in material shares ONE uniform value (a clone passes it by reference)', () => { + const lib = /** @type {any} */ (THREE.ShaderLib); + expect(lib.standard.uniforms.fogHeight.value).toBe(HEIGHT_FOG); + expect(lib.physical.uniforms.fogHeight.value).toBe(HEIGHT_FOG); + expect(lib.basic.uniforms.fogHeight.value).toBe(HEIGHT_FOG); + expect(THREE.UniformsUtils.clone(lib.standard.uniforms).fogHeight.value).toBe(HEIGHT_FOG); + setHeightFog([1, 0.5, 1, 0]); + expect(Array.from(HEIGHT_FOG)).toEqual([1, 0.5, 1, 0]); + setHeightFog([0, 0, 0, 0]); + }); + it('the world height the vertex chunk reconstructs is right for a turned, moved camera', () => { + const cam = new THREE.PerspectiveCamera(); + cam.position.set(3, 7, -2); + cam.rotation.set(-0.4, 1.1, 0.2); + cam.updateMatrixWorld(); + const view = cam.matrixWorldInverse; + const p = new THREE.Vector3(5, 1.5, 8); + const mv = p.clone().applyMatrix4(view); + const e = view.elements; // column-major, as GLSL + const col1 = new THREE.Vector3(e[4], e[5], e[6]); + const t = new THREE.Vector3(e[12], e[13], e[14]); + expect(col1.dot(mv.clone().sub(t))).toBeCloseTo(1.5, 5); + }); +}); diff --git a/tests/unit/roundedBox.test.js b/tests/unit/roundedBox.test.js new file mode 100644 index 000000000..90a38207c --- /dev/null +++ b/tests/unit/roundedBox.test.js @@ -0,0 +1,128 @@ +// 102b-papercuts: a rounded box is a Box setting (two numbers in the file), a baked one is recognised +// (never converted by itself), and equal shapes are saved once. +import { describe, it, expect, beforeAll } from 'vitest'; +import * as THREE from 'three'; +import { RoundedBox, matchRoundedBox, bakedRoundedBox, restoreRoundedBoxes, roundedBoxVertexCount } from '../../src/lib/roundedBox.js'; +import { boxGeometryFor, loadRoundedBox, roundedBoxModule } from '../../src/lib/roundedBoxGate.js'; +import { shareGeometries, inflateSharedGeometries } from '../../src/lib/sharedGeometryWrite.js'; +import { geometrySpec } from '../../src/lib/geometryParams.js'; + +describe('rounded box: before its chunk', () => { + it('a radius builds the square box for that moment', () => { + expect(roundedBoxModule()).toBeNull(); + const g = boxGeometryFor({ width: 2, height: 1, depth: 1, radius: 0.2 }); + expect(g).not.toBeInstanceOf(RoundedBox); + expect(g.attributes.position.count).toBe(24); + }); +}); + +describe('rounded box', () => { + beforeAll(async () => { + await loadRoundedBox(); + }); + + it('radius 0 or absent is exactly the old BoxGeometry', () => { + const g = boxGeometryFor({ width: 2, height: 1, depth: 1, widthSegments: 1, heightSegments: 1, depthSegments: 1 }); + expect(g).toBeInstanceOf(THREE.BoxGeometry); + expect(g).not.toBeInstanceOf(RoundedBox); + const { uuid: _a, ...mine } = /** @type {any} */ (g.toJSON()); + const { uuid: _b, ...stock } = /** @type {any} */ (new THREE.BoxGeometry(2, 1, 1, 1, 1, 1).toJSON()); + expect(mine).toEqual(stock); + expect(Object.keys(g.parameters)).not.toContain('radius'); + expect(boxGeometryFor({ width: 1, height: 1, depth: 1, radius: 0 })).not.toBeInstanceOf(RoundedBox); + }); + + it('saves as a Box with two numbers and loads back rounded', () => { + const g = boxGeometryFor({ width: 2, height: 1, depth: 3, radius: 0.2, radiusSegments: 3 }); + const json = g.toJSON(); + expect(json.type).toBe('BoxGeometry'); + expect(json.data).toBeUndefined(); + expect(json).toMatchObject({ width: 2, height: 1, depth: 3, radius: 0.2, radiusSegments: 3 }); + expect(JSON.stringify(json).length).toBeLessThan(300); + const mesh = new THREE.Mesh(g, new THREE.MeshStandardMaterial()); + const back = /** @type {any} */ (new THREE.ObjectLoader().parse(mesh.toJSON())); + expect(back.geometry).toBeInstanceOf(RoundedBox); + expect(back.geometry.attributes.position.count).toBe(roundedBoxVertexCount(3)); + }); + + it('an older reader (stock BoxGeometry.fromJSON) gets a plain box of the same size', () => { + const json = /** @type {any} */ (boxGeometryFor({ width: 2, height: 1, depth: 3, radius: 0.2 }).toJSON()); + const stock = new THREE.BoxGeometry(json.width, json.height, json.depth, json.widthSegments, json.heightSegments, json.depthSegments); + stock.computeBoundingBox(); + expect(stock.boundingBox?.max.toArray()).toEqual([1, 0.5, 1.5]); + }); + + it('recognises a baked rounded box and nothing else', () => { + const baked = bakedRoundedBox({ width: 1.2, height: 0.8, depth: 0.6, radius: 0.08, radiusSegments: 2 }); + expect(/** @type {any} */ (baked).parameters).toBeUndefined(); + expect(matchRoundedBox(baked)).toEqual({ width: 1.2, height: 0.8, depth: 0.6, radius: 0.08, radiusSegments: 2 }); + // a moved one, an edited one, a sphere and a typed box are not offered + const moved = baked.clone().translate(0.5, 0, 0); + expect(matchRoundedBox(moved)).toBeNull(); + const dented = baked.clone(); + dented.attributes.position.array[4] += 0.05; + expect(matchRoundedBox(dented)).toBeNull(); + expect(matchRoundedBox(new THREE.SphereGeometry(1).toNonIndexed())).toBeNull(); + expect(matchRoundedBox(new THREE.BoxGeometry())).toBeNull(); + }); + + it('a box an older build wrote back square is rounded again from its userData', () => { + const mesh = new THREE.Mesh(new THREE.BoxGeometry(2, 1, 1), new THREE.MeshStandardMaterial()); + mesh.userData.geometryParams = { gtype: 'Box', params: { width: 2, height: 1, depth: 1, radius: 0.1, radiusSegments: 2 } }; + expect(restoreRoundedBoxes(mesh)).toBe(1); + expect(mesh.geometry).toBeInstanceOf(RoundedBox); + // a sculpted one keeps its mesh + const edited = new THREE.Mesh(new THREE.BoxGeometry(2, 1, 1)); + edited.userData = { faceEdited: true, geometryParams: { gtype: 'Box', params: { width: 2, height: 1, depth: 1, radius: 0.1 } } }; + expect(restoreRoundedBoxes(edited)).toBe(0); + }); + + it('the Box spec stores the radius rows only once used', () => { + const spec = /** @type {any} */ (geometrySpec('Box')); + const optional = spec.params.filter((/** @type {any} */ p) => p.optional).map((/** @type {any} */ p) => p.key); + expect(optional).toEqual(['radius', 'radiusSegments']); + expect(spec.params.find((/** @type {any} */ p) => p.key === 'radiusSegments').show({ radius: 0 })).toBe(false); + }); +}); + +describe('equal shapes saved once', () => { + /** @param {string} name @param {THREE.BufferGeometry} g */ + const el = (name, g) => { + const m = new THREE.Mesh(g, new THREE.MeshStandardMaterial()); + m.name = name; + return m.toJSON(); + }; + + it('a payload without repeats is returned unchanged (same object)', () => { + const payload = { objects: [el('a', new THREE.SphereGeometry(1).toNonIndexed()), el('b', new THREE.BoxGeometry())] }; + expect(shareGeometries(payload)).toBe(payload); + }); + + it('repeats go into one table entry and come back where they were used', () => { + const crate = bakedRoundedBox({ width: 1, height: 1, depth: 1, radius: 0.1, radiusSegments: 3 }); + const objects = [el('c1', crate.clone()), el('c2', crate.clone()), el('c3', crate.clone()), el('ball', new THREE.SphereGeometry(1, 8, 6).toNonIndexed())]; + const payload = { objects }; + const before = JSON.stringify(payload); + const shared = shareGeometries(payload); + expect(JSON.stringify(payload)).toBe(before); // the live payload is untouched + expect(shared.sharedGeometries).toHaveLength(1); + const stub = shared.objects[1].geometries[0]; + expect(stub).toMatchObject({ type: 'BoxGeometry', width: 1, height: 1, depth: 1, sharedGeometry: 0 }); + expect(shared.objects[3]).toBe(objects[3]); // the one-off sphere is written as before + expect(JSON.stringify(shared).length).toBeLessThan(before.length / 2); + const back = inflateSharedGeometries(JSON.parse(JSON.stringify(shared))); + expect(back.sharedGeometries).toBeUndefined(); + expect(JSON.stringify(back) === before).toBe(true); + // each mesh owns its copy + expect(back.objects[0].geometries[0].data).not.toBe(back.objects[1].geometries[0].data); + }); + + it('an older reader parses the stub as a plain box of the mesh size', () => { + const crate = bakedRoundedBox({ width: 2, height: 1, depth: 1, radius: 0.1, radiusSegments: 1 }); + const shared = shareGeometries({ objects: [el('a', crate.clone()), el('b', crate.clone())] }); + const stub = shared.objects[0].geometries[0]; + const old = new THREE.BoxGeometry(stub.width, stub.height, stub.depth); + old.computeBoundingBox(); + expect(old.boundingBox?.max.x).toBeCloseTo(1, 4); + }); +}); diff --git a/tests/unit/sceneTools85.test.js b/tests/unit/sceneTools85.test.js index aed043cc2..c5df7400d 100644 --- a/tests/unit/sceneTools85.test.js +++ b/tests/unit/sceneTools85.test.js @@ -92,6 +92,17 @@ describe('scene: sky', () => { expect(scene.planSky({ fog: { near: 10, far: 5 } }, NOW).error).toMatch(/far must be beyond near/); expect(scene.planSky({ fog: null }, NOW).state.customPreset.fog).toBe(null); }); + it('102b-papercuts: a density is real distance fog; base / thickness make ground fog; near / far stay for older builds', () => { + const d = scene.planSky({ fog: { color: '#ffffff', density: 0.05 } }, NOW).state.customPreset.fog; + expect(d).toMatchObject({ kind: 'distance', density: 0.05, near: 0, far: 40 }); + const h = scene.planSky({ fog: { color: '#ffffff', density: 0.1, base: 1, thickness: 3 } }, NOW).state.customPreset.fog; + expect(h).toMatchObject({ kind: 'height', density: 0.1, base: 1, thickness: 3 }); + const k = scene.planSky({ fog: { kind: 'height' } }, NOW).state.customPreset.fog; + expect(k).toMatchObject({ kind: 'height', density: 0.02, base: 0, thickness: 6 }); + expect(scene.planSky({ fog: { near: 2, far: 30 } }, NOW).state.customPreset.fog.kind).toBe('linear'); + expect(scene.planSky({ fog: { kind: 'volumetric' } }, NOW).error).toMatch(/fog.kind/); + expect(scene.planSky({ fog: { thickness: 0 } }, NOW).error).toMatch(/thickness/); + }); it('takes a bundled sky with quality, never a URL', () => { const r = scene.planSky({ hdri: { name: 'starlight', quality: 'high', rotation: 1 } }, NOW); expect(r.state.customPreset.hdri).toMatchObject({ src: 'bundled:starlight', quality: 'high', rotation: 1 }); diff --git a/tests/unit/scriptIO.test.js b/tests/unit/scriptIO.test.js index b3cb34c4c..c7dde9d91 100644 --- a/tests/unit/scriptIO.test.js +++ b/tests/unit/scriptIO.test.js @@ -42,8 +42,14 @@ describe('declarations', () => { { name: 'reach', type: 'number', value: 1.2 } ]); }); - it('outputs cannot be events (a pure function cannot mint a stamp)', () => { - expect(scriptOutputs({ outputs: [{ name: 'fire', type: 'event' }] })).toEqual([{ name: 'fire', type: 'number' }]); + it('102b-papercuts: an output may be an event (fired on the rising edge by the runtime)', () => { + expect(scriptOutputs({ outputs: [{ name: 'fire', type: 'event' }] })).toEqual([{ name: 'fire', type: 'event' }]); + }); + it('102b-papercuts: an event output left out of the return is quiet, not a problem', () => { + const out = harvestOutputs({ n: 1 }, [{ name: 'n', type: 'number' }, { name: 'fire', type: 'event' }]); + expect(out.problems).toEqual([]); + expect(out.value.__handles.fire).toBe(undefined); + expect(harvestOutputs({ fire: 1 }, [{ name: 'fire', type: 'event' }]).value.__handles.fire).toBe(true); }); it('only outputs make it a value node', () => { expect(isScriptValue({ inputs: [{ name: 'a', type: 'number' }] })).toBe(false); diff --git a/tests/unit/uniformWrite.test.js b/tests/unit/uniformWrite.test.js new file mode 100644 index 000000000..8a783908d --- /dev/null +++ b/tests/unit/uniformWrite.test.js @@ -0,0 +1,52 @@ +// 102b-papercuts (row 18): Set Shader Uniform drives colour and vector uniforms +import { describe, it, expect } from 'vitest'; +import { writeUniformSlot, hexToLinear } from '../../src/lib/uniformWrite.js'; + +describe('writeUniformSlot', () => { + it('a float slot takes the number, exactly as before', () => { + const slot = { value: 0 }; + expect(writeUniformSlot(slot, { value: 0.5 })).toBe('number'); + expect(slot.value).toBe(0.5); + expect(writeUniformSlot(slot, { value: 'x' })).toBe(null); + expect(slot.value).toBe(0.5); + }); + it('a vec3 slot takes a colour in LINEAR space, in place', () => { + const arr = [0, 0, 0]; + const slot = { value: arr }; + expect(writeUniformSlot(slot, { value: 0, color: '#ff8000' })).toBe('color'); + expect(slot.value).toBe(arr); // same array three uploads from + expect(arr[0]).toBeCloseTo(1); + expect(arr[1]).toBeCloseTo(/** @type {number[]} */ (hexToLinear('#008000'))[1]); + expect(arr[1]).toBeLessThan(0.5); // linear, not 128/255 + expect(arr[2]).toBe(0); + }); + it('a vector wins over a colour; a vec4 keeps its own w', () => { + const slot = { value: [0, 0, 0, 0.25] }; + expect(writeUniformSlot(slot, { color: '#ffffff', vector: [1, 2, 3] })).toBe('vector'); + expect(slot.value).toEqual([1, 2, 3, 0.25]); + }); + it('a vec2 takes the first two components', () => { + const slot = { value: [0, 0] }; + writeUniformSlot(slot, { vector: [4, 5, 6] }); + expect(slot.value).toEqual([4, 5]); + }); + it('a number alone fills every component of a vector slot (vec4 keeps alpha)', () => { + const slot = { value: [0, 0, 0, 1] }; + expect(writeUniformSlot(slot, { value: 0.3 })).toBe('number'); + expect(slot.value).toEqual([0.3, 0.3, 0.3, 1]); + }); + it('nothing readable writes nothing', () => { + const slot = { value: [1, 1, 1] }; + expect(writeUniformSlot(slot, { value: NaN, color: 'red', vector: ['a'] })).toBe(null); + expect(slot.value).toEqual([1, 1, 1]); + expect(writeUniformSlot(null, { value: 1 })).toBe(null); + }); + it('THREE-style objects are written by component', () => { + const v = { x: 0, y: 0, z: 0, fromArray() {} }; + writeUniformSlot({ value: v }, { vector: { x: 1, y: 2, z: 3 } }); + expect([v.x, v.y, v.z]).toEqual([1, 2, 3]); + const c = { r: 0, g: 0, b: 0, fromArray() {} }; + writeUniformSlot({ value: c }, { color: '#ffffff' }); + expect(c.g).toBeCloseTo(1); + }); +}); diff --git a/tools/agent/catalog.json b/tools/agent/catalog.json index 55d3d19de..6172e137c 100644 --- a/tools/agent/catalog.json +++ b/tools/agent/catalog.json @@ -213,7 +213,7 @@ "items": { "type": "number" }, - "description": "up to 4 constructor args. Box=[w,h,d], Sphere=[radius], Cylinder=[rTop,rBottom,height], Cone=[radius,height], Plane=[w,h], Torus=[radius,tube]. Omit for sensible defaults. Sizes go down to 0.001 m." + "description": "up to 4 constructor args. Box=[w,h,d], Sphere=[radius], Cylinder=[rTop,rBottom,height], Cone=[radius,height], Plane=[w,h], Torus=[radius,tube]. Omit for sensible defaults. For a rounded box, create a Box and then set_geometry {radius}. Sizes go down to 0.001 m." }, "name": { "type": "string" @@ -746,7 +746,7 @@ { "name": "set_geometry", "title": "Reshape objects", - "description": "Reshape existing objects in place (they keep uuid, transform, material and flows): change a primitive's params (the keys list_scene shows under geometry, e.g. {width, height, depth} for a Box, {radiusTop, radiusBottom, height, radialSegments} for a Cylinder, {segments, phiLength} for a Lathe, {depth, bevel} for an extrusion), or its TYPE (box → Cylinder) with params on top of the new type's defaults. A tube (create_tube) takes path, radius / radii, closed. A 3D text (create_text) takes its text fields as params (text, font, size, depth, bevel, align, maxWidth, anchor, color …). An object whose vertices were hand-edited refuses unless force: true (the rebuild replaces those edits). Up to 100 objects per call, one undo step.\nTake a screenshot after each change to check your work.", + "description": "Reshape existing objects in place (they keep uuid, transform, material and flows): change a primitive's params (the keys list_scene shows under geometry, e.g. {width, height, depth} for a Box (plus {radius, radiusSegments} to round its edges and corners: a rounded box, saved as two numbers), {radiusTop, radiusBottom, height, radialSegments} for a Cylinder, {segments, phiLength} for a Lathe, {depth, bevel} for an extrusion), or its TYPE (box → Cylinder) with params on top of the new type's defaults. A tube (create_tube) takes path, radius / radii, closed. A 3D text (create_text) takes its text fields as params (text, font, size, depth, bevel, align, maxWidth, anchor, color …). An object whose vertices were hand-edited refuses unless force: true (the rebuild replaces those edits). Up to 100 objects per call, one undo step.\nTake a screenshot after each change to check your work.", "inputSchema": { "type": "object", "properties": { @@ -2243,7 +2243,7 @@ { "name": "create_shader", "title": "Give objects a shader", - "description": "Give objects a node shader, written as text like flow graphs: one line per node (id = type {param: value}), one per edge (source[.output] -> target.input), ending at a \"surface\" node. Example: c = color {value: \"#ff8800\"}\\nout = surface\\nc -> out.emissive. Call describe_shader_nodes for the types. Each object gets its own copy. Compiler errors come back in the result and nothing is written. Returns the graph key and its uniforms: a float one (u__) can be driven by a flow \"setuniform\" node on that object. scene: true makes the default for every object, which asks the host first.\nTake a screenshot after each change to check your work.", + "description": "Give objects a node shader, written as text like flow graphs: one line per node (id = type {param: value}), one per edge (source[.output] -> target.input), ending at a \"surface\" node. Example: c = color {value: \"#ff8800\"}\\nout = surface\\nc -> out.emissive. Call describe_shader_nodes for the types. Each object gets its own copy. Compiler errors come back in the result and nothing is written. Returns the graph key and its uniforms (u__): a flow \"setuniform\" node on that object drives a float, colour or vector one. scene: true makes the default for every object, which asks the host first.\nTake a screenshot after each change to check your work.", "inputSchema": { "type": "object", "properties": { @@ -3155,7 +3155,7 @@ { "name": "set_sky", "title": "Change the sky and fog", - "description": "The sky and the air: a look preset (studio, daylight, sunset, night, classic, clearsky, meadow, sunrise, starlight, photostudio, product, cinematic, stylised), a sky image by name (clearsky, meadow, sunrise, starlight, photostudio; a search_assets HDRI id \"cc0:polyhaven/\" (fetched into the library once) or \"lib:\"; or {hash} of an HDR in the library — upload your own .hdr / .exr with assets upload; never a URL) with rotation, intensity, blur and Sky quality (auto / standard / high / max, plus a minimum), a background colour or {top, bottom} gradient, fog ({color, near, far} in metres, or {color, density}; null = none) and exposure. \"Make it night\": preset night or hdri starlight + a dark fog. Changes the whole scene's look, so the host is asked first: it answers needsConfirm, then repeat with confirmId. One undo step.\nTake a screenshot after each change to check your work.", + "description": "The sky and the air: a look preset (studio, daylight, sunset, night, classic, clearsky, meadow, sunrise, starlight, photostudio, product, cinematic, stylised), a sky image by name (clearsky, meadow, sunrise, starlight, photostudio; a search_assets HDRI id \"cc0:polyhaven/\" (fetched into the library once) or \"lib:\"; or {hash} of an HDR in the library — upload your own .hdr / .exr with assets upload; never a URL) with rotation, intensity, blur and Sky quality (auto / standard / high / max, plus a minimum), a background colour or {top, bottom} gradient, fog ({color, near, far} in metres; {color, density} for distance fog; {color, density, base, thickness} for ground fog lying from base up over thickness metres; null = none) and exposure. \"Make it night\": preset night or hdri starlight + a dark fog. Changes the whole scene's look, so the host is asked first: it answers needsConfirm, then repeat with confirmId. One undo step.\nTake a screenshot after each change to check your work.", "inputSchema": { "type": "object", "properties": { @@ -3236,6 +3236,15 @@ "null" ], "properties": { + "kind": { + "type": "string", + "enum": [ + "linear", + "distance", + "height" + ], + "description": "linear = near to far; distance = thickens with distance (density); height = ground fog (density, base, thickness)" + }, "color": { "type": "string" }, @@ -3250,6 +3259,15 @@ "density": { "type": "number", "exclusiveMinimum": 0 + }, + "base": { + "type": "number", + "description": "ground fog: the height (m) where it is thickest" + }, + "thickness": { + "type": "number", + "exclusiveMinimum": 0, + "description": "ground fog: how far up (m) it thins out" } } }, @@ -4897,7 +4915,7 @@ { "name": "validate_scene", "title": "Check the game for problems", - "description": "Check the scene for what would not work when someone presses Play: actions nothing fires, an Object Selector wired the wrong way round, a scene-graph On Click that reaches no object, Game Start wired as an event, HUD nodes pointing at elements that do not exist, walk mode with no spawn, a scene with no light, behaviour code problems. Each issue says what to fix. Run it after building a game, then playtest.", + "description": "Check the scene for what would not work when someone presses Play: actions nothing fires, effects that act on no object, an Object Selector wired the wrong way round, a scene-graph On Click that reaches no object, Game Start wired as an event, HUD nodes pointing at elements that do not exist, walk mode with no spawn, a scene with no light, behaviour code problems. Each issue says what to fix. Run it after building a game, then playtest.", "inputSchema": { "type": "object", "properties": {} @@ -5708,7 +5726,7 @@ { "name": "upload_asset", "title": "Upload a sound or an HDR sky", - "description": "Put a SOUND or an HDR SKY you hold as bytes into the library and get its hash: your own generated music or ambience, or a file you fetched yourself (any site — the bytes come from you, so nothing needs the site to allow cross-origin reads). data = base64; a big file goes as chunk: {id, index, total} parts (each a multiple of 4 base64 characters). Sounds: WAV, OGG, MP3, FLAC, M4A or WebM up to 16 MB (decoded once to prove it is audio) → place it with effects sound {asset: hash} or music {asset: hash}. Skies: Radiance .hdr or OpenEXR up to 25 MB → set_look sky {hdri: {hash}}. The type comes from the bytes, never the name. Images go through image instead. Everyone in the session can pull it by hash.\nTake a screenshot after each change to check your work.", + "description": "Put a SOUND or an HDR SKY you hold as bytes into the library and get its hash: your own generated music or ambience, or a file you fetched yourself (any site — the bytes come from you, so nothing needs the site to allow cross-origin reads). data = base64; a big file goes as chunk: {id, index, total} parts (each a multiple of 4 base64 characters). Sounds: WAV, OGG, MP3, FLAC, M4A or WebM up to 16 MB (decoded once to prove it is audio) → place it with effects sound {asset: hash} or music {asset: hash}. Skies: Radiance .hdr or OpenEXR up to 25 MB, equirectangular (2:1) or a cube map as a cross (4:3, 3:4) or a strip of six faces (6:1, 1:6), which is converted → set_look sky {hdri: {hash}}. The type comes from the bytes, never the name. Images go through image instead. Everyone in the session can pull it by hash.\nTake a screenshot after each change to check your work.", "inputSchema": { "type": "object", "properties": { @@ -6484,7 +6502,7 @@ "items": { "type": "number" }, - "description": "up to 4 constructor args. Box=[w,h,d], Sphere=[radius], Cylinder=[rTop,rBottom,height], Cone=[radius,height], Plane=[w,h], Torus=[radius,tube]. Omit for sensible defaults. Sizes go down to 0.001 m." + "description": "up to 4 constructor args. Box=[w,h,d], Sphere=[radius], Cylinder=[rTop,rBottom,height], Cone=[radius,height], Plane=[w,h], Torus=[radius,tube]. Omit for sensible defaults. For a rounded box, create a Box and then set_geometry {radius}. Sizes go down to 0.001 m." }, "name": { "type": "string" @@ -7972,7 +7990,7 @@ "required": [ "updates" ], - "description": "Reshape existing objects in place (they keep uuid, transform, material and flows): change a primitive's params (the keys list_scene shows under geometry, e.g. {width, height, depth} for a Box, {radiusTop, radiusBottom, height, radialSegments} for a Cylinder, {segments, phiLength} for a Lathe, {depth, bevel} for an extrusion), or its TYPE (box → Cylinder) with params on top of the new type's defaults. A tube (create_shape {action: 'tube'}) takes path, radius / radii, closed. A 3D text (create_shape {action: 'text'}) takes its text fields as params (text, font, size, depth, bevel, align, maxWidth, anchor, color …). An object whose vertices were hand-edited refuses unless force: true (the rebuild replaces those edits). Up to 100 objects per call, one undo step." + "description": "Reshape existing objects in place (they keep uuid, transform, material and flows): change a primitive's params (the keys list_scene shows under geometry, e.g. {width, height, depth} for a Box (plus {radius, radiusSegments} to round its edges and corners: a rounded box, saved as two numbers), {radiusTop, radiusBottom, height, radialSegments} for a Cylinder, {segments, phiLength} for a Lathe, {depth, bevel} for an extrusion), or its TYPE (box → Cylinder) with params on top of the new type's defaults. A tube (create_shape {action: 'tube'}) takes path, radius / radii, closed. A 3D text (create_shape {action: 'text'}) takes its text fields as params (text, font, size, depth, bevel, align, maxWidth, anchor, color …). An object whose vertices were hand-edited refuses unless force: true (the rebuild replaces those edits). Up to 100 objects per call, one undo step." }, "mesh": { "type": "object", @@ -8642,7 +8660,7 @@ "description": "with confirmId: seconds to wait for the answer (up to 60)" } }, - "description": "Give objects a node shader, written as text like flow graphs: one line per node (id = type {param: value}), one per edge (source[.output] -> target.input), ending at a \"surface\" node. Example: c = color {value: \"#ff8800\"}\\nout = surface\\nc -> out.emissive. Call set_look {action: 'shader_nodes'} for the types. Each object gets its own copy. Compiler errors come back in the result and nothing is written. Returns the graph key and its uniforms: a float one (u__) can be driven by a flow \"setuniform\" node on that object. scene: true makes the default for every object, which asks the host first." + "description": "Give objects a node shader, written as text like flow graphs: one line per node (id = type {param: value}), one per edge (source[.output] -> target.input), ending at a \"surface\" node. Example: c = color {value: \"#ff8800\"}\\nout = surface\\nc -> out.emissive. Call set_look {action: 'shader_nodes'} for the types. Each object gets its own copy. Compiler errors come back in the result and nothing is written. Returns the graph key and its uniforms (u__): a flow \"setuniform\" node on that object drives a float, colour or vector one. scene: true makes the default for every object, which asks the host first." }, "edit_shader": { "type": "object", @@ -9006,6 +9024,15 @@ "null" ], "properties": { + "kind": { + "type": "string", + "enum": [ + "linear", + "distance", + "height" + ], + "description": "linear = near to far; distance = thickens with distance (density); height = ground fog (density, base, thickness)" + }, "color": { "type": "string" }, @@ -9020,6 +9047,15 @@ "density": { "type": "number", "exclusiveMinimum": 0 + }, + "base": { + "type": "number", + "description": "ground fog: the height (m) where it is thickest" + }, + "thickness": { + "type": "number", + "exclusiveMinimum": 0, + "description": "ground fog: how far up (m) it thins out" } } }, @@ -9037,7 +9073,7 @@ "description": "with confirmId: seconds to wait for the answer (up to 60)" } }, - "description": "The sky and the air: a look preset (studio, daylight, sunset, night, classic, clearsky, meadow, sunrise, starlight, photostudio, product, cinematic, stylised), a sky image by name (clearsky, meadow, sunrise, starlight, photostudio; a assets {action: 'search'} HDRI id \"cc0:polyhaven/\" (fetched into the library once) or \"lib:\"; or {hash} of an HDR in the library — upload your own .hdr / .exr with assets upload; never a URL) with rotation, intensity, blur and Sky quality (auto / standard / high / max, plus a minimum), a background colour or {top, bottom} gradient, fog ({color, near, far} in metres, or {color, density}; null = none) and exposure. \"Make it night\": preset night or hdri starlight + a dark fog. Changes the whole scene's look, so the host is asked first: it answers needsConfirm, then repeat with confirmId. One undo step." + "description": "The sky and the air: a look preset (studio, daylight, sunset, night, classic, clearsky, meadow, sunrise, starlight, photostudio, product, cinematic, stylised), a sky image by name (clearsky, meadow, sunrise, starlight, photostudio; a assets {action: 'search'} HDRI id \"cc0:polyhaven/\" (fetched into the library once) or \"lib:\"; or {hash} of an HDR in the library — upload your own .hdr / .exr with assets upload; never a URL) with rotation, intensity, blur and Sky quality (auto / standard / high / max, plus a minimum), a background colour or {top, bottom} gradient, fog ({color, near, far} in metres; {color, density} for distance fog; {color, density, base, thickness} for ground fog lying from base up over thickness metres; null = none) and exposure. \"Make it night\": preset night or hdri starlight + a dark fog. Changes the whole scene's look, so the host is asked first: it answers needsConfirm, then repeat with confirmId. One undo step." }, "light": { "type": "object", @@ -9322,7 +9358,7 @@ "validate": { "type": "object", "properties": {}, - "description": "Check the scene for what would not work when someone presses Play: actions nothing fires, an Object Selector wired the wrong way round, a scene-graph On Click that reaches no object, Game Start wired as an event, HUD nodes pointing at elements that do not exist, walk mode with no spawn, a scene with no light, behaviour code problems. Each issue says what to fix. Run it after building a game, then playtest." + "description": "Check the scene for what would not work when someone presses Play: actions nothing fires, effects that act on no object, an Object Selector wired the wrong way round, a scene-graph On Click that reaches no object, Game Start wired as an event, HUD nodes pointing at elements that do not exist, walk mode with no spawn, a scene with no light, behaviour code problems. Each issue says what to fix. Run it after building a game, then playtest." }, "logs": { "type": "object", @@ -9688,7 +9724,7 @@ "required": [ "data" ], - "description": "Put a SOUND or an HDR SKY you hold as bytes into the library and get its hash: your own generated music or ambience, or a file you fetched yourself (any site — the bytes come from you, so nothing needs the site to allow cross-origin reads). data = base64; a big file goes as chunk: {id, index, total} parts (each a multiple of 4 base64 characters). Sounds: WAV, OGG, MP3, FLAC, M4A or WebM up to 16 MB (decoded once to prove it is audio) → place it with effects sound {asset: hash} or music {asset: hash}. Skies: Radiance .hdr or OpenEXR up to 25 MB → set_look sky {hdri: {hash}}. The type comes from the bytes, never the name. Images go through image instead. Everyone in the session can pull it by hash." + "description": "Put a SOUND or an HDR SKY you hold as bytes into the library and get its hash: your own generated music or ambience, or a file you fetched yourself (any site — the bytes come from you, so nothing needs the site to allow cross-origin reads). data = base64; a big file goes as chunk: {id, index, total} parts (each a multiple of 4 base64 characters). Sounds: WAV, OGG, MP3, FLAC, M4A or WebM up to 16 MB (decoded once to prove it is audio) → place it with effects sound {asset: hash} or music {asset: hash}. Skies: Radiance .hdr or OpenEXR up to 25 MB, equirectangular (2:1) or a cube map as a cross (4:3, 3:4) or a strip of six faces (6:1, 1:6), which is converted → set_look sky {hdri: {hash}}. The type comes from the bytes, never the name. Images go through image instead. Everyone in the session can pull it by hash." }, "fonts": { "type": "object",