---
name: browser-3d-optimization
description: Audit, intake, and optimize 3D assets and Three.js / WebGL scenes for smooth desktop and mobile browser frame rates. Covers AI 3D asset verification (magic bytes, MTL slots, PBR sanity), transfer-vs-GPU budgets (Meshopt, KTX2, textures), shared skeleton-only GLB animation retargeting, greedy voxel meshing, zero-download Web Audio SFX, mobile pointer controls, and opt-in frame-time telemetry. Use when preparing 3D models or tuning WebGL/Three.js performance. Do not use for offline cinema rendering (Blender Cycles/Unreal movie export) or native-only console engines.
license: MIT
compatibility: Works in any Agent Skills harness (Claude Code, Codex, Grok Build / Cursor, Hermes, or local agents). Asset inspection benefits from Node.js 20+ and glTF tools (e.g. gltf-transform) when available.
metadata:
  version: "1.0.0"
  author: "Dylan J. Harris"
  source-article: "https://www.dylanjharris.com/writing/3d-modeling-browser-mobile-route-awakening"
  tested: "2026-12-05"
---

# Browser & Mobile 3D Optimization (Three.js / glTF)

## Goal

Keep real-time browser and mobile 3D scenes smooth, legible, and fast to load by separating network transfer budgets from GPU budgets and verifying every asset and frame metric with measurements.

## Use when / Do not use when

- **Use when:** Importing AI-generated or authored 3D meshes (`GLB`/`OBJ`) into Three.js / WebGL, optimizing draw calls and VRAM for mobile phones, retargeting shared animations across multiple character rigs, building voxel or instanced environments, or diagnosing frame hitches.
- **Do not use when:** Rendering offline high-poly stills/films where real-time frame time does not apply, or tuning native desktop-only engines outside the browser.

## Inputs

Ask for any missing item before optimizing:

1. Target device profile (desktop 60 fps = 16.7 ms/frame; mid-range mobile 30–60 fps = 16.7–33.3 ms/frame).
2. Source 3D files or URLs (`GLB`, `glTF`, `OBJ`/`MTL`) and target scene module(s).
3. Current baseline telemetry (if available): p50/p95/p99 frame time, draw calls, triangles, resident geometries, resident textures.
4. Whether animations are embedded per character or shared across a cast.

## Preconditions

- Node.js 20+ (and `gltf-transform` or equivalent inspector for binary GLB inspection when working locally).
- Access to a real browser (and ideally a mobile viewport/device) to verify visual appearance and telemetry after changes.

## First-principles invariants

1. **Transfer bytes and GPU bytes are different budgets.** `EXT_meshopt_compression` and Draco shrink download size only; decoded vertices, live triangles, skinning work, and uncompressed RGBA8 textures still hit the GPU unchanged.
2. **Never trust a filename or extension from a 3D generator.** Verify file magic bytes, inspect `MTL` slots (`map_Kd` vs roughness/normal maps), and visually inspect the pixels before binding any map as albedo.
3. **Separate resident resource gauges from per-pass draw counters.** In multi-pass renderers (portals, post-processing, shadows), set `renderer.info.autoReset = false` and reset once per frame so `renderer.info.render.calls` sums across every pass (with default `autoReset = true`, it reports only the last `render()` call), while keeping `renderer.info.memory.geometries` and `.textures` strictly as resident gauges.
4. **Zero allocation in hot render loops.** Preallocate typed arrays, scratch vectors/quaternions, and telemetry buffers so garbage collection does not cause 33 ms+ mobile hitches.
5. **Share skeletons and clips instead of duplicating animations in every character GLB.** Author bone deltas in canonical character-root space (`+X left, +Y up, +Z forward`) and convert per bone into parent space (`parentWorld^-1 * charDelta * parentWorld * bone.quaternion`), saving and restoring bones cleanly before each `AnimationMixer` step.

## Procedure

### 1. Establish explicit device budgets first
Define hard ceilings before touching assets:
- Desktop target: `<= 16.7 ms` p95 frame time; mobile target: `<= 33.3 ms` p95 frame time (`over33Ms` near zero).
- Cap `renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))` (or `1.5` on constrained mobile GPUs).
- Set explicit caps for draw calls, live triangles, and GPU texture VRAM (remember a single uncompressed 2048×2048 RGBA8 texture with mipmaps occupies ~21.3 MiB / ~22.4 MB of VRAM).

### 2. Audit raw AI 3D assets before admission
For meshes from image-to-3D providers (such as Hunyuan Rapid or Meshy via Pixio) or local generators:
- Check binary magic bytes (do not reject or regenerate just because a PNG request returned a valid JPEG; verify dimensions and re-save/label accurately).
- Inspect `MTL` / glTF texture bindings and view raw textures to ensure a grayscale roughness map or RGB normal map is not bound as color albedo.
- Confirm scale in meters, Y-up, +Z forward, origin at the feet, and applied transforms.
- Downscale oversized textures (e.g., 1K for mobile characters; KTX2/Basis where GPU VRAM is tight) and merge redundant materials.

