@null3d/engine 0.1.0 → 0.1.1

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 (381) 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 +39 -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 +65 -25
  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/E1401.md +11 -3
  146. package/docs/errors/E1402.md +13 -5
  147. package/docs/errors/E1403.md +11 -3
  148. package/docs/errors/E1404.md +13 -3
  149. package/docs/errors/E1405.md +15 -4
  150. package/docs/errors/E1406.md +31 -5
  151. package/docs/errors/E1407.md +11 -3
  152. package/docs/errors/E1408.md +11 -3
  153. package/docs/errors/E1409.md +11 -3
  154. package/docs/errors/E1410.md +13 -5
  155. package/docs/errors/E1411.md +11 -3
  156. package/docs/errors/E1412.md +29 -6
  157. package/docs/errors/E1413.md +11 -3
  158. package/docs/errors/E1414.md +11 -3
  159. package/docs/errors/E1415.md +11 -3
  160. package/docs/errors/E1416.md +27 -6
  161. package/docs/errors/E1417.md +11 -3
  162. package/docs/errors/E1418.md +12 -3
  163. package/docs/errors/E1419.md +13 -5
  164. package/docs/errors/E1420.md +11 -3
  165. package/docs/errors/E1421.md +11 -3
  166. package/docs/errors/E1422.md +12 -3
  167. package/docs/errors/E1423.md +12 -3
  168. package/docs/errors/E1424.md +24 -5
  169. package/docs/errors/E1425.md +11 -3
  170. package/docs/errors/E1426.md +11 -3
  171. package/docs/errors/E1430.md +11 -3
  172. package/docs/errors/E1431.md +11 -3
  173. package/docs/errors/E1432.md +11 -3
  174. package/docs/errors/E1433.md +11 -3
  175. package/docs/errors/E1434.md +15 -6
  176. package/docs/errors/E1435.md +11 -3
  177. package/docs/errors/E1436.md +11 -3
  178. package/docs/errors/E1437.md +14 -6
  179. package/docs/errors/E1501.md +41 -6
  180. package/docs/errors/E1502.md +13 -3
  181. package/docs/errors/E1503.md +11 -3
  182. package/docs/errors/E1504.md +11 -3
  183. package/docs/errors/E1505.md +23 -5
  184. package/docs/errors/E1506.md +11 -3
  185. package/docs/errors/index.md +52 -29
  186. package/docs/getting-started/cdn.md +15 -9
  187. package/docs/getting-started/first-scene.md +20 -12
  188. package/docs/getting-started/hosting.md +111 -63
  189. package/docs/getting-started/install.md +39 -24
  190. package/docs/getting-started/network-use.md +31 -21
  191. package/docs/getting-started/project-structure.md +20 -8
  192. package/docs/getting-started/third-party-notices.md +6 -10
  193. package/docs/guides/accessibility.md +4 -4
  194. package/docs/guides/add-ons.md +83 -13
  195. package/docs/guides/agents.md +2 -6
  196. package/docs/guides/assets-pipeline.md +248 -72
  197. package/docs/guides/audio.md +17 -15
  198. package/docs/guides/baked-light-data.md +44 -23
  199. package/docs/guides/car-paint.md +37 -31
  200. package/docs/guides/content-pages.md +53 -29
  201. package/docs/guides/custom-passes.md +59 -17
  202. package/docs/guides/custom-shaders.md +54 -37
  203. package/docs/guides/debugging.md +31 -41
  204. package/docs/guides/deploying.md +6 -27
  205. package/docs/guides/deterministic-simulation.md +23 -16
  206. package/docs/guides/first-use-shaders.md +83 -45
  207. package/docs/guides/lens-kit.md +29 -26
  208. package/docs/guides/loading-screens.md +70 -30
  209. package/docs/guides/multiple-views.md +22 -11
  210. package/docs/guides/performance.md +188 -37
  211. package/docs/guides/phones.md +71 -19
  212. package/docs/guides/physics.md +6 -10
  213. package/docs/guides/screens.md +41 -29
  214. package/docs/guides/start-time.md +120 -85
  215. package/docs/guides/testing.md +137 -98
  216. package/docs/guides/ui-overlays.md +17 -12
  217. package/docs/guides/video-textures.md +14 -13
  218. package/docs/images/api/animation/character.webp +0 -0
  219. package/docs/images/api/animation/look-at-ring.webp +0 -0
  220. package/docs/images/api/animation/morph-tulips.webp +0 -0
  221. package/docs/images/api/animation/skeleton-lines.webp +0 -0
  222. package/docs/images/api/controls/gizmo-handles.webp +0 -0
  223. package/docs/images/api/controls/walk-and-fly.webp +0 -0
  224. package/docs/images/api/debug/debug-shapes.webp +0 -0
  225. package/docs/images/api/debug/levels-view.webp +0 -0
  226. package/docs/images/api/engine/transparent-canvas.webp +0 -0
  227. package/docs/images/api/feature-draws/feature-draw-clouds.webp +0 -0
  228. package/docs/images/api/feature-instances/compute-instances.webp +0 -0
  229. package/docs/images/api/feature-instances/instances-shadow.webp +0 -0
  230. package/docs/images/api/geometry/extra-shapes.webp +0 -0
  231. package/docs/images/api/geometry/generators.webp +0 -0
  232. package/docs/images/api/geometry/mesh-arrays.webp +0 -0
  233. package/docs/images/api/geometry/vertex-updates.webp +0 -0
  234. package/docs/images/api/grass/grass-grove.webp +0 -0
  235. package/docs/images/api/grass/grass-presets.webp +0 -0
  236. package/docs/images/api/grass/grass-shadow.webp +0 -0
  237. package/docs/images/api/impostors/impostor-handover.webp +0 -0
  238. package/docs/images/api/impostors/impostor-shadow.webp +0 -0
  239. package/docs/images/api/impostors/impostors-forest.webp +0 -0
  240. package/docs/images/api/lights/spot-cookies.webp +0 -0
  241. package/docs/images/api/lines/lines-lit.webp +0 -0
  242. package/docs/images/api/lines/lines-widths.webp +0 -0
  243. package/docs/images/api/materials/coat-and-sheen.webp +0 -0
  244. package/docs/images/api/materials/decals.webp +0 -0
  245. package/docs/images/api/materials/iridescence-and-anisotropy.webp +0 -0
  246. package/docs/images/api/materials/relief.webp +0 -0
  247. package/docs/images/api/materials/shader-inputs.webp +0 -0
  248. package/docs/images/api/materials/standard-grid.webp +0 -0
  249. package/docs/images/api/materials/subsurface.webp +0 -0
  250. package/docs/images/api/materials/translucency.webp +0 -0
  251. package/docs/images/api/objects/objects-turntable.webp +0 -0
  252. package/docs/images/api/particles/campfire.webp +0 -0
  253. package/docs/images/api/points/galaxy.webp +0 -0
  254. package/docs/images/api/reference/textures/texture-wrap.webp +0 -0
  255. package/docs/images/api/render/fullscreen-glow.webp +0 -0
  256. package/docs/images/api/render/reflection-mirror.webp +0 -0
  257. package/docs/images/api/render/security-camera.webp +0 -0
  258. package/docs/images/api/scene/aurora-sky-layer.webp +0 -0
  259. package/docs/images/api/sound/sound-demo.webp +0 -0
  260. package/docs/images/api/splats/splats-demo.webp +0 -0
  261. package/docs/images/api/sprites/sprites-lines.webp +0 -0
  262. package/docs/images/api/terrain/terrain-demo.webp +0 -0
  263. package/docs/images/api/textures/canvas-screen.webp +0 -0
  264. package/docs/images/api/textures/mipmaps.webp +0 -0
  265. package/docs/images/api/vertex-draws/rain.webp +0 -0
  266. package/docs/images/api/water/ocean.webp +0 -0
  267. package/docs/images/api/water/water-demo.webp +0 -0
  268. package/docs/images/api/water/water-rain.webp +0 -0
  269. package/docs/images/api/water/waterfall.webp +0 -0
  270. package/docs/images/concepts/assets/gltf-model.webp +0 -0
  271. package/docs/images/concepts/color-management/tone-mapped.webp +0 -0
  272. package/docs/images/concepts/culling/far-from-origin.webp +0 -0
  273. package/docs/images/concepts/instances/instances.webp +0 -0
  274. package/docs/images/concepts/instances/per-row-colors.webp +0 -0
  275. package/docs/images/concepts/large-worlds/large-world.webp +0 -0
  276. package/docs/images/concepts/lighting/area-lights.webp +0 -0
  277. package/docs/images/concepts/lighting/atmosphere.webp +0 -0
  278. package/docs/images/concepts/lighting/environment.webp +0 -0
  279. package/docs/images/concepts/lighting/light-shafts.webp +0 -0
  280. package/docs/images/concepts/lighting/reflection-probes.webp +0 -0
  281. package/docs/images/concepts/lod/forest.webp +0 -0
  282. package/docs/images/concepts/materials/weighted-transparency.webp +0 -0
  283. package/docs/images/concepts/post-processing/auto-exposure.webp +0 -0
  284. package/docs/images/concepts/post-processing/bounce-light.webp +0 -0
  285. package/docs/images/concepts/post-processing/depth-of-field.webp +0 -0
  286. package/docs/images/concepts/post-processing/post-effects-alley.webp +0 -0
  287. package/docs/images/concepts/post-processing/sharpen.webp +0 -0
  288. package/docs/images/concepts/post-processing/ssr-wet-street.webp +0 -0
  289. package/docs/images/concepts/render-layers/layers-cottages.webp +0 -0
  290. package/docs/images/concepts/shadows/hero-shadow.webp +0 -0
  291. package/docs/images/cookbook/index/picking.webp +0 -0
  292. package/docs/images/cookbook/index/time-of-day.webp +0 -0
  293. package/docs/images/errors/E1431/area-lights-no-tables.webp +0 -0
  294. package/docs/images/guides/custom-passes/reflection-water.webp +0 -0
  295. package/docs/images/guides/deterministic-simulation/replay-demo.webp +0 -0
  296. package/docs/images/guides/multiple-views/split-screen.webp +0 -0
  297. package/docs/images/guides/performance/overdraw-view.webp +0 -0
  298. package/docs/images/guides/testing/hold-mode-frame.webp +0 -0
  299. package/docs/images/index/showcase-island.webp +0 -0
  300. package/docs/images/porting/threejs-shaders/dissolve.webp +0 -0
  301. package/docs/images/shaders/builtins/builtins.webp +0 -0
  302. package/docs/images/shaders/surface-functions/custom-surface.webp +0 -0
  303. package/docs/images/shaders/surface-functions/vertex-offset.webp +0 -0
  304. package/docs/images/shots.json +2231 -0
  305. package/docs/index.md +14 -23
  306. package/docs/moves.json +1 -0
  307. package/docs/nav.json +300 -0
  308. package/docs/pages.md +230 -212
  309. package/docs/porting/react-three-fiber.md +2 -6
  310. package/docs/porting/threejs-loop-and-threads.md +1 -5
  311. package/docs/porting/threejs-mapping.md +1248 -234
  312. package/docs/porting/threejs-materials.md +10 -5
  313. package/docs/porting/threejs-overview.md +26 -6
  314. package/docs/porting/threejs-postprocessing.md +2 -6
  315. package/docs/porting/threejs-shaders.md +16 -14
  316. package/docs/porting/threejs-unsupported.md +6 -6
  317. package/docs/porting/verification.md +0 -4
  318. package/docs/shaders/builtins.md +15 -4
  319. package/docs/shaders/library.md +9 -6
  320. package/docs/shaders/surface-functions.md +51 -16
  321. package/docs/shaders/wgsl-rules.md +12 -5
  322. package/lib/debug/debug.d.ts +2 -2
  323. package/lib/debug/draw.d.ts +2 -2
  324. package/lib/debug/draw.js +8 -4
  325. package/lib/debug/overlay-look.d.ts +6 -0
  326. package/lib/debug/overlay-look.js +29 -7
  327. package/lib/debug/overlay.js +15 -11
  328. package/lib/debug/stats-options.d.ts +13 -0
  329. package/lib/debug/stats-panel.d.ts +21 -6
  330. package/lib/debug/stats-panel.js +80 -13
  331. package/lib/errors/codes.d.ts +17 -3
  332. package/lib/errors/codes.js +111 -105
  333. package/lib/errors/fixes.js +18 -18
  334. package/lib/gpu/variants.d.ts +8 -2
  335. package/lib/gpu/variants.js +39 -7
  336. package/lib/gpu/webgl2/programs.js +1 -1
  337. package/lib/gpu/webgpu/pipelines.js +13 -13
  338. package/lib/index.d.ts +1 -1
  339. package/lib/page/capabilities.d.ts +1 -1
  340. package/lib/page/engine.d.ts +14 -5
  341. package/lib/page/engine.js +18 -2
  342. package/lib/page/label-loop.js +9 -4
  343. package/lib/page/stats-switch.js +2 -0
  344. package/lib/page/switches.d.ts +6 -4
  345. package/lib/page/switches.js +20 -4
  346. package/lib/scene/baked-light.d.ts +2 -2
  347. package/lib/scene/fake-call-values.js +2 -2
  348. package/lib/scene/frame-cameras.d.ts +16 -4
  349. package/lib/scene/frame-cameras.js +28 -10
  350. package/lib/scene/gltf.d.ts +1 -1
  351. package/lib/scene/level-rule.d.ts +6 -5
  352. package/lib/scene/level-rule.js +2 -2
  353. package/lib/scene/particles.d.ts +7 -3
  354. package/lib/scene/particles.js +3 -1
  355. package/lib/scene/post.d.ts +3 -2
  356. package/lib/scene/resources.d.ts +15 -9
  357. package/lib/scene/scene.d.ts +18 -2
  358. package/lib/scene/scene.js +47 -5
  359. package/lib/scene/time-of-day.d.ts +10 -5
  360. package/lib/scene/time-of-day.js +11 -7
  361. package/lib/shader-artifact.json +1 -1
  362. package/lib/shared/labels.d.ts +5 -2
  363. package/lib/shared/labels.js +5 -2
  364. package/lib/shared/permutation-words.d.ts +2 -0
  365. package/lib/shared/permutation-words.js +8 -0
  366. package/lib/shared/recording.d.ts +13 -5
  367. package/lib/shared/recording.js +31 -10
  368. package/lib/shared/sketch-data.d.ts +9 -0
  369. package/lib/shared/sketch-data.js +57 -0
  370. package/lib/shared/version.d.ts +1 -1
  371. package/lib/shared/version.js +1 -1
  372. package/lib/sketch/define-sketch.d.ts +16 -7
  373. package/lib/sketch/define-sketch.js +2 -1
  374. package/lib/sketch/runner.d.ts +4 -0
  375. package/lib/sketch/runner.js +5 -0
  376. package/lib/sketch/ui.js +4 -2
  377. package/lib/stats.d.ts +1 -0
  378. package/lib/workers/protocol.d.ts +10 -0
  379. package/lib/workers/render-worker.js +4 -1
  380. package/lib/workers/sketch-worker.js +4 -1
  381. package/package.json +1 -1
