Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.
JavaScript 3d3d-assets3d-modelingagent-skillsagent-toolsai-agentsasset-pipelinecligame-assetsglbgltfmcpmcp-servermodel-context-protocolprocedural-generationprocedural-modelingtext-to-3dthreejstypescript
import {createLandingTimeline} from './src/landing-timeline.mjs';
import {GLTFLoader} from 'three/addons/loaders/GLTFLoader.js';import {createCrewUnload} from './web/crew-unload.mjs';import {createPoseInstances} from './web/pose-instances.mjs';
// Staged bundle entry: CPU reference simulation stays in its local boat frame;
// rendering applies one placement frame, keeping planning and terrain aligned.
export async function createTroyLanding(T,{mode='instances'}={}){
if(!['instances','hierarchy'].includes(mode))throw Error('Unknown landing renderer');const base=new URL('./',import.meta.url),manifest=await(await fetch(new URL('manifest.json',base))).json(),bytes=new Map(),hash=async b=>Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256',b)),n=>n.toString(16).padStart(2,'0')).join('');
await Promise.all(manifest.files.map(async f=>{const r=await fetch(new URL(f.file,base));if(!r.ok)throw Error('Missing landing file '+f.file);const b=await r.arrayBuffer();if(await hash(b)!==f.sha256)throw Error('Landing identity mismatch '+f.file);bytes.set(f.file,b);}));
const data=JSON.parse(new TextDecoder().decode(bytes.get('queue.json'))),loader=new GLTFLoader(),ship=(await loader.parseAsync(bytes.get('ship.glb'),'')).scene,human=(await loader.parseAsync(bytes.get('crew.glb'),'')).scene;ship.scale.setScalar(1.6);ship.position.fromArray(data.shipPosition);ship.getObjectByName('BoardingPlankPivot').rotation.x=data.landing.angle;ship.updateMatrixWorld(true);
const actors=data.actors.map(entry=>{const root=human.clone(true),actor=createCrewUnload(T,{root,ship,surfaces:[],row:entry.row,side:entry.side,id:entry.id,staging:entry.staging,cachedPlan:entry.plan,finalHeading:0});if(Math.abs(actor.duration-entry.duration)>1e-6)throw Error('Staged motion duration mismatch');return {root,actor,entry,last:null};});
const group=new T.Group();group.name='ship-1-2/landing';group.position.fromArray(data.frame.position);group.rotation.y=data.frame.yaw;const renderedShip=ship.clone(true),shipSource=[],shipTarget=[];ship.traverse(n=>shipSource.push(n));renderedShip.traverse(n=>shipTarget.push(n));group.add(renderedShip);
const pose=mode==='instances'?createPoseInstances(actors.map(a=>a.root)):null,clones=[];if(pose)group.add(pose.group);else for(const a of actors){const clone=a.root.clone(true),from=[],to=[];a.root.traverse(n=>from.push(n));clone.traverse(n=>to.push(n));clones.push({clone,from,to});group.add(clone);}
group.traverse(n=>{if(n.isMesh)n.castShadow=n.receiveShadow=true;});const timeline=createLandingTimeline({queueDuration:data.duration,landedAngle:data.landing.angle}),renderedPlank=renderedShip.getObjectByName('BoardingPlankPivot');let lastTime=-1,counts={},sequence;
const copy=(from,to)=>{for(let i=0;i<from.length;i++){to[i].position.copy(from[i].position);to[i].quaternion.copy(from[i].quaternion);to[i].scale.copy(from[i].scale);}};
function update(time){if(!Number.isFinite(time)||time<0)throw Error('Invalid landing time');const clamped=Math.min(time,timeline.duration);if(clamped===lastTime)return;lastTime=clamped;sequence=timeline.sample(clamped);const queueTime=sequence.queueTime,dirty=[];counts={waiting:0,standing:0,aisle:0,walking:0,ashore:0};for(let i=0;i<actors.length;i++){const a=actors[i],local=Math.max(0,Math.min(a.actor.duration,queueTime-a.entry.start));if(local!==a.last){a.actor.sample(local);a.last=local;dirty.push(i);if(!pose)copy(clones[i].from,clones[i].to);}const stage=sequence.phase==='lowering'||queueTime<a.entry.start?'waiting':local<=3.6?'standing':local<a.actor.walkStart?'aisle':local<a.actor.duration?'walking':'ashore';counts[stage]++;}if(pose)pose.update(dirty);copy(shipSource,shipTarget);renderedPlank.rotation.x=sequence.plankAngle;group.updateMatrixWorld(true);}
update(0);return {group,update,duration:timeline.duration,stats(){return {mode,time:lastTime,sequence,counts,frame:data.frame,shipPosition:data.shipPosition,plankAngle:sequence.plankAngle,actors:actors.map(a=>({id:'ship-1-2/'+a.entry.id,position:group.localToWorld(a.root.position.clone()).toArray()})),partBatches:pose?.partCount??null,qualification:'functional prototype; not performance accepted'};},dispose(){pose?.dispose();}};
}