@null3d/engine 0.0.0 → 0.1.0
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 +19 -6
- package/dist/wasm/single/null3d_bg.wasm +0 -0
- package/dist/wasm/threaded/null3d.js +19 -6
- package/dist/wasm/threaded/null3d_bg.wasm +0 -0
- package/docs/api/animation.md +23 -67
- package/docs/api/assets.md +42 -26
- package/docs/api/cameras.md +8 -6
- package/docs/api/controls.md +46 -44
- package/docs/api/debug.md +13 -8
- package/docs/api/engine.md +17 -11
- package/docs/api/feature-draws.md +3 -3
- package/docs/api/feature-effects.md +3 -3
- package/docs/api/feature-instances.md +2 -2
- package/docs/api/features.md +14 -15
- package/docs/api/geometry.md +56 -75
- package/docs/api/grass.md +1 -1
- package/docs/api/impostors.md +120 -9
- package/docs/api/input.md +4 -11
- package/docs/api/lights.md +14 -38
- package/docs/api/lines.md +8 -25
- package/docs/api/materials.md +69 -70
- package/docs/api/math.md +6 -7
- package/docs/api/objects.md +13 -13
- package/docs/api/particles.md +2 -14
- package/docs/api/points.md +3 -25
- package/docs/api/post.md +22 -23
- package/docs/api/quality.md +11 -6
- package/docs/api/raycast.md +13 -32
- package/docs/api/reference/animation.md +9 -9
- package/docs/api/reference/assets.md +18 -18
- package/docs/api/reference/cameras.md +5 -2
- package/docs/api/reference/controls.md +75 -23
- package/docs/api/reference/debug.md +23 -8
- package/docs/api/reference/engine.md +22 -10
- package/docs/api/reference/geometry.md +161 -82
- package/docs/api/reference/grass.md +1 -1
- package/docs/api/reference/impostors.md +92 -1
- package/docs/api/reference/input.md +3 -3
- package/docs/api/reference/lights.md +9 -9
- package/docs/api/reference/lines.md +7 -7
- package/docs/api/reference/materials.md +48 -48
- package/docs/api/reference/math.md +22 -22
- package/docs/api/reference/objects.md +9 -9
- package/docs/api/reference/points.md +4 -4
- package/docs/api/reference/post.md +18 -18
- package/docs/api/reference/quality.md +7 -3
- package/docs/api/reference/raycast.md +6 -6
- package/docs/api/reference/render.md +2 -2
- package/docs/api/reference/scene.md +29 -26
- package/docs/api/reference/sketch.md +4 -0
- package/docs/api/reference/sound.md +203 -0
- package/docs/api/reference/sprites.md +5 -5
- package/docs/api/reference/terrain.md +2 -1
- package/docs/api/reference/textures.md +4 -4
- package/docs/api/reference/ui.md +3 -3
- package/docs/api/reference/vertex-draws.md +3 -3
- package/docs/api/reference/water.md +77 -6
- package/docs/api/reference/wind.md +2 -2
- package/docs/api/render.md +12 -10
- package/docs/api/scene.md +40 -29
- package/docs/api/sketch.md +64 -3
- package/docs/api/sound.md +169 -0
- package/docs/api/splats.md +7 -0
- package/docs/api/sprites.md +4 -20
- package/docs/api/tasks.md +2 -2
- package/docs/api/terrain.md +2 -14
- package/docs/api/textures.md +9 -9
- package/docs/api/ui.md +3 -15
- package/docs/api/water.md +198 -15
- package/docs/api/wind.md +5 -5
- package/docs/cli/null3d.md +93 -18
- package/docs/concepts/architecture.md +10 -10
- package/docs/concepts/assets.md +2 -19
- package/docs/concepts/backends.md +7 -5
- package/docs/concepts/color-management.md +15 -26
- package/docs/concepts/culling.md +1 -1
- package/docs/concepts/instances.md +5 -5
- package/docs/concepts/large-worlds.md +1 -1
- package/docs/concepts/lighting.md +50 -55
- package/docs/concepts/lod.md +2 -15
- package/docs/concepts/materials.md +16 -16
- package/docs/concepts/post-processing.md +7 -29
- package/docs/concepts/quality-preset-tables.md +53 -50
- package/docs/concepts/quality-presets.md +56 -27
- package/docs/concepts/render-graph.md +19 -14
- package/docs/concepts/render-layers.md +3 -17
- package/docs/concepts/shadows.md +10 -28
- package/docs/concepts/static-dynamic.md +2 -2
- package/docs/cookbook/index.md +1 -1
- package/docs/data/threejs-mapping.json +136 -102
- package/docs/errors/E1214.md +3 -3
- package/docs/errors/E1307.md +23 -0
- package/docs/errors/E1420.md +3 -3
- package/docs/errors/index.md +3 -2
- package/docs/getting-started/cdn.md +17 -1
- package/docs/getting-started/first-scene.md +1 -1
- package/docs/getting-started/hosting.md +50 -9
- package/docs/getting-started/install.md +8 -7
- package/docs/getting-started/network-use.md +10 -2
- package/docs/getting-started/project-structure.md +15 -6
- package/docs/getting-started/third-party-notices.md +8 -6
- package/docs/guides/accessibility.md +1 -1
- package/docs/guides/add-ons.md +3 -2
- package/docs/guides/agents.md +16 -5
- package/docs/guides/assets-pipeline.md +13 -12
- package/docs/guides/audio.md +109 -124
- package/docs/guides/car-paint.md +1 -1
- package/docs/guides/content-pages.md +10 -6
- package/docs/guides/custom-passes.md +9 -8
- package/docs/guides/custom-shaders.md +6 -3
- package/docs/guides/debugging.md +78 -4
- package/docs/guides/deploying.md +105 -5
- package/docs/guides/deterministic-simulation.md +1 -1
- package/docs/guides/first-use-shaders.md +4 -4
- package/docs/guides/loading-screens.md +78 -33
- package/docs/guides/multiple-views.md +41 -2
- package/docs/guides/performance.md +6 -3
- package/docs/guides/phones.md +12 -1
- package/docs/guides/physics.md +1 -1
- package/docs/guides/screens.md +7 -5
- package/docs/guides/start-time.md +328 -0
- package/docs/guides/testing.md +23 -5
- package/docs/guides/ui-overlays.md +3 -3
- package/docs/index.md +12 -8
- package/docs/pages.md +26 -23
- package/docs/porting/react-three-fiber.md +228 -5
- package/docs/porting/threejs-loop-and-threads.md +220 -6
- package/docs/porting/threejs-mapping.md +86 -84
- package/docs/porting/threejs-materials.md +212 -5
- package/docs/porting/threejs-overview.md +218 -5
- package/docs/porting/threejs-postprocessing.md +2 -2
- package/docs/porting/threejs-shaders.md +298 -5
- package/docs/porting/threejs-unsupported.md +82 -6
- package/docs/porting/verification.md +181 -5
- package/docs/shaders/builtins.md +1 -1
- package/docs/shaders/library.md +63 -39
- package/docs/shaders/surface-functions.md +11 -10
- package/docs/shaders/wgsl-rules.md +1 -1
- package/lib/debug/channel.d.ts +103 -0
- package/lib/debug/channel.js +269 -0
- package/lib/debug/debug.d.ts +9 -7
- package/lib/debug/draw.d.ts +9 -0
- package/lib/debug/draw.js +49 -2
- package/lib/debug/inspector.d.ts +60 -0
- package/lib/debug/inspector.js +475 -0
- package/lib/debug/stats-panel.d.ts +1 -1
- package/lib/debug/stats.d.ts +2 -3
- package/lib/errors/codes.js +8 -2
- package/lib/errors/engine-error.js +25 -10
- package/lib/errors/fixes.d.ts +1 -0
- package/lib/errors/fixes.js +3 -2
- 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/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 +4 -1
- 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 +15 -1
- package/lib/index.d.ts +6 -4
- 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 +7 -7
- 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 +103 -26
- package/lib/page/engine.js +420 -97
- 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 +17 -10
- 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/switches.d.ts +32 -2
- package/lib/page/switches.js +9 -1
- 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/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 +1 -1
- package/lib/scene/fog.d.ts +11 -4
- package/lib/scene/fog.js +8 -2
- package/lib/scene/frame-cameras.d.ts +19 -0
- package/lib/scene/frame-cameras.js +58 -0
- package/lib/scene/gltf.d.ts +32 -2
- 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 +120 -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/picks.d.ts +2 -2
- package/lib/scene/pointer-events.d.ts +4 -7
- package/lib/scene/post.d.ts +38 -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 +225 -254
- package/lib/scene/resources.js +179 -81
- package/lib/scene/scene.d.ts +149 -118
- package/lib/scene/scene.js +196 -98
- 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 +3 -4
- package/lib/scene/time-of-day.js +2 -3
- 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/metrics.d.ts +38 -0
- package/lib/shared/metrics.js +62 -1
- package/lib/shared/run-task.js +7 -1
- 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 +35 -0
- 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 +76 -9
- package/lib/sketch/runner.js +385 -52
- package/lib/sketch/ui.d.ts +5 -8
- package/lib/workers/probe-worker.d.ts +5 -0
- package/lib/workers/probe-worker.js +13 -4
- package/lib/workers/protocol.d.ts +61 -13
- package/lib/workers/protocol.js +4 -0
- package/lib/workers/render-worker.js +25 -4
- package/lib/workers/sketch-worker.js +104 -15
- package/package.json +2 -2
package/docs/pages.md
CHANGED
|
@@ -20,7 +20,7 @@ summary: "Every docs page by area, with its status and version."
|
|
|
20
20
|
| [Network use](getting-started/network-use.md) | What null3D downloads, when each file loads, the build's one download, and how to keep a page on its own origin. | experimental | 0.2 |
|
|
21
21
|
| [Third-party notices](getting-started/third-party-notices.md) | The third-party code in each null3D package, with its version and license, and the notices file that a build writes beside the page. | experimental | 0.2 |
|
|
22
22
|
| [Try null3D from a CDN](getting-started/cdn.md) | The CDN build; its production and development entries; one HTML file with no bundler; import maps; the policy and CORS headers; both thread modes; what needs a build step. | experimental | 0.2 |
|
|
23
|
-
| [Project structure](getting-started/project-structure.md) | Starting from the `empty` template with `bunx @null3d/cli create`; page.ts, sketch.ts, AGENTS.md and the agent hook; what runs where. | experimental | 0.3 |
|
|
23
|
+
| [Project structure](getting-started/project-structure.md) | Starting from the `empty`, `product` or `game` template with `bunx @null3d/cli create`; page.ts, sketch.ts, AGENTS.md and the agent hook; what runs where. | experimental | 0.3 |
|
|
24
24
|
|
|
25
25
|
## Concepts
|
|
26
26
|
|
|
@@ -33,7 +33,7 @@ summary: "Every docs page by area, with its status and version."
|
|
|
33
33
|
| [The particle buffer](concepts/particle-buffer.md) | What a particle buffer holds: its 32-byte records, its values and its frame's values; how an emitter or sketch code writes births into it; and the limits that it meets. | experimental | 0.2 |
|
|
34
34
|
| [GPU tiers and backends](concepts/backends.md) | WebGPU core, compatibility mode and WebGL2; color, anti-aliasing and depth on each tier; capability flags; the portable budget; never branching on GPU names. | experimental | 0.1 |
|
|
35
35
|
| [Quality presets, dynamic resolution and frame budgets](concepts/quality-presets.md) | Low to Ultra; pixel-ratio caps; the preset check; switching presets; the frame-budget governor; quality events for sketch code. | experimental | 0.1 |
|
|
36
|
-
| [Color management](concepts/color-management.md) | Linear working space; sRGB hex colors and linear arrays; texture color spaces; HDR color; exposure and tone mapping; transparent canvases
|
|
36
|
+
| [Color management](concepts/color-management.md) | Linear working space; sRGB hex colors and linear arrays; texture color spaces; HDR color; exposure and tone mapping; transparent canvases. | experimental | 0.1 |
|
|
37
37
|
| [Materials and pipelines](concepts/materials.md) | Built-in materials; permutations; pipeline warm-up; why changing shader features can stall a frame. | experimental | 0.1 |
|
|
38
38
|
| [Lighting and environment](concepts/lighting.md) | Light types, units and exposure; clustered lighting; fog; environment maps and spherical harmonics; light probe grids; sky and environment backgrounds; the sky's light and time of day. | experimental | 0.1 |
|
|
39
39
|
| [Shadows](concepts/shadows.md) | Cascades that stay still as the camera turns and blend where they meet; contact shadows where small objects touch the ground; the shadow atlas of spot and point lights; update rates and filtering per preset; bias settings. | experimental | 0.1 |
|
|
@@ -51,12 +51,12 @@ summary: "Every docs page by area, with its status and version."
|
|
|
51
51
|
| Page | What it covers | Status | Step |
|
|
52
52
|
| --- | --- | --- | --- |
|
|
53
53
|
| [Page API: createEngine](api/engine.md) | createEngine options and start errors; memory; capabilities and mode; pausing, detaching, failures, measuring, captureFrame, messages, pointer lock and destroy. | experimental | 0.1 |
|
|
54
|
-
| [Sketch API: defineSketch and the context](api/sketch.md) | The context object: scene, assets, materials, geometry, textures, input, time, engine, quality, post, render, page, ui, debug; the callbacks. | experimental | 0.1 |
|
|
54
|
+
| [Sketch API: defineSketch and the context](api/sketch.md) | The context object: scene, assets, materials, geometry, textures, input, time, engine, quality, post, render, page, ui, debug; the callbacks; the options, with the download list. | experimental | 0.1 |
|
|
55
55
|
| [Scene](api/scene.md) | Creating objects; models and copies; find; multiple views of one scene; background, environment, reflection probes, fog, sky; the light probe grid; the material override; warmUp. | experimental | 0.1 |
|
|
56
56
|
| [Objects and transforms](api/objects.md) | Setters and getters; parents; flags; destroy; pointer events. | experimental | 0.1 |
|
|
57
57
|
| [Cameras](api/cameras.md) | Perspective and orthographic cameras; a perspective camera's focal length; screenToRay; worldToScreen; layers. | experimental | 0.1 |
|
|
58
58
|
| [Lights](api/lights.md) | Directional, point, spot, hemisphere, ambient, rectangle and tube lights; shadow options. | experimental | 0.1 |
|
|
59
|
-
| [Geometry](api/geometry.md) | Generators
|
|
59
|
+
| [Geometry](api/geometry.md) | Generators of common shapes; extra shapes in @null3d/geometry; meshes from arrays; morph targets; vertices that change; levels of detail that meshes plan on the job workers; vertex formats; large meshes. | experimental | 0.1 |
|
|
60
60
|
| [three.js compatibility (@null3d/three-compat)](api/three-compat.md) | Reinhard and Cineon tone curves for post.set, and an outline with three.js OutlinePass's glow, pulse and blurred edge. | experimental | 0.3 |
|
|
61
61
|
| [Materials](api/materials.md) | standard, unlit, shader, shadowCatcher; every option. | experimental | 0.1 |
|
|
62
62
|
| [Textures](api/textures.md) | loadTexture options; KTX2 files; fromData; fromImageBitmap; fromCanvas; fromVideo; fromPass. | experimental | 0.1 |
|
|
@@ -64,7 +64,7 @@ summary: "Every docs page by area, with its status and version."
|
|
|
64
64
|
| [Animation](api/animation.md) | The animator; play, crossFade, clip weights, start times, 1D blends, layers, joint masks, additive clips, joint overrides and look-at, events; property animation of lights, cameras and materials, glTF's KHR_animation_pointer; morph weights. | experimental | 0.2 |
|
|
65
65
|
| [Raycasting and spatial queries](api/raycast.md) | raycast, raycastAny, raycastAll, raycastBatch, overlap queries, pointer events on objects. | experimental | 0.2 |
|
|
66
66
|
| [Input](api/input.md) | Pointer, keyboard, touch and gamepad; action maps; pointer events on objects. | experimental | 0.1 |
|
|
67
|
-
| [Controls (@null3d/controls)](api/controls.md) | Orbit and map controls
|
|
67
|
+
| [Controls (@null3d/controls)](api/controls.md) | Orbit and map controls; fly and first-person controls, with pointer lock; drag, rotate and scale gizmos. | experimental | 0.1 |
|
|
68
68
|
| [Post-processing API](api/post.md) | post.set for tone mapping, exposure and a camera's EV100, automatic exposure, bloom, ambient occlusion, screen-space reflections, depth of field, outlines, color grading tables and controls, the vignette and sharpening; custom effects with post.addEffect, custom tone curves, and still views. | experimental | 0.1 |
|
|
69
69
|
| [Render graph API](api/render.md) | render.addPass for scene passes, reflection passes, full-screen passes and compute passes of your own WGSL that write into textures; half and quarter render sizes; the outline mask as an input; switching and removing passes; render.dumpGraph. | experimental | 0.2 |
|
|
70
70
|
| [Add-on feature descriptions](api/features.md) | One description for an add-on's render work: the frame point, the reads and writes, the callbacks, the outputs with neutral defaults, the lighting slots, the order band and the view mask. | experimental | 0.3 |
|
|
@@ -73,7 +73,7 @@ summary: "Every docs page by area, with its status and version."
|
|
|
73
73
|
| [Add-on feature effects](api/feature-effects.md) | post.addFeatureEffect turns a full-screen feature description into a pass of your WGSL over the scene's color, at the resolve point, on both GPU paths and in every thread mode, and draws it only while the feature is ready. | experimental | 0.3 |
|
|
74
74
|
| [Add-on instance draws](api/feature-instances.md) | scene.addComputeInstances draws a capacity of instances from your own buffer, with a count that the GPU writes, on WebGPU, and an instance batch that the CPU fills on WebGL2. | experimental | 0.3 |
|
|
75
75
|
| [Quality API](api/quality.md) | quality.preset, quality.set, quality.setPreset, the preset check, the frame-budget governor, budgets for a sketch's own systems, quality events. | experimental | 0.1 |
|
|
76
|
-
| [Debug drawing and stats](api/debug.md) | debug.line, box, sphere, arrow, axes, grid, frustum, light and skeleton; the stats overlay, debug.frameStats and @null3d/engine/stats; engine.measure and its figures; debug.view, the ?view switch and add-on views; debug.shadowCamera. | experimental | 0.1 |
|
|
76
|
+
| [Debug drawing and stats](api/debug.md) | debug.line, box, sphere, arrow, axes, grid, frustum, light and skeleton; the stats overlay, debug.frameStats and @null3d/engine/stats; engine.measure and its figures; debug.view, the ?view switch and add-on views; debug.shadowCamera; the in-page inspector and engine.inspect. | experimental | 0.1 |
|
|
77
77
|
| [Math helpers](api/math.md) | vec3, quat, mat4 and color on plain arrays; math.clamp, lerp, damp and a random generator that hold mode seeds; the maths entry and createMath for a deterministic simulation. | experimental | 0.1 |
|
|
78
78
|
| [Time](api/time.md) | dt, time.now, fixed steps. | experimental | 0.1 |
|
|
79
79
|
| [Sprites](api/sprites.md) | createSprites; world and screen size modes; atlases. | experimental | 0.2 |
|
|
@@ -84,7 +84,8 @@ summary: "Every docs page by area, with its status and version."
|
|
|
84
84
|
| [Terrain (@null3d/terrain)](api/terrain.md) | createTerrain: a height field drawn as a camera-centered quadtree of blocks, lit per pixel from baked normals, with a height-blended splat surface, and heightAt and normalAt for placing objects. | experimental | 0.2 |
|
|
85
85
|
| [Grass (@null3d/grass)](api/grass.md) | createGrass: tufts of bent blades over a ground, placed by integer hashes, built in a compute pass on WebGPU with near shadows, swaying in the wind add-on's field, with a share for each quality preset. | experimental | 0.3 |
|
|
86
86
|
| [Gaussian splats (@null3d/splats)](api/splats.md) | createSplats: SPZ files, streamed chunk files and KHR_gaussian_splatting models drawn as Gaussian splats, with detail levels, a budget that follows the preset and the GPU path, and a soft mix with meshes. | experimental | 0.3 |
|
|
87
|
-
| [
|
|
87
|
+
| [Sound (@null3d/sound)](api/sound.md) | createSound and connectSound: loops, groups and one-shots placed in the scene, played with the browser's audio and driven from the sketch on the audio thread. | experimental | 0.3 |
|
|
88
|
+
| [Water (@null3d/water)](api/water.md) | createWater: a river and a lake from one shape, with waves that the current carries, ripples, caustics, foam, rain rings, a soft shore that reads the scene's depth or the shape's bed, a reflection whose cadence follows the preset, and createUnderwater for the view from below the surface. createWaterfall: a fall from a river's lip, with spray and mist. createOcean: an open sea of spectra transformed on the GPU. | experimental | 0.3 |
|
|
88
89
|
| [Wind (@null3d/wind)](api/wind.md) | createWind: one field of air that plants, flags, smoke and water read, from an analytic wind of place and time with traveling gusts and pushes, held in a grid that follows the camera. | experimental | 0.3 |
|
|
89
90
|
| [UI overlays and labels](api/ui.md) | ui.trackLabel in the sketch; engine.labels.bind on the page. | experimental | 0.2 |
|
|
90
91
|
| [Messages between sketch and page](api/page.md) | page.post and page.onMessage in the sketch; engine.postToSketch and engine.onSketchMessage on the page. | experimental | 0.1 |
|
|
@@ -101,22 +102,23 @@ summary: "Every docs page by area, with its status and version."
|
|
|
101
102
|
| [Car paint](guides/car-paint.md) | 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. | experimental | 0.3 |
|
|
102
103
|
| [Lens kit](guides/lens-kit.md) | One look for the camera's marks: a grade, a sharpening, a vignette, film grain, color fringes, an anamorphic streak read from bloom, and a speed blur. | experimental | 0.3 |
|
|
103
104
|
| [In-world screens](guides/screens.md) | Displays painted on an OffscreenCanvas in the sketch, with a refresh rate for each screen, uploads of only the changed rectangles, an LCD look that changes with distance, a boot glitch, and clicks mapped back to the canvas's coordinates. | experimental | 0.3 |
|
|
104
|
-
| [Loading screens and warm-up](guides/loading-screens.md) | preload; onProgress; scene.warmUp; shader files that load up front; the preset check; upload budgets; switching presets behind a loading screen. | experimental | 0.1 |
|
|
105
|
+
| [Loading screens and warm-up](guides/loading-screens.md) | The download list; preload; onProgress; loads one after another; scene.warmUp; shader files that load up front; the preset check during play or before it; upload budgets; switching presets behind a loading screen. | experimental | 0.1 |
|
|
106
|
+
| [A fast start](guides/start-time.md) | Where a page's start goes, on a simulated phone link and on real phones; compression, cache headers, the compiled core and HTTP/2 on the host; the early core script; what the engine does while the core downloads; downloads of the first view and of the rest; the preset check; pipelines on phones; testing and measuring a start. | experimental | 0.1 |
|
|
105
107
|
| [Accessibility](guides/accessibility.md) | What the canvas tells assistive technology; keyboard use; reduced motion; pausing; loading and errors. | experimental | 0.1 |
|
|
106
108
|
| [3D scenes on content pages](guides/content-pages.md) | Product and marketing pages: the fallback page, a load deadline, pausing off screen, scroll-driven cameras, second visits and crashes. | experimental | 0.1 |
|
|
107
109
|
| [UI, HTML overlays and labels](guides/ui-overlays.md) | HTML UI on the page; labels that follow objects; GUI panels. | experimental | 0.2 |
|
|
108
110
|
| [Video textures](guides/video-textures.md) | engine.registerVideo on the page and textures.fromVideo in the sketch; frame sizes and the upload budget; browser limits. | experimental | 0.3 |
|
|
109
|
-
| [
|
|
111
|
+
| [Sound in a scene](guides/audio.md) | Sound that follows the scene with @null3d/sound: beds whose level the scene sets, sources at places, groups of many sources, and events. | experimental | 0.1 |
|
|
110
112
|
| [Using a physics library](guides/physics.md) | Running Rapier or cannon-es in the sketch worker; copying transforms. | experimental | 0.1 |
|
|
111
113
|
| [Using null3D with a deterministic simulation](guides/deterministic-simulation.md) | The maths entry a simulation may import; createMath with an exact math library; input, picking and physics that decide a result stay out of the sketch; createEngine's input option; a camera that the player aims from recorded input; a production build of a game's engine files. | experimental | 0.3 |
|
|
112
114
|
| [Multiple views](guides/multiple-views.md) | Split screens and picture in picture with scene.createView: a camera and a rectangle of the canvas, drawn after the camera's view in the same frame; per-view culling; the cost of each view. | experimental | 0.3 |
|
|
113
115
|
| [The asset pipeline (the `assets` command)](guides/assets-pipeline.md) | optimize, convert, pack-orm, normal-from-bump, env; LODs; texture compression; blockers and stored trees; budget reports. | experimental | 0.2 |
|
|
114
116
|
| [Baked light data](guides/baked-light-data.md) | 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. | stable | 0.3 |
|
|
115
|
-
| [Testing your sketch](guides/testing.md) | Hold mode; image tests; reading results; frames that stay the same on every run. | experimental | 0.1 |
|
|
117
|
+
| [Testing your sketch](guides/testing.md) | Hold mode; image tests; reading results; frames that stay the same on every run; browser tests of a whole app. | experimental | 0.1 |
|
|
116
118
|
| [Building an add-on](guides/add-ons.md) | One package with one entry; claim a namespace and the plugin interface range; import only the public entry points; compile the shaders on both paths; name a page; run the check. | experimental | 0.3 |
|
|
117
|
-
| [Debugging](guides/debugging.md) | Error codes; the inspector; the MCP server; the render-graph dump; common failures. | experimental | 0.1 |
|
|
118
|
-
| [Deploying](guides/deploying.md) | Headers on common hosts; asset caching; size budgets. |
|
|
119
|
-
| [Working with AI agents](guides/agents.md) | Installing the null3D skills in Claude Code, claude.ai and other agent tools; docs by ID; the test loop; the MCP server and
|
|
119
|
+
| [Debugging](guides/debugging.md) | Error codes; the in-page inspector; the MCP server; the render-graph dump; common failures. | experimental | 0.1 |
|
|
120
|
+
| [Deploying](guides/deploying.md) | Headers on common hosts; asset caching; size budgets. | experimental | 0.3 |
|
|
121
|
+
| [Working with AI agents](guides/agents.md) | Installing the null3D skills in Claude Code, claude.ai and other agent tools; docs by ID; the test loop; the MCP server and the in-page inspector. | experimental | 0.1 |
|
|
120
122
|
| [Shaders that load on first use](guides/first-use-shaders.md) | The features whose shaders download on first use, which createEngine's preload takes. | generated | 0.1 |
|
|
121
123
|
|
|
122
124
|
## Shaders
|
|
@@ -132,21 +134,21 @@ summary: "Every docs page by area, with its status and version."
|
|
|
132
134
|
|
|
133
135
|
| Page | What it covers | Status | Step |
|
|
134
136
|
| --- | --- | --- | --- |
|
|
135
|
-
| [Porting from three.js](porting/threejs-overview.md) | The porting workflow; what gets faster; what needs rewriting. |
|
|
136
|
-
| [Porting materials and textures](porting/threejs-materials.md) | Parameter-by-parameter conversion; color spaces; approximations. |
|
|
137
|
-
| [Porting shaders: GLSL, onBeforeCompile and TSL](porting/threejs-shaders.md) | GLSL to WGSL; three.js built-ins to engine built-ins; worked examples. |
|
|
137
|
+
| [Porting from three.js](porting/threejs-overview.md) | The porting workflow; what gets faster; what needs rewriting. | experimental | 0.3 |
|
|
138
|
+
| [Porting materials and textures](porting/threejs-materials.md) | Parameter-by-parameter conversion; color spaces; approximations. | experimental | 0.3 |
|
|
139
|
+
| [Porting shaders: GLSL, onBeforeCompile and TSL](porting/threejs-shaders.md) | GLSL to WGSL; three.js built-ins to engine built-ins; worked examples. | experimental | 0.3 |
|
|
138
140
|
| [Porting post-processing](porting/threejs-postprocessing.md) | EffectComposer passes and pmndrs effects to post.set settings; ShaderPass to post.addEffect; three.js tone mappings to custom tone curves. | experimental | 0.2 |
|
|
139
|
-
| [The render loop, threads and the DOM](porting/threejs-loop-and-threads.md) | What moves to the sketch worker; what stays on the page; messages. |
|
|
140
|
-
| [Porting React Three Fiber](porting/react-three-fiber.md) | Canvas, useFrame, drei helpers; keeping React for the page UI. |
|
|
141
|
-
| [Unsupported three.js features](porting/threejs-unsupported.md) |
|
|
142
|
-
| [Verifying a port](porting/verification.md) | Parity images per camera view; performance comparison; the WebGL2 path; phones. |
|
|
141
|
+
| [The render loop, threads and the DOM](porting/threejs-loop-and-threads.md) | What moves to the sketch worker; what stays on the page; messages. | experimental | 0.3 |
|
|
142
|
+
| [Porting React Three Fiber](porting/react-three-fiber.md) | Canvas, useFrame, drei helpers; keeping React for the page UI. | experimental | 0.3 |
|
|
143
|
+
| [Unsupported three.js features](porting/threejs-unsupported.md) | three.js features with no null3D equivalent, with workarounds. | experimental | 0.3 |
|
|
144
|
+
| [Verifying a port](porting/verification.md) | Parity images per camera view; performance comparison; the WebGL2 path; phones. | experimental | 0.3 |
|
|
143
145
|
| [three.js to null3D mapping](porting/threejs-mapping.md) | Every three.js API a port is likely to meet, with its null3D equivalent. | generated | 0.3 |
|
|
144
146
|
|
|
145
147
|
## Command-line tool
|
|
146
148
|
|
|
147
149
|
| Page | What it covers | Status | Step |
|
|
148
150
|
| --- | --- | --- | --- |
|
|
149
|
-
| [The `null3d` command](cli/null3d.md) | create, test, bench, check-addon, shot, upgrade, assets,
|
|
151
|
+
| [The `null3d` command](cli/null3d.md) | create, test, bench, check-addon, shot, upgrade, assets, mcp. | experimental | 0.1 |
|
|
150
152
|
|
|
151
153
|
## Errors
|
|
152
154
|
|
|
@@ -172,7 +174,7 @@ summary: "Every docs page by area, with its status and version."
|
|
|
172
174
|
| [E1208: Invalid texture](errors/E1208.md) | A call that makes or updates a texture received something it cannot use. It can be an option the engine does not know, or an image without pixels or larger than the device takes. It can also be data that does not fit the texture's size and format, or numbers that make no color grading table. With a KTX2 file, it can be an option that the file cannot take, or an update of its texture. | generated | 0.1 |
|
|
173
175
|
| [E1209: Too many textures](errors/E1209.md) | A material whose shader reads more textures than a WebGL2 shader stage has units. The engine numbers the textures that a program reads, and WebGL2 gives every stage at least 16 units, as many as the device reports. The lit shader reads most of them, so a custom material or add-on draw reaches the limit with its own textures, beside the maps and the shadows. | generated | 0.3 |
|
|
174
176
|
| [E1213: Invalid setting](errors/E1213.md) | A call received a setting that it does not have, or a value that the setting does not take. Examples are a tone mapping that the engine does not know, a negative exposure, a grading control outside its range, and a built-in environment that the engine does not have. It also covers a glTF extension handler that names no extension, that another handler reads, or that returns a part without a mesh or a material. | generated | 0.1 |
|
|
175
|
-
| [E1214: Invalid sketch option](errors/E1214.md) | defineSketch() received an option out of its range. fixedRate must be a number above 0, and maxFixedSteps a whole number of 1 or more. The engine checks the options before it runs the setup function. | generated | 0.1 |
|
|
177
|
+
| [E1214: Invalid sketch option](errors/E1214.md) | defineSketch() received an option out of its range. fixedRate must be a number above 0, and maxFixedSteps a whole number of 1 or more. downloads must be an array of file addresses: an empty string, or a string that is not an address, fails. The engine checks the options before it runs the setup function. | generated | 0.1 |
|
|
176
178
|
| [E1215: Invalid custom WGSL](errors/E1215.md) | materials.shader(), post.addEffect(), post.set() or scene.setSkyLayer() received WGSL that it cannot draw with. The null3D Vite plugin compiles WGSL while it builds the project. The engine therefore takes only what the plugin compiled: a template literal after a /* wgsl */ comment, or a .wgsl file that a module imports. The WGSL of a custom material declares a surface function, a vertex offset or a lobe. A full shader instead has a @vertex entry point that takes an InstanceIn from null3d::mesh. An effect declares fn effect, a tone curve fn toneCurve, and a sky layer fn skyLayer. | generated | 0.1 |
|
|
177
179
|
| [E1216: Invalid uniform or texture](errors/E1216.md) | A custom material's or effect's uniforms or textures did not match its WGSL. The uniforms option and set() take the names of the fields of struct Uniforms in the WGSL, each with a value of its type. A field cannot have the name of a standard value, such as color or roughness, because set() takes those too. The textures option takes the names of the WGSL's `var name: texture_2d<f32>;` and `var name: texture_2d_array<f32>;` declarations, each with a texture of the layers that its declaration takes. | generated | 0.1 |
|
|
178
180
|
| [E1217: Invalid material option](errors/E1217.md) | A material factory or scene.createLines() received an option value that it does not take, such as an unknown alpha mode, blending or line mode. Custom materials refuse the 'weighted' blending, and take alpha to coverage or the hash mode only where their WGSL sets or reads the surface's alpha. A material that lets light through refuses the hash alpha mode and the weighted blending. A layered material, with a clear coat, a sheen, iridescence or anisotropy, refuses a light map, specular maps, and two textures for a pair of layer maps that glTF packs into one. It refuses a transmission or a thickness map only where one of its layer maps fills the array that the map samples: the clear coat's normal map and iridescence's thickness map fill the array that the transmission map samples, and the sheen's maps and the anisotropy map fill the array that the thickness map samples. A material with transmission refuses a thickness map beside a specular color map, whose sRGB texture never shares the thickness map's array, and other materials refuse those two maps. A material that lets light through to its other side refuses the specular maps, whose places its diffuse transmission maps take, a clear coat, a sheen and the weighted blending, and other materials refuse the diffuse transmission maps. A custom material refuses subsurfaceRadius, which only a standard material takes. A custom material whose WGSL declares a coverage function takes the mask mode, or the blend mode with the cutoff that its shadow caster tests. | generated | 0.1 |
|
|
@@ -190,6 +192,7 @@ summary: "Every docs page by area, with its status and version."
|
|
|
190
192
|
| [E1304: GPU out of memory](errors/E1304.md) | The GPU had no room for a buffer or texture that the engine made after the start. The objects that use it draw wrong or not at all. The engine reports the first time this happens on each GPU device, and draws on. | generated | 0.2 |
|
|
191
193
|
| [E1305: GPU rejected work](errors/E1305.md) | The GPU path refused a command that the engine gave it after the start, such as a buffer larger than the device allows. The objects that the command made or drew are missing, and the canvas can stay black. The engine reports the first time this happens on each GPU device, and draws on. | generated | 0.2 |
|
|
192
194
|
| [E1306: Safari before 18](errors/E1306.md) | The browser runs the WebKit engine of a Safari before 18, which null3D does not support. Every browser on iPhone and iPad runs WebKit, so Chrome, Edge and Firefox there count too. | generated | 0.2 |
|
|
195
|
+
| [E1307: Pipeline not built](errors/E1307.md) | After the start, the GPU's driver could not build a pipeline that draws objects of the scene. It refused the pipeline among the other builds, and again when the engine built it alone. A driver does this when it runs short of memory as it compiles the shader, or when its compiler fails on the shader. On WebGL2, a program that the driver refuses to link twice counts the same. The message names the pipeline: its template, such as a custom material, and the pass that its build draws in. The objects that draw with it are missing from that pass. The engine reports the first time this happens on each GPU device, and draws on. | generated | 0.3 |
|
|
193
196
|
| [E1401: Not a sketch module](errors/E1401.md) | The module passed to createEngine as the sketch does not export a sketch as its default export. | generated | 0.1 |
|
|
194
197
|
| [E1402: Engine core out of date](errors/E1402.md) | The engine core WebAssembly file lacks functions that the TypeScript side calls, so the two come from different builds. Development builds check this when the core loads. When null3D's own dev server serves the engine from a checkout of its repository, the check also compares the stamp of the Rust sources that the core was built from with the stamp of the checkout's sources. A core built before a merge or an edit of those sources then fails at once, before it can run with code that expects another core. | generated | 0.1 |
|
|
195
198
|
| [E1403: Engine core not ready](errors/E1403.md) | An engine call ran before the engine core started in this worker, or the core started twice. | generated | 0.1 |
|
|
@@ -209,7 +212,7 @@ summary: "Every docs page by area, with its status and version."
|
|
|
209
212
|
| [E1417: glTF feature not supported](errors/E1417.md) | A glTF file needs an extension that the engine does not read, as its extensionsRequired list says. When a foundation add-on reads that extension, the message names the package to import, such as "Import @null3d/splats, which reads it", and that add-on registers the handler that loads the file. Or a call asked a model for something it cannot give, such as instance batches of a model with no meshes. | generated | 0.2 |
|
|
210
213
|
| [E1418: WebAssembly blocked by the page](errors/E1418.md) | The page's Content-Security-Policy does not allow WebAssembly, so the engine core, the KTX2 transcoder, the meshopt decoder or the Draco decoder could not compile. A policy allows it only when its script-src, or its default-src without a script-src, holds 'wasm-unsafe-eval'. | generated | 0.2 |
|
|
211
214
|
| [E1419: Canvas held by another engine](errors/E1419.md) | createEngine() got a canvas that another engine still draws on, or a canvas whose drawing thread ended, so no engine can draw on it again. That thread ends when it fails, or when the canvas leaves the page. It also ends when the page goes away, or when the browser refuses memory for a new engine. A canvas serves one engine at a time. After destroy(), a new engine can start on the same canvas. | generated | 0.2 |
|
|
212
|
-
| [E1420: Engine stopped](errors/E1420.md) | Sketch code called the engine after the engine stopped, from a timer, a promise or an event handler that outlived destroy(). The call did nothing. | generated | 0.2 |
|
|
215
|
+
| [E1420: Engine stopped](errors/E1420.md) | Sketch code called the engine after the engine stopped, from a timer, a promise or an event handler that outlived destroy(). The call did nothing. engine.firstFrame rejects with it too when the page destroys the engine before its first frame. | generated | 0.2 |
|
|
213
216
|
| [E1421: Unknown feature to preload](errors/E1421.md) | createEngine()'s preload option names a feature whose shaders the engine does not have. The features are ao, background, bloom, coat_sheen, coverage, cutout, dof, effect_groups, hash, lines, lod_fade, morph, occlusion, row_values, skinning, sky, sprites, transmission and views, and instance_index and texcoords for the engine's own tests. | generated | 0.2 |
|
|
214
217
|
| [E1422: Engine file blocked by the page](errors/E1422.md) | The engine's files come from another origin than the page, such as a CDN, and the page's Content-Security-Policy blocks one of them. The engine starts each worker from a blob: address that imports the worker's script from that origin, and downloads its .wasm files from there. So the policy's worker-src must allow blob: and the other origin, and its script-src and connect-src must allow the other origin too. Firefox checks the modules that a worker imports against worker-src. | generated | 0.2 |
|
|
215
218
|
| [E1423: Engine file without CORS](errors/E1423.md) | The engine's files come from another origin than the page, such as a CDN. One of them came without a CORS header, or did not download. A page loads a module or a .wasm file from another origin only when the response carries Access-Control-Allow-Origin. | generated | 0.2 |
|
|
@@ -1,15 +1,238 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: porting/react-three-fiber
|
|
3
3
|
title: Porting React Three Fiber
|
|
4
|
-
status:
|
|
4
|
+
status: experimental
|
|
5
5
|
since: "0.3"
|
|
6
6
|
summary: "Canvas, useFrame, drei helpers; keeping React for the page UI."
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
-
<!-- null3d:placeholder -->
|
|
10
|
-
|
|
11
9
|
# Porting React Three Fiber
|
|
12
10
|
|
|
13
|
-
>
|
|
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
|
+
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.
|
|
14
|
+
|
|
15
|
+
```mermaid
|
|
16
|
+
flowchart LR
|
|
17
|
+
subgraph page["Page: React"]
|
|
18
|
+
ui["UI, forms, menus"]
|
|
19
|
+
view["A view component<br/>that holds the canvas"]
|
|
20
|
+
html["HTML of the former<br/>drei Html elements"]
|
|
21
|
+
end
|
|
22
|
+
subgraph sketch["Sketch worker"]
|
|
23
|
+
scene["The former Canvas contents:<br/>setup and onUpdate"]
|
|
24
|
+
labels["ui.trackLabel"]
|
|
25
|
+
end
|
|
26
|
+
ui -- "engine.postToSketch" --> scene
|
|
27
|
+
scene -- "page.post" --> ui
|
|
28
|
+
labels -- "positions" --> html
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The React tree above the former `<Canvas>` barely changes. The subtree inside `<Canvas>` becomes the sketch.
|
|
32
|
+
|
|
33
|
+
## A view component
|
|
34
|
+
|
|
35
|
+
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.
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { useEffect, useRef } from 'react';
|
|
39
|
+
import { createEngine, type Engine } from '@null3d/engine';
|
|
40
|
+
|
|
41
|
+
type Props = {
|
|
42
|
+
sketch: URL;
|
|
43
|
+
onMessage?: (name: string, data: unknown) => void;
|
|
44
|
+
onReady?: (engine: Engine) => void;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** How long an engine waits off the page before the component destroys it. */
|
|
48
|
+
const KEEP_MS = 60_000;
|
|
49
|
+
|
|
50
|
+
type Kept = { canvas: HTMLCanvasElement; engine: Promise<Engine>; timer?: number };
|
|
51
|
+
const kept = new Map<string, Kept>();
|
|
52
|
+
|
|
53
|
+
function keptEngine(sketch: URL): Kept {
|
|
54
|
+
let entry = kept.get(sketch.href);
|
|
55
|
+
if (!entry) {
|
|
56
|
+
// The kept engine owns its canvas, which moves between hosts with the engine.
|
|
57
|
+
const canvas = document.createElement('canvas');
|
|
58
|
+
canvas.style.cssText = 'width:100%;height:100%;display:block';
|
|
59
|
+
entry = { canvas, engine: createEngine({ canvas, sketch }) };
|
|
60
|
+
entry.engine.catch(() => kept.delete(sketch.href));
|
|
61
|
+
kept.set(sketch.href, entry);
|
|
62
|
+
}
|
|
63
|
+
clearTimeout(entry.timer);
|
|
64
|
+
return entry;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function SceneView({ sketch, onMessage, onReady }: Props) {
|
|
68
|
+
const hostRef = useRef<HTMLDivElement>(null);
|
|
69
|
+
const onMessageRef = useRef(onMessage);
|
|
70
|
+
onMessageRef.current = onMessage;
|
|
71
|
+
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
const host = hostRef.current!;
|
|
74
|
+
const entry = keptEngine(sketch);
|
|
75
|
+
let mounted = true;
|
|
76
|
+
let off: (() => void) | undefined;
|
|
77
|
+
host.append(entry.canvas);
|
|
78
|
+
entry.engine.then(
|
|
79
|
+
(engine) => {
|
|
80
|
+
if (!mounted) return;
|
|
81
|
+
engine.attach(host);
|
|
82
|
+
off = engine.onSketchMessage((name, data) => onMessageRef.current?.(name, data));
|
|
83
|
+
onReady?.(engine);
|
|
84
|
+
},
|
|
85
|
+
(error) => console.error(error),
|
|
86
|
+
);
|
|
87
|
+
return () => {
|
|
88
|
+
mounted = false;
|
|
89
|
+
off?.();
|
|
90
|
+
entry.engine.then((engine) => engine.detach(), () => {});
|
|
91
|
+
entry.timer = window.setTimeout(() => {
|
|
92
|
+
kept.delete(sketch.href);
|
|
93
|
+
entry.engine.then((engine) => engine.destroy(), () => {});
|
|
94
|
+
}, KEEP_MS);
|
|
95
|
+
};
|
|
96
|
+
}, [sketch]);
|
|
97
|
+
|
|
98
|
+
return <div ref={hostRef} style={{ width: '100%', height: '100%' }} />;
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- 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.
|
|
103
|
+
- 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.
|
|
104
|
+
- Each mount adds its own message handler and removes it when it unmounts. An old component never hears the sketch.
|
|
105
|
+
- 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.
|
|
106
|
+
|
|
107
|
+
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`.
|
|
108
|
+
|
|
109
|
+
## State between React and the sketch
|
|
110
|
+
|
|
111
|
+
React sends a change to the sketch when its state changes:
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
const [color, setColor] = useState('#c0392b');
|
|
115
|
+
const engineRef = useRef<Engine>();
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
engineRef.current?.postToSketch('config', { color });
|
|
118
|
+
}, [color]);
|
|
119
|
+
|
|
120
|
+
// <SceneView sketch={sketchUrl} onReady={(e) => { engineRef.current = e; e.postToSketch('config', { color }); }} />
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The sketch applies the change, and sends events back:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
// sketch.ts
|
|
127
|
+
page.onMessage((name, data) => {
|
|
128
|
+
if (name === 'config') bodyMaterial.set({ color: (data as { color: string }).color });
|
|
129
|
+
});
|
|
130
|
+
sole.on('click', () => page.post('part-selected', { name: 'sole' }));
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
<SceneView
|
|
135
|
+
sketch={sketchUrl}
|
|
136
|
+
onMessage={(name, data) => {
|
|
137
|
+
if (name === 'part-selected') setSelected((data as { name: string }).name);
|
|
138
|
+
}}
|
|
139
|
+
/>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Never copy per-frame scene state into React state. React would then render again in every frame, on the main thread.
|
|
143
|
+
|
|
144
|
+
## React Three Fiber and drei in null3D
|
|
145
|
+
|
|
146
|
+
| React Three Fiber or drei | null3D |
|
|
147
|
+
| --- | --- |
|
|
148
|
+
| `<Canvas camera={{ position, fov }}>` | `scene.createPerspectiveCamera({ position, fov })` in the sketch |
|
|
149
|
+
| `<Canvas dpr={[1, 2]}>` | `createEngine({ maxPixelRatio: 2 })`, or the quality preset's cap |
|
|
150
|
+
| `<Canvas shadows>` | `castShadows` on lights and meshes, and `receiveShadows` on the surfaces that show shadows |
|
|
151
|
+
| `<Canvas gl={{ antialias, alpha }}>` | `createEngine({ antialias: 'msaa' })` and `createEngine({ transparent: true })` |
|
|
152
|
+
| `<Canvas frameloop="demand">` | `engine.setPaused(true)` while nothing changes |
|
|
153
|
+
| `<mesh>` with `<boxGeometry>` and `<meshStandardMaterial>` | `scene.createMesh({ mesh: geometry.box(), material: materials.standard() })` |
|
|
154
|
+
| `<group>` | `scene.createGroup()` and `setParent` |
|
|
155
|
+
| `<primitive object={gltf.scene} />` | `scene.instantiate(prefab)` |
|
|
156
|
+
| `useFrame((state, delta) => ...)` | `onUpdate(dt)` |
|
|
157
|
+
| `useThree()` | The sketch's context: `scene`, and `ctx.engine.viewport` for the size |
|
|
158
|
+
| `useLoader(GLTFLoader, url)` and drei `useGLTF(url)` | `await assets.loadGltf(url)` |
|
|
159
|
+
| `useGLTF.preload(url)` and `<Preload all />` | `assets.preload([...])` and `scene.warmUp()` |
|
|
160
|
+
| `<Suspense fallback>`, drei `<Loader>` and `useProgress` | `assets.onProgress` and a message; the page shows the loader |
|
|
161
|
+
| drei `useAnimations(animations, ref)` | `object.animator()` |
|
|
162
|
+
| drei `<OrbitControls makeDefault />` | `createOrbitControls(ctx, camera, options)` from `@null3d/controls` |
|
|
163
|
+
| 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 |
|
|
164
|
+
| drei `<Environment files="x.hdr" background />` | `scene.setEnvironment(await assets.loadEnvironment('/x.hdr'))` and `scene.setBackground` |
|
|
165
|
+
| drei `<ContactShadows />` | `materials.shadowCatcher` on a ground plane, which shows the directional light's shadow |
|
|
166
|
+
| drei `<Html>` | `ui.trackLabel` in the sketch, and `engine.labels.bind` on the page with the HTML that React renders |
|
|
167
|
+
| drei `<Text>` and `<Text3D>` | HTML labels, a texture with the text drawn in it, or a text mesh baked into glTF |
|
|
168
|
+
| drei `<Instances>`, `<Instance>` and `<Merged>` | `scene.createInstances` |
|
|
169
|
+
| drei `<Sky>` | `scene.setBackground({ sky: { sunPosition, turbidity, rayleigh } })`, with the same props |
|
|
170
|
+
| drei `<Stars>` | `scene.createPoints` |
|
|
171
|
+
| drei `<Float>` | A sine offset in `onUpdate` |
|
|
172
|
+
| drei `<Center>` and `<Bounds>` | `prefab.bounds`, and a camera fit computed at setup |
|
|
173
|
+
| drei `<Stats>` | `debug.stats(true)` in the sketch, or `createEngine({ stats: true })` on the page |
|
|
174
|
+
| 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 |
|
|
175
|
+
| `@react-three/postprocessing` | `post.set`, as [Porting post-processing](threejs-postprocessing.md) shows |
|
|
176
|
+
| `@react-three/rapier` | Rapier in the sketch worker, as [Using a physics library](../guides/physics.md) shows |
|
|
177
|
+
| Props that React changes in every frame | Logic in `onUpdate`; React sends intent, not frames |
|
|
178
|
+
|
|
179
|
+
Some parts behave differently:
|
|
180
|
+
|
|
181
|
+
- Only the closest object and its parents get a pointer event. React Three Fiber also passes the event to the objects behind it.
|
|
182
|
+
- `onPointerMissed` has no equivalent. Set a flag in the click handlers, and treat a release of the button without it as a miss.
|
|
183
|
+
- `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.
|
|
184
|
+
|
|
185
|
+
## Adaptive quality
|
|
186
|
+
|
|
187
|
+
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.
|
|
188
|
+
|
|
189
|
+
| drei | null3D |
|
|
190
|
+
| --- | --- |
|
|
191
|
+
| `<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. |
|
|
192
|
+
| `onChange={({ factor }) => setDpr(...)}` | `quality.set({ minRenderScale, maxRenderScale })` sets the range that the render scale moves in. `quality.renderScale` reads the current scale. |
|
|
193
|
+
| `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. |
|
|
194
|
+
| `onFallback` after `flipflops` | Nothing. A step up that fails doubles the governor's wait before it tries again, so quality settles. |
|
|
195
|
+
| `<AdaptiveDpr pixelated />` with `performance.regress()` | No equivalent. The governor follows frame times, not camera movement. Remove the `regress()` calls. |
|
|
196
|
+
| `<AdaptiveEvents />` | Nothing. null3D casts no pointer rays on the main thread. |
|
|
197
|
+
|
|
198
|
+
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.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
// The app used <PerformanceMonitor onChange={({ factor }) => setDpr(0.5 + 1.5 * factor)} />.
|
|
202
|
+
// The cap of 2 moves to createEngine({ maxPixelRatio: 2 }) on the page.
|
|
203
|
+
quality.set({ minRenderScale: 0.5, maxRenderScale: 1 });
|
|
204
|
+
const SPARKS = { low: 500, medium: 1000, high: 2000, ultra: 4000 };
|
|
205
|
+
let sparkScale = 1;
|
|
206
|
+
const sparkCount = () => Math.round(SPARKS[quality.preset] * sparkScale);
|
|
207
|
+
quality.setBudget({
|
|
208
|
+
name: 'sparks',
|
|
209
|
+
ms: 1,
|
|
210
|
+
min: 0.5,
|
|
211
|
+
onScale: (scale) => {
|
|
212
|
+
sparkScale = scale;
|
|
213
|
+
sparks.setActiveCount(sparkCount());
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
quality.onChange(() => sparks.setActiveCount(sparkCount())); // a new preset
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
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.
|
|
220
|
+
|
|
221
|
+
## Step by step
|
|
222
|
+
|
|
223
|
+
1. List every component inside `<Canvas>` with its props, and mark the props that come from React state.
|
|
224
|
+
2. Write a sketch that creates the same objects at setup. Props that React changes become `page.onMessage` handlers.
|
|
225
|
+
3. Move the `useFrame` bodies into `onUpdate`, and turn refs into handles or batch rows.
|
|
226
|
+
4. Replace `<Canvas>` with the view component.
|
|
227
|
+
5. Route the events: mesh events become `object.on(...)` in the sketch, and `page.post` tells React.
|
|
228
|
+
6. Replace each drei `<Html>` with a tracked label, and let React render its HTML on the page.
|
|
229
|
+
7. Compare images of each camera view, then measure the timings ([Verifying a port](verification.md)).
|
|
230
|
+
|
|
231
|
+
## Related pages
|
|
14
232
|
|
|
15
|
-
|
|
233
|
+
- [Porting from three.js](threejs-overview.md)
|
|
234
|
+
- [The render loop, threads and the DOM](threejs-loop-and-threads.md)
|
|
235
|
+
- [Messages between sketch and page](../api/page.md)
|
|
236
|
+
- [UI, HTML overlays and labels](../guides/ui-overlays.md)
|
|
237
|
+
- [Quality presets](../concepts/quality-presets.md)
|
|
238
|
+
- [three.js to null3D mapping](threejs-mapping.md)
|