@null3d/engine 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (388) hide show
  1. package/dist/wasm/single/null3d.js +10 -8
  2. package/dist/wasm/single/null3d_bg.wasm +0 -0
  3. package/dist/wasm/threaded/null3d.js +10 -8
  4. package/dist/wasm/threaded/null3d_bg.wasm +0 -0
  5. package/docs/api/animation.md +96 -24
  6. package/docs/api/assets.md +99 -29
  7. package/docs/api/cameras.md +38 -7
  8. package/docs/api/controls.md +47 -23
  9. package/docs/api/debug.md +105 -35
  10. package/docs/api/engine.md +108 -32
  11. package/docs/api/feature-draws.md +21 -15
  12. package/docs/api/feature-effects.md +8 -8
  13. package/docs/api/feature-instances.md +68 -17
  14. package/docs/api/features.md +68 -37
  15. package/docs/api/geometry.md +83 -14
  16. package/docs/api/grass.md +55 -13
  17. package/docs/api/impostors.md +92 -50
  18. package/docs/api/input.md +22 -9
  19. package/docs/api/lights.md +67 -16
  20. package/docs/api/lines.md +38 -13
  21. package/docs/api/materials.md +187 -59
  22. package/docs/api/math.md +6 -6
  23. package/docs/api/objects.md +40 -15
  24. package/docs/api/page.md +58 -7
  25. package/docs/api/particles.md +36 -11
  26. package/docs/api/points.md +18 -8
  27. package/docs/api/post.md +57 -23
  28. package/docs/api/quality.md +36 -16
  29. package/docs/api/raycast.md +57 -24
  30. package/docs/api/reference/animation.md +1 -2
  31. package/docs/api/reference/assets.md +7 -4
  32. package/docs/api/reference/cameras.md +3 -2
  33. package/docs/api/reference/controls.md +59 -2
  34. package/docs/api/reference/debug.md +58 -6
  35. package/docs/api/reference/engine.md +40 -10
  36. package/docs/api/reference/feature-draws.md +3 -4
  37. package/docs/api/reference/feature-effects.md +2 -3
  38. package/docs/api/reference/feature-instances.md +5 -4
  39. package/docs/api/reference/features.md +7 -4
  40. package/docs/api/reference/geometry.md +132 -21
  41. package/docs/api/reference/grass.md +62 -2
  42. package/docs/api/reference/impostors.md +120 -16
  43. package/docs/api/reference/input.md +1 -2
  44. package/docs/api/reference/lights.md +4 -3
  45. package/docs/api/reference/lines.md +3 -4
  46. package/docs/api/reference/materials.md +7 -6
  47. package/docs/api/reference/math.md +13 -2
  48. package/docs/api/reference/objects.md +2 -3
  49. package/docs/api/reference/particles.md +31 -6
  50. package/docs/api/reference/points.md +2 -3
  51. package/docs/api/reference/post.md +4 -3
  52. package/docs/api/reference/quality.md +1 -2
  53. package/docs/api/reference/raycast.md +1 -2
  54. package/docs/api/reference/render.md +4 -3
  55. package/docs/api/reference/scene.md +24 -5
  56. package/docs/api/reference/sketch.md +11 -11
  57. package/docs/api/reference/sound.md +33 -2
  58. package/docs/api/reference/splats.md +42 -3
  59. package/docs/api/reference/sprites.md +2 -3
  60. package/docs/api/reference/tasks.md +16 -3
  61. package/docs/api/reference/terrain.md +26 -5
  62. package/docs/api/reference/textures.md +1 -2
  63. package/docs/api/reference/three-compat.md +18 -7
  64. package/docs/api/reference/time.md +1 -2
  65. package/docs/api/reference/ui.md +10 -7
  66. package/docs/api/reference/vertex-draws.md +2 -3
  67. package/docs/api/reference/water.md +78 -5
  68. package/docs/api/reference/wind.md +15 -2
  69. package/docs/api/render.md +67 -25
  70. package/docs/api/scene.md +129 -36
  71. package/docs/api/sketch.md +45 -19
  72. package/docs/api/sound.md +48 -12
  73. package/docs/api/splats.md +42 -15
  74. package/docs/api/sprites.md +8 -6
  75. package/docs/api/tasks.md +31 -22
  76. package/docs/api/terrain.md +44 -12
  77. package/docs/api/textures.md +61 -12
  78. package/docs/api/three-compat.md +15 -7
  79. package/docs/api/time.md +3 -5
  80. package/docs/api/ui.md +18 -10
  81. package/docs/api/vertex-draws.md +13 -12
  82. package/docs/api/water.md +111 -67
  83. package/docs/api/wind.md +61 -15
  84. package/docs/cli/null3d.md +90 -15
  85. package/docs/concepts/architecture.md +69 -23
  86. package/docs/concepts/assets.md +57 -16
  87. package/docs/concepts/backends.md +68 -24
  88. package/docs/concepts/color-management.md +42 -13
  89. package/docs/concepts/culling.md +68 -47
  90. package/docs/concepts/handles.md +14 -5
  91. package/docs/concepts/instances.md +72 -23
  92. package/docs/concepts/large-worlds.md +17 -7
  93. package/docs/concepts/lighting.md +117 -44
  94. package/docs/concepts/lod.md +128 -33
  95. package/docs/concepts/materials.md +43 -12
  96. package/docs/concepts/particle-buffer.md +48 -35
  97. package/docs/concepts/post-processing.md +77 -14
  98. package/docs/concepts/quality-preset-tables.md +3 -2
  99. package/docs/concepts/quality-presets.md +122 -39
  100. package/docs/concepts/render-graph.md +56 -21
  101. package/docs/concepts/render-layers.md +9 -5
  102. package/docs/concepts/shadows.md +96 -25
  103. package/docs/concepts/static-dynamic.md +22 -7
  104. package/docs/cookbook/index.md +163 -119
  105. package/docs/data/threejs-mapping.json +301 -340
  106. package/docs/errors/E1101.md +23 -5
  107. package/docs/errors/E1102.md +11 -3
  108. package/docs/errors/E1103.md +11 -3
  109. package/docs/errors/E1104.md +11 -3
  110. package/docs/errors/E1105.md +13 -5
  111. package/docs/errors/E1106.md +11 -3
  112. package/docs/errors/E1107.md +15 -5
  113. package/docs/errors/E1108.md +11 -3
  114. package/docs/errors/E1109.md +32 -6
  115. package/docs/errors/E1110.md +23 -5
  116. package/docs/errors/E1111.md +11 -3
  117. package/docs/errors/E1112.md +11 -3
  118. package/docs/errors/E1203.md +12 -4
  119. package/docs/errors/E1204.md +11 -3
  120. package/docs/errors/E1205.md +11 -3
  121. package/docs/errors/E1206.md +28 -6
  122. package/docs/errors/E1207.md +11 -3
  123. package/docs/errors/E1208.md +12 -3
  124. package/docs/errors/E1209.md +11 -3
  125. package/docs/errors/E1213.md +27 -5
  126. package/docs/errors/E1214.md +11 -3
  127. package/docs/errors/E1215.md +31 -6
  128. package/docs/errors/E1216.md +31 -6
  129. package/docs/errors/E1217.md +29 -6
  130. package/docs/errors/E1218.md +22 -6
  131. package/docs/errors/E1219.md +11 -3
  132. package/docs/errors/E1220.md +24 -5
  133. package/docs/errors/E1221.md +24 -5
  134. package/docs/errors/E1222.md +11 -3
  135. package/docs/errors/E1223.md +39 -6
  136. package/docs/errors/E1224.md +11 -3
  137. package/docs/errors/E1225.md +11 -3
  138. package/docs/errors/E1301.md +11 -3
  139. package/docs/errors/E1302.md +11 -3
  140. package/docs/errors/E1303.md +11 -3
  141. package/docs/errors/E1304.md +11 -3
  142. package/docs/errors/E1305.md +13 -3
  143. package/docs/errors/E1306.md +11 -3
  144. package/docs/errors/E1307.md +23 -6
  145. package/docs/errors/E1308.md +31 -0
  146. package/docs/errors/E1401.md +11 -3
  147. package/docs/errors/E1402.md +13 -5
  148. package/docs/errors/E1403.md +11 -3
  149. package/docs/errors/E1404.md +13 -3
  150. package/docs/errors/E1405.md +15 -4
  151. package/docs/errors/E1406.md +31 -5
  152. package/docs/errors/E1407.md +11 -3
  153. package/docs/errors/E1408.md +11 -3
  154. package/docs/errors/E1409.md +11 -3
  155. package/docs/errors/E1410.md +13 -5
  156. package/docs/errors/E1411.md +11 -3
  157. package/docs/errors/E1412.md +29 -6
  158. package/docs/errors/E1413.md +11 -3
  159. package/docs/errors/E1414.md +11 -3
  160. package/docs/errors/E1415.md +11 -3
  161. package/docs/errors/E1416.md +27 -6
  162. package/docs/errors/E1417.md +11 -3
  163. package/docs/errors/E1418.md +12 -3
  164. package/docs/errors/E1419.md +13 -5
  165. package/docs/errors/E1420.md +11 -3
  166. package/docs/errors/E1421.md +11 -3
  167. package/docs/errors/E1422.md +12 -3
  168. package/docs/errors/E1423.md +12 -3
  169. package/docs/errors/E1424.md +24 -5
  170. package/docs/errors/E1425.md +11 -3
  171. package/docs/errors/E1426.md +11 -3
  172. package/docs/errors/E1430.md +11 -3
  173. package/docs/errors/E1431.md +11 -3
  174. package/docs/errors/E1432.md +11 -3
  175. package/docs/errors/E1433.md +11 -3
  176. package/docs/errors/E1434.md +15 -6
  177. package/docs/errors/E1435.md +11 -3
  178. package/docs/errors/E1436.md +11 -3
  179. package/docs/errors/E1437.md +14 -6
  180. package/docs/errors/E1501.md +41 -6
  181. package/docs/errors/E1502.md +13 -3
  182. package/docs/errors/E1503.md +11 -3
  183. package/docs/errors/E1504.md +11 -3
  184. package/docs/errors/E1505.md +23 -5
  185. package/docs/errors/E1506.md +11 -3
  186. package/docs/errors/index.md +53 -29
  187. package/docs/getting-started/cdn.md +15 -9
  188. package/docs/getting-started/first-scene.md +20 -12
  189. package/docs/getting-started/hosting.md +113 -63
  190. package/docs/getting-started/install.md +39 -24
  191. package/docs/getting-started/network-use.md +31 -21
  192. package/docs/getting-started/project-structure.md +20 -8
  193. package/docs/getting-started/third-party-notices.md +6 -10
  194. package/docs/guides/accessibility.md +4 -4
  195. package/docs/guides/add-ons.md +83 -13
  196. package/docs/guides/agents.md +13 -14
  197. package/docs/guides/assets-pipeline.md +294 -72
  198. package/docs/guides/audio.md +17 -15
  199. package/docs/guides/baked-light-data.md +44 -23
  200. package/docs/guides/car-paint.md +37 -31
  201. package/docs/guides/content-pages.md +86 -29
  202. package/docs/guides/custom-passes.md +59 -17
  203. package/docs/guides/custom-shaders.md +54 -37
  204. package/docs/guides/debugging.md +31 -41
  205. package/docs/guides/deploying.md +10 -27
  206. package/docs/guides/deterministic-simulation.md +23 -16
  207. package/docs/guides/first-use-shaders.md +83 -45
  208. package/docs/guides/lens-kit.md +29 -26
  209. package/docs/guides/loading-screens.md +74 -30
  210. package/docs/guides/multiple-views.md +22 -11
  211. package/docs/guides/performance.md +188 -37
  212. package/docs/guides/phones.md +71 -19
  213. package/docs/guides/physics.md +6 -10
  214. package/docs/guides/screens.md +41 -29
  215. package/docs/guides/start-time.md +126 -85
  216. package/docs/guides/testing.md +137 -98
  217. package/docs/guides/ui-overlays.md +17 -12
  218. package/docs/guides/video-textures.md +14 -13
  219. package/docs/images/api/animation/character.webp +0 -0
  220. package/docs/images/api/animation/look-at-ring.webp +0 -0
  221. package/docs/images/api/animation/morph-tulips.webp +0 -0
  222. package/docs/images/api/animation/skeleton-lines.webp +0 -0
  223. package/docs/images/api/controls/gizmo-handles.webp +0 -0
  224. package/docs/images/api/controls/walk-and-fly.webp +0 -0
  225. package/docs/images/api/debug/debug-shapes.webp +0 -0
  226. package/docs/images/api/debug/levels-view.webp +0 -0
  227. package/docs/images/api/engine/transparent-canvas.webp +0 -0
  228. package/docs/images/api/feature-draws/feature-draw-clouds.webp +0 -0
  229. package/docs/images/api/feature-instances/compute-instances.webp +0 -0
  230. package/docs/images/api/feature-instances/instances-shadow.webp +0 -0
  231. package/docs/images/api/geometry/extra-shapes.webp +0 -0
  232. package/docs/images/api/geometry/generators.webp +0 -0
  233. package/docs/images/api/geometry/mesh-arrays.webp +0 -0
  234. package/docs/images/api/geometry/vertex-updates.webp +0 -0
  235. package/docs/images/api/grass/grass-grove.webp +0 -0
  236. package/docs/images/api/grass/grass-presets.webp +0 -0
  237. package/docs/images/api/grass/grass-shadow.webp +0 -0
  238. package/docs/images/api/impostors/impostor-handover.webp +0 -0
  239. package/docs/images/api/impostors/impostor-shadow.webp +0 -0
  240. package/docs/images/api/impostors/impostors-forest.webp +0 -0
  241. package/docs/images/api/lights/spot-cookies.webp +0 -0
  242. package/docs/images/api/lines/lines-lit.webp +0 -0
  243. package/docs/images/api/lines/lines-widths.webp +0 -0
  244. package/docs/images/api/materials/coat-and-sheen.webp +0 -0
  245. package/docs/images/api/materials/decals.webp +0 -0
  246. package/docs/images/api/materials/iridescence-and-anisotropy.webp +0 -0
  247. package/docs/images/api/materials/relief.webp +0 -0
  248. package/docs/images/api/materials/shader-inputs.webp +0 -0
  249. package/docs/images/api/materials/standard-grid.webp +0 -0
  250. package/docs/images/api/materials/subsurface.webp +0 -0
  251. package/docs/images/api/materials/translucency.webp +0 -0
  252. package/docs/images/api/objects/objects-turntable.webp +0 -0
  253. package/docs/images/api/particles/campfire.webp +0 -0
  254. package/docs/images/api/points/galaxy.webp +0 -0
  255. package/docs/images/api/reference/textures/texture-wrap.webp +0 -0
  256. package/docs/images/api/render/fullscreen-glow.webp +0 -0
  257. package/docs/images/api/render/reflection-mirror.webp +0 -0
  258. package/docs/images/api/render/security-camera.webp +0 -0
  259. package/docs/images/api/scene/aurora-sky-layer.webp +0 -0
  260. package/docs/images/api/sound/sound-demo.webp +0 -0
  261. package/docs/images/api/splats/splats-demo.webp +0 -0
  262. package/docs/images/api/sprites/sprites-lines.webp +0 -0
  263. package/docs/images/api/terrain/terrain-demo.webp +0 -0
  264. package/docs/images/api/textures/canvas-screen.webp +0 -0
  265. package/docs/images/api/textures/mipmaps.webp +0 -0
  266. package/docs/images/api/vertex-draws/rain.webp +0 -0
  267. package/docs/images/api/water/ocean.webp +0 -0
  268. package/docs/images/api/water/water-demo.webp +0 -0
  269. package/docs/images/api/water/water-rain.webp +0 -0
  270. package/docs/images/api/water/waterfall.webp +0 -0
  271. package/docs/images/concepts/assets/gltf-model.webp +0 -0
  272. package/docs/images/concepts/color-management/tone-mapped.webp +0 -0
  273. package/docs/images/concepts/culling/far-from-origin.webp +0 -0
  274. package/docs/images/concepts/instances/instances.webp +0 -0
  275. package/docs/images/concepts/instances/per-row-colors.webp +0 -0
  276. package/docs/images/concepts/large-worlds/large-world.webp +0 -0
  277. package/docs/images/concepts/lighting/area-lights.webp +0 -0
  278. package/docs/images/concepts/lighting/atmosphere.webp +0 -0
  279. package/docs/images/concepts/lighting/environment.webp +0 -0
  280. package/docs/images/concepts/lighting/light-shafts.webp +0 -0
  281. package/docs/images/concepts/lighting/reflection-probes.webp +0 -0
  282. package/docs/images/concepts/lod/forest.webp +0 -0
  283. package/docs/images/concepts/materials/weighted-transparency.webp +0 -0
  284. package/docs/images/concepts/post-processing/auto-exposure.webp +0 -0
  285. package/docs/images/concepts/post-processing/bounce-light.webp +0 -0
  286. package/docs/images/concepts/post-processing/depth-of-field.webp +0 -0
  287. package/docs/images/concepts/post-processing/post-effects-alley.webp +0 -0
  288. package/docs/images/concepts/post-processing/sharpen.webp +0 -0
  289. package/docs/images/concepts/post-processing/ssr-wet-street.webp +0 -0
  290. package/docs/images/concepts/render-layers/layers-cottages.webp +0 -0
  291. package/docs/images/concepts/shadows/hero-shadow.webp +0 -0
  292. package/docs/images/cookbook/index/picking.webp +0 -0
  293. package/docs/images/cookbook/index/time-of-day.webp +0 -0
  294. package/docs/images/errors/E1431/area-lights-no-tables.webp +0 -0
  295. package/docs/images/guides/custom-passes/reflection-water.webp +0 -0
  296. package/docs/images/guides/deterministic-simulation/replay-demo.webp +0 -0
  297. package/docs/images/guides/multiple-views/split-screen.webp +0 -0
  298. package/docs/images/guides/performance/overdraw-view.webp +0 -0
  299. package/docs/images/guides/testing/hold-mode-frame.webp +0 -0
  300. package/docs/images/index/showcase-island.webp +0 -0
  301. package/docs/images/porting/threejs-shaders/dissolve.webp +0 -0
  302. package/docs/images/shaders/builtins/builtins.webp +0 -0
  303. package/docs/images/shaders/surface-functions/custom-surface.webp +0 -0
  304. package/docs/images/shaders/surface-functions/vertex-offset.webp +0 -0
  305. package/docs/images/shots.json +2231 -0
  306. package/docs/index.md +14 -23
  307. package/docs/moves.json +1 -0
  308. package/docs/nav.json +300 -0
  309. package/docs/pages.md +230 -212
  310. package/docs/porting/react-three-fiber.md +2 -6
  311. package/docs/porting/threejs-loop-and-threads.md +1 -5
  312. package/docs/porting/threejs-mapping.md +1248 -234
  313. package/docs/porting/threejs-materials.md +10 -5
  314. package/docs/porting/threejs-overview.md +26 -6
  315. package/docs/porting/threejs-postprocessing.md +2 -6
  316. package/docs/porting/threejs-shaders.md +16 -14
  317. package/docs/porting/threejs-unsupported.md +6 -6
  318. package/docs/porting/verification.md +0 -4
  319. package/docs/shaders/builtins.md +15 -4
  320. package/docs/shaders/library.md +9 -6
  321. package/docs/shaders/surface-functions.md +51 -16
  322. package/docs/shaders/wgsl-rules.md +12 -5
  323. package/lib/debug/debug.d.ts +2 -2
  324. package/lib/debug/draw.d.ts +2 -2
  325. package/lib/debug/draw.js +8 -4
  326. package/lib/debug/overlay-look.d.ts +6 -0
  327. package/lib/debug/overlay-look.js +29 -7
  328. package/lib/debug/overlay.js +15 -11
  329. package/lib/debug/stats-options.d.ts +13 -0
  330. package/lib/debug/stats-panel.d.ts +21 -6
  331. package/lib/debug/stats-panel.js +80 -13
  332. package/lib/errors/codes.d.ts +17 -3
  333. package/lib/errors/codes.js +116 -105
  334. package/lib/errors/engine-error.js +1 -1
  335. package/lib/errors/fixes.d.ts +1 -0
  336. package/lib/errors/fixes.js +19 -18
  337. package/lib/generated/shader-permutations.d.ts +6 -0
  338. package/lib/generated/shader-permutations.js +6 -0
  339. package/lib/gpu/variants.d.ts +8 -2
  340. package/lib/gpu/variants.js +39 -7
  341. package/lib/gpu/webgl2/programs.js +1 -1
  342. package/lib/gpu/webgpu/pipelines.js +13 -13
  343. package/lib/index.d.ts +1 -1
  344. package/lib/page/capabilities.d.ts +1 -1
  345. package/lib/page/engine.d.ts +29 -7
  346. package/lib/page/engine.js +79 -7
  347. package/lib/page/label-loop.js +9 -4
  348. package/lib/page/stats-switch.js +2 -0
  349. package/lib/page/switches.d.ts +6 -4
  350. package/lib/page/switches.js +20 -4
  351. package/lib/scene/baked-light.d.ts +2 -2
  352. package/lib/scene/fake-call-values.js +2 -2
  353. package/lib/scene/frame-cameras.d.ts +16 -4
  354. package/lib/scene/frame-cameras.js +28 -10
  355. package/lib/scene/gltf.d.ts +1 -1
  356. package/lib/scene/level-rule.d.ts +6 -5
  357. package/lib/scene/level-rule.js +2 -2
  358. package/lib/scene/particles.d.ts +7 -3
  359. package/lib/scene/particles.js +3 -1
  360. package/lib/scene/post.d.ts +3 -2
  361. package/lib/scene/resources.d.ts +15 -9
  362. package/lib/scene/scene.d.ts +18 -2
  363. package/lib/scene/scene.js +47 -5
  364. package/lib/scene/time-of-day.d.ts +10 -5
  365. package/lib/scene/time-of-day.js +11 -7
  366. package/lib/shader-artifact.json +1 -1
  367. package/lib/shared/labels.d.ts +5 -2
  368. package/lib/shared/labels.js +5 -2
  369. package/lib/shared/permutation-words.d.ts +8 -0
  370. package/lib/shared/permutation-words.js +14 -0
  371. package/lib/shared/recording.d.ts +13 -5
  372. package/lib/shared/recording.js +31 -10
  373. package/lib/shared/sketch-data.d.ts +9 -0
  374. package/lib/shared/sketch-data.js +57 -0
  375. package/lib/shared/tier.d.ts +9 -0
  376. package/lib/shared/tier.js +4 -1
  377. package/lib/shared/version.d.ts +1 -1
  378. package/lib/shared/version.js +1 -1
  379. package/lib/sketch/define-sketch.d.ts +16 -7
  380. package/lib/sketch/define-sketch.js +2 -1
  381. package/lib/sketch/runner.d.ts +4 -0
  382. package/lib/sketch/runner.js +5 -0
  383. package/lib/sketch/ui.js +4 -2
  384. package/lib/stats.d.ts +1 -0
  385. package/lib/workers/protocol.d.ts +10 -0
  386. package/lib/workers/render-worker.js +4 -1
  387. package/lib/workers/sketch-worker.js +4 -1
  388. package/package.json +1 -1
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: guides/custom-passes
3
3
  title: Custom passes and render targets
