@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
@@ -8,21 +8,29 @@ summary: "loadGltf, registerGltfExtension, loadTexture, loadImageBitmap, loadLut
8
8
 
9
9
  # Assets
10
10
 
11
- > Roadmap step 0.1, first released in null3D 0.1.0. Step 0.2 adds glTF models, color grading tables and environments. The API is experimental, so it can still change between versions.
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
- The `assets` object of the sketch context downloads files and decodes them. Every call returns a promise, and its download and decode run outside the sketch's frames, so a frame never waits for them. The browser decodes images off the main thread.
13
+ The `assets` object of the sketch context downloads files and decodes them. The calls that load and make data return promises. Their downloads and decodes run outside the sketch's frames, so a frame never waits for them. The browser decodes images off the main thread.
14
14
 
15
15
  ```ts
16
16
  import { defineSketch } from '@null3d/engine';
17
17
 
18
- export default defineSketch(async ({ assets, page }) => {
19
- assets.onProgress((loaded, total) => page.post('loading', Math.min(1, loaded / Math.max(total, loaded))));
20
- await assets.preload(['/tex/bricks.png', '/tex/bricks-normal.png', '/levels/one.json']);
21
-
22
- const bricks = await assets.loadTexture('/tex/bricks.png', { wrap: 'repeat' });
23
- const normals = await assets.loadTexture('/tex/bricks-normal.png', { colorSpace: 'linear', wrap: 'repeat' });
24
- const level = await assets.loadJson<{ enemies: number }>('/levels/one.json');
25
- });
18
+ const BRICKS = '/tex/bricks.png';
19
+ const NORMALS = '/tex/bricks-normal.png';
20
+ const LEVEL = '/levels/one.json';
21
+
22
+ export default defineSketch(
23
+ async ({ assets, page }) => {
24
+ assets.onProgress((loaded, total) => page.post('loading', Math.min(1, loaded / Math.max(total, loaded))));
25
+ const [bricks, normals, level] = await Promise.all([
26
+ assets.loadTexture(BRICKS, { wrap: 'repeat' }),
27
+ assets.loadTexture(NORMALS, { colorSpace: 'linear', wrap: 'repeat' }),
28
+ assets.loadJson<{ enemies: number }>(LEVEL),
29
+ ]);
30
+ },
31
+ // The three files start to download as soon as the module has loaded.
32
+ { downloads: [BRICKS, NORMALS, LEVEL] },
33
+ );
26
34
  ```
27
35
 
28
36
  ## The calls
