@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
|
@@ -1,15 +1,229 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: porting/threejs-loop-and-threads
|
|
3
|
-
title:
|
|
4
|
-
status:
|
|
3
|
+
title: The render loop, threads and the DOM
|
|
4
|
+
status: experimental
|
|
5
5
|
since: "0.3"
|
|
6
6
|
summary: "What moves to the sketch worker; what stays on the page; messages."
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
-
<!-- null3d:placeholder -->
|
|
10
|
-
|
|
11
9
|
# The render loop, threads and the DOM
|
|
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
|
+
```mermaid
|
|
14
|
+
flowchart LR
|
|
15
|
+
subgraph page["Page: main thread"]
|
|
16
|
+
dom["HTML UI, menus,<br/>GUI panels, storage,<br/>video elements"]
|
|
17
|
+
end
|
|
18
|
+
subgraph sketch["Sketch worker"]
|
|
19
|
+
scene["Scene, loop, input,<br/>controls, loaders,<br/>game state"]
|
|
20
|
+
end
|
|
21
|
+
dom -- "engine.postToSketch" --> scene
|
|
22
|
+
scene -- "page.post" --> dom
|
|
23
|
+
page -- "input, through<br/>shared memory" --> sketch
|
|
24
|
+
sketch -- "frames" --> render["Render worker<br/>draws the canvas"]
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
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.
|
|
28
|
+
|
|
29
|
+
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.
|
|
30
|
+
|
|
31
|
+
## What goes where
|
|
32
|
+
|
|
33
|
+
| Part of the three.js app | Where it goes | How |
|
|
34
|
+
| --- | --- | --- |
|
|
35
|
+
| `<canvas>` or `renderer.domElement` | `index.html` and the page module | Put the canvas in the HTML, and pass it to `createEngine` |
|
|
36
|
+
| Renderer options: antialias, alpha, pixel ratio, tone mapping | `createEngine` options and `post.set` | The [mapping](threejs-mapping.md) lists each option |
|
|
37
|
+
| Resize handling | Nowhere | The engine follows the canvas's CSS size |
|
|
38
|
+
| Scene, cameras, lights, meshes and loaders | The sketch | The setup function of `defineSketch` |
|
|
39
|
+
| The animation loop | The sketch | `onUpdate`, `onFixedUpdate` and `onLateUpdate` |
|
|
40
|
+
| DOM input listeners for the 3D view | The sketch | `ctx.input`, which the page fills through shared memory |
|
|
41
|
+
| Camera controls | The sketch | `@null3d/controls` |
|
|
42
|
+
| HTML UI, menus, a HUD, lil-gui panels | The page | Messages to and from the sketch |
|
|
43
|
+
| `CSS2DRenderer` and `CSS3DRenderer` labels | Both | `ui.trackLabel` in the sketch and `engine.labels.bind` on the page |
|
|
44
|
+
| Positional audio | Both | `@null3d/sound`: the sketch places the sounds, and the page plays them |
|
|
45
|
+
| Video elements | The page | `engine.registerVideo` on the page and `textures.fromVideo` in the sketch |
|
|
46
|
+
| `localStorage`, cookies and URL parameters | The page | Send the sketch what it needs at the start |
|
|
47
|
+
| `fetch` of JSON, binary data and models | The sketch | Workers have `fetch`, and relative addresses resolve against the page |
|
|
48
|
+
| Physics libraries | The sketch | WebAssembly physics runs in workers |
|
|
49
|
+
| Analytics, ads and routing | The page | No change |
|
|
50
|
+
|
|
51
|
+
Workers have no `localStorage`, but they have IndexedDB. A sketch can keep a cache of generated geometry there, as arrays.
|
|
52
|
+
|
|
53
|
+
## Converting the loop
|
|
54
|
+
|
|
55
|
+
A three.js loop:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const clock = new THREE.Clock();
|
|
59
|
+
function animate() {
|
|
60
|
+
requestAnimationFrame(animate);
|
|
61
|
+
const dt = clock.getDelta();
|
|
62
|
+
updatePlayer(dt);
|
|
63
|
+
updateEnemies(dt);
|
|
64
|
+
physicsWorld.step(1 / 60, dt, 3);
|
|
65
|
+
followCamera(player);
|
|
66
|
+
controls.update();
|
|
67
|
+
composer.render();
|
|
68
|
+
}
|
|
69
|
+
animate();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The same loop in a sketch:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
return {
|
|
76
|
+
onFixedUpdate() {
|
|
77
|
+
physicsWorld.step();
|
|
78
|
+
syncBodies();
|
|
79
|
+
},
|
|
80
|
+
onUpdate(dt) {
|
|
81
|
+
updatePlayer(dt);
|
|
82
|
+
updateEnemies(dt);
|
|
83
|
+
controls.update(dt);
|
|
84
|
+
},
|
|
85
|
+
onLateUpdate() {
|
|
86
|
+
followCamera(player);
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
- `clock.getDelta()` becomes the `dt` argument, which `ctx.time.dt` also holds. `clock.getElapsedTime()` becomes `ctx.time.now`.
|
|
92
|
+
- 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)`.
|
|
93
|
+
- 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.
|
|
94
|
+
- The render call and `composer.render()` go away. The engine draws each frame by itself.
|
|
95
|
+
- Code that ran every N frames can test `ctx.time.frame % N === 0`.
|
|
96
|
+
|
|
97
|
+
[Sketch API](../api/sketch.md#the-callbacks) gives the order of the callbacks.
|
|
98
|
+
|
|
99
|
+
## Per-object classes become arrays
|
|
100
|
+
|
|
101
|
+
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:
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
class Enemy {
|
|
105
|
+
constructor(scene) {
|
|
106
|
+
this.mesh = new THREE.Mesh(geo, mat);
|
|
107
|
+
this.vel = new THREE.Vector3();
|
|
108
|
+
scene.add(this.mesh);
|
|
109
|
+
}
|
|
110
|
+
update(dt, player) {
|
|
111
|
+
const dir = player.position.clone().sub(this.mesh.position).normalize(); // allocates
|
|
112
|
+
this.vel.lerp(dir, 0.1);
|
|
113
|
+
this.mesh.position.addScaledVector(this.vel, dt);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
enemies.forEach((e) => e.update(dt, player));
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
In null3D, the enemies become one instance batch, and their state becomes typed arrays:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
const N = 2000;
|
|
123
|
+
const enemies = scene.createInstances(enemyMesh, N, { material: enemyMaterial, dynamic: true });
|
|
124
|
+
const vel = new Float32Array(N * 3);
|
|
125
|
+
const playerPos = new Float32Array(3);
|
|
126
|
+
|
|
127
|
+
function updateEnemies(dt: number) {
|
|
128
|
+
player.getPosition(playerPos);
|
|
129
|
+
const p = enemies.positions;
|
|
130
|
+
for (let i = 0; i < N; i++) {
|
|
131
|
+
const o = i * 3;
|
|
132
|
+
let dx = playerPos[0] - p[o];
|
|
133
|
+
let dy = playerPos[1] - p[o + 1];
|
|
134
|
+
let dz = playerPos[2] - p[o + 2];
|
|
135
|
+
const len = Math.sqrt(dx * dx + dy * dy + dz * dz) || 1;
|
|
136
|
+
dx /= len; dy /= len; dz /= len;
|
|
137
|
+
vel[o] += (dx - vel[o]) * 0.1;
|
|
138
|
+
vel[o + 1] += (dy - vel[o + 1]) * 0.1;
|
|
139
|
+
vel[o + 2] += (dz - vel[o + 2]) * 0.1;
|
|
140
|
+
p[o] += vel[o] * dt;
|
|
141
|
+
p[o + 1] += vel[o + 1] * dt;
|
|
142
|
+
p[o + 2] += vel[o + 2] * dt;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
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`.
|
|
148
|
+
|
|
149
|
+
## Messages between the page and the sketch
|
|
150
|
+
|
|
151
|
+
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.
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
// sketch.ts
|
|
155
|
+
page.post('score', { value: score }); // when the score changes
|
|
156
|
+
page.onMessage((name, data) => {
|
|
157
|
+
if (name === 'settings') applySettings(data);
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
// page.ts
|
|
163
|
+
engine.onSketchMessage((name, data) => {
|
|
164
|
+
if (name === 'score') scoreElement.textContent = String((data as { value: number }).value);
|
|
165
|
+
});
|
|
166
|
+
engine.postToSketch('settings', { volume: 0.8 });
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- 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.
|
|
170
|
+
- HTML elements that follow objects use `ui.trackLabel`, which sends no messages.
|
|
171
|
+
- Give the message names a TypeScript union type that both modules import, so a typo fails at compile time.
|
|
172
|
+
- Large data, such as a level file, goes straight to the sketch with `fetch`, not through the page.
|
|
173
|
+
- Engine objects belong to the sketch. To name one in a message, send a name or a number that stands for it.
|
|
174
|
+
|
|
175
|
+
[Messages between sketch and page](../api/page.md) gives the full rules, such as the order of messages and when they arrive.
|
|
176
|
+
|
|
177
|
+
## Input and camera controls
|
|
178
|
+
|
|
179
|
+
The page forwards keyboard, pointer, touch and gamepad input to the sketch through shared memory. The sketch reads it in `ctx.input`.
|
|
180
|
+
|
|
181
|
+
| three.js | null3D |
|
|
182
|
+
| --- | --- |
|
|
183
|
+
| `addEventListener('pointermove', ...)` with screen math | `input.pointer.ndcX` and `ndcY`, or `input.pointer.x` and `y` in CSS pixels |
|
|
184
|
+
| `addEventListener('keydown', ...)` with a key state object | `input.isDown('KeyW')` and `input.wasPressed('Space')` |
|
|
185
|
+
| `addEventListener('touchstart', ...)` with a list of touches | `input.touches`, with `touch-action: none` on the canvas |
|
|
186
|
+
| Gamepad API polling | `input.isDown('GamepadA')` and `input.value('GamepadLeftStickRight')` |
|
|
187
|
+
| `OrbitControls` and `MapControls` | `createOrbitControls(ctx, camera, options)` and `createMapControls`, with `controls.update(dt)` each frame |
|
|
188
|
+
| `FlyControls` and `FirstPersonControls` | `createFlyControls` and `createFirstPersonControls`, with the same option names |
|
|
189
|
+
| `PointerLockControls` | `createFirstPersonControls` with `movementSpeed: 0`, and `engine.requestPointerLock()` in a click handler on the page |
|
|
190
|
+
| `controls.listenToKeyEvents(window)` | `controls.listenToKeyEvents()`, with no element: the controls read the keys from the sketch's input |
|
|
191
|
+
| Clicks on UI buttons over the canvas | Handle them on the page, and send the action to the sketch |
|
|
192
|
+
|
|
193
|
+
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`:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
canvas.addEventListener('wheel', (e) => e.preventDefault(), { passive: false });
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
[Input](../api/input.md) and [Controls](../api/controls.md) give the details.
|
|
200
|
+
|
|
201
|
+
## Libraries in a port
|
|
202
|
+
|
|
203
|
+
| Library | What to do |
|
|
204
|
+
| --- | --- |
|
|
205
|
+
| lil-gui, dat.gui, Tweakpane | Keep them on the page, and send the values to the sketch |
|
|
206
|
+
| stats.js | `debug.stats(true)` in the sketch, or `createEngine({ stats: true })` on the page; `engine.measure(seconds)` gives percentiles |
|
|
207
|
+
| GSAP, tween.js | For a scene value, `scene.animateProperty`. DOM tweens stay on the page |
|
|
208
|
+
| cannon-es, Rapier, Ammo | Run them in the sketch, and copy the transforms into dynamic objects or batches after each step |
|
|
209
|
+
| three-mesh-bvh | Delete it, because raycasts use the engine's own acceleration structures |
|
|
210
|
+
| troika-three-text | HTML labels, a texture with the text drawn in it, or a text mesh baked into glTF |
|
|
211
|
+
| pmndrs postprocessing | `post.set` and `post.addEffect`, as [Porting post-processing](threejs-postprocessing.md) shows |
|
|
212
|
+
| three-stdlib and other addons | Look each import up in the [mapping](threejs-mapping.md), since many are built into the engine |
|
|
213
|
+
|
|
214
|
+
[Using a physics library](../guides/physics.md) shows a physics library in a sketch.
|
|
215
|
+
|
|
216
|
+
## A sketch on the main thread
|
|
217
|
+
|
|
218
|
+
`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.
|
|
219
|
+
|
|
220
|
+
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.
|
|
221
|
+
|
|
222
|
+
## Related pages
|
|
14
223
|
|
|
15
|
-
|
|
224
|
+
- [Porting from three.js](threejs-overview.md)
|
|
225
|
+
- [Architecture: threads and the frame](../concepts/architecture.md)
|
|
226
|
+
- [Messages between sketch and page](../api/page.md)
|
|
227
|
+
- [Sketch API](../api/sketch.md)
|
|
228
|
+
- [UI, HTML overlays and labels](../guides/ui-overlays.md)
|
|
229
|
+
- [Porting React Three Fiber](react-three-fiber.md)
|