@babylonjs-toolkit/agent 1.1.52 → 1.1.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -17
- package/package.json +1 -1
- package/skills/bt-gauntlet/SKILL.md +19 -19
- package/skills/bt-gauntlet/references/loop-templates.md +12 -12
- package/skills/bt-gauntlet/references/spec-composition.md +6 -6
- package/skills/bt-gauntlet/references/target-images.md +6 -6
- package/skills/bt-gauntlet/references/unity-pipeline.md +3 -3
- package/skills/bt-gauntlet/references/workspace.md +5 -5
- package/skills/bt-hero/SKILL.md +2 -2
- package/skills/bt-optimize/SKILL.md +338 -0
- package/skills/bt-optimize/scripts/BtCombine.cs +1625 -0
- package/skills/bt-optimize/scripts/BtOptimize.cs +969 -0
- package/skills/bt-optimize/scripts/qa/compare-browser.mjs +164 -0
- package/skills/bt-optimize/scripts/qa/compare-profiles.mjs +80 -0
- package/skills/bt-optimize/scripts/qa/gltf-stats.mjs +57 -0
- package/skills/bt-optimize/scripts/qa/package.json +9 -0
- package/skills/bt-optimize/scripts/qa/profile-browser.mjs +547 -0
- package/skills/bt-plan/SKILL.md +9 -9
- package/skills/bt-prototype/SKILL.md +4 -4
- package/skills/bt-recon/SKILL.md +14 -14
- package/skills/bt-spec/SKILL.md +5 -5
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: bt-optimize
|
|
3
|
+
description: "The Babylon Toolkit Optimize Skill makes an exported Unity scene run fast as BabylonJS web content. It measures the exported scene in the browser first (frame rate, draw calls per pass — shadow maps, depth / velocity buffers, main pass, post — CPU hot spots, light binding), surveys the Unity scene for the causes, then fixes them on a copy of the scene: runtime practical lights baked, realtime point / spot shadows removed, non-moving props marked static, probe-lit props with lightmap UVs lightmapped, no-op post effects removed, and static meshes and LOD groups merged with the Babylon Toolkit MeshCombiner (packed source lightmap UVs, mirrored pieces, LOD clusters with corrected switch distances). It re-bakes, exports, and proves the result — frame rate and draw calls before → after at the same pinned views, and the same look in Unity and in the browser. Every decision is written down with its reason. Use when the user wants a Unity scene to run faster or lighter in the browser, or wants static meshes / LOD groups combined (e.g. `/bt-optimize Assets/Scenes/Garden/GardenScene.unity`, `/bt-optimize --combine-only`)."
|
|
4
|
+
allowed-tools: Read, Grep, Glob, Edit, Write, Bash, WebFetch(domain:raw.githubusercontent.com)
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
You are an EXPERT Babylon Toolkit, BabylonJS and Unity technical artist and web-performance engineer. Always adhere to any rules or requirements set out in the project's agent instructions (AGENTS.md / CLAUDE.md / .github/copilot-instructions.md).
|
|
8
|
+
|
|
9
|
+
Use the user's message after the skill name as the `arguments`.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# Invocation
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
/bt-optimize [<scene asset path>] [--out:<asset path>] [--analyze] [--reset]
|
|
17
|
+
[--no-combine | --combine-only] [--no-tune:lights,static,lightmaps,post]
|
|
18
|
+
[--keep-lights:Name1,Prefix*] [--bake-shadowed-lights] [--exclude:Name1,Prefix*] [--only:Path/A,Path/B]
|
|
19
|
+
[--cell:15] [--spread:3] [--atlas-share:0.45] [--no-shadergraphs] [--no-lods] [--shaders:Name1,Name2]
|
|
20
|
+
[--textures:ktx2] [--resolution:css] [--no-bake] [--no-export] [--no-browser] [--pins:40,60] [--times:10,20,30,45,60,90]
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- **`<scene asset path>`** — the scene to optimize. Default: the Editor's active scene.
|
|
24
|
+
- **`--out:`** — the working copy. Default: `<scene folder>/<SceneName>_Optimized.unity`. **The source scene is never edited.**
|
|
25
|
+
- **`--analyze`** — measure and plan only: browser profile of the current export, Unity survey, tune plan, merge plan. Changes nothing.
|
|
26
|
+
- **`--reset`** — start again from the source (re-copies it over the working copy, deletes the previous merged meshes and reports).
|
|
27
|
+
- **`--no-combine`** — every optimization except merging meshes.
|
|
28
|
+
- **`--combine-only`** — only merge static meshes and LOD groups (no light, static, lightmap or post changes).
|
|
29
|
+
- **`--no-tune:`** — skip individual tune steps: `lights` (bake practical lights / drop point-spot shadows), `static` (mark non-moving props static), `lightmaps` (probe-lit → lightmapped), `post` (no-op post effects).
|
|
30
|
+
- **`--keep-lights:`** — light names (or prefixes ending in `*`) that stay realtime / Mixed.
|
|
31
|
+
- **`--bake-shadowed-lights`** — also bake point / spot lights that cast realtime shadows (default: they stay realtime with their shadows turned off).
|
|
32
|
+
- **`--exclude:`** — object names (or prefixes) whose subtrees are never tuned or merged. **`--only:`** limits merging to these hierarchy paths.
|
|
33
|
+
- **Merge controls:** `--cell:` area patch in meters (15), `--spread:` meters a merge may extend past its largest piece (3), `--atlas-share:` largest share of one lightmap atlas per merged mesh (0.45), `--no-shadergraphs` (do not merge Shader Graph materials even when they pass the scan), `--no-lods` (leave LOD groups alone), `--shaders:` extra custom shaders known to be pivot-independent.
|
|
34
|
+
- **Project-wide exporter settings (opt in — they change every scene's export):** `--textures:ktx2` (KTX2 / UASTC textures: ~4–8× less GPU memory, slower export), `--resolution:css` (render at CSS pixels instead of device pixels on desktop: ¼ of the pixels on a 2× screen — only helps a GPU-bound scene).
|
|
35
|
+
- **`--no-bake` / `--no-export` / `--no-browser`** — skip those phases; the report says what stayed unverified.
|
|
36
|
+
- **`--pins:`** — seconds after the scene is ready at which the profiler pins the camera (default `40,60`; pick moments inside the scene's own flythrough / spawn view). **`--times:`** — timeline sample moments.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
# What this skill guarantees
|
|
41
|
+
|
|
42
|
+
1. **Measure before and after, at the same views.** No change is justified by a guess: the browser profile of the current export names the costs, and the same profile of the optimized export proves the gain.
|
|
43
|
+
2. **The source scene — and any scene the user has open — is untouched.** All scene work happens in the working copy, which only `PrepareCopy` can create (it writes `.agents/optimize/<Out>/working-copy.txt`; Tune, Apply and RemoveCopy refuse any scene without it). Nothing switches scenes while an open scene has unsaved changes. The only project-wide edits are (a) deactivating a **zero-intensity** motion blur override in a Volume Profile (no change in Unity; recorded for undo) and (b) exporter settings the user opted into with `--textures` / `--resolution`.
|
|
44
|
+
3. **Every decision has a written reason.** `.agents/optimize/<Out>/` holds the survey, the tune plan and log, the merge plan and manifest, verify results, profiles, captures and the report.
|
|
45
|
+
4. **It looks the same.** Re-baked, then compared in Unity (LOD0 and LOD1) and in the browser at matching views. Expected, stated differences: baked practical lights lose their specular highlights and their realtime shadows on moving objects (they never had flicker or distance culling in the browser — those were Unity-only scripts).
|
|
46
|
+
5. **No vertex bloat.** Merges use the toolkit's `UnityMeshSimplifier.MeshCombiner` in *Pack Source Lightmap UVs* mode — each source keeps its own lightmap UVs, packed at its original texel density.
|
|
47
|
+
|
|
48
|
+
# Talk to the user while you work
|
|
49
|
+
|
|
50
|
+
Long runs lose people. At every phase change post one short plain-language update: **Done** (what changed, with numbers) · **Doing now** · **Next**. Say plainly which work is done by their own toolkit (MeshCombiner, exporter, bake). If you change the plan (exclude a group, keep a light, reset, re-bake), say why in one line.
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
# How a BabylonJS frame spends its time — what to look for
|
|
55
|
+
|
|
56
|
+
Learned on a 2,400-renderer URP garden that ran 8–26 FPS (1–5 FPS in hitches) after export:
|
|
57
|
+
|
|
58
|
+
- **Draw calls are the currency.** WebGPU / WebGL in the browser is CPU-bound long before it is GPU-bound: each draw costs JavaScript time (`bindForSubMesh`, `_draw`, render-pipeline lookup, uniform writes). Halving render resolution changed nothing; halving draws nearly doubled the frame rate. Every extra **pass** multiplies the draws of everything it renders.
|
|
59
|
+
- **Realtime shadows on point / spot lights are full scene passes.** Each shadowed spot light re-draws every caster in its frustum every frame (a point light: six times). The toolkit adds every shadow-casting mesh to every shadow-generating light. Six 5 m lantern spot lights = ~690 draws per frame — half the frame.
|
|
60
|
+
- **Runtime lights that the browser never animates are pure cost.** Unity sample scenes drive lights with MonoBehaviours (`LightFlicker`, `DynamicLightController` distance culling). Without a toolkit TypeScript port those scripts **do not run in the browser**: the lights neither flicker nor cull — they just cost. Baked, their light lives in the lightmaps (static objects) and light probes (moving objects) at zero runtime cost.
|
|
61
|
+
- **A depth pass appears when anything samples scene depth**: soft particles (`TOOLKIT.ShurikenParticles.SoftParticleDepth`, on by default) and Shader Graph Scene Depth nodes (`ShaderGraphPass.SceneDepth`) create a `DepthRenderer` that re-draws every visible opaque mesh each frame (20–25% of draws in the garden). Report it; merging reduces it; the runtime fix (take depth from the prepass renderer) belongs to the toolkit.
|
|
62
|
+
- **Zero-intensity post effects may still run.** URP's pipeline-default Volume Profile ships Motion Blur at intensity 0 (off in Unity); older toolkit runtimes still built a motion-blur pass for it.
|
|
63
|
+
- **Light binding is range-aware** in the toolkit (a mesh only binds lights whose range reaches it; at most `SceneController.maximumLights`, default 4).
|
|
64
|
+
- **Materials are cloned per renderer** for each reflection probe / lightmap combination and per LOD cross-fade level. Fewer renderers → fewer clones → fewer first-view shader compiles (hitches).
|
|
65
|
+
- **Lightmapped renderers never share a cached mesh** (reference §4): repeated lightmapped props are one draw each — merging is the only way to cut them. Probe-lit repeated props stay glTF instances.
|
|
66
|
+
|
|
67
|
+
# The tune rules — and why each exists
|
|
68
|
+
|
|
69
|
+
Enforced by `scripts/BtOptimize.cs` (`Tune`). A **browser driver** is anything that can move, toggle or animate an object *in the browser*: a toolkit script component (`EditorScriptComponent` subclass), Animator / Animation, a Timeline binding, Rigidbody / CharacterController / NavMeshAgent, a constraint, or a toolkit script component that points at the object — on the object or an ancestor. Unity-only MonoBehaviours are not browser drivers (they are reported as "Unity-only").
|
|
70
|
+
|
|
71
|
+
| Step | Rule | Why |
|
|
72
|
+
|---|---|---|
|
|
73
|
+
| Lights → Baked | Active Realtime / Mixed **point and spot** lights with no browser driver and **no realtime shadows** become Baked. Directional lights (the sun) and `--keep-lights` stay | Practical fill lights cost per-pixel lighting every frame; baked, they cost nothing and still light moving objects through probes |
|
|
74
|
+
| Shadowed key lights stay runtime, shadows off | A point / spot light that casts realtime shadows (or that a browser driver animates) stays Realtime / Mixed and loses only its realtime shadows. `--bake-shadowed-lights` bakes them too | Each shadowed light re-draws the scene every frame (×6 for a point light) — the shadows are the cost, not the light. Keeping the light realtime keeps its exact look (direct light, specular highlights, light on moving objects) with no bake risk; on Garden, six such lights cost little once their shadow maps were gone. Use `--bake-shadowed-lights` when a scene has many of them and Phase 5 verifies the bake |
|
|
75
|
+
| Fixture shells stop casting shadows | When a light is baked, the shadow-casting renderers of its own fixture prefab whose bounds hold the light get Cast Shadows Off | A lantern shell around the bulb is clipped by the realtime shadow map's near plane, but the lightmapper traces from the bulb: a shadow-casting shell traps a baked light (lit floor under each lantern, dark walls) |
|
|
76
|
+
| Mark static | A non-static MeshRenderer with no browser driver, not pointed at by any script, not part of a particle system, and **a visible surface** (every material's shader has a lightmapper Meta pass) becomes static (Batching, Occluder, Occludee, Reflection Probe; **Contribute GI** only with lightmap UVs, no vertex animation and opaque) | The exporter freezes static world matrices, and static objects can be lightmapped and merged. The Meta-pass check exists because an invisible stencil mesh (`Custom/StencilOnly`) wrapping a room, once marked Contribute GI, became a solid box in the bake and the room went dark |
|
|
77
|
+
| Probe-lit → lightmapped | A static LOD0 / non-LOD visible surface that is probe-lit although it has UV2, no vertex animation and is opaque gets Contribute GI + Receive GI Lightmaps; its lower LOD levels with UV2 receive lightmaps (they borrow LOD0's region) | Lightmapped meshes can merge; a merged probe-lit mesh would get one probe sample for its whole area |
|
|
78
|
+
| No-op post | Motion blur overrides with intensity 0 in the profiles the scene uses are deactivated | Off in Unity, but older toolkit runtimes still built the pass |
|
|
79
|
+
|
|
80
|
+
Kept as they are, by design: wind / vertex-animated vegetation (probe-lit by intent, and unmergeable), props without lightmap UVs (probe-lit), anything a script points at, stencil / depth / VFX utility meshes (no Meta pass), inactive objects.
|
|
81
|
+
|
|
82
|
+
**Every tune rule above came from a regression found by comparing Unity captures at the browser's poses** — run Phase 5 every time; a rule that changes what the lightmapper sees can change a whole room.
|
|
83
|
+
|
|
84
|
+
# The merge rules — and why each exists
|
|
85
|
+
|
|
86
|
+
Enforced by `scripts/BtCombine.cs` (`Analyze` / `Apply`). Each rule comes from a real regression.
|
|
87
|
+
|
|
88
|
+
**A piece can merge only if it is:**
|
|
89
|
+
|
|
90
|
+
| Rule | Why |
|
|
91
|
+
|---|---|
|
|
92
|
+
| Marked static (Contribute GI or Batching Static) | Anything else may move at runtime |
|
|
93
|
+
| Not scripted, animated, a particle system, a Rigidbody, or under one | Merged geometry cannot move or toggle on its own |
|
|
94
|
+
| Not referenced by any component (scripts, timeline bindings, cameras, signals…) | A script that toggles or moves it would lose its target |
|
|
95
|
+
| Opaque or alpha-tested (not transparent) | Transparent meshes are sorted per mesh; merging breaks draw order |
|
|
96
|
+
| A safe shader (URP/HDRP/Built-in Lit/Simple Lit/Unlit/Standard, `--shaders`) or a Shader Graph that passes the scan (on by default; `--no-shadergraphs` turns it off) | Graphs that read object space, use the Object node / model matrix, or move vertices (wind) change look when many objects share one pivot |
|
|
97
|
+
| Mirrored (negative scale) only on a toolkit whose MeshCombiner has `HandlesMirroredTransforms` | Older combiners did not reverse winding or fix normals / tangent handedness — flipped pieces rendered inside out |
|
|
98
|
+
| Not vertex-painted, not on toolkit layers 28–31 | Vertex streams and reserved layers are lost |
|
|
99
|
+
| **Lightmapped** when the scene has light probes | A merged light-probe-lit mesh gets **one** probe sample at its centre — metal rails merged across a room turned black |
|
|
100
|
+
|
|
101
|
+
**A group of pieces merges only if they:**
|
|
102
|
+
|
|
103
|
+
| Rule | Why |
|
|
104
|
+
|---|---|
|
|
105
|
+
| Share a container (highest ancestor with no scripts, animation or references) | Toggling or moving anything above still applies to the merged mesh |
|
|
106
|
+
| Share every renderer setting (shadows, GI, probes, Scale In Lightmap, lightmap parameters, layer, tag, static flags) | One mesh has one set |
|
|
107
|
+
| Share their dominant **reflection probe**, and the merged centre stays inside that probe's box | Probes are chosen from a mesh's bounds centre; merging two corridors moved the centre into a different probe zone and darkened both |
|
|
108
|
+
| Stay inside one area cell, and extend at most `--spread` past their largest piece | Keeps merges physically adjacent: culling, probes and light choice stay local |
|
|
109
|
+
| Fit the lightmap atlas budget, and stay under 200k vertices | One mesh must fit one lightmap; huge meshes cull badly |
|
|
110
|
+
| Are not reached by more runtime lights than BabylonJS applies per mesh (`maximumLights`, default 4), beyond what their most-lit piece already had | Merging must never cost a piece one of its lights |
|
|
111
|
+
|
|
112
|
+
**LOD groups** merge as **LOD clusters**: nearby groups with the same level count, transition heights, fade mode and per-level settings (typically instances of one prefab). Level *i* of every member becomes one merged mesh in a new LOD group with exactly one renderer per level (BabylonJS's fast native LOD path). New transition heights = old × (cluster size ÷ member size); a cluster is split when a member would sit more than 20% of its LOD0→LOD1 switch distance from the centre, or the rescaled LOD0 height would reach 0.95. **Lower LODs borrow LOD0's lightmap** (a LOD1/LOD2 renderer that receives lightmaps without contributing GI gets LOD0's region), so the engine applies each member's LOD0 packing to its lower levels — packing them independently turned merged rocks black at LOD1. Billboard / SpeedTree / skinned LODs, empty levels and renderers shared between levels are left alone.
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
# Pipeline
|
|
117
|
+
|
|
118
|
+
Read the Babylon Toolkit Agent Reference router and its `unity-exporter-cli.md`, `unity-editor-commands.md` and `unity-authoring-recipes.md` (§0 fidelity, §3 lights, §4 lightmaps, §5–6 probes, §9 post, §17 LOD, §19 static flags, §20 web budget, §21 verification) before the first run in a session — unless your prompt already carries those excerpts.
|
|
119
|
+
|
|
120
|
+
Set these once:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
PROJ=<absolute Unity project path> # folder holding Assets/
|
|
124
|
+
SKILL_DIR=<absolute path of this skill's folder>
|
|
125
|
+
SOURCE=Assets/.../Scene.unity # from the arguments, or the active scene (bt_status → scene)
|
|
126
|
+
OUT=Assets/.../Scene_Optimized.unity # --out, or <source folder>/<SceneName>_Optimized.unity
|
|
127
|
+
NAME=$(basename "$OUT" .unity); SRC_NAME=$(basename "$SOURCE" .unity)
|
|
128
|
+
REPORT="$PROJ/.agents/optimize/$NAME"; QA="$PROJ/.agents/optimize/.qa-tools"
|
|
129
|
+
MERGE='{"cellSize":15,"maxSpread":3,"maxAtlasShare":0.45,"includeShaderGraphs":true,"combineLods":true,"exclude":[],"only":[],"extraShaders":[]}'
|
|
130
|
+
TUNE='{"bakeLights":true,"bakeShadowedLights":false,"keepLights":[],"pointSpotShadowsOff":true,"markStatic":true,"lightmapProbeLit":true,"fixZeroMotionBlur":true,"exclude":[]}'
|
|
131
|
+
|
|
132
|
+
bt_run() { # bt_run <File: BtOptimize|BtCombine> <Method> '<json args array>'
|
|
133
|
+
unity command run_script --file "AgentScripts/bt-optimize/$1.cs" --entry "$1.$2" --args "$3" \
|
|
134
|
+
--timeout_ms 1800000 --timeout 1900 --project-path "$PROJ" --format json \
|
|
135
|
+
| python3 -c "import json,sys; d=json.load(sys.stdin); r=d['data']['result']; print(r.get('result') if r.get('success') else 'FAILED: '+json.dumps(r.get('diagnostics') or r.get('errorDetails') or r)[:4000])"
|
|
136
|
+
}
|
|
137
|
+
arg() { python3 -c "import json,sys; print(json.dumps([sys.argv[1]]))" "$1"; } # wraps one JSON options string as a run_script args array
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Map flags into `MERGE` / `TUNE` (`--exclude:A,B*` → both `"exclude":["A","B*"]`, `--keep-lights:` → `keepLights`, `--no-shadergraphs` → `"includeShaderGraphs":false`, `--no-lods` → `"combineLods":false`, `--no-tune:lights` → `"bakeLights":false,"pointSpotShadowsOff":false`, `--no-tune:static` → `"markStatic":false`, `--no-tune:lightmaps` → `"lightmapProbeLit":false`, `--no-tune:post` → `"fixZeroMotionBlur":false`, `--bake-shadowed-lights` → `"bakeShadowedLights":true`).
|
|
141
|
+
|
|
142
|
+
## Phase 0 — Preflight
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
unity pipeline list # Editor running, Pipeline reachable, not in Safe Mode
|
|
146
|
+
unity command editor_status --project-path "$PROJ" # ready, not in Play mode (editor_stop if it is)
|
|
147
|
+
mkdir -p "$PROJ/AgentScripts/bt-optimize" && cp "$SKILL_DIR"/scripts/*.cs "$PROJ/AgentScripts/bt-optimize/"
|
|
148
|
+
mkdir -p "$QA" && cp "$SKILL_DIR"/scripts/qa/* "$QA/" && (cd "$QA" && npm install --silent) # puppeteer-core, outside the Unity project
|
|
149
|
+
bt_run BtCombine Preflight "[\"$SOURCE\"]"
|
|
150
|
+
unity command bt_devserver_start --project-path "$PROJ" # rerun after every domain reload — it stops the server
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Stop and report if: no Editor is reachable (`unity-exporter-cli.md` §6), the source does not exist, **any open scene has unsaved changes** (ask the user to save or discard — never do it for them, never open another scene over it), or `packSourceLightmapUVs=False` (their `CanvasTools.dll` predates the packing combiner; merging would split vertices — tell them to update, or continue with `--no-combine`). `mergesMirrored=False` is not a stop: mirrored pieces just stay separate (say so).
|
|
154
|
+
|
|
155
|
+
## Phase 1 — Measure the current export
|
|
156
|
+
|
|
157
|
+
The exported source scene must be current: find `<exportRoot>/scenes/<source>.gltf` (`bt_status` → `exportRoot`; file names may be lower-cased). If it is missing or older than the source `.unity`, `unity command bt_export_level --scene "$SOURCE" --project-path "$PROJ" --timeout 3600` and record the duration.
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
mkdir -p "$PROJ/.agents/optimize/$SRC_NAME/profile"
|
|
161
|
+
node "$QA/profile-browser.mjs" --scene <source>.gltf --times 10,20,30,45,60,75,90 --window 4 \
|
|
162
|
+
--out "$PROJ/.agents/optimize/$SRC_NAME/profile/before-timeline.json"
|
|
163
|
+
node "$QA/profile-browser.mjs" --scene <source>.gltf --pin 40,60,75 --toggles none --window 5 --settle 8 --cpuprofile true \
|
|
164
|
+
--shots "$PROJ/.agents/optimize/$SRC_NAME/profile/shots" --out "$PROJ/.agents/optimize/$SRC_NAME/profile/before-pinned.json"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`--pin` waits the given seconds after the scene is ready, then detaches the camera from its rig and **records its exact pose** (position, rotation and lens — field of view and clip planes, because Timeline / Cinemachine tracks animate the lens too; the lens fields are made read-only while pinned, because a lens track keeps writing them and TAA builds its projection from them before any observer runs). Every later run replays those poses with `--poses before-pinned.json`, so before and after are measured from the same views — pinning by time alone lands on different views, because a lighter scene loads sooner and its tour runs ahead.
|
|
168
|
+
|
|
169
|
+
Read the JSON: `census` (engine, render size, meshes, materials, lights, shadow generators and their casters, post passes, prepass), `samples` (FPS, p95 / worst frame, draws, active meshes), and per pin `baseline.passes` / `baseline.byTarget` (draws per frame in shadow maps, `DepthRenderer`, main pass, post), `lightAudit`, and `cpu.top`. **Pin inside the scene's own motion** (a flythrough, a spawn view after compile): the first seconds after load are still compiling shaders and are not comparable. To confirm a suspected cost, re-run with `--toggles shadows,lights,motionblur,taa,bloom,dpr` (each switched off alone and settled on `whenReadyAsync` — never compare a window that follows a toggle without that wait: thousands of materials recompile).
|
|
170
|
+
|
|
171
|
+
## Phase 2 — Survey and plan
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
unity command open_scene --path "$SOURCE" --project-path "$PROJ" # only when no open scene is dirty
|
|
175
|
+
bt_run BtOptimize Survey '[]' # .agents/optimize/<source>/survey.md + .json
|
|
176
|
+
bt_run BtOptimize Tune "$(arg "$(echo "$TUNE" | sed 's/}$/,"dryRun":true}/')")" # tune-plan.md: every change, nothing changed
|
|
177
|
+
bt_run BtCombine Analyze "$(arg "$MERGE")" # combine-plan.md (on the source: merges before tune unlocks)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Tell the user in a few lines: the measured frame (FPS, draws per pass, the top costs), and the plan with numbers (lights to bake, props to mark static, renderers to lightmap, merges). `--analyze` stops here. Otherwise continue — do not wait for approval; the source is safe.
|
|
181
|
+
|
|
182
|
+
## Phase 3 — Working copy, tune, merge
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
bt_run BtCombine PrepareCopy "[\"$SOURCE\",\"$OUT\",<true if --reset else false>]"
|
|
186
|
+
bt_run BtOptimize Tune "$(arg "$TUNE")" # skip with --combine-only
|
|
187
|
+
bt_run BtCombine Analyze "$(arg "$MERGE")" # skip with --no-combine; plan after the unlocks
|
|
188
|
+
bt_run BtCombine Apply "$(arg "$MERGE")" # skip with --no-combine
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Read the Apply output: every group's `verts a -> b` must be equal. A `WARNING … re-unwrapped` on a few small groups means a lightmapped source had no usable lightmap UVs (no UV2 and UV0 outside 0..1) and was unwrapped on its own — report the count; many or large ones mean the toolkit build lacks packing — stop and report. If Apply refuses ("already combined"), continue from Phase 4 with the existing result or rerun with `--reset`.
|
|
192
|
+
|
|
193
|
+
## Phase 4 — Bake (skip only with --no-bake)
|
|
194
|
+
|
|
195
|
+
Light-mode changes, GI-flag changes and merged lightmap UVs all need one bake; a copied scene still points at the source's LightingData.
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
bake() {
|
|
199
|
+
unity command bake_lighting --confirm true --project-path "$PROJ"
|
|
200
|
+
deadline=$((SECONDS+5400)); seen=""
|
|
201
|
+
while :; do
|
|
202
|
+
s=$(unity command lighting_bake_status --project-path "$PROJ" --result-only 2>/dev/null)
|
|
203
|
+
case "$s" in
|
|
204
|
+
*completed*) break;;
|
|
205
|
+
*failed*) echo "bake failed: $s"; return 1;;
|
|
206
|
+
*baking*|*running*|*in_progress*) seen=1;;
|
|
207
|
+
*idle*) [ -n "$seen" ] && { echo "bake stopped without completing"; return 1; };;
|
|
208
|
+
esac
|
|
209
|
+
[ $SECONDS -ge $deadline ] && { echo "bake timed out"; return 1; }
|
|
210
|
+
sleep 15
|
|
211
|
+
done
|
|
212
|
+
unity command save_all --project-path "$PROJ" # the bake assigns new LightingData; exports refuse a dirty scene
|
|
213
|
+
}
|
|
214
|
+
bake
|
|
215
|
+
bt_run BtCombine CorrectDensity '[]' # merged meshes get ~84-89% of their sources' texels; raise Scale In Lightmap
|
|
216
|
+
# corrected=N with N > 0 → bake once more
|
|
217
|
+
bt_run BtCombine Verify '[]' # must end with problems=0
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Phase 5 — Unity side-by-side (browser poses, then LOD0 **and** LOD1)
|
|
221
|
+
|
|
222
|
+
**First, the browser's pinned poses in Unity**, source and working copy. `PoseView` puts a capture camera named `BtOptimizePoseCamera` at a pose recorded by the profiler (position, rotation, vertical FOV; toolkit scenes keep Unity's coordinates), copying the main camera's URP data so post-processing matches. It never saves — reopening the scene removes it. Comparing these with the browser shots of the same poses tells you which side of the export a difference is on:
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
python3 -c "import json; [print(p['atSeconds'],*p['pinned']['position'],*p['pinned']['rotation'],p['pinned']['fov']) for p in json.load(open('$PROJ/.agents/optimize/$SRC_NAME/profile/before-pinned.json'))['pins']]" > /tmp/poses.txt
|
|
226
|
+
for scene in "$OUT" "$SOURCE"; do tag=$(basename "$scene" .unity)
|
|
227
|
+
while read t px py pz qx qy qz qw fov; do
|
|
228
|
+
bt_run BtOptimize PoseView "[\"$scene\",$px,$py,$pz,$qx,$qy,$qz,$qw,$fov]" >/dev/null
|
|
229
|
+
unity command capture_game_view --camera BtOptimizePoseCamera --width 1280 --height 720 --project-path "$PROJ" --result-only --timeout 300 \
|
|
230
|
+
| python3 -c "import json,sys,base64; d=json.load(sys.stdin); d=json.loads(d) if isinstance(d,str) else d; open(sys.argv[1],'wb').write(base64.b64decode(d['base64']))" "$CAPS/unity_${tag}_pose${t}.png"
|
|
231
|
+
done < /tmp/poses.txt
|
|
232
|
+
done
|
|
233
|
+
unity command eval "UnityEditor.SceneManagement.EditorSceneManager.OpenScene(\"$OUT\"); return \"ok\";" --project-path "$PROJ"
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Then the largest merges at LOD0 and forced LOD1:
|
|
237
|
+
|
|
238
|
+
Capture the spawn view, then the three largest merges at LOD0 and forced LOD1, in both scenes (GPU Resident Drawer must be off in batch Editors — `unity-editor-commands.md` §8.1):
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
CAPS="$REPORT/captures"; mkdir -p "$CAPS"
|
|
242
|
+
CAM=$(unity command eval 'return UnityEngine.Camera.main != null ? UnityEngine.Camera.main.name : "";' --project-path "$PROJ" --result-only | python3 -c "import json,sys; print(json.load(sys.stdin)['result'])")
|
|
243
|
+
cap() { unity command capture_game_view --camera "$CAM" --width 1280 --height 720 --project-path "$PROJ" --result-only --timeout 300 \
|
|
244
|
+
| python3 -c "import json,sys,base64; d=json.load(sys.stdin); d=json.loads(d) if isinstance(d,str) else d; open(sys.argv[1],'wb').write(base64.b64decode(d['base64']))" "$1"; }
|
|
245
|
+
|
|
246
|
+
cap "$CAPS/unity_spawn_optimized.png"
|
|
247
|
+
unity command open_scene --path "$SOURCE" --project-path "$PROJ"; cap "$CAPS/unity_spawn_source.png"
|
|
248
|
+
unity command open_scene --path "$OUT" --project-path "$PROJ"
|
|
249
|
+
bt_run BtCombine QaPoints '[3]' > "$CAPS/points.txt" # name|x|y|z|size of the largest merges (LOD clusters first)
|
|
250
|
+
while IFS='|' read -r name x y z size; do
|
|
251
|
+
[ -z "$name" ] && continue
|
|
252
|
+
dist=$(python3 -c "print(max(4.0, float('$size') * 1.5))")
|
|
253
|
+
for lod in 0 1; do
|
|
254
|
+
for which in source optimized; do
|
|
255
|
+
scene=$([ "$which" = source ] && echo "$SOURCE" || echo "$OUT")
|
|
256
|
+
bt_run BtCombine QaView "[\"$scene\",$x,$y,$z,$dist,$lod]" >/dev/null && cap "$CAPS/unity_${name}_lod${lod}_${which}.png"
|
|
257
|
+
done
|
|
258
|
+
done
|
|
259
|
+
done < "$CAPS/points.txt"
|
|
260
|
+
unity command open_scene --path "$OUT" --project-path "$PROJ" # QaView never saves; reopening discards the QA camera
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Compare every pair: material, detail, baked shadows and brightness must match. **Expected** differences from baked practical lights: no specular glints from those lights, slightly different soft shadows — say so; anything darker, black or blotchy only at LOD1 is a lower-LOD lightmap problem; wrong at LOD0 is a probe or lightmap problem → exclude that merge (Phase 7 loop).
|
|
264
|
+
|
|
265
|
+
## Phase 6 — Export, measure again, compare
|
|
266
|
+
|
|
267
|
+
Only with `--textures:ktx2` / `--resolution:css` (project-wide; previous values go to `.agents/optimize/exporter-settings-undo.txt`):
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
unity command run_script --file AgentScripts/bt-optimize/BtOptimize.cs --entry BtOptimize.ExporterSettings --args '["ktx2","keep"]' --project-path "$PROJ" --format json
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Recommend `--resolution:css` only when the profile is GPU-bound (FPS rises with `--toggles dpr`); on a CPU-bound scene it costs sharpness and buys nothing.
|
|
274
|
+
|
|
275
|
+
```bash
|
|
276
|
+
unity command bt_export_level --scene "$OUT" --project-path "$PROJ" --timeout 3600 # record the duration
|
|
277
|
+
unity command bt_devserver_start --project-path "$PROJ"
|
|
278
|
+
node "$QA/gltf-stats.mjs" <source.gltf> <optimized.gltf>
|
|
279
|
+
node "$QA/profile-browser.mjs" --scene <optimized>.gltf --times 10,20,30,45,60,75,90 --window 4 --out "$REPORT/profile/after-timeline.json"
|
|
280
|
+
node "$QA/profile-browser.mjs" --scene <optimized>.gltf --poses "$PROJ/.agents/optimize/$SRC_NAME/profile/before-pinned.json" --toggles none --window 5 --settle 8 --cpuprofile true \
|
|
281
|
+
--shots "$REPORT/profile/shots" --out "$REPORT/profile/after-pinned.json"
|
|
282
|
+
node "$QA/compare-profiles.mjs" --label Original,Optimized "$PROJ/.agents/optimize/$SRC_NAME/profile/before-pinned.json" "$REPORT/profile/after-pinned.json"
|
|
283
|
+
node "$QA/compare-profiles.mjs" --label Original,Optimized "$PROJ/.agents/optimize/$SRC_NAME/profile/before-timeline.json" "$REPORT/profile/after-timeline.json"
|
|
284
|
+
node "$QA/compare-browser.mjs" --scenes <source>.gltf,<optimized>.gltf --times 20,25,30,40,50,60 --out "$REPORT/captures"
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
`compare-profiles` prints the report tables. **Before trusting a pinned comparison, look at the paired `--shots` images: the same pose must show the same view** (a lens or camera the pin did not hold shows up there first). The pinned views are the same poses in both runs; compare FPS, p50 / p95 frame time, draws per pass and the CPU top list. In `compare-browser`'s `sheet.png`, pair frames by **view, not timestamp** — a lighter scene finishes loading sooner and its flythrough runs ahead (5–10 s on the garden). Both scenes must be free of console errors.
|
|
288
|
+
|
|
289
|
+
## Phase 7 — Regression loop (at most 3 rounds)
|
|
290
|
+
|
|
291
|
+
**Find the side first.** Unity original vs Unity working copy at the same pose: different → the scene changes (tune or merge) did it; same → the export / runtime did it (compare the census and console).
|
|
292
|
+
|
|
293
|
+
**Then bisect, never guess.** Two plausible stories about Garden's dark tea room were both wrong; one bake per variant found the truth in about 30 minutes:
|
|
294
|
+
1. Control copies: `PrepareCopy` to `<Scene>_TuneOnly.unity` (reset each time), run `Tune` with steps switched off (`{"bakeLights":false,"pointSpotShadowsOff":false}` = static/lightmap steps only; `{"markStatic":false,"lightmapProbeLit":false}` = light steps only; then single steps), bake once, `PoseView` + capture the bad pose.
|
|
295
|
+
2. The step that reproduces it → diff renderer state near the spot between source and copy (static flags, Contribute GI, receive GI, shadow casting) to name the object, fix the **rule** in `BtOptimize.cs`, `RemoveCopy` the control, rerun with `--reset`.
|
|
296
|
+
3. Unity-side runtime experiments (disabling lights in the Editor) are unreliable when the scene has `[ExecuteAlways]` scripts — Garden's `DynamicLightController` re-enabled every light each frame. Change only what such scripts do not touch, or bake a control copy.
|
|
297
|
+
|
|
298
|
+
If a region looks different (darker, flatter, wrong reflections, LOD popping) or a pinned view got slower:
|
|
299
|
+
1. Identify the cause: a merge (`combine-manifest.json` → `combinedPaths`, `sources`, probes), a baked light (`tune.md`), a lightmap switch.
|
|
300
|
+
2. Exclude it (`--exclude` / `--keep-lights`), say why in one line, rerun from Phase 3 with `--reset`.
|
|
301
|
+
3. Still different without it → compare against a re-baked **untouched** copy (`PrepareCopy` to `<Scene>_Control.unity`, bake, export, capture; then `RemoveCopy`). A difference the control also shows is the re-bake (often a stale source bake), not this skill.
|
|
302
|
+
|
|
303
|
+
## Phase 8 — Report
|
|
304
|
+
|
|
305
|
+
Reply in plain language with images, and write `$REPORT/report.md`:
|
|
306
|
+
|
|
307
|
+
- **Result:** FPS and frame time (p50 / p95 / worst) before → after at each pinned view and over the timeline; draws per frame by pass; meshes, primitives, materials, vertices and export size (`gltf-stats`); export time.
|
|
308
|
+
- **What changed and why:** lights baked (count, which), shadows removed, props marked static, renderers lightmapped, post fixes, merges (static groups, LOD clusters, the biggest by name).
|
|
309
|
+
- **What stayed and why:** top kept reasons with counts, and what the user could change (UV2 on props, scripts that point at objects, vegetation by design).
|
|
310
|
+
- **Toolkit findings:** costs the scene cannot fix (for example a `DepthRenderer` pass owned by soft particles or a Scene Depth node), with the runtime fix to ask for.
|
|
311
|
+
- **Checks:** Verify result, Unity captures, browser frames, console errors; anything skipped or unverified, stated as such.
|
|
312
|
+
- **How to undo:** `--reset`, or delete the working copy, `<Out>_CombinedMeshes/`, `<Out>/` (baked lighting) and `.agents/optimize/<Out>/`; re-tick any Volume Profile override listed in `tune.md`.
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
# Files this skill writes
|
|
317
|
+
|
|
318
|
+
| Path | What |
|
|
319
|
+
|---|---|
|
|
320
|
+
| `<out scene>` | the working copy (tuned and merged) |
|
|
321
|
+
| `<out scene folder>/<Out>_CombinedMeshes/*.asset` | merged meshes |
|
|
322
|
+
| `<out scene folder>/<Out>/` | the copy's baked lighting |
|
|
323
|
+
| `.agents/optimize/<Source>/` | survey, tune plan, merge plan and "before" profiles of the source |
|
|
324
|
+
| `.agents/optimize/<Out>/` | `working-copy.txt`, `tune.md/json`, `combine-plan.*`, `combine-manifest.json`, `combine-verify.md`, `profile/`, `captures/`, `report.md` |
|
|
325
|
+
| `AgentScripts/bt-optimize/*.cs`, `.agents/optimize/.qa-tools/` | the engines and QA tools (outside `Assets/`, no recompiles) |
|
|
326
|
+
|
|
327
|
+
# Troubleshooting
|
|
328
|
+
|
|
329
|
+
| Symptom | Fix |
|
|
330
|
+
|---|---|
|
|
331
|
+
| `FAILED: … Compilation Failed` from `bt_run` | Read the diagnostics; the toolkit DLL may be older than this skill (`UnityMeshSimplifier.MeshCombiner`, `UnityTools.GetMaxSimultaneousLights`, `EditorScriptComponent` must exist) |
|
|
332
|
+
| Apply: "no Pack Source Lightmap UVs mode" | Update the Babylon Toolkit (`CanvasTools.dll`), or `--no-combine`; never accept bloat silently |
|
|
333
|
+
| Apply: "already combined" | `--reset`, or continue from Phase 4 |
|
|
334
|
+
| Bake never completes | `console --level error`; the GPU lightmapper may fall back to CPU — raise the deadline |
|
|
335
|
+
| Export refuses: unsaved changes | `unity command save_all` after the bake |
|
|
336
|
+
| Browser: connection refused | The dev server stops on every domain reload — `bt_devserver_start` again |
|
|
337
|
+
| Profile numbers jump between windows | The view was still compiling (pin later) or a toggle was measured before `whenReadyAsync` settled |
|
|
338
|
+
| Faster, but a merged area is darker in the browser only | Probe or light choice: lower `--spread` / `--cell`, or `--exclude` that container; then the control test |
|