import test from 'node:test'; import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import ts from 'typescript'; import {PerspectiveCamera,Vector3} from 'three'; import {Camera,GraphNode,ASPECT_MANUAL} from 'playcanvas'; const url=s=>`data:text/javascript;base64,${Buffer.from(s).toString('base64')}`; function moduleUrl(p){return url(ts.transpileModule(readFileSync(p,'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext}}).outputText.replace(/from "([^"]+)"/g,(_,n)=>`from "${n.startsWith('.')?moduleUrl(new URL(n+'.ts',p)):import.meta.resolve(n)}"`));} const projection=moduleUrl(new URL('../src/core/simulation/editorProjection.ts',import.meta.url)); const coordinates=moduleUrl(new URL('../src/core/simulation/aiEditorCoordinates.ts',import.meta.url)); const drag=moduleUrl(new URL('../src/core/simulation/waypointDrag.ts',import.meta.url)); const {switchProjection}=await import(projection),{toEditor}=await import(coordinates); // Execute the real runtime methods against the real PlayCanvas camera math, without a GPU/app. const source=readFileSync(new URL('../src/components/simulation/AiEditorRuntime.ts',import.meta.url),'utf8'); const ast=ts.createSourceFile('runtime.ts',source,ts.ScriptTarget.Latest,true); const cls=ast.statements.find(s=>ts.isClassDeclaration(s)); const names=new Set(['syncCamera','project','projectSemantic','moveSemantic','semanticGizmo','semanticSegmentParameter']); const methods=cls.members.filter(m=>names.has(m.name?.getText(ast))).map(m=>m.getText(ast)).join('\n'); const actual=`import {Vec3,PROJECTION_ORTHOGRAPHIC,PROJECTION_PERSPECTIVE} from "${import.meta.resolve('playcanvas')}";import {isOrthographic,screenSpan} from "${projection}";import {toEditor,toMission} from "${coordinates}";import {dragWaypoint} from "${drag}";export class Runtime {${methods}}`; const {Runtime}=await import(url(ts.transpileModule(actual,{compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022}}).outputText)); function fixture(mode){ let c=new PerspectiveCamera(60,1.5,.05,3000);c.position.set(7,12,20);c.lookAt(0,0,0);c.updateMatrixWorld(); c=switchProjection(c,mode,1.5,c.position.length()); const pc=new Camera(),node=new GraphNode();pc.node=node;pc.aspectRatioMode=ASPECT_MANUAL;pc.aspectRatio=1.5;pc.fov=60;pc.nearClip=.05;pc.farClip=3000; node.camera={get projection(){return pc.projection;},set projection(v){pc.projection=v;},get orthoHeight(){return pc.orthoHeight;},set orthoHeight(v){pc.orthoHeight=v;},worldToScreen:p=>pc.worldToScreen(p,900,600),screenToWorld:(x,y,z)=>pc.screenToWorld(x,y,z,900,600)}; const r=new Runtime();Object.assign(r,{camera:node,controls:{camera:c},canvas:{clientWidth:900,clientHeight:600},clip:Infinity,clipFloor:-Infinity,preferences:{pivot:{pixels:100}}});r.syncCamera();return {r,c,pc}; } for(const mode of ['orthographic','perspective']){ test(`${mode}: render and overlay projection agree; axis/plane dragging lands under cursor`,()=>{ const {r,c}=fixture(mode),a=[1,2,0]; for(const [axis,b] of [['x',[3,2,0]],['y',[1,4,0]],['z',[1,2,3]],['xy',[3,4,0]],['xz',[3,2,3]],['yz',[1,4,3]]]){ const pixel=r.projectSemantic(b),q=new Vector3(...toEditor(b)).project(c); assert.ok(Math.abs(pixel[0]-(q.x+1)/2)<1e-6);assert.ok(Math.abs(pixel[1]-(1-q.y)/2)<1e-6); const moved=r.moveSemantic(a,...pixel,axis);assert.ok(moved);moved.forEach((v,k)=>assert.ok(Math.abs(v-b[k])<2e-4,`${axis} ${v} != ${b[k]}`)); } }); test(`${mode}: inserted point on a sloping segment matches the pixel interpolation`,()=>{ const {r}=fixture(mode),a=[-3,-2,0],b=[4,5,4],pa=r.projectSemantic(a),pb=r.projectSemantic(b); for(const u of [.1,.4,.8]){const t=r.semanticSegmentParameter(a,b,u),p=r.projectSemantic(a.map((v,k)=>v+(b[k]-v)*t));p.forEach((v,k)=>assert.ok(Math.abs(v-(pa[k]+(pb[k]-pa[k])*u))<1e-6));} }); } test('orthographic gizmo keeps its pixel size at different depths; clipping still applies',()=>{ const {r}=fixture('orthographic');assert.equal(r.semanticGizmo([0,0,0]).length,r.semanticGizmo([0,0,10]).length); r.clip=2;r.clipFloor=-1;assert.equal(r.projectSemantic([0,0,3]),null);assert.equal(r.projectSemantic([0,0,-2]),null); const pixel=r.projectSemantic([1,2,0]);assert.ok(r.moveSemantic([1,2,0],...pixel,'xy')); });