@@ -1,16 +1,16 @@
1
1
  ---
2
2
  id: api/impostors
3
3
  title: Impostors (@null3d/impostors)
4
- status: experimental
5
- since: "0.2"
6
4
  summary: "createImpostors: far objects as camera-facing cards that blend the three stored views nearest the camera and light from baked normals; mergeFarGeometry: one mesh per far grid cell."
7
5
  ---
8
6
 
9
7
  # Impostors
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.
9
+ A far object covers few pixels, so its triangles cost work that no one sees. An impostor draws it as one baked card that faces the eye, with the world's up kept upright in its plane.
12
10
 
13
- The demo "Impostors" in `examples/impostors` draws a pine forest three ways by distance. Level-of-detail meshes stand near, one merged mesh stands per grid cell in the middle, and cards baked from the pine stand far out. Press L there to draw the far band as meshes instead, and compare. The near meshes and the cards share the hand-over band.
11
+ ![A pine forest on rolling hills at golden hour, backlit by a low sun.](../images/api/impostors/impostors-forest.webp)
12
+
13
+ The Impostors demo: meshes near, merged cells in the middle, cards far out. The hand-overs are made to be invisible.
14
14
 
15
15
  ```mermaid
16
16
  flowchart LR
@@ -21,10 +21,19 @@ flowchart LR
21
21
  copies["Many copies of a mesh,<br/>a far level of detail"] --> merge["mergeFarGeometry:<br/>one mesh per far grid cell"]
22
22
  ```
