@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.
Files changed (311) hide show
  1. package/README.md +109 -0
  2. package/THIRD-PARTY-NOTICES.txt +0 -1
  3. package/dist/wasm/single/null3d.js +19 -6
  4. package/dist/wasm/single/null3d_bg.wasm +0 -0
  5. package/dist/wasm/threaded/null3d.js +19 -6
  6. package/dist/wasm/threaded/null3d_bg.wasm +0 -0
  7. package/docs/api/animation.md +23 -67
  8. package/docs/api/assets.md +42 -26
  9. package/docs/api/cameras.md +8 -6
  10. package/docs/api/controls.md +46 -44
  11. package/docs/api/debug.md +13 -8
  12. package/docs/api/engine.md +17 -11
  13. package/docs/api/feature-draws.md +3 -3
  14. package/docs/api/feature-effects.md +3 -3
  15. package/docs/api/feature-instances.md +2 -2
  16. package/docs/api/features.md +14 -15
  17. package/docs/api/geometry.md +56 -75
  18. package/docs/api/grass.md +1 -1
  19. package/docs/api/impostors.md +120 -9
  20. package/docs/api/input.md +4 -11
  21. package/docs/api/lights.md +14 -38
  22. package/docs/api/lines.md +8 -25
  23. package/docs/api/materials.md +69 -70
  24. package/docs/api/math.md +6 -7
  25. package/docs/api/objects.md +13 -13
  26. package/docs/api/particles.md +2 -14
  27. package/docs/api/points.md +3 -25
  28. package/docs/api/post.md +22 -23
  29. package/docs/api/quality.md +11 -6
  30. package/docs/api/raycast.md +13 -32
  31. package/docs/api/reference/animation.md +9 -9
  32. package/docs/api/reference/assets.md +18 -18
  33. package/docs/api/reference/cameras.md +5 -2
  34. package/docs/api/reference/controls.md +75 -23
  35. package/docs/api/reference/debug.md +23 -8
  36. package/docs/api/reference/engine.md +22 -10
  37. package/docs/api/reference/geometry.md +161 -82
  38. package/docs/api/reference/grass.md +1 -1
  39. package/docs/api/reference/impostors.md +92 -1
  40. package/docs/api/reference/input.md +3 -3
  41. package/docs/api/reference/lights.md +9 -9
  42. package/docs/api/reference/lines.md +7 -7
  43. package/docs/api/reference/materials.md +48 -48
  44. package/docs/api/reference/math.md +22 -22
  45. package/docs/api/reference/objects.md +9 -9
  46. package/docs/api/reference/points.md +4 -4
  47. package/docs/api/reference/post.md +18 -18
  48. package/docs/api/reference/quality.md +7 -3
  49. package/docs/api/reference/raycast.md +6 -6
  50. package/docs/api/reference/render.md +2 -2
  51. package/docs/api/reference/scene.md +29 -26
  52. package/docs/api/reference/sketch.md +4 -0
  53. package/docs/api/reference/sound.md +203 -0
  54. package/docs/api/reference/sprites.md +5 -5
  55. package/docs/api/reference/terrain.md +2 -1
  56. package/docs/api/reference/textures.md +4 -4
  57. package/docs/api/reference/ui.md +3 -3
  58. package/docs/api/reference/vertex-draws.md +3 -3
  59. package/docs/api/reference/water.md +77 -6
  60. package/docs/api/reference/wind.md +2 -2
  61. package/docs/api/render.md +12 -10
  62. package/docs/api/scene.md +40 -29
  63. package/docs/api/sketch.md +64 -3
  64. package/docs/api/sound.md +169 -0
  65. package/docs/api/splats.md +7 -0
  66. package/docs/api/sprites.md +4 -20
  67. package/docs/api/tasks.md +2 -2
  68. package/docs/api/terrain.md +2 -14
  69. package/docs/api/textures.md +9 -9
  70. package/docs/api/ui.md +3 -15
  71. package/docs/api/water.md +198 -15
  72. package/docs/api/wind.md +5 -5
  73. package/docs/cli/null3d.md +93 -18
  74. package/docs/concepts/architecture.md +10 -10
  75. package/docs/concepts/assets.md +2 -19
  76. package/docs/concepts/backends.md +7 -5
  77. package/docs/concepts/color-management.md +15 -26
  78. package/docs/concepts/culling.md +1 -1
  79. package/docs/concepts/instances.md +5 -5
  80. package/docs/concepts/large-worlds.md +1 -1
  81. package/docs/concepts/lighting.md +50 -55
  82. package/docs/concepts/lod.md +2 -15
  83. package/docs/concepts/materials.md +16 -16
  84. package/docs/concepts/post-processing.md +7 -29
  85. package/docs/concepts/quality-preset-tables.md +53 -50
  86. package/docs/concepts/quality-presets.md +56 -27
  87. package/docs/concepts/render-graph.md +19 -14
  88. package/docs/concepts/render-layers.md +3 -17
  89. package/docs/concepts/shadows.md +10 -28
  90. package/docs/concepts/static-dynamic.md +2 -2
  91. package/docs/cookbook/index.md +1 -1
  92. package/docs/data/threejs-mapping.json +136 -102
  93. package/docs/errors/E1214.md +3 -3
  94. package/docs/errors/E1307.md +23 -0
  95. package/docs/errors/E1420.md +3 -3
  96. package/docs/errors/index.md +3 -2
  97. package/docs/getting-started/cdn.md +17 -1
  98. package/docs/getting-started/first-scene.md +1 -1
  99. package/docs/getting-started/hosting.md +50 -9
  100. package/docs/getting-started/install.md +8 -7
  101. package/docs/getting-started/network-use.md +10 -2
  102. package/docs/getting-started/project-structure.md +15 -6
  103. package/docs/getting-started/third-party-notices.md +8 -6
  104. package/docs/guides/accessibility.md +1 -1
  105. package/docs/guides/add-ons.md +3 -2
  106. package/docs/guides/agents.md +16 -5
  107. package/docs/guides/assets-pipeline.md +13 -12
  108. package/docs/guides/audio.md +109 -124
  109. package/docs/guides/car-paint.md +1 -1
  110. package/docs/guides/content-pages.md +10 -6
  111. package/docs/guides/custom-passes.md +9 -8
  112. package/docs/guides/custom-shaders.md +6 -3
  113. package/docs/guides/debugging.md +78 -4
  114. package/docs/guides/deploying.md +105 -5
  115. package/docs/guides/deterministic-simulation.md +1 -1
  116. package/docs/guides/first-use-shaders.md +4 -4
  117. package/docs/guides/loading-screens.md +78 -33
  118. package/docs/guides/multiple-views.md +41 -2
  119. package/docs/guides/performance.md +6 -3
  120. package/docs/guides/phones.md +12 -1
  121. package/docs/guides/physics.md +1 -1
  122. package/docs/guides/screens.md +7 -5
  123. package/docs/guides/start-time.md +328 -0
  124. package/docs/guides/testing.md +23 -5
  125. package/docs/guides/ui-overlays.md +3 -3
  126. package/docs/index.md +12 -8
  127. package/docs/pages.md +26 -23
  128. package/docs/porting/react-three-fiber.md +228 -5
  129. package/docs/porting/threejs-loop-and-threads.md +220 -6
  130. package/docs/porting/threejs-mapping.md +86 -84
  131. package/docs/porting/threejs-materials.md +212 -5
  132. package/docs/porting/threejs-overview.md +218 -5
  133. package/docs/porting/threejs-postprocessing.md +2 -2
  134. package/docs/porting/threejs-shaders.md +298 -5
  135. package/docs/porting/threejs-unsupported.md +82 -6
  136. package/docs/porting/verification.md +181 -5
  137. package/docs/shaders/builtins.md +1 -1
  138. package/docs/shaders/library.md +63 -39
  139. package/docs/shaders/surface-functions.md +11 -10
  140. package/docs/shaders/wgsl-rules.md +1 -1
  141. package/lib/debug/channel.d.ts +103 -0
  142. package/lib/debug/channel.js +269 -0
  143. package/lib/debug/debug.d.ts +9 -7
  144. package/lib/debug/draw.d.ts +9 -0
  145. package/lib/debug/draw.js +49 -2
  146. package/lib/debug/inspector.d.ts +60 -0
  147. package/lib/debug/inspector.js +475 -0
  148. package/lib/debug/stats-panel.d.ts +1 -1
  149. package/lib/debug/stats.d.ts +2 -3
  150. package/lib/errors/codes.js +8 -2
  151. package/lib/errors/engine-error.js +25 -10
  152. package/lib/errors/fixes.d.ts +1 -0
  153. package/lib/errors/fixes.js +3 -2
  154. package/lib/generated/core.d.ts +1 -1
  155. package/lib/generated/core.js +1 -1
  156. package/lib/gpu/early-builds.d.ts +68 -0
  157. package/lib/gpu/early-builds.js +158 -0
  158. package/lib/gpu/pipeline-name.d.ts +6 -0
  159. package/lib/gpu/pipeline-name.js +23 -0
  160. package/lib/gpu/webgl2/backend.d.ts +63 -2
  161. package/lib/gpu/webgl2/backend.js +226 -8
  162. package/lib/gpu/webgl2/programs.d.ts +18 -0
  163. package/lib/gpu/webgl2/programs.js +4 -1
  164. package/lib/gpu/webgpu/backend.d.ts +89 -2
  165. package/lib/gpu/webgpu/backend.js +255 -8
  166. package/lib/gpu/webgpu/pipelines.d.ts +13 -0
  167. package/lib/gpu/webgpu/pipelines.js +15 -1
  168. package/lib/index.d.ts +6 -4
  169. package/lib/index.js +3 -2
  170. package/lib/math/color.d.ts +5 -5
  171. package/lib/math/color.js +5 -5
  172. package/lib/math/mat4.d.ts +6 -12
  173. package/lib/math/mat4.js +6 -12
  174. package/lib/math/namespaces/quat.d.ts +3 -3
  175. package/lib/math/namespaces/quat.js +3 -3
  176. package/lib/math/quat.d.ts +8 -12
  177. package/lib/math/quat.js +8 -12
  178. package/lib/math/random.d.ts +2 -3
  179. package/lib/math/random.js +2 -3
  180. package/lib/math/types.d.ts +4 -4
  181. package/lib/math/vec3.d.ts +3 -6
  182. package/lib/math/vec3.js +3 -6
  183. package/lib/page/capabilities.d.ts +7 -7
  184. package/lib/page/capabilities.js +39 -25
  185. package/lib/page/check-store.d.ts +14 -2
  186. package/lib/page/check-store.js +34 -11
  187. package/lib/page/engine.d.ts +103 -26
  188. package/lib/page/engine.js +420 -97
  189. package/lib/page/first-frame.d.ts +16 -0
  190. package/lib/page/first-frame.js +35 -0
  191. package/lib/page/frame-stats.d.ts +34 -1
  192. package/lib/page/frame-stats.js +15 -0
  193. package/lib/page/label-loop.d.ts +7 -3
  194. package/lib/page/label-loop.js +17 -10
  195. package/lib/page/labels.d.ts +3 -3
  196. package/lib/page/pipeline-store.d.ts +25 -0
  197. package/lib/page/pipeline-store.js +84 -0
  198. package/lib/page/start-marker.d.ts +4 -1
  199. package/lib/page/start-marker.js +5 -2
  200. package/lib/page/switches.d.ts +32 -2
  201. package/lib/page/switches.js +9 -1
  202. package/lib/quality/check.d.ts +24 -3
  203. package/lib/quality/check.js +21 -7
  204. package/lib/quality/chooser.d.ts +6 -0
  205. package/lib/quality/chooser.js +8 -0
  206. package/lib/quality/governor.d.ts +98 -6
  207. package/lib/quality/governor.js +250 -20
  208. package/lib/quality/preset-docs.d.ts +16 -1
  209. package/lib/quality/preset-docs.js +3 -0
  210. package/lib/quality/presets.d.ts +84 -7
  211. package/lib/quality/presets.js +94 -2
  212. package/lib/render/draw.d.ts +3 -2
  213. package/lib/render/draw.js +15 -5
  214. package/lib/render/load-draw.d.ts +7 -5
  215. package/lib/render/load-draw.js +7 -5
  216. package/lib/render/loss.d.ts +28 -10
  217. package/lib/render/loss.js +50 -22
  218. package/lib/render/recovery.d.ts +20 -0
  219. package/lib/render/recovery.js +44 -4
  220. package/lib/render/renderer.d.ts +85 -9
  221. package/lib/render/renderer.js +181 -42
  222. package/lib/render/scene-renderer.d.ts +19 -1
  223. package/lib/render/scene-renderer.js +25 -1
  224. package/lib/render/webgl2-renderers.js +8 -5
  225. package/lib/render/webgpu-renderers.js +5 -4
  226. package/lib/scene/animation.d.ts +28 -32
  227. package/lib/scene/animation.js +12 -15
  228. package/lib/scene/assets.d.ts +61 -86
  229. package/lib/scene/assets.js +103 -236
  230. package/lib/scene/background.d.ts +14 -18
  231. package/lib/scene/background.js +5 -2
  232. package/lib/scene/color.d.ts +3 -3
  233. package/lib/scene/downloads.d.ts +2 -0
  234. package/lib/scene/downloads.js +371 -0
  235. package/lib/scene/environment.d.ts +7 -7
  236. package/lib/scene/environment.js +1 -1
  237. package/lib/scene/fake-call-values.js +1 -1
  238. package/lib/scene/fog.d.ts +11 -4
  239. package/lib/scene/fog.js +8 -2
  240. package/lib/scene/frame-cameras.d.ts +19 -0
  241. package/lib/scene/frame-cameras.js +58 -0
  242. package/lib/scene/gltf.d.ts +32 -2
  243. package/lib/scene/gltf.js +78 -49
  244. package/lib/scene/grading.d.ts +2 -2
  245. package/lib/scene/grading.js +3 -3
  246. package/lib/scene/ktx2.d.ts +9 -1
  247. package/lib/scene/ktx2.js +3 -2
  248. package/lib/scene/level-rule.d.ts +120 -0
  249. package/lib/scene/level-rule.js +130 -0
  250. package/lib/scene/lines.d.ts +13 -17
  251. package/lib/scene/lines.js +3 -3
  252. package/lib/scene/lut.d.ts +1 -2
  253. package/lib/scene/lut.js +1 -2
  254. package/lib/scene/picks.d.ts +2 -2
  255. package/lib/scene/pointer-events.d.ts +4 -7
  256. package/lib/scene/post.d.ts +38 -43
  257. package/lib/scene/post.js +4 -1
  258. package/lib/scene/prefab.d.ts +7 -8
  259. package/lib/scene/prefab.js +7 -8
  260. package/lib/scene/property-animation.d.ts +1 -1
  261. package/lib/scene/property-animation.js +1 -1
  262. package/lib/scene/queries.d.ts +7 -13
  263. package/lib/scene/resources.d.ts +225 -254
  264. package/lib/scene/resources.js +179 -81
  265. package/lib/scene/scene.d.ts +149 -118
  266. package/lib/scene/scene.js +196 -98
  267. package/lib/scene/serial-loads.d.ts +29 -0
  268. package/lib/scene/serial-loads.js +68 -0
  269. package/lib/scene/shader-preloads.d.ts +34 -0
  270. package/lib/scene/shader-preloads.js +38 -0
  271. package/lib/scene/sprites.d.ts +14 -17
  272. package/lib/scene/sprites.js +5 -6
  273. package/lib/scene/textures.d.ts +11 -12
  274. package/lib/scene/textures.js +9 -10
  275. package/lib/scene/time-of-day.d.ts +3 -4
  276. package/lib/scene/time-of-day.js +2 -3
  277. package/lib/scene/vertex-draws.d.ts +5 -4
  278. package/lib/scene/vertex-draws.js +7 -5
  279. package/lib/shader-artifact.json +1 -0
  280. package/lib/shared/core.d.ts +5 -0
  281. package/lib/shared/core.js +1 -0
  282. package/lib/shared/import-once.d.ts +6 -0
  283. package/lib/shared/import-once.js +19 -0
  284. package/lib/shared/metrics.d.ts +38 -0
  285. package/lib/shared/metrics.js +62 -1
  286. package/lib/shared/run-task.js +7 -1
  287. package/lib/shared/task-pending.d.ts +12 -0
  288. package/lib/shared/task-pending.js +61 -1
  289. package/lib/shared/tasks.js +79 -32
  290. package/lib/shared/version.d.ts +17 -1
  291. package/lib/shared/version.js +19 -1
  292. package/lib/shared/wasm.d.ts +12 -4
  293. package/lib/shared/wasm.js +50 -27
  294. package/lib/shared/worker-start.d.ts +7 -0
  295. package/lib/shared/worker-start.js +27 -0
  296. package/lib/sketch/define-sketch.d.ts +35 -0
  297. package/lib/sketch/input.d.ts +2 -2
  298. package/lib/sketch/preset-check.d.ts +20 -6
  299. package/lib/sketch/preset-check.js +42 -17
  300. package/lib/sketch/quality.d.ts +69 -11
  301. package/lib/sketch/quality.js +54 -13
  302. package/lib/sketch/runner.d.ts +76 -9
  303. package/lib/sketch/runner.js +385 -52
  304. package/lib/sketch/ui.d.ts +5 -8
  305. package/lib/workers/probe-worker.d.ts +5 -0
  306. package/lib/workers/probe-worker.js +13 -4
  307. package/lib/workers/protocol.d.ts +61 -13
  308. package/lib/workers/protocol.js +4 -0
  309. package/lib/workers/render-worker.js +25 -4
  310. package/lib/workers/sketch-worker.js +104 -15
  311. package/package.json +2 -2
