@null3d/engine 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +40 -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 +68 -24
- 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/E1308.md +31 -0
- 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 +53 -29
- package/docs/getting-started/cdn.md +15 -9
- package/docs/getting-started/first-scene.md +20 -12
- package/docs/getting-started/hosting.md +113 -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 +13 -14
- package/docs/guides/assets-pipeline.md +294 -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 +86 -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 +10 -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 +74 -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 +126 -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 +116 -105
- package/lib/errors/engine-error.js +1 -1
- package/lib/errors/fixes.d.ts +1 -0
- package/lib/errors/fixes.js +19 -18
- package/lib/generated/shader-permutations.d.ts +6 -0
- package/lib/generated/shader-permutations.js +6 -0
- 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 +29 -7
- package/lib/page/engine.js +79 -7
- 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 +8 -0
- package/lib/shared/permutation-words.js +14 -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/tier.d.ts +9 -0
- package/lib/shared/tier.js +4 -1
- 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/guides/audio.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: guides/audio
|
|
3
3
|
title: Sound in a scene
|
|
4
|
-
status: experimental
|
|
5
|
-
since: "0.1"
|
|
6
4
|
summary: "Sound that follows the scene with @null3d/sound: beds whose level the scene sets, sources at places, groups of many sources, and events."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Sound in a scene
|
|
10
8
|
|
|
11
|
-
> Roadmap step 0.1, first released in null3D 0.1.0. The API is experimental, so it can still change between versions.
|
|
12
|
-
|
|
13
9
|
```mermaid
|
|
14
10
|
flowchart LR
|
|
15
11
|
scene["The sketch<br/>knows the scene"] -- "level, place, rate<br/>and tone of each sound" --> sound["@null3d/sound"]
|
|
@@ -72,7 +68,12 @@ A bed that comes from one side can still have a place. The sea is louder toward
|
|
|
72
68
|
|
|
73
69
|
## Sources at places
|
|
74
70
|
|
|
75
|
-
A source at a place falls off with distance by itself. Give it `near`, the distance within which it plays at its full level, and `far`, where it has faded out. A waterfall heard across a valley might take `near:
|
|
71
|
+
A source at a place falls off with distance by itself. Give it `near`, the distance within which it plays at its full level, and `far`, where it has faded out. A waterfall heard across a valley might take `near: 5` and `far: 400`. With `far`, the sound also loses its highs with distance, as real sounds do.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const fall = sound.loop({ file: '/sounds/waterfall.mp3', near: 5, far: 400 });
|
|
75
|
+
fall.setPosition(lip[0], lip[1] - drop, lip[2]);
|
|
76
|
+
```
|
|
76
77
|
|
|
77
78
|
Choose `far` as the distance at which a listener would stop hearing the thing, and `near` as the distance at which it is as loud as it gets. The ranges differ by kind:
|
|
78
79
|
|
|
@@ -88,12 +89,7 @@ Choose `far` as the distance at which a listener would stop hearing the thing, a
|
|
|
88
89
|
| The open sea heard from shore | 40 | 300 |
|
|
89
90
|
| Thunder, heard across a city | 100 | 4000 |
|
|
90
91
|
|
|
91
|
-
A source whose `far` reaches a
|
|
92
|
-
|
|
93
|
-
```ts
|
|
94
|
-
const fall = sound.loop({ file: '/sounds/waterfall.mp3', near: 6, far: 200 });
|
|
95
|
-
fall.setPosition(lip[0], lip[1] - drop, lip[2]);
|
|
96
|
-
```
|
|
92
|
+
A source whose `far` reaches a kilometer is heard from the whole map, and a scene's opening view roars when nothing stands near it.
|
|
97
93
|
|
|
98
94
|
A long source, such as a stream, has a place that moves with the camera: the nearest point of its course. Work that point out in each frame and move the source there.
|
|
99
95
|
|
|
@@ -124,25 +120,31 @@ bounce.play(x, y, z, Math.min(1, impact / 8));
|
|
|
124
120
|
|
|
125
121
|
## A mix that works
|
|
126
122
|
|
|
127
|
-
- Normalize the files to one loudness, so a level of 1 means the same for each.
|
|
123
|
+
- Normalize the files to one loudness, such as -20 LUFS, so a level of 1 means the same for each. If you cannot change the files, multiply each sound's level by a gain that brings it there.
|
|
128
124
|
- Keep beds below the sources that tell a story. The sea at 0.6 and a passing car at 1 lets the car stand out.
|
|
129
125
|
- Tie a sound to the motion that makes it. Rustling leaves follow the wind's speed and the trees near the camera, and an engine's rate follows its car's speed.
|
|
130
126
|
- Tie a bed's level to the scene. A level that follows the distance to the shore never jumps when the camera cuts.
|
|
131
|
-
- Give every source a reach that matches what makes it. Waves are heard near water, and an engine near its own car. A
|
|
127
|
+
- Give every source a reach that matches what makes it. Waves are heard near water, and an engine near its own car. A source whose `far` reaches a kilometer makes an opening view over open water roar. A scene's first frame is the test: where nothing makes a sound, the mix is silent.
|
|
132
128
|
- Never write a level as a step. Move it along a curve that passes through its neighbors, so a car that stops fades through silence rather than cutting off at a threshold. Let the frame's own time smooth what the scene writes.
|
|
133
|
-
- Count the voices of a group. They add as power, so three
|
|
129
|
+
- Count the voices of a group. They add as power, so three voices at full level sound about five decibels louder than one loop. Give a group's members a lower level than a loop's.
|
|
134
130
|
- Write massed ambience as one loop scaled by density, such as a wood's leaves from the canopy around the camera. One source for each of fifty trees adds up in a way no listener calls rustling. A density that levels off cannot pile up.
|
|
135
131
|
- Check the mix at fixed camera places, not by ear alone: the opening pose, the shore, a town square and the climb. The opening should be silent, and each place should lead with the sound its view shows. No level should move faster between frames than a listener would expect.
|
|
136
132
|
|
|
137
133
|
## How the sound reaches the speakers
|
|
138
134
|
|
|
139
|
-
The browser offers its audio on the page's main thread only, and the sketch runs in a worker. The add-on bridges the two with a table that the page shares. The sketch writes each sound's values there, and a mixer on the browser's audio thread reads them and drives each voice.
|
|
135
|
+
The browser offers its audio on the page's main thread only, and the sketch runs in a worker. The add-on bridges the two with a table that the page shares. The sketch writes each sound's values there, and a mixer on the browser's audio thread reads them and drives each voice.
|
|
136
|
+
|
|
137
|
+
Where the page shares its memory with its workers, no message travels in a frame. The page's thread then does no work while the sounds play. When the engine pauses or the page is hidden, the sketch's frames stop, and the sound fades out with them.
|
|
140
138
|
|
|
141
139
|
The page can still play its own sounds with Web Audio, such as music or the clicks of its buttons. Such sounds belong to the page, so they need no add-on.
|
|
142
140
|
|
|
143
141
|
## Related pages
|
|
144
142
|
|
|
145
143
|
- [Sound (@null3d/sound)](../api/sound.md): every call, the distances, the voices and the costs.
|
|
144
|
+
- [Sources at places](https://github.com/null3d-engine/null3d/tree/main/examples/sound): a thunderstorm on the moor, where each strike's thunder plays from where it struck.
|
|
145
|
+
- [Beds whose level the scene sets](https://github.com/null3d-engine/null3d/tree/main/examples/showcase/creek): the creek's loops follow the water and the wind.
|
|
146
|
+
- [Groups of many sources](https://github.com/null3d-engine/null3d/tree/main/examples/showcase/skyway): the hover cars, rain, thunder and city of a rainy night.
|
|
147
|
+
- [Cars and a bus as one group](https://github.com/null3d-engine/null3d/tree/main/examples/showcase/island): the island's traffic sounds from its lanes.
|
|
146
148
|
- [Messages between sketch and page](../api/page.md): messages for events that the page shows.
|
|
147
149
|
- [Using a physics library](physics.md): events from a physics world, such as a bounce.
|
|
148
150
|
- [Hosting and cross-origin isolation](../getting-started/hosting.md): when a page shares its memory.
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: guides/baked-light-data
|
|
3
3
|
title: Baked light data
|
|
4
|
-
status: stable
|
|
5
|
-
since: "0.3"
|
|
6
4
|
summary: "One file format for probe grids and light maps baked outside the engine: the JSON members, base64 payloads, the loading calls, the engine's own bake, and the faults that the loader names."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Baked light data
|
|
10
8
|
|
|
11
|
-
>
|
|
9
|
+
> The format is stable: version 1 holds the members that this page lists.
|
|
12
10
|
|
|
13
11
|
```mermaid
|
|
14
12
|
flowchart LR
|
|
@@ -30,7 +28,7 @@ A file is JSON. Three members come first:
|
|
|
30
28
|
| `version` | The whole number `1`. A reader refuses any other version |
|
|
31
29
|
| `kind` | `probe-grid` or `light-map` |
|
|
32
30
|
|
|
33
|
-
The rest of the file depends on its kind. A reader ignores members that it does not know. A bake tool may write members that an earlier engine leaves out, and the file still reads. The `version` number changes only when a reader that predates the file would read it wrongly.
|
|
31
|
+
The rest of the file depends on its kind. A reader ignores members that it does not know. A bake tool may write members that an earlier engine leaves out, and the file still reads. The `version` number changes only when a reader that predates the file would read it wrongly.
|
|
34
32
|
|
|
35
33
|
The large values are base64. Decode them as bytes, not as text.
|
|
36
34
|
|
|
@@ -61,9 +59,18 @@ A probe grid file holds the values of `LightProbeGridData`. They are the grid's
|
|
|
61
59
|
| `validity` | Optional. Base64 of one 32-bit float for each probe, from 0 to 1 |
|
|
62
60
|
| `skyVisibility` | Optional. Base64 of one 32-bit float for each probe, from 0 to 1 |
|
|
63
61
|
|
|
64
|
-
The probes run x fastest, then y, then z. Each probe holds four RGB coefficients in the order that the shader reads them:
|
|
62
|
+
The probes run x fastest, then y, then z. Each probe holds four RGB coefficients in the order that the shader reads them:
|
|
65
63
|
|
|
66
|
-
|
|
64
|
+
- L0;
|
|
65
|
+
- L1 along y;
|
|
66
|
+
- L1 along z;
|
|
67
|
+
- L1 along x.
|
|
68
|
+
|
|
69
|
+
The 12 floats are three for L0 and three for each L1 axis. `bakeProbes` writes the same values. A bake that a tool makes therefore matches the engine's own bake for the same scene.
|
|
70
|
+
|
|
71
|
+
The coefficients hold the light that the surfaces around the probe bounce back. The validity is 1 where the probe stands in open space, or 0 where geometry encloses it, so its light never leaks through a wall.
|
|
72
|
+
|
|
73
|
+
The sky visibility is the share of the sky that the probe's rays reached. The engine weights it by how much a surface faces the sky above the horizon, and multiplies it by the environment's live light, so the hour needs no new bake.
|
|
67
74
|
|
|
68
75
|
A file without the two masks reads as one that baked the sky into its coefficients: every probe is valid, and no probe adds the live sky. A bake tool that writes version 1 without them therefore keeps the look that its coefficients alone give.
|
|
69
76
|
|
|
@@ -87,11 +94,11 @@ A file without the two masks reads as one that baked the sky into its coefficien
|
|
|
87
94
|
| `width`, `height` | The texels across and down: whole numbers of 1 or more |
|
|
88
95
|
| `encoding` | `rgb8`, three bytes per texel in sRGB, or `rgba16float`, eight bytes per texel as little-endian half floats in linear light |
|
|
89
96
|
| `radiance` | Base64 of the texels, rows along v from the first texel at u = 0, v = 0 |
|
|
90
|
-
| `direction` | Optional. Base64 of three signed bytes per texel, each axis of the
|
|
97
|
+
| `direction` | Optional. Base64 of three signed bytes per texel, from -127 to 127: each axis of the unit direction times 127, rounded. A reader divides each byte by 127 |
|
|
91
98
|
|
|
92
99
|
Use `rgba16float` for light above 1, such as a window or a lamp that the bake kept bright. It takes 8 bytes for each texel, against 3 for `rgb8`. A `direction` adds 3 bytes for each texel: a directional light map takes twice the memory of an `rgb8` radiance.
|
|
93
100
|
|
|
94
|
-
The direction is a unit vector in world space, and it points from the surface toward the light.
|
|
101
|
+
The direction is a unit vector in world space, and it points from the surface toward the light. The engine gives the direction a texture of its own, in the radiance's size and format, so the two share one texture array and one texture binding.
|
|
95
102
|
|
|
96
103
|
## Load a file
|
|
97
104
|
|
|
@@ -119,22 +126,16 @@ export default defineSketch(async ({ scene, assets, materials }) => {
|
|
|
119
126
|
|
|
120
127
|
`loadLightProbeGrid` gives the texture and the three placement values, so its result goes straight to `scene.setLightProbeGrid`. The grid replaces the environment's diffuse light, as a grid that `bakeProbes` made does.
|
|
121
128
|
|
|
122
|
-
`loadLightMap` gives a texture for a material's `lightMap`. A file with a `direction` also gives a `directionTexture`, which a material takes as `lightMapDirection`. The
|
|
129
|
+
`loadLightMap` gives a texture for a material's `lightMap`. A file with a `direction` also gives a `directionTexture`, which a material takes as `lightMapDirection`. The first load of either kind loads the reader, which is about 2 KB after Brotli. A page that reads no baked light file downloads none of it.
|
|
123
130
|
|
|
124
|
-
|
|
131
|
+
<details>
|
|
132
|
+
<summary>Technical details: the textures that the loader makes</summary>
|
|
125
133
|
|
|
126
|
-
|
|
134
|
+
The radiance texture is an `rgba8unorm` texture in sRGB, or an `rgba16float` texture in linear light. The file's three color bytes become three channels and an opaque fourth, because the engine has no three-channel texture format. The direction texture has the radiance's size and format, so a shader reads it through the light map's own binding. The weight costs one texture read and a few arithmetic operations for each pixel of a directional map, and a plain map pays a branch.
|
|
127
135
|
|
|
128
|
-
|
|
136
|
+
</details>
|
|
129
137
|
|
|
130
|
-
|
|
131
|
-
- The map reads the second texture coordinates. A mesh without a second set reads its first, as Needle's viewer does.
|
|
132
|
-
- The renderer's `lightmapScaleOffset` places the bake, and Needle's offset counts rows from the top, so the vertical offset flips in the engine.
|
|
133
|
-
- A skybox or reflection map that the extension registers is left out, and so is a pointer to a file that is not a texture of the model.
|
|
134
|
-
|
|
135
|
-
The loader makes a material for each node that places a light map of its own, so each bake lands where the file put it.
|
|
136
|
-
|
|
137
|
-
Both calls throw [E1434](../errors/E1434.md) for a file that the engine cannot read. The message names the fault. It may name a version that the engine does not read, or a payload that does not hold the count that the file gives. It may also name a file that a download cut short.
|
|
138
|
+
With `lightMapDirection`, the engine weights the light map at each pixel by how far the shaded normal turns toward the stored direction from the mesh's own normal. A normal map's relief then brightens where it faces the light of the bake and darkens where it turns away. A surface without a normal map keeps the light that the bake gave it.
|
|
138
139
|
|
|
139
140
|
## Save a grid that the engine baked
|
|
140
141
|
|
|
@@ -156,7 +157,7 @@ export default defineSketch(async ({ scene, assets }) => {
|
|
|
156
157
|
|
|
157
158
|
## What the loader checks
|
|
158
159
|
|
|
159
|
-
|
|
160
|
+
`loadLightProbeGrid` and `loadLightMap` throw [E1434](../errors/E1434.md) for a file whose contents do not agree with its own members. The message names the fault, which is one of these:
|
|
160
161
|
|
|
161
162
|
- a missing or wrong `format` member, or text that is not JSON;
|
|
162
163
|
- a `version` that this engine does not read;
|
|
@@ -171,6 +172,26 @@ A reader refuses a file whose contents do not agree with its own members. Each f
|
|
|
171
172
|
|
|
172
173
|
## Limits
|
|
173
174
|
|
|
174
|
-
- A grid takes at most 256 probes along x, 256 along y and 62 along z.
|
|
175
|
+
- A grid takes at most 256 probes along x, 256 along y and 62 along z.
|
|
175
176
|
- A light map's side is at most 4,096 texels, and one payload is at most 256 MiB, as every texture file is.
|
|
176
|
-
- The grid's texture takes 32 bytes of GPU memory for each probe,
|
|
177
|
+
- The grid's texture takes about 32 bytes of GPU memory for each probe, so a grid of 24 x 8 x 24 probes takes about 157 KiB. A light map's texture takes 4 bytes for each texel from `rgb8`, and 8 from `rgba16float`.
|
|
178
|
+
|
|
179
|
+
<details>
|
|
180
|
+
<summary>Technical details: how the grid's texture is laid out</summary>
|
|
181
|
+
|
|
182
|
+
The grid's texture is cut into four sub-volumes: three hold the coefficients, the first with the probe's own color and one direction's red, and the fourth holds the validity and the sky visibility. The z side carries a padding slice at either end of every sub-volume.
|
|
183
|
+
|
|
184
|
+
</details>
|
|
185
|
+
|
|
186
|
+
## Needle light maps
|
|
187
|
+
|
|
188
|
+
This path reads a glTF model's own light maps. It does not read a baked light file, and it throws no E1434 for one.
|
|
189
|
+
|
|
190
|
+
A model made for [Needle's viewer](https://engine.needle.tools/) carries its light maps in glTF's `NEEDLE_lightmaps` extension, and each node's `MeshRenderer` in `NEEDLE_components` names the map that it reads and the scale and offset that place the bake on its mesh. The loader gives each such node's material the map:
|
|
191
|
+
|
|
192
|
+
- The texture keeps the file's bytes, and the material decodes its RGBM texels: each color is multiplied by the texel's alpha times 8, then the sRGB transfer turns it linear. The texture is read in linear color for that reason.
|
|
193
|
+
- The map reads the second texture coordinates. A mesh without a second set reads its first, as Needle's viewer does.
|
|
194
|
+
- The renderer's `lightmapScaleOffset` places the bake, and Needle's offset counts rows from the top, so the vertical offset flips in the engine.
|
|
195
|
+
- A skybox or reflection map that the extension registers is left out, and so is a pointer to a file that is not a texture of the model.
|
|
196
|
+
|
|
197
|
+
The loader makes a material for each node that places a light map of its own, so each bake lands where the file put it.
|
package/docs/guides/car-paint.md
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: guides/car-paint
|
|
3
3
|
title: Car paint
|
|
4
|
-
|
|
5
|
-
since: "0.3"
|
|
6
|
-
summary: "A paint material over the clear coat, with glinting metal flakes that stop shimmering, panel seams that follow the pixel, and an optional carbon weave with direction-dependent shine."
|
|
4
|
+
summary: "An example add-on you copy: a paint material over the clear coat, with glinting metal flakes that stop shimmering, panel seams that follow the pixel, and an optional carbon weave with direction-dependent shine."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# Car paint
|
|
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
|
The car paint draws a metallic base under a clear coat. Three parts change the look: metal flakes,
|
|
14
10
|
panel seams, and an optional carbon weave. Each part reads the mesh's texture coordinates, so the
|
|
15
11
|
paint fits any panel that has them.
|
|
@@ -25,25 +21,28 @@ flowchart LR
|
|
|
25
21
|
coat --> light
|
|
26
22
|
```
|
|
27
23
|
|
|
28
|
-
The paint is an example add-on in the repository, in the `examples/addons/car-paint` folder. It is
|
|
29
|
-
not an npm package. Copy the folder into your project and import its entry point.
|
|
24
|
+
The paint is an example add-on in the repository, in the `examples/addons/car-paint` folder. It is not an npm package. Copy the folder into your project and import its entry point. The null3D Vite plugin compiles the folder's WGSL file, as it does for your own shaders ([Add the Vite plugin](../getting-started/install.md#add-the-vite-plugin)). A page with no build step cannot use it.
|
|
30
25
|
|
|
31
26
|
## Use it
|
|
32
27
|
|
|
33
28
|
```ts
|
|
29
|
+
import { defineSketch } from '@null3d/engine';
|
|
34
30
|
import { createCarPaint } from './addons/car-paint';
|
|
35
31
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
export default defineSketch((ctx) => {
|
|
33
|
+
const paint = createCarPaint(ctx, {
|
|
34
|
+
color: '#9e0b16',
|
|
35
|
+
metalness: 0.72,
|
|
36
|
+
roughness: 0.34,
|
|
37
|
+
flakes: { cells: [120, 210], tilt: 0.25 },
|
|
38
|
+
seams: { spacing: [0.34, 0], width: 0.0022 },
|
|
39
|
+
clearcoat: 1,
|
|
40
|
+
clearcoatRoughness: 0.02,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// `body` is the car body's mesh, from your project's model or geometry.
|
|
44
|
+
ctx.scene.createMesh({ mesh: body, material: paint });
|
|
44
45
|
});
|
|
45
|
-
|
|
46
|
-
scene.createMesh({ mesh: body, material: paint });
|
|
47
46
|
```
|
|
48
47
|
|
|
49
48
|
## Options
|
|
@@ -57,7 +56,9 @@ scene.createMesh({ mesh: body, material: paint });
|
|
|
57
56
|
| `clearcoatRoughness` | The coat's perceptual roughness, from 0 to 1 | `0.02` |
|
|
58
57
|
| `flakes` | `{ cells, tilt, angle, max }`, or `false` | `{ cells: [120, 210], tilt: 0.25, angle: 0.25, max: 64 }` |
|
|
59
58
|
| `seams` | `{ spacing, width, shade }`, or `false` | `{ spacing: [0.34, 0], width: 0.0022, shade: 0.85 }` |
|
|
60
|
-
| `weave` | `{ cells, shine }`, or `false` | `false` |
|
|
59
|
+
| `weave` | `{ cells, shine }`, or `false` | `false`; a weave object starts from `{ cells: [90, 150], shine: 0.85 }` |
|
|
60
|
+
|
|
61
|
+
A part given as an object keeps the default for each field it leaves out.
|
|
61
62
|
|
|
62
63
|
- `flakes.cells` counts flake cells across each texture coordinate.
|
|
63
64
|
- `flakes.tilt` is how far the flakes' normals spread from the surface normal. It sets the width
|
|
@@ -70,19 +71,20 @@ scene.createMesh({ mesh: body, material: paint });
|
|
|
70
71
|
- `seams.spacing` gives the interval between seams along each texture coordinate. A value of 0
|
|
71
72
|
draws no seam along that coordinate.
|
|
72
73
|
- `seams.width` is the groove's half width in the texture coordinates.
|
|
74
|
+
- `seams.shade` is how dark the groove draws, from 0 for none to 1 for black.
|
|
73
75
|
- `weave.cells` counts weave cells across each texture coordinate.
|
|
74
76
|
- `weave.shine` is how strongly a tow's rounded cross-section shapes its highlight, from 0 to 1.
|
|
75
77
|
|
|
76
78
|
## How the parts draw
|
|
77
79
|
|
|
78
|
-
The **flakes** are the metal's microfacets, drawn with the glint model of Deliot and Belcour.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
80
|
+
The **flakes** are the metal's microfacets, drawn with the glint model of Deliot and Belcour. A pixel whose flakes miss the light draws no highlight, and one where a flake catches it draws a bright spot. A car far away draws as smooth metal, without shimmering as it moves. The spot stays where the light reflects, so the sparkle follows each light as the car turns. The clear coat keeps its own smooth normal, so the flakes never disturb the coat's reflection.
|
|
81
|
+
|
|
82
|
+
<details>
|
|
83
|
+
<summary>Technical details: the glint model</summary>
|
|
84
|
+
|
|
85
|
+
Each pixel covers a number of flake cells. The paint counts how many of the flakes in that footprint reflect a light into the eye, and uses that count in place of the smooth highlight. The count's average is the smooth lobe, so a car far away draws as smooth metal.
|
|
86
|
+
|
|
87
|
+
</details>
|
|
86
88
|
|
|
87
89
|
The model replaces the base highlight of the main directional light and of each point and spot
|
|
88
90
|
light. Area lights draw their own smooth highlight, with no glints.
|
|
@@ -97,11 +99,14 @@ their own way. The gaps between the tows draw darker and rougher.
|
|
|
97
99
|
|
|
98
100
|
## Cost
|
|
99
101
|
|
|
100
|
-
The
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
The flakes add a small cost for each light. The seams and the weave add a small cost for each pixel. A material without flakes, seams or a weave draws the same surface as a plain metallic paint.
|
|
103
|
+
|
|
104
|
+
<details>
|
|
105
|
+
<summary>Technical details: what each part costs</summary>
|
|
106
|
+
|
|
107
|
+
The paint pays for the clear coat's lobe. The flakes cost the footprint's derivatives and one hash for each pixel, and the lobe adds two hashes and a short sum of terms for each pixel and each light that reaches it. The seams cost a few derivatives for each pixel. The weave adds its own pattern, slope and roughness change.
|
|
108
|
+
|
|
109
|
+
</details>
|
|
105
110
|
|
|
106
111
|
## Limits
|
|
107
112
|
|
|
@@ -119,3 +124,4 @@ plain metallic paint.
|
|
|
119
124
|
glints use.
|
|
120
125
|
- [The Showroom to street demo](https://github.com/null3d-engine/null3d/tree/main/examples/showroom): a
|
|
121
126
|
car that uses the paint.
|
|
127
|
+
- [How the parts draw](https://github.com/null3d-engine/null3d/tree/main/examples/coat-and-sheen): a clear coat, rain rings and sheen in one rainy showroom.
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: guides/content-pages
|
|
3
3
|
title: 3D scenes on content pages
|
|
4
|
-
status: experimental
|
|
5
|
-
since: "0.1"
|
|
6
4
|
summary: "Product and marketing pages: the fallback page, a load deadline, pausing off screen, scroll-driven cameras, second visits and crashes."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
7
|
# 3D scenes on content pages
|
|
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
|
On a product page or a marketing page, the visitor came for the page. The 3D scene supports it. So the page must work without the scene, show its content on time, and spend nothing on a scene that nobody can see.
|
|
14
10
|
|
|
15
11
|
A game or a full-screen app is different, because there the scene is the product. Each section says where its advice changes for such pages.
|
|
@@ -85,6 +81,41 @@ try {
|
|
|
85
81
|
|
|
86
82
|
For a game, the late state has no page content to show. There it changes the message, and opens the menus and settings that do not need the scene.
|
|
87
83
|
|
|
84
|
+
Later page blocks run after a successful start, inside that `try`.
|
|
85
|
+
|
|
86
|
+
### A scene that builds in stages
|
|
87
|
+
|
|
88
|
+
A scene whose later stages build after the first frame still reveals its canvas at the first frame. A canvas held back for a later stage makes the visitor wait for that stage's whole build.
|
|
89
|
+
|
|
90
|
+
On a Galaxy S24 on the WebGL2 path, a page whose canvas waits for its city stage shows it 6.4 seconds into a repeat visit. Revealed at the first frame, the same canvas shows after 1.8 seconds, and after 3.2 seconds on a first visit.
|
|
91
|
+
|
|
92
|
+
Hide the place of a stage that has not built yet. Fog that lies thick on the ground and thins with height hides empty ground, and leaves a subject high above it in clear air. Once the stage shows, clear the fog over a second or two. Clear it by the clock, because the next warm-up can hold the frames back.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// sketch.ts: fog over the place of the city, which builds after the first frame.
|
|
96
|
+
const mist = { color: '#5a6472', density: 0.2, height: 0, heightFalloff: 0.03, background: 1 };
|
|
97
|
+
scene.setFog(mist);
|
|
98
|
+
let clearFrom = 0;
|
|
99
|
+
void engine.firstFrame.then(async () => {
|
|
100
|
+
await buildCity(); // creates the city hidden, warms it up, then shows it
|
|
101
|
+
clearFrom = performance.now();
|
|
102
|
+
});
|
|
103
|
+
return {
|
|
104
|
+
onUpdate() {
|
|
105
|
+
if (clearFrom === 0 || mist.density === 0) return;
|
|
106
|
+
mist.density = 0.2 * Math.max(0, 1 - (performance.now() - clearFrom) / 2000);
|
|
107
|
+
scene.setFog(mist);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- Give the fog the tone of what it hides. Light words over a pale fog do not read, and a darker fog keeps the page's contrast.
|
|
113
|
+
- Start every value that the sketch eases at the page's own state, such as the hour of a day cycle. The first frame shows the ease. Pass the page's state in `sketchData`.
|
|
114
|
+
- After the deadline, the poster shows the finished scene. When the first frame comes later, keep the poster until the canvas shows as much as the poster does.
|
|
115
|
+
- Words styled to stand over the scene need the page's own ink while a loader stands on a plain background. Fade them to their color as the canvas comes in.
|
|
116
|
+
- A fill light that only a plain first view needs fades with that view. A directional light lights every view, and a device that skips the richer stage keeps it.
|
|
117
|
+
- Build each part that an interaction needs in an early stage that every device gets. A part that a lighter device skips takes the interaction with it. For example, a cube that opens round a glowing heart never opens where the heart is missing.
|
|
118
|
+
|
|
88
119
|
## Mark a decorative canvas
|
|
89
120
|
|
|
90
121
|
A scene that only decorates gets `aria-hidden="true"`, so screen readers skip it. It takes no keyboard focus. Give it `pointer-events: none` when it does not react to the pointer, so clicks reach the page. [Accessibility](accessibility.md) covers a canvas the visitor operates.
|
|
@@ -124,51 +155,77 @@ When the page also has a pause button, or pauses for reduced motion, combine the
|
|
|
124
155
|
|
|
125
156
|
## Drive the camera from the scroll position
|
|
126
157
|
|
|
127
|
-
The page reads the scroll position, and the sketch moves the camera.
|
|
158
|
+
The page reads the scroll position, and the sketch moves the camera. Pass the first position as [start data](../api/page.md#start-data), so the first frame already shows it. A held frame of a page that opens partway down then shows it too. After the start, send the position only when it changes, at most once per animation frame:
|
|
128
159
|
|
|
129
160
|
```ts
|
|
130
161
|
// page.ts
|
|
162
|
+
const progress = () => {
|
|
163
|
+
const range = Math.max(1, document.documentElement.scrollHeight - window.innerHeight);
|
|
164
|
+
return Math.min(1, Math.max(0, window.scrollY / range));
|
|
165
|
+
};
|
|
166
|
+
const engine = await createEngine({
|
|
167
|
+
canvas,
|
|
168
|
+
sketch: new URL('./sketch.ts', import.meta.url),
|
|
169
|
+
sketchData: { scroll: progress() }, // the first frame's position
|
|
170
|
+
});
|
|
131
171
|
let queued = false;
|
|
132
172
|
const sendScroll = () => {
|
|
133
173
|
queued = false;
|
|
134
|
-
|
|
135
|
-
const progress = window.scrollY / range;
|
|
136
|
-
engine.postToSketch('scroll', Math.min(1, Math.max(0, progress)));
|
|
174
|
+
engine.postToSketch('scroll', progress());
|
|
137
175
|
};
|
|
138
176
|
window.addEventListener('scroll', () => {
|
|
139
177
|
if (!queued) requestAnimationFrame(sendScroll);
|
|
140
178
|
queued = true;
|
|
141
179
|
}, { passive: true });
|
|
142
|
-
sendScroll(); //
|
|
180
|
+
sendScroll(); // a position that the browser restored during the start
|
|
143
181
|
```
|
|
144
182
|
|
|
145
|
-
The sketch eases toward
|
|
183
|
+
The sketch starts at the start data's position, and eases toward each later one. Its easing factor depends on the frame's time step, so the motion looks the same at 60 and at 144 frames per second. The browser can restore the scroll position after the start, so the sketch jumps to the first position it receives. A page that opens scrolled down then never shows a camera flight. The page reveals the canvas once `engine.firstFrame` resolves:
|
|
146
184
|
|
|
147
185
|
```ts
|
|
148
186
|
// sketch.ts
|
|
149
|
-
|
|
150
|
-
let
|
|
151
|
-
let
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
187
|
+
export default defineSketch<{ scroll: number }>(({ data, page, preferences }) => {
|
|
188
|
+
let target = data?.scroll ?? 0;
|
|
189
|
+
let current = target;
|
|
190
|
+
let placed = false;
|
|
191
|
+
let toldPage = false;
|
|
192
|
+
page.onMessage((type, value) => {
|
|
193
|
+
if (type !== 'scroll') return;
|
|
194
|
+
target = value as number;
|
|
195
|
+
if (!placed) current = target;
|
|
157
196
|
placed = true;
|
|
158
|
-
|
|
159
|
-
|
|
197
|
+
});
|
|
198
|
+
return {
|
|
199
|
+
onUpdate(dt) {
|
|
200
|
+
// Closes 99.9% of the gap in one second, at any frame rate.
|
|
201
|
+
const k = preferences.reducedMotion ? 1 : 1 - Math.pow(0.001, dt);
|
|
202
|
+
current += (target - current) * k;
|
|
203
|
+
if (Math.abs(target - current) < 0.0001) current = target;
|
|
204
|
+
placeCamera(current);
|
|
205
|
+
if (!toldPage) {
|
|
206
|
+
toldPage = true;
|
|
207
|
+
page.post('placed');
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
};
|
|
160
211
|
});
|
|
161
|
-
return {
|
|
162
|
-
onUpdate(dt) {
|
|
163
|
-
// Closes 99.9% of the gap in one second, at any frame rate.
|
|
164
|
-
const k = preferences.reducedMotion ? 1 : 1 - Math.pow(0.001, dt);
|
|
165
|
-
current += (target - current) * k;
|
|
166
|
-
if (Math.abs(target - current) < 0.0001) current = target;
|
|
167
|
-
placeCamera(current);
|
|
168
|
-
},
|
|
169
|
-
};
|
|
170
212
|
```
|
|
171
213
|
|
|
214
|
+
The sketch posts `placed` once, after it first places the camera. The page then waits for both that message and the first frame:
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
// page.ts
|
|
218
|
+
const placed = new Promise<void>((resolve) =>
|
|
219
|
+
engine.onSketchMessage((type) => { if (type === 'placed') resolve(); }),
|
|
220
|
+
);
|
|
221
|
+
await Promise.all([engine.firstFrame, placed]);
|
|
222
|
+
root.classList.add('ready');
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
A page without scroll-driven cameras awaits only `engine.firstFrame`.
|
|
226
|
+
|
|
227
|
+
A canvas behind the page's text often wants its subject beside the text, not under it. Shift the lens rather than turn the camera: `camera.setLensShift(-0.25, 0)` puts the camera's target three quarters of the way across, and upright lines stay upright. A section can change the shift as the scroll moves, in the same `onUpdate` ([Cameras](../api/cameras.md#lens-shift)).
|
|
228
|
+
|
|
172
229
|
## Second visits and navigation
|
|
173
230
|
|
|
174
231
|
| Lifetime | What keeps it | How |
|