@null3d/engine 0.0.0 → 0.1.0

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.
Files changed (311) hide show
  1. package/README.md +109 -0
  2. package/THIRD-PARTY-NOTICES.txt +0 -1
  3. package/dist/wasm/single/null3d.js +19 -6
  4. package/dist/wasm/single/null3d_bg.wasm +0 -0
  5. package/dist/wasm/threaded/null3d.js +19 -6
  6. package/dist/wasm/threaded/null3d_bg.wasm +0 -0
  7. package/docs/api/animation.md +23 -67
  8. package/docs/api/assets.md +42 -26
  9. package/docs/api/cameras.md +8 -6
  10. package/docs/api/controls.md +46 -44
  11. package/docs/api/debug.md +13 -8
  12. package/docs/api/engine.md +17 -11
  13. package/docs/api/feature-draws.md +3 -3
  14. package/docs/api/feature-effects.md +3 -3
  15. package/docs/api/feature-instances.md +2 -2
  16. package/docs/api/features.md +14 -15
  17. package/docs/api/geometry.md +56 -75
  18. package/docs/api/grass.md +1 -1
  19. package/docs/api/impostors.md +120 -9
  20. package/docs/api/input.md +4 -11
  21. package/docs/api/lights.md +14 -38
  22. package/docs/api/lines.md +8 -25
  23. package/docs/api/materials.md +69 -70
  24. package/docs/api/math.md +6 -7
  25. package/docs/api/objects.md +13 -13
  26. package/docs/api/particles.md +2 -14
  27. package/docs/api/points.md +3 -25
  28. package/docs/api/post.md +22 -23
  29. package/docs/api/quality.md +11 -6
  30. package/docs/api/raycast.md +13 -32
  31. package/docs/api/reference/animation.md +9 -9
  32. package/docs/api/reference/assets.md +18 -18
  33. package/docs/api/reference/cameras.md +5 -2
  34. package/docs/api/reference/controls.md +75 -23
  35. package/docs/api/reference/debug.md +23 -8
  36. package/docs/api/reference/engine.md +22 -10
  37. package/docs/api/reference/geometry.md +161 -82
  38. package/docs/api/reference/grass.md +1 -1
  39. package/docs/api/reference/impostors.md +92 -1
  40. package/docs/api/reference/input.md +3 -3
  41. package/docs/api/reference/lights.md +9 -9
  42. package/docs/api/reference/lines.md +7 -7
  43. package/docs/api/reference/materials.md +48 -48
  44. package/docs/api/reference/math.md +22 -22
  45. package/docs/api/reference/objects.md +9 -9
  46. package/docs/api/reference/points.md +4 -4
  47. package/docs/api/reference/post.md +18 -18
  48. package/docs/api/reference/quality.md +7 -3
  49. package/docs/api/reference/raycast.md +6 -6
  50. package/docs/api/reference/render.md +2 -2
  51. package/docs/api/reference/scene.md +29 -26
  52. package/docs/api/reference/sketch.md +4 -0
  53. package/docs/api/reference/sound.md +203 -0
  54. package/docs/api/reference/sprites.md +5 -5
  55. package/docs/api/reference/terrain.md +2 -1
  56. package/docs/api/reference/textures.md +4 -4
  57. package/docs/api/reference/ui.md +3 -3
  58. package/docs/api/reference/vertex-draws.md +3 -3
  59. package/docs/api/reference/water.md +77 -6
  60. package/docs/api/reference/wind.md +2 -2
  61. package/docs/api/render.md +12 -10
  62. package/docs/api/scene.md +40 -29
  63. package/docs/api/sketch.md +64 -3
  64. package/docs/api/sound.md +169 -0
  65. package/docs/api/splats.md +7 -0
  66. package/docs/api/sprites.md +4 -20
  67. package/docs/api/tasks.md +2 -2
  68. package/docs/api/terrain.md +2 -14
  69. package/docs/api/textures.md +9 -9
  70. package/docs/api/ui.md +3 -15
  71. package/docs/api/water.md +198 -15
  72. package/docs/api/wind.md +5 -5
  73. package/docs/cli/null3d.md +93 -18
  74. package/docs/concepts/architecture.md +10 -10
  75. package/docs/concepts/assets.md +2 -19
  76. package/docs/concepts/backends.md +7 -5
  77. package/docs/concepts/color-management.md +15 -26
  78. package/docs/concepts/culling.md +1 -1
  79. package/docs/concepts/instances.md +5 -5
  80. package/docs/concepts/large-worlds.md +1 -1
  81. package/docs/concepts/lighting.md +50 -55
  82. package/docs/concepts/lod.md +2 -15
  83. package/docs/concepts/materials.md +16 -16
  84. package/docs/concepts/post-processing.md +7 -29
  85. package/docs/concepts/quality-preset-tables.md +53 -50
  86. package/docs/concepts/quality-presets.md +56 -27
  87. package/docs/concepts/render-graph.md +19 -14
  88. package/docs/concepts/render-layers.md +3 -17
  89. package/docs/concepts/shadows.md +10 -28
  90. package/docs/concepts/static-dynamic.md +2 -2
  91. package/docs/cookbook/index.md +1 -1
  92. package/docs/data/threejs-mapping.json +136 -102
  93. package/docs/errors/E1214.md +3 -3
  94. package/docs/errors/E1307.md +23 -0
  95. package/docs/errors/E1420.md +3 -3
  96. package/docs/errors/index.md +3 -2
  97. package/docs/getting-started/cdn.md +17 -1
  98. package/docs/getting-started/first-scene.md +1 -1
  99. package/docs/getting-started/hosting.md +50 -9
  100. package/docs/getting-started/install.md +8 -7
  101. package/docs/getting-started/network-use.md +10 -2
  102. package/docs/getting-started/project-structure.md +15 -6
  103. package/docs/getting-started/third-party-notices.md +8 -6
  104. package/docs/guides/accessibility.md +1 -1
  105. package/docs/guides/add-ons.md +3 -2
  106. package/docs/guides/agents.md +16 -5
  107. package/docs/guides/assets-pipeline.md +13 -12
  108. package/docs/guides/audio.md +109 -124
  109. package/docs/guides/car-paint.md +1 -1
  110. package/docs/guides/content-pages.md +10 -6
  111. package/docs/guides/custom-passes.md +9 -8
  112. package/docs/guides/custom-shaders.md +6 -3
  113. package/docs/guides/debugging.md +78 -4
  114. package/docs/guides/deploying.md +105 -5
  115. package/docs/guides/deterministic-simulation.md +1 -1
  116. package/docs/guides/first-use-shaders.md +4 -4
  117. package/docs/guides/loading-screens.md +78 -33
  118. package/docs/guides/multiple-views.md +41 -2
  119. package/docs/guides/performance.md +6 -3
  120. package/docs/guides/phones.md +12 -1
  121. package/docs/guides/physics.md +1 -1
  122. package/docs/guides/screens.md +7 -5
  123. package/docs/guides/start-time.md +328 -0
  124. package/docs/guides/testing.md +23 -5
  125. package/docs/guides/ui-overlays.md +3 -3
  126. package/docs/index.md +12 -8
  127. package/docs/pages.md +26 -23
  128. package/docs/porting/react-three-fiber.md +228 -5
  129. package/docs/porting/threejs-loop-and-threads.md +220 -6
  130. package/docs/porting/threejs-mapping.md +86 -84
  131. package/docs/porting/threejs-materials.md +212 -5
  132. package/docs/porting/threejs-overview.md +218 -5
  133. package/docs/porting/threejs-postprocessing.md +2 -2
  134. package/docs/porting/threejs-shaders.md +298 -5
  135. package/docs/porting/threejs-unsupported.md +82 -6
  136. package/docs/porting/verification.md +181 -5
  137. package/docs/shaders/builtins.md +1 -1
  138. package/docs/shaders/library.md +63 -39
  139. package/docs/shaders/surface-functions.md +11 -10
  140. package/docs/shaders/wgsl-rules.md +1 -1
  141. package/lib/debug/channel.d.ts +103 -0
  142. package/lib/debug/channel.js +269 -0
  143. package/lib/debug/debug.d.ts +9 -7
  144. package/lib/debug/draw.d.ts +9 -0
  145. package/lib/debug/draw.js +49 -2
  146. package/lib/debug/inspector.d.ts +60 -0
  147. package/lib/debug/inspector.js +475 -0
  148. package/lib/debug/stats-panel.d.ts +1 -1
  149. package/lib/debug/stats.d.ts +2 -3
  150. package/lib/errors/codes.js +8 -2
  151. package/lib/errors/engine-error.js +25 -10
  152. package/lib/errors/fixes.d.ts +1 -0
  153. package/lib/errors/fixes.js +3 -2
  154. package/lib/generated/core.d.ts +1 -1
  155. package/lib/generated/core.js +1 -1
  156. package/lib/gpu/early-builds.d.ts +68 -0
  157. package/lib/gpu/early-builds.js +158 -0
  158. package/lib/gpu/pipeline-name.d.ts +6 -0
  159. package/lib/gpu/pipeline-name.js +23 -0
  160. package/lib/gpu/webgl2/backend.d.ts +63 -2
  161. package/lib/gpu/webgl2/backend.js +226 -8
  162. package/lib/gpu/webgl2/programs.d.ts +18 -0
  163. package/lib/gpu/webgl2/programs.js +4 -1
  164. package/lib/gpu/webgpu/backend.d.ts +89 -2
  165. package/lib/gpu/webgpu/backend.js +255 -8
  166. package/lib/gpu/webgpu/pipelines.d.ts +13 -0
  167. package/lib/gpu/webgpu/pipelines.js +15 -1
  168. package/lib/index.d.ts +6 -4
  169. package/lib/index.js +3 -2
  170. package/lib/math/color.d.ts +5 -5
  171. package/lib/math/color.js +5 -5
  172. package/lib/math/mat4.d.ts +6 -12
  173. package/lib/math/mat4.js +6 -12
  174. package/lib/math/namespaces/quat.d.ts +3 -3
  175. package/lib/math/namespaces/quat.js +3 -3
  176. package/lib/math/quat.d.ts +8 -12
  177. package/lib/math/quat.js +8 -12
  178. package/lib/math/random.d.ts +2 -3
  179. package/lib/math/random.js +2 -3
  180. package/lib/math/types.d.ts +4 -4
  181. package/lib/math/vec3.d.ts +3 -6
  182. package/lib/math/vec3.js +3 -6
  183. package/lib/page/capabilities.d.ts +7 -7
  184. package/lib/page/capabilities.js +39 -25
  185. package/lib/page/check-store.d.ts +14 -2
  186. package/lib/page/check-store.js +34 -11
  187. package/lib/page/engine.d.ts +103 -26
  188. package/lib/page/engine.js +420 -97
  189. package/lib/page/first-frame.d.ts +16 -0
  190. package/lib/page/first-frame.js +35 -0
  191. package/lib/page/frame-stats.d.ts +34 -1
  192. package/lib/page/frame-stats.js +15 -0
  193. package/lib/page/label-loop.d.ts +7 -3
  194. package/lib/page/label-loop.js +17 -10
  195. package/lib/page/labels.d.ts +3 -3
  196. package/lib/page/pipeline-store.d.ts +25 -0
  197. package/lib/page/pipeline-store.js +84 -0
  198. package/lib/page/start-marker.d.ts +4 -1
  199. package/lib/page/start-marker.js +5 -2
  200. package/lib/page/switches.d.ts +32 -2
  201. package/lib/page/switches.js +9 -1
  202. package/lib/quality/check.d.ts +24 -3
  203. package/lib/quality/check.js +21 -7
  204. package/lib/quality/chooser.d.ts +6 -0
  205. package/lib/quality/chooser.js +8 -0
  206. package/lib/quality/governor.d.ts +98 -6
  207. package/lib/quality/governor.js +250 -20
  208. package/lib/quality/preset-docs.d.ts +16 -1
  209. package/lib/quality/preset-docs.js +3 -0
  210. package/lib/quality/presets.d.ts +84 -7
  211. package/lib/quality/presets.js +94 -2
  212. package/lib/render/draw.d.ts +3 -2
  213. package/lib/render/draw.js +15 -5
  214. package/lib/render/load-draw.d.ts +7 -5
  215. package/lib/render/load-draw.js +7 -5
  216. package/lib/render/loss.d.ts +28 -10
  217. package/lib/render/loss.js +50 -22
  218. package/lib/render/recovery.d.ts +20 -0
  219. package/lib/render/recovery.js +44 -4
  220. package/lib/render/renderer.d.ts +85 -9
  221. package/lib/render/renderer.js +181 -42
  222. package/lib/render/scene-renderer.d.ts +19 -1
  223. package/lib/render/scene-renderer.js +25 -1
  224. package/lib/render/webgl2-renderers.js +8 -5
  225. package/lib/render/webgpu-renderers.js +5 -4
  226. package/lib/scene/animation.d.ts +28 -32
  227. package/lib/scene/animation.js +12 -15
  228. package/lib/scene/assets.d.ts +61 -86
  229. package/lib/scene/assets.js +103 -236
  230. package/lib/scene/background.d.ts +14 -18
  231. package/lib/scene/background.js +5 -2
  232. package/lib/scene/color.d.ts +3 -3
  233. package/lib/scene/downloads.d.ts +2 -0
  234. package/lib/scene/downloads.js +371 -0
  235. package/lib/scene/environment.d.ts +7 -7
  236. package/lib/scene/environment.js +1 -1
  237. package/lib/scene/fake-call-values.js +1 -1
  238. package/lib/scene/fog.d.ts +11 -4
  239. package/lib/scene/fog.js +8 -2
  240. package/lib/scene/frame-cameras.d.ts +19 -0
  241. package/lib/scene/frame-cameras.js +58 -0
  242. package/lib/scene/gltf.d.ts +32 -2
  243. package/lib/scene/gltf.js +78 -49
  244. package/lib/scene/grading.d.ts +2 -2
  245. package/lib/scene/grading.js +3 -3
  246. package/lib/scene/ktx2.d.ts +9 -1
  247. package/lib/scene/ktx2.js +3 -2
  248. package/lib/scene/level-rule.d.ts +120 -0
  249. package/lib/scene/level-rule.js +130 -0
  250. package/lib/scene/lines.d.ts +13 -17
  251. package/lib/scene/lines.js +3 -3
  252. package/lib/scene/lut.d.ts +1 -2
  253. package/lib/scene/lut.js +1 -2
  254. package/lib/scene/picks.d.ts +2 -2
  255. package/lib/scene/pointer-events.d.ts +4 -7
  256. package/lib/scene/post.d.ts +38 -43
  257. package/lib/scene/post.js +4 -1
  258. package/lib/scene/prefab.d.ts +7 -8
  259. package/lib/scene/prefab.js +7 -8
  260. package/lib/scene/property-animation.d.ts +1 -1
  261. package/lib/scene/property-animation.js +1 -1
  262. package/lib/scene/queries.d.ts +7 -13
  263. package/lib/scene/resources.d.ts +225 -254
  264. package/lib/scene/resources.js +179 -81
  265. package/lib/scene/scene.d.ts +149 -118
  266. package/lib/scene/scene.js +196 -98
  267. package/lib/scene/serial-loads.d.ts +29 -0
  268. package/lib/scene/serial-loads.js +68 -0
  269. package/lib/scene/shader-preloads.d.ts +34 -0
  270. package/lib/scene/shader-preloads.js +38 -0
  271. package/lib/scene/sprites.d.ts +14 -17
  272. package/lib/scene/sprites.js +5 -6
  273. package/lib/scene/textures.d.ts +11 -12
  274. package/lib/scene/textures.js +9 -10
  275. package/lib/scene/time-of-day.d.ts +3 -4
  276. package/lib/scene/time-of-day.js +2 -3
  277. package/lib/scene/vertex-draws.d.ts +5 -4
  278. package/lib/scene/vertex-draws.js +7 -5
  279. package/lib/shader-artifact.json +1 -0
  280. package/lib/shared/core.d.ts +5 -0
  281. package/lib/shared/core.js +1 -0
  282. package/lib/shared/import-once.d.ts +6 -0
  283. package/lib/shared/import-once.js +19 -0
  284. package/lib/shared/metrics.d.ts +38 -0
  285. package/lib/shared/metrics.js +62 -1
  286. package/lib/shared/run-task.js +7 -1
  287. package/lib/shared/task-pending.d.ts +12 -0
  288. package/lib/shared/task-pending.js +61 -1
  289. package/lib/shared/tasks.js +79 -32
  290. package/lib/shared/version.d.ts +17 -1
  291. package/lib/shared/version.js +19 -1
  292. package/lib/shared/wasm.d.ts +12 -4
  293. package/lib/shared/wasm.js +50 -27
  294. package/lib/shared/worker-start.d.ts +7 -0
  295. package/lib/shared/worker-start.js +27 -0
  296. package/lib/sketch/define-sketch.d.ts +35 -0
  297. package/lib/sketch/input.d.ts +2 -2
  298. package/lib/sketch/preset-check.d.ts +20 -6
  299. package/lib/sketch/preset-check.js +42 -17
  300. package/lib/sketch/quality.d.ts +69 -11
  301. package/lib/sketch/quality.js +54 -13
  302. package/lib/sketch/runner.d.ts +76 -9
  303. package/lib/sketch/runner.js +385 -52
  304. package/lib/sketch/ui.d.ts +5 -8
  305. package/lib/workers/probe-worker.d.ts +5 -0
  306. package/lib/workers/probe-worker.js +13 -4
  307. package/lib/workers/protocol.d.ts +61 -13
  308. package/lib/workers/protocol.js +4 -0
  309. package/lib/workers/render-worker.js +25 -4
  310. package/lib/workers/sketch-worker.js +104 -15
  311. package/package.json +2 -2