@@ -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)`, as three.js's `CubeTextureLoader` makes a `CubeTexture` for
15
- * `scene.background`. It does not light the scene: light comes from an environment.
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
  }
@@ -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 in
4
- * three.js, and the engine converts them to linear values. The color helpers, such as
5
- * `color.fromHsl`, give linear components.
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,2 @@
1
+ /** Fetches a file. */
2
+ export type FetchFile = (address: URL) => Promise<Response>;
@@ -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 the
10
- * room that three.js's `RoomEnvironment` builds, blurred as three.js's examples blur it. It is a
11
- * white room with six boxes and glowing panels, which gives soft, neutral light.
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, as three.js's `scene.environment` does with a texture from `PMREMGenerator`.
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, as three.js's
49
- * `scene.environmentIntensity`. A material's `envIntensity` multiplies it. The default is 1.
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, as three.js's `scene.environmentRotation`. The default is `[0, 0, 0]`.
54
+ * Z. The default is `[0, 0, 0]`.
55
55
  */
56
56
  rotation?: readonly [number, number, number];
57
57
  }
@@ -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, as three.js's `scene.environment` does with a texture from `PMREMGenerator`.
13
+ * with it.
14
14
  *
15
15
  * @category api/assets
16
16
  */
@@ -22,7 +22,7 @@ const READS = {
22
22
  setPixelRatio: 1,
23
23
  setLevelQuality: 2,
24
24
  setShadowQuality: 1,
25
- setFog: 10,
25
+ setFog: 11,
26
26
  setFogVolume: 4,
27
27
  setReflectionScale: 1,
28
28
  };
@@ -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, as three.js's
7
- * `FogExp2`: 1 - exp(-(density × d)²). Linear fog, `'linear'`, is clear up to `near` and hides
8
- * objects from `far`, with a smooth step between them, as three.js's `Fog`.
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. Give the background the same color, because the background takes no fog. */
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 background takes no fog. The
4
- // volumetric fog lights the same fog with the sun and the point and spot lights, through their
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++)