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 {bindDiscreteAction} from './touch-action.mjs';
import {createHeroExplorer} from './hero-explorer.mjs';import {createExploreInput} from './explore-input.mjs';
// DOM ownership, separate from the deterministic traversal and camera host.
export function createHeroExplorePlay(T,{canvas,pair,groundHeight,world,gate,camera,controls,onBeforeStart=()=>{},onStart=()=>{},onStop=()=>{},onPause=()=>{},onResume=()=>{},isPaused=()=>false}){
const input=createExploreInput(),primary=document.createElement('section'),element=document.createElement('div'),panel=document.createElement('section'),readout=document.createElement('output'),cleanup=[],orbit=new Map();let previousGesture=null,lastUi='';primary.className='hero-primary explore-primary';primary.hidden=true;primary.setAttribute('aria-label','Exploration movement');document.body.append(primary);element.className='hero-explore';panel.className='explore-panel';panel.hidden=true;panel.setAttribute('aria-label','Hero exploration');readout.setAttribute('aria-label','Exploration position');
const controller=createHeroExplorer(T,{pair,groundHeight,world,gate,camera,controls,onBeforeStart(side){clear();onBeforeStart(side);},onStart(){panel.hidden=false;primary.hidden=false;onStart();},onStop(){clear();panel.hidden=true;primary.hidden=true;onStop();}});
const button=(text,fn)=>{const b=document.createElement('button');b.type='button';b.textContent=text;if(fn)cleanup.push(bindDiscreteAction(b,fn));return b;};
const listen=(host,type,fn,options)=>{host.addEventListener(type,fn,options);cleanup.push(()=>host.removeEventListener(type,fn,options));};
function clear(){input.clear();orbit.clear();previousGesture=null;lastUi='';}
function start(side){const result=controller.start(side);refresh();return result;}
const auto=button('Start walking',()=>{input.autoWalk(!input.stats().autoWalking);refresh();}),pause=button('Pause exploration',()=>isPaused()?onResume():onPause()),gateButton=button('Open nearby gate · E',interact);
element.append(button('Explore as Achilles',()=>start('achilles')),button('Explore as Hector',()=>start('hector')));
const directions=document.createElement('div');directions.className='explore-directions';for(const [text,vector]of [['Forward',{forward:1}],['Back',{forward:-1}],['Left',{right:-1}],['Right',{right:1}]]){const b=button(text),owned=new Map();listen(b,'pointerdown',e=>{if(!controller.active())return;e.preventDefault();b.setPointerCapture(e.pointerId);owned.set(e.pointerId,input.pointer(e.pointerId,vector));});const release=e=>{const token=owned.get(e.pointerId);input.release(e.pointerId,token);owned.delete(e.pointerId);};for(const type of ['pointerup','pointercancel','lostpointercapture'])listen(b,type,release);directions.append(b);}
const turn=amount=>{const c=controller.stats().camera;controller.setCameraIntent({yaw:c.yaw+amount});controller.update(0,{});refresh();};
const orbitButtons=document.createElement('div');orbitButtons.append(button('Turn view left',()=>turn(-Math.PI/2)),button('Turn view right',()=>turn(Math.PI/2)));
const help=document.createElement('p');help.textContent='WASD or arrows move relative to the view. Drag to orbit; scroll or pinch to zoom. E opens the nearby gate. Step clear while the doors swing. Start walking continues forward until stopped; movement keys take over.';
const guidance=document.createElement('details'),summary=document.createElement('summary');summary.textContent='Camera and keys';guidance.append(summary,orbitButtons,help);
primary.append(auto,directions,gateButton);panel.append(readout,guidance,pause,button('Leave exploration',()=>{controller.stop();refresh();}));element.append(panel);
function nearGate(){const p=controller.stats().actor;return p&&Math.hypot(p.x-gate.root.position.x,p.z-gate.root.position.z)<=12;}
function interact(){if(controller.active()&&nearGate())gate.setOpen(!gate.stats().targetOpen);refresh();}
function refresh(){const s=controller.stats();if(!s.active)return;const p=s.actor,g=gate.stats(),a=input.stats(),key=[s.player,Math.round(p.x*10),Math.round(p.z*10),s.blocked,g.targetOpen,g.moving,g.blockedByOccupant,a.autoWalking,isPaused()].join(':');if(key===lastUi)return;lastUi=key;const place=p.z<140?'Beach':p.z<210?'Greek formations':p.z<330?'Hero arena':p.z<357?'Gate approach':p.z<369?'Gate passage':Math.abs(p.x-65)<17&&Math.abs(p.z-445)<17?'Courtyard':'Troy streets';readout.textContent=(s.player==='achilles'?'Achilles':'Hector')+' · '+place+(s.blocked?' · Path blocked':'')+(g.moving&&g.blockedByOccupant?' · Step clear of the doors':'');readout.dataset.x=String(p.x);readout.dataset.z=String(p.z);readout.dataset.hero=s.player;readout.dataset.soleMinimum=String(s.soleMinimum);auto.textContent=a.autoWalking?'Stop walking':'Start walking';auto.setAttribute('aria-pressed',String(a.autoWalking));gateButton.disabled=!nearGate();gateButton.textContent=g.targetOpen?'Close nearby gate · E':'Open nearby gate · E';pause.textContent=isPaused()?'Resume exploration':'Pause exploration';}
const keydown=e=>{if(!controller.active()||['INPUT','SELECT','TEXTAREA'].includes(document.activeElement?.tagName))return;if(['KeyW','KeyA','KeyS','KeyD','ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.code)){e.preventDefault();input.key(e.code,true);}if(e.code==='KeyE'&&!e.repeat){e.preventDefault();interact();}},keyup=e=>input.key(e.code,false),blur=()=>{clear();if(controller.active())onPause();};listen(window,'keydown',keydown);listen(window,'keyup',keyup);listen(window,'blur',blur);listen(document,'visibilitychange',()=>{if(document.hidden)blur();});
function gesture(){const values=[...orbit.values()];if(!values.length)return null;const centre=values.reduce((a,p)=>[a[0]+p.x/values.length,a[1]+p.y/values.length],[0,0]);return {count:values.length,x:centre[0],y:centre[1],distance:values.length===2?Math.hypot(values[0].x-values[1].x,values[0].y-values[1].y):0};}
listen(canvas,'pointerdown',e=>{if(!controller.active()||e.button!==0)return;canvas.setPointerCapture(e.pointerId);orbit.set(e.pointerId,{x:e.clientX,y:e.clientY});previousGesture=gesture();});
listen(canvas,'pointermove',e=>{if(!controller.active()||!orbit.has(e.pointerId))return;orbit.set(e.pointerId,{x:e.clientX,y:e.clientY});const next=gesture(),c=controller.stats().camera;if(previousGesture?.count===next.count){if(next.count===1)controller.setCameraIntent({yaw:c.yaw-(next.x-previousGesture.x)*.006,pitch:c.pitch+(next.y-previousGesture.y)*.004});else if(next.count===2&&previousGesture.distance>0&&next.distance>0)controller.setCameraIntent({distance:c.requestedDistance*previousGesture.distance/next.distance});}previousGesture=next;});
const orbitRelease=e=>{orbit.delete(e.pointerId);previousGesture=gesture();};for(const type of ['pointerup','pointercancel','lostpointercapture'])listen(canvas,type,orbitRelease);
listen(canvas,'wheel',e=>{if(!controller.active())return;e.preventDefault();controller.setCameraIntent({distance:controller.stats().camera.requestedDistance*Math.exp(e.deltaY*.0015)});},{passive:false});
return {element,start,stop(){const s=controller.stop();refresh();return s;},active:controller.active,update(dt){const result=controller.update(dt,input.value());if(result.blocked&&input.stats().autoWalking)input.autoWalk(false);refresh();return result;},setCameraIntent:controller.setCameraIntent,stats:()=>({...controller.stats(),input:input.stats()}),advance(dt,value){return controller.update(dt,value);},dispose(){clear();controller.dispose();for(const release of cleanup.splice(0))release();element.remove();primary.remove();}};
}