### 3. Optimize transfer and GPU separately
- Apply `EXT_meshopt_compression` (or `KHR_mesh_quantization` + KTX2) when transfer size is the bottleneck, and verify vertex/pose fidelity against the uncompressed source.
- Never claim a GPU speedup from Meshopt/Draco alone; pair transfer compression with triangle decimation, texture compression/resizing, and `InstancedMesh` batching for repeated scenery (trees, rocks, props) — baking contact-shadow skirts directly into merged vertex-colored geometry when dynamic shadow maps are too expensive.

### 4. Retarget shared animations cleanly across mixed rigs
- Store shared emotes/cycles in one lightweight skeleton-only GLB rather than embedding copies inside every character file.
- Resolve bone aliases across Mixamo (`mixamorig:`), Meshy, and custom skeletons; detect mirrored Left/Right handedness; scale root hip translation by measured torso height.
- Provide a zero-download procedural pose fallback so characters animate immediately while the shared GLB streams in.

### 5. Bound procedural geometry and audio overhead
- For voxel or destructible terrain, use greedy rectangle merging with preallocated typed arrays, explicit quad/triangle caps, and an integer-cursor slice budget per frame (avoiding per-frame generator or array allocations).
- For snappy mobile SFX (footsteps, clicks, chimes, combat), synthesize sounds lazily with the Web Audio API (unlocked on first user gesture, resumed on `visibilitychange`) instead of shipping dozens of audio files.

### 6. Instrument opt-in frame-time telemetry
- Gate telemetry behind an opt-in query parameter (e.g. `?perf=1`) that returns `null` with zero overhead when disabled.
- Record rolling 10-second `p50`, `p95`, `p99`, `max` ms, `>33ms` and `>50ms` hitch counts, `PerformanceObserver("longtask")`, multi-pass draw calls, triangles, resident geometries, and resident textures.
- Verify on real desktop and throttled mobile profiles for several minutes to catch thermal throttling and memory leaks (`dispose()` checks).

## Human gates

Stop and ask for explicit approval before:
- Replacing shipped visual assets with lossy decimation or aggressive texture downscales that alter art direction.
- Adding third-party WASM decoders or CDN dependencies (verify CSP and offline/self-hosted bundle policy first).
- Modifying production deployment or live multiplayer room protocols.

## Verification

- [ ] Every imported GLB/texture passes magic-byte, scale, orientation, and material-slot inspection.
- [ ] Before/after report shows separate transfer bytes, GPU resident geometries/textures, draw calls, triangles, and `p50/p95/p99` ms.
- [ ] Disposing and remounting scenes leaks zero geometries or textures in `renderer.info.memory`.
- [ ] Mobile touch controls use Pointer Events (`touch-action: none`, `pointerId` tracking) and respect `prefers-reduced-motion`.

## Failure modes

| Symptom | Likely cause | Fix |
|---|---|---|
| Mesh renders flat gray or rainbow-purple | Roughness or normal map bound as albedo (`map_Kd`) | Inspect `MTL` slots and pixel channels before material assignment |
| File shrank 22% with Meshopt but phone still drops frames | GPU VRAM and live triangles stayed unchanged | Resize textures to 1K / KTX2, merge materials, reduce draw calls via instancing |
| Shared animation twists arms backward on half the cast | Mixed rest poses or mirrored L/R bone axes | Apply deltas in character-root space (`parentWorld^-1 * charDelta * parentWorld * q`) and detect handedness |
| Periodic 40–80 ms stutter during terrain edits | Per-frame typed-array allocation or full-chunk rebuild on one frame | Preallocate reusable buffers and budget slices per frame via an integer cursor |
| Silent SFX on iOS/Safari | `AudioContext` created before user gesture or suspended after backgrounding | Unlock on first pointer/key gesture and call `resume()` on `visibilitychange` |

## Output contract

Return:
1. Asset audit table (file bytes, triangles, textures, material/rig issues found).
2. Code/asset changes made with exact file paths.
3. Before-and-after telemetry comparison (`p50/p95/p99` ms, `>33ms` hitches, draw calls, triangles, resident geometries/textures, transfer KB).
4. Unverified devices or browser constraints still needing live testing.

## Provenance

- Source guide: [3D modeling and rendering for browsers and mobile (Route Awakening)](https://www.dylanjharris.com/writing/3d-modeling-browser-mobile-route-awakening) (tested Dec 5, 2026).
- Techniques grounded in Three.js, glTF 2.0 / `EXT_meshopt_compression`, and Mikola Lysenko's greedy meshing pattern; Pixio is cited as a tested 3D generation route alongside local tools like Blender.
