@null3d/engine 0.1.0 → 0.1.2

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 (388) hide show
  1. package/dist/wasm/single/null3d.js +10 -8
  2. package/dist/wasm/single/null3d_bg.wasm +0 -0
  3. package/dist/wasm/threaded/null3d.js +10 -8
  4. package/dist/wasm/threaded/null3d_bg.wasm +0 -0
  5. package/docs/api/animation.md +96 -24
  6. package/docs/api/assets.md +99 -29
  7. package/docs/api/cameras.md +38 -7
  8. package/docs/api/controls.md +47 -23
  9. package/docs/api/debug.md +105 -35
  10. package/docs/api/engine.md +108 -32
  11. package/docs/api/feature-draws.md +21 -15
  12. package/docs/api/feature-effects.md +8 -8
  13. package/docs/api/feature-instances.md +68 -17
  14. package/docs/api/features.md +68 -37
  15. package/docs/api/geometry.md +83 -14
  16. package/docs/api/grass.md +55 -13
  17. package/docs/api/impostors.md +92 -50
  18. package/docs/api/input.md +22 -9
  19. package/docs/api/lights.md +67 -16
  20. package/docs/api/lines.md +38 -13
  21. package/docs/api/materials.md +187 -59
  22. package/docs/api/math.md +6 -6
  23. package/docs/api/objects.md +40 -15
  24. package/docs/api/page.md +58 -7
  25. package/docs/api/particles.md +36 -11
  26. package/docs/api/points.md +18 -8
  27. package/docs/api/post.md +57 -23
  28. package/docs/api/quality.md +36 -16
  29. package/docs/api/raycast.md +57 -24
  30. package/docs/api/reference/animation.md +1 -2
  31. package/docs/api/reference/assets.md +7 -4
  32. package/docs/api/reference/cameras.md +3 -2
  33. package/docs/api/reference/controls.md +59 -2
  34. package/docs/api/reference/debug.md +58 -6
  35. package/docs/api/reference/engine.md +40 -10
  36. package/docs/api/reference/feature-draws.md +3 -4
  37. package/docs/api/reference/feature-effects.md +2 -3
  38. package/docs/api/reference/feature-instances.md +5 -4
  39. package/docs/api/reference/features.md +7 -4
  40. package/docs/api/reference/geometry.md +132 -21
  41. package/docs/api/reference/grass.md +62 -2
  42. package/docs/api/reference/impostors.md +120 -16
  43. package/docs/api/reference/input.md +1 -2
  44. package/docs/api/reference/lights.md +4 -3
  45. package/docs/api/reference/lines.md +3 -4
  46. package/docs/api/reference/materials.md +7 -6
  47. package/docs/api/reference/math.md +13 -2
  48. package/docs/api/reference/objects.md +2 -3
  49. package/docs/api/reference/particles.md +31 -6
  50. package/docs/api/reference/points.md +2 -3
  51. package/docs/api/reference/post.md +4 -3
  52. package/docs/api/reference/quality.md +1 -2
  53. package/docs/api/reference/raycast.md +1 -2
  54. package/docs/api/reference/render.md +4 -3
  55. package/docs/api/reference/scene.md +24 -5
  56. package/docs/api/reference/sketch.md +11 -11
  57. package/docs/api/reference/sound.md +33 -2
  58. package/docs/api/reference/splats.md +42 -3
  59. package/docs/api/reference/sprites.md +2 -3
  60. package/docs/api/reference/tasks.md +16 -3
  61. package/docs/api/reference/terrain.md +26 -5
  62. package/docs/api/reference/textures.md +1 -2
  63. package/docs/api/reference/three-compat.md +18 -7
  64. package/docs/api/reference/time.md +1 -2
  65. package/docs/api/reference/ui.md +10 -7
  66. package/docs/api/reference/vertex-draws.md +2 -3
  67. package/docs/api/reference/water.md +78 -5
  68. package/docs/api/reference/wind.md +15 -2
  69. package/docs/api/render.md +67 -25
  70. package/docs/api/scene.md +129 -36
  71. package/docs/api/sketch.md +45 -19
  72. package/docs/api/sound.md +48 -12
  73. package/docs/api/splats.md +42 -15
  74. package/docs/api/sprites.md +8 -6
  75. package/docs/api/tasks.md +31 -22
  76. package/docs/api/terrain.md +44 -12
  77. package/docs/api/textures.md +61 -12
  78. package/docs/api/three-compat.md +15 -7
  79. package/docs/api/time.md +3 -5
  80. package/docs/api/ui.md +18 -10
  81. package/docs/api/vertex-draws.md +13 -12
  82. package/docs/api/water.md +111 -67
  83. package/docs/api/wind.md +61 -15
  84. package/docs/cli/null3d.md +90 -15
  85. package/docs/concepts/architecture.md +69 -23
  86. package/docs/concepts/assets.md +57 -16
  87. package/docs/concepts/backends.md +68 -24
  88. package/docs/concepts/color-management.md +42 -13
  89. package/docs/concepts/culling.md +68 -47
  90. package/docs/concepts/handles.md +14 -5
  91. package/docs/concepts/instances.md +72 -23
  92. package/docs/concepts/large-worlds.md +17 -7
  93. package/docs/concepts/lighting.md +117 -44
  94. package/docs/concepts/lod.md +128 -33
  95. package/docs/concepts/materials.md +43 -12
  96. package/docs/concepts/particle-buffer.md +48 -35
  97. package/docs/concepts/post-processing.md +77 -14
  98. package/docs/concepts/quality-preset-tables.md +3 -2
  99. package/docs/concepts/quality-presets.md +122 -39
  100. package/docs/concepts/render-graph.md +56 -21
  101. package/docs/concepts/render-layers.md +9 -5
  102. package/docs/concepts/shadows.md +96 -25
  103. package/docs/concepts/static-dynamic.md +22 -7
  104. package/docs/cookbook/index.md +163 -119
  105. package/docs/data/threejs-mapping.json +301 -340
  106. package/docs/errors/E1101.md +23 -5
  107. package/docs/errors/E1102.md +11 -3
  108. package/docs/errors/E1103.md +11 -3
  109. package/docs/errors/E1104.md +11 -3
  110. package/docs/errors/E1105.md +13 -5
  111. package/docs/errors/E1106.md +11 -3
  112. package/docs/errors/E1107.md +15 -5
  113. package/docs/errors/E1108.md +11 -3
  114. package/docs/errors/E1109.md +32 -6
  115. package/docs/errors/E1110.md +23 -5
  116. package/docs/errors/E1111.md +11 -3
  117. package/docs/errors/E1112.md +11 -3
  118. package/docs/errors/E1203.md +12 -4
  119. package/docs/errors/E1204.md +11 -3
  120. package/docs/errors/E1205.md +11 -3
  121. package/docs/errors/E1206.md +28 -6
  122. package/docs/errors/E1207.md +11 -3
  123. package/docs/errors/E1208.md +12 -3
  124. package/docs/errors/E1209.md +11 -3
  125. package/docs/errors/E1213.md +27 -5
  126. package/docs/errors/E1214.md +11 -3
  127. package/docs/errors/E1215.md +31 -6
  128. package/docs/errors/E1216.md +31 -6
  129. package/docs/errors/E1217.md +29 -6
  130. package/docs/errors/E1218.md +22 -6
  131. package/docs/errors/E1219.md +11 -3
  132. package/docs/errors/E1220.md +24 -5
  133. package/docs/errors/E1221.md +24 -5
  134. package/docs/errors/E1222.md +11 -3
  135. package/docs/errors/E1223.md +39 -6
  136. package/docs/errors/E1224.md +11 -3
  137. package/docs/errors/E1225.md +11 -3
  138. package/docs/errors/E1301.md +11 -3
  139. package/docs/errors/E1302.md +11 -3
  140. package/docs/errors/E1303.md +11 -3
  141. package/docs/errors/E1304.md +11 -3
  142. package/docs/errors/E1305.md +13 -3
  143. package/docs/errors/E1306.md +11 -3
  144. package/docs/errors/E1307.md +23 -6
  145. package/docs/errors/E1308.md +31 -0
  146. package/docs/errors/E1401.md +11 -3
  147. package/docs/errors/E1402.md +13 -5
  148. package/docs/errors/E1403.md +11 -3
  149. package/docs/errors/E1404.md +13 -3
  150. package/docs/errors/E1405.md +15 -4
  151. package/docs/errors/E1406.md +31 -5
  152. package/docs/errors/E1407.md +11 -3
  153. package/docs/errors/E1408.md +11 -3
  154. package/docs/errors/E1409.md +11 -3
  155. package/docs/errors/E1410.md +13 -5
  156. package/docs/errors/E1411.md +11 -3
  157. package/docs/errors/E1412.md +29 -6
  158. package/docs/errors/E1413.md +11 -3
  159. package/docs/errors/E1414.md +11 -3
  160. package/docs/errors/E1415.md +11 -3
  161. package/docs/errors/E1416.md +27 -6
  162. package/docs/errors/E1417.md +11 -3
  163. package/docs/errors/E1418.md +12 -3
  164. package/docs/errors/E1419.md +13 -5
  165. package/docs/errors/E1420.md +11 -3
  166. package/docs/errors/E1421.md +11 -3
  167. package/docs/errors/E1422.md +12 -3
  168. package/docs/errors/E1423.md +12 -3
  169. package/docs/errors/E1424.md +24 -5
  170. package/docs/errors/E1425.md +11 -3
  171. package/docs/errors/E1426.md +11 -3
  172. package/docs/errors/E1430.md +11 -3
  173. package/docs/errors/E1431.md +11 -3
  174. package/docs/errors/E1432.md +11 -3
  175. package/docs/errors/E1433.md +11 -3
  176. package/docs/errors/E1434.md +15 -6
  177. package/docs/errors/E1435.md +11 -3
  178. package/docs/errors/E1436.md +11 -3
  179. package/docs/errors/E1437.md +14 -6
  180. package/docs/errors/E1501.md +41 -6
  181. package/docs/errors/E1502.md +13 -3
  182. package/docs/errors/E1503.md +11 -3
  183. package/docs/errors/E1504.md +11 -3
  184. package/docs/errors/E1505.md +23 -5
  185. package/docs/errors/E1506.md +11 -3
  186. package/docs/errors/index.md +53 -29
  187. package/docs/getting-started/cdn.md +15 -9
  188. package/docs/getting-started/first-scene.md +20 -12
  189. package/docs/getting-started/hosting.md +113 -63
  190. package/docs/getting-started/install.md +39 -24
  191. package/docs/getting-started/network-use.md +31 -21
  192. package/docs/getting-started/project-structure.md +20 -8
  193. package/docs/getting-started/third-party-notices.md +6 -10
  194. package/docs/guides/accessibility.md +4 -4
  195. package/docs/guides/add-ons.md +83 -13
  196. package/docs/guides/agents.md +13 -14
  197. package/docs/guides/assets-pipeline.md +294 -72
  198. package/docs/guides/audio.md +17 -15
  199. package/docs/guides/baked-light-data.md +44 -23
  200. package/docs/guides/car-paint.md +37 -31
  201. package/docs/guides/content-pages.md +86 -29
  202. package/docs/guides/custom-passes.md +59 -17
  203. package/docs/guides/custom-shaders.md +54 -37
  204. package/docs/guides/debugging.md +31 -41
  205. package/docs/guides/deploying.md +10 -27
  206. package/docs/guides/deterministic-simulation.md +23 -16
  207. package/docs/guides/first-use-shaders.md +83 -45
  208. package/docs/guides/lens-kit.md +29 -26
  209. package/docs/guides/loading-screens.md +74 -30
  210. package/docs/guides/multiple-views.md +22 -11
  211. package/docs/guides/performance.md +188 -37
  212. package/docs/guides/phones.md +71 -19
  213. package/docs/guides/physics.md +6 -10
  214. package/docs/guides/screens.md +41 -29
  215. package/docs/guides/start-time.md +126 -85
  216. package/docs/guides/testing.md +137 -98
  217. package/docs/guides/ui-overlays.md +17 -12
  218. package/docs/guides/video-textures.md +14 -13
  219. package/docs/images/api/animation/character.webp +0 -0
  220. package/docs/images/api/animation/look-at-ring.webp +0 -0
  221. package/docs/images/api/animation/morph-tulips.webp +0 -0
  222. package/docs/images/api/animation/skeleton-lines.webp +0 -0
  223. package/docs/images/api/controls/gizmo-handles.webp +0 -0
  224. package/docs/images/api/controls/walk-and-fly.webp +0 -0
  225. package/docs/images/api/debug/debug-shapes.webp +0 -0
  226. package/docs/images/api/debug/levels-view.webp +0 -0
  227. package/docs/images/api/engine/transparent-canvas.webp +0 -0
  228. package/docs/images/api/feature-draws/feature-draw-clouds.webp +0 -0
  229. package/docs/images/api/feature-instances/compute-instances.webp +0 -0
  230. package/docs/images/api/feature-instances/instances-shadow.webp +0 -0
  231. package/docs/images/api/geometry/extra-shapes.webp +0 -0
  232. package/docs/images/api/geometry/generators.webp +0 -0
  233. package/docs/images/api/geometry/mesh-arrays.webp +0 -0
  234. package/docs/images/api/geometry/vertex-updates.webp +0 -0
  235. package/docs/images/api/grass/grass-grove.webp +0 -0
  236. package/docs/images/api/grass/grass-presets.webp +0 -0
  237. package/docs/images/api/grass/grass-shadow.webp +0 -0
  238. package/docs/images/api/impostors/impostor-handover.webp +0 -0
  239. package/docs/images/api/impostors/impostor-shadow.webp +0 -0
  240. package/docs/images/api/impostors/impostors-forest.webp +0 -0
  241. package/docs/images/api/lights/spot-cookies.webp +0 -0
  242. package/docs/images/api/lines/lines-lit.webp +0 -0
  243. package/docs/images/api/lines/lines-widths.webp +0 -0
  244. package/docs/images/api/materials/coat-and-sheen.webp +0 -0
  245. package/docs/images/api/materials/decals.webp +0 -0
  246. package/docs/images/api/materials/iridescence-and-anisotropy.webp +0 -0
  247. package/docs/images/api/materials/relief.webp +0 -0
  248. package/docs/images/api/materials/shader-inputs.webp +0 -0
  249. package/docs/images/api/materials/standard-grid.webp +0 -0
  250. package/docs/images/api/materials/subsurface.webp +0 -0
  251. package/docs/images/api/materials/translucency.webp +0 -0
  252. package/docs/images/api/objects/objects-turntable.webp +0 -0
  253. package/docs/images/api/particles/campfire.webp +0 -0
  254. package/docs/images/api/points/galaxy.webp +0 -0
  255. package/docs/images/api/reference/textures/texture-wrap.webp +0 -0
  256. package/docs/images/api/render/fullscreen-glow.webp +0 -0
  257. package/docs/images/api/render/reflection-mirror.webp +0 -0
  258. package/docs/images/api/render/security-camera.webp +0 -0
  259. package/docs/images/api/scene/aurora-sky-layer.webp +0 -0
  260. package/docs/images/api/sound/sound-demo.webp +0 -0
  261. package/docs/images/api/splats/splats-demo.webp +0 -0
  262. package/docs/images/api/sprites/sprites-lines.webp +0 -0
  263. package/docs/images/api/terrain/terrain-demo.webp +0 -0
  264. package/docs/images/api/textures/canvas-screen.webp +0 -0
  265. package/docs/images/api/textures/mipmaps.webp +0 -0
  266. package/docs/images/api/vertex-draws/rain.webp +0 -0
  267. package/docs/images/api/water/ocean.webp +0 -0
  268. package/docs/images/api/water/water-demo.webp +0 -0
  269. package/docs/images/api/water/water-rain.webp +0 -0
  270. package/docs/images/api/water/waterfall.webp +0 -0
  271. package/docs/images/concepts/assets/gltf-model.webp +0 -0
  272. package/docs/images/concepts/color-management/tone-mapped.webp +0 -0
  273. package/docs/images/concepts/culling/far-from-origin.webp +0 -0
  274. package/docs/images/concepts/instances/instances.webp +0 -0
  275. package/docs/images/concepts/instances/per-row-colors.webp +0 -0
  276. package/docs/images/concepts/large-worlds/large-world.webp +0 -0
  277. package/docs/images/concepts/lighting/area-lights.webp +0 -0
  278. package/docs/images/concepts/lighting/atmosphere.webp +0 -0
  279. package/docs/images/concepts/lighting/environment.webp +0 -0
  280. package/docs/images/concepts/lighting/light-shafts.webp +0 -0
  281. package/docs/images/concepts/lighting/reflection-probes.webp +0 -0
  282. package/docs/images/concepts/lod/forest.webp +0 -0
  283. package/docs/images/concepts/materials/weighted-transparency.webp +0 -0
  284. package/docs/images/concepts/post-processing/auto-exposure.webp +0 -0
  285. package/docs/images/concepts/post-processing/bounce-light.webp +0 -0
  286. package/docs/images/concepts/post-processing/depth-of-field.webp +0 -0
  287. package/docs/images/concepts/post-processing/post-effects-alley.webp +0 -0
  288. package/docs/images/concepts/post-processing/sharpen.webp +0 -0
  289. package/docs/images/concepts/post-processing/ssr-wet-street.webp +0 -0
  290. package/docs/images/concepts/render-layers/layers-cottages.webp +0 -0
  291. package/docs/images/concepts/shadows/hero-shadow.webp +0 -0
  292. package/docs/images/cookbook/index/picking.webp +0 -0
  293. package/docs/images/cookbook/index/time-of-day.webp +0 -0
  294. package/docs/images/errors/E1431/area-lights-no-tables.webp +0 -0
  295. package/docs/images/guides/custom-passes/reflection-water.webp +0 -0
  296. package/docs/images/guides/deterministic-simulation/replay-demo.webp +0 -0
  297. package/docs/images/guides/multiple-views/split-screen.webp +0 -0
  298. package/docs/images/guides/performance/overdraw-view.webp +0 -0
  299. package/docs/images/guides/testing/hold-mode-frame.webp +0 -0
  300. package/docs/images/index/showcase-island.webp +0 -0
  301. package/docs/images/porting/threejs-shaders/dissolve.webp +0 -0
  302. package/docs/images/shaders/builtins/builtins.webp +0 -0
  303. package/docs/images/shaders/surface-functions/custom-surface.webp +0 -0
  304. package/docs/images/shaders/surface-functions/vertex-offset.webp +0 -0
  305. package/docs/images/shots.json +2231 -0
  306. package/docs/index.md +14 -23
  307. package/docs/moves.json +1 -0
  308. package/docs/nav.json +300 -0
  309. package/docs/pages.md +230 -212
  310. package/docs/porting/react-three-fiber.md +2 -6
  311. package/docs/porting/threejs-loop-and-threads.md +1 -5
  312. package/docs/porting/threejs-mapping.md +1248 -234
  313. package/docs/porting/threejs-materials.md +10 -5
  314. package/docs/porting/threejs-overview.md +26 -6
  315. package/docs/porting/threejs-postprocessing.md +2 -6
  316. package/docs/porting/threejs-shaders.md +16 -14
  317. package/docs/porting/threejs-unsupported.md +6 -6
  318. package/docs/porting/verification.md +0 -4
  319. package/docs/shaders/builtins.md +15 -4
  320. package/docs/shaders/library.md +9 -6
  321. package/docs/shaders/surface-functions.md +51 -16
  322. package/docs/shaders/wgsl-rules.md +12 -5
  323. package/lib/debug/debug.d.ts +2 -2
  324. package/lib/debug/draw.d.ts +2 -2
  325. package/lib/debug/draw.js +8 -4
  326. package/lib/debug/overlay-look.d.ts +6 -0
  327. package/lib/debug/overlay-look.js +29 -7
  328. package/lib/debug/overlay.js +15 -11
  329. package/lib/debug/stats-options.d.ts +13 -0
  330. package/lib/debug/stats-panel.d.ts +21 -6
  331. package/lib/debug/stats-panel.js +80 -13
  332. package/lib/errors/codes.d.ts +17 -3
  333. package/lib/errors/codes.js +116 -105
  334. package/lib/errors/engine-error.js +1 -1
  335. package/lib/errors/fixes.d.ts +1 -0
  336. package/lib/errors/fixes.js +19 -18
  337. package/lib/generated/shader-permutations.d.ts +6 -0
  338. package/lib/generated/shader-permutations.js +6 -0
  339. package/lib/gpu/variants.d.ts +8 -2
  340. package/lib/gpu/variants.js +39 -7
  341. package/lib/gpu/webgl2/programs.js +1 -1
  342. package/lib/gpu/webgpu/pipelines.js +13 -13
  343. package/lib/index.d.ts +1 -1
  344. package/lib/page/capabilities.d.ts +1 -1
  345. package/lib/page/engine.d.ts +29 -7
  346. package/lib/page/engine.js +79 -7
  347. package/lib/page/label-loop.js +9 -4
  348. package/lib/page/stats-switch.js +2 -0
  349. package/lib/page/switches.d.ts +6 -4
  350. package/lib/page/switches.js +20 -4
  351. package/lib/scene/baked-light.d.ts +2 -2
  352. package/lib/scene/fake-call-values.js +2 -2
  353. package/lib/scene/frame-cameras.d.ts +16 -4
  354. package/lib/scene/frame-cameras.js +28 -10
  355. package/lib/scene/gltf.d.ts +1 -1
  356. package/lib/scene/level-rule.d.ts +6 -5
  357. package/lib/scene/level-rule.js +2 -2
  358. package/lib/scene/particles.d.ts +7 -3
  359. package/lib/scene/particles.js +3 -1
  360. package/lib/scene/post.d.ts +3 -2
  361. package/lib/scene/resources.d.ts +15 -9
  362. package/lib/scene/scene.d.ts +18 -2
  363. package/lib/scene/scene.js +47 -5
  364. package/lib/scene/time-of-day.d.ts +10 -5
  365. package/lib/scene/time-of-day.js +11 -7
  366. package/lib/shader-artifact.json +1 -1
  367. package/lib/shared/labels.d.ts +5 -2
  368. package/lib/shared/labels.js +5 -2
  369. package/lib/shared/permutation-words.d.ts +8 -0
  370. package/lib/shared/permutation-words.js +14 -0
  371. package/lib/shared/recording.d.ts +13 -5
  372. package/lib/shared/recording.js +31 -10
  373. package/lib/shared/sketch-data.d.ts +9 -0
  374. package/lib/shared/sketch-data.js +57 -0
  375. package/lib/shared/tier.d.ts +9 -0
  376. package/lib/shared/tier.js +4 -1
  377. package/lib/shared/version.d.ts +1 -1
  378. package/lib/shared/version.js +1 -1
  379. package/lib/sketch/define-sketch.d.ts +16 -7
  380. package/lib/sketch/define-sketch.js +2 -1
  381. package/lib/sketch/runner.d.ts +4 -0
  382. package/lib/sketch/runner.js +5 -0
  383. package/lib/sketch/ui.js +4 -2
  384. package/lib/stats.d.ts +1 -0
  385. package/lib/workers/protocol.d.ts +10 -0
  386. package/lib/workers/render-worker.js +4 -1
  387. package/lib/workers/sketch-worker.js +4 -1
  388. package/package.json +1 -1
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: api/assets
3
3
  title: Assets
