@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
package/docs/api/sound.md CHANGED
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: api/sound
3
3
  title: "Sound (@null3d/sound)"
4
- status: experimental
5
- since: "0.3"
6
4
  summary: "createSound and connectSound: loops, groups and one-shots placed in the scene, played with the browser's audio and driven from the sketch on the audio thread."
7
5
  ---
8
6
 
9
7
  # Sound (@null3d/sound)
10
8
 
11
- > Roadmap step 0.3, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
12
-
13
9
  ```mermaid
14
10
  flowchart LR
15
11
  sketch["Sketch worker<br/>createSound: loops,<br/>groups and shots"] -- "writes places, levels,<br/>rates and tones" --> table[("One shared<br/>table")]
@@ -21,7 +17,14 @@ flowchart LR
21
17
 
22
18
  `@null3d/sound` plays sound that follows the scene. A sketch makes its sounds and moves them as it moves its objects. The page plays them with the browser's audio, through the add-on's page half. A sound can come from a place, such as a waterfall or a car. It can also play in both ears alike, such as wind or rain.
23
19
 
24
- The browser offers its audio only on the page's main thread, and the sketch runs in a worker. The add-on bridges the two. The sketch writes each sound's place, level, playback rate and tone into a table that the page shares. A mixer on the browser's audio thread, compiled from Rust to WebAssembly, reads that table and drives every voice. Where the page shares its memory with its workers, no frame sends a message, and the page's thread does no work while the sounds play. Only a shot sends one, once for each play.
20
+ The browser offers its audio only on the page's main thread, and the sketch runs in a worker. The add-on bridges the two.
21
+
22
+ <details>
23
+ <summary>Technical details: how the sketch reaches the audio thread</summary>
24
+
25
+ The sketch writes each sound's place, level, playback rate and tone into a table that the page shares. A mixer on the browser's audio thread, compiled from Rust to WebAssembly, reads that table and drives every voice. Where the page shares its memory with its workers, no frame sends a message, and the page's thread does no work while sounds play. A shot sends one message for each play.
26
+
27
+ </details>
25
28
 
26
29
  ```sh
27
30
  bun add @null3d/sound
@@ -74,7 +77,7 @@ Call `sound.update()` once a frame, in `onLateUpdate`. It reads the listener's p
74
77
 
75
78
  | Call | What it does |
76
79
  | --- | --- |
77
- | `createSound(ctx, options)` | Makes the sketch's sounds. `listener` is required: the camera, or another object that hears. `capacity` is the emitters that the sounds hold together, 256 by default and 4,096 at most. `volume` sets every sound's volume, from 0 to 1. A sketch has one sound system at a time: a second call while the first lives throws error 2504. |
80
+ | `createSound(ctx, options)` | Makes the sketch's sounds. `listener` is required: the camera, or another object that hears. `capacity` is the emitters that the sounds hold together, 256 by default and 4,096 at most. `volume` sets every sound's volume, from 0 to 1. A sketch has one sound system at a time: a second call while the first lives throws E2504. |
78
81
  | `loop(options)` | A sound that plays its file over and over. `positional: false` makes a bed that plays in both ears. `level`, `rate`, `tone` and `position` give its first values. |
79
82
  | `group(options)` | `count` sounds that play one file, of which the loudest `voices` sound at once. Use it for the cars in a street or the bees of a swarm. |
80
83
  | `shot(options)` | A sound that plays its file once for each call of its `play`, such as thunder. `voices` plays that many at once, 2 by default. A play past that count fades the oldest out quickly. Each play starts about 20 ms after the call, at its own level, place and pitch, or about 80 ms after it when it replaces a play that still sounds. |
@@ -83,7 +86,9 @@ Call `sound.update()` once a frame, in `onLateUpdate`. It reads the listener's p
83
86
  | `destroy()` | Stops every sound and closes the link to the page. The page then waits for the sketch's next sound system. |
84
87
  | `connected` | True once the page's half has opened its link. |
85
88
 
86
- A loop has `setLevel`, `setRate`, `setTone`, `setPosition` and `destroy`. A group has the same setters with a member's index first, such as `setPosition(index, x, y, z)`. A group also has the arrays `positions`, `levels`, `rates` and `tones`, which are views of the table. A sketch that keeps its places in one array copies them in one call, as in `cars.positions.set(places)`. A shot has `play(x, y, z, level, rate)` and `destroy`. A destroyed sound's setters do nothing. Its arrays may then serve another sound, so a sketch drops them with the group.
89
+ A loop has `setLevel`, `setRate`, `setTone`, `setPosition` and `destroy`. A group has the same setters with a member's index first, such as `setPosition(index, x, y, z)`. A group also has the arrays `positions`, `levels`, `rates` and `tones`, which are views of the table. A sketch that keeps its places in one array copies them in one call, as in `cars.positions.set(places)`.
90
+
91
+ A shot has `play(x, y, z, level, rate)` and `destroy`. A destroyed sound's setters do nothing. Its arrays may then serve another sound, so a sketch drops them with the group.
87
92
 
88
93
  The setters write numbers into the table and make no object, so a sketch may call them in every frame. `update` makes no object either. A sound made before the page connects plays once it does, and a sketch may make sounds at any time.
89
94
 
@@ -112,13 +117,15 @@ A sound with neither distance keeps the level that the sketch gives it wherever
112
117
 
113
118
  ## Groups and their voices
114
119
 
