wallhammer-web 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
// World streaming, the execution part: runs the decisions of `StreamingManager` (module world) once per frame.
|
|
2
|
+
// It starts jobs on a provider (workers, files, or the main thread within a time budget), uploads finished cells to
|
|
3
|
+
// the GPU within a time budget, releases what the manager drops, and draws the manager's cover as a renderer layer.
|
|
4
|
+
// Clocks here decide only *when* work is done, never *what*: the manager's decisions do not see them.
|
|
5
|
+
//
|
|
6
|
+
// Positions: the authoritative position is a double. The renderer works in a render space whose origin is the
|
|
7
|
+
// camera focus snapped to a coarse grid (`originGridM`), so every number the GPU sees is small. Each cell is drawn
|
|
8
|
+
// with `u_cellOffset = cell origin − render origin`, computed in double and only then handed to the GPU as floats;
|
|
9
|
+
// a large number never reaches the GPU. Because the render origin depends only on where the camera is, the same
|
|
10
|
+
// position gives the same image however the camera got there.
|
|
11
|
+
import { boxInVolume, distanceToBox, snapRenderOrigin } from '../base/view.js';
|
|
12
|
+
import { cellContentBytes, cellKey, shapeHash } from '../gen-core/cell.js';
|
|
13
|
+
import { SHADOW_MAP_UNIT } from '../render/index.js';
|
|
14
|
+
import { StreamingManager, boxMaxRadius, decodeCell, encodeCell, horizonHidden } from '../world/index.js';
|
|
15
|
+
import { WorkerPool, serveWorker } from '../platform/index.js';
|
|
16
|
+
export class StreamedWorld {
|
|
17
|
+
/** Name in profiles (`Layer.name`). */
|
|
18
|
+
name = 'streamed-world';
|
|
19
|
+
manager;
|
|
20
|
+
renderer;
|
|
21
|
+
provider;
|
|
22
|
+
/** Render-space origin in world metres (double); x and z snap to the grid, y is 0 unless `originAxes` is `'all'`. */
|
|
23
|
+
origin = [0, 0, 0];
|
|
24
|
+
/** Textures and uniforms per material name (see the options of the same names). */
|
|
25
|
+
textures;
|
|
26
|
+
materialUniforms;
|
|
27
|
+
gpuCeiling;
|
|
28
|
+
onResident;
|
|
29
|
+
onReleased;
|
|
30
|
+
/** Uniforms set for every material before its cells are drawn. */
|
|
31
|
+
uniforms = {};
|
|
32
|
+
materials;
|
|
33
|
+
archetypes;
|
|
34
|
+
grid;
|
|
35
|
+
originAxes;
|
|
36
|
+
uploadMs;
|
|
37
|
+
blockingMs;
|
|
38
|
+
onActivity;
|
|
39
|
+
now;
|
|
40
|
+
tickets = [];
|
|
41
|
+
cpu = new Map();
|
|
42
|
+
gpu = new Map();
|
|
43
|
+
running = new Map();
|
|
44
|
+
blocking = [];
|
|
45
|
+
arrived = [];
|
|
46
|
+
failedJobs = [];
|
|
47
|
+
pendingUploads = [];
|
|
48
|
+
drawList = [];
|
|
49
|
+
drawKey = '';
|
|
50
|
+
viewList = [];
|
|
51
|
+
shadowList = [];
|
|
52
|
+
eye = [0, 0, 0];
|
|
53
|
+
eyeWorld = [0, 0, 0];
|
|
54
|
+
horizon;
|
|
55
|
+
horizonCulled = 0;
|
|
56
|
+
frame = { uploadMs: 0, uploads: 0, blockingMs: 0, blockingJobs: 0, drawCalls: 0, triangles: 0, instances: 0 };
|
|
57
|
+
totals = { genMs: 0, genJobs: 0, upMs: 0, upCells: 0 };
|
|
58
|
+
undo;
|
|
59
|
+
morph;
|
|
60
|
+
/** Hash of the manager's world shape; a cell that states another one is refused. */
|
|
61
|
+
shape;
|
|
62
|
+
shapeMismatches = 0;
|
|
63
|
+
constructor(o) {
|
|
64
|
+
this.renderer = o.renderer;
|
|
65
|
+
this.manager = new StreamingManager(o.gpuCeilingBytes === undefined ? o.config : { ...o.config, gpuBudgetBytes: o.gpuCeilingBytes });
|
|
66
|
+
this.gpuCeiling = o.gpuCeilingBytes;
|
|
67
|
+
this.textures = o.textures ?? {};
|
|
68
|
+
this.materialUniforms = o.materialUniforms ?? {};
|
|
69
|
+
this.onResident = o.onResident;
|
|
70
|
+
this.onReleased = o.onReleased;
|
|
71
|
+
this.provider = o.provider;
|
|
72
|
+
this.materials = o.materials;
|
|
73
|
+
this.archetypes = o.archetypes ?? {};
|
|
74
|
+
this.grid = o.originGridM ?? 1024;
|
|
75
|
+
this.originAxes = o.originAxes ?? 'horizontal';
|
|
76
|
+
this.uploadMs = o.uploadMsPerFrame ?? 3;
|
|
77
|
+
this.blockingMs = o.blockingMsPerFrame ?? 6;
|
|
78
|
+
this.onActivity = o.onActivity ?? (() => { });
|
|
79
|
+
this.now = o.now ?? (() => performance.now());
|
|
80
|
+
this.morph = o.morph ?? false;
|
|
81
|
+
this.horizon = o.horizon ?? null;
|
|
82
|
+
this.shape = shapeHash(o.config.shape);
|
|
83
|
+
const hook = () => this.step();
|
|
84
|
+
this.renderer.beforeFrame.push(hook);
|
|
85
|
+
// A device that keeps its uploads restores the meshes itself; otherwise the cells are uploaded again.
|
|
86
|
+
const device = this.renderer.device;
|
|
87
|
+
const unwatch = device.onRestored(() => {
|
|
88
|
+
if (!device.retainUploads)
|
|
89
|
+
this.reupload();
|
|
90
|
+
});
|
|
91
|
+
this.undo = () => {
|
|
92
|
+
unwatch();
|
|
93
|
+
const i = this.renderer.beforeFrame.indexOf(hook);
|
|
94
|
+
if (i >= 0)
|
|
95
|
+
this.renderer.beforeFrame.splice(i, 1);
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Forgets every GPU object of the cells without deleting it and has the manager list the cells for upload again
|
|
100
|
+
* (from the CPU data this layer keeps). For a fresh graphics context after a loss, on a device that did not keep
|
|
101
|
+
* the uploads; the cover fills in again within the upload budget of the next frames.
|
|
102
|
+
*/
|
|
103
|
+
reupload() {
|
|
104
|
+
for (const key of [...this.gpu.keys()]) {
|
|
105
|
+
const g = this.gpu.get(key);
|
|
106
|
+
this.gpu.delete(key);
|
|
107
|
+
this.onReleased?.(g.cell);
|
|
108
|
+
}
|
|
109
|
+
this.manager.dropResident();
|
|
110
|
+
this.drawList = [];
|
|
111
|
+
this.viewList = [];
|
|
112
|
+
this.shadowList = [];
|
|
113
|
+
this.drawKey = '';
|
|
114
|
+
this.onActivity();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Moves the render origin to the grid point at or below `p` (x and z; y too with `originAxes: 'all'`), and sets the
|
|
118
|
+
* renderer's world origin.
|
|
119
|
+
*/
|
|
120
|
+
setFocus(p) {
|
|
121
|
+
this.origin = snapRenderOrigin(p, this.grid, this.originAxes);
|
|
122
|
+
this.renderer.worldOrigin = [this.origin[0], this.origin[1], this.origin[2]];
|
|
123
|
+
}
|
|
124
|
+
/** World position (double) → render space (small numbers). */
|
|
125
|
+
toRender(p) {
|
|
126
|
+
return [p[0] - this.origin[0], p[1] - this.origin[1], p[2] - this.origin[2]];
|
|
127
|
+
}
|
|
128
|
+
/** Render space → world position (double). */
|
|
129
|
+
toWorld(p) {
|
|
130
|
+
return [p[0] + this.origin[0], p[1] + this.origin[1], p[2] + this.origin[2]];
|
|
131
|
+
}
|
|
132
|
+
/** The reasons to load for the coming frames. */
|
|
133
|
+
setTickets(tickets) {
|
|
134
|
+
this.tickets = [...tickets];
|
|
135
|
+
}
|
|
136
|
+
/** Eye position in world metres, for level-of-detail choice of instances. */
|
|
137
|
+
setEye(p) {
|
|
138
|
+
this.eye = this.toRender(p);
|
|
139
|
+
this.eyeWorld = [p[0], p[1], p[2]];
|
|
140
|
+
}
|
|
141
|
+
/** True when every wanted cell is on the GPU and drawn, and no upload waits. */
|
|
142
|
+
get settled() {
|
|
143
|
+
return this.manager.settled && this.pendingUploads.length === 0 && this.drawList.length === this.manager.drawn().length;
|
|
144
|
+
}
|
|
145
|
+
/** True while jobs run or wait, or uploads wait. */
|
|
146
|
+
get busy() {
|
|
147
|
+
const c = this.manager.counters();
|
|
148
|
+
return c.inFlight > 0 || c.queued > 0 || this.pendingUploads.length > 0 || this.blocking.length > 0 || this.arrived.length > 0 || this.manager.stale || this.manager.refreshes().pending > 0;
|
|
149
|
+
}
|
|
150
|
+
stats() {
|
|
151
|
+
return {
|
|
152
|
+
...this.manager.counters(),
|
|
153
|
+
cellsVisible: this.viewList.length,
|
|
154
|
+
...this.frame,
|
|
155
|
+
generateMsAvg: this.totals.genJobs ? this.totals.genMs / this.totals.genJobs : 0,
|
|
156
|
+
uploadMsAvg: this.totals.upCells ? this.totals.upMs / this.totals.upCells : 0,
|
|
157
|
+
renderOrigin: this.origin,
|
|
158
|
+
shapeMismatches: this.shapeMismatches,
|
|
159
|
+
horizonCulled: this.horizonCulled,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** Adds generation time measured elsewhere (a worker pool) to the average. */
|
|
163
|
+
recordGeneration(ms, jobs) {
|
|
164
|
+
this.totals.genMs += ms;
|
|
165
|
+
this.totals.genJobs += jobs;
|
|
166
|
+
}
|
|
167
|
+
/** One streaming step. Runs at the start of every rendered frame (renderer.beforeFrame). */
|
|
168
|
+
step() {
|
|
169
|
+
const m = this.manager;
|
|
170
|
+
// 1. Results that arrived since the last frame.
|
|
171
|
+
for (const { req, result } of this.arrived.splice(0))
|
|
172
|
+
this.accept(req, result);
|
|
173
|
+
for (const req of this.failedJobs.splice(0))
|
|
174
|
+
m.fail(req);
|
|
175
|
+
// 2. Decisions. With a GPU ceiling the manager is told what everything but the cells holds right now.
|
|
176
|
+
if (this.gpuCeiling !== undefined)
|
|
177
|
+
m.gpuReserveBytes = Math.max(0, this.renderer.device.memory().total - m.counters().gpuBytes);
|
|
178
|
+
const a = m.update(this.tickets);
|
|
179
|
+
for (const c of a.cancels) {
|
|
180
|
+
this.running.get(c.key)?.abort();
|
|
181
|
+
this.running.delete(c.key);
|
|
182
|
+
const i = this.blocking.findIndex((j) => j.req.key === c.key);
|
|
183
|
+
if (i >= 0)
|
|
184
|
+
this.blocking.splice(i, 1);
|
|
185
|
+
}
|
|
186
|
+
for (const c of a.releases)
|
|
187
|
+
this.release(cellKey(c));
|
|
188
|
+
for (const c of a.evictions) {
|
|
189
|
+
const k = cellKey(c);
|
|
190
|
+
this.release(k);
|
|
191
|
+
this.cpu.delete(k);
|
|
192
|
+
}
|
|
193
|
+
for (const req of a.requests)
|
|
194
|
+
this.start(req);
|
|
195
|
+
// 3. Main-thread jobs within the budget (at least one).
|
|
196
|
+
const t0 = this.now();
|
|
197
|
+
let jobs = 0;
|
|
198
|
+
while (this.blocking.length > 0 && (jobs === 0 || this.now() - t0 < this.blockingMs)) {
|
|
199
|
+
const job = this.blocking.shift();
|
|
200
|
+
const tj = this.now();
|
|
201
|
+
this.provider.request(job.req.cell, job.abort.signal).then((result) => this.arrived.push({ req: job.req, result }), () => this.failedJobs.push(job.req));
|
|
202
|
+
this.totals.genMs += this.now() - tj;
|
|
203
|
+
this.totals.genJobs++;
|
|
204
|
+
jobs++;
|
|
205
|
+
}
|
|
206
|
+
this.frame.blockingMs = this.now() - t0;
|
|
207
|
+
this.frame.blockingJobs = jobs;
|
|
208
|
+
// A blocking provider resolves at once, but the promise callbacks run after this frame: take them next frame.
|
|
209
|
+
// 4. Uploads within the budget (at least one), most urgent first.
|
|
210
|
+
this.pendingUploads = a.uploads.filter((c) => this.cpu.has(cellKey(c)) && !this.gpu.has(cellKey(c)));
|
|
211
|
+
const u0 = this.now();
|
|
212
|
+
let ups = 0;
|
|
213
|
+
while (this.pendingUploads.length > 0 && (ups === 0 || this.now() - u0 < this.uploadMs)) {
|
|
214
|
+
const c = this.pendingUploads.shift();
|
|
215
|
+
const k = cellKey(c);
|
|
216
|
+
const tu = this.now();
|
|
217
|
+
const content = this.cpu.get(k);
|
|
218
|
+
const g = this.upload(content);
|
|
219
|
+
this.gpu.set(k, g);
|
|
220
|
+
m.resident(c, g.bytes);
|
|
221
|
+
this.onResident?.(c, g.meshes, content);
|
|
222
|
+
this.totals.upMs += this.now() - tu;
|
|
223
|
+
this.totals.upCells++;
|
|
224
|
+
ups++;
|
|
225
|
+
}
|
|
226
|
+
this.frame.uploadMs = this.now() - u0;
|
|
227
|
+
this.frame.uploads = ups;
|
|
228
|
+
// 5. What to draw; scene bounds in render space; shadows refit when the set changes.
|
|
229
|
+
const drawn = m.drawn();
|
|
230
|
+
this.drawList = drawn.map((c) => this.gpu.get(cellKey(c))).filter((g) => g !== undefined);
|
|
231
|
+
const key = `${this.origin[0]},${this.origin[1]},${this.origin[2]}|${this.drawList.map((g) => g.key).join(';')}${this.morph ? this.morphKey() : ''}`;
|
|
232
|
+
if (key !== this.drawKey) {
|
|
233
|
+
this.drawKey = key;
|
|
234
|
+
this.renderer.invalidateShadows();
|
|
235
|
+
}
|
|
236
|
+
this.renderer.bounds = this.sceneBounds();
|
|
237
|
+
if (this.busy)
|
|
238
|
+
this.onActivity();
|
|
239
|
+
}
|
|
240
|
+
/** Accepts a finished job (stale results are dropped by the manager). */
|
|
241
|
+
accept(req, result) {
|
|
242
|
+
this.running.delete(req.key);
|
|
243
|
+
const content = result.content;
|
|
244
|
+
// A cell built for another world shape (another radius, another root edge) must not be drawn in this one.
|
|
245
|
+
if (content.shapeHash !== undefined && content.shapeHash !== this.shape) {
|
|
246
|
+
this.shapeMismatches++;
|
|
247
|
+
this.manager.fail(req);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!this.manager.complete(req, cellContentBytes(content), content.error))
|
|
251
|
+
return;
|
|
252
|
+
this.cpu.set(req.key, content);
|
|
253
|
+
// New content of a cell drawn with out-of-date content (`refresh`): swapped on the GPU before this frame draws.
|
|
254
|
+
if (this.manager.state(req.cell) === 'RESIDENT')
|
|
255
|
+
this.swap(req.cell, content);
|
|
256
|
+
}
|
|
257
|
+
/** Replaces the GPU data of a RESIDENT cell; it stays drawn, the frame that follows shows the new content. */
|
|
258
|
+
swap(cell, content) {
|
|
259
|
+
const k = cellKey(cell);
|
|
260
|
+
const tu = this.now();
|
|
261
|
+
const g = this.upload(content);
|
|
262
|
+
this.release(k);
|
|
263
|
+
this.gpu.set(k, g);
|
|
264
|
+
this.manager.swapped(cell, g.bytes);
|
|
265
|
+
this.onResident?.(cell, g.meshes, content);
|
|
266
|
+
this.renderer.invalidateShadows();
|
|
267
|
+
this.totals.upMs += this.now() - tu;
|
|
268
|
+
this.totals.upCells++;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The content of these cells is out of date (`StreamingManager.refresh`): cells on the GPU stay drawn and are
|
|
272
|
+
* swapped when their new content arrives, other loaded cells are built again when wanted. Returns how many tracked
|
|
273
|
+
* cells it touched.
|
|
274
|
+
*/
|
|
275
|
+
refresh(cells) {
|
|
276
|
+
const n = this.manager.refresh(cells);
|
|
277
|
+
if (n > 0)
|
|
278
|
+
this.onActivity();
|
|
279
|
+
return n;
|
|
280
|
+
}
|
|
281
|
+
/** The viewer and the blend ranges for one draw, from the manager's last step. */
|
|
282
|
+
morphFrame() {
|
|
283
|
+
const m = this.manager;
|
|
284
|
+
const t = m.viewer();
|
|
285
|
+
const none = [0, 0];
|
|
286
|
+
if (!t)
|
|
287
|
+
return { eye: [0, 0, 0, 0], up: [0, 1, 0], range: () => none };
|
|
288
|
+
const e = this.toRender(t.center);
|
|
289
|
+
const cache = new Map();
|
|
290
|
+
return {
|
|
291
|
+
eye: [e[0], e[1], e[2], t.altitude ?? 0],
|
|
292
|
+
up: m.config.shape.upAt(t.center),
|
|
293
|
+
range: (level) => {
|
|
294
|
+
let r = cache.get(level);
|
|
295
|
+
if (!r) {
|
|
296
|
+
const w = m.morphRange(level);
|
|
297
|
+
r = w && w.endM > w.startM ? [w.startM, 1 / (w.endM - w.startM)] : none;
|
|
298
|
+
cache.set(level, r);
|
|
299
|
+
}
|
|
300
|
+
return r;
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Changes when the viewer has moved far enough to change the blended shape visibly: its position and altitude in
|
|
306
|
+
* steps of 1/16 of the finest drawn cell's blend length. Part of the key that decides when shadows are redrawn.
|
|
307
|
+
*/
|
|
308
|
+
morphKey() {
|
|
309
|
+
const t = this.manager.viewer();
|
|
310
|
+
if (!t)
|
|
311
|
+
return '';
|
|
312
|
+
let finest = -1;
|
|
313
|
+
for (const g of this.drawList)
|
|
314
|
+
if (g.cell.level > finest)
|
|
315
|
+
finest = g.cell.level;
|
|
316
|
+
const w = finest >= 0 ? this.manager.morphRange(finest) : null;
|
|
317
|
+
if (!w)
|
|
318
|
+
return '';
|
|
319
|
+
const q = Math.max(1e-3, (w.endM - w.startM) / 16);
|
|
320
|
+
return `|m${Math.round(t.center[0] / q)},${Math.round(t.center[1] / q)},${Math.round(t.center[2] / q)},${Math.round((t.altitude ?? 0) / q)},${finest}`;
|
|
321
|
+
}
|
|
322
|
+
start(req) {
|
|
323
|
+
const abort = new AbortController();
|
|
324
|
+
if (this.provider.blocking) {
|
|
325
|
+
this.blocking.push({ req, abort });
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
this.running.set(req.key, abort);
|
|
329
|
+
this.provider.request(req.cell, abort.signal).then((result) => {
|
|
330
|
+
this.arrived.push({ req, result });
|
|
331
|
+
this.onActivity();
|
|
332
|
+
}, (e) => {
|
|
333
|
+
// A cancelled job may already have a successor under the same key; leave that one's controller alone.
|
|
334
|
+
if (this.running.get(req.key) === abort)
|
|
335
|
+
this.running.delete(req.key);
|
|
336
|
+
// An aborted job was cancelled by the manager, which already raised the cell's epoch.
|
|
337
|
+
if (e?.name !== 'AbortError')
|
|
338
|
+
this.failedJobs.push(req);
|
|
339
|
+
this.onActivity();
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
upload(c) {
|
|
343
|
+
const r = this.renderer;
|
|
344
|
+
const key = cellKey(c.cell);
|
|
345
|
+
const g = { cell: c.cell, key, origin: this.manager.config.shape.origin(c.cell), bounds: c.bounds, meshes: [], batches: [], bytes: 0, maxRadius: 0 };
|
|
346
|
+
if (this.horizon) {
|
|
347
|
+
const o = g.origin;
|
|
348
|
+
const b = c.bounds;
|
|
349
|
+
g.maxRadius = boxMaxRadius([o[0] + b.min[0], o[1] + b.min[1], o[2] + b.min[2]], [o[0] + b.max[0], o[1] + b.max[1], o[2] + b.max[2]], this.horizon.center);
|
|
350
|
+
}
|
|
351
|
+
for (const cm of c.meshes) {
|
|
352
|
+
const material = this.materials[cm.material];
|
|
353
|
+
if (!material)
|
|
354
|
+
throw new Error(`streamed cell ${key}: unknown material "${cm.material}"`);
|
|
355
|
+
g.meshes.push({ material, mesh: r.createMesh(cm.mesh, material) });
|
|
356
|
+
g.bytes += cm.mesh.vertices.byteLength + (cm.mesh.indices?.byteLength ?? 0);
|
|
357
|
+
}
|
|
358
|
+
for (const b of c.instances) {
|
|
359
|
+
const arch = this.archetypes[b.archetype];
|
|
360
|
+
if (!arch)
|
|
361
|
+
throw new Error(`streamed cell ${key}: unknown archetype "${b.archetype}"`);
|
|
362
|
+
const material = this.materials[arch.material];
|
|
363
|
+
if (!material)
|
|
364
|
+
throw new Error(`archetype ${b.archetype}: unknown material "${arch.material}"`);
|
|
365
|
+
const count = (b.data.data.length * 4) / b.data.layout.stride;
|
|
366
|
+
if (count === 0)
|
|
367
|
+
continue;
|
|
368
|
+
const instances = r.createInstances(b.data);
|
|
369
|
+
const lods = arch.lods.map((l) => r.createMesh(l.mesh, material, instances));
|
|
370
|
+
g.batches.push({ archetype: b.archetype, material, instances, lods, count });
|
|
371
|
+
g.bytes += b.data.data.byteLength + arch.lods.reduce((s, l) => s + l.mesh.vertices.byteLength + (l.mesh.indices?.byteLength ?? 0), 0);
|
|
372
|
+
}
|
|
373
|
+
return g;
|
|
374
|
+
}
|
|
375
|
+
release(key) {
|
|
376
|
+
const g = this.gpu.get(key);
|
|
377
|
+
if (!g)
|
|
378
|
+
return;
|
|
379
|
+
const d = this.renderer.device;
|
|
380
|
+
for (const m of g.meshes)
|
|
381
|
+
d.deleteMesh(m.mesh);
|
|
382
|
+
for (const b of g.batches) {
|
|
383
|
+
for (const l of b.lods)
|
|
384
|
+
d.deleteMesh(l);
|
|
385
|
+
d.deleteInstances(b.instances);
|
|
386
|
+
}
|
|
387
|
+
this.gpu.delete(key);
|
|
388
|
+
this.onReleased?.(g.cell);
|
|
389
|
+
}
|
|
390
|
+
/** Render-space box of a drawn cell. */
|
|
391
|
+
box(g) {
|
|
392
|
+
const ox = g.origin[0] - this.origin[0];
|
|
393
|
+
const oy = g.origin[1] - this.origin[1];
|
|
394
|
+
const oz = g.origin[2] - this.origin[2];
|
|
395
|
+
return {
|
|
396
|
+
min: [ox + g.bounds.min[0], oy + g.bounds.min[1], oz + g.bounds.min[2]],
|
|
397
|
+
max: [ox + g.bounds.max[0], oy + g.bounds.max[1], oz + g.bounds.max[2]],
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
sceneBounds() {
|
|
401
|
+
if (this.drawList.length === 0)
|
|
402
|
+
return null;
|
|
403
|
+
const min = [Infinity, Infinity, Infinity];
|
|
404
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
405
|
+
for (const g of this.drawList) {
|
|
406
|
+
const b = this.box(g);
|
|
407
|
+
for (let i = 0; i < 3; i++) {
|
|
408
|
+
min[i] = Math.min(min[i], b.min[i]);
|
|
409
|
+
max[i] = Math.max(max[i], b.max[i]);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return { min, max };
|
|
413
|
+
}
|
|
414
|
+
cull(volume, purpose) {
|
|
415
|
+
let kept = this.drawList.filter((g) => boxInVolume(this.box(g), volume));
|
|
416
|
+
if (purpose === 'view' && this.horizon) {
|
|
417
|
+
// Behind the horizon: the nearest point of the cell's box is farther than its highest point could be seen.
|
|
418
|
+
const h = this.horizon;
|
|
419
|
+
const eye = this.toRender(this.eyeWorld);
|
|
420
|
+
const fromCenter = [this.eyeWorld[0] - h.center[0], this.eyeWorld[1] - h.center[1], this.eyeWorld[2] - h.center[2]];
|
|
421
|
+
const inView = kept.length;
|
|
422
|
+
kept = kept.filter((g) => !horizonHidden(fromCenter, h.radiusM, distanceToBox(eye, this.box(g)), g.maxRadius));
|
|
423
|
+
this.horizonCulled = inView - kept.length;
|
|
424
|
+
}
|
|
425
|
+
if (purpose === 'shadow')
|
|
426
|
+
this.shadowList = kept;
|
|
427
|
+
else
|
|
428
|
+
this.viewList = kept;
|
|
429
|
+
return purpose === 'view' ? kept.reduce((s, g) => s + g.batches.reduce((t, b) => t + b.count, 0), 0) : 0;
|
|
430
|
+
}
|
|
431
|
+
draw(ctx) {
|
|
432
|
+
const list = ctx.pass === 'shadow' ? this.shadowList : this.viewList;
|
|
433
|
+
if (list.length === 0)
|
|
434
|
+
return;
|
|
435
|
+
const counting = ctx.pass === 'color' || ctx.pass === 'data';
|
|
436
|
+
if (counting)
|
|
437
|
+
this.frame = { ...this.frame, drawCalls: 0, triangles: 0, instances: 0 };
|
|
438
|
+
const names = Object.keys(this.materials).sort();
|
|
439
|
+
const morph = this.morph ? this.morphFrame() : null;
|
|
440
|
+
for (const name of names) {
|
|
441
|
+
const material = this.materials[name];
|
|
442
|
+
const program = material.program(ctx.pass);
|
|
443
|
+
if (!program)
|
|
444
|
+
continue;
|
|
445
|
+
let used = false;
|
|
446
|
+
const begin = () => {
|
|
447
|
+
if (used)
|
|
448
|
+
return;
|
|
449
|
+
used = true;
|
|
450
|
+
const state = material.state[ctx.pass];
|
|
451
|
+
if (state)
|
|
452
|
+
ctx.device.setState(state);
|
|
453
|
+
ctx.device.use(program);
|
|
454
|
+
if (ctx.pass === 'shadow')
|
|
455
|
+
program.mat4('u_lightRender', ctx.lightRender);
|
|
456
|
+
else
|
|
457
|
+
program.int('u_shadowMap', SHADOW_MAP_UNIT);
|
|
458
|
+
for (const u in this.uniforms)
|
|
459
|
+
program.set(u, this.uniforms[u]);
|
|
460
|
+
const own = this.materialUniforms[name];
|
|
461
|
+
if (own)
|
|
462
|
+
for (const u in own)
|
|
463
|
+
program.set(u, own[u]);
|
|
464
|
+
const textures = this.textures[name];
|
|
465
|
+
if (textures) {
|
|
466
|
+
let unit = SHADOW_MAP_UNIT + 1;
|
|
467
|
+
for (const sampler in textures) {
|
|
468
|
+
ctx.device.texture(unit, textures[sampler]);
|
|
469
|
+
program.int(sampler, unit++);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (morph) {
|
|
473
|
+
program.set('u_morphEye', { vec4: morph.eye });
|
|
474
|
+
program.vec3('u_morphUp', morph.up[0], morph.up[1], morph.up[2]);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
for (const g of list) {
|
|
478
|
+
const ox = g.origin[0] - this.origin[0];
|
|
479
|
+
const oy = g.origin[1] - this.origin[1];
|
|
480
|
+
const oz = g.origin[2] - this.origin[2];
|
|
481
|
+
const blend = morph ? morph.range(g.cell.level) : null;
|
|
482
|
+
for (const m of g.meshes) {
|
|
483
|
+
if (m.material !== material)
|
|
484
|
+
continue;
|
|
485
|
+
begin();
|
|
486
|
+
program.vec3('u_cellOffset', ox, oy, oz);
|
|
487
|
+
program.float('u_cellLevel', g.cell.level);
|
|
488
|
+
if (blend)
|
|
489
|
+
program.vec2('u_cellMorph', blend[0], blend[1]);
|
|
490
|
+
ctx.device.draw(m.mesh);
|
|
491
|
+
ctx.count(m.mesh.triangles);
|
|
492
|
+
if (counting) {
|
|
493
|
+
this.frame.drawCalls++;
|
|
494
|
+
this.frame.triangles += m.mesh.triangles;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
let dist = -1;
|
|
498
|
+
for (const b of g.batches) {
|
|
499
|
+
if (b.material !== material)
|
|
500
|
+
continue;
|
|
501
|
+
if (dist < 0)
|
|
502
|
+
dist = distanceToBox(this.eye, this.box(g));
|
|
503
|
+
const arch = this.archetypes[b.archetype];
|
|
504
|
+
const lod = arch.lods.findIndex((l) => dist <= l.maxDistance);
|
|
505
|
+
if (lod < 0)
|
|
506
|
+
continue;
|
|
507
|
+
begin();
|
|
508
|
+
program.vec3('u_cellOffset', ox, oy, oz);
|
|
509
|
+
program.float('u_cellLevel', g.cell.level);
|
|
510
|
+
if (blend)
|
|
511
|
+
program.vec2('u_cellMorph', blend[0], blend[1]);
|
|
512
|
+
const mesh = b.lods[lod];
|
|
513
|
+
ctx.device.draw(mesh);
|
|
514
|
+
ctx.count(mesh.triangles * mesh.instances);
|
|
515
|
+
if (counting) {
|
|
516
|
+
this.frame.drawCalls++;
|
|
517
|
+
this.frame.triangles += mesh.triangles * mesh.instances;
|
|
518
|
+
this.frame.instances += mesh.instances;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/** Releases every GPU resource, aborts jobs and detaches from the renderer. */
|
|
525
|
+
dispose() {
|
|
526
|
+
this.undo();
|
|
527
|
+
for (const a of this.running.values())
|
|
528
|
+
a.abort();
|
|
529
|
+
this.running.clear();
|
|
530
|
+
for (const k of [...this.gpu.keys()])
|
|
531
|
+
this.release(k);
|
|
532
|
+
this.cpu.clear();
|
|
533
|
+
this.renderer.remove(this);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
/** Tickets for a viewer: one at the eye, and one at the predicted point ahead when it moves (design: `predict`). */
|
|
537
|
+
export function viewTickets(eye, altitude, forward, velocity = [0, 0], lookAheadS = 2) {
|
|
538
|
+
const t = [{ id: 'camera', kind: 'camera', center: eye, altitude: Math.max(0, altitude), base: 0, dir: forward }];
|
|
539
|
+
const speed = Math.sqrt(velocity[0] * velocity[0] + velocity[1] * velocity[1]);
|
|
540
|
+
if (speed > 0.5) {
|
|
541
|
+
t.push({ id: 'predict', kind: 'predict', center: [eye[0] + velocity[0] * lookAheadS, eye[1], eye[2] + velocity[1] * lookAheadS], altitude: Math.max(0, altitude), base: 1, dir: velocity });
|
|
542
|
+
}
|
|
543
|
+
return t;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Tickets for a viewer on any world shape: as `viewTickets`, with the directions as world vectors and, for selection
|
|
547
|
+
* by screen-space error, the pixels one radian covers on the viewer's screen (`pixelsPerRadian` of world).
|
|
548
|
+
*/
|
|
549
|
+
export function viewerTickets(o) {
|
|
550
|
+
const altitude = Math.max(0, o.altitude);
|
|
551
|
+
const px = o.pixelsPerRadian !== undefined ? { pixelsPerRadian: o.pixelsPerRadian } : {};
|
|
552
|
+
const t = [{ id: 'camera', kind: 'camera', center: o.eye, altitude, base: 0, dir: o.forward, ...px }];
|
|
553
|
+
const v = o.velocity ?? [0, 0, 0];
|
|
554
|
+
const look = o.lookAheadS ?? 2;
|
|
555
|
+
if (Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) > 0.5) {
|
|
556
|
+
t.push({ id: 'predict', kind: 'predict', center: [o.eye[0] + v[0] * look, o.eye[1] + v[1] * look, o.eye[2] + v[2] * look], altitude, base: 1, dir: v, ...px });
|
|
557
|
+
}
|
|
558
|
+
return t;
|
|
559
|
+
}
|
|
560
|
+
/** Generator provider running in a pool of module workers. The worker script calls `serveCellGenerator`. */
|
|
561
|
+
export class WorkerCellProvider {
|
|
562
|
+
name;
|
|
563
|
+
blocking = false;
|
|
564
|
+
pool;
|
|
565
|
+
seed;
|
|
566
|
+
edits;
|
|
567
|
+
/** With `edits`, the edits text of a cell travels with its job when it is not empty (read when the request starts). */
|
|
568
|
+
constructor(workerUrl, seed, size, edits) {
|
|
569
|
+
this.pool = new WorkerPool(workerUrl, size);
|
|
570
|
+
this.seed = seed;
|
|
571
|
+
this.edits = edits;
|
|
572
|
+
this.name = `generator (${this.pool.size} workers)`;
|
|
573
|
+
}
|
|
574
|
+
async request(cell, signal) {
|
|
575
|
+
const message = { seed: this.seed, cell: { face: cell.face, level: cell.level, x: cell.x, y: cell.y } };
|
|
576
|
+
const edits = this.edits ? this.edits(cell) : '';
|
|
577
|
+
if (edits)
|
|
578
|
+
message.edits = edits;
|
|
579
|
+
const job = this.pool.run(message);
|
|
580
|
+
const onAbort = () => job.cancel();
|
|
581
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
582
|
+
try {
|
|
583
|
+
const buf = await job.promise;
|
|
584
|
+
const bytes = new Uint8Array(buf);
|
|
585
|
+
return { content: decodeCell(bytes), bytes, origin: 'generated' };
|
|
586
|
+
}
|
|
587
|
+
finally {
|
|
588
|
+
signal.removeEventListener('abort', onAbort);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
dispose() {
|
|
592
|
+
this.pool.dispose();
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
export function serveCellGenerator(generate) {
|
|
596
|
+
serveWorker((message) => {
|
|
597
|
+
const { seed, cell, edits } = message;
|
|
598
|
+
const id = Object.freeze({ ...cell });
|
|
599
|
+
const bytes = encodeCell(typeof edits === 'string' && edits ? generate(seed, id, edits) : generate(seed, id));
|
|
600
|
+
return { value: bytes.buffer, transfer: [bytes.buffer] };
|
|
601
|
+
});
|
|
602
|
+
}
|