--- name: ha-3d-floorplan description: Build an interactive 3D "dollhouse" model of a home (three.js, single HTML file) with live Home Assistant lights — tap a lamp to switch it, hold to dim, per-room views with automatic wall cut-away, day/evening lighting, real mirrors, and a whole-house view that stays fast on a cheap tablet. Use when someone wants a 3D floor plan / 3D house / digital twin of their home for a Home Assistant dashboard or wall tablet, or wants to extend or fix one built this way. --- # 3D floor plan for Home Assistant The result is one self-contained HTML file (three.js from a CDN, no build step), served by Home Assistant from `/config/www/…` → `https:///local/…`. It shows the home as a cut-away dollhouse; every lamp in the model is linked to one or more HA entities and reflects their live state (on/off, brightness, colour, colour temperature). Start by copying `assets/template.html` to the user's project (e.g. `house-3d.html`) — it is a complete, working example house (living room + kitchen, bedroom, hall) with every system below already wired up. Build the user's home by replacing the sections marked `>>> YOUR HOUSE <<<`. Do not rewrite the engine parts from scratch; they contain fixes for real problems (see "Lessons" below). ## Workflow 1. **Collect input from the user** - A floor plan with dimensions (image, PDF, sketch or just measurements per room). Ceiling height. - Photos of each room: furniture placement, colours, materials, lamp types. Ask for more photos of a room before modelling it in detail — guessing furniture positions leads to many correction rounds. - The list of lamp entities per room (use the HA MCP tools if available, otherwise ask). Note multi-bulb fixtures (e.g. 3 spots = 3 entities) and which lamps should dim together. - Target device (tablet model) — sets the performance budget. 2. **Coordinate system.** Pick plan units: trace the floor plan image in pixels and set `PXM` to pixels-per-metre (measure a known wall), or use centimetres (`PXM = 100`). Set `OX, OY` to a convenient origin. Plan x → east, plan y → south, heights in metres. 3. **Rooms + views.** Fill `ROOMS` (rect incl. walls, wall colour) and `VIEWS` (one per room or room group, plus "whole house"). 4. **Walls** with `wall(id, x0,y0,x1,y1, {N/S/E/W: room}, openings)`. Name each side's room so the cut-away knows which walls to lower. Openings: `[from, to, sill, head, glass]` — windows have a sill, doors have sill 0 and `glass=false`. 5. **Per room:** `ROOM = 'x'; roomBase(); floor(...)`, then furniture with `box / cyl / RoundedBox / groups`, then lamps. 6. **Lamps:** map fixture keys to entities in `ENTITY` (+ `NAMES`, `FAMILY`, `LBL`). Every lamp = a visible `glowMesh` (tappable, emissive, halo) + one or more real `pointLight / spotLight` registered under the same key. For multi-bulb fixtures set `E = 'light.xyz'` before each bulb, reset `E = null` after. 7. **Test with screenshots** (see "Testing"). Check every view in day and evening mode, from at least two angles, before showing the user. 8. **Deploy** to HA `/config/www//` (Samba, File editor, SSH, or the user's own method — ask; never handle their passwords yourself). Open `https:///local//house-3d.html`, tap the status pill to log in with HA OAuth once per browser. 9. **Embed** (optional) in a dashboard: an iframe with `?embed=1` on the same origin. See `references/architecture.md` → "Embedding". 10. Iterate on the user's feedback with photos; after every round re-shoot the affected views. ## Reference files | Read when | File | |---|---| | Changing or debugging the engine: walls/cut-away, lamps/state, whole-house light pool, HA connection, picker, render loop, embedding messages | `references/architecture.md` | | Making furniture, materials and textures look good (wood, fabric, tiles, plants, mirrors, LED strips, balconies, curved shapes) | `references/modeling.md` | | Running the local preview + screenshot loop | `scripts/serve.js` and "Testing" below | ## Testing Run `node scripts/serve.js [port]` and open the page in a browser (the Claude browser pane works, headless Chrome too): ``` house-3d.html?shot=1&w=1600&h=1000&save=NAME&view=living&mode=day|evening&on=all ``` - `shot=1` renders one frame with no UI; `save=NAME` posts it to the server → `NAME.png` next to the HTML; read the PNG to look at it. - `view`, `mode`, `az` / `el` (camera degrees), `dist` (m), `tx` / `ty` (look-at point in plan units), `on=all` or `on=key1,key2`. - Off-localhost the page connects to HA; on localhost it runs in demo mode (taps toggle locally). - Look at each change from close by (`dist=5&tx=..&ty=..`) — problems like z-fighting, objects poking through walls or a slab sticking out are invisible in the overview. - Delete the shot PNGs afterwards. ## Lessons (each one cost a correction round) - **Room rect ≠ floor.** The room rect is also used for camera framing; if it includes a balcony or bay, pass a tighter rect to `roomBase(rect)` or the slab sticks out under the balcony. - **Things on walls must follow their wall.** Kitchen cabinets, bookcases, mirrors, wall lamps, tiles: wrap them with `onWall(id, mesh)` / `sinkSince(mark, id)` so they shrink with the cut-away wall, or `wallItem(id, mesh)` to hide them. Attach to the wall they physically hang on — usually the exterior wall that stays up — not a nearby interior wall. - **Whole house = too many lights for a tablet.** Never enable 40+ real lights at once (shader compile fails / context loss). The template uses floor "pools" + a fixed pool of 24 real lights with nearest-merge and automatic step-down. Do not "fix" dimming by making lamps share one light only — the user will notice lamps dimming each other. - **Mirrors (Reflector) are expensive**: room views only, hidden in the whole-house view. - **Render on demand** (`dirty` flag) and adaptive pixel ratio; a wall tablet otherwise runs hot and drains. - **Hit-testing lamps** by projected screen distance (70 px) to their glow mesh beats raycasting on a touchscreen. - **Interior walls between two visible rooms** stay low; facade walls go down only on the camera side. Lintels/glass above openings hide when the wall is lowered (otherwise floating bars). - **Patching the big HTML with scripts:** use `str.replace(a, () => b)` (function form) — a plain replacement string turns `$$` into `$`. Avoid inserting `//` comments mid-line with sed. - **Pinch/two-finger pan** must be clamped to the house bounds or users lose the model. - **Embedded in a dashboard with a screensaver:** forward touch activity from the iframe to the parent (`h3d-activity` message), or the screensaver kicks in while the user is using the 3D view. - **Privacy:** if the user wants a demo/shareable version, strip names (sign texts, lamp names like "Bedside "), entity IDs and anything location-related; generate fake data. ## Scope and safety - Only call HA services the user expects (lights/switches on, off, toggle, brightness). Ask before touching automations, configs or add-ons. - Never ask for or type the user's HA password or long-lived token; the page uses HA's own OAuth login, done by the user.