4
- status: experimental
5
- since: "0.2"
6
4
  summary: "Custom effects and tone curves in WGSL; declaring passes; reading and writing named textures; layer masks; reflections and a water recipe; full-screen passes, a soft outline glow and effects that read textures."
7
5
  ---
8
6
 
9
7
  # Custom passes and render targets
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.
12
-
13
9
  ```mermaid
14
10
  flowchart LR
15
11
  scene["Scene passes:<br/>linear HDR color"] --> e1["Effect 1"]
@@ -22,7 +18,16 @@ flowchart LR
22
18
  final --> canvas["Canvas"]
23
19
  ```
24
20
 
25
- A custom effect is a full-screen pass of your own WGSL. It reads the scene's color, and its depth if it asks for it, and writes a new color for each pixel. Effects run after the scene passes, on linear HDR color, before bloom and the tone curve. A custom tone curve takes the place of the built-in curves in the final pass. A scene pass draws the scene from another camera into a texture, which materials show: see [Render to a texture](#render-to-a-texture). A full-screen pass runs an effect's WGSL into a texture of its own, which materials, effects and other passes read: see [Full-screen passes](#full-screen-passes).
21
+ A custom pass adds work of your own to the frame. This page covers six kinds:
22
+
23
+ | Kind | Reads | Writes | Section |
24
+ | --- | --- | --- | --- |
25
+ | Custom effect | The scene's color, and its depth if it asks | A new color for the screen | [Custom effects](#custom-effects) |
26
+ | Custom tone curve | Linear color after bloom | The final display color | [Custom tone curves](#custom-tone-curves) |
27
+ | Scene pass | The scene, from a camera of its own | A texture that materials show | [Render to a texture](#render-to-a-texture) |
28
+ | Reflection pass | The scene, mirrored across a plane | A texture that materials read | [Reflections](#reflections) |
29
+ | Full-screen pass | An effect's WGSL, and an input image | A texture of its own | [Full-screen passes](#full-screen-passes) |
30
+ | Compute pass | A compute shader | A storage texture | [Compute passes](#compute-passes) |
26
31
 
27
32
  ## Custom effects
28
33
 
@@ -84,7 +89,7 @@ Effects import from the shader library as materials do, such as `#import null3d:
84
89
 
85
90
  ### Uniforms
86
91
 
87
- Declare uniforms as `struct Uniforms`, and read them as `uniforms.name`. The rules are those of custom materials:
92
+ Declare uniforms as `struct Uniforms`, and read them as `uniforms.name`. A custom material reads its own as `material.name`, but the other rules are the same:
88
93
 
89
94
  - The types are `f32`, `i32`, `u32`, `vec2f`, `vec3f` and `vec4f`, in at most 32 numbers. Each `vec3f` and `vec4f` starts a group of four.
90
95
  - A `vec3f` also takes a color string or a hex number, which the engine converts from sRGB to linear.
@@ -213,15 +218,21 @@ For caustics on the bed, find each pixel's point in the world as `eye + line.xyz
213
218
  ### Cost
214
219
 
215
220
  - A full-screen pass reads and writes 8 bytes per pixel of the render size, in `rgba16float`. On a phone at full resolution each pass costs tens of megabytes of memory traffic per frame. One pass at full size costs about 0.05 ms of GPU time on a MacBook Pro, 0.1 ms on an iPad Pro with the A12X, and 0.25 to 0.31 ms on a Pixel 10.
216
- - The engine joins effects to save passes, while the render scale cannot drop below the whole canvas. An effect that reads only its own pixel joins the pass of the effect before it: the pass runs both, one after the other, for each pixel. So you need not join such effects into one function yourself. While the scale can drop, the engine runs each effect in a pass of its own, at every scale, because the final pass then works in two steps ([The post-processing chain](../concepts/post-processing.md#below-the-whole-canvass-render-scale)).
221
+ - The engine joins effects at every render scale. An effect that reads only its own pixel joins the pass of the effect before it: the pass runs both, one after the other, for each pixel. So you need not join such effects into one function yourself.
222
+ - The last pass of effects folds into the final pass only in frames at the whole canvas's scale, with bloom, depth of field and FXAA off. While the scale can drop below the whole canvas, nothing folds, even in a frame at scale 1 ([The post-processing chain](../concepts/post-processing.md#below-the-whole-canvass-render-scale)).
223
+ - Adding or removing an effect changes the frame's passes. A new effect's pipeline builds while the last image stays on screen, as a custom material's does. So a new effect shows a few frames after `addEffect`. To show it at a set moment, add it a little before.
224
+
225
+ <details>
226
+ <summary>Technical details: how the engine joins and folds effects</summary>
227
+
217
228
  - An effect that calls `effectPixel` or `effectColor` reads other pixels of the color before it. So it starts a pass of its own. The effects after it can join it. Depth reads do not stop an effect from joining.
218
- - The last pass of effects folds into the final pass when nothing reads the image between them. That needs bloom and FXAA off, and it needs the render scale to stay at the whole canvas: while the scale can drop below it, nothing folds, also in frames that draw at scale 1. One effect that reads only its own pixel then costs no pass of its own.
219
229
  - A joined pass needs a shader that the engine makes from the effects' WGSL. It builds in the background after the effects' own shaders. Until it is built, each effect draws a pass of its own, so no frame waits for it.
220
230
  - Some browsers cannot compile WebGL2 shaders in the background, such as Chrome on the Android phones tested. On WebGL2 they join only the effects that are there before the first frame. Effects added or reordered later draw a pass each there. A joined shader would hold up a frame while it compiles. Add your effects before the first frame to get the joined passes on those devices. Development builds tell you once in the console when this happens.
221
231
  - Between joined effects the color keeps 32-bit precision instead of the 16 bits of a target. So the image can differ from separate passes in the last bits.
222
232
  - The effects share two targets, whatever their number. They follow the render scale, so a new scale makes no new target.
223
233
  - A depth read costs one texture read per call. The scene's render pass then keeps its depth in memory.
224
- - Adding or removing an effect changes the frame's passes. A new effect's pipeline builds while the last image stays on screen, as a custom material's does. So a new effect shows a few frames after `addEffect`. To show it at a set moment, add it a little before.
234
+
235
+ </details>
225
236
 
226
237
  ### Devices without HDR color
227
238
 
@@ -251,6 +262,8 @@ post.set({ toneMapping: 'aces' });
251
262
  - A curve needs HDR color, as effects do. On a device with no HDR target, the built-in curve stays.
252
263
  - The final pass builds again with the curve, so the first frame with a new curve waits for its pipeline.
253
264
 
265
+ The [Creek showcase](https://github.com/null3d-engine/null3d/tree/main/examples/showcase/creek) sets its own WGSL tone curve in the scene's look.
266
+
254
267
  ## Render to a texture
255
268
 
256
269
  A scene pass draws the scene from a camera of your own into a texture. `textures.fromPass` gives the texture to any material or sprite.
@@ -303,21 +316,43 @@ A pass that need not change every frame draws every few frames. `setPassEvery` g
303
316
  render.setPassEvery(map, 4);
304
317
  ```
305
318
 
306
- The call compiles nothing, so a sketch may change the cadence whenever the quality preset or the frame budget moves, as often as once a frame. A material that samples the pass's texture in a frame that the pass skips shows the last image that it drew: the target is neither culled nor cleared. Pace a pass with the cadence, not with the switch. A pass that has never drawn is out of the plan while it is off, so switching it on compiles the render graph. Once a pass has drawn, its passes stay in the plan while it is off: the switch only skips its recording, and compiles nothing.
319
+ `setPassEvery` compiles nothing, so a sketch may change the cadence as often as once a frame, for example when the quality preset or the frame budget moves. In a frame that the pass skips, a material that samples its texture shows the last image that it drew.
320
+
321
+ `render.setPassEnabled(pass, false)` turns a pass off, and its texture keeps its last image. Use the switch when a feature starts or stops, and the cadence to draw less often. Switching on a pass that has never drawn compiles the render graph. [Switching and removing passes](../api/render.md#switching-and-removing-passes) gives the full rule.
322
+
323
+ <details>
324
+ <summary>Technical details: cadence, the switch and the compiled plan</summary>
325
+
326
+ The engine compiles the render graph into a plan of GPU passes. A skipped frame neither culls nor clears the pass's target. A pass that has never drawn is out of the plan while it is off, so switching it on compiles the graph. Once a pass has drawn, its GPU passes stay in the plan while it is off: the switch only skips their recording and compiles nothing. [Render graph](../concepts/render-graph.md) explains the plan.
327
+
328
+ </details>
307
329
 
308
330
  In [hold mode](testing.md#hold-mode) every pass draws in every frame, whatever its cadence. The engine draws only the hold's last frames, so a pass that skipped one of them would show an image that no drawn frame made: an empty mirror, for one.
309
331
 
310
332
  ### What a scene pass draws
311
333
 
312
- A scene pass draws with the scene's materials, the sun and its shadows where the camera's shadow cascades reach. It draws the ambient and hemisphere lights, the environment's light and the fog. It also draws the point and spot lights that its camera sees. It draws the scene's background behind them, the sky included. With a `clearColor` the texture clears to that color and shows no background, which suits a minimap. A pass draws no ambient occlusion.
334
+ A scene pass draws with the scene's materials, the sun and its shadows where the camera's shadow cascades reach. It draws:
335
+
336
+ - the ambient and hemisphere lights, the environment's light and the fog;
337
+ - the point and spot lights that its camera sees;
338
+ - the scene's background behind them, the sky included.
339
+
340
+ With a `clearColor` the texture clears to that color and shows no background, which suits a minimap. A pass draws no ambient occlusion.
341
+
342
+ A point or spot light casts its shadow in a pass when the camera's view gives it a shadow. The engine gives shadows only to the lights that the camera sees. So a light that only the pass sees lights the pass without a shadow.
343
+
344
+ <details>
345
+ <summary>Technical details: lights and shadows in a scene pass</summary>
313
346
 
314
- A point or spot light casts its shadow in a pass when the camera's view gives it a shadow. The engine gives shadows only to the lights that the camera sees. So a light that only the pass sees lights the pass without a shadow. Reflection passes draw these lights the same way. A pass that sees a point or spot light keeps its own list of lights, as the camera's view does. The list takes under 1 MB of memory. A pass that sees none keeps no list.
347
+ Reflection passes draw these lights the same way. A pass that sees a point or spot light keeps its own list of lights, as the camera's view does. The list takes under 1 MB of memory. A pass that sees none keeps no list.
348
+
349
+ </details>
315
350
 
316
351
  The texture holds linear color after the exposure, so a material that shows it gives the camera the light that the pass saw. On devices that draw 8-bit color, each material tone maps its own color. In compatibility mode with MSAA, the texture holds that color turned back to linear, so the tone curve applies twice and the image looks a little lighter in the middle tones. On WebGL2 devices without float targets, the texture holds display color, which looks brighter and flatter.
317
352
 
318
353
  ## Reflections
319
354
 
320
- A reflection pass draws the camera's view mirrored across a plane into a texture. Everything below the plane is clipped. A custom material reads the texture where its surface shows on the screen, so water and polished floors reflect what stands on them. [Render graph API](../api/render.md#reflection-passes) lists the options, and [Surface functions](../shaders/surface-functions.md#reflections) the WGSL side.
355
+ A reflection pass draws the camera's view mirrored across a plane into a texture. Everything below the plane is clipped. A custom material reads the texture where its surface shows on the screen, so water and polished floors reflect what stands on them. [Render graph API](../api/render.md#reflection-passes) lists the options, and [Surface functions](../shaders/surface-functions.md#reflections) the WGSL side. The [water demo](https://github.com/null3d-engine/null3d/tree/main/examples/water) uses the add-on's reflection for a river and a lake.
321
356
 
322
357
  ```ts
323
358
  const pass = render.addPass({ kind: 'reflection', writes: 'water', plane: { point: [0, 0, 0] } });
@@ -333,7 +368,13 @@ const water = materials.shader({ wgsl: waterWgsl, textures: { mirror: textures.f
333
368
 
334
369
  For water in a scene, use [`@null3d/water`](../api/water.md): it draws a river and a lake from one shape, with the waves, the foam, the soft shore and the reflection that this recipe builds by hand. This recipe shows the pieces that the add-on builds on, for a water surface of your own.
335
370
 
336
- This surface function makes a stream. It has ripples that move, a tint that deepens with the depth, foam at the banks, and the reflection of the banks and the sky. It needs the height of the bed under each point of the water. A terrain made in code gives it with the same function that shapes the terrain. A loaded terrain gives it with a height map texture, which the function samples instead.
371
+ ![Boxes and a ball mirror in rippled water through a reflection pass.](../images/guides/custom-passes/reflection-water.webp)
372
+
373
+ A reflection pass read by a water material.
374
+
375
+ This surface function makes a stream. It has ripples that move, a tint that deepens with the depth, foam at the banks, and the reflection of the banks and the sky.
376
+
377
+ It needs the height of the bed under each point of the water. A terrain made in code gives it with the same function that shapes the terrain. A loaded terrain gives it with a height map texture, which the function samples instead.
337
378
 
338
379
  ```ts
339
380
  const waterWgsl = /* wgsl */ `
@@ -418,8 +459,7 @@ surface.setRotationEuler(-Math.PI / 2, 0, 0);
418
459
  - `ripple` sets how far the waves tilt the normal. The tilt bends the reflection and the sun's highlights.
419
460
  - The foam hides the reflection where it lies, through the reflection's share in `a`.
420
461
  - The water is opaque, so the bed does not show through it. The shallow tint stands in for the bed's color.
421
- - Without a height map of the bed, a blended sheet reads the depth of the opaque scene behind it with `sceneInputs: ['sceneDepth']`. The water's depth along the view ray is the gap between the sheet and the geometry behind the pixel. Convert the pixel's own depth to a distance with the camera's range in `engine_frame.camera_range`, then subtract it from `sceneDistance(uv)`. Fade the water's alpha to zero over the last centimeters of the gap. The water then thins and foams against the bank, so its shore ends softly. Such a sheet takes up to 6 textures of its own, as every custom material does. So this recipe's mirror and a foam map join it. [Materials](../api/materials.md#the-scene-behind-a-surface) shows the options.
422
-
462
+ - Without a height map of the bed, a blended sheet can read the scene's depth with `sceneInputs: ['sceneDepth']` and fade its alpha near the bank for a soft shore. [Materials](../api/materials.md#the-scene-behind-a-surface) shows the WGSL.
423
463
  - Waves that move the surface's vertices need `setBounds`, as [Culling](../concepts/culling.md) explains. The reflection's plane stays flat, which suits small waves.
424
464
 
425
465
  ## Full-screen passes
@@ -504,7 +544,7 @@ post.addEffect({ wgsl: addGlow, textures: { glow: textures.fromPass(glow) } });
504
544
 
505
545
  ## Compute passes
506
546
 
507
- A compute pass runs a compute shader over a texture of its own, on WebGPU. Use one when the work suits a GPU's parallel threads better than a pixel: a wave simulation that combines each pixel with its neighbors, a table that the shader fills in a loop, or a field that several materials read. [Render graph API](../api/render.md#compute-passes) lists its options and the bindings that its shader declares.
547
+ A compute pass runs a compute shader over a texture of its own, on WebGPU. Use one when the work suits a GPU's parallel threads better than a pixel: a wave simulation that combines each pixel with its neighbors, a table that the shader fills in a loop, or a field that several materials read. [Render graph API](../api/render.md#compute-passes) lists its options and the bindings that its shader declares. The [Ocean demo](https://github.com/null3d-engine/null3d/tree/main/examples/ocean) runs compute passes through the water add-on.
508
548
 
509
549
  ```ts
510
550
  const ripples = /* wgsl */ `
@@ -581,6 +621,8 @@ const water = materials.unlit({ map: textures.fromPass(field) });
581
621
  - [Post-processing API](../api/post.md): `post.addEffect`, `post.setEffectUniform`, `post.removeEffect` and `post.set`.
582
622
  - [Render graph API](../api/render.md): `render.addPass`, `render.setPassUniform`, `render.setPassEnabled`, `render.removePass` and `render.dumpGraph`.
583
623
  - [The security camera demo](https://github.com/null3d-engine/null3d/tree/main/examples/security-camera): a scene pass whose texture a monitor shows.
624
+ - [The post effects demo](https://github.com/null3d-engine/null3d/tree/main/examples/post-effects): its effect code lives in `examples/addons/lens-kit/`, which the demo imports.
625
+ - [The custom shader inputs demo](https://github.com/null3d-engine/null3d/tree/main/examples/shader-inputs): a custom material's shader, uniforms and textures.
584
626
  - [The post-processing chain](../concepts/post-processing.md): where effects run, and the built-in effects.
585
627
  - [Custom shaders](custom-shaders.md): the WGSL build, uniforms and the shader library.
586
628
  - [Porting post-processing](../porting/threejs-postprocessing.md): three.js's `ShaderPass` and effect passes.
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: guides/custom-shaders
3
3
  title: Custom shaders
4
- status: experimental
5
- since: "0.1"
6
4
  summary: "WGSL in sketch code; shader errors; surface functions; full shaders; uniforms, textures and typed materials; hot reload."
7
5
  ---
8
6
 
9
7
  # Custom shaders
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. Custom materials with surface functions, vertex offsets, uniforms, textures and full shaders are built, and so is hot reload on the dev server.
12
-
13
9
  ```mermaid
14
10
  flowchart LR
15
11
  file["A .wgsl file<br/>that a module imports"] --> plugin["The null3D Vite plugin<br/>in vite and vite build"]
@@ -19,7 +15,7 @@ flowchart LR
19
15
  plugin -. "a problem" .-> error["An error at the file,<br/>line and column"]
20
16
  ```
21
17
 
22
- You write each null3D shader once, in WGSL. The null3D Vite plugin compiles the WGSL in your code while Vite serves or builds the project. Each shader becomes WGSL for WebGPU and GLSL ES 3.00 for WebGL2, so the page never downloads a shader translator.
18
+ You write each null3D shader once, in WGSL. The null3D Vite plugin compiles the WGSL in your code while Vite serves or builds the project. Each shader becomes WGSL for WebGPU and GLSL ES 3.00 for WebGL2, so the page never downloads a shader translator. The examples on this page tag WGSL with a `/* wgsl */` comment. [WGSL in sketch code](#wgsl-in-sketch-code) gives the two forms and the rules of the tag.
23
19
 
24
20
  ## Custom materials
25
21
 
@@ -40,7 +36,7 @@ fn surface(input: SurfaceInput) -> Surface {
40
36
  const red = materials.shader({ wgsl: rings, color: '#e04040', roughness: 0.5 });
41
37
  ```
42
38
 
43
- The WGSL can declare uniforms as `struct Uniforms`, which `set()` changes at any time. It can declare textures as `var name: texture_2d<f32>;`, or texture arrays whose layers the shader picks as `var name: texture_2d_array<f32>;`, which the `textures` option gives. A vertex offset, `fn vertexOffset`, moves the mesh's vertices. A lobe, `fn lobe`, adds specular beside the base lobe for the main directional light and for each point and spot light. [Surface functions](../shaders/surface-functions.md) describes the surface input, the surface record, `defaultSurface`, uniforms, textures, vertex offsets and lobes. [Built-in shader inputs](../shaders/builtins.md) lists the values that every custom material reads, such as `frame.time`.
39
+ The WGSL can declare uniforms as `struct Uniforms` and read each field as `material.name`, and `set()` changes them at any time. It can declare textures as `var name: texture_2d<f32>;`, or texture arrays whose layers the shader picks as `var name: texture_2d_array<f32>;`, which the `textures` option gives. A vertex offset, `fn vertexOffset`, moves the mesh's vertices. A lobe, `fn lobe`, adds specular beside the base lobe for the main directional light and for each point and spot light. [Surface functions](../shaders/surface-functions.md) describes the surface input, the surface record, `defaultSurface`, uniforms, textures, vertex offsets and lobes. [Built-in shader inputs](../shaders/builtins.md) lists the values that every custom material reads, such as `frame.time`. The [custom shader inputs demo](https://github.com/null3d-engine/null3d/tree/main/examples/shader-inputs) makes 900 crystals from one custom material.
44
40
 
45
41
  ## Full shaders
46
42
 
@@ -87,13 +83,26 @@ const ghost = materials.shader({ wgsl: hologram, doubleSided: true });
87
83
  The plugin takes WGSL as a full shader when its `@vertex` entry point takes an `InstanceIn`. The shader follows these rules:
88
84
 
89
85
  - It has one `@vertex` and one `@fragment` entry point.
90
- - The vertex entry point reads the mesh at the engine's locations. The position is at 0, the normal at 1, the first texture coordinates at 2 and the second at 3. The tangent is at 4, and the color at 5. The joints are at 6, as a `vec4u`, and the weights at 7. A mesh draws only when it has every attribute that the shader reads.
86
+ - The vertex entry point reads the mesh at the engine's locations, in the table below. A mesh draws only when it has every attribute that the shader reads.
91
87
  - Pass the position through `mesh_position`, and texture coordinates through `mesh_uv` and `mesh_second_uv`, from `null3d::vertex`. They give the values that the mesh holds on every GPU path. WebGPU reads plain integer attributes as fractions, and these functions scale them back. Floats and normalized integers pass through unchanged.
92
88
  - It finds its instance with `InstanceIn` and `find_instance` from `null3d::mesh`. On each GPU path, the engine gives each instance's transform in its own way, and these hide the difference.
93
89
  - `null3d::mesh` also gives `clip_position(found, position)`, `relative_position(found, position)` and `world_normal(found, normal)`. Positions are relative to the camera, as in the engine's own shaders.
94
- - The fragment entry point writes its linear color through `finish(color, clip.xy)` from `null3d::mesh`, which prepares it for the engine's output. `finish` multiplies the color by the exposure, as the engine does with an unlit material's color. The engine exposes the frame's lights already. So a shader that adds their light writes that sum through `finish_exposed` instead.
90
+ - The fragment entry point writes its linear color through `finish(color, clip.xy)` from `null3d::mesh`, which prepares it for the engine's output. `finish` multiplies the color by the exposure, as the engine does with an unlit material's color.
91
+ - For color that the engine already exposed, such as the sum of the frame's lights, write it through `finish_exposed` instead.
95
92
  - `fill_builtins(origin)` from `null3d::builtins` fills `frame`, `camera` and `object` in a stage. Pass the object's origin relative to the camera, or zero when the shader does not read `object`.
96
- - The fixed options for faces and depth apply, such as `doubleSided` and `depthBias`. The standard values, uniforms and textures do not reach a full shader. `vertexColors` and the `mask` alpha mode change nothing. The shader reads the colors and discards pixels itself. The build stops at a texture that a full shader declares.
93
+ - The options for faces and depth apply, such as `doubleSided` and `depthBias`.
94
+ - The standard values, uniforms and textures do not reach a full shader. `vertexColors` and the `mask` alpha mode change nothing. The shader reads the colors and discards pixels itself. The build stops at a texture that a full shader declares.
95
+
96
+ | Location | Attribute | WGSL type |
97
+ | --- | --- | --- |
98
+ | 0 | Position | `vec3f` |
99
+ | 1 | Normal | `vec3f` |
100
+ | 2 | First texture coordinates | `vec2f` |
101
+ | 3 | Second texture coordinates | `vec2f` |
102
+ | 4 | Tangent | `vec4f` |
103
+ | 5 | Color | `vec4f` |
104
+ | 6 | Joints | `vec4u` |
105
+ | 7 | Weights | `vec4f` |
97
106
 
98
107
  ## WGSL in sketch code
99
108
 
@@ -111,17 +120,11 @@ The second form is a template literal after a `/* wgsl */` comment:
111
120
  ```ts
112
121
  // sketch.ts
113
122
  const glow = /* wgsl */ `
114
- #import null3d::color
115
-
116
- @vertex
117
- fn vs_main(@builtin(vertex_index) index: u32) -> @builtin(position) vec4f {
118
- let corner = vec2f(f32((index << 1u) & 2u), f32(index & 2u));
119
- return vec4f(corner * 2.0 - 1.0, 0.0, 1.0);
120
- }
121
-
122
- @fragment
123
- fn fs_main() -> @location(0) vec4f {
124
- return vec4f(null3d::color::linear_to_srgb(vec3f(0.5)), 1.0);
123
+ fn surface(input: SurfaceInput) -> Surface {
124
+ var s = defaultSurface(input);
125
+ let pulse = 0.5 + 0.5 * sin(input.uv.x * 12.0);
126
+ s.baseColor = mix(s.baseColor, vec3f(1.0, 0.6, 0.2), pulse);
127
+ return s;
125
128
  }
126
129
  `;
127
130
  ```
@@ -147,7 +150,9 @@ The plugin compiles these kinds of WGSL:
147
150
 
148
151
  WGSL that is neither stops the build with an error that says how to fix it.
149
152
 
150
- The plugin compiles each shader once and caches the result, so a later build compiles only what changed. [Install null3D](../getting-started/install.md#builds) says where the cache lives and how to ignore it.
153
+ A sketch uses a whole shader only as a compute pass's shader ([Compute passes](custom-passes.md#compute-passes)). WGSL with `@vertex` and `@fragment` entry points draws only as a full shader, with an `InstanceIn` vertex input, or as a draw shader, with the vertex and instance indices. Other render pipelines are for the engine's own passes, and no public call takes them.
154
+
155
+ The plugin compiles each shader once and caches the result, so a later build compiles only what changed. [Install null3D](../getting-started/install.md#shader-compiles-and-the-cache) says where the cache lives and how to ignore it.
151
156
 
152
157
  A shader can import the engine's library modules, such as `#import null3d::math`. The plugin resolves each import. [Shader library and imports](../shaders/library.md) lists the modules.
153
158
 
@@ -163,28 +168,33 @@ Each shader builds twice. The WebGPU build is WGSL. The WebGL2 build holds one G
163
168
 
164
169
  Both builds follow the [WGSL rules for portable shaders](../shaders/wgsl-rules.md). The rules page says what the build rejects, and what you must test on each path yourself.
165
170
 
166
- ### Where a custom material's builds go
171
+ You can use arrays in every form that WGSL allows; your WGSL does not change.
172
+
173
+ <details>
174
+ <summary>Technical details: how the WebGL2 build rewrites arrays</summary>
175
+
176
+ A function can return an array, and an array can take values that are not constants, such as `array<vec3f, 2>(a, b)`. Some Android GPUs reject these forms in GLSL, so the WebGL2 build rewrites them. A function that returns an array gives it through an extra `out` parameter, and an array built from values fills one element at a time. The WebGPU build keeps it as you wrote it.
177
+
178
+ </details>
167
179
 
168
- A custom material has many builds: one for each variant of the standard material, on each GPU path. A device draws with a few of them. So the plugin does not put the builds in your module:
180
+ ### Where the builds go
169
181
 
170
- - It puts them in small files of their own, one for each GPU path, each kind of device, and the builds that skin meshes. These are 4 files for WebGPU and 8 for WebGL2, named `material-*.js`. The files of a module hold the builds of all its custom materials, from one `.wgsl` file or from the tagged literals of one script. The text that the builds share is in each file once.
171
- - The material's value in your module holds only its uniforms, its textures, its vertex inputs and a function that imports a file.
172
- - When the sketch first uses the material, it downloads its device's file. The first frame waits for that file and for the material's pipelines, as it waits for any pipeline. A device downloads another file only when it needs one: the builds that skin, for the material's first skinned mesh, or the builds without tone mapping, when bloom or depth of field turns on.
173
- - In `vite build`, the files go into the assets folder with the build's other files. On the dev server, the plugin serves them from memory.
182
+ A custom material, effect or tone curve builds into small files of its own, not your module. The sketch downloads its device's file on first use. The first frame waits for a material's file, and a pass or an effect draws nothing until its file arrives while the rest of the frame draws. Deploy every file that the build writes, or the engine stops with [E1424](../errors/E1424.md).
174
183
 
175
- For example, the battle comparison's sketch has 5 custom materials, and its module takes 5 KB. With every build in it, the module would take 1,504 KB, or 41 KB with Brotli compression. A device downloads a file of 235 to 346 KB, or 18 to 21 KB with Brotli, and one more for its skinned meshes. The thread that draws keeps 3 to 4 MB of shader text. Every build of the device's GPU path would take 7 MB on WebGPU and 27 MB on WebGL2.
184
+ For example, the [battle comparison's sketch](https://github.com/null3d-engine/null3d/tree/main/examples/compare/battle) has 5 custom materials, and its module takes 5 KB. A device downloads one file of about 20 KB after Brotli compression, and one more for its skinned meshes.
176
185
 
177
- ### Where a custom effect's and a tone curve's builds go
186
+ <details>
187
+ <summary>Technical details: the build files and their sizes</summary>
178
188
 
179
- A custom effect and a custom tone curve build into every variant of their template, on each GPU path. The plugin keeps their builds out of your module in the same way as a material's:
189
+ A custom material has many builds: one for each variant of the standard material, on each GPU path. The plugin puts them in files of their own, one for each GPU path, each kind of device, and the builds that skin meshes. These are 4 files for WebGPU and 8 for WebGL2, named `material-*.js`. The files of a module hold the builds of all its custom materials, from one `.wgsl` file or from the tagged literals of one script. The text that the builds share is in each file once.
180
190
 
181
- - An effect or a curve gets one file for each GPU path, named `effect-*.js` or `curve-*.js`. Each holds the text that the builds share once.
182
- - Its value in your module holds its uniforms, its textures and a function that imports a file.
183
- - When the sketch first uses the effect or the curve, it downloads its device's file. A pass or an effect whose pipelines wait for it draws nothing until the file arrives, and the rest of the frame keeps drawing.
191
+ The material's value in your module holds only its uniforms, its textures, its vertex inputs and a function that imports a file. A device downloads another file only when it needs one: the builds that skin, for the material's first skinned mesh, or the builds without tone mapping, when bloom or depth of field turns on. In `vite build`, the files go into the assets folder with the build's other files. On the dev server, the plugin serves them from memory.
184
192
 
185
- Deploy every file that the build writes. When a file that a device needs does not download, the engine stops drawing with [E1424](../errors/E1424.md).
193
+ A custom effect and a custom tone curve build into every variant of their template, on each GPU path. An effect or a curve gets one file for each GPU path, named `effect-*.js` or `curve-*.js`, and its value holds its uniforms, its textures and a function that imports a file.
186
194
 
187
- You can use arrays in every form that WGSL allows. A function can return an array, and an array can take values that are not constants, such as `array<vec3f, 2>(a, b)`. Some Android GPUs reject these forms in GLSL, so the WebGL2 build rewrites them. A function that returns an array gives it through an extra `out` parameter, and an array built from values fills one element at a time. Your WGSL does not change, and the WebGPU build keeps it as you wrote it.
195
+ With every build in it, the battle comparison's module would take 1,504 KB, or 41 KB with Brotli compression. The device's file is 235 to 346 KB, or 18 to 21 KB with Brotli. The thread that draws keeps 3 to 4 MB of shader text. Every build of the device's GPU path would take 7 MB on WebGPU and 27 MB on WebGL2.
196
+
197
+ </details>
188
198
 
189
199
  ## Shader errors
190
200
 
@@ -213,10 +223,17 @@ These rules apply:
213
223
  - A change to other code in the script file runs the module again, so Vite reloads the page as it does for any module.
214
224
  - WGSL that does not compile shows in Vite's overlay and in the terminal. The page keeps drawing with the last shader that compiled. The overlay closes when your fix compiles.
215
225
  - A change to the uniforms, the textures or the vertex attributes of a custom material reloads the page. So does a change between a surface function and a full shader, a vertex offset that you add or remove, and the scene inputs that the WGSL reads. The materials keep their values in a layout that these set, and a vertex offset gives a material the shaders of its shadows.
216
- - A whole shader reloads the page, because your own code draws it.
226
+ - A compute pass's shader reloads the page.
217
227
  - A change to the WGSL of a custom post effect or a custom tone curve reloads the page.
218
228
 
219
- The plugin compiles WGSL on worker threads, so the dev server answers the page's other requests while a shader compiles. It starts one thread for each core but one, at most 8, and each takes about 20 MB. It splits each custom material's variants among the threads.
229
+ The plugin compiles WGSL on worker threads, so the dev server answers the page's other requests while a shader compiles.
230
+
231
+ <details>
232
+ <summary>Technical details: how the plugin compiles on worker threads</summary>
233
+
234
+ It starts one thread for each core but one, at most 8, and each takes about 20 MB. It splits each custom material's variants among the threads.
235
+
236
+ </details>
220
237
 
221
238
  ## TypeScript
222
239
 
@@ -1,14 +1,12 @@
1
1
  ---
2
2
  id: guides/debugging
3
3
  title: Debugging
4
- status: experimental
5
- since: "0.1"
6
4
  summary: "Error codes; the in-page inspector; the MCP server; the render-graph dump; common failures."
7
5
  ---
8
6
 
9
7
  # Debugging
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. The in-page inspector and the MCP server run in development builds only.
9
+ > The in-page inspector and the MCP server run in development builds only.
12
10
 
13
11
  ```mermaid
14
12
  flowchart TD
@@ -19,7 +17,7 @@ flowchart TD
19
17
  hold["Hold mode"] -- "the first error" --> held["window.__null3dHold<br/>with ok: false"]
20
18
  ```
21
19
 
22
- The engine reports a problem in one of four places, as the diagram shows. Each message says what failed and how to fix it.
20
+ The engine reports a problem in one of four places, as the diagram shows. Each message says what failed and how to fix it. Some problems raise no error, such as a canvas that shows only the background. [Common failures](#common-failures) lists them with their fixes.
23
21
 
24
22
  ## Error messages
25
23
 
@@ -94,7 +92,15 @@ console.log(JSON.stringify(engine.report)); // every result of the start's tests
94
92
 
95
93
  Add `engine.report` to a bug report. It holds the result of every test, and the GPU's name where the browser shows one.
96
94
 
97
- When the page draws although a worker was meant to, `engine.mode.renderFallback` says why. `no-surface` means that the browser cannot draw with the GPU path in a worker. `no-answer` means that the engine's test worker did not answer in time, twice, as on a stalled GPU or a very busy machine. `failed-to-start` means that the test worker's script did not load. A development build also logs a warning with the reason. Drawing on the page works, but the page's own code then shares its thread with the drawing. [Page API: createEngine](../api/engine.md#what-the-engine-reports) describes each value.
95
+ When the page draws although a worker was meant to, `engine.mode.renderFallback` says why:
96
+
97
+ | Value | Meaning |
98
+ | --- | --- |
99
+ | `no-surface` | The browser cannot draw with the GPU path in a worker. |
100
+ | `no-answer` | The engine's test worker did not answer in time, twice, as on a stalled GPU or a very busy machine. |
101
+ | `failed-to-start` | The test worker's script did not load. |
102
+
103
+ A development build also logs a warning with the reason. Drawing on the page works, but the page's own code then shares its thread with the drawing ([Page API: createEngine](../api/engine.md#what-the-engine-reports)).
98
104
 
99
105
  Switches in the page's address force a choice, so you can find which path shows a fault. The engine reads them in development builds. A production build reads them only when the Vite plugin's `urlSwitches` option is on:
100
106
 
@@ -121,7 +127,9 @@ debug.arrow(muzzle, aim, 10); // where a shot goes, from
121
127
 
122
128
  Only development builds draw the lines. [Debug drawing and stats](../api/debug.md) lists every call.
123
129
 
124
- To check normals, depth, overdraw, triangle edges, shadows or levels of detail, draw the whole scene with a debug view. In the sketch, `debug.view('wireframe')` draws every triangle's edges, and `debug.view('lit')` draws the materials again. `?view=levels` in the page's address shows a demo's levels with no code change. [Debug views](../api/debug.md#debug-views) lists the views. To see whether shadow edges crawl as the camera moves, draw `'shadows'` from a still camera. Then let `debug.shadowCamera` place the cascades from the moving one ([Watch the shadow cascades from elsewhere](../api/debug.md#watch-the-shadow-cascades-from-elsewhere)).
130
+ To check normals, depth, overdraw, triangle edges, shadows or levels of detail, draw the whole scene with a debug view. In the sketch, `debug.view('wireframe')` draws every triangle's edges, and `debug.view('lit')` draws the materials again. `?view=levels` in the page's address shows a demo's levels with no code change. [Debug views](../api/debug.md#debug-views) lists the views.
131
+
132
+ To see whether shadow edges crawl as the camera moves, draw `'shadows'` from a still camera. Then let `debug.shadowCamera` place the cascades from the moving one ([Watch the shadow cascades from elsewhere](../api/debug.md#watch-the-shadow-cascades-from-elsewhere)).
125
133
 
126
134
  ## Reproduce a frame
127
135
 
@@ -135,7 +143,13 @@ It also saves the errors and warnings that the page logged, with their stacks, i
135
143
 
136
144
  ## Measure
137
145
 
138
- The stats overlay shows frame figures over the top-right corner of the canvas. Its header shows the frame rate. A click opens a card with each thread's CPU time and the GPU's time against the target frame rate. The card also shows memory, draw calls, triangles and objects drawn, and what holds the frame rate back. It lists the GPU time of each step of the frame that the GPU timed, such as the opaque pass or bloom. An add-on feature that reports figures gets a row of its own, with its GPU time, its settings, its budget and its memory. A development build also lists the memory of each engine table and allocator under `Memory by system`. Each row gives the bytes that the area uses, reserves and has used at most. Show it from the page with `createEngine({ stats: true })`, `engine.stats(true)` or the `?stats` switch, or from the sketch with `debug.stats(true)`. The call `debug.frameStats()` gives the sketch the engine's figures, for logs and tests. Both work in production builds too. [Debug drawing and stats](../api/debug.md#stats-overlay-and-frame-figures) lists the figures.
146
+ The stats overlay shows frame figures over the top-right corner of the canvas, or over its bottom-right corner with the `corner` option. Its header shows the frame rate. A click opens a card with each thread's CPU time and the GPU's time against the target frame rate. The card also shows memory, draw calls, triangles and objects drawn, and what holds the frame rate back.
147
+
148
+ It lists the GPU time of each step of the frame that the GPU timed, such as the opaque pass or bloom. An add-on feature that reports figures gets a row of its own, with its GPU time, its settings, its budget and its memory.
149
+
150
+ Show it from the page with `createEngine({ stats: true })`, `engine.stats(true)` or the `?stats` switch, or from the sketch with `debug.stats(true)`.
151
+
152
+ The call `debug.frameStats()` gives the sketch the engine's figures, for logs and tests. Both work in production builds too. A development build also breaks memory down by engine system. [Debug drawing and stats](../api/debug.md#stats-overlay-and-frame-figures) lists the figures.
139
153
 
140
154
  `engine.measure(seconds)` measures the running engine from the page: CPU time per frame by thread and phase, GPU time, frame intervals, uploads, draw calls and memory. [Debug drawing and stats](../api/debug.md#frame-measurement) shows an example, and the [performance guide](performance.md) explains the numbers.
141
155
 
@@ -143,7 +157,13 @@ To test a page's handling of a lost GPU, call `engine.simulateGpuLoss()`. The en
143
157
 
144
158
  ## The in-page inspector
145
159
 
146
- `?inspect` opens a panel at the left edge of the page, over the canvas. The panel lists the scene as a tree, with each object's name and kind under its parent. A search box narrows the tree by name or kind. A click selects an object and outlines it in the view. It then shows the object's fields: position, rotation, scale, visibility, the static or dynamic flag and, for a mesh, its base color, roughness, metalness and opacity. The panel also shows the frame figures at its foot, which it reads every second.
160
+ `?inspect` opens a panel at the left edge of the page, over the canvas. The panel lists the scene as a tree, with each object's name and kind under its parent. A search box narrows the tree by name or kind. A click selects an object and outlines it in the view. It then shows the object's fields:
161
+
162
+ - position, rotation and scale;
163
+ - visibility and the static or dynamic flag;
164
+ - for a mesh, its base color, roughness, metalness and opacity.
165
+
166
+ The panel also shows the frame figures at its foot, which it reads every second.
147
167
 
148
168
  The panel runs in a development build only, and its code loads when the page first asks for it. A held page and a replay open no panel and publish no bridge. A change lasts until the page reloads. Live edits are for exploring: write the final values into the sketch and its tests.
149
169
 
@@ -177,39 +197,7 @@ Each call that names an object takes a selector, and each call returns a JSON-sa
177
197
 
178
198
  `bunx @null3d/cli mcp` starts a Model Context Protocol server over stdio. A coding agent lists and changes the scene's objects, captures a frame, and reads the frame figures, the errors and the render graph. It can also switch the quality preset or the GPU path. The page needs a development build, because a production build leaves the inspection channel out.
179
199
 
180
- The server reaches the scene in one of three ways:
181
-
182
- - By default, it starts the project's own Vite dev server and a headless Chrome, as `shot` does. This works on a cloud agent with no window.
183
- - `--connect <endpoint>` attaches to a Chrome that already runs with a debugging port, and drives the tab that already shows the page. The agent then sees exactly what the developer sees. Start Chrome with `--remote-debugging-port=9222`, and open the page from your Vite dev server. Then run `bunx @null3d/cli mcp --connect http://127.0.0.1:9222`. Every development build publishes the bridge, so the page needs no switch. `?inspect` only opens the panel.
184
- - `--url <address>` uses a dev server that already runs, and still starts its own headless browser.
185
-
186
- The other options:
187
-
188
- - `--page <path>` names the page, `/` by default. With `--connect`, the server drives the tab at that path.
189
- - `--size <width>x<height>` sets the window of the server's own headless browser, 1280x720 by default.
190
- - `--gpu <tier>` opens the server's own page on `webgpu`, `compat` or `webgl2`.
191
- - `--timeout <seconds>` sets how long the page may take to publish the inspector, 60 by default.
192
-
193
- A held page and a replay publish no bridge, so the server cannot reach them. [The `mcp` command](../cli/null3d.md#mcp) is the full reference of the command.
194
-
195
- | Tool | What it does |
196
- | --- | --- |
197
- | `list_objects` | Lists the live objects, with a filter |
198
- | `get_object` | Reads one object's transform, flags and material |
199
- | `set_transform` | Changes an object's transform, visibility, static or dynamic flag, or layers |
200
- | `set_material` | Changes a mesh's material values |
201
- | `capture` | Returns a PNG of the current frame |
202
- | `stats` | Reads the frame figures |
203
- | `errors` | Reads the engine errors and the console's errors and warnings |
204
- | `render_graph` | Returns the compiled render graph as DOT text |
205
- | `set_quality` | Switches the preset, or changes quality settings |
206
- | `set_gpu` | Switches the GPU path and reloads the page |
207
-
208
- Register the server in Claude Code with:
209
-
210
- ```sh
211
- claude mcp add null3d -- bunx @null3d/cli mcp
212
- ```
200
+ By default the server starts the project's own Vite dev server and a headless Chrome, which works on a cloud agent with no window. With `--connect <endpoint>` it drives a Chrome that you already run with a debugging port, so the agent sees exactly what you see. A held page and a replay publish no bridge. [The `mcp` command](../cli/null3d.md#mcp) lists every option and tool.
213
201
 
214
202
  Live changes are for exploring. Write the final values into the sketch and the tests, and check them with `bunx @null3d/cli test`.
215
203
 
@@ -234,3 +222,5 @@ Live changes are for exploring. Write the final values into the sketch and the t
234
222
  - [Testing your sketch](testing.md): hold mode, image tests and switches.
235
223
  - [Debug drawing and stats](../api/debug.md): lines over the scene, the stats overlay, and `engine.measure`.
236
224
  - [Performance guide](performance.md): what to do when frames take too long.
225
+ - [The objects demo](https://github.com/null3d-engine/null3d/tree/main/examples/objects): crates on a turntable that step between two parents.
226
+ - [Hold mode](https://github.com/null3d-engine/null3d/tree/main/examples/hold-mode): a frame that every run draws the same, for a test that compares pictures.
@@ -1,15 +1,11 @@
1
1
  ---
2
2
  id: guides/deploying
3
3
  title: Deploying
4
- status: experimental
5
- since: "0.3"
6
4
  summary: "Headers on common hosts; asset caching; size budgets."
7
5
  ---
8
6
 
9
7
  # Deploying
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
  build["bunx vite build<br/>with the null3D plugin"] --> dist["dist/<br/>the page, assets/, notices"]
@@ -58,30 +54,11 @@ Serve the page over HTTPS. WebGPU and shared memory need a secure page. Over pla
58
54
 
59
55
  ## Let browsers keep the files
60
56
 
61
- Vite names the files in `assets/` with a hash of their content, so a file under a given name never changes. Let browsers keep them for a year, and ask browsers to check the HTML page on each visit:
62
-
63
- ```text
64
- /assets/*
65
- Cache-Control: public, max-age=31536000, immutable
66
- /*.html
67
- Cache-Control: no-cache
68
- ```
69
-
70
- That is the `_headers` form for Netlify and Cloudflare Pages. Every engine thread loads its own copy of its scripts, so each copy after the first comes from the cache. Without these headers, each copy waits for a round trip.
71
-
72
- Send each `.wasm` file with `Content-Type: application/wasm`, with no parameter after it. Chrome then keeps the machine code that it compiled from the engine core. A repeat visit skips most of the compile ([Let Chrome keep the compiled core](../getting-started/hosting.md#let-chrome-keep-the-compiled-core)).
73
-
74
- Send no `Vary: Origin` header with the build's own files. A worker's request carries an `Origin` header that the page's request does not, so `Vary: Origin` makes the browser check the file again.
57
+ Vite names the files in `assets/` with a hash of their content, so a file under a given name never changes. Let browsers keep those files for a year, and have them check the HTML page on each visit. Send `.wasm` files with `Content-Type: application/wasm`, so Chrome keeps the compiled core. [Hosting](../getting-started/hosting.md#let-browsers-keep-the-build-files) gives the headers for each host, and its [compiled core](../getting-started/hosting.md#let-chrome-keep-the-compiled-core) section gives the rule.
75
58
 
76
59
  ## Compress with Brotli
77
60
 
78
- Brotli shrinks the engine's files more than gzip does, because the engine's shader text repeats far apart. The engine core's WebAssembly file takes 2.0 MB as it is, about 0.8 MB with gzip and about 0.6 MB with Brotli.
79
-
80
- - Netlify, Cloudflare Pages and Vercel send Brotli to browsers that accept it.
81
- - GitHub Pages sends gzip only.
82
- - With nginx, compress each file in `dist/assets/` once with `brotli -q 11`, the `.wasm` files included. Then send them with the `ngx_brotli` module's `brotli_static on;`.
83
-
84
- Many servers compress only text, and send `.wasm` files as they are. Open the browser's network panel, and compare the core's transferred size with its size ([Send the files with Brotli](../getting-started/hosting.md#send-the-files-with-brotli)).
61
+ Brotli shrinks the engine's files more than gzip does, because the engine's shader text repeats far apart. The engine core's WebAssembly file takes about 0.6 MB with Brotli. Compress every file of the build, `.wasm` included: Netlify, Cloudflare Pages and Vercel do it for you, and nginx needs `ngx_brotli`'s `brotli_static`. [Send the files with Brotli](../getting-started/hosting.md#send-the-files-with-brotli) gives each host's settings.
85
62
 
86
63
  ## Size budgets
87
64
 
@@ -89,11 +66,17 @@ A page's start downloads the engine, then the files of the first view. Keep each
89
66
 
90
67
  | Part | Size after Brotli | How to check it |
91
68
  | --- | --- | --- |
92
- | The engine's JavaScript at the start | About 130 KB | The network panel |
93
- | The engine core's WebAssembly | About 0.6 MB | The network panel |
69
+ | The engine's JavaScript at the start | About 217 KB on WebGL2 and 203 KB on WebGPU, for a pipelined page | The network panel |
70
+ | The engine core: its WebAssembly file and its JavaScript glue | About 627 KB | The network panel |
94
71
  | Your own models and textures | Your choice | `bunx @null3d/cli assets optimize --report report.json` |
95
72
  | An add-on's files after the start | The budgets of the `sizes` gate | `bunx @null3d/cli check-addon` |
96
73
 
74
+ So the engine's start takes about 844 KB after Brotli, before the page's own code and the scene's files. The JavaScript counts the engine's code for the page and its workers and the GPU path's renderer. It also counts the largest shader file that a start can load. [Send the files with Brotli](../getting-started/hosting.md#send-the-files-with-brotli) gives the figures for gzip and for files as they are.
75
+
76
+ The host stores the shader files of both GPU paths, and a page downloads one path's. A build that ships a subset copies no files of the paths it leaves out. That saves most of the shader set's size in the app package, the installer and the host. The start stays the same.
77
+
78
+ A visitor who downloads the whole app also downloads less: an Electron installer, a zip of the site, or the files that a service worker caches. [Ship a subset of the GPU paths](assets-pipeline.md#ship-a-subset-of-the-gpu-paths) gives the measured sizes.
79
+
97
80
  `assets optimize` makes models and their textures smaller, and its report warns for each quality preset whose texture memory a model passes ([The budget report](assets-pipeline.md#the-budget-report)). Phones and tablets start at the Low and Medium presets, which give textures less memory.
98
81
 
99
82
  To shorten the start, begin the first view's downloads at the top of the setup. Draw the first frame before the large models arrive ([A fast start](start-time.md)).