4
- status: experimental
5
- since: "0.1"
6
4
  summary: "loadGltf, registerGltfExtension, loadTexture, loadImageBitmap, loadLut, lutFromData, loadEnvironment, builtinEnvironment, skyEnvironment, loadCubemap, loadLightProbeGrid, loadLightMap, lightProbeGridFile, loadJson, loadBinary, preload, onProgress, cache."
7
5
  ---
8
6
 
9
7
  # Assets
10
8
 
11
- > Roadmap step 0.1, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
12
-
13
9
  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
10
 
15
11
  ```ts
@@ -56,7 +52,14 @@ export default defineSketch(
56
52
  | `onProgress(handler)` | A function that removes the handler |
57
53
  | `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
54
 
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.
55
+ In [hold mode](../guides/testing.md#hold-mode), and while a recording replays, the calls run one at a time, so several loads take longer there.
56
+
57
+ <details>
58
+ <summary>Technical details: loads in hold mode and replays</summary>
59
+
60
+ Each load 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.
61
+
62
+ </details>
60
63
 
61
64
  ## glTF models
62
65
 
@@ -74,19 +77,30 @@ export default defineSketch(async ({ scene, assets }) => {
74
77
  });
75
78
  ```
76
79
 
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.
80
+ 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.
81
+
82
+ 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.
83
+
84
+ 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. The [Island showcase](https://github.com/null3d-engine/null3d/tree/main/examples/showcase/island) loads models with levels and far cards.
85
+
86
+ A worker parses the file outside the sketch's frames, and decodes the images that the file holds there. The files that a `.gltf` file names download through `assets`, so `preload` and `onProgress` cover them too.
87
+
88
+ <details>
89
+ <summary>Technical details: what the first load downloads</summary>
78
90
 
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.
91
+ 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 engine copies the vertex data into its own memory, and keeps no other copy of it.
80
92
 
81
93
  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.
82
94
 
95
+ </details>
96
+
83
97
  The prefab turns each part of the file into the engine's own:
84
98
 
85
99
  | In the file | In the engine |
86
100
  | --- | --- |
87
101
  | 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 |
88
102
  | 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 |
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 |
103
+ | A material | `materials.standard`, or `materials.unlit` with `KHR_materials_unlit`. [Material extensions](#material-extensions) lists what each extension sets. |
90
104
  | A texture | A texture with the file's sampler and texture coordinates. `KHR_texture_basisu` textures load through the KTX2 transcoder |
91
105
  | `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 |
92
106
  | `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 |
@@ -113,6 +127,37 @@ A mesh with vertex colors turns them on, and a mesh without normals shades flat.
113
127
  | `prefab.url` | The address the model came from |
114
128
  | `prefab.destroy()` | Frees the model: [Freeing a model](#freeing-a-model) |
115
129
 
130
+ ### Material extensions
131
+
132
+ | Extension | What it sets | See |
133
+ | --- | --- | --- |
134
+ | `KHR_materials_unlit` | `materials.unlit` instead of `materials.standard` | |
135
+ | `KHR_materials_emissive_strength` | `emissiveIntensity` | |
136
+ | `KHR_materials_specular` | `specularIntensity`, `specularColor` and their maps | [Specular reflection](materials.md#specular-reflection-and-index-of-refraction) |
137
+ | `KHR_materials_ior` | `ior`. An `ior` of 0 becomes 1000, a surface that reflects nearly all light | [Specular reflection](materials.md#specular-reflection-and-index-of-refraction) |
138
+ | `KHR_materials_transmission` | `transmission`, and its map | [Transmission](materials.md#transmission) |
139
+ | `KHR_materials_volume` | `thickness`, `attenuationColor` and `attenuationDistance` | [Transmission](materials.md#transmission) |
140
+ | `KHR_materials_dispersion` | `dispersion`, which separates the light that passes through into colors | [Transmission](materials.md#transmission) |
141
+ | `KHR_materials_diffuse_transmission` | `diffuseTransmission`, `diffuseTransmissionColor` and their maps | [Light through thin surfaces](materials.md#light-through-thin-surfaces) |
142
+ | `KHR_materials_clearcoat` | `clearcoat`, `clearcoatRoughness`, `clearcoatNormalScale` and their maps | [Clear coat and sheen](materials.md#clear-coat-and-sheen) |
143
+ | `KHR_materials_sheen` | `sheenColor`, `sheenRoughness` and their maps | [Clear coat and sheen](materials.md#clear-coat-and-sheen) |
144
+ | `KHR_materials_iridescence` | `iridescence`, `iridescenceIor`, `iridescenceThicknessRange` and their maps | [Iridescence and anisotropy](materials.md#iridescence-and-anisotropy) |
145
+ | `KHR_materials_anisotropy` | `anisotropy`, `anisotropyRotation` and its map | [Iridescence and anisotropy](materials.md#iridescence-and-anisotropy) |
146
+ | `KHR_materials_volume_scatter` (a draft) | `subsurfaceRadius` and `subsurfaceColor`, each channel spreading `attenuationDistance` times its `multiscatterColor` | [Light under the surface](materials.md#light-under-the-surface) |
147
+
148
+ A material with many extensions can drop some textures, and the loader notes each one it drops.
149
+
150
+ <details>
151
+ <summary>Technical details: which textures a material with many extensions keeps</summary>
152
+
153
+ 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 samples a specular slot's texture array.
154
+
155
+ A thickness texture stays out beside a specular color texture, whose sRGB texels never share its array. A layered material goes without its specular textures and its diffuse transmission, unless a layer map fills the array that it samples, and the loader notes the one that stays out. A coat roughness or sheen roughness texture that is not the texture of its pair is not drawn.
156
+
157
+ 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. Transmission wins over diffuse transmission. A volume without an attenuation distance spreads no light, and `scatterAnisotropy` is not drawn.
158
+
159
+ </details>
160
+
116
161
  ### Freeing a model
117
162
 
118
163
  `prefab.destroy()` frees a model that the scene no longer needs. It frees every mesh, material and texture that the load made, and the model's skeleton and clips. First destroy the copies that `scene.instantiate` made and the batches of `scene.createInstances`. They can go in the same frame, just before the model:
@@ -135,23 +180,25 @@ An add-on can read a glTF extension that the engine leaves out. It registers a h
135
180
  // A handler draws a marker at each primitive that carries the example extension.
136
181
  const remove = assets.registerGltfExtension({
137
182
  extensions: ['EXT_example_marker'],
138
- load(data, makers) {
139
- const mesh = makers.geometry.box();
140
- const material = makers.materials.unlit({ color: [1, 0.6, 0] });
183
+ load(data, context) {
184
+ const mesh = context.geometry.box();
185
+ const material = context.materials.unlit({ color: [1, 0.6, 0] });
141
186
  return { mesh, material };
142
187
  },
143
188
  });
144
189
  remove(); // the handler reads no more files
145
190
  ```
146
191
 
147
- `registerGltfExtension(handler)` returns a function that unregisters the handler. Two handlers cannot read one extension. The call throws [E1213](../errors/E1213.md) for that case, for a handler that names no extension, and for a handler without a `load` function.
192
+ `assets.registerGltfExtension(handler)` returns a function that unregisters the handler. It throws [E1213](../errors/E1213.md) for an extension that another handler reads, for a handler that names no extension, and for a handler without a `load` function.
148
193
 
149
194
  An add-on registers its own handler as its module loads, with the engine's `registerGltfExtension`,
150
195
  which serves every sketch of the thread. A page that imports the add-on then loads the add-on's
151
196
  files with no other call, and a file that requires the extension names the package to import instead
152
- of failing with E1417. `@null3d/splats` reads `KHR_gaussian_splatting` this way. A handler that a
197
+ of failing with E1417. `@null3d/splats` reads `KHR_gaussian_splatting` this way.
198
+
199
+ A handler that a
153
200
  sketch registers with `assets.registerGltfExtension` for the same extension reads its files in the
154
- add-on's place.
201
+ add-on's place. A later add-on handler for the same extension replaces the earlier one, so a development reload of the add-on works. When two add-ons read one extension, the one that loaded last reads it.
155
202
 
156
203
  The loader calls `load` once for each primitive that carries one of the handler's extensions. It gets `data`, which describes the primitive, and `context`, which holds the engine's makers:
157
204
 
@@ -168,9 +215,13 @@ The loader calls `load` once for each primitive that carries one of the handler'
168
215
  | `context.geometry` | The engine's `geometry`, for the meshes that the handler returns |
169
216
  | `context.materials` | The engine's `materials`, for the materials that the handler returns |
170
217
 
171
- `load` returns one part, a list of parts, or nothing, which draws nothing. A part is `{ mesh, material }`. The loader places the parts at the primitive's node, so copies of the model draw them, and `prefab.destroy()` frees them. A primitive that a handler claims draws only what the handler returns. The other primitives of the same mesh draw as usual. `load` may return a promise.
218
+ `load` returns one part, a list of parts, or nothing, which draws nothing. A part is `{ mesh, material }`. The loader places the parts at the primitive's node, so copies of the model draw them, and `prefab.destroy()` frees them. A primitive that a handler claims draws only what the handler returns.
219
+
220
+ The other primitives of the same mesh draw as usual. `load` may return a promise. In hold mode, `load` must not wait for another `assets` call, since that call waits for the load that runs the handler.
172
221
 
173
- A file that requires an extension which an add-on reads loads with that add-on's handler. Without it, the load fails with [E1417](../errors/E1417.md), and the message names the package to import. For example, the message for `KHR_gaussian_splatting` names `@null3d/splats`. A file that only uses such an extension, and does not require it, draws without that extension's data when no handler reads it. Development builds warn about each name in a file's `extensionsUsed` that neither the loader nor a registered handler reads. When an add-on reads the extension, the warning names the package to import. Production builds print nothing.
222
+ A file that requires an extension which an add-on reads loads with that add-on's handler. Without it, the load fails with [E1417](../errors/E1417.md), and the message names the package to import. For example, the message for `KHR_gaussian_splatting` names `@null3d/splats`.
223
+
224
+ A file that only uses such an extension, and does not require it, draws without that extension's data when no handler reads it. Development builds warn about each name in a file's `extensionsUsed` that neither the loader nor a registered handler reads. When an add-on reads the extension, the warning names the package to import. Production builds print nothing.
174
225
 
175
226
  ## Color grading tables
176
227
 
@@ -181,7 +232,7 @@ A file that requires an extension which an add-on reads loads with that add-on's
181
232
  - Values outside 0 to 1 are clamped, as the canvas would clamp them.
182
233
  - 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.
183
234
 
184
- `lut.destroy()` frees the table's GPU memory. Give `post.set` a table that lives.
235
+ `lut.destroy()` frees the table's GPU memory. Give `post.set` a table that lives. The comparisons build their color grade with `lutFromData`: the [night town](https://github.com/null3d-engine/null3d/tree/main/examples/compare/night-town), [battle](https://github.com/null3d-engine/null3d/tree/main/examples/compare/battle) and [factory](https://github.com/null3d-engine/null3d/tree/main/examples/compare/factory) comparisons.
185
236
 
186
237
  ### Tables from numbers
187
238
 
@@ -226,6 +277,8 @@ export default defineSketch(async ({ assets, post }) => {
226
277
  - 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.
227
278
  - 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.
228
279
 
280
+ Prefer the tool's KTX2 file for maps that ship with a game; use an HDR file for maps that change, such as user uploads. The [environment light demo](https://github.com/null3d-engine/null3d/tree/main/examples/environment) shows an HDR file's light, an EXR file's light and the built-in room.
281
+
229
282
  `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).
230
283
 
231
284
  ```ts
@@ -243,18 +296,28 @@ export default defineSketch(async ({ scene, assets }) => {
243
296
  ```
244
297
 
245
298
  - An `Environment` has its cube map's `size`, the width of the largest faces, its `levels`, its `format` and its GPU `bytes`.
246
- - The first environment file loads the file reader, under 1 KB after Brotli. The first built-in room or sky's environment loads the code and the shaders that make it on the GPU, about 12 KB after Brotli. [Lighting and environment](../concepts/lighting.md#cost) gives the GPU's time.
247
- - A KTX2 file's cube map uploads in the frames after the load, and the scene draws without it until it is on the GPU.
248
- - A worker reads an HDR file, outside the sketch's frames. The first one loads the reader and its worker, about 6.5 KB after Brotli. It also loads the code and shaders that make the room, which filter the file. They start at the call for an address that ends in `.hdr` or `.exr`, so they load during the download. The call resolves once the file is read and those shaders are ready. The GPU then filters the whole map in the next frame, before that frame draws. So no frame draws the scene without its light. Load HDR files while the scene loads.
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.
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.
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.
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.
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.
299
+ - The first room, sky or HDR call starts the download of the code and shaders that make the room, about 12 KB after Brotli, and each call resolves once they are ready.
300
+ - A KTX2 file's cube map uploads in the frames after the load, and the scene draws without it until it is on the GPU. Load it while the scene loads.
301
+ - The first HDR file loads the reader and its worker, about 6.5 KB after Brotli. A worker reads the file outside the sketch's frames, and the GPU then filters the whole map in the next frame, before that frame draws. So no frame draws the scene without its light. Load HDR files while the scene loads.
302
+ - `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. Ask for the room while the scene loads. During play, the call makes one long frame.
303
+ - `skyEnvironment` loads the same code and shaders as the room, and resolves once they are ready. The next frame makes the whole map. Until the scene's first sky background, the map shows the sky's defaults.
304
+ - The sky's map follows the sky: after a change that shows, it refreshes in stages, one a frame. A small change waits, and one last refresh captures the sky once it stops changing. The scene draws with the old map until the last stage, and the map leaves out the sun's disc, whose light the scene's directional light gives.
255
305
  - `environment.destroy()` frees the cube map's GPU memory. The scene then draws without it.
256
306
  - Other KTX2 files, such as `loadTexture`'s, fail with E1412. So do supercompressed files.
257
- - The tool's file needs no reading or filtering at load, so prefer it for maps that ship with a game. HDR files suit maps that change, such as files that users upload. The tool's file is not always the smaller download. After Brotli, Venice Sunset's 2K Radiance file takes 3.8 MB and its map 1.4 MB. A 1K OpenEXR file and its map take about 1.3 MB each.
307
+ - An HDR map's faces are 256 texels by default, and an image wider than 2,048 texels is averaged down first. The HDR reader reads single-part scanline files with every compression but DWAA and DWAB. Tiled, deep and multi-part files fail with E1412, as do Radiance files stored from the bottom row up.
308
+
309
+ <details>
310
+ <summary>Technical details: sizes, limits and GPU times</summary>
311
+
312
+ The first environment file loads the file reader, under 1 KB after Brotli. 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.
313
+
314
+ 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.
315
+
316
+ The room's map takes about 20 ms of GPU time on a MacBook Pro, and 50 to 110 ms on recent phones. The sky's map takes about 10 ms on a MacBook Pro. The sky refreshes in 20 stages, each about 1 ms of GPU time on a MacBook Pro; 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%.
317
+
318
+ After Brotli, Venice Sunset's 2K Radiance file takes 3.8 MB and its map 1.4 MB. A 1K OpenEXR file and its map take about 1.3 MB each. [Lighting and environment](../concepts/lighting.md#sky-and-backgrounds) gives the detail.
319
+
320
+ </details>
258
321
 
259
322
  [Lighting and environment](../concepts/lighting.md#environment-maps) says how an environment lights the scene.
260
323
 
@@ -307,7 +370,9 @@ The files that the setup loads start sooner when `defineSketch`'s `downloads` op
307
370
 
308
371
  `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.
309
372
 
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.
373
+ `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))`.
374
+
375
+ 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.
311
376
 
312
377
  ```ts
313
378
  assets.onProgress((loaded, total, url) => page.post('loading', { loaded, total, url }));
@@ -316,7 +381,7 @@ await assets.preload(['/tex/a.png', '/tex/b.png', '/tex/c.png']);
316
381
  // (120_000, 120_000) as the last of the three arrives.
317
382
  ```
318
383
 
319
- [Loading screens and warm-up](../guides/loading-screens.md) shows a whole loading screen.
384
+ [Loading screens and warm-up](../guides/loading-screens.md) shows a whole loading screen. The [glTF model demo](https://github.com/null3d-engine/null3d/tree/main/examples/gltf-model) drives its loading bar this way, through the demos' shared helper.
320
385
 
321
386
  ## Caching
322
387
 
@@ -352,10 +417,15 @@ async function tile(x: number, z: number): Promise<Float32Array> {
352
417
  - `put` returns at once, and the write starts after the call. Leave the bytes unchanged until its promise resolves. A write that fails, or that would hold too much memory, is dropped.
353
418
  - The cache works in the sketch worker and on the page's thread. In the sketch worker it reads fastest.
354
419
 
420
+ <details>
421
+ <summary>Technical details: how much the cache keeps</summary>
422
+
355
423
  Every namespace shares one budget: a tenth of the storage that the browser allows the page's origin. Chrome and Firefox allow about 10 GiB, so the cache keeps about 1 GiB there, and Safari on an iPhone keeps about 3.8 GiB. When the cache passes its budget, it deletes the entries used longest ago.
356
424
 
357
425
  The browser can still delete the origin's storage. Under storage pressure, it deletes the storage of whole origins, the least recently used first. Safari deletes it after seven days of browsing without a visit to the site, unless the game runs from the Home Screen. The engine never asks for persistent storage, because Firefox asks the player with a prompt. A game can ask for it with `navigator.storage.persist()`.
358
426
 
427
+ </details>
428
+
359
429
  The `?derived-cache=off` switch turns the cache off, so every load makes its data again, as on a first visit ([Testing](../guides/testing.md#switches-for-tests)).
360
430
 
361
431
  ## Errors
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: api/cameras
3
3
  title: Cameras
4
- status: experimental
5
- since: "0.1"
6
- summary: "Perspective and orthographic cameras; a perspective camera's focal length; screenToRay; worldToScreen; layers."
4
+ summary: "Perspective and orthographic cameras; a perspective camera's focal length; lens shift; screenToRay; worldToScreen; layers."
7
5
  ---
8
6
 
9
7
  # Cameras
10
8
 
11
- > Roadmap step 0.1, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
12
-
13
9
  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
10
 
15
11
  ```ts
@@ -73,6 +69,37 @@ scene.setActiveCamera(camera);
73
69
 
74
70
  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.
75
71
 
72
+ ## Lens shift
73
+
74
+ A shift lens moves the picture off the camera's axis and keeps its perspective. A web page uses one to put a scene beside its text without turning the camera. Turning the camera would change the perspective: upright lines would lean, and the subject would show another side.
75
+
76
+ `setLensShift(x, y)` moves the view right by `x` times its width and up by `y` times its height. The scene moves the other way in the picture. The `lensShift` property reads the shift back as `[x, y]`, and the default is `[0, 0]`.
77
+
78
+ ```ts
79
+ import { defineSketch } from '@null3d/engine';
80
+
81
+ export default defineSketch(({ scene }) => {
82
+ const camera = scene.createPerspectiveCamera({ position: [0, 2, 8], target: [0, 1, 0] });
83
+ // The page's text fills the left half, so the target sits three quarters of the way across.
84
+ camera.setLensShift(-0.25, 0);
85
+ scene.setActiveCamera(camera);
86
+ });
87
+ ```
88
+
89
+ The shifted picture is the matching crop of a wider picture from the same place. A shift of 0.5 puts the camera's axis on the edge of the picture. Both kinds of camera take a shift. An orthographic view moves across by the same fractions of its size.
90
+
91
+ Everything that uses the camera's view follows the shift. That covers culling, the shadow cascades, picking, `screenToRay`, `worldToScreen` and HTML labels. It also covers views from `scene.createView`, and the post effects that rebuild positions from depth. A sketch can change the shift in any frame, and the call allocates nothing.
92
+
93
+ To draw one tile of a larger picture, narrow the lens to the tile and shift the view onto it. The canvas must have the tile's shape. Take a tile that is `w` of the full picture's width and `h` of its height. Its middle lies `cx` right of and `cy` above the full picture's middle. All four are fractions of the full picture:
94
+
95
+ ```ts
96
+ const half = Math.tan((fullFov * Math.PI) / 360) * h;
97
+ camera.setFov((360 / Math.PI) * Math.atan(half)); // or camera.setOrthoHeight(fullHeight * h)
98
+ camera.setLensShift(cx / w, cy / h);
99
+ ```
100
+
101
+ The [three.js mapping](../porting/threejs-mapping.md) gives the same numbers for three.js's `setViewOffset`.
102
+
76
103
  ## Which kind of camera
77
104
 
78
105
  `camera.isOrthographic` is true for an orthographic camera and false for a perspective one. Code that works with either kind, such as camera controls, reads `fov` or `height` after that check.
@@ -134,7 +161,9 @@ export default defineSketch(({ scene, input, page }) => {
134
161
 
135
162
  The engine draws a frame on one thread while the sketch computes the next one on another. A click therefore lands on a frame that is older than the sketch's current camera. During a fast pan, a ray from the current camera would miss what the user clicked.
136
163
 
137
- So the engine keeps the cameras of the last four views that it drew. Frames in a row with the same view count as one view. The frames that the engine draws while the setup runs count too, so a click during the setup picks what that frame showed. When `x` and `y` come from `input.pointer` or a finger in `input.touches`, the ray uses the camera of the frame on screen at that event. Any other point uses the camera of the frame that last ran, with its lens as it is now. This holds in every thread mode.
164
+ So the engine keeps the cameras of the last four views that it drew. Frames in a row with the same view count as one view. The frames that the engine draws while the setup runs count too, so a click during the setup picks what that frame showed. When `x` and `y` come from `input.pointer` or a finger in `input.touches`, the ray uses the camera of the frame on screen at that event.
165
+
166
+ Any other point uses the camera of the frame that last ran, with its lens as it is now. This holds in every thread mode.
138
167
 
139
168
  Objects keep their current positions. A ray finds a moving object where it is now. That can be up to a frame of its motion away from where the user saw it.
140
169
 
@@ -144,7 +173,7 @@ A view from `scene.createView` draws its camera into a rectangle of the canvas,
144
173
 
145
174
  ## Several cameras
146
175
 
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.
176
+ A scene can have several cameras, and `setActiveCamera` switches between them. The active camera fills the canvas. `scene.createView` draws more cameras into rectangles of it ([Multiple views](../guides/multiple-views.md)). Until you pick an active camera, and after the engine removes it, the canvas shows only the background.
148
177
 
149
178
  ## Related pages
150
179
 
@@ -155,6 +184,8 @@ A scene can have several cameras, and `setActiveCamera` switches between them. T
155
184
  - [Input](input.md): the pointer and touches whose positions `screenToRay` takes.
156
185
  - [Raycasting](raycast.md): finding the objects along a ray.
157
186
  - [Math helpers](math.md): `quat.lookAt`, and why cameras swap its eye and target.
187
+ - [Camera layer masks](https://github.com/null3d-engine/null3d/tree/main/examples/layers): a camera that draws another set of layers every 2 seconds.
188
+ - [A picture in picture](https://github.com/null3d-engine/null3d/tree/main/examples/multiple-views): one scene from two cameras, split across the canvas or in an inset.
158
189
 
159
190
  ## API reference
160
191
 
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: api/controls
3
3
  title: "Controls (@null3d/controls)"
4
- status: experimental
5
- since: "0.1"
6
4
  summary: "Orbit and map controls; fly and first-person controls, with pointer lock; drag, rotate and scale gizmos."
7
5
  ---
8
6
 
9
7
  # Controls (@null3d/controls)
10
8
 
11
- > Roadmap step 0.1, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
12
-
13
9
  ```mermaid
14
10
  flowchart LR
15
11
  events["Pointer, wheel and touch events<br/>on the canvas"] --> input["ctx.input,<br/>once per frame"]
@@ -18,7 +14,16 @@ flowchart LR
18
14
  update --> gizmo["A held handle moves,<br/>turns or scales an object"]
19
15
  ```
20
16
 
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.
17
+ 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. The [glTF model demo](https://github.com/null3d-engine/null3d/tree/main/examples/gltf-model) uses orbit controls through the demos' shared helper, as a model viewer does.
18
+
19
+ | Controls | Call | Moves like |
20
+ | --- | --- | --- |
21
+ | Orbit | `createOrbitControls` | A model viewer: the camera turns around a target point |
22
+ | Map | `createMapControls` | A map or a strategy game: the camera pans over the ground |
23
+ | [Fly](#fly-controls) | `createFlyControls` | A flight game: the camera moves in every direction |
24
+ | [First-person](#first-person-controls) | `createFirstPersonControls` | A first-person game: the camera walks over the ground and looks around |
25
+ | [Gizmos](#gizmos) | `createTransformControls` | An editor: handles move, turn or scale an object |
26
+ | [Drag](#drag-controls) | `createDragControls` | Drags objects over a plane that faces the camera |
22
27
 
23
28
  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
29
 
@@ -103,7 +108,7 @@ Each option is also a property of the controls, with the same name. A sketch can
103
108
 
104
109
  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.
105
110
 
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.
111
+ - `update` allocates nothing, so you can call it in every frame. The engine's allocation benchmark checks this in every frame and through every gesture.
107
112
  - 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.
108
113
  - The sketch can move the camera itself with `camera.setPosition`. The controls carry on from the new position at the next `update`.
109
114
  - Auto-rotation stops while the user asks for less motion. [Accessibility](../guides/accessibility.md) explains the preference.
@@ -171,6 +176,10 @@ return {
171
176
 
172
177
  `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.
173
178
 
179
+ ![A ruined temple at golden hour, seen from eye height between columns.](../images/api/controls/walk-and-fly.webp)
180
+
181
+ First-person and fly controls.
182
+
174
183
  ```ts
175
184
  import { createFirstPersonControls } from '@null3d/controls';
176
185
 
@@ -196,10 +205,27 @@ return {
196
205
  - 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.
197
206
  - When two fingers are down, the first finger's movement turns the view.
198
207
  - 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.
208
+ - 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` maps the view's angle into that range, and `lookVertical: false` keeps the view level.
200
209
  - `heightSpeed`, with `heightCoef`, `heightMin` and `heightMax`, walks faster forward the higher the camera is. `autoForward` walks forward along the view all the time.
201
210
  - `lookAt(x, y, z)` turns the camera toward a point.
202
211
 
212
+ ### Moving from code
213
+
214
+ 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:
215
+
216
+ ```ts
217
+ const controls = createFirstPersonControls(ctx, camera, { movementSpeed: 0 });
218
+ return {
219
+ onUpdate(dt) {
220
+ controls.update(dt);
221
+ const walk = (ctx.input.isDown('KeyW') ? 1 : 0) - (ctx.input.isDown('KeyS') ? 1 : 0);
222
+ const strafe = (ctx.input.isDown('KeyD') ? 1 : 0) - (ctx.input.isDown('KeyA') ? 1 : 0);
223
+ controls.moveForward(walk * 5 * dt); // 5 meters a second
224
+ controls.moveRight(strafe * 5 * dt);
225
+ },
226
+ };
227
+ ```
228
+
203
229
  ## Pointer lock
204
230
 
205
231
  The page asks the browser to lock the pointer to the canvas with `engine.requestPointerLock()` ([Page API](engine.md#the-running-engine)). The browser then hides the pointer and sends the mouse's movement, with no edge to stop it. Browsers lock the pointer only right after the user acts, so call it in a click or key handler on the page:
@@ -221,22 +247,9 @@ While the pointer is locked, `ctx.input.pointer.locked` is true, and `pointer.dx
221
247
  - The view stays between `minPolarAngle` and `maxPolarAngle` from +Y. The limits apply after a frame's moves add up.
222
248
  - A drag looks around no more, and the buttons do not walk, so the sketch can use them, for example to fire.
223
249
 
224
- The user ends the lock with Esc, and the page with `document.exitPointerLock()`. Destroying the engine also ends it.
225
-
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:
250
+ These options follow three.js's PointerLockControls, and the drag options follow its FirstPersonControls. The 85-degree cap applies only to a drag, and `minPolarAngle` and `maxPolarAngle` apply only while the pointer is locked. `constrainVertical` maps the view's angle into `verticalMin` to `verticalMax` in both cases.
227
251
 
228
- ```ts
229
- const controls = createFirstPersonControls(ctx, camera, { movementSpeed: 0 });
230
- return {
231
- onUpdate(dt) {
232
- controls.update(dt);
233
- const walk = (ctx.input.isDown('KeyW') ? 1 : 0) - (ctx.input.isDown('KeyS') ? 1 : 0);
234
- const strafe = (ctx.input.isDown('KeyD') ? 1 : 0) - (ctx.input.isDown('KeyA') ? 1 : 0);
235
- controls.moveForward(walk * 5 * dt); // 5 metres a second
236
- controls.moveRight(strafe * 5 * dt);
237
- },
238
- };
239
- ```
252
+ The user ends the lock with Esc, and the page with `document.exitPointerLock()`. Destroying the engine also ends it.
240
253
 
241
254
  The [first-person and fly controls demo](https://github.com/null3d-engine/null3d/tree/main/examples/walk-and-fly) walks through a ruined temple. A click asks for the pointer lock, and Space switches between first-person and fly controls.
242
255
 
@@ -244,6 +257,10 @@ The [first-person and fly controls demo](https://github.com/null3d-engine/null3d
244
257
 
245
258
  `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.
246
259
 
260
+ ![Move handles, an arrow per axis and plane tiles, on a selected sphere on a plinth.](../images/api/controls/gizmo-handles.webp)
261
+
262
+ The gizmo in translate mode.
263
+
247
264
  ```ts
248
265
  import { createTransformControls } from '@null3d/controls';
249
266
 
@@ -278,7 +295,14 @@ A press on a handle begins a drag, and the pointer then moves, turns or scales t
278
295
  - The handles draw over the scene, so a handle that lies behind a wall still shows and still takes the pointer.
279
296
  - The handle meshes take `levels: false`. The handles keep one share of the screen, so a level would never switch.
280
297
 
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.
298
+ 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, and a click handler on the object behind it does not hear it. By default the handles use layers 30 and 29. When a camera draws every layer, set `layers` and `pickLayers` to two other layers, as masks such as `1 << 28`.
299
+
300
+ <details>
301
+ <summary>Technical details: the gizmo's layers</summary>
302
+
303
+ `layers` is the layer that the handles draw on, which the controls add to the camera's layers and take away at `destroy`. `pickLayers` is the layer of the handles that answer a raycast, which no camera draws.
304
+
305
+ </details>
282
306
 
283
307
  `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.
284
308