23
23
 
24
- A far object covers few pixels, so its triangles cost work that no one sees. A level of detail cuts the triangles but keeps one draw per object. An impostor goes further. A bake draws the object from many directions into atlases ahead of time. Far away, the object draws as one card that faces the eye, with the world's up kept upright in its plane. The card blends the three stored views nearest the camera, and the light falls on it from the normals the bake stored, so a card follows the scene's sun. A card's turn about the world's up turns its views and its light by the same angle, so a turned tree hands over to a card whose crown matches it. The light stays on the tree's side as the camera moves: the card faces the eye, but its stored normal does not turn with the camera. Many cards draw in one instance batch.
24
+ A level of detail cuts the triangles but keeps one draw per object. An impostor goes further. A bake draws the object from many directions into atlases ahead of time. Far away, the object draws as one card that faces the eye. The card blends the three stored views nearest the camera, and the light falls on it from the normals the bake stored, so a card follows the scene's sun. Many cards draw in one instance batch. The [impostors demo](https://github.com/null3d-engine/null3d/tree/main/examples/impostors) draws a pine forest three ways by distance, with cards far out. The [Island showcase](https://github.com/null3d-engine/null3d/tree/main/examples/showcase/island) draws its far trees as cards.
25
+
26
+ <details>
27
+ <summary>Technical details: how a card's turn and light follow its mesh</summary>
28
+
29
+ A card's turn about the world's up turns its views and its light by the same angle, so a turned tree hands over to a card whose crown matches it. The light stays on the tree's side as the camera moves: the card faces the eye, but its stored normal does not turn with the camera.
30
+
31
+ </details>
25
32
 
26
33
  `@null3d/impostors` holds the cards and a second tool for far geometry. `mergeFarGeometry` writes many copies of a mesh into the arrays of one mesh. A whole grid cell of a forest or a skyline then draws in one draw.
27
34
 
35
+ The demo "Impostors" in `examples/impostors` draws a pine forest three ways by distance. Level-of-detail meshes stand near, one merged mesh stands per grid cell in the middle, and cards baked from the pine stand far out. Press L there to draw the far band as meshes instead, and compare.
36
+
28
37
  ## Install
29
38
 
30
39
  The package is an add-on. Install it beside the engine, and import it where a sketch needs it.
@@ -35,24 +44,26 @@ bun add @null3d/engine @null3d/impostors
35
44
 
36
45
  ## Create the cards
37
46
 
38
- `createImpostors` makes one instance batch: one quad per position, drawn by the package's material. Each quad faces the eye about the object's middle, with the world's up kept upright in its plane, so a card seen from above holds its mesh's size, and the material blends the three atlas views nearest the direction from the card toward the eye. The cards draw both faces, so a card seen from either side draws: a flat card culled from one side would cast no shadow, because the shadow pass draws casters with their front faces culled. The batch otherwise draws as any batch does, on WebGPU and WebGL2. The card's quad takes `levels: false`: a card is the far stand-in of a mesh, and has no levels of its own.
47
+ `createImpostors` makes one instance batch: one quad per position, drawn by the package's material. Each quad faces the eye about the object's middle, with the world's up kept upright in its plane, so a card seen from above holds its mesh's size, and the material blends the three atlas views nearest the direction from the card toward the eye. The batch draws as any batch does, on WebGPU and WebGL2.
48
+
49
+ <details>
50
+ <summary>Technical details: why the cards draw both faces</summary>
51
+
52
+ The cards draw both faces, so a card seen from either side draws. A flat card culled from one side would cast no shadow, because the shadow pass draws casters with their front faces culled. The card's quad takes `levels: false`: a card is the far stand-in of a mesh, and has no levels of its own.
53
+
54
+ </details>
39
55
 
40
56
  ```ts
41
- import { createImpostors, octahedralAtlas } from '@null3d/impostors';
57
+ import { createImpostors, loadImpostorAtlas } from '@null3d/impostors';
42
58
  import { defineSketch } from '@null3d/engine';
43
59
 
44
60
  export default defineSketch(async ({ scene, materials, geometry, assets }) => {
45
61
  const camera = scene.createPerspectiveCamera({ position: [0, 8, 30], target: [0, 3, 0] });
46
62
  scene.setActiveCamera(camera);
47
63
 
48
- // The atlases hold the object's views. `bakeImpostorAtlas` makes them in the sketch, or
49
- // `bunx @null3d/cli assets impostor` writes them ahead of time.
50
- const atlas = octahedralAtlas(8);
51
- const color = await assets.loadTexture('forest-tree-impostor-color.png', { mipmaps: false });
52
- const surface = await assets.loadTexture('forest-tree-impostor-surface.png', {
53
- colorSpace: 'linear',
54
- mipmaps: false,
55
- });
64
+ // `bunx @null3d/cli assets impostor models/pine.glb --out public/trees`
65
+ // writes the atlas files ahead of time. `bakeImpostorAtlas` makes them in the sketch.
66
+ const pine = await loadImpostorAtlas(assets, 'trees/pine-impostor.json');
56
67
  const positions = new Float32Array(20_000 * 3);
57
68
  const turns = new Float32Array(20_000);
58
69
  for (let i = 0; i < 20_000; i++) {
@@ -61,7 +72,7 @@ export default defineSketch(async ({ scene, materials, geometry, assets }) => {
61
72
  }
62
73
  const trees = createImpostors(
63
74
  { scene, materials, geometry },
64
- { color, surface, atlas, positions, turn: turns, size: 6, alphaMode: 'mask' },
75
+ { ...pine, positions, turn: turns, alphaMode: 'mask' },
65
76
  );
66
77
  });
67
78
  ```
@@ -76,13 +87,35 @@ The `depth` option is the object's depth along a view, in world units: the dista
76
87
 
77
88
  A card's light comes from the scene's lights, as a mesh's does. The surface atlas holds each view's normal, so the sun lights the side of the tree that it would light on the mesh. `roughness` and `metalness` set how the cards light, as the standard material's options do.
78
89
 
