/** * Asset URL helpers for the host app's CORS-safe asset proxy (`/asset`). * Shared by the web viewers and `@tts/tabletop`, which both route external * assets (textures, models) through the proxy so three.js can load them. */ /** * Resolve a part's asset URL. Absolute URLs (http/https, data:, blob:) pass * through unchanged; relative paths are resolved against `baseUrl` — the * directory of the part's source file (the json/yaml/markdown that defined * it). Without a `baseUrl`, a relative path is returned as-is. */ export function resolveAssetUrl(url: string, baseUrl?: string): string { if (/^(https?:|data:|blob:)/i.test(url)) return url; if (!baseUrl) return url; // `baseUrl` is a path relative to the games root (no scheme), so join rather // than resolve as a URL. A fake origin lets `new URL` normalize `./` and // `../`; the origin is stripped from the result. const resolved = new URL(url, `http://localhost/${baseUrl}`).pathname; return resolved.replace(/^\/+/, ''); } /** Route an external asset URL through the proxy so three.js can load it. */ export function assetUrl(url: string): string { return `/asset?url=${encodeURIComponent(url)}`; }