@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
|
@@ -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`,
|
|
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
|
|
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.
|
|
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
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
180
|
+
### Where the builds go
|
|
169
181
|
|
|
170
|
-
|
|
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.
|
|
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
|
-
|
|
186
|
+
<details>
|
|
187
|
+
<summary>Technical details: the build files and their sizes</summary>
|
|
178
188
|
|
|
179
|
-
A custom
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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.
|
|
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
|
|
package/docs/guides/debugging.md
CHANGED
|
@@ -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
|
-
>
|
|
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
|
|
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.
|
|
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.
|
|
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:
|
|
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
|
-
|
|
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.
|
package/docs/guides/deploying.md
CHANGED
|
@@ -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
|
|
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
|
|
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,13 @@ 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
|
|
93
|
-
| The engine core
|
|
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
|
+
|
|
97
76
|
`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
77
|
|
|
99
78
|
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)).
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: guides/deterministic-simulation
|
|
3
3
|
title: Using null3D with a deterministic simulation
|
|
4
|
-
status: experimental
|
|
5
|
-
since: "0.3"
|
|
6
4
|
summary: "The maths entry a simulation may import; createMath with an exact math library; input, picking and physics that decide a result stay out of the sketch; createEngine's input option; a camera that the player aims from recorded input; a production build of a game's engine files."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Using null3D with a deterministic simulation
|
|
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
|
recorder["A library on the page<br/>records the player's input"] --> simulation["The simulation steps by ticks:<br/>@null3d/engine/math and<br/>createMath on an exact library"]
|
|
@@ -53,7 +49,14 @@ export function stepShip(position: number[], heading: number[], thrust: number):
|
|
|
53
49
|
}
|
|
54
50
|
```
|
|
55
51
|
|
|
56
|
-
Never import `@null3d/engine` itself in simulation code.
|
|
52
|
+
Never import `@null3d/engine` itself in simulation code. A bundler that drops unused code still keeps engine code that runs at load, such as a `FinalizationRegistry`. A replay that checks the simulation's bundle for page-only APIs refuses it. A bundler that keeps whole namespaces also brings page APIs, and helpers that call `Math.sin` and `Math.random`.
|
|
53
|
+
|
|
54
|
+
<details>
|
|
55
|
+
<summary>Technical details: measured bundle sizes</summary>
|
|
56
|
+
|
|
57
|
+
Bun 1.4.3 bundles six helpers from the full entry into 9,133 bytes, against 1,202 bytes from the sub-paths. Bun 1.3.11, which keeps whole re-exported namespaces, takes 87,857 bytes for the same six, with `Atomics`, `document`, `navigator`, `requestAnimationFrame`, and helpers that call `Math.sin`, `Math.cos`, `Math.acos`, `Math.exp` and `Math.random`.
|
|
58
|
+
|
|
59
|
+
</details>
|
|
57
60
|
|
|
58
61
|
The maths entry leaves out four groups:
|
|
59
62
|
|
|
@@ -64,7 +67,7 @@ The maths entry leaves out four groups:
|
|
|
64
67
|
|
|
65
68
|
## Angles and exponentials: createMath
|
|
66
69
|
|
|
67
|
-
`createMath({ sin, cos, acos, exp })` returns the eight helpers that the maths entry leaves out, built on the four functions that you pass. Pass an exact math library's functions, which give the same bits on every JavaScript engine.
|
|
70
|
+
`createMath({ sin, cos, acos, exp })` returns the eight helpers that the maths entry leaves out, built on the four functions that you pass. Pass an exact math library's functions, which give the same bits on every JavaScript engine. The `dmath` entry of the [@clockwork2/engine](https://www.npmjs.com/package/@clockwork2/engine) package is one, and the [deterministic simulation demo](https://github.com/null3d-engine/null3d/tree/main/examples/replay) uses it:
|
|
68
71
|
|
|
69
72
|
```ts
|
|
70
73
|
// sim/turn.ts: simulation code
|
|
@@ -107,7 +110,7 @@ The sketch still draws with the main entry's helpers, which use the browser's `M
|
|
|
107
110
|
|
|
108
111
|
## Input that another library records
|
|
109
112
|
|
|
110
|
-
`createEngine({ input: false })` keeps the engine from listening for input. The engine then adds no listener that takes input: none for pointers, keys, the wheel, focus, the pointer lock or gamepads.
|
|
113
|
+
`createEngine({ input: false })` keeps the engine from listening for input. The engine then adds no listener that takes input: none for pointers, keys, the wheel, focus, the pointer lock or gamepads. [Input off](../api/input.md#input-off) lists them. The library that records the player's input listens alone. The [deterministic simulation demo](https://github.com/null3d-engine/null3d/tree/main/examples/replay) takes its input on the page, and the [input demo](https://github.com/null3d-engine/null3d/tree/main/examples/input) binds an action map to the keyboard and a gamepad.
|
|
111
114
|
|
|
112
115
|
```ts
|
|
113
116
|
// page.ts
|
|
@@ -130,6 +133,10 @@ The sketch's `ctx.input` then stays at rest. Every key, button and action is up,
|
|
|
130
133
|
|
|
131
134
|
A game whose result depends on where the player looks records the look as input. An example is a game that scores what the player aims at. The camera is then the simulation's, and the sketch only draws it:
|
|
132
135
|
|
|
136
|
+

