/** * Captures every OrbitControls-like instance (anything that assigns `enableDamping` in its constructor) so the * harness can move the page's own camera without the controls snapping it back. Same Object.prototype-setter trick * as the scene hook in browser.ts; installed as an extra init script by the viewer route only. */ export declare const CONTROLS_HOOK = "(() => {\n if (globalThis.__vqvControls) return;\n const list = []; globalThis.__vqvControls = list;\n try {\n Object.defineProperty(Object.prototype, 'enableDamping', {\n configurable: true, enumerable: false,\n set(v) {\n try { Object.defineProperty(this, 'enableDamping', { value: v, writable: true, configurable: true, enumerable: true }); } catch (e) {}\n if (this && !list.includes(this)) list.push(this);\n },\n get() { return undefined; },\n });\n } catch (e) {}\n})();"; /** * Installs `globalThis.__vqv` in the frame. Requires INSTALL_SCENE_STATS (commonAsserts.ts) to be installed first. * Every position it returns is WORLD space, rounded to 0.1 mm. */ export declare const INSTALL_VIEWER_PAGE = "globalThis.__vqv = (function () {\n const r4 = (v) => Math.round(v * 10000) / 10000;\n const S = { before: null, beforeMeshes: null, host: null, controls: null, pinned: null };\n const scene = () => { const draw = activeDraw(); return (draw && draw.scene) || (globalThis.__vqa && globalThis.__vqa.scene) || null; };\n\n /**\n * HOLD THE AIMED POSE. A single camera.position/lookAt write is not enough: the surface's own render loop drives its\n * OrbitControls every frame (controls.update() recomputes position from ITS target and spherical), and a re-fit on a\n * layout edge writes camera.position outright, so the aimed pose is gone before the screenshot and every fitted frame\n * renders the surface's default view \u2014 a rear-mounted part is then never in frame. Re-apply the pose from a wrapper\n * around camera.updateMatrixWorld, which three.js calls during every render (after the surface's own writes), so the\n * drawn frame is the aimed one. The same pin the whole-vehicle route uses (vehicleProbe `setCam`).\n *\n * Each camera retains only its own current pose. Update the entry before aim's matrix update, or that update\n * would restore the previous shot. Never lend one camera's pose to another captured camera.\n */\n function pinCam(cam, pose) {\n if (!S.pinned) S.pinned = new Map();\n const existing = S.pinned.get(cam);\n if (existing) { existing.pose = pose; return; }\n const entry = { orig: cam.updateMatrixWorld, pose };\n S.pinned.set(cam, entry);\n cam.updateMatrixWorld = function (force) {\n const p = entry.pose;\n cam.position.set(p.pos[0], p.pos[1], p.pos[2]);\n cam.up.set(p.up[0], p.up[1], p.up[2]);\n cam.lookAt(p.target[0], p.target[1], p.target[2]);\n return entry.orig.call(cam, force);\n };\n }\n /** Restore exactly the method each camera had before capture. */\n function releaseCam() {\n if (S.pinned) { for (const [cam, entry] of S.pinned) cam.updateMatrixWorld = entry.orig; S.pinned = null; }\n return true;\n }\n const meshesOf = (root) => { const out = []; if (root) root.traverse((o) => { if (o.isMesh && !o.isBatchedMesh && o.geometry) out.push(o); }); return out; };\n const chainVisible = (o) => { for (let p = o; p; p = p.parent) if (p.visible === false) return false; return true; };\n const fakeRoot = (list) => ({ updateMatrixWorld() {}, traverse(cb) { for (const m of list) cb(m); } });\n const statsOf = (list) => { const s = scene(); if (s) s.updateMatrixWorld(true); return globalThis.__vqaStats(fakeRoot(list)); };\n const worldPos = (o) => { const e = o.matrixWorld.elements; return [r4(e[12]), r4(e[13]), r4(e[14])]; };\n\n /**\n * The viewer owns a scene-level Group containing its loaded model; the customiser adds the GLTF Group directly.\n * Studio lights and the contact-shadow/floor meshes are siblings of that container, never its descendants.\n * Select that ownership boundary by object identity/type, not authored names: a studio lamp, floor rug, or\n * model named \"studio-contact-shadow\" is still the subject. Missing containers fail closed.\n */\n function hostRoot() {\n const s = scene(); if (!s) return null;\n let host = null;\n for (const c of s.children) {\n if (!c.isGroup && !c.isScene) continue;\n if (!meshesOf(c).length) continue;\n // A different viewer layout needs an explicit root contract; never guess among multiple model containers.\n if (host) return null;\n host = c;\n }\n return host;\n }\n\n /** Remember every object in the scene now (before the part is fitted). */\n function snapshot() {\n const s = scene(); if (!s) return null;\n s.updateMatrixWorld(true);\n S.before = new Set(); S.beforeMeshes = [];\n s.traverse((o) => { S.before.add(o); if (o.isMesh && o.geometry) S.beforeMeshes.push(o); });\n S.host = hostRoot();\n return { objects: S.before.size, meshes: S.beforeMeshes.length, host: S.host ? (S.host.name || S.host.type) : null };\n }\n\n /** Meshes that did not exist at snapshot time and are drawn (visible chain). */\n function newMeshes() {\n const s = scene(); if (!s || !S.before) return [];\n const out = [];\n // A `helix:vehicle-batch:*` BatchedMesh (re)built after the fit packs every part in its LOCAL frame: it is never\n // the part. The parts it draws stay in the graph as proxies with their real transforms, and those are measured.\n if (S.host) S.host.traverse((o) => { if (o.isMesh && !o.isBatchedMesh && o.geometry && !S.before.has(o) && chainVisible(o)) out.push(o); });\n return out;\n }\n function newMeshCount() { return newMeshes().length; }\n\n /** Group new meshes into fitted instances: one group per top-most newly-added ancestor. */\n function instances() {\n const groups = new Map();\n for (const m of newMeshes()) {\n let top = m;\n while (top.parent && !S.before.has(top.parent)) top = top.parent;\n if (!groups.has(top)) groups.set(top, []);\n groups.get(top).push(m);\n }\n const out = [];\n for (const [top, list] of groups) {\n let socket = null; const chain = [];\n for (let p = list[0]; p; p = p.parent) { chain.push(p.name || p.type); if (!socket && /^socket:/.test(p.name || '')) socket = p.name.slice(7); }\n const st = statsOf(list);\n out.push({ key: top.name || top.type, socket, attachedTo: top.parent ? (top.parent.name || top.parent.type) : null,\n chain: chain.slice(0, 7).join(' < '), meshes: list.length, triangles: st.triangles, bounds: st.bounds, origin: worldPos(top) });\n }\n return out;\n }\n\n /** SceneStats over every new mesh (the fitted part as drawn). */\n function partStats() { return statsOf(newMeshes()); }\n /** SceneStats of the subject (host / base) as drawn at snapshot time. */\n function hostStats() { return S.host ? statsOf(meshesOf(S.host).filter(chainVisible)) : null; }\n /** SceneStats over the drawn meshes owned by the model container (prop route: the item is the whole subject). */\n function subjectStats() { const h = hostRoot(); return h ? statsOf(meshesOf(h).filter(chainVisible)) : null; }\n\n /** Per-corner drawn wheel geometry of the host (rim/tyre/wheel nodes), measured at snapshot time. */\n function hostWheels() {\n const h = S.host; if (!h) return [];\n const corners = new Map(); const norm = { fl: 'lf', fr: 'rf', rl: 'lr', bl: 'lr', br: 'rr' };\n h.traverse((o) => {\n const m = /^(rim|rims|tyre|tire|tyres|tires|wheel|whl|hub)[_\\-. ]?(lf|rf|lr|rr|fl|fr|rl|bl|br)(?![a-z])/i.exec(o.name || '');\n if (!m) return;\n const c = norm[m[2].toLowerCase()] || m[2].toLowerCase();\n if (!corners.has(c)) corners.set(c, { nodes: new Set(), meshes: new Set() });\n corners.get(c).nodes.add(o.name);\n for (const mm of meshesOf(o)) if (chainVisible(mm)) corners.get(c).meshes.add(mm);\n });\n const out = [];\n for (const [corner, v] of corners) {\n const st = statsOf([...v.meshes]);\n out.push({ corner, nodes: [...v.nodes].sort(), meshes: v.meshes.size, bounds: st.bounds });\n }\n return out;\n }\n\n /** Host frame facts: origin (vehicle_root if present, else the subject root), scale, quaternion. */\n function hostFrame() {\n const h = S.host || hostRoot(); if (!h) return null;\n h.updateMatrixWorld(true);\n let vr = null; h.traverse((o) => { if (!vr && /^vehicle_root$/i.test(o.name || '')) vr = o; });\n const e = (vr || h).matrixWorld.elements;\n const sc = Math.hypot(e[0], e[1], e[2]);\n return { node: (vr || h).name || (vr || h).type, origin: [r4(e[12]), r4(e[13]), r4(e[14])], scale: r4(sc),\n xAxis: [r4(e[0] / sc), r4(e[1] / sc), r4(e[2] / sc)], zAxis: [r4(e[8] / sc), r4(e[9] / sc), r4(e[10] / sc)] };\n }\n\n /**\n * The presentation transform a viewer put on the model: walking down from the subject root through single-child\n * groups, the first node carrying a non-unit uniform scale (the item viewer's fitModelToStage scales the model to a\n * 2.15 m stage). Bounds divided by it are the item's authored size.\n */\n function presentationScale() {\n let n = hostRoot(); if (!n) return { scale: 1, origin: [0, 0, 0], node: null };\n n.updateMatrixWorld(true);\n for (let depth = 0; n && depth < 6; depth++) {\n const sc = n.scale;\n if (Math.abs(sc.x - 1) > 1e-4 && Math.abs(sc.x - sc.y) < 1e-6 && Math.abs(sc.x - sc.z) < 1e-6) {\n const e = n.matrixWorld.elements;\n return { scale: r4(Math.hypot(e[0], e[1], e[2])), origin: [r4(e[12]), r4(e[13]), r4(e[14])], node: n.name || n.type, depth };\n }\n const kids = n.children.filter((c) => !c.isLight);\n if (kids.length !== 1 || n.isMesh) break;\n n = kids[0];\n }\n return { scale: 1, origin: [0, 0, 0], node: null };\n }\n\n /** World position of named host nodes (first match per name). */\n function nodePositions(names) {\n const s = scene(); const out = {}; if (!s) return out;\n s.updateMatrixWorld(true);\n s.traverse((o) => { if (names.includes(o.name) && !out[o.name]) { const st = statsOf(meshesOf(o).filter(chainVisible)); out[o.name] = { origin: worldPos(o), drawn: st.bounds }; } });\n return out;\n }\n\n /** Bones of the base (any skinned mesh in the snapshot subject), world positions. */\n function bones() {\n const out = {}; const s = scene(); if (!s) return out;\n s.updateMatrixWorld(true);\n const root = S.host || hostRoot(); if (!root) return out;\n root.traverse((o) => { if (o.isBone && !(o.name in out)) out[o.name] = worldPos(o); });\n return out;\n }\n\n /**\n * Evenly sampled WORLD vertices of a mesh list (skinning applied), flat [x,y,z,...], at most maxPoints. Only\n * index-referenced vertices inside the draw range count: a POSITION buffer can carry vertices nothing draws.\n */\n function sample(list, maxPoints) {\n const s = scene(); if (s) s.updateMatrixWorld(true);\n const refs = list.map((m) => {\n const g = m.geometry; const pos = g.attributes.position; if (!pos) return null;\n const idx = g.index ? g.index.array : null; const n = idx ? idx.length : pos.count;\n const start = (g.drawRange && g.drawRange.start) || 0;\n const count = Math.min(n - start, (g.drawRange && Number.isFinite(g.drawRange.count)) ? g.drawRange.count : n);\n let used;\n if (idx) { used = new Set(); for (let k = start; k < start + count; k++) used.add(idx[k]); used = [...used]; }\n else { used = []; for (let k = start; k < start + count; k++) used.push(k); }\n return { m, pos, used };\n }).filter(Boolean);\n let total = 0; for (const r of refs) total += r.used.length;\n const step = Math.max(1, Math.ceil(total / maxPoints));\n const out = [];\n for (const { m, pos, used } of refs) {\n const e = m.matrixWorld.elements;\n const skinned = m.isSkinnedMesh && typeof m.getVertexPosition === 'function';\n const tmp = skinned ? new (m.position.constructor)() : null;\n for (let j = 0; j < used.length; j += step) {\n const i = used[j];\n let x, y, z;\n if (skinned) { m.getVertexPosition(i, tmp); x = tmp.x; y = tmp.y; z = tmp.z; } else { x = pos.getX(i); y = pos.getY(i); z = pos.getZ(i); }\n if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z)) continue;\n out.push(r4(e[0] * x + e[4] * y + e[8] * z + e[12]), r4(e[1] * x + e[5] * y + e[9] * z + e[13]), r4(e[2] * x + e[6] * y + e[10] * z + e[14]));\n }\n }\n return out;\n }\n function samplePart(maxPoints) { return sample(newMeshes(), maxPoints); }\n function sampleBody(maxPoints) { return sample((S.beforeMeshes || []).filter((m) => S.host && isUnder(m, S.host)), maxPoints); }\n function isUnder(o, root) { for (let p = o; p; p = p.parent) if (p === root) return true; return false; }\n\n /**\n * The camera the surface ACTUALLY draws the visible canvas with, and its controls.\n *\n * `__vqa.main` is the most-used camera across every `render(scene, camera)` call, which is NOT the buyer-facing\n * camera once an add-on is fitted: the surface then runs an internal orthographic pass (the environment/shadow\n * render), and main flips to that ortho camera. Aiming it moves a camera that never draws the canvas, so every\n * fitted frame renders the surface's default view. The surface's own OrbitControls are constructed with the visible\n * camera. The completed default-framebuffer receipt binds that camera to the canvas; ambiguous canvases fail.\n */\n const ids = new WeakMap(); let nextId = 0;\n const idOf = (object) => { if (!object) return null; if (!ids.has(object)) ids.set(object, ++nextId); return ids.get(object); };\n function activeDraw() {\n const H = globalThis.__vqa || {};\n const marked = typeof document !== 'undefined' ? document.querySelector('[data-vqv-canvas]') : null;\n const matches = [...(H.draws || new Map()).values()].filter((d) => d.scene?.isScene && d.camera.isPerspectiveCamera\n && d.canvas === d.renderer.domElement && (!marked || d.canvas === marked));\n return matches.length === 1 ? matches[0] : null;\n }\n function rig() {\n const draw = activeDraw(); const cam = draw && draw.camera;\n const ctl = (globalThis.__vqvControls || []).find((c) => c && c.object === cam && c.target) || null;\n return { cam, ctl, draw };\n }\n function renderCam() {\n const { cam, draw } = rig(); if (!cam || !draw) return null;\n const e = cam.matrixWorld.elements;\n return { cameraId: idOf(cam), rendererId: idOf(draw.renderer), canvasId: idOf(draw.canvas),\n pos: cam.position.toArray(), matrixWorld: [e[12], e[13], e[14]], worldMatrix: e.slice(), projection: cam.projectionMatrix.elements.slice(), zoom: cam.zoom, fov: cam.fov, aspect: cam.aspect, view: !!cam.view?.enabled,\n draw: { cameraId: idOf(draw.camera), rendererId: idOf(draw.renderer), canvasId: idOf(draw.canvas),\n frame: draw.frame, worldMatrix: draw.worldMatrix, projection: draw.projection, zoom: draw.zoom, view: draw.view, pos: draw.pos, matrixWorld: draw.matrixWorld, direction: draw.direction, up: draw.up } };\n }\n /** Select only the canvas actually drawn by the subject's perspective camera. */\n function isolateCanvas() {\n const draw = activeDraw(); const canvas = draw && draw.canvas;\n if (!canvas) return false;\n canvas.setAttribute('data-vqv-canvas', '1');\n for (let node = canvas; node && node !== document.body && node.parentElement; node = node.parentElement) {\n for (const sib of node.parentElement.children) if (sib !== node && sib.style) { sib.style.setProperty('visibility', 'hidden', 'important'); sib.style.setProperty('opacity', '0', 'important'); }\n }\n return true;\n }\n\n /** Point the page's camera at box [min,max] from direction dir (unit, target\u2192camera). */\n /**\n * Point the page's camera from direction dir (unit, target\u2192camera). `fit` chooses what must fill the frame:\n * 'box' = the 8 corners of [min,max]; 'scene' = sampled drawn vertices of the subject + the fitted part;\n * 'part' = sampled drawn vertices of the fitted part only. Vertex fits are tight and re-centred on the silhouette.\n */\n function aim(min, max, dir, margin, fit) {\n const { cam, ctl, draw } = rig(); if (!cam) return { ok: false, why: 'no unique subject canvas/camera draw' };\n const V = cam.position.constructor;\n const c = new V((min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2);\n if (cam.clearViewOffset) cam.clearViewOffset();\n const vfov = (cam.fov || 40) * Math.PI / 180; const aspect = cam.aspect || 1.6;\n const tv = Math.tan(vfov / 2), th = Math.tan(vfov / 2) * aspect;\n const d = new V(dir[0], dir[1], dir[2]).normalize();\n const upRef = Math.abs(d.y) > 0.98 ? new V(0, 0, -1) : new V(0, 1, 0);\n const right = new V().crossVectors(upRef, d).normalize(); const up = new V().crossVectors(d, right).normalize();\n let pts = [];\n if (fit === 'scene' || fit === 'part') {\n const list = fit === 'part' ? newMeshes() : newMeshes().concat(meshesOf(S.host || hostRoot()).filter(chainVisible));\n const flat = sample(list, 6000);\n for (let i = 0; i + 2 < flat.length; i += 3) pts.push(new V(flat[i], flat[i + 1], flat[i + 2]));\n }\n if (!pts.length) for (let i = 0; i < 8; i++) pts.push(new V(i & 1 ? max[0] : min[0], i & 2 ? max[1] : min[1], i & 4 ? max[2] : min[2]));\n // Re-centre on the silhouette in the image plane, then take the distance at which every point just fits.\n let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;\n for (const p of pts) { const q = p.clone().sub(c); const x = q.dot(right), y = q.dot(up); if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y; }\n c.addScaledVector(right, (x0 + x1) / 2).addScaledVector(up, (y0 + y1) / 2);\n let dist = 0.05;\n for (const p of pts) {\n const q = p.clone().sub(c);\n const along = q.dot(d); const x = Math.abs(q.dot(right)); const y = Math.abs(q.dot(up));\n dist = Math.max(dist, along + x / th, along + y / tv);\n }\n dist *= (margin || 1.08);\n const upv = Math.abs(d.y) > 0.98 ? [0, 0, -1] : [0, 1, 0];\n const px = c.x + d.x * dist, py = c.y + d.y * dist, pz = c.z + d.z * dist;\n // PIN THIS SHOT'S POSE FIRST. If an earlier shot already installed the updateMatrixWorld wrapper, the call below\n // would re-apply the PREVIOUS pose (overwriting cam.position) and every fitted frame would render the first shot's\n // camera. Setting this camera's pose first makes that call re-apply the NEW pose; the return uses the\n // computed numbers so it can never report a stale position.\n pinCam(cam, { pos: [px, py, pz], target: [c.x, c.y, c.z], up: upv });\n cam.position.set(px, py, pz);\n cam.near = Math.max(0.005, dist / 200); cam.far = Math.max(cam.far || 100, dist * 20);\n cam.up.set(upv[0], upv[1], upv[2]);\n cam.lookAt(c); cam.updateProjectionMatrix(); cam.updateMatrixWorld(true);\n if (ctl) {\n try {\n ctl.autoRotate = false; ctl.minDistance = 0; ctl.maxDistance = Infinity; ctl.minPolarAngle = 0; ctl.maxPolarAngle = Math.PI;\n ctl.minAzimuthAngle = -Infinity; ctl.maxAzimuthAngle = Infinity; if ('maxTargetRadius' in ctl) ctl.maxTargetRadius = Infinity;\n if ('minZoom' in ctl) ctl.minZoom = 0;\n ctl.target.copy(c); ctl.update(); ctl.dispatchEvent && ctl.dispatchEvent({ type: 'change' });\n } catch (e) {}\n }\n cam.updateMatrixWorld(true);\n return { worldMatrix: cam.matrixWorld.elements.slice(), projection: cam.projectionMatrix.elements.slice(), zoom: cam.zoom, ok: true, controls: !!ctl, pinned: true, centre: [r4(c.x), r4(c.y), r4(c.z)], distance: r4(dist), camera: [r4(px), r4(py), r4(pz)], cameraId: idOf(cam), rendererId: idOf(draw.renderer), canvasId: idOf(draw.canvas), beforeDrawFrame: draw.frame, fov: cam.fov, aspect: cam.aspect };\n }\n\n /** Temporarily hide (true) / restore (false) the snapshot subject's own meshes, leaving the fitted part drawn. */\n function hideHost(on) {\n if (on) { S.hidden = (S.beforeMeshes || []).filter((m) => m.visible && S.host && isUnder(m, S.host)); for (const m of S.hidden) m.visible = false; }\n else { for (const m of S.hidden || []) m.visible = true; S.hidden = null; }\n return (S.hidden || []).length;\n }\n\n /** Temporarily hide (true) / restore (false) the fitted part's meshes. */\n function hidePart(on) {\n if (on) { S.hiddenPart = newMeshes(); for (const m of S.hiddenPart) m.visible = false; }\n else { for (const m of S.hiddenPart || []) m.visible = true; S.hiddenPart = null; }\n return (S.hiddenPart || []).length;\n }\n\n /**\n * X-ray: draw the subject's own meshes translucent (opacity, no depth write) so an enclosed part reads through the\n * body; the fitted part stays opaque. Materials are swapped for clones and restored exactly on xray(false).\n */\n function xray(on, opacity) {\n if (on) {\n S.xray = [];\n const clones = new Map();\n for (const m of (S.beforeMeshes || []).filter((x) => x.visible && S.host && isUnder(x, S.host))) {\n const orig = m.material;\n const swap = (mat) => {\n if (!mat) return mat;\n if (!clones.has(mat)) {\n const c = mat.clone();\n c.transparent = true; c.opacity = opacity == null ? 0.18 : opacity; c.depthWrite = false;\n if ('transmission' in c) c.transmission = 0;\n c.needsUpdate = true;\n clones.set(mat, c);\n }\n return clones.get(mat);\n };\n m.material = Array.isArray(orig) ? orig.map(swap) : swap(orig);\n S.xray.push([m, orig, m.renderOrder]);\n m.renderOrder = 10;\n }\n // The part goes LAST in the transparent pass, fully opaque and depth-writing: the translucent host (which\n // writes no depth) is blended first, so the part is drawn crisp on top instead of veiled by every body layer.\n const onTop = new Map();\n for (const m of newMeshes()) {\n const orig = m.material;\n const lift = (mat) => {\n if (!mat) return mat;\n if (!onTop.has(mat)) { const c = mat.clone(); c.transparent = true; c.opacity = 1; c.depthWrite = true; c.depthTest = true; c.needsUpdate = true; onTop.set(mat, c); }\n return onTop.get(mat);\n };\n m.material = Array.isArray(orig) ? orig.map(lift) : lift(orig);\n S.xray.push([m, orig, m.renderOrder]);\n m.renderOrder = 999;\n }\n return S.xray.length;\n }\n for (const [m, orig, ro] of S.xray || []) { m.material = orig; m.renderOrder = ro; }\n const n = (S.xray || []).length; S.xray = null;\n return n;\n }\n\n /** Resolve after n animation frames (and report how many renders happened meanwhile). */\n function frames(n) {\n const H = globalThis.__vqa || {}; const f0 = H.frames || 0;\n return new Promise((res) => { let k = 0; const tick = () => { if (++k >= n) res((H.frames || 0) - f0); else requestAnimationFrame(tick); }; requestAnimationFrame(tick); });\n }\n\n return { snapshot, newMeshCount, instances, partStats, hostStats, subjectStats, hostWheels, hostFrame, nodePositions, bones,\n samplePart, sampleBody, isolateCanvas, aim, releaseCam, renderCam, frames, hideHost, hidePart, xray, presentationScale, rig: () => { const r = rig(); return { camera: !!r.cam, controls: !!r.ctl, fov: r.cam ? r.cam.fov : null, aspect: r.cam ? r.cam.aspect : null }; } };\n})();";