115
- A browser mixes a few dozen voices well, and a scene may have hundreds of sources of one kind. A group gives its members a few voices. The mixer ranks the members by their loudness where the listener stands, from their level and distance. It gives each voice to one of the loudest. A voice keeps its member while that member stays close to the cut. So a voice does not flap between two members at the same distance. A voice that moves to another member fades out first, so its sound never jumps across the scene. It takes the new member's pitch before it fades in.
120
+ A browser mixes a few dozen voices well, and a scene may have hundreds of sources of one kind. A group gives its members a few voices. The mixer ranks the members by their loudness where the listener stands, from their level and distance. It gives each voice to one of the loudest.
121
+
122
+ A voice keeps its member while that member stays close to the cut. So a voice does not flap between two members at the same distance. A voice that moves to another member fades out first, so its sound never jumps across the scene. It takes the new member's pitch before it fades in.
116
123
 
117
124
  A group's voices add as power: three at full level sound about 5 dB louder than one. Give a group's points lower levels than a loop's. Write a mass of like sources as one loop, whose level follows the density around the camera, not one source for each object.
118
125
 
119
126
  A group's voices play one file, each from a point of its own, so the copies never sound in step. Rates that differ for each member add no richness that the offsets do not already give. They also make the group's pitch change as the camera moves and the nearest members change. Keep the members at one rate, and use `setRate` for a source whose own speed differs, such as a car's engine.
120
127
 
121
- The page plays 32 voices at most. A loop takes one voice, a group takes its `voices`, and a shot takes its `voices`, wherever free voices sit. A sketch that asks for more than 32 at once gets error 2501, which names the count. While other voices are free, a destroyed sound's voices rest for a third of a second before another sound takes them. So their fade ends first.
128
+ The page plays 32 voices at most. A loop takes one voice, a group takes its `voices`, and a shot takes its `voices`, wherever free voices sit. A sketch that asks for more than 32 at once gets E2501, which names the count. While other voices are free, a destroyed sound's voices rest for a third of a second before another sound takes them. So their fade ends first.
122
129
 
123
130
  ## The page's calls
124
131
 
@@ -137,6 +144,10 @@ While the browser holds the audio, the page starts no file. So the browser warns
137
144
 