79
- The cards cast a shadow only if you ask. Give a batch `castShadows: true` and a layer that the sun draws and no camera does, and the card casts the crown's shape rather than its quad, because the material tests the coverage that the bake stored. A batch with `receiveShadows: true` takes the shadows of other objects at the surface that the stored depth shows, not at its own plane, so a card does not darken itself. A card takes the shadow map's shadows alone: the contact shadows' march takes a flat card for a solid wall, as it takes a leaf card. A card turns toward the view that draws it, so its shadow holds still while the sun does, and the far shadow cascades stay cached. A scene that turns a card of a casting batch from frame to frame moves its caster while the light stands still, and the far cascades then keep a stale shadow: pass `castsStill: false` for such a scene, and its casters draw whenever their maps do.
90
+ ### Cast and receive shadows
91
+
92
+ The cards cast a shadow only if you ask. Give the batch `castShadows: true` and a layer that the sun draws. To keep the caster off the camera's layers, make a second batch from the same options and rows on a layer that only the sun draws, and add that layer to the sun's mask. A caster that no camera draws pays nothing on the camera. When you use a second batch, keep the rows of both batches in step: write and mark the other too.
93
+
94
+ ![Crown-shaped shadows on the ground from a batch of cards that only the light draws.](../images/api/impostors/impostor-shadow.webp)
95
+
96
+ Card shadows from a caster-only batch.
97
+
98
+ In the shadow pass each card turns toward the sun and cuts the crown's shape from its stored coverage. A batch with `receiveShadows: true` takes the shadows of other objects at the surface that the stored depth shows, not at its own plane, so a card does not darken itself. The default `castsStill` holds the caster still while the sun stands still, so the far shadow cascades stay cached. Pass `castsStill: false` when the scene turns cards from frame to frame, and their casters draw whenever their maps do.
99
+
100
+ <details>
101
+ <summary>Technical details: how cards cast and receive shadows</summary>
102
+
103
+ A card takes the shadow map's shadows alone: the contact shadows' march takes a flat card for a solid wall, as it takes a leaf card.
104
+
105
+ </details>
80
106
 
81
107
  ## The atlas and its cells
82
108
 
83
109
  `octahedralAtlas(views)` gives the squarest grid that holds at least `views` views, as `{ columns, rows }`. The cells count from the top left, along each row and then down, as an image's rows do. The rows of the texture run from the bottom up, as `textures.fromData` reads them.
84
110
 
85
- `frameDirection(frame, atlas)` gives the direction a cell's view is drawn from. `impostorFrame(direction, atlas)` gives the cell a direction shows. A bake walks `frameDirection` over every frame. The cards pick their cells as `impostorFrame` does. The two agree on every cell, because they are inverses. `encodeOctahedral` and `decodeOctahedral` fold the directions onto one square. The front half of the sphere fills the square's middle diamond, and the back half fills its four corners. So a grid covers both poles too. The back pole, straight behind, lands on the corner at the bottom right.
111
+ `frameDirection(frame, atlas)` gives the direction a cell's view is drawn from. `impostorFrame(direction, atlas)` gives the cell a direction shows. A bake walks `frameDirection` over every frame. The cards pick their cells as `impostorFrame` does. The two agree on every cell, because they are inverses. `encodeOctahedral` and `decodeOctahedral` fold the directions onto one square.
112
+
113
+ <details>
114
+ <summary>Technical details: how the octahedral fold covers the sphere</summary>
115
+
116
+ The front half of the sphere fills the square's middle diamond, and the back half fills its four corners. So a grid covers both poles too. The back pole, straight behind, lands on the corner at the bottom right.
117
+
118
+ </details>
86
119
 
87
120
  A bake needs the direction of each cell and one view per cell, looking from that direction at the object. `bakeImpostorAtlas` draws them all on the CPU, so every GPU path gets the same two atlases.
88
121
 
