import * as T from 'three'; import { buildAppearance, disposeAppearance } from './appearance-model'; import type {Entity, HouseScene, Snapshot} from './generated'; /** Indoor presentation only. Positions and furniture geometry come from Go. */ export class HouseView { readonly scene=new T.Scene(); readonly camera=new T.OrthographicCamera(-10,10,10,-10,.1,100); private room=new T.Group();private actors=new Mapvoid}>(); private signature='';private active=false;private size=12;private ray=new T.Raycaster(); private disposables:(()=>void)[]=[]; constructor(){this.scene.background=new T.Color('#172329');this.scene.add(new T.HemisphereLight('#fff5db','#4b3930',2));const sun=new T.DirectionalLight('#ffdfaa',3);sun.position.set(2,9,6);this.scene.add(sun,this.room)} private box(w:number,h:number,d:number,x:number,y:number,z:number,color:string,parent:T.Object3D=this.room){const geometry=new T.BoxGeometry(w,h,d),material=new T.MeshStandardMaterial({color,roughness:.85});const mesh=new T.Mesh(geometry,material);mesh.position.set(x,y,z);parent.add(mesh);this.disposables.push(()=>{geometry.dispose();material.dispose()});return mesh} update(snapshot:Snapshot,model?:T.Group){ const house=snapshot.house;if(!house){if(this.active)this.clear();return}this.active=true;this.size=house.size; const signature=JSON.stringify([house.id,house.size,house.furniture]);if(this.signature!==signature){this.clear();this.active=true;this.signature=signature;this.build(house)} const entities=[...house.people.filter(p=>p.id!==snapshot.self.entity.id),{...snapshot.self.entity,position:snapshot.self.housePosition??{x:0,y:0,z:0}}]; const ids=new Set(entities.map(p=>p.id));for(const [id,v] of this.actors)if(!ids.has(id)){this.scene.remove(v.root);v.dispose();this.actors.delete(id)} for(const e of entities){let actor=this.actors.get(e.id);const appearanceKey=JSON.stringify(e.appearance??null);if(actor&&actor.root.userData.appearance!==appearanceKey){this.scene.remove(actor.root);actor.dispose();this.actors.delete(e.id);actor=undefined}if(!actor){const firstDisposable=this.disposables.length,root=new T.Group();root.userData.appearance=appearanceKey;if(e.appearance){const visual=buildAppearance(e.appearance);root.add(visual);this.disposables.push(()=>disposeAppearance(visual))}else if(model)root.add(model.clone(true));else{this.box(.55,1.25,.45,0,.8,0,e.id===snapshot.self.entity.id?'#bdd1ad':'#88a9c6',root);this.box(.4,.4,.4,0,1.65,0,'#d6b49a',root)} const canvas=document.createElement('canvas');canvas.width=256;canvas.height=48;const ctx=canvas.getContext('2d')!;ctx.fillStyle='#13201ee6';ctx.fillRect(0,0,256,48);ctx.fillStyle='#fff5df';ctx.textAlign='center';ctx.font='24px sans-serif';ctx.fillText(e.name,128,32,248);const texture=new T.CanvasTexture(canvas),material=new T.SpriteMaterial({map:texture,depthTest:false}),name=new T.Sprite(material);name.position.y=2.2;name.scale.set(2.5,.47,1);root.add(name);this.disposables.push(()=>{texture.dispose();material.dispose()});root.position.set(e.position.x,e.position.y,e.position.z);const cleanup=this.disposables.splice(firstDisposable);actor={root,target:root.position.clone(),dispose:()=>cleanup.forEach(f=>f())};this.actors.set(e.id,actor);this.scene.add(root) }actor.target.set(e.position.x,e.position.y,e.position.z) } } private build(h:HouseScene){ this.box(h.size,.18,h.size,0,-.09,0,'#907451');this.box(h.size,2.8,.2,0,1.4,-h.size/2,'#bab297');this.box(.2,2.8,h.size,-h.size/2,1.4,0,'#9e9986'); for(let x=-h.size/2;x.002)a.root.rotation.y=Math.atan2(-delta.x,-delta.z);a.root.position.lerp(a.target,1-Math.exp(-14*dt))}renderer.render(this.scene,this.camera); } pick(canvas:HTMLCanvasElement,x:number,y:number):{entity?:Entity;point?:T.Vector3}{const r=canvas.getBoundingClientRect();this.ray.setFromCamera(new T.Vector2((x-r.left)/r.width*2-1,-(y-r.top)/r.height*2+1),this.camera);for(const hit of this.ray.intersectObject(this.room,true)){let o:T.Object3D|null=hit.object;while(o){if(o.userData.entity)return{entity:o.userData.entity as Entity};o=o.parent}if(Math.abs(hit.point.y)<.02)return{point:hit.point}}return{}} clear(){for(const dispose of this.disposables)dispose();this.disposables=[];this.room.clear();for(const a of this.actors.values()){this.scene.remove(a.root);a.dispose()}this.actors.clear();this.signature='';this.active=false} }