@@ -36,7 +44,7 @@ export default defineSketch(async ({ assets, page }) => {
36
44
  | `loadLut(url)` | A color grading table from a `.cube` or a `.3dl` file, for `post.set({ lut })`. [Color grading tables](#color-grading-tables) says what it reads |
37
45
  | `lutFromData({ size, data })` | A color grading table made from numbers in code, with no file. [Tables from numbers](#tables-from-numbers) says what it takes |
38
46
  | `loadEnvironment(url)` | An `Environment` for `scene.setEnvironment`, from a file of `bunx @null3d/cli assets env` or from an HDR file: Radiance (`.hdr`) or OpenEXR (`.exr`). [Environments](#environments) says what it reads |
39
- | `builtinEnvironment('room')` | The built-in room, the scene of three.js's `RoomEnvironment`, as an `Environment` |
47
+ | `builtinEnvironment('room')` | The built-in room, a white room with boxes and glowing panels for soft, neutral light, as an `Environment` |
40
48
  | `skyEnvironment()` | The light of the scene's sky, as an `Environment` that follows the sky background |
41
49
  | `loadCubemap(urls)` | A `Cubemap` of six images, a sky box for `scene.setBackground`. [Cube maps](#cube-maps) says what it reads |
42
50
  | `loadLightProbeGrid(url)` | A probe grid baked outside the engine, as the options that `scene.setLightProbeGrid` takes. [Baked light](#baked-light) says what it reads |
@@ -46,6 +54,9 @@ export default defineSketch(async ({ assets, page }) => {
46
54
  | `loadBinary(url)` | The file's bytes, as an `ArrayBuffer` |
47
55
  | `preload(urls)` | Nothing: it downloads the files ahead of their loads |
48
56
  | `onProgress(handler)` | A function that removes the handler |
57
+ | `cache(name, version)` | A namespace of the browser's cache for data that the sketch or an add-on makes itself: [The cache of derived data](#the-cache-of-derived-data) |
58
+
59
+ In [hold mode](../guides/testing.md#hold-mode), and while a recording replays, the calls run one at a time in the order the sketch made them. Each starts once the call before it has answered. A load makes its textures, meshes and materials as its files arrive, and the files arrive in another order on each run. A held frame would otherwise draw the scene's content in another order. Several loads at once therefore take longer there. A glTF extension handler's `load` must not wait for another `assets` call in hold mode, since that call waits for the load that runs the handler.
49
60
 
50
61
  ## glTF models
51
62
 
@@ -65,9 +76,9 @@ export default defineSketch(async ({ scene, assets }) => {
65
76
 
66
77
  A model that the tool wrote with `--stages` holds its coarsest level. Each finer level downloads from a file of its own. `loadGltf` returns once the coarse level is ready. The copies that you make then refine as the finer levels arrive. The loader holds back a level that the camera never draws, and fetches it once the camera comes close enough to show it. So a page downloads the levels that its camera shows, and no others. `maxLevels` sets the most levels to load, the file's own counted first. Set 1 to draw the coarse level alone and download no stage. A model with a cluster tree streams the tree's bands the same way, and a band counts as a level. [Levels of detail](../concepts/lod.md#levels-from-the-asset-tool) explains the files.
67
78
 
68
- A worker parses the file outside the sketch's frames, and decodes the images that the file holds there. The first `loadGltf` call downloads the loader and its worker, about 15 KB after Brotli, so a page without glTF files downloads neither. The files that a `.gltf` file names download through `assets`, so `preload` and `onProgress` cover them too. The engine copies the vertex data into its own memory, and keeps no other copy of it.
79
+ A worker parses the file outside the sketch's frames, and decodes the images that the file holds there. The first `loadGltf` call downloads the loader and its worker, about 15 KB after Brotli, so a page without glTF files downloads neither. The worker starts as soon as the loader has arrived, while the model file still downloads. The files that a `.gltf` file names download through `assets`, so `preload` and `onProgress` cover them too. The engine copies the vertex data into its own memory, and keeps no other copy of it.
69
80
 
70
- The `assets` API itself loads with its first call too, so a page that loads no file downloads none of it. Every call answers once that code has arrived. Two calls answer with a function of their own, `onProgress` and `registerGltfExtension`, so they answer at once and register the handler when it arrives.
81
+ The `assets` API itself loads with its first call too, so a page that loads no file downloads none of it. Every promise answers once that code has arrived. Three calls answer at once. Both `registerGltfExtension` and `onProgress` give a function that removes the handler, and `onProgress` registers its handler when the code arrives. The `cache` call gives its namespace, whose reads and writes wait for the code.
71
82
 
72
83
  The prefab turns each part of the file into the engine's own:
73
84
 
@@ -75,11 +86,11 @@ The prefab turns each part of the file into the engine's own:
75
86
  | --- | --- |
76
87
  | A node | A `Group`, or a `Mesh` for a node with a mesh of one material. A node with lights or a mesh of several materials becomes a group with one object for each |
77
88
  | A mesh | One mesh for each material, with its vertex arrays in the types that the file holds them in, `KHR_mesh_quantization` types included. Points and lines are left out; development builds warn about them |
78
- | A material | `materials.standard`, or `materials.unlit` with `KHR_materials_unlit`. `KHR_materials_emissive_strength` sets `emissiveIntensity`. `KHR_materials_specular` sets `specularIntensity`, `specularColor` and their maps, and `KHR_materials_ior` sets `ior`, as three.js's `GLTFLoader` sets them on a `MeshPhysicalMaterial`. An `ior` of 0 becomes 1000, as in three.js. `KHR_materials_transmission` sets `transmission`, and `KHR_materials_volume` sets `thickness`, `attenuationColor` and `attenuationDistance` ([Transmission](materials.md#transmission)). `KHR_materials_dispersion` sets `dispersion`, which separates the light that passes through into colors. Their transmission and thickness textures load into `transmissionMap` and `thicknessMap`, a masked material keeps its transmission, so its cut-out decides which pixels draw and the light from behind fills the rest. A material that also gives specular textures keeps both families: each transmission or thickness texture keeps a row slot of its own and samples a specular slot's texture array. A thickness texture stays out beside a specular color texture, whose sRGB texels never share its array. A layered material keeps each texture unless a layer map fills the array that it samples, and the loader notes the one that stays out. `KHR_materials_diffuse_transmission` sets `diffuseTransmission`, `diffuseTransmissionColor` and their maps ([Light through thin surfaces](materials.md#light-through-thin-surfaces)), with the volume's values too. Such a material leaves out its specular maps, and transmission wins over it. `KHR_materials_clearcoat` sets `clearcoat`, `clearcoatRoughness`, `clearcoatNormalScale` and their maps, and `KHR_materials_sheen` sets `sheenColor`, `sheenRoughness` and their maps ([Clear coat and sheen](materials.md#clear-coat-and-sheen)). `KHR_materials_iridescence` sets `iridescence`, `iridescenceIor`, `iridescenceThicknessRange` and their maps, and `KHR_materials_anisotropy` sets `anisotropy`, `anisotropyRotation` and its map ([Iridescence and anisotropy](materials.md#iridescence-and-anisotropy)). A layered material goes without its specular textures and its diffuse transmission, and a coat roughness or sheen roughness texture that is not the texture of its pair is not drawn. The coat's and the sheen's textures keep their places ahead of the iridescence's and the anisotropy's, whose textures are then noted. The draft `KHR_materials_volume_scatter` sets `subsurfaceRadius` and `subsurfaceColor` ([Light under the surface](materials.md#light-under-the-surface)): each channel spreads `attenuationDistance` times its `multiscatterColor`, as the Khronos sample renderer finds it. A volume without an attenuation distance spreads no light, and `scatterAnisotropy` is not drawn |
89
+ | A material | `materials.standard`, or `materials.unlit` with `KHR_materials_unlit`. `KHR_materials_emissive_strength` sets `emissiveIntensity`. `KHR_materials_specular` sets `specularIntensity`, `specularColor` and their maps, and `KHR_materials_ior` sets `ior`. An `ior` of 0 becomes 1000, a surface that reflects nearly all light. `KHR_materials_transmission` sets `transmission`, and `KHR_materials_volume` sets `thickness`, `attenuationColor` and `attenuationDistance` ([Transmission](materials.md#transmission)). `KHR_materials_dispersion` sets `dispersion`, which separates the light that passes through into colors. Their transmission and thickness textures load into `transmissionMap` and `thicknessMap`, a masked material keeps its transmission, so its cut-out decides which pixels draw and the light from behind fills the rest. A material that also gives specular textures keeps both families: each transmission or thickness texture keeps a row slot of its own and samples a specular slot's texture array. A thickness texture stays out beside a specular color texture, whose sRGB texels never share its array. A layered material keeps each texture unless a layer map fills the array that it samples, and the loader notes the one that stays out. `KHR_materials_diffuse_transmission` sets `diffuseTransmission`, `diffuseTransmissionColor` and their maps ([Light through thin surfaces](materials.md#light-through-thin-surfaces)), with the volume's values too. Such a material leaves out its specular maps, and transmission wins over it. `KHR_materials_clearcoat` sets `clearcoat`, `clearcoatRoughness`, `clearcoatNormalScale` and their maps, and `KHR_materials_sheen` sets `sheenColor`, `sheenRoughness` and their maps ([Clear coat and sheen](materials.md#clear-coat-and-sheen)). `KHR_materials_iridescence` sets `iridescence`, `iridescenceIor`, `iridescenceThicknessRange` and their maps, and `KHR_materials_anisotropy` sets `anisotropy`, `anisotropyRotation` and its map ([Iridescence and anisotropy](materials.md#iridescence-and-anisotropy)). A layered material goes without its specular textures and its diffuse transmission, and a coat roughness or sheen roughness texture that is not the texture of its pair is not drawn. The coat's and the sheen's textures keep their places ahead of the iridescence's and the anisotropy's, whose textures are then noted. The draft `KHR_materials_volume_scatter` sets `subsurfaceRadius` and `subsurfaceColor` ([Light under the surface](materials.md#light-under-the-surface)): each channel spreads `attenuationDistance` times its `multiscatterColor`, as the Khronos sample renderer finds it. A volume without an attenuation distance spreads no light, and `scatterAnisotropy` is not drawn |
79
90
  | A texture | A texture with the file's sampler and texture coordinates. `KHR_texture_basisu` textures load through the KTX2 transcoder |
80
91
  | `EXT_texture_webp`, `EXT_texture_avif` | The texture's WebP or AVIF image, which the browser decodes in the loader's worker. A texture with a KTX2 image takes that first, then WebP, then AVIF. The texture's own image is a fallback for other loaders, and the engine does not download or decode it |
81
92
  | `KHR_texture_transform` | Each map's own transform, in the material's `mapUvTransforms`: its offset, repeat and rotation. A map that the file gives no transform reads the texture coordinates as the mesh gives them |
82
- | `KHR_lights_punctual` | Directional, point and spot lights, in the units of glTF and three.js |
93
+ | `KHR_lights_punctual` | Directional, point and spot lights, in glTF's units: lux for a directional light, and candela for a point or spot light |
83
94
  | `KHR_node_visibility` | A node that the file hides draws nothing until a clip shows it |
84
95
  | `NEEDLE_lightmaps`, `NEEDLE_components` | The light map that a node's Needle `MeshRenderer` names, on the material that draws the node: its radiance, read at the second texture coordinates, and the scale and offset that place it. The texture keeps its RGBM texels, which the material decodes as [Needle's viewer](../guides/baked-light-data.md#needle-light-maps) does. A skybox or reflection entry of the extension is left out |
85
96
  | `KHR_animation_pointer` | Property tracks of a clip, which move a light's intensity, color or range, a material's values, the offset and scale of a base color or emissive texture's transform, and a node's visibility. A pointer the engine does not read leaves its value as the file has it, and development builds warn: [Property animation](animation.md#property-animation) |
@@ -90,7 +101,7 @@ The prefab turns each part of the file into the engine's own:
90
101
  | `KHR_meshopt_compression`, `EXT_meshopt_compression` | The decoded vertex and index data, as the uncompressed file holds it. The first file with meshopt data downloads the decoder, about 6 KB after Brotli. A fallback buffer never downloads |
91
102
  | `KHR_draco_mesh_compression` | The decoded vertices and triangles. Normals and tangents become 8-bit integers, and texture coordinates between 0 and 1 become 16-bit integers. The first file with Draco data downloads Draco's decoder, about 60 KB after Brotli. Development builds warn about each such file and name the command that converts it to meshopt |
92
103
 
93
- Materials follow three.js's `GLTFLoader`. A mesh with vertex colors turns them on, and a mesh without normals shades flat. A mesh without tangents turns the normal map's green channel over. Blended materials write no depth. The engine finds the lights near each surface by their ranges. So a point or spot light without a range ends where its light falls below 0.001 lux.
104
+ A mesh with vertex colors turns them on, and a mesh without normals shades flat. A mesh without tangents turns the normal map's green channel over. Blended materials write no depth. The engine finds the lights near each surface by their ranges. So a point or spot light without a range ends where its light falls below 0.001 lux.
94
105
 
95
106
  | Member | Gives |
96
107
  | --- | --- |
@@ -163,10 +174,10 @@ A file that requires an extension which an add-on reads loads with that add-on's
163
174
 
164
175
  ## Color grading tables
165
176
 
166
- `loadLut` reads the two forms that three.js's `LUTCubeLoader` and `LUT3dlLoader` read, with tables of 2 to 256 texels a side. The table becomes a 3D texture of 4 bytes per texel: 144 KB for a table of 33, and 1.1 MB for one of 65.
177
+ `loadLut` reads two forms of file, `.cube` and `.3dl`, with tables of 2 to 256 texels a side. The table becomes a 3D texture of 4 bytes per texel: 144 KB for a table of 33, and 1.1 MB for one of 65.
167
178
 
168
179
  - A `.cube` file gives `LUT_3D_SIZE`, and the table's values from 0 to 1, red changing fastest. Its `TITLE` becomes the table's `title`. Its domain comes from `DOMAIN_MIN` and `DOMAIN_MAX`, or from DaVinci Resolve's `LUT_3D_INPUT_RANGE`, and becomes `domainMin` and `domainMax`. A table in 1D (`LUT_1D_SIZE`) fails with E1412.
169
- - A `.3dl` file gives whole numbers, blue changing fastest, after an optional line of its input grid, such as `0 64 128 ... 1023`. The grid's points may differ from even steps by one, as rounding leaves them, which three.js's loader refuses. The values' bit depth comes from a Lustre `Mesh` line, or else from the largest value.
180
+ - A `.3dl` file gives whole numbers, blue changing fastest, after an optional line of its input grid, such as `0 64 128 ... 1023`. The grid's points may differ from even steps by one, as rounding leaves them. The values' bit depth comes from a Lustre `Mesh` line, or else from the largest value.
170
181
  - Values outside 0 to 1 are clamped, as the canvas would clamp them.
171
182
  - The first table loads the readers, a file of about 2 KB. The thread that runs the sketch reads the file between frames: a table of 33 takes about 10 ms on a desktop.
172
183
 
@@ -174,7 +185,7 @@ A file that requires an extension which an add-on reads loads with that add-on's
174
185
 
175
186
  ### Tables from numbers
176
187
 
177
- `lutFromData` makes a table from numbers that your code computes, as three.js's `LUTPass` takes a `Data3DTexture` that code fills. It takes the numbers in the order of a `.cube` file's lines, so a file and its numbers make the same table:
188
+ `lutFromData` makes a table from numbers that your code computes. It takes the numbers in the order of a `.cube` file's lines, so a file and its numbers make the same table:
178
189
 
179
190
  ```ts
180
191
  import { defineSketch } from '@null3d/engine';
@@ -213,9 +224,9 @@ export default defineSketch(async ({ assets, post }) => {
213
224
  `loadEnvironment` reads two kinds of file:
214
225
 
215
226
  - The KTX2 file that `bunx @null3d/cli assets env` writes, the fast path. It holds a cube map in `rgb9e5ufloat` or `rgba16float`, with one mip level for each step of roughness. It also holds the nine coefficients of its diffuse light.
216
- - An HDR file of an equirectangular panorama, as three.js's `HDRLoader` and `EXRLoader` read it: a Radiance file (`.hdr`) or an OpenEXR file (`.exr`). The engine filters it on the GPU at load, with the asset tool's steps, as three.js's `PMREMGenerator.fromEquirectangular` does.
227
+ - An HDR file of an equirectangular panorama: a Radiance file (`.hdr`) or an OpenEXR file (`.exr`). The engine filters it on the GPU at load, with the asset tool's steps.
217
228
 
218
- `builtinEnvironment('room')` makes the room that three.js's `RoomEnvironment` builds. `skyEnvironment()` makes the light of the sky that [`scene.setBackground({ sky })`](scene.md#environments-cube-maps-and-the-sky) draws, as three.js's `PMREMGenerator.fromScene` does with its `Sky`. The GPU draws each of them and filters it, so no file downloads. Give any of them to [`scene.setEnvironment`](scene.md#the-environment).
229
+ `builtinEnvironment('room')` makes a white room with six boxes, six glowing panels and one point light. It gives soft, neutral light. `skyEnvironment()` makes the light of the sky that [`scene.setBackground({ sky })`](scene.md#environments-cube-maps-and-the-sky) draws. The GPU draws each of them and filters it, so no file downloads. Give any of them to [`scene.setEnvironment`](scene.md#the-environment).
219
230
 
220
231
  ```ts
221
232
  import { defineSketch } from '@null3d/engine';
@@ -238,6 +249,7 @@ export default defineSketch(async ({ scene, assets }) => {
238
249
  - An HDR map has faces of 256 texels, as the asset tool's default. An image wider than 2,048 texels becomes the averages of squares of its texels first. An unclipped sun, or other light beyond 65,408, keeps its share of the rough levels and the diffuse light. Only the sharpest level stops at 65,408, as in the tool's files.
239
250
  - The OpenEXR reader reads single-part files of scanlines with R, G and B channels, as half floats, floats or whole numbers. It reads every compression but DWAA and DWAB: none, RLE, ZIPS, ZIP, PIZ, PXR24, B44 and B44A. Tiled, deep and multi-part files fail with E1412, as do Radiance files stored from the bottom row up.
240
251
  - `builtinEnvironment` resolves once the code and the shaders that make the room are ready. The GPU then makes the whole map in the next frame, before that frame draws. So the first frame with the room already has its light. That frame takes longer by the map's GPU time: about 20 ms on a MacBook Pro, and 50 to 110 ms on recent phones. Ask for the room while the scene loads. During play, the call makes one long frame.
252
+ - The first call of either starts the download of that code and those shaders at once. They then download while the engine and the scene start.
241
253
  - `skyEnvironment` loads the same code and shaders as the room, and resolves once they are ready. The next frame makes the whole map, in about 10 ms on a MacBook Pro. Until the scene's first sky background, the map shows the sky's defaults.
242
254
  - The sky's map follows the sky. After a change that shows, the map refreshes in 20 stages, one a frame. A sun that turns less than half a degree leaves the map as it is, and so does a color value that drifts by less than 2%. Slowly drifting clouds wait too, and their changes add up. One last refresh captures the sky exactly once it stops changing. The scene draws with the old map until the last stage. Each stage takes about 1 ms of GPU time on a MacBook Pro. The map leaves out the sun's disc, whose light the scene's directional light gives. [Lighting and environment](../concepts/lighting.md#sky-and-backgrounds) gives the detail.
243
255
  - `environment.destroy()` frees the cube map's GPU memory. The scene then draws without it.
@@ -248,7 +260,7 @@ export default defineSketch(async ({ scene, assets }) => {
248
260
 
249
261
  ## Cube maps
250
262
 
251
- `loadCubemap` loads six images into a cube map, as three.js's `CubeTextureLoader` does. Give their addresses in three.js's order: the faces toward +X, -X, +Y, -Y, +Z and -Z. Each face is square, every face has the same size, and a face takes at most 2,048 pixels a side. Give the cube map to [`scene.setBackground`](scene.md#environments-cube-maps-and-the-sky).
263
+ `loadCubemap` loads six images into a cube map. Give their addresses in this order: the faces toward +X, -X, +Y, -Y, +Z and -Z. The engine reads the faces as seen from inside the cube, mirrored across x, so a common six-image sky box shows the right way round. Each face is square, every face has the same size, and a face takes at most 2,048 pixels a side. Give the cube map to [`scene.setBackground`](scene.md#environments-cube-maps-and-the-sky).
252
264
 
253
265
  ```ts
254
266
  import { defineSketch } from '@null3d/engine';
@@ -291,9 +303,11 @@ A relative address resolves against the page's address, in every thread mode, as
291
303
 
292
304
  ## Loading screens
293
305
 
294
- `preload` downloads files ahead of the loads that use them, all at once, and resolves when every file has arrived. The next load of each address takes its file from memory, so it downloads nothing. The files wait in memory until then.
306
+ The files that the setup loads start sooner when `defineSketch`'s `downloads` option names them. A live start then begins to download them as soon as the sketch's module has loaded, before the setup runs ([The download list](sketch.md#the-download-list)). A development build warns when one of the setup's loads waited for another ([Loads one after another](sketch.md#loads-one-after-another)).
307
+
308
+ `preload` downloads files ahead of the loads that use them, all at once, and resolves when every file has arrived. Use it for files that a later stage loads during play. The next load of each address takes its file from memory, so it downloads nothing. The files wait in memory until then.
295
309
 
296
- `onProgress` calls its handler as a download's bytes arrive and when one finishes or fails, with two counts: the bytes downloaded so far and the bytes expected so far, which are the lengths of the responses that gave one and 0 while none has. A response with no length still counts its bytes into the first count, so a page draws `Math.min(1, loaded / Math.max(total, loaded))`. A download that fails counts as finished too, so a loading bar reaches its end: it counts the bytes its response announced, or one byte when it announced none. Each call names the address whose bytes moved.
310
+ `onProgress` calls its handler as a download's bytes arrive and when one finishes or fails. It passes two counts: the bytes downloaded so far and the bytes expected so far. The expected bytes are the lengths of the responses that gave one, and 0 while none has. A response with no length still counts its bytes into the first count, so a page draws `Math.min(1, loaded / Math.max(total, loaded))`. A download that fails counts as finished too, so a loading bar reaches its end. It counts the bytes its response announced, or one byte when it announced none. Each call names the address whose bytes moved. The files of the download list count too, also the bytes that arrived before the setup added the handler.
297
311
 
298
312
  ```ts
299
313
  assets.onProgress((loaded, total, url) => page.post('loading', { loaded, total, url }));
@@ -306,7 +320,9 @@ await assets.preload(['/tex/a.png', '/tex/b.png', '/tex/c.png']);
306
320
 
307
321
  ## Caching
308
322
 
309
- Loads of one address at the same time share one download. Files that `preload` downloaded wait in memory until a load takes them. The HTTP cache keeps everything else, as it does for a page's own requests, so three.js's `THREE.Cache` has no counterpart.
323
+ Loads of one address at the same time share one download. Files that `preload` or the download list downloaded wait in memory until a load takes them. The HTTP cache keeps everything else, as it does for a page's own requests.
324
+
325
+ A download that the network rejects, or that a server answers with a 5xx, is tried once more after a short wait. A tunnel or a proxy can refuse the first request of a session while the same file serves a moment later. A refused answer such as a 404, and a body that arrived but will not read, are reported at once.
310
326
 
311
327
  A game that plays with no network caches its files and the engine's in its own service worker, as [Hosting](../getting-started/hosting.md#offline-play) shows. [Caches between visits](../getting-started/hosting.md#caches-between-visits) shows how each cache fits in.
312
328
 
@@ -359,7 +375,7 @@ Each call rejects with an engine error that says how to fix the problem:
359
375
  | [E1413](../errors/E1413.md) | A file from another origin, whose server did not allow the page to read it |
360
376
  | [E1208](../errors/E1208.md) | A texture option that the engine does not know, or one that a KTX2 file cannot take. Also numbers that `lutFromData` cannot make a table from |
361
377
 
362
- `preload` rejects with the error of the first file that fails. The files that arrived stay in memory, and a later load of the failed file tries again.
378
+ `preload` rejects with the error of the first file that fails. The files that arrived stay in memory, and a later load of the failed file tries again. A file of the download list that fails has no caller to reject. The load that takes it rejects with its error, such as E1411, which names `defineSketch()`'s downloads.
363
379
 
364
380
  ## Models that users upload
365
381
 
@@ -8,7 +8,7 @@ summary: "Perspective and orthographic cameras; a perspective camera's focal len
8
8
 
9
9
  # Cameras
10
10
 
11
- > Roadmap step 0.1, first released in null3D 0.1.0. Step 0.2 adds `screenToRay`, `worldToScreen` and `setFocalLength`. The API is experimental, so it can still change between versions.
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
13
  A camera is the object that the engine draws the scene from. There are two kinds: `scene.createPerspectiveCamera` makes a perspective camera, and `scene.createOrthographicCamera` makes an orthographic one. `scene.setActiveCamera` picks the camera that the canvas shows.
14
14
 
@@ -38,7 +38,7 @@ A perspective camera shows near things larger than far things, as the eye does.
38
38
  - `near` and `far` are the distances to the nearest and farthest things the camera shows. The defaults are 0.1 and 2000.
39
39
  - `setFov(degrees)` and `setNearFar(near, far)` change the lens later. The `fov`, `near` and `far` properties read it.
40
40
 
41
- The defaults match three.js's `PerspectiveCamera`. The aspect ratio follows the canvas in every frame, so a camera needs no call when the canvas changes size.
41
+ The aspect ratio follows the canvas in every frame, so a camera needs no call when the canvas changes size.
42
42
 
43
43
  The field of view goes from 0 to 180 degrees, and the near plane must lie in front of the camera, beyond 0. In development builds, values outside these ranges throw E1108.
44
44
 
@@ -54,7 +54,7 @@ post.set({ dof: { aperture: 1.8, focusDistance: 4 } });
54
54
 
55
55
  [Depth of field](post.md#depth-of-field) takes the active camera's focal length unless its settings give one, so the blur matches the framing. The aperture and the focus distance live in `post.set`'s `dof` settings, because they change only the blur. The [depth of field demo](https://github.com/null3d-engine/null3d/tree/main/examples/camera-lens) runs a dolly zoom. Its lens goes from 35 to 85 mm while the camera backs away, so the board keeps its size.
56
56
 
57
- three.js's `setFocalLength` measures its film gauge, 35 mm by default, along the canvas's longer side. null3D measures the sensor's 24 mm along the height, so the field of view stays the same on every canvas shape. The two agree on a square canvas with three.js's `filmGauge` set to 24. A focal length that is not above 0 throws E1108 in development builds.
57
+ The sensor's 24 mm side lies along the canvas's height, so a focal length gives the same vertical field of view on every canvas shape. A focal length that is not above 0 throws E1108 in development builds.
58
58
 
59
59
  ## Orthographic cameras
60
60
 
@@ -65,10 +65,10 @@ const camera = scene.createOrthographicCamera({ height: 20, near: 1, far: 200, p
65
65
  scene.setActiveCamera(camera);
66
66
  ```
67
67
 
68
- - `height` is the height of the view in world units. The width follows the canvas's aspect ratio in every frame. The default height is 2, as in three.js.
69
- - `left`, `right`, `top` and `bottom` give the four edges instead, as three.js's `OrthographicCamera` takes them. The view then keeps these edges on any canvas, and stretches to fill it. Give all four edges and no `height`.
68
+ - `height` is the height of the view in world units. The width follows the canvas's aspect ratio in every frame. The default height is 2.
69
+ - `left`, `right`, `top` and `bottom` give the four edges instead. The view then keeps these edges on any canvas, and stretches to fill it. Give all four edges and no `height`.
70
70
  - `near` and `far` are the distances along the view to its nearest and farthest planes. The defaults are 0.1 and 2000. The near plane can lie behind the camera, because nothing in the view grows as it gets closer.
71
- - `setOrthoHeight(height)` zooms: a smaller height shows less of the scene, and shows it larger. A view made from four edges scales about its center and keeps its shape, as three.js's `zoom` scales it.
71
+ - `setOrthoHeight(height)` zooms: a smaller height shows less of the scene, and shows it larger. A view made from four edges scales about its center and keeps its shape.
72
72
  - The `height` property reads the view's height. The `width` property reads the width of a view made from edges, and is `undefined` while the width follows the canvas.
73
73
 
74
74
  In development builds, a view with no size throws E1108. So does a far plane that is not beyond the near plane, for both kinds of camera.
@@ -140,6 +140,8 @@ Objects keep their current positions. A ray finds a moving object where it is no
140
140
 
141
141
  `worldToScreen` always uses the camera of the frame that last ran. In `onLateUpdate`, that is the frame being drawn, so a point placed there lines up with the image.
142
142
 
143
+ A view from `scene.createView` draws its camera into a rectangle of the canvas, with the rectangle's shape. `view.screenToRay(x, y, ray)` and `view.worldToScreen(point, out)` map a point of that image. They take CSS pixels from the canvas's top-left corner too. `scene.viewAt(x, y)` gives the view under a point. [Multiple views](../guides/multiple-views.md#picking-in-a-view) shows them.
144
+
143
145
  ## Several cameras
144
146
 
145
147
  A scene can have several cameras, and `setActiveCamera` switches between them. The canvas shows the scene from one camera at a time. Until you pick one, and after the engine removes the active camera, the canvas shows only the background.
@@ -3,12 +3,12 @@ id: api/controls
3
3
  title: "Controls (@null3d/controls)"
4
4
  status: experimental
5
5
  since: "0.1"
6
- summary: "Orbit and map controls (0.1); fly and first-person controls, with pointer lock (0.2); drag, rotate and scale gizmos."
6
+ summary: "Orbit and map controls; fly and first-person controls, with pointer lock; drag, rotate and scale gizmos."
7
7
  ---
8
8
 
9
9
  # Controls (@null3d/controls)
10
10
 
11
- > Roadmap step 0.1, first released in null3D 0.1.0. Step 0.2 adds the fly and first-person controls, and gizmos that move, turn and scale an object. The API is experimental, so it can still change between versions. The three.js options `zoomToCursor`, `keys`, `keyPanSpeed`, `keyRotateSpeed`, `cursor`, `minTargetRadius` and `maxTargetRadius` are not built yet, and neither are the methods `saveState` and `reset`. The gizmos do not limit a position with `minX` to `maxZ` yet. Coding agents must not use these parts.
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
13
  ```mermaid
14
14
  flowchart LR
@@ -18,7 +18,7 @@ flowchart LR
18
18
  update --> gizmo["A held handle moves,<br/>turns or scales an object"]
19
19
  ```
20
20
 
21
- The `@null3d/controls` package moves a camera with the mouse, the wheel, a trackpad, touch and the keyboard, and it moves, turns and scales scene objects with gizmos. Orbit controls turn the camera around a target point, as in a model viewer. Map controls pan over the ground, as in a map or a strategy game. [Fly controls](#fly-controls) move the camera in all directions, as in a flight game. [First-person controls](#first-person-controls) walk it over the ground and look around, as in a first-person game. [Gizmos](#gizmos) draw handles on an object that the user drags, as an editor does. Each takes three.js's option names and defaults. For the same input, each camera control gives the camera the pose that three.js's control of the same name gives it.
21
+ The `@null3d/controls` package moves a camera with the mouse, the wheel, a trackpad, touch and the keyboard, and it moves, turns and scales scene objects with gizmos. Orbit controls turn the camera around a target point, as in a model viewer. Map controls pan over the ground, as in a map or a strategy game. [Fly controls](#fly-controls) move the camera in all directions, as in a flight game. [First-person controls](#first-person-controls) walk it over the ground and look around, as in a first-person game. [Gizmos](#gizmos) draw handles on an object that the user drags, as an editor does.
22
22
 
23
23
  The controls run in the sketch, where the camera is. They add no DOM listeners: `controls.update(dt)` reads [`ctx.input`](input.md) once per frame and moves the camera, or the object that a gizmo holds.
24
24
 
@@ -61,12 +61,13 @@ Pass the whole sketch context, `ctx`, as the first argument. The controls read i
61
61
  | Two fingers | Dolly as they spread or close, and pan as they move | Dolly as they spread or close, and rotate as they move |
62
62
  | Shift, Control or Meta held at the start of a drag | Swap rotate and pan | Swap rotate and pan |
63
63
 
64
- - Rotate turns the camera around the target. A drag as long as the canvas is high turns it once around.
65
- - Dolly moves the camera toward the target or away from it. Each 100 pixels of wheel scroll change the distance by about 5%. A pinch on a trackpad dollies ten times as far as its scroll, as in three.js.
66
- - An orthographic camera zooms instead: it keeps its distance, and the view's height changes by the same factor. three.js changes the camera's `zoom` the same way.
64
+ - Rotate turns the camera around the target, about the +Y axis. A drag as long as the canvas is high turns it once around.
65
+ - Dolly moves the camera toward the target or away from it. Each 100 pixels of wheel scroll change the distance by about 5%. A pinch on a trackpad dollies ten times as far as its scroll.
66
+ - An orthographic camera zooms instead: it keeps its distance, and the view's height changes by the same factor.
67
+ - With `zoomToCursor: true`, the wheel and a pinch zoom toward the point under the pointer, or between the fingers. The camera moves along the ray through that point, and the target moves with it. Over the ground, a camera within 20 degrees of level keeps its target and turns back toward it. A middle drag still dollies toward the target.
67
68
  - Pan moves the camera and the target together. Orbit controls pan in the plane of the screen. A map drag pans over the ground, and keeps the ground point under the pointer.
68
69
  - The wheel does nothing during a drag. Scroll after the release dollies, even when the release and the scroll both come between two frames.
69
- - `mouseButtons` and `touches` change what each button and each count of fingers do, with three.js's names, such as `mouseButtons: { LEFT: 'pan', RIGHT: 'rotate' }` and `touches: { ONE: 'pan', TWO: 'dolly-rotate' }`. Null gives a button no action.
70
+ - `mouseButtons` and `touches` change what each button and each count of fingers do, such as `mouseButtons: { LEFT: 'pan', RIGHT: 'rotate' }` and `touches: { ONE: 'pan', TWO: 'dolly-rotate' }`. Their fields are `LEFT`, `MIDDLE` and `RIGHT`, and `ONE` and `TWO`. Null gives a button no action.
70
71
 
71
72
  ## The page
72
73
 
@@ -77,7 +78,7 @@ The controls read the input that the page forwards. So the page must keep the br
77
78
 
78
79
  ## Options
79
80
 
80
- The options and the controls' properties have three.js's names and defaults. A sketch can change a property at any time, such as `controls.enablePan = false`. The [API reference](#api-reference) describes each one.
81
+ Each option is also a property of the controls, with the same name. A sketch can change a property at any time, such as `controls.enablePan = false`. The [API reference](#api-reference) describes each one and gives its default.
81
82
 
82
83
  | Options | Use |
83
84
  | --- | --- |
@@ -90,22 +91,25 @@ The options and the controls' properties have three.js's names and defaults. A s
90
91
  | `minPolarAngle`, `maxPolarAngle` | Limit how high and how low the camera goes, as angles from +Y |
91
92
  | `minAzimuthAngle`, `maxAzimuthAngle` | Limit how far the camera goes around the target |
92
93
  | `screenSpacePanning` | Pan in the plane of the screen, or over the ground |
94
+ | `zoomToCursor` | Zoom toward the point under the pointer, not toward the target |
95
+ | `cursor`, `minTargetRadius`, `maxTargetRadius` | Keep the target within a sphere around `cursor`, and out of a smaller one |
96
+ | `keys`, `keyPanSpeed`, `keyRotateSpeed` | Change the keys and their speeds, once `listenToKeyEvents()` turns the keys on |
93
97
  | `autoRotate`, `autoRotateSpeed` | Turn the camera slowly while the user does not drag |
94
98
  | `mouseButtons`, `touches` | Change what each button and each count of fingers do |
95
99
 
96
- `controls.target` is an array `[x, y, z]`. Change it in place, for example with `vec3.set(controls.target, 0, 1, 0)` from the [math helpers](math.md). The next `update` turns the camera toward it.
100
+ `controls.target` and `controls.cursor` are arrays `[x, y, z]`. Change it in place, for example with `vec3.set(controls.target, 0, 1, 0)` from the [math helpers](math.md). The next `update` turns the camera toward it.
97
101
 
98
102
  ## Each frame
99
103
 
100
104
  Call `controls.update(dt)` once per frame in `onUpdate`, with the step that `onUpdate` receives. It returns true when the camera or the target moved.
101
105
 
102
106
  - `update` allocates nothing. `tests/image/controls.spec.ts` checks it through every gesture, and `bun run bench:allocation --calls` samples it in every frame.
103
- - With damping, each frame applies a share of the motion that remains. The share suits the frame's step, so the motion takes the same time at every frame rate. At 60 frames per second, it matches three.js.
107
+ - With damping, each frame applies a share of the motion that remains: `dampingFactor` at 60 frames per second. The share suits the frame's step, so the motion takes the same time at every frame rate.
104
108
  - The sketch can move the camera itself with `camera.setPosition`. The controls carry on from the new position at the next `update`.
105
109
  - Auto-rotation stops while the user asks for less motion. [Accessibility](../guides/accessibility.md) explains the preference.
106
110
  - If the camera has a parent, the parent must not be rotated. The controls turn the camera with `lookAt`, which assumes that.
107
111
 
108
- `rotateLeft(angle)`, `rotateUp(angle)`, `pan(deltaX, deltaY)`, `dollyIn(scale)` and `dollyOut(scale)` move the camera from code, and the next `update` applies them. `stop()` drops the motion that damping still carries, so the camera is still from the next `update` on: call it when something else takes the camera, such as a route, or the fling resumes at the handover. `getDistance()`, `getPolarAngle()` and `getAzimuthalAngle()` read the camera's place, as in three.js. Use them to drive the controls with keys or a gamepad:
112
+ `rotateLeft(angle)`, `rotateUp(angle)`, `pan(deltaX, deltaY)`, `dollyIn(scale)` and `dollyOut(scale)` move the camera from code, and the next `update` applies them. `stop()` drops the motion that damping still carries, so the camera is still from the next `update` on: call it when something else takes the camera, such as a route, or the fling resumes at the handover. `getDistance()`, `getPolarAngle()` and `getAzimuthalAngle()` read the camera's place. Use them to drive the controls with keys or a gamepad:
109
113
 
110
114
  ```ts
111
115
  const { input } = ctx;
@@ -119,9 +123,22 @@ return {
119
123
  };
120
124
  ```
121
125
 
126
+ ### Keys
127
+
128
+ The keys are off until the sketch calls `controls.listenToKeyEvents()`, as in three.js. three.js's call takes a DOM element. The sketch already reads the keys, so this call takes none. `stopListenToKeyEvents()` turns them off again.
129
+
130
+ - The arrow keys pan the camera by `keyPanSpeed` pixels of a drag at each step, 7 by default.
131
+ - With Shift, Control or Meta down, the arrow keys turn the camera instead. `keyRotateSpeed` scales each turn.
132
+ - `keys` changes the keys, such as `keys: { LEFT: 'KeyA', UP: 'KeyW', RIGHT: 'KeyD', BOTTOM: 'KeyS' }`. Each is a [key code name](input.md).
133
+ - A press takes one step, as one `keydown` event does in three.js. A held key repeats after half a second, then 30 times a second. The browser's own key repeat sets that rate for three.js, so it can differ from system to system.
134
+
135
+ ### Save and reset
136
+
137
+ `controls.saveState()` keeps the target, the camera's position and an orthographic camera's zoom. `controls.reset()` puts them back. Without a saved state, it puts back the state from when the controls started. As in three.js, `reset` then updates once and ends the user's drag.
138
+
122
139
  ## Fly controls
123
140
 
124
- `createFlyControls(ctx, camera, options)` makes fly controls, which three.js calls `FlyControls`. They move the camera along its own axes and turn it about them, so the camera can loop and roll.
141
+ `createFlyControls(ctx, camera, options)` makes fly controls. They move the camera along its own axes and turn it about them, so the camera can loop and roll.
125
142
 
126
143
  ```ts
127
144
  import { createFlyControls } from '@null3d/controls';
@@ -144,14 +161,15 @@ return {
144
161
  | The pointer's place on the canvas | Turn: not at all at the middle, and at full speed at an edge |
145
162
  | Left and right buttons | Move forward and back |
146
163
 
147
- - `movementSpeed` is in world units per second. `rollSpeed` sets how fast the keys and the pointer turn the camera. At full turn, the camera turns about twice `rollSpeed` radians per second, as in three.js.
164
+ - `movementSpeed` is in world units per second. `rollSpeed` sets how fast the keys and the pointer turn the camera. At full turn, the camera turns about twice `rollSpeed` radians per second.
148
165
  - The pointer steers whenever it moves over the canvas, and its turn holds until it moves again. With `dragToLook: true`, the pointer steers only during a drag, and the buttons do not move the camera.
149
166
  - `autoForward: true` moves the camera forward all the time, unless S or the right button moves it back.
150
- - The keys add to the pointer's turn. In three.js, the pointer's last move replaces the arrow keys' turn.
167
+ - The keys add to the pointer's turn.
168
+ - The camera stores its pose in 32-bit floats, so fly and first-person controls keep their own copy in double precision. At each `update`, they take a pose that the sketch set, and carry on from it.
151
169
 
152
170
  ## First-person controls
153
171
 
154
- `createFirstPersonControls(ctx, camera, options)` makes first-person controls. They take the place of two three.js controls. Without a pointer lock, they act as `FirstPersonControls`. While the canvas holds the [pointer lock](#pointer-lock), the mouse turns the view as `PointerLockControls` turn it.
172
+ `createFirstPersonControls(ctx, camera, options)` makes first-person controls. Without a pointer lock, keys walk and a drag looks around. While the canvas holds the [pointer lock](#pointer-lock), the mouse turns the view.
155
173
 
156
174
  ```ts
157
175
  import { createFirstPersonControls } from '@null3d/controls';
@@ -176,8 +194,9 @@ return {
176
194
 
177
195
  - The keys walk over the ground, in the direction the view faces around +Y. Two keys at once walk no faster than one.
178
196
  - A drag turns the view at a speed that grows with the drag's length, in `lookSpeed` degrees per second for each pixel. While a key walks forward or back, a press only looks.
179
- - Moves and turns start and stop smoothly: each frame applies `dampingFactor` of the change of speed that remains. The share suits the frame's step, so the motion takes the same time at every frame rate. At 60 frames per second, it matches three.js.
180
- - A drag tips the view at most 85 degrees above or below the level. `constrainVertical` with `verticalMin` and `verticalMax` limits it further, and `lookVertical: false` keeps the view level.
197
+ - When two fingers are down, the first finger's movement turns the view.
198
+ - Moves and turns start and stop smoothly: each frame applies a share of the change of speed that remains, `dampingFactor` at 60 frames per second. The share suits the frame's step, so the motion takes the same time at every frame rate.
199
+ - The controls turn the camera with no roll. A drag tips the view at most 85 degrees above or below the level. `constrainVertical` with `verticalMin` and `verticalMax` limits it further, and `lookVertical: false` keeps the view level.
181
200
  - `heightSpeed`, with `heightCoef`, `heightMin` and `heightMax`, walks faster forward the higher the camera is. `autoForward` walks forward along the view all the time.
182
201
  - `lookAt(x, y, z)` turns the camera toward a point.
183
202
 
@@ -196,15 +215,15 @@ canvas.addEventListener('click', () => {
196
215
 
197
216
  An engine started with `input: false` reads no movement, and `engine.requestPointerLock()` fails there with [E1426](../errors/E1426.md). The page's own input code then locks the canvas and reads the movement, as [Input off](input.md#input-off) says.
198
217
 
199
- While the pointer is locked, `ctx.input.pointer.locked` is true, and `pointer.dx` and `pointer.dy` give the mouse's movement ([Input](input.md#the-pointer)). First-person controls then turn the view by the movement, as three.js's `PointerLockControls` do:
218
+ While the pointer is locked, `ctx.input.pointer.locked` is true, and `pointer.dx` and `pointer.dy` give the mouse's movement ([Input](input.md#the-pointer)). First-person controls then turn the view by the movement:
200
219
 
201
220
  - Each pixel turns the view by 0.002 radians times `pointerSpeed`.
202
- - The view stays between `minPolarAngle` and `maxPolarAngle` from +Y.
221
+ - The view stays between `minPolarAngle` and `maxPolarAngle` from +Y. The limits apply after a frame's moves add up.
203
222
  - A drag looks around no more, and the buttons do not walk, so the sketch can use them, for example to fire.
204
223
 
205
224
  The user ends the lock with Esc, and the page with `document.exitPointerLock()`. Destroying the engine also ends it.
206
225
 
207
- To port code that moves the camera itself with `PointerLockControls`, set `movementSpeed: 0`, so the keys do not walk, and call the controls' `moveForward(distance)` and `moveRight(distance)`. As in three.js, `moveForward` walks over the ground at right angles to the camera's right, and `getDirection(out)` gives the direction of the view:
226
+ To move the camera from the sketch's own code, set `movementSpeed: 0`, so the keys do not walk, and call the controls' `moveForward(distance)` and `moveRight(distance)`. The call `moveForward` walks over the ground at right angles to the camera's right, and `getDirection(out)` gives the direction of the view:
208
227
 
209
228
  ```ts
210
229
  const controls = createFirstPersonControls(ctx, camera, { movementSpeed: 0 });
@@ -223,7 +242,7 @@ The [first-person and fly controls demo](https://github.com/null3d-engine/null3d
223
242
 
224
243
  ## Gizmos
225
244
 
226
- `createTransformControls(ctx, camera, options)` draws handles on one object, and a drag of a handle moves, turns or scales that object. three.js calls the same control `TransformControls`. The [gizmos demo](https://github.com/null3d-engine/null3d/tree/main/examples/gizmos) selects one of three pieces and drags its handles.
245
+ `createTransformControls(ctx, camera, options)` draws handles on one object, and a drag of a handle moves, turns or scales that object. The [gizmos demo](https://github.com/null3d-engine/null3d/tree/main/examples/gizmos) selects one of three pieces and drags its handles.
227
246
 
228
247
  ```ts
229
248
  import { createTransformControls } from '@null3d/controls';
@@ -248,8 +267,10 @@ A press on a handle begins a drag, and the pointer then moves, turns or scales t
248
267
  | `'scale'` | A rod with a cube on each axis, and a cube at the center | Scales the object along the axes that the handle names |
249
268
 
250
269
  - `gizmo.axis` names the handle under the pointer, or the one that a drag holds: `'X'`, `'Y'`, `'Z'`, `'XY'`, `'YZ'`, `'XZ'`, `'XYZ'` or `'E'`. `gizmo.object` is the object that `attach` picked.
251
- - `mode` picks the handles, and `space` picks between the world's axes and the object's own. A scale drag always follows the object's own axes, as in three.js.
270
+ - `mode` picks the handles, and `space` picks between the world's axes and the object's own. A scale drag always follows the object's own axes.
252
271
  - `showX`, `showY` and `showZ` hide the handles of an axis, and the planes that it lies in.
272
+ - `showXY`, `showYZ` and `showXZ` hide one plane handle each. As in three.js, a flag hides each handle whose name holds its pair, so `showXY: false` and `showYZ: false` also hide the center cube.
273
+ - `minX`, `maxX`, `minY`, `maxY`, `minZ` and `maxZ` limit a move. The drag clamps the object's position relative to its parent, after any snap, as three.js does. The defaults set no limit.
253
274
  - `translationSnap`, `rotationSnap` and `scaleSnap` round a move in meters, a turn in radians and a scale to steps. Null, the default, gives a free drag.
254
275
  - `size` scales the handles on the screen. They keep the same share of the screen as the camera moves.
255
276
  - A turn follows the pointer around the ring, so the handle stays under it. An object turns by the angle that the pointer sweeps around the axis.
@@ -257,13 +278,13 @@ A press on a handle begins a drag, and the pointer then moves, turns or scales t
257
278
  - The handles draw over the scene, so a handle that lies behind a wall still shows and still takes the pointer.
258
279
  - The handle meshes take `levels: false`. The handles keep one share of the screen, so a level would never switch.
259
280
 
260
- The handles answer a raycast on a layer of their own, so a press tests the gizmo alone. No scene object stands between the pointer and a handle. `layers` sets the layer that the handles draw on, which the controls add to the camera's layers and take away at `destroy`. `pickLayers` sets the layer of the handles that answer a raycast, which no camera draws. Both default to layers out of the way of a scene's own: 30 and 29. Give a camera that draws every layer another pair, or its image shows the pickers.
281
+ The handles answer a raycast on a layer of their own, so a press tests the gizmo alone. No scene object stands between the pointer and a handle. A click on a handle reaches the handle and its parents alone. A click handler on the object behind it does not hear it. `layers` sets the layer that the handles draw on, which the controls add to the camera's layers and take away at `destroy`. `pickLayers` sets the layer of the handles that answer a raycast, which no camera draws. Both default to layers out of the way of a scene's own: 30 and 29. Give a camera that draws every layer another pair, or its image shows the pickers.
261
282
 
262
283
  `gizmo.destroy()` removes the handles and gives the camera back its own layers. `gizmo.detach()` hides the handles and leaves the object where it is.
263
284
 
264
285
  ## Drag controls
265
286
 
266
- `createDragControls(ctx, camera, objects, options)` drags any object of a list over a plane that faces the camera. The right button turns the object instead, about the camera's axes. three.js calls the same control `DragControls`.
287
+ `createDragControls(ctx, camera, objects, options)` drags any object of a list over a plane that faces the camera. The right button turns the object instead, about the camera's axes.
267
288
 
268
289
  ```ts
269
290
  import { createDragControls } from '@null3d/controls';
@@ -280,29 +301,10 @@ return {
280
301
  - A press on an object of the list takes hold of it, and the pointer then drags it over the plane through its place at the press, facing the camera.
281
302
  - `dragger.dragging` is the object that a drag holds, or null. `dragger.hovered` is the object under the pointer, and `dragger.state` is `'pan'` or `'rotate'`.
282
303
  - `recursive` drags a child of a listed object as that object, and `transformGroup` drags the outermost listed object that holds the one under the pointer.
283
- - `mouseButtons` and `touches` change what each button and each finger does, with three.js's names: `{ LEFT: 'pan', RIGHT: 'rotate' }` and `{ ONE: 'pan' }`.
304
+ - `mouseButtons` and `touches` change what each button and each finger does: `'pan'` drags and `'rotate'` turns. The defaults are `{ LEFT: 'pan', MIDDLE: 'pan', RIGHT: 'rotate' }` and `{ ONE: 'pan' }`.
284
305
  - `layers` sets the layers that the pointer's ray tests. The default is layer 0 alone.
285
306
  - `rotateSpeed` scales the turn that a rotate drag makes.
286
307
 
287
- ## Differences from three.js
288
-
289
- - The controls take the sketch context and the camera: `createOrbitControls(ctx, camera, options)`. They need no DOM element, and have no `connect`, `disconnect`, `dispose` or `listenToKeyEvents`.
290
- - `update` takes the frame's step in seconds, and it must run every frame.
291
- - Actions are strings, such as `'rotate'` and `'dolly-pan'`, in place of the numbers of `THREE.MOUSE` and `THREE.TOUCH`.
292
- - The controls send no `change`, `start` or `end` events. `update` returns true when the camera moved.
293
- - `rotateLeft`, `rotateUp`, `pan`, `dollyIn` and `dollyOut` take effect at the next `update`, not at once.
294
- - The controls orbit around +Y. three.js orbits around the camera's `up`, which is +Y unless a page changes it.
295
- - three.js takes a frame's events one at a time. When two fingers move in the same frame, its pan differs from null3D's by a few percent of the move.
296
- - Fly and first-person controls keep the camera's pose in double precision, as three.js does. At each `update` they take a pose that the sketch set, and carry on from it.
297
- - First-person controls turn the camera with no roll. `PointerLockControls` keep a roll that the camera had.
298
- - While the pointer is locked, the mouse's moves in one frame add up before the polar limits apply. three.js applies them after each move, so a frame whose moves reach a limit and come back can end at a different angle.
299
- - `FirstPersonControls` and `PointerLockControls` are one set of controls, so `lock`, `unlock`, `isLocked` and the `lock` and `unlock` events have no equivalent. The page locks the pointer with `engine.requestPointerLock()`, and the sketch reads `input.pointer.locked`.
300
- - When two fingers are down, the first finger's movement turns the view. three.js takes the movement of any finger from where the last finger touched down.
301
- - Gizmos need no `getHelper()`: the controls create their handles in the scene, and `destroy` removes them. A click on a handle reaches the handle, and it goes on to the handle's parents alone, so a click handler on the object behind it does not hear it.
302
- - A gizmo reports what it does through `dragging` and `axis` instead of the `dragging-changed`, `objectChange`, `mouseDown` and `mouseUp` events. A drag control reports `dragging`, `hovered` and `state` instead of `dragstart`, `drag`, `dragend`, `hoveron` and `hoveroff`. `update` returns true when it moved something.
303
- - A turn with a gizmo uses the exact angle that the pointer sweeps around the axis. three.js scales a linear approximation of that angle, so the two can differ by a few degrees in a long drag.
304
- - `showXY`, `showYZ` and `showXZ` have no counterparts: `showX`, `showY` and `showZ` hide the planes too. `minX` to `maxZ` are not built yet.
305
-
306
308
  ## Related pages
307
309
 
308
310
  - [Input](input.md): the pointer, the wheel and the touches that the controls read.
package/docs/api/debug.md CHANGED
@@ -3,12 +3,12 @@ id: api/debug
3
3
  title: Debug drawing and stats
4
4
  status: experimental
5
5
  since: "0.1"
6
- summary: "debug.line, box, sphere, arrow, axes, grid, frustum, light and skeleton; the stats overlay, debug.frameStats and @null3d/engine/stats; engine.measure and its figures; debug.view, the ?view switch and add-on views; debug.shadowCamera."
6
+ summary: "debug.line, box, sphere, arrow, axes, grid, frustum, light and skeleton; the stats overlay, debug.frameStats and @null3d/engine/stats; engine.measure and its figures; debug.view, the ?view switch and add-on views; debug.shadowCamera; the in-page inspector and engine.inspect."
7
7
  ---
8
8
 
9
9
  # Debug drawing and stats
10
10
 
11
- > Roadmap step 0.1, first released in null3D 0.1.0. Step 0.2 adds `debug.skeleton` and `@null3d/engine/stats`. It also adds the stats overlay's page switches, its options and card, and its figures of GPU time, triangles, objects and memory. The API is experimental, so it can still change between versions.
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
13
  Debug drawing shows where things are in the scene: lines, boxes, spheres, arrows, axes, grids, camera frustums, lights and skeletons. Debug views draw the whole scene with one debug shading, such as its normals, its wireframe or its shadows. The overlay of `debug.stats` shows the engine's frame figures over the top-right corner of the canvas, and `debug.frameStats` gives them to the sketch. On the page, `engine.measure` measures the running engine.
14
14
 
@@ -52,10 +52,10 @@ export default defineSketch(({ scene, materials, geometry, debug }) => {
52
52
  | `sphere(center, radius, color)` | Three circles around the center, one in each plane of the world's axes | Yellow |
53
53
  | `arrow(origin, direction, length, color)` | A line from `origin` with a head at its tip, 1 meter long unless `length` says otherwise | Yellow |
54
54
  | `axes(target, size)` | The x, y and z axes, `size` meters long, at a position or on an object | Red, green and blue |
55
- | `grid(size, divisions, options)` | A square grid on the horizontal plane, as three.js's `GridHelper` draws it | Gray, darker through the center |
55
+ | `grid(size, divisions, options)` | A square grid on the horizontal plane, `size` meters wide, with `divisions` cells along each side | Gray, darker through the center when `divisions` is even |
56
56
  | `frustum(camera, color)` | The near and far planes of a camera's view, and the edges between them | Orange |
57
57
  | `light(light, options)` | A directional light: a square that faces the light, and an arrow in the direction its light travels | The light's color |
58
- | `skeleton(object, color)` | The joints of an animated object: a line from each joint of a skin to its parent joint, as three.js's `SkeletonHelper` draws them | Blue at the joint, green at its parent |
58
+ | `skeleton(object, color)` | The joints of an animated object: a line from each joint of a skin to its parent joint | Blue at the joint, green at its parent |
59
59
 
60
60
  `grid` takes `center`, `color` and `centerColor`, gray (`'#888888'`) and darker gray (`'#444444'`) by default. `light` takes `position`, `size` in meters (1 by default) and `color`.
61
61
 
@@ -71,7 +71,7 @@ Debug drawing works in development builds only. In a production build, every dra
71
71
 
72
72
  ### Limits
73
73
 
74
- - Lines are one pixel wide on every GPU, because WebGPU draws lines no wider. Wide lines come with [lines](lines.md), from step 0.2.
74
+ - Lines are one pixel wide on every GPU, because WebGPU draws lines no wider. For wide lines, use [lines](lines.md).
75
75
  - A frame draws at most 131,072 lines. The engine leaves out the lines after that, and warns once in the console.
76
76
  - A frame without debug drawing runs no debug pass, uploads nothing and allocates nothing.
77
77
 
@@ -127,7 +127,7 @@ export default defineSketch(({ debug, input }) => {
127
127
  - The wireframe view keeps an edge list for each mesh once it has shown, which takes twice the GPU memory of the mesh's triangle indices.
128
128
  - The levels view reads the level of each draw from the culling that picks it. Each level is a bucket of its own, so the view needs no shader for each material. Its first frame builds the pipelines that carry the level code, so the levels can be missing for a few frames after a change.
129
129
  - `debug.cullingSpheres(true)` draws the bounding sphere that culling tests for each of the first 128 objects whose mesh has levels, until `debug.cullingSpheres(false)`. Use it beside the levels view to see why the size rule hid a copy. It reads the mesh's own sphere around the object's origin, so it does not draw the sphere that `setBounds` gave an object. Instance batch rows draw no sphere. `debug.cullingSpheresDrawn()` gives the spheres that the last frame drew, which is zero while the switch is off, so a page can tell that the switch drew them rather than nothing.
130
- - Debug views work in development builds only. In a release build, `debug.view` does nothing, and the build holds none of the views' shaders or drawing code. A few small parts stay, because the engine shares them: the scene's traversal of objects whose meshes have levels, and the culling's level code. Together they add about 2.4 KB after Brotli to a single-threaded build, and less to a pipelined one. A name that the engine does not know, and that no add-on feature registers, fails with [E1213](../errors/E1213.md).
130
+ - Debug views work in development builds only. In a release build, `debug.view` does nothing, and the build holds none of the views' shaders or drawing code. A few small parts stay, because the engine shares them: the scene's traversal of objects whose meshes have levels, and the culling's level code. Together they add about 2.4 KB after Brotli to a single-threaded build, and less to a pipelined one. In a development build, a name that the engine does not know, and that no add-on feature registers, fails with [E1213](../errors/E1213.md).
131
131
 
132
132
  ### The view from the address
133
133
 
@@ -172,7 +172,7 @@ export default defineSketch(({ scene, debug, time }) => {
172
172
  });
173
173
  ```
174
174
 
175
- The cascades keep their boxes and their split distances from the other camera's view, so they fall where they fall in that camera's own frames. In a release build the call does nothing. three.js's cascaded shadow maps (`CSM`) take their camera as an option in the same way, so they can follow another camera than the one that renders.
175
+ The cascades keep their boxes and their split distances from the other camera's view, so they fall where they fall in that camera's own frames. In a release build the call does nothing.
176
176
 
177
177
  ## Stats overlay and frame figures
178
178
 
@@ -273,6 +273,7 @@ export default defineSketch(({ debug, page, time }) => {
273
273
  | `frames`, `seconds` | The presented frames of the window and its length. Both are 0 before the first window ends |
274
274
  | `presentedFps` | Frames per second that the engine presented |
275
275
  | `completedFps` | Frames per second that the GPU finished. Below `presentedFps`, the GPU limits the frame rate |
276
+ | `updateErrors` | Frames of the window whose scene update did not run to its end, because the sketch's code or a core call in a frame threw. Those frames still present. The overlay shows a warning line while it is above 0 |
276
277
  | `cpuMs` | CPU time per frame of the busiest thread, in milliseconds |
277
278
  | `threads` | Each engine thread's name, its CPU time per frame, and the time of each phase, as `engine.measure` names them |
278
279
  | `gpuMs` | GPU time per frame in milliseconds, from one frame in eleven, or null without timer queries. It is the latest pass end less the earliest pass begin, because a tile GPU overlaps the passes |
@@ -292,7 +293,7 @@ export default defineSketch(({ debug, page, time }) => {
292
293
  The engine counts every draw of every pass on the thread that draws. That covers shadow maps, the depth prepass, the passes that shade and the passes of post effects. three.js's `renderer.info` counts triangles the same way, so the two figures compare.
293
294
 
294
295
  - A draw of triangles adds its vertices or indices over 3, times its instances. A draw of lines adds no triangles.
295
- - A draw adds its instances to `objects`. So an object counts once in each pass that draws it. A box that casts shadows into 4 cascades counts 5 times, and 6 with the depth prepass. Each part of a mesh with several materials counts once, as each part is a mesh of its own in three.js.
296
+ - A draw adds its instances to `objects`. So an object counts once in each pass that draws it. A box that casts shadows into 4 cascades counts 5 times, and 6 with the depth prepass. Each part of a mesh with several materials counts once, because each part is an object of its own.
296
297
  - On WebGPU, the GPU culls most objects itself, and only the GPU knows how many it drew. The engine copies those counts back from the GPU on one frame in eleven. It does so while the overlay's card is open, and after the sketch's first call of `debug.frameStats()`. A development build copies them back in any case, so that it can warn about copies that the culling shader drops. The counts arrive a few frames late, and each frame adds the newest of them. Until then, the WebGPU figures count only the draws that the CPU issues.
297
298
  - So in a production build on WebGPU, `engine.measure` counts only the draws that the CPU issues, unless the card is open or the sketch has called `debug.frameStats()`. A scene that the GPU culls can then show almost no triangles. Call `debug.frameStats()` once in the sketch to count them all, at the cost that [Cost](#cost) gives.
298
299
 
@@ -440,6 +441,10 @@ A pass whose role has a place among the passes of that role, such as a shadow ca
440
441
 
441
442
  These figures time the GPU's work only. Work that the browser does outside the passes shows in `gpuLatencyMs` and in the frame rates.
442
443
 
444
+ ## The in-page inspector
445
+
446
+ `?inspect` opens a panel over the canvas that lists the scene's objects, outlines the one you select and edits its fields. On the page, `engine.inspect(call, args)` runs the same calls from your own code, and `window.__null3dInspector` holds them for the MCP server. These exist in a development build only. A held page or a replay publishes no bridge and opens no panel. [The in-page inspector](../guides/debugging.md#the-in-page-inspector) lists the calls.
447
+
443
448
  ## API reference
444
449
 
445
450
  [The API reference](reference/debug.md) lists every export of this page with its type and description. The engine's doc comments make it.