@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/lib/scene/background.js
CHANGED
|
@@ -11,8 +11,8 @@ import { ShaderPreloads } from './shader-preloads.js';
|
|
|
11
11
|
import { Texture } from './textures.js';
|
|
12
12
|
/**
|
|
13
13
|
* A cube map of six images, which `assets.loadCubemap` loads, for a sky box behind every object:
|
|
14
|
-
* `scene.setBackground(cubemap)
|
|
15
|
-
*
|
|
14
|
+
* `scene.setBackground(cubemap)`. The engine reads the faces as seen from inside the cube, mirrored
|
|
15
|
+
* across x. It does not light the scene: light comes from an environment.
|
|
16
16
|
*
|
|
17
17
|
* @category api/assets
|
|
18
18
|
*/
|
|
@@ -287,6 +287,9 @@ export class SceneBackground {
|
|
|
287
287
|
else {
|
|
288
288
|
writeSky(values, source.sky);
|
|
289
289
|
}
|
|
290
|
+
// A sky with clouds draws the cloud tier over it where the settings march the tier.
|
|
291
|
+
if (kind === BACKGROUND_KIND_SKY || kind === BACKGROUND_KIND_ATMOSPHERE)
|
|
292
|
+
this.shaders.needClouds(values[BACKGROUND_VALUE_CLOUD_COVERAGE]);
|
|
290
293
|
const status = this.core.glue.setBackgroundSource(kind, texture);
|
|
291
294
|
this.core.check(status, CALL, 'a texture', true);
|
|
292
295
|
}
|
package/lib/scene/color.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A color: a hex string such as `'#4a8cff'` or `'#48f'`, a number such as `0x4a8cff`, or three
|
|
3
|
-
* linear components from 0 to 1, such as `[1, 0.26, 0.05]`. Hex values are sRGB, as on the web and
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* linear components from 0 to 1, such as `[1, 0.26, 0.05]`. Hex values are sRGB, as on the web, and
|
|
4
|
+
* the engine converts them to linear values. The color helpers, such as `color.fromHsl`, give
|
|
5
|
+
* linear components.
|
|
6
6
|
*
|
|
7
7
|
* @category api/materials
|
|
8
8
|
*/
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
// The downloads behind the sketch's loading calls (assets.ts): each file fetched with one retry, its
|
|
2
|
+
// bytes counted for the progress handlers, and its failure given its code. Files that `preload` or a
|
|
3
|
+
// sketch's download list fetched wait in memory until a load takes them, and loads of one address at
|
|
4
|
+
// the same time share one download; the HTTP cache keeps everything else. A live sketch's list
|
|
5
|
+
// starts as soon as its module loads, before the engine's core has arrived, so this part of the
|
|
6
|
+
// loader needs no core. The list keeps few of its files downloading at once, so the engine's own
|
|
7
|
+
// start files find a free connection on a server that gives a page few. A development build also
|
|
8
|
+
// watches the sketch's own loads during the setup, and says when one waited for another
|
|
9
|
+
// (serial-loads.ts).
|
|
10
|
+
import { DEV } from '../errors/checks.js';
|
|
11
|
+
import { EngineError } from '../errors/engine-error.js';
|
|
12
|
+
import { reasonOf } from '../errors/message.js';
|
|
13
|
+
import { SerialLoads } from './serial-loads.js';
|
|
14
|
+
/** How long a download waits before its one retry, in ms. */
|
|
15
|
+
const RETRY_DELAY_MS = 250;
|
|
16
|
+
/** The call that a failed download of a listed file names, which the load that takes it rethrows. */
|
|
17
|
+
const LIST_CALL = 'the downloads of defineSketch';
|
|
18
|
+
/** Fetches a file as the page's other requests are fetched. */
|
|
19
|
+
const fetchFile = (address) => fetch(address);
|
|
20
|
+
/**
|
|
21
|
+
* Waits before a download's retry. A tunnel or a proxy can refuse a session's first request while
|
|
22
|
+
* the same file serves a moment later, and a short wait lets it answer again.
|
|
23
|
+
*/
|
|
24
|
+
function waitBeforeRetry() {
|
|
25
|
+
return new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS));
|
|
26
|
+
}
|
|
27
|
+
/** @internal The downloads of one engine's loading calls, and the bytes they counted. */
|
|
28
|
+
export class Downloads {
|
|
29
|
+
base;
|
|
30
|
+
/** Downloads that `preload` or the list started, by address, which the first load of each takes. */
|
|
31
|
+
preloaded = new Map();
|
|
32
|
+
/** Downloads under way, by address, which loads at the same time share. */
|
|
33
|
+
pending = new Map();
|
|
34
|
+
/** The listed files that have not started, in the list's order, by address. */
|
|
35
|
+
queued = new Map();
|
|
36
|
+
/**
|
|
37
|
+
* The listed files that a load asked for while the most at once downloaded, in the order of
|
|
38
|
+
* their loads, by address: each one's download, and the start that the queue calls.
|
|
39
|
+
*/
|
|
40
|
+
asked = new Map();
|
|
41
|
+
/** The listed files that download now, whether the list or a load started them. */
|
|
42
|
+
listedActive = 0;
|
|
43
|
+
/** The most listed files that download at once, while no load waits for one. */
|
|
44
|
+
listedAtOnce = 0;
|
|
45
|
+
/** The most listed files that download at once, those that loads asked for included. */
|
|
46
|
+
listedMost = 0;
|
|
47
|
+
/** Fetches a listed file that no load has asked for yet. */
|
|
48
|
+
fetchListed = fetchFile;
|
|
49
|
+
handlers = new Set();
|
|
50
|
+
/** The handlers in registration order, for the report to walk by index. */
|
|
51
|
+
handlerList = [];
|
|
52
|
+
/** Bytes that have arrived, and the bytes of the responses whose length is known so far. */
|
|
53
|
+
loadedBytes = 0;
|
|
54
|
+
totalBytes = 0;
|
|
55
|
+
/** In a development build, the watch for the sketch's loads that wait for one another. */
|
|
56
|
+
serial = DEV ? new SerialLoads() : undefined;
|
|
57
|
+
/** @param base The page's address, which tells another origin's files from the page's own. */
|
|
58
|
+
constructor(base) {
|
|
59
|
+
this.base = base;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* In a development build, gives the test of whether the sketch's loads are watched now: during
|
|
63
|
+
* the setup of a live start whose sketch did not turn the warning off.
|
|
64
|
+
*/
|
|
65
|
+
watch(watching) {
|
|
66
|
+
if (DEV && this.serial)
|
|
67
|
+
this.serial.watching = watching;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Takes the downloads of a sketch's list. `started` holds the responses of its first addresses,
|
|
71
|
+
* which the thread fetched with `fetchListed` as the sketch's module loaded. The rest start in
|
|
72
|
+
* the list's order as those finish, `atOnce` at a time. A load that asks for one that has not
|
|
73
|
+
* started moves it ahead of the others, and up to `most` download while loads wait for them.
|
|
74
|
+
*/
|
|
75
|
+
list(addresses, started, atOnce, most, fetchListed) {
|
|
76
|
+
this.listedAtOnce = atOnce;
|
|
77
|
+
this.listedMost = most;
|
|
78
|
+
this.fetchListed = fetchListed;
|
|
79
|
+
addresses.forEach((address, at) => {
|
|
80
|
+
const response = started[at];
|
|
81
|
+
if (response)
|
|
82
|
+
this.startListed(address, response);
|
|
83
|
+
else
|
|
84
|
+
this.queued.set(address.href, address);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A file for one of the sketch's own loading calls. A development build first checks whether
|
|
89
|
+
* its download starts only after another load's file arrived.
|
|
90
|
+
*/
|
|
91
|
+
load(address, call) {
|
|
92
|
+
if (DEV)
|
|
93
|
+
this.serial?.starts(address, call, this.starts(address));
|
|
94
|
+
const download = this.file(address, call);
|
|
95
|
+
if (DEV)
|
|
96
|
+
this.serial?.follow(download, address);
|
|
97
|
+
return download;
|
|
98
|
+
}
|
|
99
|
+
/** In a development build, notes the text of a JSON file that a load read. */
|
|
100
|
+
noteText(address, text) {
|
|
101
|
+
if (DEV)
|
|
102
|
+
this.serial?.read(address, text);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* In a development build, notes a call of the sketch's that made its data with no file, such as
|
|
106
|
+
* a built-in environment, as it ends: a download that starts only after it could have run beside
|
|
107
|
+
* it.
|
|
108
|
+
*/
|
|
109
|
+
noteMade(call) {
|
|
110
|
+
if (DEV)
|
|
111
|
+
this.serial?.made(call);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A file that `preload` or the list downloaded, a download of it under way, or a download of it
|
|
115
|
+
* now. A listed file that has not started becomes the load's own download, which starts at once
|
|
116
|
+
* unless the most listed files download, and then goes ahead of the list's other files.
|
|
117
|
+
*/
|
|
118
|
+
file(address, call) {
|
|
119
|
+
const key = address.href;
|
|
120
|
+
const preloaded = this.preloaded.get(key);
|
|
121
|
+
if (preloaded) {
|
|
122
|
+
this.preloaded.delete(key);
|
|
123
|
+
return preloaded;
|
|
124
|
+
}
|
|
125
|
+
const waiting = this.asked.get(key);
|
|
126
|
+
if (waiting)
|
|
127
|
+
return waiting.download;
|
|
128
|
+
if (this.queued.delete(key))
|
|
129
|
+
return this.ask(address, call);
|
|
130
|
+
return this.share(address, call, fetchFile);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Downloads files ahead of their loads, all at once, and resolves when every one has arrived.
|
|
134
|
+
* The next load of each address takes its file from memory. A failed file is not kept: a later
|
|
135
|
+
* load downloads it again.
|
|
136
|
+
*/
|
|
137
|
+
async preload(addresses, call) {
|
|
138
|
+
const downloads = addresses.map((address) => {
|
|
139
|
+
const key = address.href;
|
|
140
|
+
let download = this.preloaded.get(key) ?? this.asked.get(key)?.download;
|
|
141
|
+
if (!download) {
|
|
142
|
+
if (DEV)
|
|
143
|
+
this.serial?.starts(address, call, this.starts(address));
|
|
144
|
+
this.queued.delete(key);
|
|
145
|
+
download = this.share(address, call, fetchFile);
|
|
146
|
+
this.preloaded.set(key, download);
|
|
147
|
+
download.then(undefined, () => this.preloaded.delete(key));
|
|
148
|
+
}
|
|
149
|
+
return download;
|
|
150
|
+
});
|
|
151
|
+
await Promise.all(downloads);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Calls `handler` as a download's bytes arrive and when one finishes or fails. Returns a
|
|
155
|
+
* function that removes the handler.
|
|
156
|
+
*/
|
|
157
|
+
onProgress(handler) {
|
|
158
|
+
if (!this.handlers.has(handler)) {
|
|
159
|
+
this.handlers.add(handler);
|
|
160
|
+
this.handlerList.push(handler);
|
|
161
|
+
}
|
|
162
|
+
return () => {
|
|
163
|
+
if (!this.handlers.delete(handler))
|
|
164
|
+
return;
|
|
165
|
+
// A new list, so a report that is walking the old one keeps its place and skips this
|
|
166
|
+
// handler by the set's membership.
|
|
167
|
+
this.handlerList = this.handlerList.filter((each) => each !== handler);
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/** True when a load of `address` would start a download, rather than take one. */
|
|
171
|
+
starts(address) {
|
|
172
|
+
const key = address.href;
|
|
173
|
+
return !this.preloaded.has(key) && !this.pending.has(key) && !this.asked.has(key);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The download of a listed file that a load asked for before the list started it: now, while
|
|
177
|
+
* fewer than the most listed files download, and otherwise once the queue reaches it.
|
|
178
|
+
*/
|
|
179
|
+
ask(address, call) {
|
|
180
|
+
if (this.listedActive < this.listedMost)
|
|
181
|
+
return this.listed(this.share(address, call, fetchFile));
|
|
182
|
+
let start = () => { };
|
|
183
|
+
const download = new Promise((resolve, reject) => {
|
|
184
|
+
start = () => this.listed(this.share(address, call, fetchFile)).then(resolve, reject);
|
|
185
|
+
});
|
|
186
|
+
this.asked.set(address.href, { download, start });
|
|
187
|
+
return download;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Starts a listed file that no load has asked for, from `response` when the thread fetched it
|
|
191
|
+
* already. A load takes it, or the error it failed with.
|
|
192
|
+
*/
|
|
193
|
+
startListed(address, response) {
|
|
194
|
+
const download = this.listed(this.share(address, LIST_CALL, this.fetchListed, response));
|
|
195
|
+
// Its failure reaches the load that takes it, and no unhandled rejection reports it before.
|
|
196
|
+
download.catch(() => { });
|
|
197
|
+
this.preloaded.set(address.href, download);
|
|
198
|
+
}
|
|
199
|
+
/** Counts a listed file's download while it runs, and starts the next one when it ends. */
|
|
200
|
+
listed(download) {
|
|
201
|
+
this.listedActive++;
|
|
202
|
+
const next = () => {
|
|
203
|
+
this.listedActive--;
|
|
204
|
+
this.startQueued();
|
|
205
|
+
};
|
|
206
|
+
download.then(next, next);
|
|
207
|
+
return download;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Starts the listed files that loads asked for, in the order of their loads, while fewer than
|
|
211
|
+
* the most download, then the others in the list's order while fewer than the most at once do.
|
|
212
|
+
*/
|
|
213
|
+
startQueued() {
|
|
214
|
+
for (const [key, waiting] of this.asked) {
|
|
215
|
+
if (this.listedActive >= this.listedMost)
|
|
216
|
+
return;
|
|
217
|
+
this.asked.delete(key);
|
|
218
|
+
waiting.start();
|
|
219
|
+
}
|
|
220
|
+
for (const [key, address] of this.queued) {
|
|
221
|
+
if (this.listedActive >= this.listedAtOnce)
|
|
222
|
+
return;
|
|
223
|
+
this.queued.delete(key);
|
|
224
|
+
this.startListed(address);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/** The download of an address under way, or a new one with `fetch`. */
|
|
228
|
+
share(address, call, fetch, response) {
|
|
229
|
+
const key = address.href;
|
|
230
|
+
let download = this.pending.get(key);
|
|
231
|
+
if (!download) {
|
|
232
|
+
download = this.download(address, call, fetch, response);
|
|
233
|
+
this.pending.set(key, download);
|
|
234
|
+
if (DEV)
|
|
235
|
+
this.serial?.begin(download);
|
|
236
|
+
const forget = () => this.pending.delete(key);
|
|
237
|
+
download.then(forget, forget);
|
|
238
|
+
}
|
|
239
|
+
return download;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Downloads a file with `fetch`, counts its bytes for the progress handlers, and gives failures
|
|
243
|
+
* their codes. `started` is the response of a fetch that began before, which takes the first
|
|
244
|
+
* attempt's place. A fetch that fails at the network level, or a server fault, gets one retry
|
|
245
|
+
* after a short wait; a refused answer and a body that will not read are reported as they come.
|
|
246
|
+
*/
|
|
247
|
+
async download(address, call, fetch, started) {
|
|
248
|
+
// The bytes this download's response announced, and whether it delivered them.
|
|
249
|
+
let announced = 0;
|
|
250
|
+
let delivered = false;
|
|
251
|
+
try {
|
|
252
|
+
let response;
|
|
253
|
+
let failure;
|
|
254
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
255
|
+
response = undefined;
|
|
256
|
+
failure = undefined;
|
|
257
|
+
try {
|
|
258
|
+
response = attempt === 0 && started ? await started : await fetch(address);
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
failure = error;
|
|
262
|
+
if (attempt === 0) {
|
|
263
|
+
await waitBeforeRetry();
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
if (response.status >= 500 && attempt === 0) {
|
|
269
|
+
await waitBeforeRetry();
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
if (!response) {
|
|
275
|
+
// Fetch gives no reason for a blocked response, so another origin's refusal and a
|
|
276
|
+
// network failure look alike. Another origin's file usually fails for its headers.
|
|
277
|
+
if (address.origin !== new URL(this.base).origin)
|
|
278
|
+
throw new EngineError('E1413', `${call}() could not read ${address}: its server did not allow this page to read it, or could not be reached (${reasonOf(failure)}).`);
|
|
279
|
+
throw new EngineError('E1411', `${call}() could not download ${address}: ${reasonOf(failure)}.`);
|
|
280
|
+
}
|
|
281
|
+
if (!response.ok)
|
|
282
|
+
throw new EngineError('E1411', `${call}() could not download ${address}: HTTP ${response.status}.`);
|
|
283
|
+
const length = Number(response.headers.get('content-length'));
|
|
284
|
+
if (Number.isFinite(length) && length > 0) {
|
|
285
|
+
this.totalBytes += length;
|
|
286
|
+
announced = length;
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
// A page that watches progress reads the body in chunks, which is the only way to see
|
|
290
|
+
// the bytes before the whole file arrives. A listed file reads in chunks too, since a
|
|
291
|
+
// handler that the setup adds later still sees its bytes arrive. Otherwise the browser
|
|
292
|
+
// makes the blob in one step.
|
|
293
|
+
if ((this.handlers.size > 0 || call === LIST_CALL) && response.body) {
|
|
294
|
+
const streamed = await this.read(response, address);
|
|
295
|
+
delivered = true;
|
|
296
|
+
return streamed;
|
|
297
|
+
}
|
|
298
|
+
const blob = await response.blob();
|
|
299
|
+
this.loadedBytes += blob.size;
|
|
300
|
+
this.report(address.href);
|
|
301
|
+
delivered = true;
|
|
302
|
+
return blob;
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
throw new EngineError('E1411', `${call}() could not download ${address}: ${reasonOf(error)}.`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
finally {
|
|
309
|
+
// A file that failed counts as done, so a loading bar reaches its end: the bytes its
|
|
310
|
+
// response announced still count, and a failure that announced none counts one byte. A
|
|
311
|
+
// file that arrived reported as its last bytes came, so it reports nothing here.
|
|
312
|
+
if (!delivered) {
|
|
313
|
+
if (announced > 0)
|
|
314
|
+
this.loadedBytes += announced;
|
|
315
|
+
else {
|
|
316
|
+
this.loadedBytes += 1;
|
|
317
|
+
this.totalBytes += 1;
|
|
318
|
+
}
|
|
319
|
+
this.report(address.href);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/** Reads a response's body in chunks, counting each as it arrives. */
|
|
324
|
+
async read(response, address) {
|
|
325
|
+
const reader = response.body?.getReader();
|
|
326
|
+
if (!reader) {
|
|
327
|
+
const blob = await response.blob();
|
|
328
|
+
this.loadedBytes += blob.size;
|
|
329
|
+
return blob;
|
|
330
|
+
}
|
|
331
|
+
const chunks = [];
|
|
332
|
+
for (;;) {
|
|
333
|
+
const { done, value } = await reader.read();
|
|
334
|
+
if (done)
|
|
335
|
+
break;
|
|
336
|
+
chunks.push(value);
|
|
337
|
+
this.loadedBytes += value.byteLength;
|
|
338
|
+
this.report(address.href);
|
|
339
|
+
}
|
|
340
|
+
return new Blob(chunks);
|
|
341
|
+
}
|
|
342
|
+
/** The bytes, total and address that the handlers last saw, so a repeat reports nothing. */
|
|
343
|
+
reportedLoaded = 0;
|
|
344
|
+
reportedTotal = 0;
|
|
345
|
+
reportedUrl = null;
|
|
346
|
+
report(url) {
|
|
347
|
+
// A chunk's report and the download's end can hold the same numbers; the handlers see one.
|
|
348
|
+
if (url === this.reportedUrl &&
|
|
349
|
+
this.loadedBytes === this.reportedLoaded &&
|
|
350
|
+
this.totalBytes === this.reportedTotal)
|
|
351
|
+
return;
|
|
352
|
+
this.reportedLoaded = this.loadedBytes;
|
|
353
|
+
this.reportedTotal = this.totalBytes;
|
|
354
|
+
this.reportedUrl = url;
|
|
355
|
+
// The handlers as this report began: one removed during it is skipped. An addition is
|
|
356
|
+
// appended to the same list, so the walk reaches it unless an earlier removal replaced the
|
|
357
|
+
// list. Walking by index makes no iterator per report.
|
|
358
|
+
const handlers = this.handlerList;
|
|
359
|
+
for (let i = 0; i < handlers.length; i++) {
|
|
360
|
+
const handler = handlers[i];
|
|
361
|
+
if (handler === undefined || !this.handlers.has(handler))
|
|
362
|
+
continue;
|
|
363
|
+
try {
|
|
364
|
+
handler(this.loadedBytes, this.totalBytes, url);
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
console.error(error);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export type EnvironmentFormat = 'rgb9e5ufloat' | 'rgba16float';
|
|
8
8
|
/**
|
|
9
|
-
* The names of the built-in environments that `assets.builtinEnvironment` loads. `room` is
|
|
10
|
-
* room
|
|
11
|
-
*
|
|
9
|
+
* The names of the built-in environments that `assets.builtinEnvironment` loads. `room` is a
|
|
10
|
+
* white room with six boxes and glowing panels, lit by one point light. It is slightly blurred,
|
|
11
|
+
* and gives soft, neutral light.
|
|
12
12
|
*
|
|
13
13
|
* @category api/assets
|
|
14
14
|
*/
|
|
@@ -17,7 +17,7 @@ export type BuiltinEnvironmentName = 'room';
|
|
|
17
17
|
* An environment map, which `assets.loadEnvironment` and `assets.builtinEnvironment` load. It
|
|
18
18
|
* holds the light around a scene from every direction, filtered for each roughness, and the
|
|
19
19
|
* diffuse light that it gives. Give it to `scene.setEnvironment`, which lights standard materials
|
|
20
|
-
* with it
|
|
20
|
+
* with it.
|
|
21
21
|
*
|
|
22
22
|
* @category api/assets
|
|
23
23
|
*/
|
|
@@ -45,13 +45,13 @@ export declare class Environment {
|
|
|
45
45
|
*/
|
|
46
46
|
export interface EnvironmentOptions {
|
|
47
47
|
/**
|
|
48
|
-
* The factor of the environment's light on every surface, 0 or more
|
|
49
|
-
* `
|
|
48
|
+
* The factor of the environment's light on every surface, 0 or more. A material's
|
|
49
|
+
* `envIntensity` multiplies it. The default is 1.
|
|
50
50
|
*/
|
|
51
51
|
intensity?: number;
|
|
52
52
|
/**
|
|
53
53
|
* The turn of the environment about the scene, as Euler angles in radians in the order X, Y,
|
|
54
|
-
* Z
|
|
54
|
+
* Z. The default is `[0, 0, 0]`.
|
|
55
55
|
*/
|
|
56
56
|
rotation?: readonly [number, number, number];
|
|
57
57
|
}
|
package/lib/scene/environment.js
CHANGED
|
@@ -10,7 +10,7 @@ import { ENVIRONMENT_VALUE_COUNT, ENVIRONMENT_VALUE_INTENSITY, ENVIRONMENT_VALUE
|
|
|
10
10
|
* An environment map, which `assets.loadEnvironment` and `assets.builtinEnvironment` load. It
|
|
11
11
|
* holds the light around a scene from every direction, filtered for each roughness, and the
|
|
12
12
|
* diffuse light that it gives. Give it to `scene.setEnvironment`, which lights standard materials
|
|
13
|
-
* with it
|
|
13
|
+
* with it.
|
|
14
14
|
*
|
|
15
15
|
* @category api/assets
|
|
16
16
|
*/
|
package/lib/scene/fog.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ import type { CoreMemory } from './memory.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* How fog thickens with distance. Exponential fog, `'exponential'`, follows light through an even
|
|
5
5
|
* haze. An object at distance d takes the fog color by a factor of 1 - exp(-density × d).
|
|
6
|
-
* Exponential squared fog, `'exp2'`, thickens with the square of the distance
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* Exponential squared fog, `'exp2'`, thickens with the square of the distance:
|
|
7
|
+
* 1 - exp(-(density × d)²). Linear fog, `'linear'`, is clear up to `near` and hides objects from
|
|
8
|
+
* `far`, with a smooth step between them.
|
|
9
9
|
*
|
|
10
10
|
* @category api/scene
|
|
11
11
|
*/
|
|
@@ -17,7 +17,7 @@ export type FogCurve = 'exponential' | 'exp2' | 'linear';
|
|
|
17
17
|
* @category api/scene
|
|
18
18
|
*/
|
|
19
19
|
export interface FogOptions {
|
|
20
|
-
/** The fog's color.
|
|
20
|
+
/** The fog's color. A scene whose background takes the fog needs no other color for it. */
|
|
21
21
|
color: ColorInput;
|
|
22
22
|
/** How the fog thickens with distance. The default is `'exponential'`. */
|
|
23
23
|
curve?: FogCurve;
|
|
@@ -52,6 +52,13 @@ export interface FogOptions {
|
|
|
52
52
|
* smaller glow. The default is 8.
|
|
53
53
|
*/
|
|
54
54
|
sunGlowExponent?: number;
|
|
55
|
+
/**
|
|
56
|
+
* How much of the fog the background takes, 0 to 1. At 1, the sky and a cube-map background take
|
|
57
|
+
* the same fog as a surface at the camera's far plane, so a scene's haze carries on over the sky
|
|
58
|
+
* it meets and the far edge of the land and the sky read as one. The camera's far plane must be
|
|
59
|
+
* within the fog's reach for the haze to be full. The default is 0: the background takes no fog.
|
|
60
|
+
*/
|
|
61
|
+
background?: number;
|
|
55
62
|
/**
|
|
56
63
|
* Lights the fog with the main directional light and the point and spot lights, through their
|
|
57
64
|
* shadows: sun rays through gaps in trees, and glowing cones under lamps and headlights. `true`
|
package/lib/scene/fog.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// The scene's fog. The core keeps it, and the lit and unlit shaders mix each object's linear color
|
|
2
2
|
// toward the fog color by its straight-line distance from the camera, along a curve. The fog can
|
|
3
|
-
// thin with height and glow toward the main directional light. The
|
|
4
|
-
//
|
|
3
|
+
// thin with height and glow toward the main directional light. The sky and the background take the
|
|
4
|
+
// fog that a surface at the camera's far plane takes, at the share that a scene sets with its
|
|
5
|
+
// `background` option. The volumetric fog lights the same fog with the sun and the point and spot lights, through their
|
|
5
6
|
// shadows, in a grid that follows the camera's view (D-133).
|
|
6
7
|
import { checkNumber, DEV } from '../errors/checks.js';
|
|
7
8
|
import { EngineError } from '../errors/engine-error.js';
|
|
@@ -33,6 +34,7 @@ const VALUE_NAMES = [
|
|
|
33
34
|
'heightFalloff',
|
|
34
35
|
'sunGlow',
|
|
35
36
|
'sunGlowExponent',
|
|
37
|
+
'background',
|
|
36
38
|
];
|
|
37
39
|
const CALL = 'setFog';
|
|
38
40
|
const FOG = { describe: () => 'the fog' };
|
|
@@ -67,6 +69,9 @@ function checkFog(fog, values) {
|
|
|
67
69
|
checkNotNegative('sun glow', sunGlow);
|
|
68
70
|
if (!(sunGlowExponent > 0))
|
|
69
71
|
throw new EngineError('E1108', `${CALL}() got the sun glow exponent ${sunGlowExponent}; it takes a number above 0.`);
|
|
72
|
+
const background = fog.background;
|
|
73
|
+
if (background !== undefined && !(background >= 0 && background <= 1))
|
|
74
|
+
throw new EngineError('E1108', `${CALL}() got the background share ${background}; it takes a number from 0 to 1.`);
|
|
70
75
|
}
|
|
71
76
|
/**
|
|
72
77
|
* Throws E1108 for a volumetric fog option out of its range, and E1203 for a value that is not
|
|
@@ -108,6 +113,7 @@ export function setSceneFog(core, fog) {
|
|
|
108
113
|
values[FIRST + 4] = fog.heightFalloff ?? 0;
|
|
109
114
|
values[FIRST + 5] = fog.sunGlow ?? 0;
|
|
110
115
|
values[FIRST + 6] = fog.sunGlowExponent ?? DEFAULT_SUN_GLOW_EXPONENT;
|
|
116
|
+
values[FIRST + 7] = Math.min(1, Math.max(0, fog.background ?? 0));
|
|
111
117
|
// `volumetric: true` takes every default. Each number of the sketch's own options goes into a
|
|
112
118
|
// local, read only where the object holds it: a read that can give `undefined` boxes a fraction.
|
|
113
119
|
const { volumetric } = fog;
|
|
@@ -114,6 +114,25 @@ export declare class FrameCameras {
|
|
|
114
114
|
* view.
|
|
115
115
|
*/
|
|
116
116
|
worldToScreen(camera: FrameLens, point: Vec3Like, out: Vec3Like, call: string): void;
|
|
117
|
+
/**
|
|
118
|
+
* Writes the ray from `camera` as it stands through the point (`x`, `y`) into `out`, for a view
|
|
119
|
+
* that draws into `rect` of the canvas: x, y, width and height as fractions of the canvas from
|
|
120
|
+
* its top-left corner. The point counts in CSS pixels from the canvas's top-left corner, and the
|
|
121
|
+
* lens takes the rectangle's shape, as the view's frame draws it. A camera that is gone throws
|
|
122
|
+
* the core's error, naming `call`.
|
|
123
|
+
*/
|
|
124
|
+
viewRay(camera: FrameLens, rect: readonly number[], x: number, y: number, out: Ray, call: string): void;
|
|
125
|
+
/**
|
|
126
|
+
* Writes where `point` lies on the canvas for `camera` as it stands, drawn into `rect` of the
|
|
127
|
+
* canvas, into `out`: x and y in CSS pixels from the canvas's top-left corner, then the distance
|
|
128
|
+
* in front of the camera along its view. The lens takes the rectangle's shape.
|
|
129
|
+
*/
|
|
130
|
+
viewToScreen(camera: FrameLens, rect: readonly number[], point: Vec3Like, out: Vec3Like, call: string): void;
|
|
131
|
+
/**
|
|
132
|
+
* True when the point (`x`, `y`) in CSS pixels from the canvas's top-left corner lies in `rect`
|
|
133
|
+
* of the canvas as it is now, its left and top edges included.
|
|
134
|
+
*/
|
|
135
|
+
holds(rect: readonly number[], x: number, y: number): boolean;
|
|
117
136
|
/**
|
|
118
137
|
* Writes the projection of `camera` as it stands, on a canvas of `width` by `height` device
|
|
119
138
|
* pixels, into `view` (`VIEW_FLOATS` numbers), for `projectPoint`. Returns false, and writes
|
|
@@ -191,6 +191,52 @@ export class FrameCameras {
|
|
|
191
191
|
out[0] = ((out[0] + 1) / 2) * e[CSS_WIDTH];
|
|
192
192
|
out[1] = ((1 - out[1]) / 2) * e[CSS_HEIGHT];
|
|
193
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Writes the ray from `camera` as it stands through the point (`x`, `y`) into `out`, for a view
|
|
196
|
+
* that draws into `rect` of the canvas: x, y, width and height as fractions of the canvas from
|
|
197
|
+
* its top-left corner. The point counts in CSS pixels from the canvas's top-left corner, and the
|
|
198
|
+
* lens takes the rectangle's shape, as the view's frame draws it. A camera that is gone throws
|
|
199
|
+
* the core's error, naming `call`.
|
|
200
|
+
*/
|
|
201
|
+
viewRay(camera, rect, x, y, out, call) {
|
|
202
|
+
const e = this.stand(camera, call);
|
|
203
|
+
const { point } = this;
|
|
204
|
+
point[0] = x - rect[0] * e[CSS_WIDTH];
|
|
205
|
+
point[1] = y - rect[1] * e[CSS_HEIGHT];
|
|
206
|
+
inRect(e, rect);
|
|
207
|
+
writeRay(e, 0, point, out);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Writes where `point` lies on the canvas for `camera` as it stands, drawn into `rect` of the
|
|
211
|
+
* canvas, into `out`: x and y in CSS pixels from the canvas's top-left corner, then the distance
|
|
212
|
+
* in front of the camera along its view. The lens takes the rectangle's shape.
|
|
213
|
+
*/
|
|
214
|
+
viewToScreen(camera, rect, point, out, call) {
|
|
215
|
+
const e = this.stand(camera, call);
|
|
216
|
+
const left = rect[0] * e[CSS_WIDTH];
|
|
217
|
+
const top = rect[1] * e[CSS_HEIGHT];
|
|
218
|
+
inRect(e, rect);
|
|
219
|
+
const view = this.view;
|
|
220
|
+
writeView(e, 0, view);
|
|
221
|
+
projectPoint(view, point, out);
|
|
222
|
+
out[0] = left + ((out[0] + 1) / 2) * e[CSS_WIDTH];
|
|
223
|
+
out[1] = top + ((1 - out[1]) / 2) * e[CSS_HEIGHT];
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* True when the point (`x`, `y`) in CSS pixels from the canvas's top-left corner lies in `rect`
|
|
227
|
+
* of the canvas as it is now, its left and top edges included.
|
|
228
|
+
*/
|
|
229
|
+
holds(rect, x, y) {
|
|
230
|
+
const { slotFloats } = this.control;
|
|
231
|
+
const width = slotFloats[Slot.CanvasCssWidth];
|
|
232
|
+
const height = slotFloats[Slot.CanvasCssHeight];
|
|
233
|
+
const left = rect[0] * width;
|
|
234
|
+
const top = rect[1] * height;
|
|
235
|
+
return (x >= left &&
|
|
236
|
+
y >= top &&
|
|
237
|
+
x < left + rect[2] * width &&
|
|
238
|
+
y < top + rect[3] * height);
|
|
239
|
+
}
|
|
194
240
|
/**
|
|
195
241
|
* Writes the projection of `camera` as it stands, on a canvas of `width` by `height` device
|
|
196
242
|
* pixels, into `view` (`VIEW_FLOATS` numbers), for `projectPoint`. Returns false, and writes
|
|
@@ -232,6 +278,18 @@ export class FrameCameras {
|
|
|
232
278
|
entry.set(this.matrix, at + MATRIX);
|
|
233
279
|
}
|
|
234
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Shrinks the canvas of the entry at 0 to the rectangle `rect` of it, in device and in CSS pixels,
|
|
283
|
+
* so the lens takes the rectangle's shape, as a view's frame builds its projection from it.
|
|
284
|
+
*/
|
|
285
|
+
function inRect(e, rect) {
|
|
286
|
+
const width = rect[2];
|
|
287
|
+
const height = rect[3];
|
|
288
|
+
e[WIDTH] = e[WIDTH] * width;
|
|
289
|
+
e[HEIGHT] = e[HEIGHT] * height;
|
|
290
|
+
e[CSS_WIDTH] = e[CSS_WIDTH] * width;
|
|
291
|
+
e[CSS_HEIGHT] = e[CSS_HEIGHT] * height;
|
|
292
|
+
}
|
|
235
293
|
/** True when the ring's entry at `at` holds the same view as `view`, whatever their frames. */
|
|
236
294
|
function sameView(ring, at, view) {
|
|
237
295
|
for (let k = LAYERS; k < ENTRY; k++)
|