@@ -3,12 +3,12 @@ id: guides/debugging
3
3
  title: Debugging
4
4
  status: experimental
5
5
  since: "0.1"
6
- summary: "Error codes; the inspector; the MCP server; the render-graph dump; common failures."
6
+ summary: "Error codes; the in-page inspector; the MCP server; the render-graph dump; common failures."
7
7
  ---
8
8
 
9
9
  # Debugging
10
10
 
11
- > Roadmap step 0.1, first released in null3D 0.1.0. The API is experimental, so it can still change between versions. The in-page inspector and the MCP server come in step 0.3 and are not built yet, so coding agents must not use them.
11
+ > Roadmap step 0.1, first released in null3D 0.1.0. The API is experimental, so it can still change between versions. The in-page inspector and the MCP server run in development builds only.
12
12
 
13
13
  ```mermaid
14
14
  flowchart TD
@@ -45,6 +45,7 @@ The first error in a thread can show its fix a moment later, while the engine do
45
45
  | A callback such as `onUpdate` throws | The console shows each distinct error once, with its stack. The engine carries on, and calls the callback again when it next runs |
46
46
  | After the start, the GPU is lost and cannot come back, or an engine thread fails | `engine.onFailure` receives [E1302](../errors/E1302.md) or [E1404](../errors/E1404.md). The first handler that attaches after the failure still receives it, and the console shows it too. The canvas keeps the last frame: destroy the engine and start a new one |
47
47
  | After the start, the WebGPU device runs out of memory or rejects the engine's work | `engine.onFailure` receives [E1304](../errors/E1304.md) or [E1305](../errors/E1305.md), once per GPU device. The engine draws on without the objects that failed |
48
+ | After the start, the GPU's driver refuses to build a pipeline that draws objects, such as a custom material's | The engine builds it once more, alone, after the other builds. When the driver refuses it again, `engine.onFailure` receives [E1307](../errors/E1307.md), once per GPU device, with the pipeline's name. The engine draws on without the objects that draw with that pipeline. A refused pipeline of a pass of the frame's own, such as the final pass, or a descriptor that the browser's validation refuses, still ends the frames with [E1404](../errors/E1404.md) |
48
49
  | Sketch code runs after `destroy()`, from a timer, a promise or an event listener | The call throws [E1420](../errors/E1420.md). Remove such code in the sketch's `onDestroy` |
49
50
  | Hold mode | The hold stops at the first error, and the result says why. [Testing your sketch](testing.md#when-a-hold-fails) lists the cases |
50
51
 
@@ -95,7 +96,7 @@ Add `engine.report` to a bug report. It holds the result of every test, and the
95
96
 
96
97
  When the page draws although a worker was meant to, `engine.mode.renderFallback` says why. `no-surface` means that the browser cannot draw with the GPU path in a worker. `no-answer` means that the engine's test worker did not answer in time, twice, as on a stalled GPU or a very busy machine. `failed-to-start` means that the test worker's script did not load. A development build also logs a warning with the reason. Drawing on the page works, but the page's own code then shares its thread with the drawing. [Page API: createEngine](../api/engine.md#what-the-engine-reports) describes each value.
97
98
 
98
- Switches in the page's address force a choice, so you can find which path shows a fault. The engine reads them in development builds only:
99
+ Switches in the page's address force a choice, so you can find which path shows a fault. The engine reads them in development builds. A production build reads them only when the Vite plugin's `urlSwitches` option is on:
99
100
 
100
101
  | Switch | Effect |
101
102
  | --- | --- |
@@ -103,8 +104,9 @@ Switches in the page's address force a choice, so you can find which path shows
103
104
  | `?threads=off` | Run the single-threaded build |
104
105
  | `?render=main` | Draw on the page's main thread |
105
106
  | `?latency=low` | Use the low latency mode |
107
+ | `?inspect` | Open the in-page inspector, which lists the scene and edits it live, in a development build ([The in-page inspector](#the-in-page-inspector)) |
106
108
 
107
- [Testing your sketch](testing.md#switches-for-tests) lists every switch.
109
+ [Testing your sketch](testing.md#switches-for-tests) lists more switches.
108
110
 
109
111
  ## See the scene with debug drawing
110
112
 
@@ -139,6 +141,78 @@ The stats overlay shows frame figures over the top-right corner of the canvas. I
139
141
 
140
142
  To test a page's handling of a lost GPU, call `engine.simulateGpuLoss()`. The engine starts a new GPU device and draws the whole scene again, as it does after a real loss. The `gpuLosses` figure of `engine.measure()` counts the losses that the engine carried on after since it started, the simulated ones included.
141
143
 
144
+ ## The in-page inspector
145
+
146
+ `?inspect` opens a panel at the left edge of the page, over the canvas. The panel lists the scene as a tree, with each object's name and kind under its parent. A search box narrows the tree by name or kind. A click selects an object and outlines it in the view. It then shows the object's fields: position, rotation, scale, visibility, the static or dynamic flag and, for a mesh, its base color, roughness, metalness and opacity. The panel also shows the frame figures at its foot, which it reads every second.
147
+
148
+ The panel runs in a development build only, and its code loads when the page first asks for it. A held page and a replay open no panel and publish no bridge. A change lasts until the page reloads. Live edits are for exploring: write the final values into the sketch and its tests.
149
+
150
+ The page publishes the panel's bridge as `window.__null3dInspector`, and the engine's `inspect` method runs the same calls from your own code. Both exist in a development build only:
151
+
152
+ ```ts
153
+ const objects = await engine.inspect?.('objects');
154
+ if (engine.inspect) {
155
+ await engine.inspect('setObject', { selector: 'cr', position: [0, 1, 0] });
156
+ await engine.inspect('setMaterial', { selector: 'cr', values: { color: '#ff0000' } });
157
+ }
158
+ ```
159
+
160
+ Each call that names an object takes a selector, and each call returns a JSON-safe value. A selector is an object's name, its slot as `#<slot>`, `#camera` for the active camera, or a slot number.
161
+
162
+ | Call | Arguments | Answer |
163
+ | --- | --- | --- |
164
+ | `objects` | `{ filter?, limit? }` | `{ objects, count, truncated }`: the live objects, or those whose name or kind holds the filter, at most 50,000. Each gives its slot, handle, name, kind, static or dynamic flag, visibility, layers, parent, child count and, for a mesh, its material's id. `count` counts every match, and `truncated` is true when the list holds fewer |
165
+ | `object` | `{ selector }` | One object, with its position, rotation, scale, world position, flags and material values |
166
+ | `setObject` | `{ selector, position?, rotation?, rotationEuler?, scale?, visible?, dynamic?, layers? }` | The object after the change. `rotation` is a quaternion; `rotationEuler` is X, Y and Z angles in radians |
167
+ | `setMaterial` | `{ selector, values }` | The mesh after the change. `values` takes the material's values, such as `color`, `opacity`, `metalness` and `roughness` |
168
+ | `stats` | none | The frame figures that `debug.frameStats` returns |
169
+ | `errors` | none | The errors that the sketch's code and the engine's frame calls reported, newest last |
170
+ | `graph` | none | The compiled render graph as DOT text |
171
+ | `setQuality` | `{ preset?, settings? }` | The preset and settings after the change |
172
+ | `highlight` | `{ selector? }` | The outlined object, or none |
173
+ | `info` | none | The GPU path, the preset and the render scale |
174
+ | `capture` | none | `{ png, bytes }`: the next frame as a PNG image in base64 text, and its size in bytes. The page answers this call itself |
175
+
176
+ ## The MCP server
177
+
178
+ `bunx @null3d/cli mcp` starts a Model Context Protocol server over stdio. A coding agent lists and changes the scene's objects, captures a frame, and reads the frame figures, the errors and the render graph. It can also switch the quality preset or the GPU path. The page needs a development build, because a production build leaves the inspection channel out.
179
+
180
+ The server reaches the scene in one of three ways:
181
+
182
+ - By default, it starts the project's own Vite dev server and a headless Chrome, as `shot` does. This works on a cloud agent with no window.
183
+ - `--connect <endpoint>` attaches to a Chrome that already runs with a debugging port, and drives the tab that already shows the page. The agent then sees exactly what the developer sees. Start Chrome with `--remote-debugging-port=9222`, and open the page from your Vite dev server. Then run `bunx @null3d/cli mcp --connect http://127.0.0.1:9222`. Every development build publishes the bridge, so the page needs no switch. `?inspect` only opens the panel.
184
+ - `--url <address>` uses a dev server that already runs, and still starts its own headless browser.
185
+
186
+ The other options:
187
+
188
+ - `--page <path>` names the page, `/` by default. With `--connect`, the server drives the tab at that path.
189
+ - `--size <width>x<height>` sets the window of the server's own headless browser, 1280x720 by default.
190
+ - `--gpu <tier>` opens the server's own page on `webgpu`, `compat` or `webgl2`.
191
+ - `--timeout <seconds>` sets how long the page may take to publish the inspector, 60 by default.
192
+
193
+ A held page and a replay publish no bridge, so the server cannot reach them. [The `mcp` command](../cli/null3d.md#mcp) is the full reference of the command.
194
+
195
+ | Tool | What it does |
196
+ | --- | --- |
197
+ | `list_objects` | Lists the live objects, with a filter |
198
+ | `get_object` | Reads one object's transform, flags and material |
199
+ | `set_transform` | Changes an object's transform, visibility, static or dynamic flag, or layers |
200
+ | `set_material` | Changes a mesh's material values |
201
+ | `capture` | Returns a PNG of the current frame |
202
+ | `stats` | Reads the frame figures |
203
+ | `errors` | Reads the engine errors and the console's errors and warnings |
204
+ | `render_graph` | Returns the compiled render graph as DOT text |
205
+ | `set_quality` | Switches the preset, or changes quality settings |
206
+ | `set_gpu` | Switches the GPU path and reloads the page |
207
+
208
+ Register the server in Claude Code with:
209
+
210
+ ```sh
211
+ claude mcp add null3d -- bunx @null3d/cli mcp
212
+ ```
213
+
214
+ Live changes are for exploring. Write the final values into the sketch and the tests, and check them with `bunx @null3d/cli test`.
215
+
142
216
  ## Common failures
143
217
 
144
218
  | What you see | Cause | Fix |
@@ -1,15 +1,115 @@
1
1
  ---
2
2
  id: guides/deploying
3
3
  title: Deploying
4
- status: planned
4
+ status: experimental
5
5
  since: "0.3"
6
6
  summary: "Headers on common hosts; asset caching; size budgets."
7
7
  ---
8
8
 
9
- <!-- null3d:placeholder -->
10
-
11
9
  # Deploying
12
10
 
13
- > Planned for roadmap step 0.3, first released in null3D 0.1.0. This page is a placeholder. No release has this feature yet, so the APIs it names do not exist. Coding agents must not use them.
11
+ > Roadmap step 0.3, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
12
+
13
+ ```mermaid
14
+ flowchart LR
15
+ build["bunx vite build<br/>with the null3D plugin"] --> dist["dist/<br/>the page, assets/, notices"]
16
+ dist --> host["The host sends<br/>the two headers"]
17
+ host --> cache["Long cache lifetime<br/>for assets/"]
18
+ host --> brotli["Brotli for every file,<br/>.wasm included"]
19
+ ```
20
+
21
+ A null3D project ships as the static files of a Vite production build. This page lists what the host must do with those files, so that the engine runs on worker threads and starts fast. [Hosting and cross-origin isolation](../getting-started/hosting.md) explains each step in full.
22
+
23
+ ## Build for production
24
+
25
+ ```sh
26
+ bunx vite build
27
+ bunx vite preview
28
+ ```
29
+
30
+ `bunx vite build` writes the build to `dist/`. The null3D Vite plugin compiles the sketch for its worker, ships the engine's WebAssembly core and drops the engine's development checks. It also writes these files beside the page:
31
+
32
+ | File | What it is for |
33
+ | --- | --- |
34
+ | `null3d-third-party-notices.txt` | The licenses of the code that the engine ships. Publish it with the build, or show its text in your credits ([Third-party notices](../getting-started/third-party-notices.md)). |
35
+ | `null3d-files.json` | The list of the build's files, for a service worker that caches the game for offline play ([Offline play](../getting-started/hosting.md#offline-play)). |
36
+
37
+ `bunx vite preview` serves the build with the same headers and caching as a well-set host. Test a start there, never on the dev server ([A fast start](start-time.md#test-a-production-build)).
38
+
39
+ ## Send the two headers
40
+
41
+ Worker threads need the page to be cross-origin isolated. The host sends two headers with the page:
42
+
43
+ ```text
44
+ Cross-Origin-Opener-Policy: same-origin
45
+ Cross-Origin-Embedder-Policy: require-corp
46
+ ```
47
+
48
+ | Host | Where the headers go |
49
+ | --- | --- |
50
+ | Netlify, Cloudflare Pages | A `_headers` file at the root of the site |
51
+ | Vercel | The `headers` list of `vercel.json` |
52
+ | nginx | `add_header` lines with `always` |
53
+ | GitHub Pages | No custom headers, so the page runs single-threaded |
54
+
55
+ [Headers on common hosts](../getting-started/hosting.md#headers-on-common-hosts) gives each file. Without the headers, the engine loads its single-threaded build. The page still runs, with the same API and features, on one thread. Files from other sites need CORS or `Cross-Origin-Resource-Policy` headers under isolation ([Files from other sites](../getting-started/hosting.md#files-from-other-sites)).
56
+
57
+ Serve the page over HTTPS. WebGPU and shared memory need a secure page. Over plain HTTP, the engine draws with WebGL2.
58
+
59
+ ## Let browsers keep the files
60
+
61
+ Vite names the files in `assets/` with a hash of their content, so a file under a given name never changes. Let browsers keep them for a year, and ask browsers to check the HTML page on each visit:
62
+
63
+ ```text
64
+ /assets/*
65
+ Cache-Control: public, max-age=31536000, immutable
66
+ /*.html
67
+ Cache-Control: no-cache
68
+ ```
69
+
70
+ That is the `_headers` form for Netlify and Cloudflare Pages. Every engine thread loads its own copy of its scripts, so each copy after the first comes from the cache. Without these headers, each copy waits for a round trip.
71
+
72
+ Send each `.wasm` file with `Content-Type: application/wasm`, with no parameter after it. Chrome then keeps the machine code that it compiled from the engine core. A repeat visit skips most of the compile ([Let Chrome keep the compiled core](../getting-started/hosting.md#let-chrome-keep-the-compiled-core)).
73
+
74
+ Send no `Vary: Origin` header with the build's own files. A worker's request carries an `Origin` header that the page's request does not, so `Vary: Origin` makes the browser check the file again.
75
+
76
+ ## Compress with Brotli
77
+
78
+ Brotli shrinks the engine's files more than gzip does, because the engine's shader text repeats far apart. The engine core's WebAssembly file takes 2.0 MB as it is, about 0.8 MB with gzip and about 0.6 MB with Brotli.
79
+
80
+ - Netlify, Cloudflare Pages and Vercel send Brotli to browsers that accept it.
81
+ - GitHub Pages sends gzip only.
82
+ - With nginx, compress each file in `dist/assets/` once with `brotli -q 11`, the `.wasm` files included. Then send them with the `ngx_brotli` module's `brotli_static on;`.
83
+
84
+ Many servers compress only text, and send `.wasm` files as they are. Open the browser's network panel, and compare the core's transferred size with its size ([Send the files with Brotli](../getting-started/hosting.md#send-the-files-with-brotli)).
85
+
86
+ ## Size budgets
87
+
88
+ A page's start downloads the engine, then the files of the first view. Keep each part small:
89
+
90
+ | Part | Size after Brotli | How to check it |
91
+ | --- | --- | --- |
92
+ | The engine's JavaScript at the start | About 130 KB | The network panel |
93
+ | The engine core's WebAssembly | About 0.6 MB | The network panel |
94
+ | Your own models and textures | Your choice | `bunx @null3d/cli assets optimize --report report.json` |
95
+ | An add-on's files after the start | The budgets of the `sizes` gate | `bunx @null3d/cli check-addon` |
96
+
97
+ `assets optimize` makes models and their textures smaller, and its report warns for each quality preset whose texture memory a model passes ([The budget report](assets-pipeline.md#the-budget-report)). Phones and tablets start at the Low and Medium presets, which give textures less memory.
98
+
99
+ To shorten the start, begin the first view's downloads at the top of the setup. Draw the first frame before the large models arrive ([A fast start](start-time.md)).
100
+
101
+ ## Check the deployed page
102
+
103
+ 1. Open the deployed page, and read `crossOriginIsolated` in the browser's console. It is `true` when the headers arrived.
104
+ 2. Read `engine.capabilities.threaded` and `engine.capabilities.tier` in your page, to see which build and GPU path the engine took.
105
+ 3. In the network panel, check that the `.wasm` files arrive compressed, with `Content-Type: application/wasm`.
106
+ 4. Reload the page. The files in `assets/` should come from the cache, with no request to the host.
107
+ 5. Open the page on a phone, over the deployed HTTPS address.
108
+
109
+ ## Related pages
14
110
 
15
- This page will cover: Headers on common hosts; asset caching; size budgets.
111
+ - [Hosting and cross-origin isolation](../getting-started/hosting.md)
112
+ - [A fast start](start-time.md)
113
+ - [Network use](../getting-started/network-use.md)
114
+ - [Try null3D from a CDN](../getting-started/cdn.md)
115
+ - [Phones and tablets](phones.md)
@@ -33,7 +33,7 @@ The maths entry holds the math helpers that use exact arithmetic alone: `+`, `-`
33
33
  | `@null3d/engine/math/quat` | `create`, `set`, `copy`, `identity`, `fromMat4`, `lookAt`, `rotationTo`, `multiply`, `invert`, `normalize` and `dot` |
34
34
  | `@null3d/engine/math/mat4` | `create`, `identity`, `copy`, `multiply`, `invert`, `compose` and `decompose` |
35
35
  | `@null3d/engine/math/math` | `clamp`, `lerp`, `inverseLerp`, `mapLinear`, `smoothstep`, `degToRad`, `radToDeg` and `euclideanModulo` |
36
- | `@null3d/engine/math` | `createMath`, and the types `Vec2Like`, `Vec3Like`, `QuatLike`, `Mat4Like`, `EulerOrder`, `MathFunctions` and `MathHelpers` |
36
+ | `@null3d/engine/math` | `createMath`, and the types `Vec2Like`, `Vec3Like`, `QuatLike`, `Mat4Like`, `EulerOrder`, `MathFunctions`, `MathHelpers`, `NumberHelpers`, `QuatHelpers` and `Vec3Helpers` |
37
37
 
38
38
  Each helper is the same function as the main entry's helper of that name, so the [Math helpers](../api/math.md) page describes it. Import a sub-path as a namespace or by name. A bundler then keeps only the helpers that the simulation calls: six helpers add about 1.2 KB.
39
39
 
@@ -23,14 +23,14 @@ Most shaders come with the engine's start. The shaders of each feature below dow
23
23
  | `'effect_groups'` | the first time custom effects join into one pass, or fold into the final pass. Until its shaders are built, each effect draws a pass of its own |
24
24
  | `'bloom'` | when `post.set` turns bloom on |
25
25
  | `'picking'` | with the first `scene.pick()` call, which waits for the shaders |
26
- | `'ao'` | when `post.set` turns ambient occlusion on, or when the preset draws the screen-space bounce light that reads its steps |
26
+ | `'ao'` | when `post.set` turns ambient occlusion on, or with the first opaque material where the preset draws the screen-space bounce light that reads its steps |
27
27
  | `'dof'` | when `post.set` turns depth of field on |
28
28
  | `'subsurface'` | with the first material whose light spreads under its surface (`subsurfaceRadius`). The skin pass and the blur draw its light |
29
29
  | `'exposure'` | when `post.set` turns automatic exposure on. The camera keeps its own exposure until the meter is built |
30
30
  | `'fog_volume'` | when `scene.setFog` turns the volumetric fog on. The fog keeps its sun glow until the fog is built |
31
31
  | `'taa'` | when `post.set` turns the temporal resolve on, a prototype for measuring only (D-206) |
32
32
  | `'views'` | with the first scene pass. Its texture shows nothing until its shaders are built |
33
- | `'color_copy'` | with the first step that reads the color that the frame before showed: the first material that lets light through, screen-space reflections, or the bounce light that the preset draws from the start of the page |
33
+ | `'color_copy'` | with the first step that reads the color that the frame before showed: the first material that lets light through, screen-space reflections, or the bounce light that the preset draws, whose file downloads with the first opaque material |
34
34
  | `'transmission'` | with the first material that lets light through (`transmission`) |
35
35
  | `'coat_sheen'` | with the first material with a clear coat, a sheen, iridescence or anisotropy |
36
36
  | `'weighted'` | with the first material of the `'weighted'` blending |
@@ -43,7 +43,7 @@ Most shaders come with the engine's start. The shaders of each feature below dow
43
43
  | `'motion'` | when a pass first reads the motion. The motion debug view is such a pass, and it exists in development builds only |
44
44
  | `'background'` | with the first texture, environment or cube map background |
45
45
  | `'sky'` | with the first sky background |
46
- | `'clouds'` | with the first frame that draws the sky's volumetric cloud tier: the preset's `cloudSteps` above 0, or `quality.set({ cloudSteps })`. A page that draws the flat layer downloads none of it. The noise field of its march is made in the browser then |
46
+ | `'clouds'` | with the first sky background with clouds while `cloudSteps` is above 0, as High and Ultra set it, or with the first frame that draws the sky's volumetric cloud tier after `quality.set({ cloudSteps })`. A page that draws the flat layer downloads none of it. The noise field of its march is made in the browser when the tier first draws |
47
47
  | `'physical_sky'` | with the first physical sky background (`atmosphere`). The view shows the background color until the sky is built |
48
48
  | `'skinning'` | with the first skinned mesh, or as soon as the sketch reads a glTF file with skins |
49
49
  | `'morph'` | with the first morphed mesh, or as soon as the sketch reads a glTF file with morph targets. WebGPU morphs in the skinning pass, so there `'morph'` downloads the skinning shaders |
@@ -54,4 +54,4 @@ Most shaders come with the engine's start. The shaders of each feature below dow
54
54
  | `'displacement'` | with the first material with a displacement map, which also loads the builds of parallax. Its mesh draws nothing until they are built |
55
55
  | `'bump'` | with the first material with a bump map |
56
56
  | `'lod_fade'` | with the first mesh whose levels of detail fade, or whose distance bands fade. The quality setting `lodFade` turns the bands' fades off on Low, not a level's fade |
57
- | `'occlusion'` | with the first opaque object that may hide others, while GPU occlusion culling runs on WebGPU. Until its shaders are built, the engine draws without it. WebGL2 has no such shaders |
57
+ | `'occlusion'` | with the first opaque material while GPU occlusion culling runs on WebGPU. Until its shaders are built, the engine draws without it. WebGL2 has no such shaders |
@@ -3,7 +3,7 @@ id: guides/loading-screens
3
3
  title: Loading screens and warm-up
4
4
  status: experimental
5
5
  since: "0.1"
6
- summary: "preload; onProgress; scene.warmUp; shader files that load up front; the preset check; upload budgets; switching presets behind a loading screen."
6
+ summary: "The download list; preload; onProgress; loads one after another; scene.warmUp; shader files that load up front; the preset check during play or before it; upload budgets; switching presets behind a loading screen."
7
7
  ---
8
8
 
9
9
  # Loading screens and warm-up
@@ -12,45 +12,59 @@ summary: "preload; onProgress; scene.warmUp; shader files that load up front; th
12
12
 
13
13
  ```mermaid
14
14
  flowchart LR
15
- start["createEngine starts<br/>the core and the sketch"] --> preload["The sketch preloads its files<br/>and reports progress"]
16
- preload --> setup["The sketch builds its scene<br/>from the files in memory"]
15
+ start["createEngine starts<br/>the core and the sketch"] --> preload["The sketch's listed files download<br/>while the engine starts"]
16
+ preload --> setup["The setup reports progress and<br/>builds its scene from the files"]
17
17
  setup --> warm["await scene.warmUp():<br/>the GPU builds its pipelines"]
18
- warm --> uploads["Textures upload over<br/>the first frames"]
19
- uploads --> check["The preset check:<br/>a lighter preset if the GPU is slow,<br/>skipped on a repeat visit"]
20
- check --> first["createEngine and engine.firstFrame<br/>resolve: remove the loading screen"]
18
+ warm --> first["createEngine and engine.firstFrame<br/>resolve: remove the loading screen"]
19
+ first --> uploads["Textures upload over<br/>the first frames"]
20
+ first --> check["The preset check in the first frames of play:<br/>a lighter preset if the GPU is slow,<br/>skipped on a repeat visit"]
21
21
  ```
22
22
 
23
- A loading screen covers the canvas while the engine starts and the sketch loads its files. It stays while the sketch builds its scene and the GPU gets ready to draw it. The page draws the screen in HTML, and the sketch tells it how far loading has come with messages. Then comes warm-up: the GPU builds a pipeline for each kind of object in the scene. When the engine chose the quality preset itself, it last checks that the GPU draws the scene fast enough at that preset.
23
+ A loading screen covers the canvas while the engine starts and the sketch loads its files. It stays while the sketch builds its scene and the GPU gets ready to draw it. The page draws the screen in HTML, and the sketch tells it how far loading has come with messages. Then comes warm-up: the GPU builds a pipeline for each kind of object in the scene. When the engine chose the quality preset itself, it then checks that the GPU draws the scene fast enough at that preset. By default it checks in the first moments of play, so the loading screen does not wait for it.
24
24
 
25
25
  ## Report progress from the sketch
26
26
 
27
- `assets.preload` downloads a list of files at once, and `assets.onProgress` counts them as they arrive. The loads that follow take the files from memory, so the scene builds without further waits.
27
+ The `downloads` option of `defineSketch` names the files that the setup loads. A live start begins to download them as soon as the sketch's module has loaded, often while the engine's core still downloads. The handler of `assets.onProgress` counts them as they arrive, including the bytes that arrived before the setup added it. The setup's loads take the files that arrived or are on their way, so nothing downloads twice.
28
28
 
29
29
  ```ts
30
30
  // sketch.ts
31
31
  import { defineSketch } from '@null3d/engine';
32
32
 
33
- export default defineSketch(async ({ assets, page, scene }) => {
34
- assets.onProgress((loaded, total) =>
35
- page.post('loading', {
36
- text: 'Downloading the scene files',
37
- value: total > 0 ? loaded / Math.max(total, loaded) : undefined,
38
- }),
39
- );
40
- await assets.preload(['/levels/one.json', '/tex/terrain.png', '/tex/rocks.png']);
41
-
42
- const level = await assets.loadJson('/levels/one.json');
43
- const terrain = await assets.loadTexture('/tex/terrain.png', { wrap: 'repeat', anisotropy: 8 });
44
- // Build the scene from the level and its textures here.
45
- await scene.warmUp();
46
- });
33
+ const LEVEL = '/levels/one.json';
34
+ const TERRAIN = '/tex/terrain.png';
35
+ const ROCKS = '/tex/rocks.png';
36
+
37
+ export default defineSketch(
38
+ async ({ assets, page, scene }) => {
39
+ assets.onProgress((loaded, total) =>
40
+ page.post('loading', {
41
+ text: 'Downloading the scene files',
42
+ value: total > 0 ? loaded / Math.max(total, loaded) : undefined,
43
+ }),
44
+ );
45
+ const [level, terrain, rocks] = await Promise.all([
46
+ assets.loadJson(LEVEL),
47
+ assets.loadTexture(TERRAIN, { wrap: 'repeat', anisotropy: 8 }),
48
+ assets.loadTexture(ROCKS),
49
+ ]);
50
+ // Build the scene from the level and its textures here.
51
+ await scene.warmUp();
52
+ },
53
+ { downloads: [LEVEL, TERRAIN, ROCKS] },
54
+ );
47
55
  ```
48
56
 
57
+ List the files in the order that the setup needs them, and name only the files that the first frame waits for. Two listed files download at a time, so the engine's own files, such as the first frame's shaders, still find free connections to the server. A load of a later file moves it to the front of the list. Up to four listed files then download while loads wait for them. [The download list](../api/sketch.md#the-download-list) gives the rules. Start the files that the first frame does not need when the sketch's `engine.firstFrame` resolves. They then leave the network to the first frame's files ([The engine field](../api/sketch.md#the-engine-field)). For a later stage that loads during play, `assets.preload` downloads its files together when the call runs.
58
+
59
+ A development build watches the setup's loads. It warns once when a load started its download only after another load's file had arrived, while no other file downloaded. The warning names both files. Start both loads before you await either, as `Promise.all` does above, or name both in `downloads`. When the second address comes from the first file's contents, the order is needed: pass `serialLoadWarning: false` to turn the warning off ([Loads one after another](../api/sketch.md#loads-one-after-another)).
60
+
49
61
  The handler gets the bytes downloaded so far and the bytes expected so far. The total is the length of each response that announced one, so it grows as the scene starts more files. A file that fails counts as done, so the bar still reaches its end. The failed load rejects with an error that says what went wrong. [Assets](../api/assets.md) covers the loading calls and their errors.
50
62
 
63
+ [A fast start](start-time.md) gives what each of these steps saved on a phone's connection.
64
+
51
65
  ## Show progress on the page
52
66
 
53
- The page passes `onSketchMessage` to `createEngine`, so it hears the messages that the sketch sends during its setup. It removes the loading screen when `engine.firstFrame` resolves: until the GPU has finished the first frame, the canvas is blank.
67
+ The page passes `onSketchMessage` to `createEngine`, so it hears the messages that the sketch sends during its setup. It removes the loading screen when `engine.firstFrame` resolves: until the GPU has finished the first frame, the canvas is blank. When the promise rejects, the engine stopped before that frame, and the screen shows the error instead.
54
68
 
55
69
  A percentage must come from work that is counted. `onProgress` gives the engine core's bytes, measured against the length that the server announced. `assets.onProgress` gives a scene's files the same way. Neither knows how much of the whole start is left. So the page measures each phase on its own, and it names the phase beside its number. Within a phase the percentage never falls. A scene's total grows as it starts more files, so the bytes that arrived can be a smaller share than before. Across the whole start the figure can fall: a counted phase that ends returns the card to the engine's stage, and a later file starts a new measure under the same label.
56
70
 
@@ -86,8 +100,13 @@ const engine = await createEngine({
86
100
  if (name === 'loading') show(message.text ?? '', message.value);
87
101
  },
88
102
  });
89
- await engine.firstFrame;
90
- document.querySelector('#loading')?.remove();
103
+ try {
104
+ await engine.firstFrame;
105
+ document.querySelector('#loading')?.remove();
106
+ } catch (error) {
107
+ // The engine stopped before its first frame: say why on the loading screen.
108
+ show((error as Error).message);
109
+ }
91
110
  ```
92
111
 
93
112
  A stage without a countable total shows no percentage. Keep the bar moving, or show a count that the engine does know. Do not take a number from a timer or from the stage's place in the start.
@@ -102,7 +121,7 @@ The GPU draws each object with a pipeline: compiled shaders, and the drawing sta
102
121
 
103
122
  Pipelines take time to build. In the engine's warm-up test, a scene of ten pipelines took up to 0.4 seconds to build in Chrome and Safari on a MacBook Pro. It took 0.8 seconds on WebGPU in Firefox.
104
123
 
105
- The engine's benchmark scenes and demos need 2 to 4 pipelines each. In the engine's warm-up time test, they took these times on a phone and a tablet:
124
+ The engine's benchmark scenes need 2 to 4 pipelines each. In the engine's warm-up time test, they took these times on a phone and a tablet:
106
125
 
107
126
  | Device and GPU path | First visit | Repeat visit |
108
127
  | --- | --- | --- |
@@ -112,6 +131,8 @@ The engine's benchmark scenes and demos need 2 to 4 pipelines each. In the engin
112
131
 
113
132
  On a first visit, the browser compiles every shader. On a repeat visit, it reuses what it compiled before. The S4 benchmark, a town with a shadow-casting sun, was the slowest scene on each device.
114
133
 
134
+ A larger scene needs many more pipelines, because each pass, effect and custom material adds its own. A custom material's shader holds the whole lit shading code around its own function, so its pipelines are among the slowest to build. The showcase scenes' first frames build 46 to 63 pipelines on a phone, and 73 to 84 on a MacBook Pro at High. On a phone they take most of the start. They took 1.8 to 5.4 seconds on a Galaxy S25, and 11.8 to 16.5 seconds on an iPhone 17. A Galaxy Tab A9 Plus took up to 23.5 seconds. Safari reused its compiled shaders on a repeat visit. Chrome's own cache of compiled pipelines on Android is too small to keep a scene's worth. There the engine's stored list does the work, as [Repeat visits](#repeat-visits) says. [A fast start](start-time.md#pipelines-on-phones) gives each figure.
135
+
115
136
  The engine builds pipelines without blocking any thread:
116
137
 
117
138
  - On WebGPU, the browser builds each pipeline in the background.
@@ -119,21 +140,42 @@ The engine builds pipelines without blocking any thread:
119
140
 
120
141
  The first frame waits until every pipeline that it needs is built, so it shows the whole scene. After the first frame, the engine draws each frame at once. An object whose pipeline is still building draws nothing until the pipeline is built, and the rest of the scene draws as usual.
121
142
 
143
+ ### Repeat visits
144
+
145
+ The engine keeps a list of the pipelines that the first frame of each sketch built, in the page's `localStorage`. On the next start in the same browser, the thread that draws builds them as soon as it has its GPU. The engine downloads and the setup runs meanwhile. Without the list, the builds start only when the setup's first frame asks for them. A custom material's pipelines start when the setup creates the material and its shader arrives. The first frame then takes the pipelines that are built, or waits for the rest.
146
+
147
+ On a Galaxy Tab A9 Plus in Chrome, the island showcase's repeat visits drew their first frame 8.8 to 11.2 seconds after the setup. Without the list, they took about 23.6 seconds.
148
+
149
+ The list applies to a start of the same sketch on the same GPU path, at the same quality preset. A scene that changed since the last start builds some pipelines that no frame takes, and its first frame stores a new list. Hold mode neither reads nor stores a list. The `?builds=fresh` switch makes the first frame start every build itself, as on a first visit.
150
+
122
151
  ## The first frame
123
152
 
124
- `engine.firstFrame` resolves once the GPU has finished the first frame, with every pipeline built. The promise of `createEngine` resolves once the sketch's setup has run, and after the preset check when the engine runs one. Either can come first. So remove the loading screen only once both have resolved, as the page above does.
153
+ `engine.firstFrame` resolves once the GPU has finished the first frame after the setup, with every pipeline built. It rejects when the engine stops drawing first, with the failure that ended its frames. So a loading screen that waits for it can say why the scene never came. The promise of `createEngine` resolves once the sketch's setup has run. Either can come first. So remove the loading screen only once both have resolved, as the page above does. Neither waits for the preset check, unless the page asks for the check before play, as the next section says.
125
154
 
126
155
  A setup function that awaits `scene.warmUp()` after it creates the scene, as the sketch above does, lets the engine build the pipelines while the setup runs. The engine then draws the first frame as soon as they are built.
127
156
 
128
157
  ## The preset check
129
158
 
130
- When the page leaves the quality preset to the engine, the engine checks its choice after the setup. For about three quarters of a second, it draws the scene that the setup built and measures the frame rate. The sketch's `onUpdate` does not run yet. Where the GPU cannot hold the [target frame rate](../concepts/quality-presets.md#the-target-frame-rate), the engine lowers the preset and measures again. By default the target is the display's rate, up to 60 frames per second. The loading screen hides these frames.
159
+ When the page leaves the quality preset to the engine, the engine checks its choice in the first frames of play. For about three quarters of a second, it measures the frame rate while the sketch plays. The sketch's `onUpdate`, its input and its animations run as usual. Where the GPU cannot hold the [target frame rate](../concepts/quality-presets.md#the-target-frame-rate), the engine lowers the preset and measures again. By default the target is the display's rate, up to 60 frames per second. Once the check has ended, `engine.presetChecked` resolves, and `engine.mode.preset` holds the preset that it chose.
160
+
161
+ So the loading screen goes at the first frame, and a lighter preset applies in front of the viewer. The pixel ratio, the shadows and the anti-aliasing change. The last frame stays on screen while the new preset's pipelines build. A page that would rather hide that change asks for the check before play:
162
+
163
+ ```ts
164
+ // page.ts
165
+ const engine = await createEngine({
166
+ canvas: document.querySelector('canvas')!,
167
+ sketch: new URL('./sketch.ts', import.meta.url),
168
+ presetCheck: 'before-play',
169
+ });
170
+ ```
171
+
172
+ The engine then draws the scene that the setup built, and measures it before the sketch's `onUpdate` runs. The promise of `createEngine` resolves after the check, and the loading screen hides its frames. The viewer waits for them: the check takes most of the start on a tablet. An 11-inch iPad Pro measured this with the check before play. Each benchmark scene and demo showed its first frame about 1 second after the call to `createEngine`. It took about 2 seconds when the check lowered the preset once. During play, the first frame shows as soon as the GPU has drawn it. On a MacBook Pro, the instances demo's loading screen left 0.49 seconds after navigation on a first visit. With the check before play, it left after 1.18 seconds. A phone starts at Low, which has no lighter preset, so the engine skips the check. On a Galaxy S24+, the first frame showed after 0.23 to 0.43 seconds. [Quality presets](../concepts/quality-presets.md#the-preset-check) gives the rules.
173
+
174
+ The engine stores the check's result in the page's `localStorage`. A repeat visit in the same browser on the same device takes that result. Its setup runs at the checked preset, and the check does not run. Only the first visit, and the first after a week, runs the check. A viewer who leaves during the check loses none of the presets it measured. The next visit starts at the preset that the check had lowered to, and its check goes on from there. On the iPad, the first frame of a repeat visit showed after 0.25 to 0.52 seconds. [Quality presets](../concepts/quality-presets.md#repeat-visits) says when a stored result applies, and [Caches between visits](../getting-started/hosting.md#caches-between-visits) shows the other caches that a repeat visit uses.
131
175
 
132
- The check takes most of the start on a tablet. On an 11-inch iPad Pro, the first frame of each benchmark scene and demo showed about 1 second after `createEngine` was called. It took about 2 seconds when the check lowered the preset once. A phone starts at Low, which has no lighter preset, so the engine skips the check. On a Galaxy S24+, the first frame showed after 0.23 to 0.43 seconds. [Quality presets](../concepts/quality-presets.md#the-preset-check) gives the rules.
176
+ So build the whole first view in the setup, with its textures. The check measures the scene from its first frame, and waits while textures upload. A sketch whose setup leaves the scene empty gets a preset that the scene may not hold.
133
177
 
134
- The engine stores the check's result in the page's `localStorage`. A repeat visit in the same browser on the same device takes that result. Its setup runs at the checked preset, and the check does not run. Only the first visit, and the first after a week, waits for the check. On the iPad, the first frame of a repeat visit showed after 0.25 to 0.52 seconds. [Quality presets](../concepts/quality-presets.md#repeat-visits) says when a stored result applies, and [Caches between visits](../getting-started/hosting.md#caches-between-visits) shows the other caches that a repeat visit uses.
135
178
 
136
- So build the whole first view in the setup, with its textures: the check measures what the setup built, and waits while textures upload. A sketch whose setup leaves the scene empty gets a preset that the scene may not hold.
137
179
 
138
180
  ## A later loading stage
139
181
 
@@ -149,7 +191,7 @@ await scene.warmUp(); // every pipeline that the scene needs is built
149
191
  for (const piece of pieces) piece.setVisible(true);
150
192
  ```
151
193
 
152
- `scene.warmUp()` builds the pipelines of every object in the scene, hidden objects too, and resolves once they are all built. The scene keeps drawing while it waits. Without a warm-up, each new object appears when its pipeline is built, which can be a few frames after the others.
194
+ `scene.warmUp()` builds the pipelines of every object in the scene, hidden objects too, and resolves once they are all built. The scene keeps drawing while it waits. An object of a new kind can need more than one pipeline. Where the scene draws the depth prepass, the object also gets a depth pipeline. On a computer's WebGPU at High and Ultra it gets a third, for the occlusion culling's depth pass. So `engine.measure()` can count 2 or 3 pipelines for one new object ([The depth prepass](../concepts/quality-presets.md#the-depth-prepass)). Without a warm-up, each new object appears when its pipeline is built, which can be a few frames after the others.
153
195
 
154
196
  In hold mode, the engine draws one frame, which waits for its pipelines, so `scene.warmUp()` resolves at once.
155
197
 
@@ -170,7 +212,7 @@ const engine = await createEngine({
170
212
  });
171
213
  ```
172
214
 
173
- The listed files download beside the engine's own shaders, so the start waits only for the largest file. The list also loads the file of each pair or group of listed features whose shaders need them together. Their shaders compile before the first frame, as far as the files alone allow. On the Mac, a list of three features added about 70 ms to the start. When the sketch turns a listed feature on during play, the engine downloads nothing. On WebGL2 the feature's first objects then draw at once. On WebGPU a new object's pipeline still needs the scene's targets and its material's state, so it can take a frame to build. Skinned meshes wait for their pipelines in the same way: which ones they need depends on their materials. To build those too before the loading screen goes, create the objects in the setup and `await scene.warmUp()`, as "A later loading stage" shows. The `createEngine` call throws E1421 for a name it does not know.
215
+ The listed files download beside the engine's own shaders, so the start waits only for the largest file. Their downloads start right after the engine tests the GPU, while the engine's core downloads. The list also loads the file of each pair or group of listed features whose shaders need them together. Their shaders compile before the first frame, as far as the files alone allow. On the Mac, a list of three features added about 70 ms to the start. When the sketch turns a listed feature on during play, the engine downloads nothing. On WebGL2 the feature's first objects then draw at once. On WebGPU a new object's pipeline still needs the scene's targets and its material's state, so it can take a frame to build. Skinned meshes wait for their pipelines in the same way: which ones they need depends on their materials. To build those too before the loading screen goes, create the objects in the setup and `await scene.warmUp()`, as "A later loading stage" shows. The `createEngine` call throws E1421 for a name it does not know.
174
216
 
175
217
  The engine does not put every feature into the start's file. That file would then grow for every page, also for the pages that never use a feature. A list keeps the cost on the games that ask for it.
176
218
 
@@ -204,6 +246,7 @@ Keep the textures of the first view small, so the first frames show them. Or wai
204
246
  | `load.warmUpMs` | Time from the start of the first frame's pipeline builds until none was building |
205
247
  | `load.firstDrawMs` | Time the first frame's draw took, with the compiles that it waited for |
206
248
  | `load.firstFramePipelines` | The pipelines that the first frame built |
249
+ | `load.drawingStart` | When the thread that draws got its GPU device, its shader file, the core and its renderer, in milliseconds from the page's navigation. It shows which of them held up the first frame |
207
250
  | `pipelines` | The pipelines built during the measurement, which stays at 0 in steady play |
208
251
  | `skippedDraws` | Draws that frames skipped because their pipeline was still building, so their objects were missing. A warm-up before new objects show keeps it at 0 |
209
252
 
@@ -211,6 +254,8 @@ Where a browser compiles WebGL2 programs without the extension, `load.warmUpMs`
211
254
 
212
255
  ## Related pages
213
256
 
257
+ - [A fast start](start-time.md): the host's part of a start, downloads that start together, and a first frame before the large models.
258
+ - [Sketch API](../api/sketch.md#the-download-list): the download list, and the warning for loads one after another.
214
259
  - [Assets](../api/assets.md): `assets.preload`, `assets.onProgress` and the loading calls.
215
260
  - [Textures](../api/textures.md): how texture uploads work.
216
261
  - [Performance guide](performance.md): what makes the GPU build a pipeline, and the costs of a frame.
@@ -54,6 +54,47 @@ Each half draws with the aspect ratio of its rectangle, so objects keep their pr
54
54
  - `view.live`, `view.camera` and `view.rect` read the view.
55
55
  - The view takes its camera's layers, and follows them when they change, as a scene pass does.
56
56
 
57
+ ## Picking in a view
58
+
59
+ `camera.screenToRay` and `camera.worldToScreen` map the whole canvas, with the canvas's shape. A view's image has its own camera and shape, so a view has the same two calls:
60
+
61
+ - `view.screenToRay(x, y, ray)` writes the ray from the view's camera through a point of the view's image.
62
+ - `view.worldToScreen(point, out)` writes where a point in the world lies in the view's image: `x`, `y` and the depth.
63
+ - `scene.viewAt(x, y)` gives the view under a point, or `undefined` where only the camera's view shows. Where views overlap, it gives the one on top.
64
+ - `view.contains(x, y)` says whether a point lies in the view's rectangle.
65
+
66
+ All of them count CSS pixels from the canvas's top-left corner, as `input.pointer` does. So pass the pointer's position as it is, with no offset for the rectangle. The lens takes the shape of the view's rectangle, as the view draws it.
67
+
68
+ This sketch picks through whichever image is under the pointer:
69
+
70
+ ```ts
71
+ import { defineSketch, type RaycastHit, vec3 } from '@null3d/engine';
72
+
73
+ export default defineSketch(({ scene, input }) => {
74
+ const camera = scene.createPerspectiveCamera({ fov: 60, position: [0, 2, 6], target: [0, 0, 0] });
75
+ scene.setActiveCamera(camera);
76
+ const top = scene.createPerspectiveCamera({ fov: 40, position: [0, 12, 0.01], target: [0, 0, 0] });
77
+ scene.createView({ camera: top, rect: [0.69, 0.02, 0.29, 0.32] });
78
+
79
+ // Made once, and filled again by each call.
80
+ const ray = { origin: vec3.create(), direction: vec3.create() };
81
+ const hit: RaycastHit = { object: null, instance: -1, point: vec3.create(), normal: vec3.create(), distance: 0, triangle: -1 };
82
+ const options = {};
83
+ return {
84
+ onUpdate() {
85
+ if (!input.wasPressed('Mouse0')) return;
86
+ const { x, y } = input.pointer;
87
+ const view = scene.viewAt(x, y);
88
+ if (view) view.screenToRay(x, y, ray);
89
+ else camera.screenToRay(x, y, ray);
90
+ if (scene.raycast(ray.origin, ray.direction, options, hit)) console.log('picked', hit.point);
91
+ },
92
+ };
93
+ });
94
+ ```
95
+
96
+ A view's calls use the view's camera of the frame that last ran, with its lens as it is now. `camera.screenToRay` at a pointer event uses the camera of the frame that was on screen at that event, and a view's call does not. A click during a fast turn of the view's camera can pick up to a frame of its motion away.
97
+
57
98
  ## Cost
58
99
 
59
100
  Each view culls the scene once more and draws it once more, at its rectangle's pixels. A picture in picture at a tenth of the canvas costs about a tenth of a scene draw. The camera's view always draws first, so a split screen of two halves costs the camera's draw and two view draws.
@@ -70,8 +111,6 @@ No preset switches views off or lowers their count. On a phone, keep a picture i
70
111
 
71
112
  A view draws no object whose material shows a render pass's texture, since a pass cannot read a target that another pass draws into. Keep such objects in the camera's view, or show the pass's texture on a screen in the world instead.
72
113
 
73
- `camera.screenToRay` and `camera.worldToScreen` map the whole canvas, with the shape of the canvas. They do not read a view's rectangle, so a ray through a point of a view's image does not follow that view's camera. They take no view, and picking inside a view's rectangle is not supported yet.
74
-
75
114
  A scene and its passes share 32 views, the camera's included. Reflection probes take six while they capture. `scene.createView` throws E1501 when no view is free, and the same error names the limit when a frame draws too many.
76
115
 
77
116
  ## Related pages
@@ -10,10 +10,12 @@ summary: "Measuring; the frame budget on computers, phones and tablets; common c
10
10
 
11
11
  > Roadmap step 0.1, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
12
12
 
13
- null3D keeps its own work per frame small, so your sketch code usually decides how long a frame takes. This guide shows where frame time goes, how to write per-frame code that stays fast, and how to measure. The figures come from the engine's benchmark scenes, which the engine's repository runs with `bun run bench:run`.
13
+ null3D is built to be faster than three.js, and it beats three.js in five of its six benchmark scenes. It keeps its own work per frame small, so your sketch code usually decides how long a frame takes. This guide shows where frame time goes, how to write per-frame code that stays fast, and how to measure. The figures come from the engine's benchmark scenes, which the engine's repository runs with `bun run bench:run`.
14
14
 
15
15
  Measure in every rendering mode and preset, over the whole camera path, with repeats: [Testing your sketch](testing.md#check-every-rendering-mode) says how.
16
16
 
17
+ This guide covers the frames. [A fast start](start-time.md) covers the wait before the first frame: the host, the downloads and the setup.
18
+
17
19
  ## Where frame time goes
18
20
 
19
21
  ```mermaid
@@ -213,7 +215,7 @@ For a large crowd, use models with fewer vertices, give far characters a longer
213
215
 
214
216
  ## See-through objects
215
217
 
216
- Blended objects draw after the opaque ones, farthest first, and a run of neighbors that share a mesh and a material draws together. Many blended objects therefore cost many draws. A double-sided blended material draws twice, back faces first, as three.js draws it, so its near side always covers its far side. Solid double-sided materials draw once.
218
+ Blended objects draw after the opaque ones, farthest first, and a run of neighbors that share a mesh and a material draws together. Many blended objects therefore cost many draws. A double-sided blended material draws twice, back faces first, so its near side always covers its far side. Solid double-sided materials draw once.
217
219
 
218
220
  The second draw costs about as much as the first. In the S2 benchmark with every box blended and double-sided, the transparent pass took 1.88 ms of GPU time on a Mac with one draw per box, and 4.09 ms with two. For flat surfaces, such as leaves and panes of glass, whose two faces never overlap on screen, set `forceSinglePass: true`. It saves the second draw: [Double-sided blended surfaces](../api/materials.md#double-sided-blended-surfaces). Masked and hashed materials need no sort and no second draw, and neither do materials with alpha to coverage.
219
221
 
@@ -326,7 +328,7 @@ For a quick look while the scene runs, add `?stats` to the page's address, or ca
326
328
  | `pipelines` | GPU pipelines built, which can stall the frame they happen in |
327
329
  | `skippedDraws` | Draws skipped because their pipeline was still building, so their objects were missing from those frames |
328
330
  | `memory` | The engine's WebAssembly memory and the JavaScript heap |
329
- | `load` | The start's times in milliseconds: the GPU probe, the core's download and compile, `createEngine`, and the first frame's submit and finish. It also gives the pipelines that the first frame built, and how long they took |
331
+ | `load` | The start's times in milliseconds: the GPU probe, the core's download and compile, `createEngine`, and the first frame's submit and finish. `load.drawingStart` gives when the thread that draws had its GPU device, its shader file and its renderer. It also gives the pipelines that the first frame built, and how long they took ([A fast start](start-time.md#measure-a-start)) |
330
332
  | `downloadBytes` | The size of the engine core's WebAssembly file as the page downloaded it |
331
333
  | `gpuLosses` | Times the browser took the GPU away and the engine carried on with a new device |
332
334
  | `lostRecords` | Frames that the figures miss, because the page read their records too late. It is 0 in a clean run |
@@ -359,6 +361,7 @@ The engine checks a WebGL2 fence at its next frame callback, so there `gpuLatenc
359
361
  - Measure a production build, as your users get it. Development builds check each call's arguments. In each frame they also check every static object, and a share of the rows of static batches. In the S2 benchmark on a MacBook Pro, the check of static objects cost about 0.06 ms per frame on WebGPU. It cost 0.03 ms on WebGL2. These checks grow with the scene, so a large scene pays more.
360
362
  - Know which build a page runs. `vite build` with the null3D plugin makes a production build, and so does the CDN build's production entry, `lib/index.js`. These run as development builds: the Vite dev server, `bunx @null3d/cli shot` and `test`, and the CDN build's development entry, `lib-dev/index.js`. So does engine code built without the null3D plugin, or with no bundler at all ([Try null3D from a CDN](../getting-started/cdn.md#production-and-development-entries)).
361
363
  - To compare null3D with three.js, time a production build of each engine, on the same page size and the same computer.
364
+ - Start a measurement of steady play once `engine.presetChecked` has resolved. On a first visit the preset check runs in the first frames of play. It can lower the preset inside your window ([The preset check](../concepts/quality-presets.md#the-preset-check)). `bunx @null3d/cli bench` waits for it.
362
365
  - Let the sketch run for several seconds before you measure, so that the browser has optimized your per-frame code.
363
366
  - Keep the page visible, the screen unlocked and the display awake. Safari stops running a page while the Mac is locked.
364
367
  - Compare runs at the same display refresh rate. `refreshHz` records it with each measurement. Runs at 120 and at 144 frames per second differed by about 10% for both engines.