138
145
  The [sound demo](https://github.com/null3d-engine/null3d/tree/main/examples/sound) shows a storm. Each strike's thunder plays from where the bolt struck, once sound could have reached the camera. A brook in it is a group of points.
139
146
 
147
+ ![Lightning strikes a dark moor in the rain, behind a ring of standing stones and a brook.](../images/api/sound/sound-demo.webp)
148
+
149
+ The sound demo: thunder, rain and a brook.
150
+
140
151
  ## Pauses and hidden pages
141
152
 
142
153
  When the sketch's frames stop, every sound fades out within about a second, and the page suspends the audio soon after. That happens when `engine.setPaused(true)` pauses the engine, when the browser hides the page, and when a sketch stops calling `update`. It also happens when the engine was paused before the sound started. The audio resumes, and the sounds fade back in, when the frames move again.
@@ -149,7 +160,14 @@ A sound that comes from a place plays through the browser's panner. The High and
149
160
 
150
161
  ## Files
151
162
 
152
- A relative address resolves against the page's address, as the engine's loaders do ([Assets](assets.md)). So `'sounds/sea.mp3'` on `https://example.com/game/` loads `https://example.com/game/sounds/sea.mp3`. An address from `new URL('./sea.mp3', import.meta.url)` resolves against the sketch module instead. MP3 decodes in every browser. An MP3 encoder adds samples at each end of a file, and records how many in a header in the file's first frame. Some browsers keep them as they decode. Safari kept 576 at each end of a mono loop, which would dip at every join. The add-on reads the header and drops what the browser kept, so a loop joins with no gap. A file without that header loses only the silence at its ends, up to 2,048 samples. A file made to loop joins its end to its start, as the sample loops do.
163
+ A relative address resolves against the page's address, as the engine's loaders do ([Assets](assets.md)). So `'sounds/sea.mp3'` on `https://example.com/game/` loads `https://example.com/game/sounds/sea.mp3`. An address from `new URL('./sea.mp3', import.meta.url)` resolves against the sketch module instead. A file made to loop joins its end to its start, as the sample loops do.
164
+
165
+ <details>
166
+ <summary>Technical details: what the add-on does with MP3 padding</summary>
167
+
168
+ MP3 decodes in every browser. An MP3 encoder adds samples at each end of a file, and records how many in a header in the file's first frame. Some browsers keep them as they decode. Safari keeps 576 samples at each end of a mono MP3, which dips at every join of a loop. The add-on reads the header and drops what the browser kept, so a loop joins with no gap. A file without that header loses only the silence at its ends, up to 2,048 samples.
169
+
170
+ </details>
153
171
 
154
172
  ## Browsers
155
173
 
@@ -165,5 +183,23 @@ A relative address resolves against the page's address, as the engine's loaders
165
183
  - The table holds 12 numbers for the listener and 6 for each emitter: 6 KB at the default capacity.
166
184
  - The page plays 32 voices at once and the table holds 4,096 emitters at most.
167
185
 
168
- [The API reference](reference/sound.md) lists every export of this page with its type and
169
- description. The add-on's doc comments make it.
186
+ ## Errors
187
+
188
+ | Code | Cause |
189
+ | --- | --- |
190
+ | E2500 | A missing listener or file; a count, voices, capacity, volume, near or far that is not a valid number; far not above near; or a call on a destroyed sound system. |
191
+ | E2501 | More than 32 voices. |
192
+ | E2502 | No room in the table: raise `capacity`. |
193
+ | E2503 | A member index out of range. |
194
+ | E2504 | A second sound system. |
195
+
196
+ ## Related pages
197
+
198
+ - [Assets](assets.md): loading the sound files.
199
+ - [Hosting and cross-origin isolation](../getting-started/hosting.md): the headers that the mixer needs.
200
+ - [Quality API](quality.md): the presets that drop the HRTF panner.
201
+ - [Loops, groups and shots](https://github.com/null3d-engine/null3d/tree/main/examples/sound): a thunderstorm on the moor, with positional thunder, rain and a brook.
202
+
203
+ ## API reference
204
+
205
+ [The API reference](reference/sound.md) lists every export of this page with its type and description. The add-on's doc comments make it.
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: api/splats
3
3
  title: Gaussian splats (@null3d/splats)
4
- status: experimental
5
- since: "0.3"
6
4
  summary: "createSplats: SPZ files, streamed chunk files and KHR_gaussian_splatting models drawn as Gaussian splats, with detail levels, a budget that follows the preset and the GPU path, and a soft mix with meshes."
7
5
  ---
8
6
 
9
7
  # Gaussian splats
10
8
 
11
- > Roadmap step 0.3, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
12
-
13
9
  ```mermaid
14
10
  flowchart LR
15
11
  spz["An SPZ file"] --> read["decodeSpz"]
@@ -30,6 +26,10 @@ training run produces. A splat has a center, three principal spreads and a rotat
30
26
  shape, and a color and an opacity. The add-on draws each one as a camera-facing card that covers
31
27
  the splat's projected ellipse and fades as a Gaussian, so overlapping splats read as one surface.
32
28
 
29
+ ![A drone scan of a priory drawn from Gaussian splats.](../images/api/splats/splats-demo.webp)
30
+
31
+ The splats demo. On feature-draws, say the add-on makes the draw.
32
+
33
33
  The add-on reads three kinds of file:
34
34
 
35
35
  - **SPZ**, Niantic's packed form, in its gzip and zstd containers.
@@ -38,6 +38,12 @@ The add-on reads three kinds of file:
38
38
  - **glTF** models that carry `KHR_gaussian_splatting`. Importing the add-on registers the reader for
39
39
  every sketch of the thread, so `assets.loadGltf` loads such a model with no other call.
40
40
 
41
+ The package is an add-on. Install it beside the engine.
42
+
43
+ ```sh
44
+ bun add @null3d/splats
45
+ ```
46
+
41
47
  ```ts
42
48
  import { defineSketch } from '@null3d/engine';
43
49
  import { createSplats } from '@null3d/splats';
@@ -80,7 +86,9 @@ so the engine plans no levels for it.
80
86
  A splat's shader computes the covariance that its spreads and rotation make, projects it to the
81
87
  screen once for each corner of its card, and spans a camera-facing card along the ellipse's own
82
88
  axes. All three of the splat's spreads add to that ellipse, so a flat splat seen edge on still gets
83
- a card that covers it. A pixel's place on the card is its distance from the center in standard
89
+ a card that covers it.
90
+
91
+ A pixel's place on the card is its distance from the center in standard
84
92
  deviations, so a fragment fades by it with no math of its own, and the card is a Gaussian, not a
85
93
  disc. A pixel too faint to show is dropped before it blends.
86
94
 
@@ -103,6 +111,7 @@ writes only the rows that its budget keeps.
103
111
  cover the most surface: a splat's opacity times the area of its largest cross-section. A prefix of
104
112
  a chunk is a coarser level of it: all of it, a half, a quarter and an eighth. As a chunk is built,
105
113
  the add-on measures how much of the chunk's surface each prefix covers, seen from three directions.
114
+
106
115
  A prefix grows the splats that it keeps by what it leaves uncovered, up to three times. So a scan's
107
116
  ground of small flat splats draws as the same surface with fewer, larger splats rather than a
108
117
  sparse one, and a dense cloud whose splats overlap barely grows.
@@ -111,17 +120,19 @@ sparse one, and a dense cloud whose splats overlap barely grows.
111
120
  chunk draws. Every chunk the budget reaches keeps an eighth of itself first. The budget then
112
121
  gives the nearest chunks a finer prefix, up to the level that their distance asks for. A chunk's
113
122
  level follows its distance measured in the radius of the whole cloud, so a finer cut does not thin
114
- the view. A budget too small for every level then thins the whole cloud. A chunk nearer the camera
123
+ the view. A budget too small for every level then thins the whole cloud.
124
+
125
+ A chunk nearer the camera
115
126
  still keeps more of its splats than a far one. The budget stops the total. The selection and its
116
127
  distance sort run in the add-on's Rust module on the job worker, so a large scene costs the
117
- sketch's thread nothing. A change of the camera or of the budget starts a new selection. The core
128
+ sketch's thread nothing.
129
+
130
+ A change of the camera or of the budget starts a new selection. The core
118
131
  then packs the chosen rows into the batch in its own memory, so a moved frame copies no rows
119
132
  through the sketch's heap.
120
133
 
121
134
  `createSplats` starts the selection's task and the SPZ reader as it starts, beside the download of
122
- the index or the file. So the first selection and the first chunk's decode wait for neither. In the
123
- splat demo, on a simulated phone connection, the reader arrived at 4.4 seconds instead of 7.9. The
124
- first parts showed at 7.0 seconds instead of 8.3.
135
+ the index or the file. So the first selection and the first chunk's decode wait for neither download. In the splats demo on a simulated phone connection, the SPZ reader is ready at about 4.4 seconds and the first parts show at about 7 seconds.
125
136
 
126
137
  The budget follows the quality preset. On each preset the add-on draws at most:
127
138
 
@@ -139,7 +150,7 @@ preset's value, and `capacity` sets the rows that the batch holds.
139
150
 
140
151
  The batch allocates its rows when `createSplats` runs, from the preset in use then. A move to a
141
152
  higher preset after that draws no more splats, while a move to a lower one draws fewer. A scene
142
- that offers the Quality control at run time passes `capacity` for the highest preset it may reach,
153
+ that offers a quality menu at run time passes `capacity` for the highest preset it may reach,
143
154
  and pays that memory on every preset.
144
155
 
145
156
  A splat's color is the light recorded with it, so the scene's lights do not relight a cloud. Splats
@@ -149,7 +160,9 @@ A whole file appears as its chunks are cut: the first chunks draw while the rest
149
160
  large file never holds up a frame. `ready` resolves when every chunk of a file is ready. For a
150
161
  stream, it resolves once the nearest chunks have arrived, which takes about a megabyte of files, or
151
162
  the nearest chunk alone: the first view draws from those, and the cloud fills in as the rest
152
- arrive. Call `update` once before you await it. `settle()` resolves once the chosen splats are
163
+ arrive.
164
+
165
+ Call `update` once before you await it. `settle()` resolves once the chosen splats are
153
166
  packed, and for a stream once the chunks that they need have arrived, which a still picture waits
154
167
  for.
155
168
 
@@ -209,8 +222,21 @@ total size and `splats.loadedBytes` gives the part that has arrived.
209
222
  | `checkSplats(data, what)` | The same `data`, once its arrays hold the number of splats that it names. Otherwise it throws E2101, whose message names `what` |
210
223
  | `SPLAT_EXTENSION` | The name of the glTF extension that the add-on reads, `'KHR_gaussian_splatting'` |
211
224
 
212
- `Splats` gives `update(eye)`, `setBudget(count)`, `settle()`, `ready`, `total`, `drawn`, `chunks`,
213
- `loaded`, `failed`, `bytes`, `loadedBytes`, `feature` and `draw`, and `destroy()` frees it.
225
+ `update(eye)` chooses the splats that draw from the camera's position, and `setBudget(count)` caps the splats that draw below the preset's count. The other members are:
226
+
227
+ | Member | What it does |
228
+ | --- | --- |
229
+ | `update(eye)` | Chooses the splats that draw from the camera's position. Call it once a frame |
230
+ | `setBudget(count)` | Caps the splats that draw below the preset's count |
231
+ | `ready` | Resolves when the cloud can draw its first view |
232
+ | `settle()` | Resolves once the chosen splats are packed and their chunks have arrived |
233
+ | `total`, `drawn` | The splats that the cloud holds, and the splats that draw now |
234
+ | `chunks`, `loaded`, `failed` | The cloud's chunks, the ones that have loaded, and the ones that stayed unreachable |
235
+ | `bytes`, `loadedBytes` | The cloud's total size, and the part that has arrived |
236
+ | `feature`, `draw` | The add-on feature, whose settings, budget and figures the engine resolves, and the draw, whose batch rows are the splats that draw |
237
+ | `destroy()` | Frees the cloud |
238
+
239
+ The [API reference](reference/splats.md#splats) gives each member's type.
214
240
 
215
241
  ## Costs and limits
216
242
 
@@ -224,7 +250,7 @@ total size and `splats.loadedBytes` gives the part that has arrived.
224
250
  - A glTF model's splats draw in the file's order inside each part. For a large cloud, use an SPZ
225
251
  file or a splat stream, whose rows the engine sorts one by one.
226
252
  - A splat casts no shadow and receives none.
227
- - The cards need the scene's depth copy, which a material that reads the scene's depth makes. It
253
+ - The cards need the scene's depth copy, which the splat cards' material makes. It
228
254
  costs one full-screen pass and one texture of four bytes for each pixel.
229
255
  - A stream spends one request for each chunk that the view needs. Keep the chunks large enough that
230
256
  the requests do not dominate, and small enough that the first view arrives quickly: before that
@@ -257,6 +283,7 @@ total size and `splats.loadedBytes` gives the part that has arrived.
257
283
  - [Add-on feature descriptions](features.md): the settings, budget and figures that the cloud
258
284
  declares.
259
285
  - [Assets](assets.md#extensions-that-add-ons-read): how an add-on reads a glTF extension.
286
+ - [Streaming and the budget](https://github.com/null3d-engine/null3d/tree/main/examples/splats): a 1.7 million splat scan of Bolton Priory, streamed nearest first within the preset's budget.
260
287
 
261
288
  ## API reference
262
289
 
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: api/sprites
3
3
  title: Sprites
4
- status: experimental
5
- since: "0.2"
6
4
  summary: "createSprites; world and screen size modes; atlases."
7
5
  ---
8
6
 
9
7
  # Sprites
10
8
 
11
- > Roadmap step 0.2, first released in null3D 0.1.0. The API is experimental, so it can still change between versions. Sprites do not cast or receive shadows, and overlap queries do not find them.
12
-
13
9
  ```mermaid
14
10
  flowchart LR
15
11
  code["Sketch code writes rows<br/>into typed arrays"] --> rows[("Sprite rows<br/>position, size, rotation,<br/>color, frame")]
@@ -22,6 +18,10 @@ A sprite is a flat picture that always faces the camera, such as a particle, a m
22
18
 
23
19
  A batch is one draw for the GPU, whatever its size. The engine culls the sprites and sorts blended sprites back to front, as it does for instance rows. This work runs on the job workers or on the GPU. The [sprites and lines demo](https://github.com/null3d-engine/null3d/tree/main/examples/sprites-lines) draws a fountain of 2,000 sparks in one batch.
24
20
 
21
+ ![A fountain of glowing sparks over a dark pool, ringed by a neon helix and a dashed loop of lines.](../images/api/sprites/sprites-lines.webp)
22
+
23
+ Only the sparks are sprites; the helix and the loop are lines.
24
+
25
25
  ```ts
26
26
  import { defineSketch } from '@null3d/engine';
27
27
 
@@ -142,13 +142,15 @@ With `sizeAttenuation: false`, sizes are in CSS pixels, at every distance and on
142
142
 
143
143
  ## Blending, sorting and depth
144
144
 
145
- Blended sprites draw after the opaque objects, farthest first, in the same transparent pass as other blended objects. The engine sorts each sprite by the depth of its position in the camera's view. It sorts the sprites of a batch among each other and among the scene's other blended objects and rows, every frame. Sorting costs time in large batches. For particles that add light, such as sparks and fire, `blending: 'additive'` with `depthWrite: false` needs no exact order to look right.
145
+ Blended sprites draw after the opaque objects, farthest first, in the same transparent pass as other blended objects. The engine sorts each sprite by the depth of its position in the camera's view. It sorts the sprites of a batch among each other and among the scene's other blended objects and rows, every frame. Sorting costs time in large batches. For particles that add light, such as sparks and fire, `blending: 'additive'` with `depthWrite: false` needs no exact order to look right. Sprites do not cast or receive shadows.
146
146
 
147
147
  `alphaMode: 'mask'` draws sprites opaque, cut out by their alpha, with no sorting. Use it for foliage and other sharp edged pictures.
148
148
 
149
149
  ## Clicks and raycasts
150
150
 
151
- Raycasts hit a sprite where the ray crosses its quad. The hit's `object` is the batch, and its `instance` is the sprite. A sprite batch takes `sprites.on('click', handler)` and the other [pointer events on objects](input.md#pointer-events-on-objects), as an instance batch does. A ray hits the whole quad, also where the sprite's map is clear. [Raycasting](raycast.md#sprites-points-and-lines) has the details.
151
+ Raycasts hit a sprite where the ray crosses its quad. The hit's `object` is the batch, and its `instance` is the sprite. A sprite batch takes `sprites.on('click', handler)` and the other [pointer events on objects](input.md#pointer-events-on-objects), as an instance batch does. A ray hits the whole quad, also where the sprite's map is clear.
152
+
153
+ [Raycasting](raycast.md#sprites-points-and-lines) has the details. Overlap queries do not find sprites. Use a raycast.
152
154
 
153
155
  ```ts
154
156
  sprites.on('click', (event) => {
package/docs/api/tasks.md CHANGED
@@ -1,48 +1,57 @@
1
1
  ---
2
2
  id: api/tasks
3
3
  title: Tasks on the job workers
4
- status: experimental
5
- since: "0.3"
6
4
  summary: "runTask: an add-on's code and WebAssembly on the job workers; shared memory; ordering with the frames; failures."
7
5
  ---
8
6
 
9
7
  # Tasks on the job workers
10
8
 
11
- > Roadmap step 0.3, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
12
-
13
- An add-on can move heavy work off the thread that runs the sketch, and onto the engine's job workers. `runTask` runs a task there while the frames continue. An add-on that chooses what to draw, or in what order, from many objects uses this hook: the Gaussian splats add-on chooses each chunk's prefix there, nearest first. The engine's transparent pass then sorts the chosen rows on the job workers, as it does for every blended draw, so an add-on does not sort its drawn rows through a task.
9
+ An add-on can move heavy work off the thread that runs the sketch, and onto the engine's job workers. `runTask` runs a task there while the frames continue.
14
10
 
15
11
  A task is an object with three parts: a name, the address of the module that runs, and the WebAssembly files that the module uses. The module exports `run`, and the worker calls it for each task. The answer travels back to the thread that called `runTask`.
16
12
 
17
13
  ```ts
18
14
  import { runTask, type Task } from '@null3d/engine';
19
15
 
20
- export const SORT_TASK: Task = {
21
- name: 'splat-sort',
22
- script: new URL('./sort-task.ts', import.meta.url),
16
+ export const SELECT_TASK: Task = {
17
+ name: 'splats-select',
18
+ script: new URL('./select-task.ts', import.meta.url),
23
19
  wasm: [
24
- { name: 'splat', url: new URL('./splat.wasm', import.meta.url), what: 'the splat sorter' },
20
+ { name: 'splats', url: new URL('./splats.wasm', import.meta.url), what: 'the splat selector' },
25
21
  ],
26
22
  };
27
23
 
28
- export async function sortRound(view: Int32Array): Promise<number[]> {
29
- const answer = await runTask<{ order: number[] }>(SORT_TASK, { view });
30
- return answer.order;
24
+ export async function selectChunks(view: Int32Array): Promise<number[]> {
25
+ const answer = await runTask<{ chosen: number[] }>(SELECT_TASK, { view });
26
+ return answer.chosen;
31
27
  }
32
28
  ```
33
29
 
30
+ The Gaussian splats add-on uses a task like this to choose each chunk's splats, nearest first. The engine's transparent pass sorts the chosen rows itself, as it does for every blended draw, so a task chooses rows but does not sort them for drawing.
31
+
34
32
  The module that the task's address names looks like this:
35
33
 
36
34
  ```ts
37
35
  export function run(input, wasm) {
38
- const module = wasm('splat'); // the compiled WebAssembly module, by the name the task gives it
39
- const sorter = startSorter(module); // the add-on's own start of its WebAssembly
40
- const order = sorter.sort(input.view); // reads the caller's shared buffer
41
- // ... write the order into the shared buffer ...
42
- return { output: { order } };
36
+ const module = wasm('splats'); // the compiled WebAssembly module, by the name the task gives it
37
+ const selector = startSelector(module); // the add-on's own start of its WebAssembly
38
+ const chosen = selector.select(input.view); // reads the caller's shared buffer
39
+ // ... write the chosen chunks into the shared buffer ...
40
+ return { output: { chosen } };
43
41
  }
44
42
  ```
45
43
 
44
+ `run` returns an object with the answer in `output`, and the promise of `runTask` resolves with that `output`. A `run` that returns its answer directly resolves the promise with `undefined`.
45
+
46
+ <details>
47
+ <summary>Technical details: what can pass to a worker, and moving buffers</summary>
48
+
49
+ A task's input and output must pass to another worker: numbers, strings, plain objects and arrays, typed arrays, `ArrayBuffer`s and `SharedArrayBuffer`s.
50
+
51
+ The engine copies an `ArrayBuffer` by default. To move one instead of copying it, name it in a transfer list. `run` can return `{ output, transfer: [buffer] }` to move a buffer back to the caller. `runTask(task, input, [buffer])` moves a buffer of the input to the worker. A moved buffer is empty on the side that sent it.
52
+
53
+ </details>
54
+
46
55
  The engine imports the module once in each worker that runs the task, so the module keeps its state between tasks. Each WebAssembly file compiles once per page, and the engine sends the compiled module to the workers.
47
56
 
48
57
  The build bundles the module that the task names, so it may be TypeScript with its imports. A worker imports it as it does any module, and raw TypeScript would not parse there. The engine's Vite plugin emits the module as its own chunk for that reason.
@@ -60,16 +69,16 @@ Throw a `TaskLoadError` when the module's code or WebAssembly cannot start. The
60
69
 
61
70
  ## Shared memory
62
71
 
63
- A per-frame sort copies no data when its input and output live in one `SharedArrayBuffer`. The engine posts the buffer to the worker without copying it, and the task writes the result into it.
72
+ A per-frame task copies no data when its input and output live in one `SharedArrayBuffer`. The engine posts the buffer to the worker without copying it, and the task writes the result into it.
64
73
 
65
74
  ```ts
66
75
  const shared = new SharedArrayBuffer(4 * (1 + count * 2));
67
76
  const view = new Int32Array(shared);
68
- // view[0] holds the count, view[1] to view[count] the values, and the task writes the sorted
69
- // values after them.
77
+ // view[0] holds the count, view[1] to view[count] the values, and the task writes the chosen
78
+ // indices after them.
70
79
  Atomics.store(view, 0, count);
71
- const answer = await runTask(SORT_TASK, { view });
72
- const sorted = view.subarray(count + 1, count + 1 + count);
80
+ const answer = await runTask(SELECT_TASK, { view });
81
+ const chosen = view.subarray(count + 1, count + 1 + count);
73
82
  ```
74
83
 
75
84
  While a task runs, both threads read the shared memory. Order the writes that both threads read with `Atomics`. Once the promise resolves, the worker has stopped writing, and the sketch reads the buffer as it stands.
@@ -1,14 +1,14 @@
1
1
  ---
2
2
  id: api/terrain
3
3
  title: "Terrain (@null3d/terrain)"
4
- status: experimental
5
- since: "0.2"
6
4
  summary: "createTerrain: a height field drawn as a camera-centered quadtree of blocks, lit per pixel from baked normals, with a height-blended splat surface, and heightAt and normalAt for placing objects."
7
5
  ---
8
6
 
9
7
  # Terrain (@null3d/terrain)
10
8
 
11
- > Roadmap step 0.2, first released in null3D 0.1.0. The API is experimental, so it can still change between versions. The ground has no collision. A block morphs toward its parent block's surface before it gives way to it, and a skirt closes the seam between neighbours that differ by more than one level.
9
+ ![A hilly valley at golden hour drawn by the terrain add-on, with snow on the far ridge.](../images/api/terrain/terrain-demo.webp)
10
+
11
+ The terrain demo.
12
12
 
13
13
  ```mermaid
14
14
  flowchart LR
@@ -18,7 +18,9 @@ flowchart LR
18
18
  splat --> screen["The ground"]
19
19
  ```
20
20
 
21
- The `@null3d/terrain` package draws a height field as terrain. It divides the ground into a quadtree of blocks. A block near the camera draws a fine grid, and a block far away draws a coarse grid over more ground. The ground near the camera then keeps its detail, and the far hills stay cheap. Every pixel takes its normal from a map baked over the whole ground, so a coarse block still shows the relief of the hills. One material blends up to four tiled color layers over every block by a splat map, and each layer's own height shows it through its neighbours where two layers meet.
21
+ The `@null3d/terrain` package draws a height field as terrain. It divides the ground into a quadtree of blocks. A block near the camera draws a fine grid, and a block far away draws a coarse grid over more ground. The ground near the camera then keeps its detail, and the far hills stay cheap.
22
+
23
+ Every pixel takes its normal from a map baked over the whole ground, so a coarse block still shows the relief of the hills. One material blends up to four tiled color layers over every block by a splat map, and each layer's own height shows it through its neighbors where two layers meet.
22
24
 
23
25
  The package is built on the engine's public API. Its surface function ships compiled, so your build does not compile it, and the package needs no extra setup for it.
24
26
 
@@ -65,9 +67,11 @@ export default defineSketch((ctx) => {
65
67
  });
66
68
  ```
67
69
 
70
+ [`TerrainOptions`](reference/terrain.md#terrainoptions) lists every option with its default, [`TerrainLayer`](reference/terrain.md#terrainlayer) every layer field, and [`Terrain`](reference/terrain.md#terrain) every member of the object that `createTerrain` returns.
71
+
68
72
  ## How it works
69
73
 
70
- `createTerrain` samples a height field once, into a flat array of heights. The height comes from a function or from a height map held as a `Float32Array`. Later reads, such as `heightAt`, interpolate between the four samples around the point.
74
+ `createTerrain` samples a height field once, into a flat array of heights. The height comes from a function of x and z, or from a `Float32Array` height map. A map holds `resolution` × `resolution` heights in meters (256 × 256 by default), row by row from the ground's −z edge. [`TerrainOptions`](reference/terrain.md#terrainoptions) lists every option and its default. Later reads, such as `heightAt`, interpolate between the four samples around the point.
71
75
 
72
76
  The ground is a quadtree. The root block covers the whole ground with a coarse grid. A block splits into four while one of its cells would show more screen error than the ground allows.
73
77
 
@@ -77,31 +81,57 @@ The `error` option sets the allowed error in pixels. The default follows the qua
77
81
 
78
82
  Each block is one mesh whose vertices carry the heights. The height field never changes shape after `createTerrain`. Once a block is made, the engine reuses it. The terrain picks its blocks again when the camera crosses a cell of the finest level, rises by one such cell, or draws through a different lens or buffer size. A still camera therefore costs no work at all.
79
83
 
80
- Every pixel lights from a normal map that `createTerrain` bakes from the height field's slopes. A far block covers ground in cells larger than the height samples, so its own vertices cannot show the small hills. The normal map keeps that relief at every distance. Set `bakedNormals: false` to light from the mesh's own normals instead. A map laid from above stretches into streaks on a steep face, so `steepFaces: true` maps the third layer, the rock, from the sides there, for two more samples of that map.
84
+ Every pixel lights from a normal map that `createTerrain` bakes from the height field's slopes. A far block covers ground in cells larger than the height samples, so its own vertices cannot show the small hills. The normal map keeps that relief at every distance. Set `bakedNormals: false` to light from the mesh's own normals instead.
85
+
86
+ A map laid from above stretches into streaks on a steep face, so `steepFaces: true` maps the third layer, the rock, from the sides there, for two more samples of that map.
81
87
 
82
- Each block morphs toward its parent block's surface as the camera pulls away. A block hands over to its parent at the distance where the parent's own cell would no longer show the allowed error. Over the last part of the approach, the block's vertices move to the parent grid's heights, so the two surfaces match when the switch comes and the ground does not pop. The parent grid is triangulated the same way, so a block whose morph is complete has exactly the parent's surface. The morph adds no draw and no work in a frame: each block carries the parent's height at each vertex in its own vertex record, written when the block is built or reused. `morph: false` switches each block at once instead.
88
+ Each block morphs toward its parent block's surface as the camera pulls away. A block hands over to its parent at the distance where the parent's own cell would no longer show the allowed error. `morph: false` switches each block at once instead.
83
89
 
84
90
  The quadtree is the ground's levels of detail, so each block mesh takes `levels: false`, and the engine plans no levels of its own for it.
85
91
 
86
- A skirt hangs from the edge of each block, a little below the ground. The selection does not restrict neighbours, so two blocks that meet can differ by more than one level. The morph ends on a block's own parent, which cannot match a neighbour two levels coarser, so the skirt still covers that seam. On a very steep seam a thin sliver of skirt can still show.
92
+ A skirt hangs from the edge of each block to close the seam between blocks of different detail. On a very steep seam a thin sliver of skirt can still show.
93
+
94
+ <details>
95
+ <summary>Technical details: the morph and the skirt</summary>
96
+
97
+ Over the last part of the approach, the block's vertices move to the parent grid's heights, so the two surfaces match when the switch comes and the ground does not pop. The parent grid is triangulated the same way, so a block whose morph is complete has exactly the parent's surface. The morph adds no draw and no work in a frame: each block carries the parent's height at each vertex in its own vertex record, written when the block is built or reused.
98
+
99
+ The selection does not restrict neighbors, so two blocks that meet can differ by more than one level. The morph ends on a block's own parent, which cannot match a neighbor two levels coarser, so the skirt still covers that seam.
100
+
101
+ </details>
87
102
 
88
103
  ## Layers and the splat map
89
104
 
90
105
  The ground's material blends up to four layers. Each layer is a tiled color map with its own repeat distance and roughness. A splat map, one channel per layer, gives the weight of each layer at each point.
91
106
 
92
- A layer also carries a height at each texel. Its map's brightness gives that height by default, or its alpha channel where `heightFrom` says `'alpha'`. The four maps that `terrainTextures` makes keep a height in their alpha channel, so give their layers `heightFrom: 'alpha'`. At a boundary between two layers, the heights push one through the other. Rock then shows through the grass in ragged patches rather than in a smooth cross-fade. `contrast` sets how strongly a layer's height scales its weight, from 0 to 1. The default is 1, and 0 blends by the splat map's weights alone.
107
+ A layer also carries a height at each texel. Its map's brightness gives that height by default, or its alpha channel where `heightFrom` says `'alpha'`. The four maps that `terrainTextures` makes keep a height in their alpha channel, so give their layers `heightFrom: 'alpha'`. At a boundary between two layers, the heights push one through the other.
93
108
 
94
- The default splat map comes from the ground itself. Grass takes the low, gentle ground, soil the higher dry ground, rock the steep slopes, and snow the ground above. Give `splat` to paint the weights yourself. `terrainTextures` makes the four layer maps in code, or give any `Texture` from `textures.fromData` or `assets`.
109
+ Rock then shows through the grass in ragged patches rather than in a smooth cross-fade. `contrast` sets how strongly a layer's height scales its weight, from 0 to 1. The default is 1, and 0 blends by the splat map's weights alone.
110
+
111
+ The default splat map comes from the ground itself. Grass takes the low, gentle ground, soil the higher dry ground, rock the steep slopes, and snow the ground above. Give `splat`, a function of x and z that writes the four layer weights into `out`, to set the weights yourself. `terrainTextures` makes the four layer maps in code, or give any `Texture` from `textures.fromData` or `assets`.
95
112
 
96
113
  ## Placing things on the ground
97
114
 
98
115
  The terrain carries no collision. Read `heightAt(x, z)` and `normalAt(x, z, out)` to place anything that stands on the ground, and to set its upright direction. Both allocate nothing, so a frame may call them freely.
99
116
 
117
+ ## Hiding and tinting the ground
118
+
119
+ `terrain.setVisible(false)` hides the ground, and `setVisible(true)` shows the current selection again. `terrain.setTint(color)` multiplies the ground's own colors, white by default, so a scene can take the ground down with the hour at night without changing its day look.
120
+
100
121
  ## Speed
101
122
 
102
- A terrain draws one mesh per visible block. The blocks near the camera are the most and the finest, and the few coarse blocks cover the rest. A 1,200 meter ground at the default settings draws about 200 blocks, near 130,000 triangles, from a camera near the ground. That figure is for a picture 1,080 pixels tall. A taller picture draws more and finer blocks, and a shorter one draws fewer. A higher camera or a wider field of view draws fewer and coarser blocks, and the Low preset draws coarser blocks than High. A block splits or merges only when the camera moves far enough to need other blocks.
123
+ A terrain draws one mesh per visible block. The blocks near the camera are the most and the finest, and the few coarse blocks cover the rest. A 1,200 meter ground at the default settings draws about 200 blocks, near 130,000 triangles, from a camera near the ground. That figure is for a picture 1,080 pixels tall. A taller picture draws more and finer blocks, and a shorter one draws fewer.
124
+
125
+ A higher camera or a wider field of view draws fewer and coarser blocks, and the Low preset draws coarser blocks than High. A block splits or merges only when the camera moves far enough to need other blocks.
126
+
127
+ The quadtree selection and the block build run in the add-on's own WebAssembly module. The module runs on the engine's job workers, so `update` only asks for the work. A crossing can need tens of new blocks. The ground draws its last complete selection until the new one is built, and `terrain.building` is true meanwhile. `blocksPerUpdate` caps each task's blocks, at 16 by default.
128
+
129
+ <details>
130
+ <summary>Technical details: how blocks build on the job workers</summary>
131
+
132
+ The first selection builds every block that it needs, over a few tasks. The arrays that the tasks fill are made once and shared, so the frame path allocates nothing. A block that the view leaves gives up its mesh to the next block that the ground needs. A terrain that follows a moving camera therefore builds meshes only until it holds `maxBlocks` of them.
103
133
 
104
- The quadtree selection and the block build run in the add-on's own WebAssembly module. The module runs on the engine's job workers, so `update` only asks for the work. A crossing can need tens of new blocks. One task builds at most `blocksPerUpdate` of them, 16 by default. The first selection builds every block that it needs, over a few tasks. The ground draws its last complete selection until the new one is built, and `terrain.building` is true meanwhile. The arrays that the tasks fill are made once and shared, so the frame path allocates nothing. A block that the view leaves gives up its mesh to the next block that the ground needs. A terrain that follows a moving camera therefore builds meshes only until it holds `maxBlocks` of them. `terrain.setVisible(false)` hides the ground, and `setVisible(true)` shows the current selection again. `terrain.setTint(color)` multiplies the ground's own colors, white by default, so a scene can take the ground down with the hour at night without changing its day look.
134
+ </details>
105
135
 
106
136
  A sketch that never imports `@null3d/terrain` ships no terrain code and starts no terrain work. The package follows the engine's add-on rules: its surface function ships compiled, and the selection and build module loads when a terrain first picks its blocks.
107
137
 
@@ -112,6 +142,8 @@ A sketch that never imports `@null3d/terrain` ships no terrain code and starts n
112
142
  - [Materials](materials.md): the standard material and custom surfaces.
113
143
  - [Particles](particles.md): the other add-on of the same kind.
114
144
  - [The three.js mapping](../porting/threejs-mapping.md): terrain beside three.js's ways of making ground.
145
+ - [Terrain](https://github.com/null3d-engine/null3d/tree/main/examples/terrain): a 1.2 km valley with grass, soil, rock and snow blended by height and slope.
146
+ - [Used in a large scene](https://github.com/null3d-engine/null3d/tree/main/examples/showcase/island): the Island showcase sets `steepFaces` and passes its own `splat` function.
115
147
 
116
148
  ## API reference
117
149