|
|
137
|
+
|
|
138
|
+
The run's start, built from the seed.
|
|
139
|
+
|
|
133
140
|
1. The page's input code locks the pointer on the canvas with `canvas.requestPointerLock()`, in a click handler. When a drag also turns the view, ask only on a click that barely moved, since a drag ends in a click too. Read `document.pointerLockElement` at each event, since a press can come before `pointerlockchange` does. Take no pointer capture under the lock: the browser refuses one with an `InvalidStateError`. The engine's own `requestPointerLock()` belongs to its input, and fails with [E1426](../errors/E1426.md) when that is off.
|
|
134
141
|
2. Each tick, the page records the mouse's movement since the last tick as whole counts, and carries the fraction to the next tick. A browser can report a fraction of a pixel, and the log holds what the simulation reads.
|
|
135
142
|
3. The simulation turns the counts into a yaw and a pitch, and builds the camera's rotation with `createMath`'s `quat.fromEuler`.
|
|
@@ -163,7 +170,9 @@ export function step(state: State, counts: number): void {
|
|
|
163
170
|
|
|
164
171
|
## Sending the state to the sketch
|
|
165
172
|
|
|
166
|
-
The page sends the simulation's state with `engine.postToSketch`. The sketch takes it with `page.onMessage`, as [Messages between the page and the sketch](../api/page.md) shows, and draws it in `onUpdate`.
|
|
173
|
+
The page sends the simulation's state with `engine.postToSketch`. The sketch takes it with `page.onMessage`, as [Messages between the page and the sketch](../api/page.md) shows, and draws it in `onUpdate`.
|
|
174
|
+
|
|
175
|
+
Send only the numbers that the sketch draws, in a small typed array. Send a new array each time: a sketch on the page's thread gets the very array that the page sent. When the sketch runs in a worker, the browser copies each message's data.
|
|
167
176
|
|
|
168
177
|
Until the first state arrives, as in a held frame, the sketch can draw the run's start. It imports the simulation's start function and builds the first state from the same seed, and it never steps the state.
|
|
169
178
|
|
|
@@ -209,17 +218,15 @@ export default defineConfig({
|
|
|
209
218
|
|
|
210
219
|
The game's module then imports `createEngine` and `SKETCH` from the folder's `entry.js`, and passes `SKETCH` as the `sketch` option. The plugin also writes `null3d-files.json`, which lists the files that the start loads and those of each feature, as [Hosting](../getting-started/hosting.md) describes.
|
|
211
220
|
|
|
212
|
-
The shader modules decide most of the folder's size.
|
|
213
|
-
|
|
214
|
-
1. Record the words from a run of the game's page, as [Build only the shader permutations you draw with](assets-pipeline.md#build-only-the-shader-permutations-you-draw-with) shows. Name the game's pages with `--page`, or `--live` for a page that the recorder drives through its states. Record on both GPU paths, and at the low and high presets.
|
|
215
|
-
2. In a checkout of null3D, build the shader modules from the list: `NULL3D_SHADER_PERMUTATIONS=<list> bun run shaders`. The checkout's `packages/engine/src/generated` then holds them.
|
|
216
|
-
3. Build the game with `NULL3D_ENGINE_SHADERS` set to that folder, such as `NULL3D_ENGINE_SHADERS=<checkout>/packages/engine/src/generated bunx vite build`. The plugin then takes the narrowed modules in place of the full set.
|
|
221
|
+
The shader modules decide most of the folder's size. Copy only the builds of the shader permutation words that the game draws with:
|
|
217
222
|
|
|
218
|
-
|
|
223
|
+
1. Build a development copy of the folder (`bunx vite build --mode development`). A production build ignores `?permutations`, `?gpu` and `?preset`, so a recording of it holds nothing.
|
|
224
|
+
2. Record the words from a run of the game's page, as [Build only the shader permutations you draw with](assets-pipeline.md#build-only-the-shader-permutations-you-draw-with) shows. The game's engine starts on a click, so name its page with `--live` and give the recorder the start control. Serve the game with its development folder, and give the recorder its address: `bunx @null3d/cli shaders record --base http://localhost:8080 --live /game.html --click "#play"`.
|
|
225
|
+
3. Name the list in the engine folder's config, as `null3d({ shaders: { list: 'shader-permutations.txt' } })`, and build the folder in production again. The build copies only the listed builds, from the release asset, and compiles nothing.
|
|
219
226
|
|
|
220
|
-
|
|
227
|
+
With null3D 0.1.0's shader set, a small scene drew with 6 words. Its build copied 218 shader files and 17 MB, against 7,597 files and 4,115 MB without a list. Build the set again after each engine upgrade.
|
|
221
228
|
|
|
222
|
-
|
|
229
|
+
The shader modules in a release asset are the full set. A game that builds against that asset without a list copies every module into its files folder. The build then warns with the size that it copied. That can exceed the folder's file and byte limits. Record again after the game gains a feature that turns on a new word. The engine names a word that the list lacks the first time a pipeline needs it.
|
|
223
230
|
|
|
224
231
|
## API reference
|
|
225
232
|
|