@null3d/engine 0.0.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/README.md +109 -0
- package/THIRD-PARTY-NOTICES.txt +0 -1
- package/dist/wasm/single/null3d.js +25 -10
- package/dist/wasm/single/null3d_bg.wasm +0 -0
- package/dist/wasm/threaded/null3d.js +25 -10
- package/dist/wasm/threaded/null3d_bg.wasm +0 -0
- package/docs/api/animation.md +114 -86
- package/docs/api/assets.md +135 -49
- package/docs/api/cameras.md +45 -12
- package/docs/api/controls.md +88 -62
- package/docs/api/debug.md +116 -41
- package/docs/api/engine.md +117 -35
- package/docs/api/feature-draws.md +23 -17
- package/docs/api/feature-effects.md +11 -11
- package/docs/api/feature-instances.md +70 -19
- package/docs/api/features.md +77 -47
- package/docs/api/geometry.md +132 -82
- package/docs/api/grass.md +55 -13
- package/docs/api/impostors.md +175 -22
- package/docs/api/input.md +24 -18
- package/docs/api/lights.md +78 -51
- package/docs/api/lines.md +41 -33
- package/docs/api/materials.md +248 -121
- package/docs/api/math.md +12 -13
- package/docs/api/objects.md +47 -22
- package/docs/api/page.md +58 -7
- package/docs/api/particles.md +33 -20
- package/docs/api/points.md +19 -31
- package/docs/api/post.md +75 -42
- package/docs/api/quality.md +42 -17
- package/docs/api/raycast.md +65 -51
- package/docs/api/reference/animation.md +10 -11
- package/docs/api/reference/assets.md +25 -22
- package/docs/api/reference/cameras.md +8 -4
- package/docs/api/reference/controls.md +134 -25
- package/docs/api/reference/debug.md +81 -14
- package/docs/api/reference/engine.md +58 -17
- 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 +287 -97
- package/docs/api/reference/grass.md +63 -3
- package/docs/api/reference/impostors.md +208 -13
- package/docs/api/reference/input.md +4 -5
- package/docs/api/reference/lights.md +13 -12
- package/docs/api/reference/lines.md +10 -11
- package/docs/api/reference/materials.md +54 -53
- package/docs/api/reference/math.md +35 -24
- package/docs/api/reference/objects.md +11 -12
- package/docs/api/reference/particles.md +31 -6
- package/docs/api/reference/points.md +6 -7
- package/docs/api/reference/post.md +21 -20
- package/docs/api/reference/quality.md +8 -5
- package/docs/api/reference/raycast.md +7 -8
- package/docs/api/reference/render.md +5 -4
- package/docs/api/reference/scene.md +52 -30
- package/docs/api/reference/sketch.md +15 -11
- package/docs/api/reference/sound.md +234 -0
- package/docs/api/reference/splats.md +42 -3
- package/docs/api/reference/sprites.md +7 -8
- package/docs/api/reference/tasks.md +16 -3
- package/docs/api/reference/terrain.md +27 -5
- package/docs/api/reference/textures.md +5 -6
- package/docs/api/reference/three-compat.md +18 -7
- package/docs/api/reference/time.md +1 -2
- package/docs/api/reference/ui.md +12 -9
- package/docs/api/reference/vertex-draws.md +5 -6
- package/docs/api/reference/water.md +153 -9
- package/docs/api/reference/wind.md +17 -4
- package/docs/api/render.md +73 -29
- package/docs/api/scene.md +164 -60
- package/docs/api/sketch.md +102 -15
- package/docs/api/sound.md +205 -0
- package/docs/api/splats.md +46 -12
- package/docs/api/sprites.md +10 -24
- package/docs/api/tasks.md +33 -24
- package/docs/api/terrain.md +42 -22
- package/docs/api/textures.md +68 -19
- package/docs/api/three-compat.md +15 -7
- package/docs/api/time.md +3 -5
- package/docs/api/ui.md +19 -23
- package/docs/api/vertex-draws.md +13 -12
- package/docs/api/water.md +262 -35
- package/docs/api/wind.md +63 -17
- package/docs/cli/null3d.md +177 -27
- package/docs/concepts/architecture.md +73 -27
- package/docs/concepts/assets.md +57 -33
- package/docs/concepts/backends.md +69 -27
- package/docs/concepts/color-management.md +54 -36
- package/docs/concepts/culling.md +68 -47
- package/docs/concepts/handles.md +14 -5
- package/docs/concepts/instances.md +74 -25
- package/docs/concepts/large-worlds.md +18 -8
- package/docs/concepts/lighting.md +155 -87
- package/docs/concepts/lod.md +126 -44
- package/docs/concepts/materials.md +58 -27
- package/docs/concepts/particle-buffer.md +48 -35
- package/docs/concepts/post-processing.md +84 -43
- package/docs/concepts/quality-preset-tables.md +56 -52
- package/docs/concepts/quality-presets.md +164 -52
- package/docs/concepts/render-graph.md +69 -29
- package/docs/concepts/render-layers.md +11 -21
- package/docs/concepts/shadows.md +105 -52
- package/docs/concepts/static-dynamic.md +22 -7
- package/docs/cookbook/index.md +164 -120
- package/docs/data/threejs-mapping.json +362 -367
- 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 +14 -6
- 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 +40 -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 +14 -6
- 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 +54 -30
- package/docs/getting-started/cdn.md +32 -10
- package/docs/getting-started/first-scene.md +21 -13
- package/docs/getting-started/hosting.md +152 -63
- package/docs/getting-started/install.md +44 -28
- package/docs/getting-started/network-use.md +33 -15
- package/docs/getting-started/project-structure.md +33 -12
- package/docs/getting-started/third-party-notices.md +12 -14
- package/docs/guides/accessibility.md +5 -5
- package/docs/guides/add-ons.md +86 -15
- package/docs/guides/agents.md +16 -9
- package/docs/guides/assets-pipeline.md +259 -82
- package/docs/guides/audio.md +115 -128
- package/docs/guides/baked-light-data.md +44 -23
- package/docs/guides/car-paint.md +38 -32
- package/docs/guides/content-pages.md +63 -35
- package/docs/guides/custom-passes.md +65 -22
- package/docs/guides/custom-shaders.md +59 -39
- package/docs/guides/debugging.md +74 -10
- package/docs/guides/deploying.md +85 -6
- package/docs/guides/deterministic-simulation.md +24 -17
- package/docs/guides/first-use-shaders.md +83 -45
- package/docs/guides/lens-kit.md +29 -26
- package/docs/guides/loading-screens.md +132 -47
- package/docs/guides/multiple-views.md +63 -13
- package/docs/guides/performance.md +192 -38
- package/docs/guides/phones.md +78 -15
- package/docs/guides/physics.md +7 -11
- package/docs/guides/screens.md +47 -33
- package/docs/guides/start-time.md +363 -0
- package/docs/guides/testing.md +152 -95
- package/docs/guides/ui-overlays.md +19 -14
- 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 +18 -23
- package/docs/moves.json +1 -0
- package/docs/nav.json +300 -0
- package/docs/pages.md +230 -209
- package/docs/porting/react-three-fiber.md +225 -6
- package/docs/porting/threejs-loop-and-threads.md +217 -7
- package/docs/porting/threejs-mapping.md +1249 -233
- package/docs/porting/threejs-materials.md +218 -6
- package/docs/porting/threejs-overview.md +240 -7
- package/docs/porting/threejs-postprocessing.md +4 -8
- package/docs/porting/threejs-shaders.md +301 -6
- package/docs/porting/threejs-unsupported.md +83 -7
- package/docs/porting/verification.md +178 -6
- package/docs/shaders/builtins.md +15 -4
- package/docs/shaders/library.md +72 -45
- package/docs/shaders/surface-functions.md +59 -23
- package/docs/shaders/wgsl-rules.md +12 -5
- package/lib/debug/channel.d.ts +103 -0
- package/lib/debug/channel.js +269 -0
- package/lib/debug/debug.d.ts +11 -9
- package/lib/debug/draw.d.ts +11 -2
- package/lib/debug/draw.js +57 -6
- package/lib/debug/inspector.d.ts +60 -0
- package/lib/debug/inspector.js +475 -0
- 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 +22 -7
- package/lib/debug/stats-panel.js +80 -13
- package/lib/debug/stats.d.ts +2 -3
- package/lib/errors/codes.d.ts +17 -3
- package/lib/errors/codes.js +117 -105
- package/lib/errors/engine-error.js +25 -10
- package/lib/errors/fixes.d.ts +1 -0
- package/lib/errors/fixes.js +20 -19
- package/lib/generated/core.d.ts +1 -1
- package/lib/generated/core.js +1 -1
- package/lib/gpu/early-builds.d.ts +68 -0
- package/lib/gpu/early-builds.js +158 -0
- package/lib/gpu/pipeline-name.d.ts +6 -0
- package/lib/gpu/pipeline-name.js +23 -0
- package/lib/gpu/variants.d.ts +8 -2
- package/lib/gpu/variants.js +39 -7
- package/lib/gpu/webgl2/backend.d.ts +63 -2
- package/lib/gpu/webgl2/backend.js +226 -8
- package/lib/gpu/webgl2/programs.d.ts +18 -0
- package/lib/gpu/webgl2/programs.js +5 -2
- package/lib/gpu/webgpu/backend.d.ts +89 -2
- package/lib/gpu/webgpu/backend.js +255 -8
- package/lib/gpu/webgpu/pipelines.d.ts +13 -0
- package/lib/gpu/webgpu/pipelines.js +28 -14
- package/lib/index.d.ts +7 -5
- package/lib/index.js +3 -2
- package/lib/math/color.d.ts +5 -5
- package/lib/math/color.js +5 -5
- package/lib/math/mat4.d.ts +6 -12
- package/lib/math/mat4.js +6 -12
- package/lib/math/namespaces/quat.d.ts +3 -3
- package/lib/math/namespaces/quat.js +3 -3
- package/lib/math/quat.d.ts +8 -12
- package/lib/math/quat.js +8 -12
- package/lib/math/random.d.ts +2 -3
- package/lib/math/random.js +2 -3
- package/lib/math/types.d.ts +4 -4
- package/lib/math/vec3.d.ts +3 -6
- package/lib/math/vec3.js +3 -6
- package/lib/page/capabilities.d.ts +8 -8
- package/lib/page/capabilities.js +39 -25
- package/lib/page/check-store.d.ts +14 -2
- package/lib/page/check-store.js +34 -11
- package/lib/page/engine.d.ts +117 -31
- package/lib/page/engine.js +438 -99
- package/lib/page/first-frame.d.ts +16 -0
- package/lib/page/first-frame.js +35 -0
- package/lib/page/frame-stats.d.ts +34 -1
- package/lib/page/frame-stats.js +15 -0
- package/lib/page/label-loop.d.ts +7 -3
- package/lib/page/label-loop.js +26 -14
- package/lib/page/labels.d.ts +3 -3
- package/lib/page/pipeline-store.d.ts +25 -0
- package/lib/page/pipeline-store.js +84 -0
- package/lib/page/start-marker.d.ts +4 -1
- package/lib/page/start-marker.js +5 -2
- package/lib/page/stats-switch.js +2 -0
- package/lib/page/switches.d.ts +38 -6
- package/lib/page/switches.js +29 -5
- package/lib/quality/check.d.ts +24 -3
- package/lib/quality/check.js +21 -7
- package/lib/quality/chooser.d.ts +6 -0
- package/lib/quality/chooser.js +8 -0
- package/lib/quality/governor.d.ts +98 -6
- package/lib/quality/governor.js +250 -20
- package/lib/quality/preset-docs.d.ts +16 -1
- package/lib/quality/preset-docs.js +3 -0
- package/lib/quality/presets.d.ts +84 -7
- package/lib/quality/presets.js +94 -2
- package/lib/render/draw.d.ts +3 -2
- package/lib/render/draw.js +15 -5
- package/lib/render/load-draw.d.ts +7 -5
- package/lib/render/load-draw.js +7 -5
- package/lib/render/loss.d.ts +28 -10
- package/lib/render/loss.js +50 -22
- package/lib/render/recovery.d.ts +20 -0
- package/lib/render/recovery.js +44 -4
- package/lib/render/renderer.d.ts +85 -9
- package/lib/render/renderer.js +181 -42
- package/lib/render/scene-renderer.d.ts +19 -1
- package/lib/render/scene-renderer.js +25 -1
- package/lib/render/webgl2-renderers.js +8 -5
- package/lib/render/webgpu-renderers.js +5 -4
- package/lib/scene/animation.d.ts +28 -32
- package/lib/scene/animation.js +12 -15
- package/lib/scene/assets.d.ts +61 -86
- package/lib/scene/assets.js +103 -236
- package/lib/scene/background.d.ts +14 -18
- package/lib/scene/background.js +5 -2
- package/lib/scene/baked-light.d.ts +2 -2
- package/lib/scene/color.d.ts +3 -3
- package/lib/scene/downloads.d.ts +2 -0
- package/lib/scene/downloads.js +371 -0
- package/lib/scene/environment.d.ts +7 -7
- package/lib/scene/environment.js +1 -1
- package/lib/scene/fake-call-values.js +3 -3
- package/lib/scene/fog.d.ts +11 -4
- package/lib/scene/fog.js +8 -2
- package/lib/scene/frame-cameras.d.ts +35 -4
- package/lib/scene/frame-cameras.js +86 -10
- package/lib/scene/gltf.d.ts +33 -3
- package/lib/scene/gltf.js +78 -49
- package/lib/scene/grading.d.ts +2 -2
- package/lib/scene/grading.js +3 -3
- package/lib/scene/ktx2.d.ts +9 -1
- package/lib/scene/ktx2.js +3 -2
- package/lib/scene/level-rule.d.ts +121 -0
- package/lib/scene/level-rule.js +130 -0
- package/lib/scene/lines.d.ts +13 -17
- package/lib/scene/lines.js +3 -3
- package/lib/scene/lut.d.ts +1 -2
- package/lib/scene/lut.js +1 -2
- package/lib/scene/particles.d.ts +7 -3
- package/lib/scene/particles.js +3 -1
- package/lib/scene/picks.d.ts +2 -2
- package/lib/scene/pointer-events.d.ts +4 -7
- package/lib/scene/post.d.ts +39 -43
- package/lib/scene/post.js +4 -1
- package/lib/scene/prefab.d.ts +7 -8
- package/lib/scene/prefab.js +7 -8
- package/lib/scene/property-animation.d.ts +1 -1
- package/lib/scene/property-animation.js +1 -1
- package/lib/scene/queries.d.ts +7 -13
- package/lib/scene/resources.d.ts +238 -261
- package/lib/scene/resources.js +179 -81
- package/lib/scene/scene.d.ts +167 -120
- package/lib/scene/scene.js +243 -103
- package/lib/scene/serial-loads.d.ts +29 -0
- package/lib/scene/serial-loads.js +68 -0
- package/lib/scene/shader-preloads.d.ts +34 -0
- package/lib/scene/shader-preloads.js +38 -0
- package/lib/scene/sprites.d.ts +14 -17
- package/lib/scene/sprites.js +5 -6
- package/lib/scene/textures.d.ts +11 -12
- package/lib/scene/textures.js +9 -10
- package/lib/scene/time-of-day.d.ts +11 -7
- package/lib/scene/time-of-day.js +11 -8
- package/lib/scene/vertex-draws.d.ts +5 -4
- package/lib/scene/vertex-draws.js +7 -5
- package/lib/shader-artifact.json +1 -0
- package/lib/shared/core.d.ts +5 -0
- package/lib/shared/core.js +1 -0
- package/lib/shared/import-once.d.ts +6 -0
- package/lib/shared/import-once.js +19 -0
- package/lib/shared/labels.d.ts +5 -2
- package/lib/shared/labels.js +5 -2
- package/lib/shared/metrics.d.ts +38 -0
- package/lib/shared/metrics.js +62 -1
- 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/run-task.js +7 -1
- package/lib/shared/sketch-data.d.ts +9 -0
- package/lib/shared/sketch-data.js +57 -0
- package/lib/shared/task-pending.d.ts +12 -0
- package/lib/shared/task-pending.js +61 -1
- package/lib/shared/tasks.js +79 -32
- package/lib/shared/version.d.ts +17 -1
- package/lib/shared/version.js +19 -1
- package/lib/shared/wasm.d.ts +12 -4
- package/lib/shared/wasm.js +50 -27
- package/lib/shared/worker-start.d.ts +7 -0
- package/lib/shared/worker-start.js +27 -0
- package/lib/sketch/define-sketch.d.ts +51 -7
- package/lib/sketch/define-sketch.js +2 -1
- package/lib/sketch/input.d.ts +2 -2
- package/lib/sketch/preset-check.d.ts +20 -6
- package/lib/sketch/preset-check.js +42 -17
- package/lib/sketch/quality.d.ts +69 -11
- package/lib/sketch/quality.js +54 -13
- package/lib/sketch/runner.d.ts +80 -9
- package/lib/sketch/runner.js +390 -52
- package/lib/sketch/ui.d.ts +5 -8
- package/lib/sketch/ui.js +4 -2
- package/lib/stats.d.ts +1 -0
- package/lib/workers/probe-worker.d.ts +5 -0
- package/lib/workers/probe-worker.js +13 -4
- package/lib/workers/protocol.d.ts +71 -13
- package/lib/workers/protocol.js +4 -0
- package/lib/workers/render-worker.js +29 -5
- package/lib/workers/sketch-worker.js +108 -16
- package/package.json +2 -2
|
@@ -1,15 +1,234 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: porting/react-three-fiber
|
|
3
3
|
title: Porting React Three Fiber
|
|
4
|
-
status: planned
|
|
5
|
-
since: "0.3"
|
|
6
4
|
summary: "Canvas, useFrame, drei helpers; keeping React for the page UI."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
|
-
<!-- null3d:placeholder -->
|
|
10
|
-
|
|
11
7
|
# Porting React Three Fiber
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
React Three Fiber describes a three.js scene as React components that run on the main thread. In null3D, the scene lives in a sketch that runs in a worker. React keeps the page UI. A port moves the component tree inside `<Canvas>` into sketch code, and connects React to the sketch with messages.
|
|
10
|
+
|
|
11
|
+
```mermaid
|
|
12
|
+
flowchart LR
|
|
13
|
+
subgraph page["Page: React"]
|
|
14
|
+
ui["UI, forms, menus"]
|
|
15
|
+
view["A view component<br/>that holds the canvas"]
|
|
16
|
+
html["HTML of the former<br/>drei Html elements"]
|
|
17
|
+
end
|
|
18
|
+
subgraph sketch["Sketch worker"]
|
|
19
|
+
scene["The former Canvas contents:<br/>setup and onUpdate"]
|
|
20
|
+
labels["ui.trackLabel"]
|
|
21
|
+
end
|
|
22
|
+
ui -- "engine.postToSketch" --> scene
|
|
23
|
+
scene -- "page.post" --> ui
|
|
24
|
+
labels -- "positions" --> html
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The React tree above the former `<Canvas>` barely changes. The subtree inside `<Canvas>` becomes the sketch.
|
|
28
|
+
|
|
29
|
+
## A view component
|
|
30
|
+
|
|
31
|
+
This component holds one engine for each sketch, while the app shows other views. Leaving the view takes the canvas off the page and pauses the engine. Coming back puts it on the page again, with no new start.
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { useEffect, useRef } from 'react';
|
|
35
|
+
import { createEngine, type Engine } from '@null3d/engine';
|
|
36
|
+
|
|
37
|
+
type Props = {
|
|
38
|
+
sketch: URL;
|
|
39
|
+
onMessage?: (name: string, data: unknown) => void;
|
|
40
|
+
onReady?: (engine: Engine) => void;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** How long an engine waits off the page before the component destroys it. */
|
|
44
|
+
const KEEP_MS = 60_000;
|
|
45
|
+
|
|
46
|
+
type Kept = { canvas: HTMLCanvasElement; engine: Promise<Engine>; timer?: number };
|
|
47
|
+
const kept = new Map<string, Kept>();
|
|
48
|
+
|
|
49
|
+
function keptEngine(sketch: URL): Kept {
|
|
50
|
+
let entry = kept.get(sketch.href);
|
|
51
|
+
if (!entry) {
|
|
52
|
+
// The kept engine owns its canvas, which moves between hosts with the engine.
|
|
53
|
+
const canvas = document.createElement('canvas');
|
|
54
|
+
canvas.style.cssText = 'width:100%;height:100%;display:block';
|
|
55
|
+
entry = { canvas, engine: createEngine({ canvas, sketch }) };
|
|
56
|
+
entry.engine.catch(() => kept.delete(sketch.href));
|
|
57
|
+
kept.set(sketch.href, entry);
|
|
58
|
+
}
|
|
59
|
+
clearTimeout(entry.timer);
|
|
60
|
+
return entry;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function SceneView({ sketch, onMessage, onReady }: Props) {
|
|
64
|
+
const hostRef = useRef<HTMLDivElement>(null);
|
|
65
|
+
const onMessageRef = useRef(onMessage);
|
|
66
|
+
onMessageRef.current = onMessage;
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
const host = hostRef.current!;
|
|
70
|
+
const entry = keptEngine(sketch);
|
|
71
|
+
let mounted = true;
|
|
72
|
+
let off: (() => void) | undefined;
|
|
73
|
+
host.append(entry.canvas);
|
|
74
|
+
entry.engine.then(
|
|
75
|
+
(engine) => {
|
|
76
|
+
if (!mounted) return;
|
|
77
|
+
engine.attach(host);
|
|
78
|
+
off = engine.onSketchMessage((name, data) => onMessageRef.current?.(name, data));
|
|
79
|
+
onReady?.(engine);
|
|
80
|
+
},
|
|
81
|
+
(error) => console.error(error),
|
|
82
|
+
);
|
|
83
|
+
return () => {
|
|
84
|
+
mounted = false;
|
|
85
|
+
off?.();
|
|
86
|
+
entry.engine.then((engine) => engine.detach(), () => {});
|
|
87
|
+
entry.timer = window.setTimeout(() => {
|
|
88
|
+
kept.delete(sketch.href);
|
|
89
|
+
entry.engine.then((engine) => engine.destroy(), () => {});
|
|
90
|
+
}, KEEP_MS);
|
|
91
|
+
};
|
|
92
|
+
}, [sketch]);
|
|
93
|
+
|
|
94
|
+
return <div ref={hostRef} style={{ width: '100%', height: '100%' }} />;
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
- Make the sketch's address once, at module level: `const sketchUrl = new URL('./sketch.ts', import.meta.url)`. A new address on each render would start a new engine.
|
|
99
|
+
- React's StrictMode runs effects twice in development. The second run finds the kept engine and attaches it, so development starts one engine, as production does.
|
|
100
|
+
- Each mount adds its own message handler and removes it when it unmounts. An old component never hears the sketch.
|
|
101
|
+
- A kept engine holds its memory and GPU buffers. For a view that the app shows once, destroy the engine when the component unmounts instead.
|
|
102
|
+
|
|
103
|
+
A simpler component also works. It renders a `<canvas>`, creates the engine in its effect, and calls `destroy()` in the cleanup. Under StrictMode, the second start waits for the first engine to stop, then draws on the same canvas. [Page API: createEngine](../api/engine.md) describes `attach`, `detach` and `destroy`.
|
|
104
|
+
|
|
105
|
+
## State between React and the sketch
|
|
106
|
+
|
|
107
|
+
React sends a change to the sketch when its state changes:
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
const [color, setColor] = useState('#c0392b');
|
|
111
|
+
const engineRef = useRef<Engine>();
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
engineRef.current?.postToSketch('config', { color });
|
|
114
|
+
}, [color]);
|
|
115
|
+
|
|
116
|
+
// <SceneView sketch={sketchUrl} onReady={(e) => { engineRef.current = e; e.postToSketch('config', { color }); }} />
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The sketch applies the change, and sends events back:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
// sketch.ts
|
|
123
|
+
page.onMessage((name, data) => {
|
|
124
|
+
if (name === 'config') bodyMaterial.set({ color: (data as { color: string }).color });
|
|
125
|
+
});
|
|
126
|
+
sole.on('click', () => page.post('part-selected', { name: 'sole' }));
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
<SceneView
|
|
131
|
+
sketch={sketchUrl}
|
|
132
|
+
onMessage={(name, data) => {
|
|
133
|
+
if (name === 'part-selected') setSelected((data as { name: string }).name);
|
|
134
|
+
}}
|
|
135
|
+
/>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Never copy per-frame scene state into React state. React would then render again in every frame, on the main thread.
|
|
139
|
+
|
|
140
|
+
## React Three Fiber and drei in null3D
|
|
141
|
+
|
|
142
|
+
| React Three Fiber or drei | null3D |
|
|
143
|
+
| --- | --- |
|
|
144
|
+
| `<Canvas camera={{ position, fov }}>` | `scene.createPerspectiveCamera({ position, fov })` in the sketch |
|
|
145
|
+
| `<Canvas dpr={[1, 2]}>` | `createEngine({ maxPixelRatio: 2 })`, or the quality preset's cap |
|
|
146
|
+
| `<Canvas shadows>` | `castShadows` on lights and meshes, and `receiveShadows` on the surfaces that show shadows |
|
|
147
|
+
| `<Canvas gl={{ antialias, alpha }}>` | `createEngine({ antialias: 'msaa' })` and `createEngine({ transparent: true })` |
|
|
148
|
+
| `<Canvas frameloop="demand">` | `engine.setPaused(true)` while nothing changes |
|
|
149
|
+
| `<mesh>` with `<boxGeometry>` and `<meshStandardMaterial>` | `scene.createMesh({ mesh: geometry.box(), material: materials.standard() })` |
|
|
150
|
+
| `<group>` | `scene.createGroup()` and `setParent` |
|
|
151
|
+
| `<primitive object={gltf.scene} />` | `scene.instantiate(prefab)` |
|
|
152
|
+
| `useFrame((state, delta) => ...)` | `onUpdate(dt)` |
|
|
153
|
+
| `useThree()` | The sketch's context: `scene`, and `ctx.engine.viewport` for the size |
|
|
154
|
+
| `useLoader(GLTFLoader, url)` and drei `useGLTF(url)` | `await assets.loadGltf(url)` |
|
|
155
|
+
| `useGLTF.preload(url)` and `<Preload all />` | `assets.preload([...])` and `scene.warmUp()` |
|
|
156
|
+
| `<Suspense fallback>`, drei `<Loader>` and `useProgress` | `assets.onProgress` and a message; the page shows the loader |
|
|
157
|
+
| drei `useAnimations(animations, ref)` | `object.animator()` |
|
|
158
|
+
| drei `<OrbitControls makeDefault />` | `createOrbitControls(ctx, camera, options)` from `@null3d/controls` |
|
|
159
|
+
| drei `<Environment preset="studio" />` | An environment map that `bunx @null3d/cli assets env` makes from the preset's HDR file, or `assets.builtinEnvironment('room')` for neutral light |
|
|
160
|
+
| drei `<Environment files="x.hdr" background />` | `scene.setEnvironment(await assets.loadEnvironment('/x.hdr'))` and `scene.setBackground` |
|
|
161
|
+
| drei `<ContactShadows />` | `materials.shadowCatcher` on a ground plane, which shows the directional light's shadow |
|
|
162
|
+
| drei `<Html>` | `ui.trackLabel` in the sketch, and `engine.labels.bind` on the page with the HTML that React renders |
|
|
163
|
+
| drei `<Text>` and `<Text3D>` | HTML labels, a texture with the text drawn in it, or a text mesh baked into glTF |
|
|
164
|
+
| drei `<Instances>`, `<Instance>` and `<Merged>` | `scene.createInstances` |
|
|
165
|
+
| drei `<Sky>` | `scene.setBackground({ sky: { sunPosition, turbidity, rayleigh } })`, with the same props |
|
|
166
|
+
| drei `<Stars>` | `scene.createPoints` |
|
|
167
|
+
| drei `<Float>` | A sine offset in `onUpdate` |
|
|
168
|
+
| drei `<Center>` and `<Bounds>` | `prefab.bounds`, and a camera fit computed at setup |
|
|
169
|
+
| drei `<Stats>` | `debug.stats(true)` in the sketch, or `createEngine({ stats: true })` on the page |
|
|
170
|
+
| Mesh events: `onClick`, `onPointerDown`, `onPointerUp`, `onPointerMove`, `onPointerOver`, `onPointerOut` | `object.on('click' \| 'pointerdown' \| 'pointerup' \| 'pointermove' \| 'pointerenter' \| 'pointerleave', fn)`, then `page.post` if React needs to know |
|
|
171
|
+
| `@react-three/postprocessing` | `post.set`, as [Porting post-processing](threejs-postprocessing.md) shows |
|
|
172
|
+
| `@react-three/rapier` | Rapier in the sketch worker, as [Using a physics library](../guides/physics.md) shows |
|
|
173
|
+
| Props that React changes in every frame | Logic in `onUpdate`; React sends intent, not frames |
|
|
174
|
+
|
|
175
|
+
Some parts behave differently:
|
|
176
|
+
|
|
177
|
+
- Only the closest object and its parents get a pointer event. React Three Fiber also passes the event to the objects behind it.
|
|
178
|
+
- `onPointerMissed` has no equivalent. Set a flag in the click handlers, and treat a release of the button without it as a miss.
|
|
179
|
+
- `materials.shadowCatcher` shows the light's own shadow. drei's `<ContactShadows />` draws a blurred shadow from above, and null3D has no equivalent of that blur. null3D's own [contact shadows](../concepts/shadows.md#contact-shadows) are another effect: the short dark line where an object meets the ground, cast toward the main directional light.
|
|
180
|
+
|
|
181
|
+
## Adaptive quality
|
|
182
|
+
|
|
183
|
+
drei's `<PerformanceMonitor>`, `<AdaptiveDpr>` and `<AdaptiveEvents>` measure the frame rate on the main thread, and the app lowers the pixel ratio. null3D has this built in. Its frame-budget governor runs in the engine on every page, with no component ([Quality presets](../concepts/quality-presets.md)). Remove the drei components, and do not build your own frame timer, because it would fight the governor.
|
|
184
|
+
|
|
185
|
+
| drei | null3D |
|
|
186
|
+
| --- | --- |
|
|
187
|
+
| `<PerformanceMonitor onDecline onIncline>` that sets `dpr` | The governor lowers the render scale when frames run over budget for about a second. It raises it after about 5 seconds with time to spare. |
|
|
188
|
+
| `onChange={({ factor }) => setDpr(...)}` | `quality.set({ minRenderScale, maxRenderScale })` sets the range that the render scale moves in. `quality.renderScale` reads the current scale. |
|
|
189
|
+
| `onDecline` handlers that cut particles or effects | `quality.setBudget({ name, ms, min, onScale })`. The governor lowers the budget's scale in steps of 0.25, one before each step of the render scale, and `onScale` sets the count from the scale. `quality.onChange` runs after each step past the render scale. |
|
|
190
|
+
| `onFallback` after `flipflops` | Nothing. A step up that fails doubles the governor's wait before it tries again, so quality settles. |
|
|
191
|
+
| `<AdaptiveDpr pixelated />` with `performance.regress()` | No equivalent. The governor follows frame times, not camera movement. Remove the `regress()` calls. |
|
|
192
|
+
| `<AdaptiveEvents />` | Nothing. null3D casts no pointer rays on the main thread. |
|
|
193
|
+
|
|
194
|
+
The governor judges each window by the slower of two rates: the frames presented, and the frames that the GPU finished. A frame counter on the main thread sees only the first, so it misses a GPU that falls behind.
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
// The app used <PerformanceMonitor onChange={({ factor }) => setDpr(0.5 + 1.5 * factor)} />.
|
|
198
|
+
// The cap of 2 moves to createEngine({ maxPixelRatio: 2 }) on the page.
|
|
199
|
+
quality.set({ minRenderScale: 0.5, maxRenderScale: 1 });
|
|
200
|
+
const SPARKS = { low: 500, medium: 1000, high: 2000, ultra: 4000 };
|
|
201
|
+
let sparkScale = 1;
|
|
202
|
+
const sparkCount = () => Math.round(SPARKS[quality.preset] * sparkScale);
|
|
203
|
+
quality.setBudget({
|
|
204
|
+
name: 'sparks',
|
|
205
|
+
ms: 1,
|
|
206
|
+
min: 0.5,
|
|
207
|
+
onScale: (scale) => {
|
|
208
|
+
sparkScale = scale;
|
|
209
|
+
sparks.setActiveCount(sparkCount());
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
quality.onChange(() => sparks.setActiveCount(sparkCount())); // a new preset
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
To measure the scene's own cost while you port it, turn the governor off with `quality.set({ governor: false })`. Turn it on again before you ship.
|
|
216
|
+
|
|
217
|
+
## Step by step
|
|
218
|
+
|
|
219
|
+
1. List every component inside `<Canvas>` with its props, and mark the props that come from React state.
|
|
220
|
+
2. Write a sketch that creates the same objects at setup. Props that React changes become `page.onMessage` handlers.
|
|
221
|
+
3. Move the `useFrame` bodies into `onUpdate`, and turn refs into handles or batch rows.
|
|
222
|
+
4. Replace `<Canvas>` with the view component.
|
|
223
|
+
5. Route the events: mesh events become `object.on(...)` in the sketch, and `page.post` tells React.
|
|
224
|
+
6. Replace each drei `<Html>` with a tracked label, and let React render its HTML on the page.
|
|
225
|
+
7. Compare images of each camera view, then measure the timings ([Verifying a port](verification.md)).
|
|
226
|
+
|
|
227
|
+
## Related pages
|
|
14
228
|
|
|
15
|
-
|
|
229
|
+
- [Porting from three.js](threejs-overview.md)
|
|
230
|
+
- [The render loop, threads and the DOM](threejs-loop-and-threads.md)
|
|
231
|
+
- [Messages between sketch and page](../api/page.md)
|
|
232
|
+
- [HTML UI around the scene](../guides/ui-overlays.md)
|
|
233
|
+
- [Quality presets](../concepts/quality-presets.md)
|
|
234
|
+
- [three.js to null3D mapping](threejs-mapping.md)
|
|
@@ -1,15 +1,225 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: porting/threejs-loop-and-threads
|
|
3
|
-
title:
|
|
4
|
-
status: planned
|
|
5
|
-
since: "0.3"
|
|
3
|
+
title: The render loop, threads and the DOM
|
|
6
4
|
summary: "What moves to the sketch worker; what stays on the page; messages."
|
|
7
5
|
---
|
|
8
6
|
|
|
9
|
-
<!-- null3d:placeholder -->
|
|
10
|
-
|
|
11
7
|
# The render loop, threads and the DOM
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
```mermaid
|
|
10
|
+
flowchart LR
|
|
11
|
+
subgraph page["Page: main thread"]
|
|
12
|
+
dom["HTML UI, menus,<br/>GUI panels, storage,<br/>video elements"]
|
|
13
|
+
end
|
|
14
|
+
subgraph sketch["Sketch worker"]
|
|
15
|
+
scene["Scene, loop, input,<br/>controls, loaders,<br/>game state"]
|
|
16
|
+
end
|
|
17
|
+
dom -- "engine.postToSketch" --> scene
|
|
18
|
+
scene -- "page.post" --> dom
|
|
19
|
+
page -- "input, through<br/>shared memory" --> sketch
|
|
20
|
+
sketch -- "frames" --> render["Render worker<br/>draws the canvas"]
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
A three.js app usually runs everything on the main thread: the DOM, input, scene updates and drawing. null3D splits the app in two. The page module runs on the main thread and keeps the HTML. The sketch runs in a worker and keeps the scene. The two talk through small messages.
|
|
24
|
+
|
|
25
|
+
This page shows where each part of a three.js app goes, how the loop converts, and how the two sides talk. [Architecture: threads and the frame](../concepts/architecture.md) explains the threads themselves.
|
|
26
|
+
|
|
27
|
+
## What goes where
|
|
28
|
+
|
|
29
|
+
| Part of the three.js app | Where it goes | How |
|
|
30
|
+
| --- | --- | --- |
|
|
31
|
+
| `<canvas>` or `renderer.domElement` | `index.html` and the page module | Put the canvas in the HTML, and pass it to `createEngine` |
|
|
32
|
+
| Renderer options: antialias, alpha, pixel ratio, tone mapping | `createEngine` options and `post.set` | The [mapping](threejs-mapping.md) lists each option |
|
|
33
|
+
| Resize handling | Nowhere | The engine follows the canvas's CSS size |
|
|
34
|
+
| Scene, cameras, lights, meshes and loaders | The sketch | The setup function of `defineSketch` |
|
|
35
|
+
| The animation loop | The sketch | `onUpdate`, `onFixedUpdate` and `onLateUpdate` |
|
|
36
|
+
| DOM input listeners for the 3D view | The sketch | `ctx.input`, which the page fills through shared memory |
|
|
37
|
+
| Camera controls | The sketch | `@null3d/controls` |
|
|
38
|
+
| HTML UI, menus, a HUD, lil-gui panels | The page | Messages to and from the sketch |
|
|
39
|
+
| `CSS2DRenderer` and `CSS3DRenderer` labels | Both | `ui.trackLabel` in the sketch and `engine.labels.bind` on the page |
|
|
40
|
+
| Positional audio | Both | `@null3d/sound`: the sketch places the sounds, and the page plays them |
|
|
41
|
+
| Video elements | The page | `engine.registerVideo` on the page and `textures.fromVideo` in the sketch |
|
|
42
|
+
| `localStorage`, cookies and URL parameters | The page | Send the sketch what it needs at the start |
|
|
43
|
+
| `fetch` of JSON, binary data and models | The sketch | Workers have `fetch`, and relative addresses resolve against the page |
|
|
44
|
+
| Physics libraries | The sketch | WebAssembly physics runs in workers |
|
|
45
|
+
| Analytics, ads and routing | The page | No change |
|
|
46
|
+
|
|
47
|
+
Workers have no `localStorage`, but they have IndexedDB. A sketch can keep a cache of generated geometry there, as arrays.
|
|
48
|
+
|
|
49
|
+
## Converting the loop
|
|
50
|
+
|
|
51
|
+
A three.js loop:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
const clock = new THREE.Clock();
|
|
55
|
+
function animate() {
|
|
56
|
+
requestAnimationFrame(animate);
|
|
57
|
+
const dt = clock.getDelta();
|
|
58
|
+
updatePlayer(dt);
|
|
59
|
+
updateEnemies(dt);
|
|
60
|
+
physicsWorld.step(1 / 60, dt, 3);
|
|
61
|
+
followCamera(player);
|
|
62
|
+
controls.update();
|
|
63
|
+
composer.render();
|
|
64
|
+
}
|
|
65
|
+
animate();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The same loop in a sketch:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
return {
|
|
72
|
+
onFixedUpdate() {
|
|
73
|
+
physicsWorld.step();
|
|
74
|
+
syncBodies();
|
|
75
|
+
},
|
|
76
|
+
onUpdate(dt) {
|
|
77
|
+
updatePlayer(dt);
|
|
78
|
+
updateEnemies(dt);
|
|
79
|
+
controls.update(dt);
|
|
80
|
+
},
|
|
81
|
+
onLateUpdate() {
|
|
82
|
+
followCamera(player);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
- `clock.getDelta()` becomes the `dt` argument, which `ctx.time.dt` also holds. `clock.getElapsedTime()` becomes `ctx.time.now`.
|
|
88
|
+
- Physics moves to `onFixedUpdate`. It runs at the rate that `defineSketch(setup, { fixedRate: 60 })` sets, 60 steps per second by default. One `world.step()` call per fixed step replaces `world.step(1 / 60, dt, 3)`.
|
|
89
|
+
- Code that makes the camera follow an object moves to `onLateUpdate`. The camera then sees the frame's final positions, and its move shows in the same frame.
|
|
90
|
+
- The render call and `composer.render()` go away. The engine draws each frame by itself.
|
|
91
|
+
- Code that ran every N frames can test `ctx.time.frame % N === 0`.
|
|
92
|
+
|
|
93
|
+
[Sketch API](../api/sketch.md#the-callbacks) gives the order of the callbacks.
|
|
94
|
+
|
|
95
|
+
## Per-object classes become arrays
|
|
96
|
+
|
|
97
|
+
three.js code often wraps each object in a class with an `update()` method. That shape allocates, calls setters one object at a time, and scatters data across memory:
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
class Enemy {
|
|
101
|
+
constructor(scene) {
|
|
102
|
+
this.mesh = new THREE.Mesh(geo, mat);
|
|
103
|
+
this.vel = new THREE.Vector3();
|
|
104
|
+
scene.add(this.mesh);
|
|
105
|
+
}
|
|
106
|
+
update(dt, player) {
|
|
107
|
+
const dir = player.position.clone().sub(this.mesh.position).normalize(); // allocates
|
|
108
|
+
this.vel.lerp(dir, 0.1);
|
|
109
|
+
this.mesh.position.addScaledVector(this.vel, dt);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
enemies.forEach((e) => e.update(dt, player));
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
In null3D, the enemies become one instance batch, and their state becomes typed arrays:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
const N = 2000;
|
|
119
|
+
const enemies = scene.createInstances(enemyMesh, N, { material: enemyMaterial, dynamic: true });
|
|
120
|
+
const vel = new Float32Array(N * 3);
|
|
121
|
+
const playerPos = new Float32Array(3);
|
|
122
|
+
|
|
123
|
+
function updateEnemies(dt: number) {
|
|
124
|
+
player.getPosition(playerPos);
|
|
125
|
+
const p = enemies.positions;
|
|
126
|
+
for (let i = 0; i < N; i++) {
|
|
127
|
+
const o = i * 3;
|
|
128
|
+
let dx = playerPos[0] - p[o];
|
|
129
|
+
let dy = playerPos[1] - p[o + 1];
|
|
130
|
+
let dz = playerPos[2] - p[o + 2];
|
|
131
|
+
const len = Math.sqrt(dx * dx + dy * dy + dz * dz) || 1;
|
|
132
|
+
dx /= len; dy /= len; dz /= len;
|
|
133
|
+
vel[o] += (dx - vel[o]) * 0.1;
|
|
134
|
+
vel[o + 1] += (dy - vel[o + 1]) * 0.1;
|
|
135
|
+
vel[o + 2] += (dz - vel[o + 2]) * 0.1;
|
|
136
|
+
p[o] += vel[o] * dt;
|
|
137
|
+
p[o + 1] += vel[o + 1] * dt;
|
|
138
|
+
p[o + 2] += vel[o + 2] * dt;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Enemies with different meshes become one batch per mesh. An enemy from a glTF file with several meshes takes `scene.createInstances(prefab, N, { dynamic: true })`, so one row moves every mesh of the model. Keep health, targets and timers in typed arrays indexed by row. When an enemy dies, move the last live row into its place and lower the batch's active count with `setActiveCount`.
|
|
144
|
+
|
|
145
|
+
## Messages between the page and the sketch
|
|
146
|
+
|
|
147
|
+
Messages go one way and do not wait for a reply. The browser copies their data, so keep them small and send them only when something changes.
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
// sketch.ts
|
|
151
|
+
page.post('score', { value: score }); // when the score changes
|
|
152
|
+
page.onMessage((name, data) => {
|
|
153
|
+
if (name === 'settings') applySettings(data);
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
// page.ts
|
|
159
|
+
engine.onSketchMessage((name, data) => {
|
|
160
|
+
if (name === 'score') scoreElement.textContent = String((data as { value: number }).value);
|
|
161
|
+
});
|
|
162
|
+
engine.postToSketch('settings', { volume: 0.8 });
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
- Send events and changes, not state for every frame. A three.js app that writes a speed into the DOM every frame sends it only when it changes by a visible amount.
|
|
166
|
+
- HTML elements that follow objects use `ui.trackLabel`, which sends no messages.
|
|
167
|
+
- Give the message names a TypeScript union type that both modules import, so a typo fails at compile time.
|
|
168
|
+
- Large data, such as a level file, goes straight to the sketch with `fetch`, not through the page.
|
|
169
|
+
- Engine objects belong to the sketch. To name one in a message, send a name or a number that stands for it.
|
|
170
|
+
|
|
171
|
+
[Messages between sketch and page](../api/page.md) gives the full rules, such as the order of messages and when they arrive.
|
|
172
|
+
|
|
173
|
+
## Input and camera controls
|
|
174
|
+
|
|
175
|
+
The page forwards keyboard, pointer, touch and gamepad input to the sketch through shared memory. The sketch reads it in `ctx.input`.
|
|
176
|
+
|
|
177
|
+
| three.js | null3D |
|
|
178
|
+
| --- | --- |
|
|
179
|
+
| `addEventListener('pointermove', ...)` with screen math | `input.pointer.ndcX` and `ndcY`, or `input.pointer.x` and `y` in CSS pixels |
|
|
180
|
+
| `addEventListener('keydown', ...)` with a key state object | `input.isDown('KeyW')` and `input.wasPressed('Space')` |
|
|
181
|
+
| `addEventListener('touchstart', ...)` with a list of touches | `input.touches`, with `touch-action: none` on the canvas |
|
|
182
|
+
| Gamepad API polling | `input.isDown('GamepadA')` and `input.value('GamepadLeftStickRight')` |
|
|
183
|
+
| `OrbitControls` and `MapControls` | `createOrbitControls(ctx, camera, options)` and `createMapControls`, with `controls.update(dt)` each frame |
|
|
184
|
+
| `FlyControls` and `FirstPersonControls` | `createFlyControls` and `createFirstPersonControls`, with the same option names |
|
|
185
|
+
| `PointerLockControls` | `createFirstPersonControls` with `movementSpeed: 0`, and `engine.requestPointerLock()` in a click handler on the page |
|
|
186
|
+
| `controls.listenToKeyEvents(window)` | `controls.listenToKeyEvents()`, with no element: the controls read the keys from the sketch's input |
|
|
187
|
+
| Clicks on UI buttons over the canvas | Handle them on the page, and send the action to the sketch |
|
|
188
|
+
|
|
189
|
+
Pointer events on HTML elements above the canvas do not reach the engine. To stop the wheel from scrolling the page over the canvas, the page adds a wheel listener that calls `preventDefault`:
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
canvas.addEventListener('wheel', (e) => e.preventDefault(), { passive: false });
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
[Input](../api/input.md) and [Controls](../api/controls.md) give the details.
|
|
196
|
+
|
|
197
|
+
## Libraries in a port
|
|
198
|
+
|
|
199
|
+
| Library | What to do |
|
|
200
|
+
| --- | --- |
|
|
201
|
+
| lil-gui, dat.gui, Tweakpane | Keep them on the page, and send the values to the sketch |
|
|
202
|
+
| stats.js | `debug.stats(true)` in the sketch, or `createEngine({ stats: true })` on the page; `engine.measure(seconds)` gives percentiles |
|
|
203
|
+
| GSAP, tween.js | For a scene value, `scene.animateProperty`. DOM tweens stay on the page |
|
|
204
|
+
| cannon-es, Rapier, Ammo | Run them in the sketch, and copy the transforms into dynamic objects or batches after each step |
|
|
205
|
+
| three-mesh-bvh | Delete it, because raycasts use the engine's own acceleration structures |
|
|
206
|
+
| troika-three-text | HTML labels, a texture with the text drawn in it, or a text mesh baked into glTF |
|
|
207
|
+
| pmndrs postprocessing | `post.set` and `post.addEffect`, as [Porting post-processing](threejs-postprocessing.md) shows |
|
|
208
|
+
| three-stdlib and other addons | Look each import up in the [mapping](threejs-mapping.md), since many are built into the engine |
|
|
209
|
+
|
|
210
|
+
[Using a physics library](../guides/physics.md) shows a physics library in a sketch.
|
|
211
|
+
|
|
212
|
+
## A sketch on the main thread
|
|
213
|
+
|
|
214
|
+
`createEngine({ sketchThread: 'main' })` runs the sketch on the page's main thread, where it can reach the DOM. A large app can port its three.js calls first in this mode, then move its DOM code to the page. The render and job workers keep running. But the sketch then shares the main thread with the page, so layout work and page scripts can delay frames.
|
|
215
|
+
|
|
216
|
+
A page without cross-origin isolation also runs the sketch on the main thread, in the single-threaded build. [Hosting and cross-origin isolation](../getting-started/hosting.md) shows the two headers that turn on worker threads.
|
|
217
|
+
|
|
218
|
+
## Related pages
|
|
14
219
|
|
|
15
|
-
|
|
220
|
+
- [Porting from three.js](threejs-overview.md)
|
|
221
|
+
- [Architecture: threads and the frame](../concepts/architecture.md)
|
|
222
|
+
- [Messages between sketch and page](../api/page.md)
|
|
223
|
+
- [Sketch API](../api/sketch.md)
|
|
224
|
+
- [HTML UI around the scene](../guides/ui-overlays.md)
|
|
225
|
+
- [Porting React Three Fiber](react-three-fiber.md)
|