Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(compose): Scene Composer — real-time 3D item forging + avatar outfit builder

nirholas7df2d8b363cf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bad9da105..7b98b68d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ Public history for [three.ws](https://three.ws), newest first. New pages come fr
## 2026-06-13
- **AR & WebXR — Place Any 3D Agent in Your Real World** (`/features/ar`) — Every three.ws avatar and Forge model has a View in AR button. On mobile it anchors the model to real surfaces through your camera. WebXR on Android, Quick Look on iOS — no app, no download.
+- **Scene Composer** (`/compose`) — Real-time 3D scene composer: forge items from text, attach them to your avatar's skeleton bones, arrange and scale freely, then export your creation or save it as an outfit.
- **3D model categories — every model is now typed as Avatar, Accessory, Item, Scene, Creature, or Vehicle** — All 3D models on the platform now carry a category that describes what they actually are, not just how they were created. Avatar cards in the marketplace show a color-coded type badge (Avatar, Accessory, Item, Scene, Creature, or Vehicle) and a new filter row lets you browse by type. The Forge gallery shows the same badges on showcase cards, and when you finish forging a model you can tag it right in the result bar with a single click. This is the foundation for the upcoming agent equipment system — where your agents will be able to wear accessories, hold items, and inhabit scenes. (`/marketplace`) `[feature, improvement]`
- **All 14 three.ws MCP servers are now live on npm and the official MCP registry** — Every three.ws MCP server is now publicly installable and discoverable. The eight install-and-run servers ship on npm under the @three-ws scope — pull any of them with one command, e.g. npx -y @three-ws/mcp-server (3D + agent tools), @three-ws/three-token-mcp ($THREE price, hold, and burn), @three-ws/pumpfun-mcp, @three-ws/ibm-x402-mcp, @three-ws/avatar-agent, and more. Together with the six hosted remote servers, all 14 are registered in the official Model Context Protocol registry, so any MCP-compatible client — Claude, Cursor, and others — can find them by name. No more dead install links: the documented commands now work. (`/docs/mcp`) `[sdk, feature]`
- **AR is live on the homepage — place any 3D model in your real space from the front page** — The three.ws homepage now has a live AR section: real models from the Forge gallery load in an interactive 3D viewer, and on mobile you can tap 'Place in your space' to anchor the model in your real environment through the camera. On desktop a QR code appears so you can scan and place from your phone in one step. WebXR on Android, Quick Look on iOS — no app, no download. A full blog post explains how the AR pipeline works across both platforms. (`/blog/see-your-3d-in-ar`) `[feature, improvement]`
@@ -17,6 +18,7 @@ Public history for [three.ws](https://three.ws), newest first. New pages come fr
- **pump.fun MCP server now listed in the official MCP registry** — Our free, read-only pump.fun and Solana tools are now discoverable in the official Model Context Protocol registry, so any MCP-compatible client (Claude, Cursor, and others) can find and connect to them by name. It's a remote server — no install, no API key for the free tools — covering token discovery, bonding-curve and holder analysis, creator intelligence, and Solana Name Service lookups. This brings the number of three.ws servers in the registry to six. (`/`) `[feature, sdk]`
- **Refine any Forge model to higher quality in one click — plus sharper multi-view guidance** — Three upgrades to the Forge (/forge). After a model generates, a new Refine button re-runs that exact prompt or photos one quality tier higher — Draft to Standard to High — so you can preview cheaply, then commit to the best version without retyping anything. When building from photos, a live counter now shows how many of the four reference views you've added and reminds you that more angles make a sharper mesh. And the action bar under each finished model now reflows cleanly on phones, with Refine and Make-another leading at full width. Sketch mode also makes its required description unmistakable. (`/forge`) `[improvement]`
- **Reliability pass: steadier live feed, token art that never blanks, and a graceful 3D fallback** — We traced a batch of production errors to their roots and fixed them. The live activity ticker no longer hammers our rate store on every open tab — one shared reader now fans updates out to everyone on a server and quiets down while the feed is idle, which is what was silently exhausting our request quota and freezing the feed. When a remote token image stalls, you now get a clean, on-brand placeholder — a unique gradient gem per token — instead of a timeout or a broken-image tile. On devices that can't run 3D (some locked-down or headless browsers), the viewer now shows a clear 'preview unavailable' card with a reload option instead of a blank stage. Text-to-3D generations that take a little longer to start no longer get cut off early, and a stray click-handling error on the homepage, search, and glossary is gone. `[fix, infra]`
+- **Scene Composer — forge items in real time and dress your avatar** — New /compose page lets you build a 3D scene live: load an avatar (or start empty), type a prompt to forge new items, and drop them straight into the scene. Select any object with a click to translate, rotate, or scale it with the gizmo. Items can be attached to specific avatar bones — hat to head, sword to right hand — and the inspector shows live transform values you can also type in. When you're done, export the full scene as a GLB or save an outfit back to your avatar. Your recent forge creations appear in the panel so you can reuse them across sessions without re-generating. (`/compose`) `[feature]`
- **See any Forge model in AR — scan from your desktop, place it in your room** — Every finished model now has a View in AR button. On a phone it drops the model into your space through the camera; on a desktop it shows a QR code you scan with your phone to open the same model, one tap from AR — so you can walk around what you just made at real-world scale. The embed viewer gained a big one-tap AR launcher on phones too, so models embedded on other sites are placeable in AR as well. No app, no download — it runs in the browser. (`/forge`) `[feature]`
- **Selfie to 3D avatar now works with your own API key — Meshy and Tripo supported** — The selfie-to-avatar pipeline (/create/selfie) is fully open: if you have a Meshy or Tripo API key, you can turn a photo into a rigged 3D avatar in about 60–90 seconds. Enter your key once on the setup screen and it stays cached for the session — or save it to your account settings so you never have to enter it again. The pipeline now properly routes polling through whichever provider submitted the job, so BYOK jobs no longer stall mid-build. The page no longer shows 'still warming up' — it detects which mode the platform is running in and gives you the right experience immediately. (`/create/selfie`) `[feature, improvement]`
- **The full page directory is now one click away — and instantly filterable** — three.ws has grown past 200 pages, so the sitemap directory got a real upgrade. A new "All pages" link in the Discover menu takes you straight to it, and the page itself now has an instant filter — press / and type to narrow every page by name, path, or description, with live per-section counts. Filtered views are shareable links, so you can send someone exactly the slice of the platform you mean. The directory now finds you when you're lost, too: it's a quick action in the ⌘K search palette, a no-results search hands you off to it with your query carried over, the 404 page offers it pre-filtered to the closest matches for the address you tried, and /pages, /directory, /everything, and /all-pages all take you there. Eighteen pages that had never been registered in the index — What is three.ws?, the 3D Scanner, the Glossary, Support, the agent-economy demos, and all six IBM watsonx showcases under a new Integrations section — are now in the directory, the search palette, and the crawler sitemap. (`/sitemap`) `[improvement]`
diff --git a/data/changelog.json b/data/changelog.json
index 97ad8a01b..39529a2f6 100644
--- a/data/changelog.json
+++ b/data/changelog.json
@@ -1,6 +1,15 @@
{
"$comment": "Curated changelog entries for three.ws — the editorial layer of the public changelog. New PAGE launches are tracked automatically via the `added` field in data/pages.json; this file is for everything else holders care about: improvements, fixes, SDK releases, trading/payments capabilities, security work. scripts/build-page-index.mjs merges both sources into CHANGELOG.md, public/changelog.json, and public/changelog.xml. Newest first. Allowed tags: feature, improvement, fix, sdk, infra, docs, security. `link` is optional and must be a live three.ws path.",
"entries": [
+ {
+ "date": "2026-06-13",
+ "title": "Scene Composer — forge items in real time and dress your avatar",
+ "summary": "New /compose page lets you build a 3D scene live: load an avatar (or start empty), type a prompt to forge new items, and drop them straight into the scene. Select any object with a click to translate, rotate, or scale it with the gizmo. Items can be attached to specific avatar bones — hat to head, sword to right hand — and the inspector shows live transform values you can also type in. When you're done, export the full scene as a GLB or save an outfit back to your avatar. Your recent forge creations appear in the panel so you can reuse them across sessions without re-generating.",
+ "tags": [
+ "feature"
+ ],
+ "link": "/compose"
+ },
{
"date": "2026-06-13",
"title": "3D model categories — every model is now typed as Avatar, Accessory, Item, Scene, Creature, or Vehicle",
diff --git a/data/pages.json b/data/pages.json
index 1ea1ec2f5..be3d520b6 100644
--- a/data/pages.json
+++ b/data/pages.json
@@ -159,6 +159,14 @@
"changefreq": "monthly",
"added": "2026-06-12"
},
+ {
+ "path": "/compose",
+ "title": "Scene Composer",
+ "description": "Real-time 3D scene composer: forge items from text, attach them to your avatar's skeleton bones, arrange and scale freely, then export your creation or save it as an outfit.",
+ "priority": 0.9,
+ "changefreq": "weekly",
+ "added": "2026-06-13"
+ },
{
"path": "/validation",
"title": "glTF Validator",
diff --git a/pages/app.html b/pages/app.html
index 6925ab36d..3da48c176 100644
--- a/pages/app.html
+++ b/pages/app.html
@@ -151,6 +151,14 @@
</svg>
<span data-state-label>Deploy on Solana</span>
</a>
+ <a
+ class="open-in-composer-btn"
+ id="open-in-composer-btn"
+ hidden
+ title="Open this model in the Scene Composer to attach accessories or build a scene"
+ >
+ Open in Composer
+ </a>
<a
class="view-public-profile-btn"
id="view-public-profile-btn"
diff --git a/pages/forge-v2.html b/pages/forge-v2.html
index 0d4edea6b..6c9afffcf 100644
--- a/pages/forge-v2.html
+++ b/pages/forge-v2.html
@@ -2251,6 +2251,7 @@
>Split into parts</a
>
<a class="btn btn-ghost" id="download" download>Download GLB</a>
+ <a class="btn btn-ghost" id="open-in-composer" href="/compose" title="Open this model in the Scene Composer to attach it to an avatar or place it in a scene">Open in Composer</a>
<button class="btn" type="button" id="again">Make another</button>
</div>
</div>
diff --git a/public/changelog.json b/public/changelog.json
index cb7641d51..34232e636 100644
--- a/public/changelog.json
+++ b/public/changelog.json
@@ -1,5 +1,5 @@
{
- "generated_at": "2026-06-13T09:31:37.488Z",
+ "generated_at": "2026-06-13T09:36:41.540Z",
"generated_by": "scripts/build-page-index.mjs from data/pages.json + data/changelog.json",
"site": {
"name": "three.ws",
@@ -16,6 +16,16 @@
"launch"
]
},
+ {
+ "date": "2026-06-13",
+ "type": "launch",
+ "title": "Scene Composer",
+ "summary": "Real-time 3D scene composer: forge items from text, attach them to your avatar's skeleton bones, arrange and scale freely, then export your creation or save it as an outfit.",
+ "link": "/compose",
+ "tags": [
+ "launch"
+ ]
+ },
{
"date": "2026-06-13",
"type": "update",
@@ -121,6 +131,16 @@
"infra"
]
},
+ {
+ "date": "2026-06-13",
+ "type": "update",
+ "title": "Scene Composer — forge items in real time and dress your avatar",
+ "summary": "New /compose page lets you build a 3D scene live: load an avatar (or start empty), type a prompt to forge new items, and drop them straight into the scene. Select any object with a click to translate, rotate, or scale it with the gizmo. Items can be attached to specific avatar bones — hat to head, sword to right hand — and the inspector shows live transform values you can also type in. When you're done, export the full scene as a GLB or save an outfit back to your avatar. Your recent forge creations appear in the panel so you can reuse them across sessions without re-generating.",
+ "link": "/compose",
+ "tags": [
+ "feature"
+ ]
+ },
{
"date": "2026-06-13",
"type": "update",
diff --git a/public/changelog.xml b/public/changelog.xml
index 53608d5b5..8e36e6118 100644
--- a/public/changelog.xml
+++ b/public/changelog.xml
@@ -14,6 +14,14 @@
<category>launch</category>
<description>Every three.ws avatar and Forge model has a View in AR button. On mobile it anchors the model to real surfaces through your camera. WebXR on Android, Quick Look on iOS — no app, no download.</description>
</item>
+ <item>
+ <title>Scene Composer</title>
+ <link>https://three.ws/compose</link>
+ <guid isPermaLink="false">2026-06-13:Scene Composer</guid>
+ <pubDate>Sat, 13 Jun 2026 12:00:00 GMT</pubDate>
+ <category>launch</category>
+ <description>Real-time 3D scene composer: forge items from text, attach them to your avatar's skeleton bones, arrange and scale freely, then export your creation or save it as an outfit.</description>
+ </item>
<item>
<title>3D model categories — every model is now typed as Avatar, Accessory, Item, Scene, Creature, or Vehicle</title>
<link>https://three.ws/marketplace</link>
@@ -94,6 +102,14 @@
<category>fix</category>
<description>We traced a batch of production errors to their roots and fixed them. The live activity ticker no longer hammers our rate store on every open tab — one shared reader now fans updates out to everyone on a server and quiets down while the feed is idle, which is what was silently exhausting our request quota and freezing the feed. When a remote token image stalls, you now get a clean, on-brand placeholder — a unique gradient gem per token — instead of a timeout or a broken-image tile. On devices that can't run 3D (some locked-down or headless browsers), the viewer now shows a clear 'preview unavailable' card with a reload option instead of a blank stage. Text-to-3D generations that take a little longer to start no longer get cut off early, and a stray click-handling error on the homepage, search, and glossary is gone.</description>
</item>
+ <item>
+ <title>Scene Composer — forge items in real time and dress your avatar</title>
+ <link>https://three.ws/compose</link>
+ <guid isPermaLink="false">2026-06-13:Scene Composer — forge items in real time and dress your avatar</guid>
+ <pubDate>Sat, 13 Jun 2026 12:00:00 GMT</pubDate>
+ <category>feature</category>
+ <description>New /compose page lets you build a 3D scene live: load an avatar (or start empty), type a prompt to forge new items, and drop them straight into the scene. Select any object with a click to translate, rotate, or scale it with the gizmo. Items can be attached to specific avatar bones — hat to head, sword to right hand — and the inspector shows live transform values you can also type in. When you're done, export the full scene as a GLB or save an outfit back to your avatar. Your recent forge creations appear in the panel so you can reuse them across sessions without re-generating.</description>
+ </item>
<item>
<title>See any Forge model in AR — scan from your desktop, place it in your room</title>
<link>https://three.ws/forge</link>
diff --git a/public/features.json b/public/features.json
index 2364b5166..729d07d0f 100644
--- a/public/features.json
+++ b/public/features.json
@@ -1,5 +1,5 @@
{
- "generated_at": "2026-06-13T09:31:37.107Z",
+ "generated_at": "2026-06-13T09:36:41.523Z",
"generated_by": "scripts/build-page-index.mjs from data/pages.json",
"site": {
"name": "three.ws",
@@ -192,6 +192,16 @@
"tags": [],
"showcase": false
},
+ {
+ "path": "/compose",
+ "title": "Scene Composer",
+ "description": "Real-time 3D scene composer: forge items from text, attach them to your avatar's skeleton bones, arrange and scale freely, then export your creation or save it as an outfit.",
+ "added": "2026-06-13",
+ "auth": null,
+ "indexable": true,
+ "tags": [],
+ "showcase": false
+ },
{
"path": "/validation",
"title": "glTF Validator",
diff --git a/public/llms-full.txt b/public/llms-full.txt
index 7a2b9c3dd..69a29a446 100644
--- a/public/llms-full.txt
+++ b/public/llms-full.txt
@@ -120,6 +120,12 @@ URL: https://three.ws/scene
Full 3D scene editor in the browser — import GLB models, arrange them with transform gizmos, edit materials and lights, and export complete scenes.
+### Scene Composer
+
+URL: https://three.ws/compose
+
+Real-time 3D scene composer: forge items from text, attach them to your avatar's skeleton bones, arrange and scale freely, then export your creation or save it as an outfit.
+
### glTF Validator
URL: https://three.ws/validation
diff --git a/public/llms.txt b/public/llms.txt
index 1f71aaab1..d63553957 100644
--- a/public/llms.txt
+++ b/public/llms.txt
@@ -32,6 +32,7 @@ Create and customize an agent.
- [Describe it to 3D](https://three.ws/create/prompt): Type a description. About a minute later: a rigged 3D avatar you can animate and download.
- [Forge — Text to 3D](https://three.ws/forge): Type a prompt — or drop in photos or a sketch — and get a downloadable textured 3D model (GLB). Pick from multiple generation engines with live health status.
- [Scene Studio](https://three.ws/scene): Full 3D scene editor in the browser — import GLB models, arrange them with transform gizmos, edit materials and lights, and export complete scenes.
+- [Scene Composer](https://three.ws/compose): Real-time 3D scene composer: forge items from text, attach them to your avatar's skeleton bones, arrange and scale freely, then export your creation or save it as an outfit.
- [glTF Validator](https://three.ws/validation): Khronos-spec validator for glTF/GLB files. Paste a URL or upload a model.
- [Widget Studio](https://three.ws/studio): Pick an avatar, configure embed options, copy a one-line snippet for your site.
- [Artifact Viewer](https://three.ws/artifact): Renders Claude artifact bundles as standalone embeddable apps.
diff --git a/public/nav-data.js b/public/nav-data.js
index c8955e55c..8a42272b2 100644
--- a/public/nav-data.js
+++ b/public/nav-data.js
@@ -70,9 +70,14 @@ export const NAV_GROUPS = [
{
title: 'Scene Studio',
href: '/scene',
- badge: 'New',
desc: 'Assemble models into full 3D scenes',
},
+ {
+ title: 'Scene Composer',
+ href: '/compose',
+ badge: 'New',
+ desc: 'Forge items & dress your avatar in real time',
+ },
{
title: 'Voice Lab',
href: '/voice',
diff --git a/public/sitemap/index.html b/public/sitemap/index.html
index 889f66fad..e3e38946b 100644
--- a/public/sitemap/index.html
+++ b/public/sitemap/index.html
@@ -65,7 +65,7 @@
<main class="sm-wrap">
<div class="sm-hero">
<h1>Sitemap</h1>
- <p>All 265 pages on three.ws, grouped by purpose. Looking for the machine-readable versions?</p>
+ <p>All 266 pages on three.ws, grouped by purpose. Looking for the machine-readable versions?</p>
<div class="sm-formats">
<a href="/sitemap.xml"><code>sitemap.xml</code> · for search engines</a>
<a href="/llms.txt"><code>llms.txt</code> · for AI agents</a>
@@ -77,7 +77,7 @@
</div>
<div class="sm-filter">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m21 21-4.3-4.3"/></svg>
- <input id="sm-filter-input" type="search" placeholder="Filter 265 pages by name, path, or description…" aria-label="Filter pages" autocomplete="off" spellcheck="false" />
+ <input id="sm-filter-input" type="search" placeholder="Filter 266 pages by name, path, or description…" aria-label="Filter pages" autocomplete="off" spellcheck="false" />
<kbd aria-hidden="true">/</kbd>
</div>
<p class="sm-filter-count" id="sm-filter-count" role="status" aria-live="polite"></p>
@@ -176,7 +176,7 @@
<section class="sm-section" id="build">
<header>
- <h2>Build<span class="sm-count" data-count>28</span></h2>
+ <h2>Build<span class="sm-count" data-count>29</span></h2>
<p class="sm-section-desc">Create and customize an agent.</p>
</header>
<ul class="sm-list">
@@ -229,6 +229,13 @@
<span class="sm-desc">Full 3D scene editor in the browser — import GLB models, arrange them with transform gizmos, edit materials and lights, and export complete scenes.</span>
</a>
</li>
+ <li>
+ <a href="/compose">
+ <span class="sm-title">Scene Composer</span>
+ <span class="sm-path">/compose</span>
+ <span class="sm-desc">Real-time 3D scene composer: forge items from text, attach them to your avatar's skeleton bones, arrange and scale freely, then export your creation or save it as an outfit.</span>
+ </a>
+ </li>
<li>
<a href="/validation">
<span class="sm-title">glTF Validator</span>
diff --git a/src/app.js b/src/app.js
index d65bcb91a..1fea05c6b 100644
--- a/src/app.js
+++ b/src/app.js
@@ -752,6 +752,18 @@ class App {
btn.hidden = !hasModel;
}
+ _refreshOpenInComposerButton() {
+ const btn = document.getElementById('open-in-composer-btn');
+ if (!btn) return;
+ const url = this._currentModelUrl;
+ if (url) {
+ btn.href = `/compose?glb=${encodeURIComponent(url)}`;
+ btn.hidden = false;
+ } else {
+ btn.hidden = true;
+ }
+ }
+
// Surface the deploy CTA next to the public-profile link in /app. It always
// reads "Deploy on Solana" and hands the user to the on-chain deployment
// agent — the guided flow for putting 3D assets on Solana — never to a bare
@@ -1736,6 +1748,7 @@ class App {
this._currentLocalFile = typeof rootFile !== 'string' ? rootFile : null;
this._refreshMakeWidgetButton();
this._refreshSaveToAccountButton();
+ this._refreshOpenInComposerButton();
// Emit load start
protocol.emit({
diff --git a/src/forge.js b/src/forge.js
index 0a9f1df01..803d86651 100644
--- a/src/forge.js
+++ b/src/forge.js
@@ -80,6 +80,7 @@ const els = {
errorMessage: document.getElementById('error-message'),
forgeShareBtn: document.getElementById('forge-share-btn'),
segmentBtn: document.getElementById('forge-segment-btn'),
+ openInComposer: document.getElementById('open-in-composer'),
categoryPicker: document.getElementById('forge-category-picker'),
savedChip: document.getElementById('result-saved'),
creations: document.getElementById('creations'),
@@ -1267,6 +1268,7 @@ function showResult(glbUrl, label, meta, { autoSaved = false } = {}) {
);
// Cross-link into Parts Studio with this exact model pre-loaded.
if (els.segmentBtn) els.segmentBtn.href = `/segment?mesh=${encodeURIComponent(glbUrl)}`;
+ if (els.openInComposer) els.openInComposer.href = `/compose?glb=${encodeURIComponent(glbUrl)}`;
// Offer "Refine" when a higher tier exists and this job can be re-run. Use the
// tier the result was actually produced at (meta), falling back to the current
// selection for re-opened gallery models that don't carry tier metadata.
diff --git a/src/scene-compose.js b/src/scene-compose.js
new file mode 100644
index 000000000..634e0708b
--- /dev/null
+++ b/src/scene-compose.js
@@ -0,0 +1,984 @@
+/**
+ * Scene Composer — real-time 3D scene editor with Forge integration.
+ *
+ * Architecture:
+ * - Single Three.js scene (NOT reusing viewer.js — different multi-object lifecycle)
+ * - SceneObjects Map: id → { mesh, name, glbUrl, visible, boneAttached, boneName }
+ * - TransformControls for gizmo; OrbitControls for camera; both co-exist cleanly
+ * - Forge panel: prompt → POST /api/forge → poll /api/forge?job= → GLB into scene
+ * - Creations gallery: /api/forge-gallery lists prior creations to drag-in
+ * - Save: GLTFExporter for GLB export; PATCH /api/avatars/:id for outfit attach
+ */
+
+import * as THREE from 'three';
+import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
+import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js';
+import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
+import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js';
+import { RoomEnvironment } from 'three/examples/jsm/environments/RoomEnvironment.js';
+
+// ── DOM refs ──────────────────────────────────────────────────────────────────
+const canvasEl = document.getElementById('compose-canvas');
+const loadingEl = document.getElementById('canvas-loading');
+const loadingMsgEl = document.getElementById('loading-msg');
+const avatarPromptEl = document.getElementById('avatar-prompt');
+const canvasHintEl = document.getElementById('canvas-hint');
+const objectListEl = document.getElementById('object-list');
+const inspectorEl = document.getElementById('inspector');
+const saveStatusEl = document.getElementById('save-status');
+
+// Topbar
+const btnTranslate = document.getElementById('btn-translate');
+const btnRotate = document.getElementById('btn-rotate');
+const btnScale = document.getElementById('btn-scale');
+const btnSpace = document.getElementById('btn-space');
+const spaceLabel = document.getElementById('space-label');
+const btnExport = document.getElementById('btn-export-glb');
+const btnSaveOutfit = document.getElementById('btn-save-outfit');
+
+// Forge panel
+const forgePromptEl = document.getElementById('forge-prompt');
+const forgeBtn = document.getElementById('forge-btn');
+const forgeProgress = document.getElementById('forge-progress');
+const forgeProgressFill = document.getElementById('forge-progress-fill');
+const forgeProgressMsg = document.getElementById('forge-progress-msg');
+const forgeError = document.getElementById('forge-error');
+const creationsListEl = document.getElementById('creations-list');
+const dropZone = document.getElementById('drop-zone');
+const glbFileInput = document.getElementById('glb-file-input');
+
+// Avatar load
+const avatarUrlInput = document.getElementById('avatar-url-input');
+const btnLoadUrl = document.getElementById('btn-load-url');
+const btnBrowseAvatars = document.getElementById('btn-browse-avatars');
+const btnSkipAvatar = document.getElementById('btn-skip-avatar');
+const btnLoadSmall = document.getElementById('btn-load-avatar-small');
+const avatarModal = document.getElementById('avatar-modal');
+const avatarModalClose = document.getElementById('avatar-modal-close');
+const avatarModalBody = document.getElementById('avatar-modal-body');
+
+// ── Client identity (auth-free per CLAUDE.md / forge pattern) ─────────────────
+function getClientKey() {
+ let k = localStorage.getItem('forge_client_key');
+ if (!k) { k = crypto.randomUUID(); localStorage.setItem('forge_client_key', k); }
+ return k;
+}
+const CLIENT_KEY = getClientKey();
+const CLIENT_HEADERS = { 'x-forge-client': CLIENT_KEY };
+
+// ── Scene state ───────────────────────────────────────────────────────────────
+const sceneObjects = new Map(); // id → { group, name, glbUrl, visible, boneAttached, boneName }
+let nextId = 1;
+let selectedId = null;
+let avatarId = null; // object id of the loaded avatar
+let avatarBones = []; // { name, bone } from skeleton
+let transformMode = 'translate'; // 'translate' | 'rotate' | 'scale'
+let transformSpace = 'world'; // 'world' | 'local'
+
+// ── Three.js setup ────────────────────────────────────────────────────────────
+const renderer = new THREE.WebGLRenderer({ canvas: canvasEl, antialias: true });
+renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
+renderer.toneMapping = THREE.ACESFilmicToneMapping;
+renderer.toneMappingExposure = 1;
+renderer.shadowMap.enabled = true;
+renderer.shadowMap.type = THREE.PCFSoftShadowMap;
+renderer.outputColorSpace = THREE.SRGBColorSpace;
+
+const scene = new THREE.Scene();
+scene.background = new THREE.Color(0x07070f);
+
+// Environment
+const pmremGenerator = new THREE.PMREMGenerator(renderer);
+scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture;
+pmremGenerator.dispose();
+
+// Grid helper
+const grid = new THREE.GridHelper(20, 40, 0x222233, 0x111122);
+grid.material.opacity = 0.6;
+grid.material.transparent = true;
+scene.add(grid);
+
+// Camera
+const camera = new THREE.PerspectiveCamera(45, 1, 0.01, 1000);
+camera.position.set(0, 1.5, 3.5);
+camera.lookAt(0, 1, 0);
+
+// OrbitControls
+const orbit = new OrbitControls(camera, renderer.domElement);
+orbit.target.set(0, 1, 0);
+orbit.enableDamping = true;
+orbit.dampingFactor = 0.08;
+orbit.minDistance = 0.2;
+orbit.maxDistance = 50;
+
+// TransformControls
+const transform = new TransformControls(camera, renderer.domElement);
+transform.setMode(transformMode);
+transform.setSpace(transformSpace);
+scene.add(transform);
+
+// Pause orbit while dragging the gizmo
+transform.addEventListener('dragging-changed', (e) => {
+ orbit.enabled = !e.value;
+ if (!e.value) updateInspector(); // sync inspector after drag
+});
+
+// Light
+const ambient = new THREE.AmbientLight(0xffffff, 0.6);
+scene.add(ambient);
+const dirLight = new THREE.DirectionalLight(0xffffff, 1.2);
+dirLight.position.set(3, 8, 5);
+dirLight.castShadow = true;
+dirLight.shadow.mapSize.set(1024, 1024);
+scene.add(dirLight);
+
+// ── Resize ────────────────────────────────────────────────────────────────────
+function resize() {
+ const wrap = canvasEl.parentElement;
+ const w = wrap.clientWidth;
+ const h = wrap.clientHeight;
+ renderer.setSize(w, h, false);
+ camera.aspect = w / h;
+ camera.updateProjectionMatrix();
+}
+new ResizeObserver(resize).observe(canvasEl.parentElement);
+resize();
+
+// ── Render loop ───────────────────────────────────────────────────────────────
+function animate() {
+ requestAnimationFrame(animate);
+ orbit.update();
+ renderer.render(scene, camera);
+}
+animate();
+
+// ── Loading helpers ───────────────────────────────────────────────────────────
+function showLoading(msg = 'Loading…') {
+ loadingMsgEl.textContent = msg;
+ loadingEl.classList.remove('hidden');
+}
+function hideLoading() {
+ loadingEl.classList.add('hidden');
+}
+
+// ── GLB loader ────────────────────────────────────────────────────────────────
+const gltfLoader = new GLTFLoader();
+
+function loadGLB(url, name, opts = {}) {
+ return new Promise((resolve, reject) => {
+ gltfLoader.load(
+ url,
+ (gltf) => {
+ const group = gltf.scene;
+ group.traverse((n) => {
+ if (n.isMesh) {
+ n.castShadow = true;
+ n.receiveShadow = true;
+ }
+ });
+ // Auto-fit: center at origin, scale if huge
+ const box = new THREE.Box3().setFromObject(group);
+ const size = new THREE.Vector3();
+ box.getSize(size);
+ const maxDim = Math.max(size.x, size.y, size.z);
+ if (!opts.preserveTransform) {
+ const center = new THREE.Vector3();
+ box.getCenter(center);
+ group.position.sub(center);
+ group.position.y += size.y / 2; // stand on ground plane
+ if (maxDim > 4) {
+ const s = 2 / maxDim;
+ group.scale.setScalar(s);
+ }
+ }
+ // Collect bones if this is an avatar
+ const bones = [];
+ group.traverse((n) => {
+ if (n.isBone) bones.push({ name: n.name, bone: n });
+ });
+ resolve({ group, gltf, bones });
+ },
+ undefined,
+ reject,
+ );
+ });
+}
+
+// ── Scene object registry ─────────────────────────────────────────────────────
+function addObjectToScene(group, name, glbUrl, role = 'item') {
+ const id = nextId++;
+ scene.add(group);
+ sceneObjects.set(id, { group, name, glbUrl, visible: true, boneAttached: false, boneName: null, role });
+ renderObjectList();
+ selectObject(id);
+ return id;
+}
+
+function removeObject(id) {
+ const obj = sceneObjects.get(id);
+ if (!obj) return;
+ if (selectedId === id) deselect();
+ // Detach from bone if attached
+ if (obj.boneAttached && obj.boneName) {
+ const boneEntry = avatarBones.find((b) => b.name === obj.boneName);
+ if (boneEntry) {
+ scene.attach(obj.group);
+ }
+ }
+ scene.remove(obj.group);
+ sceneObjects.delete(id);
+ if (id === avatarId) {
+ avatarId = null;
+ avatarBones = [];
+ }
+ renderObjectList();
+ if (selectedId === null) inspectorEl.innerHTML = '<span class="empty-msg">Select an object to inspect</span>';
+}
+
+function toggleVisibility(id) {
+ const obj = sceneObjects.get(id);
+ if (!obj) return;
+ obj.visible = !obj.visible;
+ obj.group.visible = obj.visible;
+ renderObjectList();
+}
+
+// ── Selection ─────────────────────────────────────────────────────────────────
+function selectObject(id) {
+ selectedId = id;
+ const obj = sceneObjects.get(id);
+ if (!obj) { deselect(); return; }
+ transform.attach(obj.group);
+ renderObjectList();
+ updateInspector();
+}
+
+function deselect() {
+ selectedId = null;
+ transform.detach();
+ renderObjectList();
+ inspectorEl.className = 'empty';
+ inspectorEl.innerHTML = '<span class="empty-msg">Select an object to inspect</span>';
+}
+
+// Click-to-select on canvas
+const raycaster = new THREE.Raycaster();
+const mouse = new THREE.Vector2();
+canvasEl.addEventListener('pointerdown', (e) => {
+ if (e.button !== 0) return;
+ // Ignore if clicking the gizmo itself
+ if (transform.dragging) return;
+ const rect = canvasEl.getBoundingClientRect();
+ mouse.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
+ mouse.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
+ raycaster.setFromCamera(mouse, camera);
+ // Build meshes list, skip gizmo
+ const meshes = [];
+ sceneObjects.forEach((obj, id) => {
+ if (!obj.visible) return;
+ obj.group.traverse((n) => { if (n.isMesh) meshes.push({ mesh: n, id }); });
+ });
+ const hits = raycaster.intersectObjects(meshes.map((m) => m.mesh));
+ if (hits.length) {
+ const hitMesh = hits[0].object;
+ const entry = meshes.find((m) => m.mesh === hitMesh);
+ if (entry) selectObject(entry.id);
+ } else {
+ deselect();
+ }
+});
+
+// ── Transform mode ────────────────────────────────────────────────────────────
+function setMode(mode) {
+ transformMode = mode;
+ transform.setMode(mode);
+ [btnTranslate, btnRotate, btnScale].forEach((b) => b.classList.remove('active'));
+ ({ translate: btnTranslate, rotate: btnRotate, scale: btnScale })[mode]?.classList.add('active');
+}
+
+btnTranslate.addEventListener('click', () => setMode('translate'));
+btnRotate.addEventListener('click', () => setMode('rotate'));
+btnScale.addEventListener('click', () => setMode('scale'));
+
+btnSpace.addEventListener('click', () => {
+ transformSpace = transformSpace === 'world' ? 'local' : 'world';
+ transform.setSpace(transformSpace);
+ spaceLabel.textContent = transformSpace === 'world' ? 'World' : 'Local';
+});
+
+document.addEventListener('keydown', (e) => {
+ if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
+ if (e.key === 'w' || e.key === 'W') setMode('translate');
+ if (e.key === 'e' || e.key === 'E') setMode('rotate');
+ if (e.key === 'r' || e.key === 'R') setMode('scale');
+ if (e.key === 'Escape') deselect();
+ if ((e.key === 'Delete' || e.key === 'Backspace') && selectedId !== null) {
+ if (e.target === document.body) { e.preventDefault(); removeObject(selectedId); }
+ }
+});
+
+// ── Object list (left panel) ──────────────────────────────────────────────────
+function renderObjectList() {
+ objectListEl.innerHTML = '';
+ sceneObjects.forEach((obj, id) => {
+ const row = document.createElement('div');
+ row.className = 'obj-row' + (id === selectedId ? ' selected' : '');
+ row.dataset.id = id;
+
+ const icon = document.createElement('span');
+ icon.className = 'obj-icon';
+ icon.textContent = iconForRole(obj.role);
+ row.appendChild(icon);
+
+ const nameEl = document.createElement('span');
+ nameEl.className = 'obj-name';
+ nameEl.textContent = obj.name;
+ row.appendChild(nameEl);
+
+ if (obj.role) {
+ const roleEl = document.createElement('span');
+ roleEl.className = 'obj-role' + (obj.boneAttached ? ' attached' : '');
+ roleEl.textContent = obj.boneAttached ? `↑ ${obj.boneName?.split(/[_:]/).pop() || 'bone'}` : obj.role;
+ row.appendChild(roleEl);
+ }
+
+ const visBtn = document.createElement('button');
+ visBtn.className = 'obj-vis';
+ visBtn.title = obj.visible ? 'Hide' : 'Show';
+ visBtn.textContent = obj.visible ? '●' : '○';
+ visBtn.addEventListener('click', (e) => { e.stopPropagation(); toggleVisibility(id); });
+ row.appendChild(visBtn);
+
+ const delBtn = document.createElement('button');
+ delBtn.className = 'obj-del';
+ delBtn.title = 'Remove';
+ delBtn.textContent = '×';
+ delBtn.addEventListener('click', (e) => { e.stopPropagation(); removeObject(id); });
+ row.appendChild(delBtn);
+
+ row.addEventListener('click', () => selectObject(id));
+ objectListEl.appendChild(row);
+ });
+
+ if (sceneObjects.size === 0) {
+ const empty = document.createElement('div');
+ empty.style.cssText = 'padding:16px 12px;font-size:11px;color:#52525b;';
+ empty.textContent = 'No objects yet. Load an avatar or forge an item.';
+ objectListEl.appendChild(empty);
+ }
+}
+
+function iconForRole(role) {
+ return { avatar: '◉', accessory: '◈', item: '◇', scene: '▦', creature: '◆', vehicle: '▷', other: '○' }[role] || '◇';
+}
+
+// ── Inspector ─────────────────────────────────────────────────────────────────
+function updateInspector() {
+ const obj = sceneObjects.get(selectedId);
+ if (!obj) { deselect(); return; }
+
+ inspectorEl.className = '';
+ const g = obj.group;
+
+ const fmt = (v) => v.toFixed(3);
+
+ let html = `
+ <div class="insp-section">Position</div>
+ <div class="xform-row">
+ <label>X</label>
+ <input class="xform-input" data-axis="px" value="${fmt(g.position.x)}" />
+ <input class="xform-input" data-axis="py" value="${fmt(g.position.y)}" />
+ <input class="xform-input" data-axis="pz" value="${fmt(g.position.z)}" />
+ </div>
+ <div class="insp-section">Rotation (°)</div>
+ <div class="xform-row">
+ <label>R</label>
+ <input class="xform-input" data-axis="rx" value="${fmt(THREE.MathUtils.radToDeg(g.rotation.x))}" />
+ <input class="xform-input" data-axis="ry" value="${fmt(THREE.MathUtils.radToDeg(g.rotation.y))}" />
+ <input class="xform-input" data-axis="rz" value="${fmt(THREE.MathUtils.radToDeg(g.rotation.z))}" />
+ </div>
+ <div class="insp-section">Scale</div>
+ <div class="xform-row">
+ <label>S</label>
+ <input class="xform-input" data-axis="sx" value="${fmt(g.scale.x)}" />
+ <input class="xform-input" data-axis="sy" value="${fmt(g.scale.y)}" />
+ <input class="xform-input" data-axis="sz" value="${fmt(g.scale.z)}" />
+ </div>
+ `;
+
+ // Bone attach section — only for non-avatar items when an avatar with bones is loaded
+ if (obj.role !== 'avatar' && avatarId !== null && avatarBones.length > 0) {
+ const boneOptions = avatarBones
+ .map((b) => `<option value="${b.name}" ${obj.boneName === b.name ? 'selected' : ''}>${cleanBoneName(b.name)}</option>`)
+ .join('');
+ html += `
+ <div class="attach-section">
+ <div class="attach-label">Attach to Bone</div>
+ <div class="attach-controls">
+ <select class="bone-select" id="bone-select">${boneOptions}</select>
+ ${obj.boneAttached
+ ? `<button class="detach-btn" id="detach-btn">Detach</button>`
+ : `<button class="attach-btn" id="attach-btn">Attach</button>`
+ }
+ </div>
+ </div>
+ `;
+ }
+
+ inspectorEl.innerHTML = html;
+
+ // Wire inspector inputs
+ inspectorEl.querySelectorAll('.xform-input').forEach((input) => {
+ input.addEventListener('change', () => {
+ const v = parseFloat(input.value);
+ if (!isFinite(v)) return;
+ const axis = input.dataset.axis;
+ const o = sceneObjects.get(selectedId)?.group;
+ if (!o) return;
+ if (axis === 'px') o.position.x = v;
+ else if (axis === 'py') o.position.y = v;
+ else if (axis === 'pz') o.position.z = v;
+ else if (axis === 'rx') o.rotation.x = THREE.MathUtils.degToRad(v);
+ else if (axis === 'ry') o.rotation.y = THREE.MathUtils.degToRad(v);
+ else if (axis === 'rz') o.rotation.z = THREE.MathUtils.degToRad(v);
+ else if (axis === 'sx') o.scale.x = v;
+ else if (axis === 'sy') o.scale.y = v;
+ else if (axis === 'sz') o.scale.z = v;
+ });
+ });
+
+ document.getElementById('attach-btn')?.addEventListener('click', () => {
+ const boneName = document.getElementById('bone-select')?.value;
+ if (boneName) attachToBone(selectedId, boneName);
+ });
+ document.getElementById('detach-btn')?.addEventListener('click', () => {
+ detachFromBone(selectedId);
+ });
+}
+
+function cleanBoneName(name) {
+ return name
+ .replace(/^mixamorig:?/i, '')
+ .replace(/^CC_Base_/i, '')
+ .replace(/^rig_/i, '')
+ .replace(/_/g, ' ');
+}
+
+// ── Bone attachment ───────────────────────────────────────────────────────────
+function attachToBone(itemId, boneName) {
+ const item = sceneObjects.get(itemId);
+ const boneEntry = avatarBones.find((b) => b.name === boneName);
+ if (!item || !boneEntry) return;
+
+ // Detach from previous bone first
+ if (item.boneAttached) detachFromBone(itemId, false);
+
+ // Re-attach: world position preserved, then reparented to bone
+ scene.remove(item.group);
+ boneEntry.bone.add(item.group);
+ item.group.position.set(0, 0, 0); // reset local offset after attach
+ item.group.rotation.set(0, 0, 0);
+ item.boneAttached = true;
+ item.boneName = boneName;
+
+ renderObjectList();
+ updateInspector();
+}
+
+function detachFromBone(itemId, updateUI = true) {
+ const item = sceneObjects.get(itemId);
+ if (!item || !item.boneAttached) return;
+ const boneEntry = avatarBones.find((b) => b.name === item.boneName);
+ if (boneEntry) {
+ // scene.attach preserves world transform
+ scene.attach(item.group);
+ }
+ item.boneAttached = false;
+ item.boneName = null;
+ if (updateUI) {
+ renderObjectList();
+ updateInspector();
+ }
+}
+
+// ── Avatar loader ─────────────────────────────────────────────────────────────
+async function loadAvatar(url, name = 'Avatar') {
+ showLoading(`Loading avatar…`);
+ avatarPromptEl.classList.add('hidden');
+ canvasHintEl.classList.remove('hidden');
+ try {
+ const { group, bones } = await loadGLB(url, name);
+ // Remove existing avatar if any
+ if (avatarId !== null) removeObject(avatarId);
+ const id = addObjectToScene(group, name, url, 'avatar');
+ avatarId = id;
+ avatarBones = bones;
+
+ // Frame the avatar
+ const box = new THREE.Box3().setFromObject(group);
+ const center = new THREE.Vector3();
+ box.getCenter(center);
+ const size = new THREE.Vector3();
+ box.getSize(size);
+ orbit.target.copy(center);
+ camera.position.set(center.x, center.y + size.y * 0.3, center.z + size.y * 1.4);
+ orbit.update();
+ } catch (err) {
+ console.error('Avatar load failed:', err);
+ avatarPromptEl.classList.remove('hidden');
+ showError(`Failed to load avatar: ${err.message}`);
+ } finally {
+ hideLoading();
+ }
+}
+
+// ── Avatar prompt handlers ────────────────────────────────────────────────────
+btnLoadUrl.addEventListener('click', () => {
+ const url = avatarUrlInput.value.trim();
+ if (!url) return;
+ loadAvatar(url, 'Avatar');
+});
+avatarUrlInput.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter') btnLoadUrl.click();
+});
+
+btnSkipAvatar.addEventListener('click', () => {
+ avatarPromptEl.classList.add('hidden');
+ canvasHintEl.classList.remove('hidden');
+ hideLoading();
+});
+
+btnLoadSmall.addEventListener('click', () => {
+ avatarPromptEl.classList.remove('hidden');
+});
+
+btnBrowseAvatars.addEventListener('click', openAvatarModal);
+avatarModalClose.addEventListener('click', closeAvatarModal);
+avatarModal.addEventListener('click', (e) => { if (e.target === avatarModal) closeAvatarModal(); });
+
+async function openAvatarModal() {
+ avatarModal.hidden = false;
+ avatarModalBody.innerHTML = '<div style="grid-column:1/-1;text-align:center;padding:20px;color:#52525b;font-size:12px;">Loading…</div>';
+ try {
+ const res = await fetch(`/api/explore?type=avatar&limit=20`);
+ const data = await res.json().catch(() => ({}));
+ const avatars = data.avatars || data.items || [];
+ if (!avatars.length) {
+ avatarModalBody.innerHTML = '<div style="grid-column:1/-1;text-align:center;padding:20px;color:#52525b;font-size:12px;">No avatars found. Create one in the <a href="/forge" style="color:#a5b4fc;">Forge</a>.</div>';
+ return;
+ }
+ avatarModalBody.innerHTML = '';
+ for (const av of avatars) {
+ if (!av.glbUrl && !av.glb_url) continue;
+ const glbUrl = av.glbUrl || av.glb_url;
+ const card = document.createElement('div');
+ card.style.cssText = 'aspect-ratio:1;border-radius:8px;overflow:hidden;cursor:pointer;background:rgba(255,255,255,0.04);border:1px solid rgba(255,255,255,0.08);position:relative;transition:border-color 0.15s,transform 0.12s;';
+ card.addEventListener('mouseenter', () => { card.style.borderColor = 'rgba(99,102,241,0.5)'; card.style.transform = 'scale(1.03)'; });
+ card.addEventListener('mouseleave', () => { card.style.borderColor = ''; card.style.transform = ''; });
+ if (av.thumbnailUrl || av.thumbnail_url) {
+ const img = document.createElement('img');
+ img.src = av.thumbnailUrl || av.thumbnail_url;
+ img.style.cssText = 'width:100%;height:100%;object-fit:cover;';
+ img.alt = av.name || 'Avatar';
+ card.appendChild(img);
+ } else {
+ card.style.display = 'flex';
+ card.style.alignItems = 'center';
+ card.style.justifyContent = 'center';
+ card.innerHTML = '<span style="font-size:28px;opacity:0.3;">◉</span>';
+ }
+ const label = document.createElement('div');
+ label.style.cssText = 'position:absolute;bottom:0;left:0;right:0;padding:4px 6px;background:rgba(0,0,0,0.6);font-size:10px;color:#fff;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;';
+ label.textContent = av.name || 'Avatar';
+ card.appendChild(label);
+ card.addEventListener('click', () => {
+ closeAvatarModal();
+ loadAvatar(glbUrl, av.name || 'Avatar');
+ });
+ avatarModalBody.appendChild(card);
+ }
+ } catch {
+ avatarModalBody.innerHTML = '<div style="grid-column:1/-1;text-align:center;padding:20px;color:#f87171;font-size:12px;">Failed to load avatars.</div>';
+ }
+}
+
+function closeAvatarModal() {
+ avatarModal.hidden = true;
+}
+
+// ── ?avatar= / ?glb= URL params — auto-load ───────────────────────────────────
+(async () => {
+ const params = new URLSearchParams(location.search);
+ // ?glb=<url> — preload a forged GLB item (e.g. arriving from /forge)
+ const glbParam = params.get('glb');
+ if (glbParam) {
+ avatarPromptEl.classList.add('hidden');
+ canvasHintEl.classList.remove('hidden');
+ showLoading('Loading item…');
+ try {
+ const { group } = await loadGLB(glbParam, 'Forged item');
+ addObjectToScene(group, 'Forged item', glbParam, 'item');
+ } catch (err) {
+ showError(`Failed to load: ${err.message}`);
+ } finally {
+ hideLoading();
+ }
+ }
+ const avatarParam = params.get('avatar');
+ if (avatarParam) {
+ // Could be a URL or an avatar ID — try as URL first, then fetch by ID
+ if (avatarParam.startsWith('http')) {
+ loadAvatar(avatarParam, 'Avatar');
+ } else {
+ showLoading('Fetching avatar…');
+ try {
+ const res = await fetch(`/api/avatars/${encodeURIComponent(avatarParam)}`);
+ const data = await res.json().catch(() => ({}));
+ const glbUrl = data.glbUrl || data.glb_url;
+ if (glbUrl) {
+ await loadAvatar(glbUrl, data.name || 'Avatar');
+ } else {
+ hideLoading();
+ }
+ } catch {
+ hideLoading();
+ }
+ }
+ } else {
+ hideLoading();
+ }
+})();
+
+// ── Forge integration ─────────────────────────────────────────────────────────
+let forgeJobId = null;
+let forgePollTimer = null;
+let forgePollStart = 0;
+const FORGE_TIMEOUT_MS = 5 * 60 * 1000; // 5 min
+
+function setForgeState(busy) {
+ forgeBtn.disabled = busy;
+ forgeBtn.classList.toggle('busy', busy);
+ forgePromptEl.disabled = busy;
+}
+
+function showForgeProgress(msg, pct) {
+ forgeProgress.classList.add('visible');
+ forgeProgressMsg.textContent = msg;
+ forgeProgressFill.style.width = `${pct}%`;
+}
+
+function hideForgeProgress() {
+ forgeProgress.classList.remove('visible');
+}
+
+function showError(msg) {
+ forgeError.textContent = msg;
+ forgeError.classList.add('visible');
+ setTimeout(() => forgeError.classList.remove('visible'), 6000);
+}
+
+forgeBtn.addEventListener('click', startForge);
+forgePromptEl.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) startForge();
+});
+
+async function startForge() {
+ const prompt = forgePromptEl.value.trim();
+ if (!prompt) { forgePromptEl.focus(); return; }
+ if (forgeBtn.disabled) return;
+
+ setForgeState(true);
+ hideForgeProgress();
+ forgeError.classList.remove('visible');
+
+ try {
+ const res = await fetch('/api/forge', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json', ...CLIENT_HEADERS },
+ body: JSON.stringify({ prompt }),
+ });
+ const data = await res.json().catch(() => ({}));
+ if (!res.ok) throw new Error(data.error || `HTTP ${res.status}`);
+
+ if (data.status === 'done' && data.glb_url) {
+ // Instant result (cached / fast backend)
+ await addForgedItem(data, prompt);
+ setForgeState(false);
+ return;
+ }
+ if (!data.job_id) throw new Error('No job ID returned');
+
+ forgeJobId = data.job_id;
+ forgePollStart = Date.now();
+ showForgeProgress('Generating…', 10);
+ pollForge();
+ } catch (err) {
+ setForgeState(false);
+ hideForgeProgress();
+ showError(`Forge failed: ${err.message}`);
+ }
+}
+
+function pollForge() {
+ clearTimeout(forgePollTimer);
+ if (!forgeJobId) return;
+
+ if (Date.now() - forgePollStart > FORGE_TIMEOUT_MS) {
+ setForgeState(false);
+ hideForgeProgress();
+ showError('Generation timed out. Try again.');
+ forgeJobId = null;
+ return;
+ }
+
+ forgePollTimer = setTimeout(async () => {
+ try {
+ const res = await fetch(`/api/forge?job=${encodeURIComponent(forgeJobId)}`, {
+ headers: CLIENT_HEADERS,
+ });
+ const data = await res.json().catch(() => ({}));
+
+ if (data.status === 'done' && data.glb_url) {
+ const elapsed = (Date.now() - forgePollStart) / 1000;
+ showForgeProgress(`Done in ${elapsed.toFixed(0)}s`, 100);
+ const prompt = forgePromptEl.value.trim();
+ await addForgedItem(data, prompt);
+ setTimeout(() => { hideForgeProgress(); setForgeState(false); }, 1000);
+ forgeJobId = null;
+ loadCreationsGallery();
+ } else if (data.status === 'failed') {
+ throw new Error(data.error || 'Generation failed');
+ } else {
+ // Still processing
+ const elapsed = Date.now() - forgePollStart;
+ const pct = Math.min(10 + (elapsed / FORGE_TIMEOUT_MS) * 80, 90);
+ const stages = ['Initializing…', 'Processing image…', 'Building mesh…', 'Generating textures…', 'Finalizing…'];
+ const stage = stages[Math.min(Math.floor(pct / 20), stages.length - 1)];
+ showForgeProgress(stage, pct);
+ pollForge();
+ }
+ } catch (err) {
+ setForgeState(false);
+ hideForgeProgress();
+ showError(`Error: ${err.message}`);
+ forgeJobId = null;
+ }
+ }, 3000);
+}
+
+async function addForgedItem(data, prompt) {
+ const url = data.glb_url;
+ if (!url) return;
+ showLoading('Adding to scene…');
+ try {
+ const { group } = await loadGLB(url, prompt.slice(0, 40));
+ // Offset items slightly so they don't stack on top of each other
+ const count = sceneObjects.size;
+ group.position.x = (count % 4) * 0.6 - 0.9;
+ group.position.z = Math.floor(count / 4) * 0.6;
+ addObjectToScene(group, prompt.slice(0, 40), url, data.model_category || 'item');
+ } catch (err) {
+ showError(`Failed to load GLB: ${err.message}`);
+ } finally {
+ hideLoading();
+ }
+}
+
+// ── Creations gallery (right panel) ──────────────────────────────────────────
+async function loadCreationsGallery() {
+ try {
+ const res = await fetch('/api/forge-gallery?limit=20', { headers: CLIENT_HEADERS });
+ const data = await res.json().catch(() => ({}));
+ const creations = data.creations || [];
+ if (!creations.length) {
+ creationsListEl.innerHTML = '<div class="creations-empty">Forge your first item above</div>';
+ return;
+ }
+ creationsListEl.innerHTML = '';
+ for (const c of creations) {
+ if (!c.glb_url) continue;
+ const card = document.createElement('div');
+ card.className = 'creation-card';
+ card.title = c.prompt || 'Forged item';
+
+ if (c.preview_image_url) {
+ const img = document.createElement('img');
+ img.src = c.preview_image_url;
+ img.alt = c.prompt || '';
+ img.loading = 'lazy';
+ img.onerror = () => applyGradientToCard(card, c.prompt);
+ card.appendChild(img);
+ } else {
+ const div = document.createElement('div');
+ div.className = 'card-gradient';
+ div.textContent = c.prompt || 'Forged item';
+ div.style.background = promptGradient(c.prompt);
+ card.appendChild(div);
+ }
+
+ const overlay = document.createElement('div');
+ overlay.className = 'card-overlay';
+ const promptEl = document.createElement('div');
+ promptEl.className = 'card-prompt';
+ promptEl.textContent = c.prompt || 'Forged item';
+ const addEl = document.createElement('div');
+ addEl.className = 'card-add';
+ addEl.textContent = '+ Add to scene';
+ overlay.appendChild(promptEl);
+ overlay.appendChild(addEl);
+ card.appendChild(overlay);
+
+ card.addEventListener('click', async () => {
+ showLoading('Loading…');
+ try {
+ const { group } = await loadGLB(c.glb_url, (c.prompt || 'item').slice(0, 40));
+ const count = sceneObjects.size;
+ group.position.x = (count % 4) * 0.6 - 0.9;
+ group.position.z = Math.floor(count / 4) * 0.6;
+ addObjectToScene(group, (c.prompt || 'item').slice(0, 40), c.glb_url, c.model_category || 'item');
+ } catch (err) {
+ showError(`Failed to load: ${err.message}`);
+ } finally {
+ hideLoading();
+ }
+ });
+
+ creationsListEl.appendChild(card);
+ }
+ } catch {
+ creationsListEl.innerHTML = '<div class="creations-empty">Could not load creations</div>';
+ }
+}
+
+function promptGradient(str) {
+ let h = 0;
+ for (let i = 0; i < (str || '').length; i++) h = (Math.imul(31, h) + str.charCodeAt(i)) | 0;
+ const hue = Math.abs(h) % 360;
+ return `linear-gradient(135deg, hsl(${hue},28%,14%) 0%, hsl(${(hue+40)%360},22%,10%) 100%)`;
+}
+
+function applyGradientToCard(card, prompt) {
+ const existing = card.querySelector('img');
+ if (existing) existing.remove();
+ const div = document.createElement('div');
+ div.className = 'card-gradient';
+ div.textContent = prompt || 'Forged item';
+ div.style.background = promptGradient(prompt);
+ card.prepend(div);
+}
+
+loadCreationsGallery();
+
+// ── File drop / GLB upload ────────────────────────────────────────────────────
+dropZone.addEventListener('dragover', (e) => { e.preventDefault(); dropZone.classList.add('drag-over'); });
+dropZone.addEventListener('dragleave', () => dropZone.classList.remove('drag-over'));
+dropZone.addEventListener('drop', (e) => {
+ e.preventDefault();
+ dropZone.classList.remove('drag-over');
+ const file = e.dataTransfer?.files?.[0];
+ if (file) loadFileGLB(file);
+});
+glbFileInput.addEventListener('change', () => {
+ const file = glbFileInput.files?.[0];
+ if (file) loadFileGLB(file);
+});
+
+function loadFileGLB(file) {
+ const url = URL.createObjectURL(file);
+ const name = file.name.replace(/\.(glb|gltf)$/i, '');
+ showLoading(`Loading ${file.name}…`);
+ loadGLB(url, name)
+ .then(({ group }) => {
+ addObjectToScene(group, name, url, 'item');
+ })
+ .catch((err) => showError(`Failed to load file: ${err.message}`))
+ .finally(() => hideLoading());
+}
+
+// ── Export ────────────────────────────────────────────────────────────────────
+btnExport.addEventListener('click', exportScene);
+
+async function exportScene() {
+ if (sceneObjects.size === 0) { showError('Nothing to export.'); return; }
+ showLoading('Exporting…');
+ try {
+ const exporter = new GLTFExporter();
+ const exportGroup = new THREE.Group();
+ sceneObjects.forEach((obj) => {
+ if (obj.visible && !obj.boneAttached) exportGroup.add(obj.group.clone());
+ });
+ const glb = await new Promise((resolve, reject) => {
+ exporter.parse(exportGroup, resolve, reject, { binary: true });
+ });
+ const blob = new Blob([glb], { type: 'model/gltf-binary' });
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = 'scene-compose.glb';
+ a.click();
+ URL.revokeObjectURL(url);
+ flashSaveStatus('Exported');
+ } catch (err) {
+ showError(`Export failed: ${err.message}`);
+ } finally {
+ hideLoading();
+ }
+}
+
+// ── Save outfit ───────────────────────────────────────────────────────────────
+btnSaveOutfit.addEventListener('click', saveOutfit);
+
+async function saveOutfit() {
+ if (avatarId === null) { showError('Load an avatar first.'); return; }
+ const avatarObj = sceneObjects.get(avatarId);
+ if (!avatarObj) return;
+
+ // Collect attached items
+ const attachedItems = [];
+ sceneObjects.forEach((obj, id) => {
+ if (id !== avatarId && obj.boneAttached) {
+ attachedItems.push({ bone: obj.boneName, glbUrl: obj.glbUrl, name: obj.name });
+ }
+ });
+
+ if (!attachedItems.length) {
+ showError('Attach at least one item to a bone before saving.');
+ return;
+ }
+
+ // Extract avatar ID from URL
+ const urlMatch = avatarObj.glbUrl?.match(/\/avatars\/([^/?]+)/);
+ const avatarApiId = urlMatch?.[1];
+ if (!avatarApiId) {
+ // No persistent ID — export the whole thing as a GLB instead
+ showError('Avatar has no persistent ID — use Export GLB instead.');
+ return;
+ }
+
+ showLoading('Saving outfit…');
+ try {
+ const res = await fetch(`/api/avatars/${encodeURIComponent(avatarApiId)}`, {
+ method: 'PATCH',
+ headers: { 'Content-Type': 'application/json', ...CLIENT_HEADERS },
+ body: JSON.stringify({ accessories: attachedItems }),
+ });
+ if (!res.ok) {
+ const err = await res.json().catch(() => ({}));
+ throw new Error(err.error || `HTTP ${res.status}`);
+ }
+ flashSaveStatus('Outfit saved ✓');
+ } catch (err) {
+ showError(`Save failed: ${err.message}`);
+ } finally {
+ hideLoading();
+ }
+}
+
+function flashSaveStatus(msg = 'Saved ✓') {
+ saveStatusEl.textContent = msg;
+ saveStatusEl.classList.add('visible');
+ setTimeout(() => saveStatusEl.classList.remove('visible'), 3000);
+}
+
+// ── Initial state ─────────────────────────────────────────────────────────────
+renderObjectList();