@@ -104,7 +137,11 @@ const color = textures.fromData({ ...bake.color, colorSpace: 'srgb', mipmaps: fa
104
137
  const surface = textures.fromData({ ...bake.surface, colorSpace: 'linear', mipmaps: false });
105
138
  ```
106
139
 
107
- The bake is arithmetic over the arrays. It gives the same pixels on every device, and it needs no GPU pass, so a scene's fog and lights never color the picture. It returns the card's `size`: the width that holds the object's widest horizontal reach, and its full height. A card at scale 1 stands on the object's lowest point. Each picture also holds the object from above, so it is taller than that height by the bake's `room`, which `createImpostors` takes as `room`. A picture that you paint yourself fills its card, and takes the default of 1.
140
+ The bake is arithmetic over the arrays. It gives the same pixels on every device, and it needs no GPU pass, so a scene's fog and lights never color the picture. It returns the card's `size`: the width that holds the object's widest horizontal reach, and its full height.
141
+
142
+ A card at scale 1 stands on the object's lowest point. Each view's picture is taller than the card, so that the object fits from every angle, from the side through to straight above. The picture's height is the diagonal of the card's width and the object's height.
143
+
144
+ The bake's `room` is the factor between the two: the picture's height divided by the card's height. Pass it to `createImpostors` as `room`. A picture that you paint to fit its card takes the default, 1.
108
145
 
109
146
  ## Load a baked atlas
110
147
 
@@ -115,13 +152,9 @@ bunx @null3d/cli assets impostor models/pine.glb --out public/trees
115
152
  ```
116
153
 
117
154
  ```ts
118
- import { createImpostors, loadImpostorAtlas } from '@null3d/impostors';
155
+ import { loadImpostorAtlas } from '@null3d/impostors';
119
156
 
120
157
  const pine = await loadImpostorAtlas(assets, 'trees/pine-impostor.json');
121
- const trees = createImpostors(
122
- { scene, materials, geometry },
123
- { ...pine, positions, turn: turns, alphaMode: 'mask' },
124
- );
125
158
  ```
126
159
 
127
160
  The images load from the JSON file's folder, with no mipmaps. The color atlas loads in sRGB, and the surface atlas loads linear. The files hold the same texels as a bake in the sketch, for the same model, grid and texels. The loader throws when the JSON file is not an impostor atlas file. It also throws for a version of the format that the package does not read. Bake the model again with the command of the same version.
@@ -132,13 +165,7 @@ The bake stores each point's normal in the object's own frame. At draw time the
132
165
 
133
166
  ## Make a card read as its mesh
134
167
 
135
- A card that draws a different picture from its mesh reads as another species. The difference shows
136
- most in how the crown lights, not in its outline. The island's trees once baked a card as one unlit
137
- side view, with one outward-and-up normal over each face and no normal bake. Measured against its
138
- mesh at the hand-over, that card was 34 to 79 per cent brighter. Its crown's shading spread was 31
139
- to 44 per cent lower, and its trunk 54 to 185 per cent brighter in red and green. Without the sun
140
- the same card was darker than its mesh. The baked color was sound; the card lit as no surface of the
141
- tree does.
168
+ A card that draws a different picture from its mesh reads as another species at the hand-over. The difference shows most in how the crown lights, not in its outline. A card baked as one side view, with one normal over each face, lights brighter and flatter than its mesh, because it lights as no surface of the tree does. The check below tells you how to measure a card.
142
169
 
143
170
  The bake decides the result:
144
171
 
@@ -148,21 +175,15 @@ The bake decides the result:
148
175
  the light and fixes it to one hour.
149
176
  - Store a normal for every texel, in the object's own frame, in the surface atlas. Without it a card
150
177
  catches the sun everywhere. It reads flat and bright, and its trunk lights like a crown.
151
- - Store enough views that no direction lies far from one. One side view shared by every face shows
152
- the same picture from every side, and cannot light by direction. The octahedral grid's cells sit
153
- widely: a four by four grid holds 53 degrees at worst and five by five holds 42.5. A far crown
154
- needs about five by five cells, and ten by ten hold 23 degrees.
155
- - Choose the texels in a cell against the card's apparent size. A crown that covers about 80 pixels
156
- wants 32 to 64 texels across its cell.
178
+ - Store enough views that no direction lies far from one, and choose the texels in a cell against
179
+ the card's apparent size. [Bake several views, not one side](#bake-several-views-not-one-side)
180
+ gives the numbers.
157
181
  - Take the bake's `size` and its foot. A card at scale 1 stands on the object's lowest point. A card
158
182
  pivoted at its middle sinks into the ground.
159
- - Bake the views with the engine's baker unless the card stays tiny. `bakeImpostorAtlas` draws a
160
- real grid of views and stores a normal for every texel, so a card blends the views nearest the
161
- eye and lights as its mesh does from every direction. A hand-painted side view cannot: it shows
162
- the same picture from every side, and a hand-painted top picture on a horizontal plane reads as a
163
- flat plate at a shallow sight. Keep a hand bake only for a card that never covers more than a few
164
- tens of pixels. The island's showcase cards reached 140 to 215 pixels at their switch, and their
165
- hand bakes read as stacked plates there; the engine's bake is their card now.
183
+ - Bake the views with `bakeImpostorAtlas` unless the card never covers more than a few tens of
184
+ pixels. A hand-painted side view shows the same picture from every side, and a hand-painted top
185
+ picture on a horizontal plane reads as a flat plate at a shallow sight. A card at 140 to 215
186
+ pixels needs the engine's bake.
166
187
  - Check a card beside its mesh before accepting it. Draw both at one pose under one light, and crop
167
188
  each to the same size. Compare the mean color per channel within 10 per cent. Compare the
168
189
  silhouette's area within 10 per cent, and the crown's shading spread within 25 per cent. A card
@@ -173,7 +194,9 @@ The bake decides the result:
173
194
  A card's picture is what the player sees of the object, so it should hold as many views as the
174
195
  card's apparent size needs. `bakeImpostorAtlas` draws the mesh from every cell of an octahedral
175
196
  grid; the cell's square of texels holds the color, and the surface atlas holds each texel's normal
176
- in the object's own frame with its depth and its coverage. `createImpostors` blends the three views
197
+ in the object's own frame with its depth and its coverage.
198
+
199
+ `createImpostors` blends the three views
177
200
  nearest the eye and lights the blend from those normals, so a card turns with the camera and still
178
201
  catches the sun on the side the mesh would.
179
202
 
@@ -186,9 +209,12 @@ Two numbers decide the picture:
186
209
  across wants 32 to 64 texels a cell, and one 215 pixels across wants about 128 to 256. The atlas
187
210
  grows with the square of the texels, and each object takes two atlases.
188
211
 
189
- The island's five plants bake a five by five grid at 128 texels a cell: two 640-square atlases a
190
- plant, 3.3 MB of texture apiece at four bytes a texel, and a tenth to four tenths of a second a
191
- plant in its build.
212
+ <details>
213
+ <summary>Technical details: the Island's card budget</summary>
214
+
215
+ The Island's five plants bake a five by five grid at 128 texels a cell: two 640-square atlases a plant, 3.3 MB of texture apiece at four bytes a texel, and a tenth to four tenths of a second a plant in its build.
216
+
217
+ </details>
192
218
 
193
219
  ```ts
194
220
  import { bakeImpostorAtlas, octahedralAtlas } from '@null3d/impostors';
@@ -223,6 +249,10 @@ const surface = textures.fromData({ ...bake.surface, colorSpace: 'linear', wrap:
223
249
 
224
250
  `handOverToCards(meshes, cards, switchDistance, start)` sets the distance bands of a batch of near meshes and the batch of their cards. Call it once at setup, after you write the cards' sizes.
225
251
 
252
+ ![Three pines in the hand-over band with full crowns.](../images/api/impostors/impostor-handover.webp)
253
+
254
+ Each pine draws as its mesh and its card at once, and no sky shows through the crowns.
255
+
226
256
  ```ts
227
257
  import { handOverToCards } from '@null3d/impostors';
228
258
 
@@ -233,10 +263,17 @@ meshes.markDirty();
233
263
  handOverToCards(meshes, trees.batch, 300);
234
264
  ```
235
265
 
236
- The meshes draw from `start`, 0 by default, up to the switch. A coarser level's batch passes the start of its own band. The engine dithers the last `EDGE_BAND` share of that range out, 8 per cent of it. The cards start earlier, by as much as the largest card's size and both edges need. Their own dithered edge then ends before the mesh's edge begins to thin.
266
+ The meshes draw from `start`, 0 by default, up to the switch. A coarser level's batch passes the start of its own band. The cards start a little before the switch, so the two overlap.
267
+
268
+ <details>
269
+ <summary>Technical details: how the two dither bands avoid gaps</summary>
270
+
271
+ The engine dithers the last `EDGE_BAND` share of that range out, 8 per cent of it. The cards start earlier, by as much as the largest card's size and both edges need. Their own dithered edge then ends before the mesh's edge begins to thin.
237
272
 
238
273
  The mesh and the card are different sources, so their dither phases differ. If both edges fell on the same distances, some pixels would drop out of both, and the sky would show through the crown. With the bands apart, each pixel that a mesh's dither drops shows its card. A tree moves from its mesh to its card with one dither.
239
274
 
275
+ </details>
276
+
240
277
  To draw every tree as its mesh again, call `trees.batch.setActiveCount(0)` and `meshes.setDrawRange(0, 1e9)`. The demo's L key does this.
241
278
 
242
279
  ## The surface atlas
@@ -249,7 +286,12 @@ The demo's bake in `examples/impostors/forest.ts` shows the whole loop.
249
286
 
250
287
  ## Merged far geometry
251
288
 
252
- `mergeFarGeometry(parts)` writes copies of a mesh's arrays into the arrays of one mesh, each copy moved by its own transform. It returns `MeshArrays` for `geometry.fromArrays`. Positions and indices are always kept. Attach normals, texture coordinates, colors and tangents when every copy has them. A copy's normals turn by the inverse transpose of its transform, so a stretched copy keeps its shading. Its tangents turn with it, so its normal map lights from the right side. A mirrored copy, such as one scaled by -1 along an axis, gets its triangles' winding reversed and its tangents' sign flipped. So its front faces stay outward.
289
+ `mergeFarGeometry(parts)` writes copies of a mesh's arrays into the arrays of one mesh, each copy moved by its own transform. It returns `MeshArrays` for `geometry.fromArrays`.
290
+
291
+ - Positions and indices are always kept. Attach normals, texture coordinates, colors and tangents when every copy has them.
292
+ - A copy's normals turn by the inverse transpose of its transform, so a stretched copy keeps its shading.
293
+ - Its tangents turn with it, so its normal map lights from the right side.
294
+ - A mirrored copy, such as one scaled by -1 along an axis, gets its triangles' winding reversed and its tangents' sign flipped. So its front faces stay outward.
253
295
 
254
296
  ```ts
255
297
  import { farCellGroups, mergeFarGeometry } from '@null3d/impostors';
package/docs/api/input.md CHANGED
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: api/input
3
3
  title: Input
4
- status: experimental
5
- since: "0.1"
6
4
  summary: "Pointer, keyboard, touch and gamepad; action maps; pointer events on objects."
7
5
  ---
8
6
 
9
7
  # Input
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, key and wheel events<br/>on the page"] --> ring["Input ring<br/>in shared memory"]
@@ -82,7 +78,9 @@ The sketch counts presses and releases per frame. A key can go down and up betwe
82
78
 
83
79
  Use `dragDx` and `dragDy` for drags, such as turning an object with the mouse. A frame can hold the end of a hover and the start of a drag, and `dx` counts both.
84
80
 
85
- A frame's drag belongs to one press. During a slow frame, one drag can end and the next can start before the sketch reads either. The next press and the events after it then wait for the following frame, so each drag keeps its own button and movement. Two quick clicks between two frames also count as two presses, one in each of the next two frames. Wheel scroll that follows a release waits for the following frame in the same way. So a frame's scroll never comes after the end of its drag. Controls that ignore the wheel during a drag still take the scroll after it.
81
+ A frame's drag belongs to one press. During a slow frame, one drag can end and the next can start before the sketch reads either. The next press and the events after it then wait for the following frame, so each drag keeps its own button and movement. Two quick clicks between two frames also count as two presses, one in each of the next two frames.
82
+
83
+ Wheel scroll that follows a release waits for the following frame in the same way. So a frame's scroll never comes after the end of its drag. Controls that ignore the wheel during a drag still take the scroll after it.
86
84
 
87
85
  Browsers send a pinch on a trackpad as wheel scroll with the Control key's flag, while no Control key is down. Unless the page stops it, the browser also zooms the whole page on a pinch. [What the page does](#what-the-page-does) shows how to stop it.
88
86
 
@@ -203,16 +201,23 @@ The page reads the gamepads once per display frame while at least one is connect
203
201
  - In hold mode, the sketch gets no input, so a held frame is the same on every run. See [Testing your sketch](../guides/testing.md).
204
202
  - `createEngine({ input: false })` turns the engine's listening off, for a page whose input another library records, such as a game whose simulation a server replays from a log. [Input off](#input-off) says what changes.
205
203
 
204
+ The [first-person and fly controls demo](https://github.com/null3d-engine/null3d/tree/main/examples/walk-and-fly) turns its view with the pointer lock. The demos' shared runner asks for the lock, not the demo's sketch.
205
+
206
206
  ## Input off
207
207
 
208
- `createEngine({ input: false })` is for a page whose input another library records. The engine then adds no listener that takes input:
208
+ `createEngine({ input: false })` is for a page whose input another library records. The engine then adds no listener that takes input, and the canvas's context menu stays the browser's. The [deterministic simulation demo](https://github.com/null3d-engine/null3d/tree/main/examples/replay) starts this way, and its page takes the keys and the pointer lock.
209
+
210
+ <details>
211
+ <summary>Technical details: the listeners the engine leaves out and keeps</summary>
212
+
213
+ The engine leaves out:
209
214
 
210
215
  - none of its pointer, key, wheel and context menu listeners on the canvas, and no pointer capture;
211
216
  - no `pointerup`, `pointercancel`, `keydown`, `keyup` or `blur` listener on the window;
212
217
  - no `visibilitychange` or `pointerlockchange` listener of its input on the document;
213
218
  - no gamepad listener, and no reading of the gamepads.
214
219
 
215
- The canvas's context menu stays the browser's. The engine keeps the listeners that take no input, and each reads only the page's state:
220
+ It keeps a few listeners that take no input. They only read the page's state:
216
221
 
217
222
  | Listener | Why the engine keeps it |
218
223
  | --- | --- |
@@ -222,14 +227,22 @@ The canvas's context menu stays the browser's. The engine keeps the listeners th
222
227
  | `securitypolicyviolation` on the window | It names what a Content-Security-Policy blocked while the engine loads its files, as [E1418](../errors/E1418.md) and [E1422](../errors/E1422.md) say. |
223
228
  | `message`, `error` and `abort` on the engine's own objects | Its workers, ports and signals talk to the page. |
224
229
 
225
- A development build also listens for the dev server's shader updates. The input test records the engine's listeners in every thread mode.
230
+ A development build also listens for the dev server's shader updates.
231
+
232
+ </details>
226
233
 
227
234
  The sketch's `ctx.input` stays at rest. Every key, button and action is up, `value` gives 0 and `input.touches` is empty. The pointer stays at the canvas's top-left corner, with no movement, wheel or buttons, and objects get no pointer events.
228
235
 
229
236
  - `engine.requestPointerLock()` fails with [E1426](../errors/E1426.md). The lock is part of the engine's input, since the sketch reads the mouse's movement through the engine's listeners. Lock the canvas with the code that records the input, which reads the movement.
230
237
  - Stopping the engine leaves a lock that the page took. It ends only a lock that the engine took or that its input read.
231
238
  - A development build warns in the console when the sketch relies on input. It warns once for each kind: a read of `ctx.input`, an object's pointer event handler, and `@null3d/controls`. Each warning names `input: false`. A production build carries none of them.
232
- - The warning about the controls comes as they load in the sketch's thread. A thread loads a module once, so it misses controls that another sketch in the same worker loaded first. Two engines that run their sketches on the page's thread both hear a load by either one's sketch.
239
+
240
+ <details>
241
+ <summary>Technical details: when the controls warning comes</summary>
242
+
243
+ The warning about the controls comes as they load in the sketch's thread. A thread loads a module once, so it misses controls that another sketch in the same worker loaded first. Two engines that run their sketches on the page's thread both hear a load by either one's sketch.
244
+
245
+ </details>
233
246
 
234
247
  [Using null3D with a deterministic simulation](../guides/deterministic-simulation.md#input-that-another-library-records) shows the page.
235
248
 
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: api/lights
3
3
  title: Lights
4
- status: experimental
5
- since: "0.1"
6
4
  summary: "Directional, point, spot, hemisphere, ambient, rectangle and tube lights; shadow options."
7
5
  ---
8
6
 
9
7
  # Lights
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. Surfaces show up to four directional lights: the first casts the shadows, and three more add fill and rim light.
12
-
13
9
  A light is a scene object, like a mesh or a camera. It has a position, a rotation, a parent and layers, and `setVisible` and `destroy` work on it. Each kind of light has a class and a create call of its own. The standard material reflects lights, and the unlit material ignores them.
14
10
 
15
11
  ```ts
@@ -47,7 +43,13 @@ Every light takes `color` and `intensity`, except the hemisphere light, which ta
47
43
 
48
44
  ## Where a light points
49
45
 
50
- Directional, spot and rectangle lights send their light along their -Z axis, the way a camera looks. A tube light runs along its -Z axis. The `direction` option and `setDirection` turn that axis to a direction relative to the light's parent. `lookAt` turns it toward a point. A spot, rectangle or tube light's `target` option does the same when you create the light. The default points straight down, (0, -1, 0). Parents turn a light with them, as they turn any object.
46
+ Directional, spot and rectangle lights send their light along their -Z axis, the way a camera looks. A tube light runs along its -Z axis.
47
+
48
+ - The `direction` option and `setDirection` turn that axis to a direction relative to the light's parent.
49
+ - `lookAt` turns it toward a point.
50
+ - A spot, rectangle or tube light's `target` option does the same when you create the light.
51
+ - The default points straight down, (0, -1, 0).
52
+ - Parents turn a light with them, as they turn any object.
51
53
 
52
54
  A directional light's position does not matter: only its direction does. The sky of a hemisphere light lies along its +Y axis, which points straight up until you turn the light. Its position does not matter either.
53
55
 
@@ -57,6 +59,10 @@ A light's scale changes neither its range, its cone nor its size.
57
59
 
58
60
  A spot light can project a picture over its cone, as a stage light throws a gobo pattern, a window throws its frame, or a projector shows an image. The `map` option and `setMap` take an engine texture, or sRGB pixels, four bytes each, top row first, as a rectangle light's `image` does.
59
61
 
62
+ ![A rose window projected on a wall by a spot light, over spheres on plinths.](../images/api/lights/spot-cookies.webp)
63
+
64
+ A spot light with a map. Its shadows cut the projected window.
65
+
60
66
  ```ts
61
67
  import { defineSketch } from '@null3d/engine';
62
68
 
@@ -78,9 +84,16 @@ export default defineSketch(({ scene, materials, geometry }) => {
78
84
  - The picture's top row lies along the world's up. Aim the light first: turning the light turns the picture with it, and rolling the light about its own axis leaves the picture as it is.
79
85
  - The color and intensity tint the picture: a dark part of it gives less light, and a red part gives red light. `setMap(null)` goes back to one color.
80
86
  - The map is sRGB pixels, four bytes each with the top row first, or a texture. The engine copies a texture into the light's picture once, when the texture's texels reach the GPU, so a later change to the texture does not reach the light: give a still texture. A video's texture and a render pass's texture change every frame, and a 3D or cube texture holds more than one image, so each throws E1112.
81
- - The engine keeps the picture's detail up to 512 texels a side, in every mip level, and reduces a larger texture to that. A small gobo therefore costs less memory than a detailed photograph of the same light. A surface far from the light shows the picture blurred and a surface near the light shows it sharp. Up to 32 lights show pictures at once, and rectangle and spot lights share those places. A new picture takes up to a few tens of milliseconds, so set it at setup or now and then.
82
- - A picture of 128 x 128 texels a side takes about 0.2 MB with its mip levels. One of 512 x 512 takes about 2.8 MB. The engine keeps room for four pictures once a scene has one. A scene with one detailed picture therefore takes about 11 MB.
83
- - Spot light maps light every GPU path in the same way: WebGPU, WebGPU's compatibility mode and WebGL2. They add no texture to the lit shaders, because the lights' pictures share a table that every lit surface already reads.
87
+ - The engine keeps one shared table for the lights' pictures. Up to 32 lights show pictures at once, and rectangle and spot lights share those places. A new picture takes up to a few tens of milliseconds, so set it at setup or now and then.
88
+ - The engine keeps each picture's detail up to 512 texels a side, and reduces a larger texture to that. It keeps room for four pictures once a scene has one.
89
+ - Spot light maps light every GPU path in the same way: WebGPU, WebGPU's compatibility mode and WebGL2. They add no texture to the lit shaders, because the pictures share a table that every lit surface already reads.
90
+
91
+ <details>
92
+ <summary>Technical details: picture size and memory</summary>
93
+
94
+ A small gobo therefore costs less memory than a detailed photograph of the same light. A surface far from the light shows the picture blurred and a surface near the light shows it sharp. A picture of 128 x 128 texels a side takes about 0.2 MB with its mip levels. One of 512 x 512 takes about 2.8 MB. A scene with one detailed picture therefore takes about 11 MB.
95
+
96
+ </details>
84
97
 
85
98
  A picture from an image file is one call:
86
99
 
@@ -118,7 +131,9 @@ export default defineSketch(({ scene, post }) => {
118
131
  });
119
132
  ```
120
133
 
121
- A point or spot light ends at its `range`, in meters. It has no default, because the engine finds the lights near each surface by their ranges. Its light fades with distance by `decay`, and the default, 2, is the physical rate. A spot light's `angle` goes from its direction to the edge of its cone, in radians, up to π/2, and the default is π/3. Its `penumbra`, from 0 to 1, is the part of the cone over which the light fades out toward the edge.
134
+ A point or spot light ends at its `range`, in meters. It has no default, because the engine finds the lights near each surface by their ranges. Its light fades with distance by `decay`, and the default, 2, is the physical rate.
135
+
136
+ A spot light's `angle` goes from its direction to the edge of its cone, in radians, up to π/2, and the default is π/3. Its `penumbra`, from 0 to 1, is the part of the cone over which the light fades out toward the edge.
122
137
 
123
138
  Every setter allocates nothing, `setColor` and `setGroundColor` included. So `onUpdate` can call `setIntensity`, `setColor`, `setDirection`, `setRange` and the others in every frame.
124
139
 
@@ -135,7 +150,14 @@ To turn a light off, hide it with `setVisible(false)`, set its intensity to 0, o
135
150
 
136
151
  ## Area lights
137
152
 
138
- A rectangle light glows over its whole area, like a shop window, a screen or a studio's soft box. A tube light glows along a tube, like a neon sign or a fluorescent lamp. A point light gives a shiny surface a small dot of light. An area light gives it the light's whole shape, stretched along the surface as on a wet street, and lights a rough surface softly. The engine shades them with linearly transformed cosines, the method of Heitz, Dupuy, Hill and Neubelt.
153
+ A rectangle light glows over its whole area, like a shop window, a screen or a studio's soft box. A tube light glows along a tube, like a neon sign or a fluorescent lamp. A point light gives a shiny surface a small dot of light. An area light gives it the light's whole shape, stretched along the surface as on a wet street, and lights a rough surface softly. The engine shades them with linearly transformed cosines.
154
+
155
+ <details>
156
+ <summary>Technical details: how area lights are shaded</summary>
157
+
158
+ The method is that of Heitz, Dupuy, Hill and Neubelt.
159
+
160
+ </details>
139
161
 
140
162
  ```ts
141
163
  import { defineSketch } from '@null3d/engine';
@@ -155,14 +177,26 @@ export default defineSketch(({ scene, materials, geometry }) => {
155
177
  - A rectangle light's light leaves the side that its -Z axis points to. Surfaces behind it get none. `width` runs along its X axis and `height` along its Y axis, and both default to 1 meter.
156
178
  - A tube light shines all around. `length` runs along its -Z axis, and defaults to 1 meter. `radius` defaults to 0.025 meters, a thin neon tube. The ends of the tube add no light, which a thin tube does not show.
157
179
  - Area lights need a `range`, as point and spot lights do. The light fades to nothing at the range from the light's middle. Pick the distance where the light no longer matters.
158
- - A rectangle light can show a picture over its area, such as a shop window or a screen. The `image` option and `setImage({ width, height, data })` take sRGB pixels, four bytes each, top row first. The light then varies over its area: a puddle shows the picture, and a rough wall gets its blurred colors. The color and intensity tint the picture. The light gives out its intensity times the picture's average color, in linear light. A mostly dark picture, such as thin neon tubes on black, lights little around it. Keep the intensity at the tubes' brightness, and paint a faint glow around the tubes to light more. The engine keeps the picture's detail up to 512 texels a side, in every mip level. Up to 32 lights show pictures at once, and `setImage(null)` goes back to one color. A new picture allocates and takes up to a few tens of milliseconds, so set it at setup or now and then.
159
180
  - `setSize`, `setRange`, `setDirection` and `setIntensity` take effect in the next frame and allocate nothing.
160
181
  - Area lights cast no shadows. Use a spot light beside one for a shadow.
161
182
  - An area light costs each surface it reaches several times what a point light costs. The quality preset's `areaLights` caps how many one frame shades.
162
- - The engine downloads the area lights' tables, a file of 19 KB, with the first area light. It also downloads the builds of the lit shaders that shade area lights. Area lights give light once both have arrived, and the scene draws as before meanwhile. A scene without area lights downloads neither, and its shaders carry no code for them. `createEngine`'s `preload` takes `'area_lights'` to download the builds before the first frame. A failed download reports [E1431](../errors/E1431.md).
183
+ - The engine downloads the area lights' tables and the builds of the lit shaders that shade area lights with the first area light. Area lights give light once both have arrived, and the scene draws as before meanwhile. `createEngine`'s `preload` takes `'area_lights'` to download the builds before the first frame. A failed download reports [E1431](../errors/E1431.md).
163
184
  - Area lights light every GPU path in the same way: WebGPU, WebGPU's compatibility mode and WebGL2.
164
185
  - Scene passes and reflection passes draw area lights too, with their pictures. The `areaLights` cap applies to each pass on its own.
165
186
 
187
+ A rectangle light can show a picture over its area, such as a shop window or a screen. The `image` option and `setImage({ width, height, data })` take sRGB pixels, four bytes each, top row first. The light then varies over its area: a puddle shows the picture, and a rough wall gets its blurred colors.
188
+
189
+ The color and intensity tint the picture, and the light gives out its intensity times the picture's average color. A mostly dark picture, such as thin neon tubes on black, lights little around it, so paint a faint glow around the tubes to light more. Pictures follow the same limits as [spot light maps](#spot-light-maps).
190
+
191
+ `setImage(null)` goes back to one color. A new picture allocates and takes up to a few tens of milliseconds, so set it at setup or now and then.
192
+
193
+ <details>
194
+ <summary>Technical details: what area lights download</summary>
195
+
196
+ The tables are a file of 19 KB. A scene without area lights downloads neither the tables nor the builds, and its shaders carry no code for them.
197
+
198
+ </details>
199
+
166
200
  ## Hemisphere lights
167
201
 
168
202
  A hemisphere light gives a cheap fill light from the sky and the ground. A surface that faces up gets the sky color, and a surface that faces down gets the ground color. A surface between them gets a blend. The blend follows the angle between the surface's normal and the light's +Y axis. The light is diffuse only: it gives no highlights.
@@ -193,7 +227,9 @@ export default defineSketch(({ scene, time }) => {
193
227
 
194
228
  ## Light from the sky
195
229
 
196
- A directional light gives the sun's own light. The light of the sky around it comes from an environment. [`assets.skyEnvironment()`](assets.md#environments) makes one of the sky that `scene.setBackground({ sky })` draws, and it follows the sky as the sun moves. [`timeOfDay`](scene.md#time-of-day) gives the sun's direction, color and intensity for an hour, and the sky that matches them. At night it gives the moon, and a navy sky lit from the moon's place.
230
+ A directional light gives the sun's own light. The light of the sky around it comes from an environment. [`assets.skyEnvironment()`](assets.md#environments) makes one of the sky that `scene.setBackground({ sky })` draws, and it follows the sky as the sun moves.
231
+
232
+ [`timeOfDay`](scene.md#time-of-day) gives the sun's direction, color and intensity for an hour, and the sky that matches them. At night it gives the moon, and a navy sky lit from the moon's place.
197
233
 
198
234
  ```ts
199
235
  import { defineSketch, timeOfDay } from '@null3d/engine';
@@ -209,7 +245,9 @@ export default defineSketch(async ({ scene, assets }) => {
209
245
 
210
246
  ## Shadows
211
247
 
212
- `castShadows: true` and `setCastShadows(true)` make a directional light cast shadows. The first directional light created casts them, and objects need `castShadows` and `receiveShadows` of their own. The `shadow` option and `setShadow` set the light's cascades, map size, distance and biases, and `intensity`, how much of the shadow it draws, from 0 to 1:
248
+ Only one directional light casts shadows: the first one you created among those that are visible and share a layer with the camera. It casts them when it has `castShadows: true` or after `setCastShadows(true)`.
249
+
250
+ On another directional light the option has no effect until that light becomes the first, for example when you hide or destroy the earlier ones. Objects need `castShadows` and `receiveShadows` of their own. The `shadow` option and `setShadow` set the light's cascades, map size, distance and biases, and `intensity`, how much of the shadow it draws, from 0 to 1:
213
251
 
214
252
  ```ts
215
253
  const sun = scene.createDirectionalLight({
@@ -221,7 +259,9 @@ const sun = scene.createDirectionalLight({
221
259
  sun.setShadow({ bias: 0.02, normalBias: 0.04 });
222
260
  ```
223
261
 
224
- The `distance` default is `'auto'`. The engine follows the farthest surface that receives shadows in the camera's view, rounded up on a ladder. A small scene then gets sharper shadows without naming a distance. Pass a number to fix it, as a port of a scene with its own shadow distance does. See [Shadows](../concepts/shadows.md#settings).
262
+ The `distance` default is `'auto'`. The engine follows the farthest surface that receives shadows in the camera's view, rounded up on a ladder. A small scene then gets sharper shadows without naming a distance.
263
+
264
+ Pass a number to fix it, as a port of a scene with its own shadow distance does. See [Shadows](../concepts/shadows.md#settings).
225
265
 
226
266
  Spot and point lights cast shadows with `castShadows` too. Their `shadow` option and `setShadow` take the biases and `intensity` alone:
227
267
 
@@ -249,7 +289,12 @@ sun.setShadow({ hero: car });
249
289
  sun.setHeroShadow(null);
250
290
  ```
251
291
 
252
- The object must cast shadows. Its tile comes from the same atlas as the spot and point lights' shadows, and takes its last layer. Surfaces within the object's box along the light read the tile, and the cascades shadow everything else. The tile size is 1,024, 2,048 or 4,096 texels on each side. Name one with `setHeroShadow(car, 4096)` or the `heroTileSize` option. The quality preset's `heroTileSize` is the default. See [A hero shadow](../concepts/shadows.md#a-hero-shadow).
292
+ The object must cast shadows.
293
+
294
+ - Its tile comes from the spot and point lights' shadow atlas and takes its last layer.
295
+ - Surfaces within its box along the light read the tile, and the cascades shadow everything else.
296
+ - The tile is 1,024, 2,048 or 4,096 texels on each side, set with `setHeroShadow(car, 4096)` or `heroTileSize`, with the quality preset's `heroTileSize` as the default.
297
+ - [A hero shadow](../concepts/shadows.md#a-hero-shadow) explains it.
253
298
 
254
299
  [Shadows](../concepts/shadows.md) explains cascades, contact shadows, hero shadows, the shadow atlas of spot and point lights, each setting and its default, and the biases.
255
300
 
@@ -260,6 +305,12 @@ The object must cast shadows. Its tile comes from the same atlas as the spot and
260
305
  - [Objects and transforms](objects.md): the calls that lights share with every object.
261
306
  - [Materials](materials.md): the standard material, which lights shade, and the unlit material, which they do not.
262
307
  - [Math helpers](math.md#colors): color conversions.
308
+ - [Area lights](https://github.com/null3d-engine/null3d/tree/main/examples/area-lights): rectangle windows and tube-light neon, whose whole shapes show in a wet street.
309
+ - [Spot light maps](https://github.com/null3d-engine/null3d/tree/main/examples/spot-cookies): a rose window and a window frame thrown across a dark hall.
310
+ - [Shadows from a hero map](https://github.com/null3d-engine/null3d/tree/main/examples/hero-shadow): a car with a shadow map fitted tightly to it, beside the cascades.
311
+ - [Contact shadows](https://github.com/null3d-engine/null3d/tree/main/examples/contact-shadows): the dark line where each pebble, blade and stone meets the sand.
312
+ - [Light from the sky](https://github.com/null3d-engine/null3d/tree/main/examples/time-of-day): a day passes over a lighthouse, and its windows light at dusk.
313
+ - [Point light shadows](https://github.com/null3d-engine/null3d/tree/main/examples/particles): a campfire whose point light casts shadows on High and Ultra.
263
314
 
264
315
  ## API reference
265
316