@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.
- package/dist/wasm/single/null3d.js +10 -8
- package/dist/wasm/single/null3d_bg.wasm +0 -0
- package/dist/wasm/threaded/null3d.js +10 -8
- package/dist/wasm/threaded/null3d_bg.wasm +0 -0
- package/docs/api/animation.md +96 -24
- package/docs/api/assets.md +99 -29
- package/docs/api/cameras.md +38 -7
- package/docs/api/controls.md +47 -23
- package/docs/api/debug.md +105 -35
- package/docs/api/engine.md +108 -32
- package/docs/api/feature-draws.md +21 -15
- package/docs/api/feature-effects.md +8 -8
- package/docs/api/feature-instances.md +68 -17
- package/docs/api/features.md +68 -37
- package/docs/api/geometry.md +83 -14
- package/docs/api/grass.md +55 -13
- package/docs/api/impostors.md +92 -50
- package/docs/api/input.md +22 -9
- package/docs/api/lights.md +67 -16
- package/docs/api/lines.md +38 -13
- package/docs/api/materials.md +187 -59
- package/docs/api/math.md +6 -6
- package/docs/api/objects.md +40 -15
- package/docs/api/page.md +58 -7
- package/docs/api/particles.md +36 -11
- package/docs/api/points.md +18 -8
- package/docs/api/post.md +57 -23
- package/docs/api/quality.md +36 -16
- package/docs/api/raycast.md +57 -24
- package/docs/api/reference/animation.md +1 -2
- package/docs/api/reference/assets.md +7 -4
- package/docs/api/reference/cameras.md +3 -2
- package/docs/api/reference/controls.md +59 -2
- package/docs/api/reference/debug.md +58 -6
- package/docs/api/reference/engine.md +39 -10
- package/docs/api/reference/feature-draws.md +3 -4
- package/docs/api/reference/feature-effects.md +2 -3
- package/docs/api/reference/feature-instances.md +5 -4
- package/docs/api/reference/features.md +7 -4
- package/docs/api/reference/geometry.md +132 -21
- package/docs/api/reference/grass.md +62 -2
- package/docs/api/reference/impostors.md +120 -16
- package/docs/api/reference/input.md +1 -2
- package/docs/api/reference/lights.md +4 -3
- package/docs/api/reference/lines.md +3 -4
- package/docs/api/reference/materials.md +7 -6
- package/docs/api/reference/math.md +13 -2
- package/docs/api/reference/objects.md +2 -3
- package/docs/api/reference/particles.md +31 -6
- package/docs/api/reference/points.md +2 -3
- package/docs/api/reference/post.md +4 -3
- package/docs/api/reference/quality.md +1 -2
- package/docs/api/reference/raycast.md +1 -2
- package/docs/api/reference/render.md +4 -3
- package/docs/api/reference/scene.md +24 -5
- package/docs/api/reference/sketch.md +11 -11
- package/docs/api/reference/sound.md +33 -2
- package/docs/api/reference/splats.md +42 -3
- package/docs/api/reference/sprites.md +2 -3
- package/docs/api/reference/tasks.md +16 -3
- package/docs/api/reference/terrain.md +26 -5
- package/docs/api/reference/textures.md +1 -2
- package/docs/api/reference/three-compat.md +18 -7
- package/docs/api/reference/time.md +1 -2
- package/docs/api/reference/ui.md +10 -7
- package/docs/api/reference/vertex-draws.md +2 -3
- package/docs/api/reference/water.md +78 -5
- package/docs/api/reference/wind.md +15 -2
- package/docs/api/render.md +67 -25
- package/docs/api/scene.md +129 -36
- package/docs/api/sketch.md +45 -19
- package/docs/api/sound.md +48 -12
- package/docs/api/splats.md +42 -15
- package/docs/api/sprites.md +8 -6
- package/docs/api/tasks.md +31 -22
- package/docs/api/terrain.md +44 -12
- package/docs/api/textures.md +61 -12
- package/docs/api/three-compat.md +15 -7
- package/docs/api/time.md +3 -5
- package/docs/api/ui.md +18 -10
- package/docs/api/vertex-draws.md +13 -12
- package/docs/api/water.md +111 -67
- package/docs/api/wind.md +61 -15
- package/docs/cli/null3d.md +90 -15
- package/docs/concepts/architecture.md +69 -23
- package/docs/concepts/assets.md +57 -16
- package/docs/concepts/backends.md +65 -25
- package/docs/concepts/color-management.md +42 -13
- package/docs/concepts/culling.md +68 -47
- package/docs/concepts/handles.md +14 -5
- package/docs/concepts/instances.md +72 -23
- package/docs/concepts/large-worlds.md +17 -7
- package/docs/concepts/lighting.md +117 -44
- package/docs/concepts/lod.md +128 -33
- package/docs/concepts/materials.md +43 -12
- package/docs/concepts/particle-buffer.md +48 -35
- package/docs/concepts/post-processing.md +77 -14
- package/docs/concepts/quality-preset-tables.md +3 -2
- package/docs/concepts/quality-presets.md +122 -39
- package/docs/concepts/render-graph.md +56 -21
- package/docs/concepts/render-layers.md +9 -5
- package/docs/concepts/shadows.md +96 -25
- package/docs/concepts/static-dynamic.md +22 -7
- package/docs/cookbook/index.md +163 -119
- package/docs/data/threejs-mapping.json +301 -340
- package/docs/errors/E1101.md +23 -5
- package/docs/errors/E1102.md +11 -3
- package/docs/errors/E1103.md +11 -3
- package/docs/errors/E1104.md +11 -3
- package/docs/errors/E1105.md +13 -5
- package/docs/errors/E1106.md +11 -3
- package/docs/errors/E1107.md +15 -5
- package/docs/errors/E1108.md +11 -3
- package/docs/errors/E1109.md +32 -6
- package/docs/errors/E1110.md +23 -5
- package/docs/errors/E1111.md +11 -3
- package/docs/errors/E1112.md +11 -3
- package/docs/errors/E1203.md +12 -4
- package/docs/errors/E1204.md +11 -3
- package/docs/errors/E1205.md +11 -3
- package/docs/errors/E1206.md +28 -6
- package/docs/errors/E1207.md +11 -3
- package/docs/errors/E1208.md +12 -3
- package/docs/errors/E1209.md +11 -3
- package/docs/errors/E1213.md +27 -5
- package/docs/errors/E1214.md +11 -3
- package/docs/errors/E1215.md +31 -6
- package/docs/errors/E1216.md +31 -6
- package/docs/errors/E1217.md +29 -6
- package/docs/errors/E1218.md +22 -6
- package/docs/errors/E1219.md +11 -3
- package/docs/errors/E1220.md +24 -5
- package/docs/errors/E1221.md +24 -5
- package/docs/errors/E1222.md +11 -3
- package/docs/errors/E1223.md +39 -6
- package/docs/errors/E1224.md +11 -3
- package/docs/errors/E1225.md +11 -3
- package/docs/errors/E1301.md +11 -3
- package/docs/errors/E1302.md +11 -3
- package/docs/errors/E1303.md +11 -3
- package/docs/errors/E1304.md +11 -3
- package/docs/errors/E1305.md +13 -3
- package/docs/errors/E1306.md +11 -3
- package/docs/errors/E1307.md +23 -6
- package/docs/errors/E1401.md +11 -3
- package/docs/errors/E1402.md +13 -5
- package/docs/errors/E1403.md +11 -3
- package/docs/errors/E1404.md +13 -3
- package/docs/errors/E1405.md +15 -4
- package/docs/errors/E1406.md +31 -5
- package/docs/errors/E1407.md +11 -3
- package/docs/errors/E1408.md +11 -3
- package/docs/errors/E1409.md +11 -3
- package/docs/errors/E1410.md +13 -5
- package/docs/errors/E1411.md +11 -3
- package/docs/errors/E1412.md +29 -6
- package/docs/errors/E1413.md +11 -3
- package/docs/errors/E1414.md +11 -3
- package/docs/errors/E1415.md +11 -3
- package/docs/errors/E1416.md +27 -6
- package/docs/errors/E1417.md +11 -3
- package/docs/errors/E1418.md +12 -3
- package/docs/errors/E1419.md +13 -5
- package/docs/errors/E1420.md +11 -3
- package/docs/errors/E1421.md +11 -3
- package/docs/errors/E1422.md +12 -3
- package/docs/errors/E1423.md +12 -3
- package/docs/errors/E1424.md +24 -5
- package/docs/errors/E1425.md +11 -3
- package/docs/errors/E1426.md +11 -3
- package/docs/errors/E1430.md +11 -3
- package/docs/errors/E1431.md +11 -3
- package/docs/errors/E1432.md +11 -3
- package/docs/errors/E1433.md +11 -3
- package/docs/errors/E1434.md +15 -6
- package/docs/errors/E1435.md +11 -3
- package/docs/errors/E1436.md +11 -3
- package/docs/errors/E1437.md +14 -6
- package/docs/errors/E1501.md +41 -6
- package/docs/errors/E1502.md +13 -3
- package/docs/errors/E1503.md +11 -3
- package/docs/errors/E1504.md +11 -3
- package/docs/errors/E1505.md +23 -5
- package/docs/errors/E1506.md +11 -3
- package/docs/errors/index.md +52 -29
- package/docs/getting-started/cdn.md +15 -9
- package/docs/getting-started/first-scene.md +20 -12
- package/docs/getting-started/hosting.md +111 -63
- package/docs/getting-started/install.md +39 -24
- package/docs/getting-started/network-use.md +31 -21
- package/docs/getting-started/project-structure.md +20 -8
- package/docs/getting-started/third-party-notices.md +6 -10
- package/docs/guides/accessibility.md +4 -4
- package/docs/guides/add-ons.md +83 -13
- package/docs/guides/agents.md +2 -6
- package/docs/guides/assets-pipeline.md +248 -72
- package/docs/guides/audio.md +17 -15
- package/docs/guides/baked-light-data.md +44 -23
- package/docs/guides/car-paint.md +37 -31
- package/docs/guides/content-pages.md +53 -29
- package/docs/guides/custom-passes.md +59 -17
- package/docs/guides/custom-shaders.md +54 -37
- package/docs/guides/debugging.md +31 -41
- package/docs/guides/deploying.md +6 -27
- package/docs/guides/deterministic-simulation.md +23 -16
- package/docs/guides/first-use-shaders.md +83 -45
- package/docs/guides/lens-kit.md +29 -26
- package/docs/guides/loading-screens.md +70 -30
- package/docs/guides/multiple-views.md +22 -11
- package/docs/guides/performance.md +188 -37
- package/docs/guides/phones.md +71 -19
- package/docs/guides/physics.md +6 -10
- package/docs/guides/screens.md +41 -29
- package/docs/guides/start-time.md +120 -85
- package/docs/guides/testing.md +137 -98
- package/docs/guides/ui-overlays.md +17 -12
- package/docs/guides/video-textures.md +14 -13
- package/docs/images/api/animation/character.webp +0 -0
- package/docs/images/api/animation/look-at-ring.webp +0 -0
- package/docs/images/api/animation/morph-tulips.webp +0 -0
- package/docs/images/api/animation/skeleton-lines.webp +0 -0
- package/docs/images/api/controls/gizmo-handles.webp +0 -0
- package/docs/images/api/controls/walk-and-fly.webp +0 -0
- package/docs/images/api/debug/debug-shapes.webp +0 -0
- package/docs/images/api/debug/levels-view.webp +0 -0
- package/docs/images/api/engine/transparent-canvas.webp +0 -0
- package/docs/images/api/feature-draws/feature-draw-clouds.webp +0 -0
- package/docs/images/api/feature-instances/compute-instances.webp +0 -0
- package/docs/images/api/feature-instances/instances-shadow.webp +0 -0
- package/docs/images/api/geometry/extra-shapes.webp +0 -0
- package/docs/images/api/geometry/generators.webp +0 -0
- package/docs/images/api/geometry/mesh-arrays.webp +0 -0
- package/docs/images/api/geometry/vertex-updates.webp +0 -0
- package/docs/images/api/grass/grass-grove.webp +0 -0
- package/docs/images/api/grass/grass-presets.webp +0 -0
- package/docs/images/api/grass/grass-shadow.webp +0 -0
- package/docs/images/api/impostors/impostor-handover.webp +0 -0
- package/docs/images/api/impostors/impostor-shadow.webp +0 -0
- package/docs/images/api/impostors/impostors-forest.webp +0 -0
- package/docs/images/api/lights/spot-cookies.webp +0 -0
- package/docs/images/api/lines/lines-lit.webp +0 -0
- package/docs/images/api/lines/lines-widths.webp +0 -0
- package/docs/images/api/materials/coat-and-sheen.webp +0 -0
- package/docs/images/api/materials/decals.webp +0 -0
- package/docs/images/api/materials/iridescence-and-anisotropy.webp +0 -0
- package/docs/images/api/materials/relief.webp +0 -0
- package/docs/images/api/materials/shader-inputs.webp +0 -0
- package/docs/images/api/materials/standard-grid.webp +0 -0
- package/docs/images/api/materials/subsurface.webp +0 -0
- package/docs/images/api/materials/translucency.webp +0 -0
- package/docs/images/api/objects/objects-turntable.webp +0 -0
- package/docs/images/api/particles/campfire.webp +0 -0
- package/docs/images/api/points/galaxy.webp +0 -0
- package/docs/images/api/reference/textures/texture-wrap.webp +0 -0
- package/docs/images/api/render/fullscreen-glow.webp +0 -0
- package/docs/images/api/render/reflection-mirror.webp +0 -0
- package/docs/images/api/render/security-camera.webp +0 -0
- package/docs/images/api/scene/aurora-sky-layer.webp +0 -0
- package/docs/images/api/sound/sound-demo.webp +0 -0
- package/docs/images/api/splats/splats-demo.webp +0 -0
- package/docs/images/api/sprites/sprites-lines.webp +0 -0
- package/docs/images/api/terrain/terrain-demo.webp +0 -0
- package/docs/images/api/textures/canvas-screen.webp +0 -0
- package/docs/images/api/textures/mipmaps.webp +0 -0
- package/docs/images/api/vertex-draws/rain.webp +0 -0
- package/docs/images/api/water/ocean.webp +0 -0
- package/docs/images/api/water/water-demo.webp +0 -0
- package/docs/images/api/water/water-rain.webp +0 -0
- package/docs/images/api/water/waterfall.webp +0 -0
- package/docs/images/concepts/assets/gltf-model.webp +0 -0
- package/docs/images/concepts/color-management/tone-mapped.webp +0 -0
- package/docs/images/concepts/culling/far-from-origin.webp +0 -0
- package/docs/images/concepts/instances/instances.webp +0 -0
- package/docs/images/concepts/instances/per-row-colors.webp +0 -0
- package/docs/images/concepts/large-worlds/large-world.webp +0 -0
- package/docs/images/concepts/lighting/area-lights.webp +0 -0
- package/docs/images/concepts/lighting/atmosphere.webp +0 -0
- package/docs/images/concepts/lighting/environment.webp +0 -0
- package/docs/images/concepts/lighting/light-shafts.webp +0 -0
- package/docs/images/concepts/lighting/reflection-probes.webp +0 -0
- package/docs/images/concepts/lod/forest.webp +0 -0
- package/docs/images/concepts/materials/weighted-transparency.webp +0 -0
- package/docs/images/concepts/post-processing/auto-exposure.webp +0 -0
- package/docs/images/concepts/post-processing/bounce-light.webp +0 -0
- package/docs/images/concepts/post-processing/depth-of-field.webp +0 -0
- package/docs/images/concepts/post-processing/post-effects-alley.webp +0 -0
- package/docs/images/concepts/post-processing/sharpen.webp +0 -0
- package/docs/images/concepts/post-processing/ssr-wet-street.webp +0 -0
- package/docs/images/concepts/render-layers/layers-cottages.webp +0 -0
- package/docs/images/concepts/shadows/hero-shadow.webp +0 -0
- package/docs/images/cookbook/index/picking.webp +0 -0
- package/docs/images/cookbook/index/time-of-day.webp +0 -0
- package/docs/images/errors/E1431/area-lights-no-tables.webp +0 -0
- package/docs/images/guides/custom-passes/reflection-water.webp +0 -0
- package/docs/images/guides/deterministic-simulation/replay-demo.webp +0 -0
- package/docs/images/guides/multiple-views/split-screen.webp +0 -0
- package/docs/images/guides/performance/overdraw-view.webp +0 -0
- package/docs/images/guides/testing/hold-mode-frame.webp +0 -0
- package/docs/images/index/showcase-island.webp +0 -0
- package/docs/images/porting/threejs-shaders/dissolve.webp +0 -0
- package/docs/images/shaders/builtins/builtins.webp +0 -0
- package/docs/images/shaders/surface-functions/custom-surface.webp +0 -0
- package/docs/images/shaders/surface-functions/vertex-offset.webp +0 -0
- package/docs/images/shots.json +2231 -0
- package/docs/index.md +14 -23
- package/docs/moves.json +1 -0
- package/docs/nav.json +300 -0
- package/docs/pages.md +230 -212
- package/docs/porting/react-three-fiber.md +2 -6
- package/docs/porting/threejs-loop-and-threads.md +1 -5
- package/docs/porting/threejs-mapping.md +1248 -234
- package/docs/porting/threejs-materials.md +10 -5
- package/docs/porting/threejs-overview.md +26 -6
- package/docs/porting/threejs-postprocessing.md +2 -6
- package/docs/porting/threejs-shaders.md +16 -14
- package/docs/porting/threejs-unsupported.md +6 -6
- package/docs/porting/verification.md +0 -4
- package/docs/shaders/builtins.md +15 -4
- package/docs/shaders/library.md +9 -6
- package/docs/shaders/surface-functions.md +51 -16
- package/docs/shaders/wgsl-rules.md +12 -5
- package/lib/debug/debug.d.ts +2 -2
- package/lib/debug/draw.d.ts +2 -2
- package/lib/debug/draw.js +8 -4
- package/lib/debug/overlay-look.d.ts +6 -0
- package/lib/debug/overlay-look.js +29 -7
- package/lib/debug/overlay.js +15 -11
- package/lib/debug/stats-options.d.ts +13 -0
- package/lib/debug/stats-panel.d.ts +21 -6
- package/lib/debug/stats-panel.js +80 -13
- package/lib/errors/codes.d.ts +17 -3
- package/lib/errors/codes.js +111 -105
- package/lib/errors/fixes.js +18 -18
- package/lib/gpu/variants.d.ts +8 -2
- package/lib/gpu/variants.js +39 -7
- package/lib/gpu/webgl2/programs.js +1 -1
- package/lib/gpu/webgpu/pipelines.js +13 -13
- package/lib/index.d.ts +1 -1
- package/lib/page/capabilities.d.ts +1 -1
- package/lib/page/engine.d.ts +14 -5
- package/lib/page/engine.js +18 -2
- package/lib/page/label-loop.js +9 -4
- package/lib/page/stats-switch.js +2 -0
- package/lib/page/switches.d.ts +6 -4
- package/lib/page/switches.js +20 -4
- package/lib/scene/baked-light.d.ts +2 -2
- package/lib/scene/fake-call-values.js +2 -2
- package/lib/scene/frame-cameras.d.ts +16 -4
- package/lib/scene/frame-cameras.js +28 -10
- package/lib/scene/gltf.d.ts +1 -1
- package/lib/scene/level-rule.d.ts +6 -5
- package/lib/scene/level-rule.js +2 -2
- package/lib/scene/particles.d.ts +7 -3
- package/lib/scene/particles.js +3 -1
- package/lib/scene/post.d.ts +3 -2
- package/lib/scene/resources.d.ts +15 -9
- package/lib/scene/scene.d.ts +18 -2
- package/lib/scene/scene.js +47 -5
- package/lib/scene/time-of-day.d.ts +10 -5
- package/lib/scene/time-of-day.js +11 -7
- package/lib/shader-artifact.json +1 -1
- package/lib/shared/labels.d.ts +5 -2
- package/lib/shared/labels.js +5 -2
- package/lib/shared/permutation-words.d.ts +2 -0
- package/lib/shared/permutation-words.js +8 -0
- package/lib/shared/recording.d.ts +13 -5
- package/lib/shared/recording.js +31 -10
- package/lib/shared/sketch-data.d.ts +9 -0
- package/lib/shared/sketch-data.js +57 -0
- package/lib/shared/version.d.ts +1 -1
- package/lib/shared/version.js +1 -1
- package/lib/sketch/define-sketch.d.ts +16 -7
- package/lib/sketch/define-sketch.js +2 -1
- package/lib/sketch/runner.d.ts +4 -0
- package/lib/sketch/runner.js +5 -0
- package/lib/sketch/ui.js +4 -2
- package/lib/stats.d.ts +1 -0
- package/lib/workers/protocol.d.ts +10 -0
- package/lib/workers/render-worker.js +4 -1
- package/lib/workers/sketch-worker.js +4 -1
- package/package.json +1 -1
package/docs/api/math.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: api/math
|
|
3
3
|
title: Math helpers
|
|
4
|
-
status: experimental
|
|
5
|
-
since: "0.1"
|
|
6
4
|
summary: "vec3, quat, mat4 and color on plain arrays; math.clamp, lerp, damp and a random generator that hold mode seeds; the maths entry and createMath for a deterministic simulation."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Math helpers
|
|
10
8
|
|
|
11
|
-
> Roadmap step 0.1, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
|
|
12
|
-
|
|
13
9
|
The math helpers work on plain arrays, in the style of the gl-matrix library. A helper that makes a vector, a rotation, a matrix or a color writes it into the array you pass first, `out`. It also returns that array. The helpers allocate nothing, so create your arrays once in the setup and reuse them in every frame.
|
|
14
10
|
|
|
15
11
|
```ts
|
|
@@ -95,7 +91,7 @@ boxes.markDirty();
|
|
|
95
91
|
color.fromHex(rgb, '#ff8800'); // the linear RGB of an sRGB hex color
|
|
96
92
|
```
|
|
97
93
|
|
|
98
|
-
Each row's color multiplies its material's base color and opacity, as [Instances and batching](../concepts/instances.md#per-row-colors) explains. `color.fromHsl` and `color.fromHex` write three numbers, so a row keeps its alpha.
|
|
94
|
+
Each row's color multiplies its material's base color and opacity, as [Instances and batching](../concepts/instances.md#per-row-colors) explains. `color.fromHsl` and `color.fromHex` write three numbers, so a row keeps its alpha. The [sprites and lines demo](https://github.com/null3d-engine/null3d/tree/main/examples/sprites-lines) turns hex colors to linear RGB with `color.fromHex` for its neon helix and its sparks.
|
|
99
95
|
|
|
100
96
|
## Random numbers
|
|
101
97
|
|
|
@@ -106,6 +102,8 @@ Each row's color multiplies its material's base color and opacity, as [Instances
|
|
|
106
102
|
- Hold mode seeds it before the sketch loads, and makes `Math.random` draw from it too. A held frame is then the same on every run. See [Testing your sketch](../guides/testing.md).
|
|
107
103
|
- `math.randFloat`, `math.randInt` and `math.randFloatSpread` draw their numbers from it.
|
|
108
104
|
|
|
105
|
+
The [hold mode demo](https://github.com/null3d-engine/null3d/tree/main/examples/hold-mode) calls `math.seed(2)` and draws the same stone texture on every run.
|
|
106
|
+
|
|
109
107
|
## The maths entry, for a deterministic simulation
|
|
110
108
|
|
|
111
109
|
A simulation that a server replays must give the same bits on every JavaScript engine, and may not use page APIs. It imports its helpers from the maths entry, never from `@null3d/engine`:
|
|
@@ -122,7 +120,9 @@ const exact = createMath({ sin, cos, acos, exp }); // once, at load
|
|
|
122
120
|
const turn = exact.quat.setAxisAngle(quat.create(), [0, 1, 0], 0.5);
|
|
123
121
|
```
|
|
124
122
|
|
|
125
|
-
The entry leaves out the helpers that need a sine, a cosine, an arc cosine or an exponential, which `Math` computes differently on different engines. They are `quat.setAxisAngle`, `fromEuler`, `rotateX`, `rotateY`, `rotateZ` and `slerp`, `vec3.angle` and `math.damp`. The call `createMath({ sin, cos, acos, exp })` builds those eight on the functions that you pass, with the main entry's arithmetic.
|
|
123
|
+
The entry leaves out the helpers that need a sine, a cosine, an arc cosine or an exponential, which `Math` computes differently on different engines. They are `quat.setAxisAngle`, `fromEuler`, `rotateX`, `rotateY`, `rotateZ` and `slerp`, `vec3.angle` and `math.damp`. The call `createMath({ sin, cos, acos, exp })` builds those eight on the functions that you pass, with the main entry's arithmetic.
|
|
124
|
+
|
|
125
|
+
It also leaves out the random helpers, which start from `Math.random`, and `color`. [Using null3D with a deterministic simulation](../guides/deterministic-simulation.md) lists each sub-path's helpers, and says what else stays out of a simulation. The [deterministic simulation demo](https://github.com/null3d-engine/null3d/tree/main/examples/replay) imports from this entry and steps its orb hunt by ticks.
|
|
126
126
|
|
|
127
127
|
## API reference
|
|
128
128
|
|
package/docs/api/objects.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: api/objects
|
|
3
3
|
title: Objects and transforms
|
|
4
|
-
status: experimental
|
|
5
|
-
since: "0.1"
|
|
6
4
|
summary: "Setters and getters; parents; flags; destroy; pointer events."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Objects and transforms
|
|
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
|
Groups, meshes, cameras and lights are objects: nodes in the scene with a position, a rotation, a scale and a parent. Each class extends `Object3D`, so the calls in the first sections of this page work on all of them. Meshes have more calls, which [Mesh calls](#mesh-calls) lists, and [Lights](lights.md) gives the calls of each kind of light.
|
|
14
10
|
|
|
15
11
|
```ts
|
|
@@ -45,11 +41,18 @@ Each object has a position, a rotation and a scale, all relative to its parent.
|
|
|
45
41
|
|
|
46
42
|
- `setPosition(x, y, z)` and `setScale(x, y, z)` set the position and the scale.
|
|
47
43
|
- `setRotation(x, y, z, w)` sets the rotation as a quaternion. `setRotationEuler(x, y, z, order)` sets it from Euler angles. The order names the axes in turn: the default, `'XYZ'`, turns the object about its own X axis, then its Y axis, then its Z axis. The other orders are `'YXZ'`, `'ZXY'`, `'ZYX'`, `'YZX'` and `'XZY'`.
|
|
48
|
-
- `setPose(position, rotation)` sets the position and, when you give it, the rotation, from two arrays that hold them: `[x, y, z]` and the quaternion `[x, y, z, w]`. Give it the arrays that a pose lives in, and no number crosses the call. The
|
|
44
|
+
- `setPose(position, rotation)` sets the position and, when you give it, the rotation, from two arrays that hold them: `[x, y, z]` and the quaternion `[x, y, z, w]`. Give it the arrays that a pose lives in, and no number crosses the call. The controls write a camera's pose this way in every frame.
|
|
49
45
|
- `rotateX(angle)`, `rotateY(angle)` and `rotateZ(angle)` turn the object by an angle about its own X, Y or Z axis.
|
|
50
46
|
- `translate(x, y, z)` moves the object along its own axes. The object's rotation turns the vector, but its scale does not stretch it. So `camera.translate(0, 0, -1)` moves a camera 1 m forward.
|
|
51
47
|
- `lookAt(x, y, z)` turns a mesh or a group so that its +Z axis points at a point. A camera or a light turns its -Z axis there instead. The call assumes that the object's parents are not rotated.
|
|
52
48
|
|
|
49
|
+
<details>
|
|
50
|
+
<summary>Technical details: why setPose takes arrays</summary>
|
|
51
|
+
|
|
52
|
+
The calls above pass each number on its own. A number passed to a call that the browser does not inline becomes an object of its own.
|
|
53
|
+
|
|
54
|
+
</details>
|
|
55
|
+
|
|
53
56
|
Setters write straight into the engine's memory and mark the object as changed. They send no message and allocate nothing, so `onUpdate` can call them for many objects in every frame. In development builds, a setter that gets `NaN` or an infinite number throws E1203.
|
|
54
57
|
|
|
55
58
|
Positions are 32-bit floats by default, which move in steps of 6 cm at 1,000 km from the origin. An engine started with `largeWorld: true` keeps each position that you set exact at any distance. [Large worlds and precision](../concepts/large-worlds.md) explains both.
|
|
@@ -68,6 +71,10 @@ The world getters read the engine's last transform update. A change that you mak
|
|
|
68
71
|
|
|
69
72
|
An object gets a parent from the `parent` option, or later from `setParent(parent)`. `setParent(null)` makes it a root object. `getParent()` returns the parent, or null for a root object.
|
|
70
73
|
|
|
74
|
+

|
|
75
|
+
|
|
76
|
+
The objects demo moves crates between parents; a still picture cannot show it.
|
|
77
|
+
|
|
71
78
|
By default the object keeps its position, rotation and scale relative to the parent. Its place in the world then changes with the new parent. `setParent(parent, { keepWorld: true })` keeps its place, rotation and size in the world instead. The engine gives the object the position, rotation and scale that do that under the new parent.
|
|
72
79
|
|
|
73
80
|
```ts
|
|
@@ -96,7 +103,7 @@ A parent loop puts an object under itself, or under an object below it, so it ca
|
|
|
96
103
|
|
|
97
104
|
These calls change the structure of the scene, so they take effect when the engine processes the frame, after `onUpdate` returns. [Scene](scene.md#when-changes-take-effect) gives the details.
|
|
98
105
|
|
|
99
|
-
In development builds, every call on
|
|
106
|
+
In development builds, every call on a destroyed object throws [E1101](../errors/E1101.md), except `describe` and `off`. A model's copy also allows `find` and `setOutlined`, which skip its destroyed parts. A call that gets a destroyed object as an argument, such as `setParent`, also throws E1101. Once the frame has removed the object, its world getters throw E1101 in every build. In a release build, the other calls on a destroyed object change nothing, and never reach the object that takes its slot ([Handles](../concepts/handles.md#stale-handles)).
|
|
100
107
|
|
|
101
108
|
## Layers
|
|
102
109
|
|
|
@@ -108,19 +115,35 @@ The mask belongs to the object alone, so its children keep their own. Like `setV
|
|
|
108
115
|
|
|
109
116
|
A mesh has these calls besides the ones above. Like the structural calls, they take effect in the next frame.
|
|
110
117
|
|
|
118
|
+
These calls rebuild the draw tables, so call them at setup or behind a loading screen:
|
|
119
|
+
|
|
111
120
|
- `setMaterial(material)` changes the material, and `setMesh(mesh)` changes the shape. The new mesh's bounding sphere replaces any bounds that `setBounds` gave.
|
|
112
121
|
- `setCastShadows(true)` and `setReceiveShadows(true)` make the mesh cast and receive the shadows of every light that casts them. The `castShadows` and `receiveShadows` options of `createMesh` set them at the start. Both are false by default. Unlit materials show no shadows. [Shadows](../concepts/shadows.md) explains them.
|
|
113
|
-
- `shaderId` is the number that a custom material reads as `object.id` for the mesh. Pass it to a uniform to pick out the mesh in a shader, as [Built-in shader inputs](../shaders/builtins.md#object) shows.
|
|
114
122
|
- `setOutlined(true)` draws an outline around the mesh while `post.set({ outline })` turns outlines on. It is false by default. A model's copy from `scene.instantiate` has `setOutlined` too, which outlines each of its meshes. [Outlines](post.md#outlines) gives the outline's settings.
|
|
115
|
-
- `setRenderOrder(order)` sets the order in which blended objects draw, lower first. Objects of one order draw farthest first. The engine orders opaque and masked objects itself, for speed, as the depth test decides what shows. The order costs nothing: the engine sorts blended objects in every frame anyway. [Materials and pipelines](../concepts/materials.md#the-transparent-pass) explains the sort.
|
|
116
|
-
- `setMorphWeight(nameOrIndex, weight)` and `getMorphWeight(nameOrIndex)` set and read a morph target's weight: [Morph targets](animation.md#morph-targets).
|
|
117
123
|
- `setFrustumCulled(false)` makes the engine draw the mesh even when its bounds are out of view.
|
|
118
|
-
- `setBounds(center, radius)
|
|
119
|
-
- `setLevelBias(bias)` raises or lowers the threshold that picks this object's levels of detail, as Godot's `lod_bias
|
|
120
|
-
- `setDrawRange(start, end)` draws the mesh only between those distances from the camera to its nearest point, fading at each edge. [Distance bands](../concepts/lod.md#distance-bands) shows it.
|
|
121
|
-
|
|
124
|
+
- `setBounds(center, radius)` gives the object a bounding sphere of its own, which culling tests instead of the mesh's sphere. Use it when a shader moves vertices outside the mesh's sphere. [Culling](../concepts/culling.md#bounds-that-you-set) explains how the engine culls with your bounds.
|
|
125
|
+
- `setLevelBias(bias)` raises or lowers the threshold that picks this object's levels of detail, as the inverse of Godot's `lod_bias`: a Godot bias of 2 is about a null3D bias of 0.5. A bias above 1 draws coarser levels, and one below 1 keeps the object's detail. The same factor moves the distance at which the object hides. The bias is 1 by default, and does nothing without a mesh that has levels. [Levels of detail](../concepts/lod.md#the-bias-of-one-object) shows it.
|
|
126
|
+
- `setDrawRange(start, end)` draws the mesh only between those distances from the camera to its nearest point, fading at each edge. [Distance bands](../concepts/lod.md#distance-bands) shows it. The [impostors demo](https://github.com/null3d-engine/null3d/tree/main/examples/impostors) calls it on the merged mesh of each grid cell.
|
|
127
|
+
|
|
128
|
+
These calls cost nothing during play:
|
|
129
|
+
|
|
130
|
+
- `shaderId` is the number that a custom material reads as `object.id` for the mesh. Pass it to a uniform to pick out the mesh in a shader, as [Built-in shader inputs](../shaders/builtins.md#object) shows.
|
|
131
|
+
- `setRenderOrder(order)` sets the order in which blended objects draw, lower first. Objects of one order draw farthest first. The engine orders opaque and masked objects itself, for speed, as the depth test decides what shows. [Materials and pipelines](../concepts/materials.md#the-transparent-pass) explains the sort.
|
|
132
|
+
- `setMorphWeight(nameOrIndex, weight)` and `getMorphWeight(nameOrIndex)` set and read a morph target's weight: [Morph targets](animation.md#morph-targets).
|
|
133
|
+
- `setOccluder(true)` makes the mesh block the view on WebGL2 while software occlusion culling runs, which every preset leaves off. On WebGPU, GPU occlusion culling needs no mark where it runs: every opaque mesh may hide what lies behind it. There, `setOccluder(false)` keeps a mesh out, and `setOccluder(true)` lets it back in. The `occluder` option of `createMesh` and `instantiate` sets it at the start, and a change needs no rebuild. [Culling](../concepts/culling.md#gpu-occlusion-culling-on-webgpu) explains both methods.
|
|
134
|
+
|
|
135
|
+
A click that outlines the picked object rebuilds them once, which is fine. The [performance guide](../guides/performance.md#objects-during-play) lists the cost of each call.
|
|
136
|
+
|
|
137
|
+
<details>
|
|
138
|
+
<summary>Technical details: occluders and bounds</summary>
|
|
139
|
+
|
|
140
|
+
On WebGL2, software occlusion culling is off in every preset. Mark large, solid meshes of up to 4,096 triangles, such as buildings. A model that `assets optimize` gave blockers starts with those meshes blocking, and each draws its blocker in place of its mesh. Meshes that blend, use an alpha mask or a custom material, or are skinned never block on WebGL2.
|
|
141
|
+
|
|
142
|
+
The center that `setBounds` takes is relative to the object's origin, and both values are before the object's scale. A group has no sphere of its own, so it needs one wherever a sphere is read, such as a directional light's hero shadow.
|
|
143
|
+
|
|
144
|
+
</details>
|
|
122
145
|
|
|
123
|
-
|
|
146
|
+
The radius that `setBounds` takes must be 0 or more. A negative radius throws [E1108](../errors/E1108.md) in development builds.
|
|
124
147
|
|
|
125
148
|
## Animation
|
|
126
149
|
|
|
@@ -128,7 +151,7 @@ An object that a model with animations created has an animator, and `animator()`
|
|
|
128
151
|
|
|
129
152
|
## Pointer events
|
|
130
153
|
|
|
131
|
-
`on(type, handler)` calls `handler` for each pointer event of `type` on the object: `'click'`, `'pointerdown'`, `'pointerup'`, `'pointermove'`, `'pointerenter'` or `'pointerleave'`. `off(type, handler)` removes the handler. An event on a child goes on to its parents, so a handler on a group hears the events of everything under it.
|
|
154
|
+
`on(type, handler)` calls `handler` for each pointer event of `type` on the object: `'click'`, `'pointerdown'`, `'pointerup'`, `'pointermove'`, `'pointerenter'` or `'pointerleave'`. `off(type, handler)` removes the handler. An event on a child goes on to its parents, so a handler on a group hears the events of everything under it. The `'pointerenter'` handler of the [picking demo](https://github.com/null3d-engine/null3d/tree/main/examples/picking) lights the piece under the pointer.
|
|
132
155
|
|
|
133
156
|
```ts
|
|
134
157
|
door.on('click', () => door.rotateY(Math.PI / 2));
|
|
@@ -149,6 +172,8 @@ The `name` option gives an object a name that error messages show, such as `"Cra
|
|
|
149
172
|
- [Math helpers](math.md): vectors, quaternions and matrices for the setters and getters.
|
|
150
173
|
- [Animation](animation.md): the animator of an animated object.
|
|
151
174
|
- [Input](input.md#pointer-events-on-objects): pointer events on objects.
|
|
175
|
+
- [Camera layer masks](https://github.com/null3d-engine/null3d/tree/main/examples/layers): a camera that changes its layers every 2 seconds.
|
|
176
|
+
- [The hero fir and the B key](https://github.com/null3d-engine/null3d/tree/main/examples/forest): the nearest tree keeps a level-of-detail bias of its own, for comparison.
|
|
152
177
|
|
|
153
178
|
## API reference
|
|
154
179
|
|
package/docs/api/page.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: api/page
|
|
3
3
|
title: Messages between sketch and page
|
|
4
|
-
|
|
5
|
-
since: "0.1"
|
|
6
|
-
summary: "page.post and page.onMessage in the sketch; engine.postToSketch and engine.onSketchMessage on the page."
|
|
4
|
+
summary: "page.post and page.onMessage in the sketch; engine.postToSketch and engine.onSketchMessage on the page; start data that the setup reads as ctx.data."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Messages between sketch and page
|
|
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
|
By default the sketch runs in a worker, and the page runs on the browser's main thread. They share no variables, so they talk through messages. The page keeps the HTML, and the sketch keeps the scene.
|
|
14
10
|
|
|
15
11
|
| Side | Sends with | Receives with |
|
|
@@ -19,6 +15,8 @@ By default the sketch runs in a worker, and the page runs on the browser's main
|
|
|
19
15
|
|
|
20
16
|
A message has a name, such as `'color'`, and data, which is optional. A handler gets both. The third argument, `transfer`, is also optional. `page.onMessage` and `engine.onSketchMessage` return a function that removes the handler.
|
|
21
17
|
|
|
18
|
+
A message can reach the sketch only after its setup has run. To give the setup a state that the page knows at the start, pass it as [start data](#start-data).
|
|
19
|
+
|
|
22
20
|
## Example: a color picker on the page
|
|
23
21
|
|
|
24
22
|
The page sends the picked color to the sketch. The sketch paints its box and tells the page when the box is ready.
|
|
@@ -69,12 +67,57 @@ The optional `transfer` list names objects to move instead of copy, such as the
|
|
|
69
67
|
- Messages from one side arrive in the order that side sent them.
|
|
70
68
|
- When the sketch runs in a worker, its handlers run as soon as the worker receives the message, between two frames. The sketch's next frame includes any change that a handler makes to the scene.
|
|
71
69
|
- Messages also arrive while the engine is paused. Changes to the scene then show when the engine resumes.
|
|
72
|
-
- The page can send messages only after `createEngine` resolves, and by then the sketch's setup function has run. Register the sketch's handlers in the setup function. A message that arrives while the sketch has no handler is lost.
|
|
70
|
+
- The page can send messages only after `createEngine` resolves, and by then the sketch's setup function has run. Register the sketch's handlers in the setup function. A message that arrives while the sketch has no handler is lost. Pass what the setup needs as [start data](#start-data).
|
|
73
71
|
|
|
74
72
|
The sketch can send messages from the start of its setup. Until the page registers its first handler, the engine keeps the newest 256 of the sketch's messages. The first handler that `engine.onSketchMessage` registers gets them at once, in order. Later handlers get only the messages that arrive after them.
|
|
75
73
|
|
|
76
74
|
The `onSketchMessage` option of `createEngine` gets every message as it arrives, from the start of the sketch's setup. With the option, the engine keeps no messages for later handlers. Use the option for progress that the sketch reports while it loads.
|
|
77
75
|
|
|
76
|
+
## Start data
|
|
77
|
+
|
|
78
|
+
The `sketchData` option of `createEngine` gives the sketch a state that the page knows before the scene starts. Examples are the section that the page's address names, a saved color mode or a level number. The setup reads it as `ctx.data`, before the first frame. `defineSketch<Data>` gives it a type, and it is `undefined` when the page passes none.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
// page.ts
|
|
82
|
+
import { createEngine } from '@null3d/engine';
|
|
83
|
+
import type { StartData } from './sketch';
|
|
84
|
+
|
|
85
|
+
const params = new URLSearchParams(location.search);
|
|
86
|
+
const sketchData: StartData = {
|
|
87
|
+
stop: params.get('stop') ?? 'hero', // the section that the address names
|
|
88
|
+
dark: localStorage.getItem('dark') === '1', // a saved color mode
|
|
89
|
+
};
|
|
90
|
+
const engine = await createEngine({
|
|
91
|
+
canvas: document.querySelector('canvas')!,
|
|
92
|
+
sketch: new URL('./sketch.ts', import.meta.url),
|
|
93
|
+
sketchData,
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
// sketch.ts
|
|
99
|
+
import { defineSketch } from '@null3d/engine';
|
|
100
|
+
|
|
101
|
+
export interface StartData {
|
|
102
|
+
stop: string;
|
|
103
|
+
dark: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default defineSketch<StartData>(({ data, scene }) => {
|
|
107
|
+
// data is undefined when the page passes no sketchData, as a sketch test's page does.
|
|
108
|
+
scene.setBackground(data?.dark ? '#05070a' : '#dfe8f0');
|
|
109
|
+
const camera = scene.createPerspectiveCamera({
|
|
110
|
+
position: data?.stop === 'city' ? [12, 30, 40] : [0, 2, 8],
|
|
111
|
+
target: [0, 0, 0],
|
|
112
|
+
});
|
|
113
|
+
scene.setActiveCamera(camera);
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The data must be plain JSON data: objects, arrays, strings, finite numbers, booleans and `null`. Its JSON text may take up to 64 KiB. Any other value, such as a `Date`, a `Map` or a typed array, fails the start with [E1213](../errors/E1213.md), which names where the value sits. A property whose value is `undefined` is left out, as JSON leaves it out. Load larger data in the sketch as a file, with [`ctx.assets`](assets.md).
|
|
118
|
+
|
|
119
|
+
The sketch gets its own copy of the data on every thread, also when it runs on the page's thread. A change to the copy reaches neither the page nor a recording. A [recording](../guides/testing.md#record-a-session-that-went-wrong) keeps the data, and its replay gives the sketch the recorded data, not the data of the page that plays it.
|
|
120
|
+
|
|
78
121
|
## A sketch on the page's thread
|
|
79
122
|
|
|
80
123
|
On a page without cross-origin isolation, the engine runs the sketch on the page's thread (see [Hosting and cross-origin isolation](../getting-started/hosting.md)). So does the `sketchThread: 'main'` option of `createEngine` ([Where the sketch runs](../concepts/architecture.md#where-the-sketch-runs)). A message then reaches the other side during the call that sends it, unless it waits for the page's first handler. The data is not copied, and the `transfer` list moves nothing.
|
|
@@ -87,7 +130,13 @@ Write message code that works on both threads:
|
|
|
87
130
|
|
|
88
131
|
## Hold mode
|
|
89
132
|
|
|
90
|
-
In hold mode, `createEngine` resolves only after the engine has drawn the held frame. So the page cannot send the sketch a message before that frame. The sketch's messages from its setup and its held frames reach the page as usual. After the held frame, the sketch's handlers still receive messages, but the sketch runs no more frames. See [Testing your sketch](../guides/testing.md).
|
|
133
|
+
In hold mode, `createEngine` resolves only after the engine has drawn the held frame. So the page cannot send the sketch a message before that frame. [Start data](#start-data) reaches the setup in hold mode as in a live start, so a held frame shows the state that it names. The sketch's messages from its setup and its held frames reach the page as usual. After the held frame, the sketch's handlers still receive messages, but the sketch runs no more frames. See [Testing your sketch](../guides/testing.md).
|
|
134
|
+
|
|
135
|
+
A page that reads its start data from its own address draws that state from the command line too. The command opens the page with its query, and the engine adds its switches beside it:
|
|
136
|
+
|
|
137
|
+
```sh
|
|
138
|
+
bunx @null3d/cli shot --page "/?stop=city"
|
|
139
|
+
```
|
|
91
140
|
|
|
92
141
|
## Related pages
|
|
93
142
|
|
|
@@ -95,3 +144,5 @@ In hold mode, `createEngine` resolves only after the engine has drawn the held f
|
|
|
95
144
|
- [Sketch API: defineSketch and the context](sketch.md): the `page` object in the sketch's context.
|
|
96
145
|
- [Architecture: threads and the frame](../concepts/architecture.md): which thread runs what.
|
|
97
146
|
- [3D scenes on content pages](../guides/content-pages.md): messages that drive a camera from the scroll position.
|
|
147
|
+
- [Messages between page and sketch](https://github.com/null3d-engine/null3d/tree/main/examples/gizmos): the page's buttons send a message, and the sketch acts on it.
|
|
148
|
+
- [What a message can carry](https://github.com/null3d-engine/null3d/tree/main/examples/replay): the replay page sends a typed array to the sketch each frame.
|
package/docs/api/particles.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: api/particles
|
|
3
3
|
title: "Particles (@null3d/particles)"
|
|
4
|
-
status: experimental
|
|
5
|
-
since: "0.2"
|
|
6
4
|
summary: "createParticles: emitters, bursts and sub-emitters; closed-form GPU motion; soft particles; flipbooks made in code."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Particles (@null3d/particles)
|
|
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. Particles do not cast shadows, and lights do not light them. A system does not sort its own particles back to front.
|
|
12
|
-
|
|
13
9
|
```mermaid
|
|
14
10
|
flowchart LR
|
|
15
11
|
update["system.update()<br/>in onLateUpdate"] --> births["Records of the particles<br/>born in the frame"]
|
|
@@ -20,7 +16,13 @@ flowchart LR
|
|
|
20
16
|
|
|
21
17
|
The `@null3d/particles` package makes particle systems: sparks, fire, smoke, dust, rain, falling leaves, fireflies and magic. A system is an emitter and a buffer of particles. The emitter decides where and when particles are born. The GPU moves each particle, turns it and changes its color and size over its life.
|
|
22
18
|
|
|
23
|
-
The GPU works out each particle in closed form, from its birth and its age. The engine's core writes each particle's record once, when the particle is born. The sketch starts the frame and reads settings, but runs no loop over the births. No pass simulates the particles.
|
|
19
|
+
The GPU works out each particle in closed form, from its birth and its age. The engine's core writes each particle's record once, when the particle is born. The sketch starts the frame and reads settings, but runs no loop over the births. No pass simulates the particles.
|
|
20
|
+
|
|
21
|
+
So a system of a million particles costs the CPU one call in a frame, and it draws the same on WebGPU and WebGL2. A held frame at a given time is the same whatever frames came before it.
|
|
22
|
+
|
|
23
|
+

|
|
24
|
+
|
|
25
|
+
Five particle systems in one sketch.
|
|
24
26
|
|
|
25
27
|
```sh
|
|
26
28
|
bun add @null3d/particles
|
|
@@ -58,7 +60,10 @@ export default defineSketch(async (ctx) => {
|
|
|
58
60
|
});
|
|
59
61
|
```
|
|
60
62
|
|
|
61
|
-
Pass the whole sketch context, `ctx`, as the first argument
|
|
63
|
+
- Pass the whole sketch context, `ctx`, as the first argument. The system reads its scene, and the frame's step from its clock.
|
|
64
|
+
- Call `update()` once per frame in `onLateUpdate`. There, objects' world places are the frame's, so an emitter that follows a moving object starts its particles where the object is now.
|
|
65
|
+
- `update(dt)` steps by `dt` seconds in place of the frame's step.
|
|
66
|
+
- `update` allocates nothing, once the table of a system with sub-emitters has grown.
|
|
62
67
|
|
|
63
68
|
## Emitters
|
|
64
69
|
|
|
@@ -140,9 +145,16 @@ return {
|
|
|
140
145
|
};
|
|
141
146
|
```
|
|
142
147
|
|
|
143
|
-
The particle shader samples the field once for each particle at the place where the particle passes. That air replaces `wind`, so the system's fixed wind stands aside while a field is given. A particle takes the air's own displacement over its age, which costs one texture read in the vertex shader. It cannot ride a gust along its path, because it keeps no speed of its own
|
|
148
|
+
The particle shader samples the field once for each particle at the place where the particle passes. That air replaces `wind`, so the system's fixed wind stands aside while a field is given. A particle takes the air's own displacement over its age, which costs one texture read in the vertex shader. It cannot ride a gust along its path, because it keeps no speed of its own. A system without a field costs no texture read. To drive a particle buffer yourself, see [The wind field](../concepts/particle-buffer.md#the-wind-field).
|
|
149
|
+
|
|
150
|
+
A page that drives a particle buffer itself calls `buffer.setWind(field, time.now)` after `wind.update()`, and `buffer.setWind(undefined, time.now)` to remove the field. Give the sketch time, which the field's gust and swirl read.
|
|
151
|
+
|
|
152
|
+
<details>
|
|
153
|
+
<summary>Technical details: why the field is given in every frame</summary>
|
|
144
154
|
|
|
145
|
-
The field's box follows the camera
|
|
155
|
+
The field's box follows the camera, and its newest state changes in every frame. So the add-on gives the field to the core in every frame.
|
|
156
|
+
|
|
157
|
+
</details>
|
|
146
158
|
|
|
147
159
|
## Look
|
|
148
160
|
|
|
@@ -174,11 +186,15 @@ Without a map, each particle is a soft round dot. `particleTextures` makes textu
|
|
|
174
186
|
|
|
175
187
|
### Soft particles
|
|
176
188
|
|
|
177
|
-
A particle that crosses the ground or a wall shows a hard line where it cuts into it. With `softness`, the particle fades out as it nears the surface behind it.
|
|
189
|
+
A particle that crosses the ground or a wall shows a hard line where it cuts into it. With `softness`, the particle fades out as it nears the surface behind it.
|
|
190
|
+
|
|
191
|
+
The camera's view then copies its opaque depth once per frame, after its opaque objects, and the copy costs one full-screen pass. Frames without a soft system copy nothing. Views other than the camera's draw particles without the fade. The Low preset turns the `softParticles` quality setting off, so phones keep one render pass and draw particles with hard edges.
|
|
178
192
|
|
|
179
193
|
## Drawing
|
|
180
194
|
|
|
181
|
-
A system is one scene object, with one draw of all its particles. The particles blend and write no depth. The system sorts among the other blended objects by its bounds.
|
|
195
|
+
A system is one scene object, with one draw of all its particles. The particles blend and write no depth. The system sorts among the other blended objects by its bounds.
|
|
196
|
+
|
|
197
|
+
Its own particles draw in the order they were born, so `additive` blending, which needs no order, suits most effects. With `normal` blending, keep particles soft and faint, as smoke is.
|
|
182
198
|
|
|
183
199
|
The system keeps its object's culling sphere around its live particles, from where they were born and how far they can move. Particles do not cast shadows, and lights do not light them. Fog fades them as it fades other surfaces. Additive particles fade out in fog, and others take on its color.
|
|
184
200
|
|
|
@@ -190,7 +206,14 @@ A system draws its quads from its own records, not from a mesh, so it has no lev
|
|
|
190
206
|
|
|
191
207
|
Each live particle costs the GPU one quad, whose vertex shader works out its state. The core writes 32 bytes per birth, and the engine uploads only the records born in the frame. A system with a large capacity costs vertex work for every record, live or not, so size `capacity` for the particles you need.
|
|
192
208
|
|
|
193
|
-
|
|
209
|
+
A system that runs for hours adds no long frame.
|
|
210
|
+
|
|
211
|
+
<details>
|
|
212
|
+
<summary>Technical details: birth times and the sub-emitter table</summary>
|
|
213
|
+
|
|
214
|
+
The GPU keeps birth times as 32-bit floats. So every 1,024 seconds, a system moves the time that its birth times count from. It rewrites the records' birth times 16,384 at a time, one share per frame. A system with sub-emitters keeps a table of the events to come, 80 bytes per event. It starts small and doubles while the system warms up, up to one event per sub-emitter for each particle.
|
|
215
|
+
|
|
216
|
+
</details>
|
|
194
217
|
|
|
195
218
|
## Related pages
|
|
196
219
|
|
|
@@ -199,6 +222,8 @@ The GPU keeps birth times as 32-bit floats. So every 1,024 seconds, a system mov
|
|
|
199
222
|
- [Points](points.md): squares of one size.
|
|
200
223
|
- [Materials](materials.md): blending and depth writes.
|
|
201
224
|
- [The three.js mapping](../porting/threejs-mapping.md): particles beside three.js's ways of making them.
|
|
225
|
+
- [Emitters, flipbooks and look](https://github.com/null3d-engine/null3d/tree/main/examples/particles): a campfire glade with flames, embers, smoke, falling leaves and fireflies.
|
|
226
|
+
- [Wind field shared with plants](https://github.com/null3d-engine/null3d/tree/main/examples/showcase/creek): the leaves that fall from the trees move in the wind field.
|
|
202
227
|
|
|
203
228
|
## API reference
|
|
204
229
|
|
package/docs/api/points.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: api/points
|
|
3
3
|
title: Points
|
|
4
|
-
status: experimental
|
|
5
|
-
since: "0.2"
|
|
6
4
|
summary: "createPoints; size attenuation; textures."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Points
|
|
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. Points do not cast or receive shadows, and overlap queries do not find them.
|
|
12
|
-
|
|
13
9
|
```mermaid
|
|
14
10
|
flowchart LR
|
|
15
11
|
code["positions and colors<br/>of every point"] --> rows[("Point rows<br/>position, color,<br/>one size")]
|
|
@@ -18,7 +14,9 @@ flowchart LR
|
|
|
18
14
|
cull --> draw["One instanced draw<br/>of squares that face<br/>the camera"]
|
|
19
15
|
```
|
|
20
16
|
|
|
21
|
-
A point cloud is many small squares that face the camera, all of one size, such as stars, dust, sparks or a scanned object. null3D draws each point as a square of two triangles, so points of any size draw the same on every GPU path. A point has no largest size, and it stays in view until its whole square leaves the screen.
|
|
17
|
+
A point cloud is many small squares that face the camera, all of one size, such as stars, dust, sparks or a scanned object. null3D draws each point as a square of two triangles, so points of any size draw the same on every GPU path. A point has no largest size, and it stays in view until its whole square leaves the screen.
|
|
18
|
+
|
|
19
|
+
Each point is a row in the batch's typed arrays, with its own position and color. Sketch code writes the rows straight into engine memory, with no call per point, as it does for [instance batches](../concepts/instances.md).
|
|
22
20
|
|
|
23
21
|
A batch is one draw for the GPU, whatever its size. Points are [sprites](sprites.md) of one size, with no rotation and no atlas. So the engine culls them, and sorts blended points back to front, as it does for sprites.
|
|
24
22
|
|
|
@@ -56,7 +54,13 @@ export default defineSketch(async ({ scene, time }) => {
|
|
|
56
54
|
|
|
57
55
|
## Create a batch
|
|
58
56
|
|
|
59
|
-
`scene.createPoints(options)` returns a promise of the batch. The first call downloads the sprite code, which points share with sprites. A page without points or sprites never downloads it.
|
|
57
|
+
`scene.createPoints(options)` returns a promise of the batch. The first call downloads the sprite code, which points share with sprites. A page without points or sprites never downloads it. If the sprite code does not download, the promise rejects with [E1406](../errors/E1406.md). Await the call in the setup function, as the example does.
|
|
58
|
+
|
|
59
|
+

|
|
60
|
+
|
|
61
|
+
The galaxy demo (120,000 soft stars), not the code above it.
|
|
62
|
+
|
|
63
|
+
The [points demo](https://github.com/null3d-engine/null3d/tree/main/examples/galaxy) draws a galaxy of 120,000 stars in one batch, and writes their positions in every frame.
|
|
60
64
|
|
|
61
65
|
`scene.createPoints` takes these options:
|
|
62
66
|
|
|
@@ -91,7 +95,9 @@ export default defineSketch(async ({ scene, time }) => {
|
|
|
91
95
|
| `positions` | 3 | Its position in the world (x, y, z), relative to the batch's `origin` |
|
|
92
96
|
| `colors` | 4 | A linear color (r, g, b, a), white at first |
|
|
93
97
|
|
|
94
|
-
Point `i` starts at index `i * 3` in `positions` and `i * 4` in `colors`. The `colors` array always holds 4 numbers per point, even when `colors` in the options held 3: the call writes an alpha of 1 then.
|
|
98
|
+
Point `i` starts at index `i * 3` in `positions` and `i * 4` in `colors`. The `colors` array always holds 4 numbers per point, even when `colors` in the options held 3: the call writes an alpha of 1 then.
|
|
99
|
+
|
|
100
|
+
Both arrays are `Float32Array` views of engine memory. The rules of [instance batches](../concepts/instances.md#the-row-arrays) apply. Read the arrays from the batch each time you use them, such as at the start of `onUpdate`. A view from before the engine's memory grew can be empty.
|
|
95
101
|
|
|
96
102
|
Color components from 0 to 1,024 draw, and alpha from 0 to 1. Brighter colors work with [bloom](post.md). Convert an sRGB color, such as a hex string, with `color.fromHex` from the [math helpers](math.md).
|
|
97
103
|
|
|
@@ -115,9 +121,13 @@ A point without a map is a square. For round points, give a map of a disc, with
|
|
|
115
121
|
|
|
116
122
|
Opaque and masked points need no sorting, and cost the least. Blended points draw after the opaque objects, farthest first, in the same transparent pass as other blended objects. Every frame, the engine sorts the points of a batch among themselves and among the scene's other blended objects. For points that add light, such as sparks, `blending: 'additive'` with `depthWrite: false` needs no exact order to look right.
|
|
117
123
|
|
|
124
|
+
Points do not cast or receive shadows, and overlap queries do not find them.
|
|
125
|
+
|
|
118
126
|
## Clicks and raycasts
|
|
119
127
|
|
|
120
|
-
Raycasts hit a point where the ray crosses its square. The hit's `object` is the batch, and its `instance` is the point. A point batch takes `points.on('click', handler)` and the other [pointer events on objects](input.md#pointer-events-on-objects), as an instance batch does.
|
|
128
|
+
Raycasts hit a point where the ray crosses its square. The hit's `object` is the batch, and its `instance` is the point. A point batch takes `points.on('click', handler)` and the other [pointer events on objects](input.md#pointer-events-on-objects), as an instance batch does.
|
|
129
|
+
|
|
130
|
+
The `pointThreshold` option changes the test: a ray then hits a point within that many meters, whatever the point's size. That makes small points easier to click. [Raycasting](raycast.md#sprites-points-and-lines) has the details.
|
|
121
131
|
|
|
122
132
|
## Speed
|
|
123
133
|
|