wallhammer-web 0.0.0-stage → 0.4.1
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 +92 -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,1100 @@
|
|
|
1
|
+
// Generic renderer: a shadow map pass from the main directional light, then the main pass, then an overlay pass.
|
|
2
|
+
// It knows meshes, materials and passes, nothing about what they show. Everything drawn comes from layers registered
|
|
3
|
+
// from outside, drawn in registration order in every pass. The shadow map is refitted only when the view, the light
|
|
4
|
+
// direction or `shadowKeyExtra` changes, so a static frame is a single pass. Layers whose casters move
|
|
5
|
+
// (`shadowCasters: 'dynamic'`) are drawn over a kept copy of the static casters' depth in the frames where they
|
|
6
|
+
// changed (`shadowDirty`), so a moving part costs its own triangles, not the whole scene's (mode `'single'`).
|
|
7
|
+
//
|
|
8
|
+
// Depth. The shadow pass always draws standard depth into its depth target. The main passes (`color`, `data`,
|
|
9
|
+
// `overlay`) follow the renderer's depth mode (`RendererOptions.depth`, default `'standard'`): the device owns the
|
|
10
|
+
// convention of the bound target (clear value, comparison, sign of the polygon offset, clip range), the renderer
|
|
11
|
+
// builds the matching view matrix (`depthViewProj`) and, in the logarithmic mode, wraps every fragment shader of the
|
|
12
|
+
// main target so that it writes its depth itself (`resolve`). Layers and materials are the same in every mode.
|
|
13
|
+
//
|
|
14
|
+
// Shadow modes (`RendererOptions.shadows`, default `'single'`: the one fitted map described above). `'snapped'` and
|
|
15
|
+
// `'cascades'` keep square areas of the light's plane in the map instead (shadow-areas.ts): fixed to the world's texel
|
|
16
|
+
// grid, following a focus in whole blocks and redrawn only where they newly cover something, or as a whole when the
|
|
17
|
+
// light or the scene changes. Beyond their reach the lookup falls back to a term baked by the material or to the
|
|
18
|
+
// scene's light horizon texture (`shadowHorizon`). The lookup lives in the program header (shadow-glsl.ts).
|
|
19
|
+
import { cross, dot, normalize, orthoMatrix } from '../base/math.js';
|
|
20
|
+
import { depthViewProj, newCornerRays, newOrthoFrame } from '../base/view.js';
|
|
21
|
+
import { UBO_BINDING } from '../gpu/index.js';
|
|
22
|
+
import { LOCAL_UP_DEFINE, LOCAL_UP_FLOATS, LOCAL_UP_GLSL, LOCAL_UP_MEMBER, LOG_DEPTH_EPILOGUE, LOG_DEPTH_PRELUDE, NOISE, PIXEL_SIZE_GLSL, frameBlockFloats, shaderHeader } from './glsl.js';
|
|
23
|
+
import { DEPTH_FS, DEPTH_VS, SHADOW_AREAS_GLSL, SHADOW_AREAS_MAX, SHADOW_HORIZON_UNIT, shadowAreaFloats, shadowAreaMembers } from './shadow-glsl.js';
|
|
24
|
+
import { AREA_BLOCKS, AREA_REACH_BLOCKS, followFocus, ladderStep, lightFrame, newShadowArea } from './shadow-areas.js';
|
|
25
|
+
/** Texture unit that holds the shadow map during the `color` and `data` passes. */
|
|
26
|
+
export const SHADOW_MAP_UNIT = 0;
|
|
27
|
+
/** A compiled material: a program per pass, its attribute locations and draw state. */
|
|
28
|
+
export class Material {
|
|
29
|
+
name;
|
|
30
|
+
attribs;
|
|
31
|
+
state;
|
|
32
|
+
programs = new Map();
|
|
33
|
+
/** `resolve` turns a shader source of the given pass into what is compiled (includes, see `Renderer.resolve`). */
|
|
34
|
+
constructor(device, desc, resolve = (src) => src) {
|
|
35
|
+
this.name = desc.name;
|
|
36
|
+
this.attribs = desc.attribs;
|
|
37
|
+
this.state = desc.state ?? {};
|
|
38
|
+
for (const [pass, src] of Object.entries(desc.passes)) {
|
|
39
|
+
const s = src === 'depth' ? { vs: DEPTH_VS, fs: DEPTH_FS } : { vs: resolve(src.vs, pass), fs: resolve(src.fs, pass) };
|
|
40
|
+
this.programs.set(pass, device.program(`${desc.name} ${pass}`, s.vs, s.fs, desc.attribs));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Passes this material has a program for. */
|
|
44
|
+
get passes() {
|
|
45
|
+
return [...this.programs.keys()];
|
|
46
|
+
}
|
|
47
|
+
/** Program for a pass. The `data` pass falls back to the `color` program. */
|
|
48
|
+
program(pass) {
|
|
49
|
+
return this.programs.get(pass) ?? (pass === 'data' ? (this.programs.get('color') ?? null) : null);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** The penumbra should cover at least this many shadow map texels. Fewer gives stair-steps, more wastes resolution. */
|
|
53
|
+
const PENUMBRA_TEXELS = 4.5;
|
|
54
|
+
const SHADOW_MIN_PX = 512;
|
|
55
|
+
/** Step by which the shadow map viewport size changes. Keeps the texel size constant when panning. */
|
|
56
|
+
const SHADOW_SIZE_STEP_M = 16;
|
|
57
|
+
/** Depth offset of the shadow pass, scaled by the surface slope, so a surface does not shadow itself. */
|
|
58
|
+
const SHADOW_OFFSET = [2.0, 4.0];
|
|
59
|
+
/** Shadow areas: share of an area's reach over which it blends into the next area, and the last one into the far term. */
|
|
60
|
+
const AREA_BLEND = 0.15;
|
|
61
|
+
const AREA_BLEND_LAST = 0.25;
|
|
62
|
+
/** Shadow areas: the depth range along the light follows the scene extent in steps of this many metres. */
|
|
63
|
+
const AREA_DEPTH_STEP_M = 16;
|
|
64
|
+
const BASE_STATE = {
|
|
65
|
+
depthTest: true,
|
|
66
|
+
depthFunc: 'less',
|
|
67
|
+
depthWrite: true,
|
|
68
|
+
cullBack: true,
|
|
69
|
+
cull: 'back',
|
|
70
|
+
blend: null,
|
|
71
|
+
alphaToCoverage: false,
|
|
72
|
+
polygonOffset: null,
|
|
73
|
+
};
|
|
74
|
+
const SHADOW_STATE = { ...BASE_STATE, polygonOffset: SHADOW_OFFSET };
|
|
75
|
+
export class Renderer {
|
|
76
|
+
device;
|
|
77
|
+
layers = [];
|
|
78
|
+
/** Called at the start of every frame, before anything is read from the renderer's fields. */
|
|
79
|
+
beforeFrame = [];
|
|
80
|
+
/** Extent of the scene; `null` draws only the background. */
|
|
81
|
+
bounds = null;
|
|
82
|
+
/**
|
|
83
|
+
* World position of the render-space origin, for a world drawn relative to a moving origin (double precision, CPU
|
|
84
|
+
* only). Shadow map texels snap to the world grid instead of the render-space grid, so a rebase does not shift them.
|
|
85
|
+
*/
|
|
86
|
+
worldOrigin = [0, 0, 0];
|
|
87
|
+
background = [0, 0, 0];
|
|
88
|
+
/** Unit direction toward the main directional light. */
|
|
89
|
+
lightDirection = [0, 1, 0];
|
|
90
|
+
/** Anything besides the view and the light direction that changes shadows (environment slots contribute to it). */
|
|
91
|
+
shadowKeyExtra = '';
|
|
92
|
+
/** `'data'` draws the `data` pass instead of `color` and skips `overlay`. */
|
|
93
|
+
mode = 'color';
|
|
94
|
+
/** Contents of the owner's slots in the Frame block, uploaded every frame. */
|
|
95
|
+
frameSlots;
|
|
96
|
+
/** Shader includes by name (`#include <name>` on a line of its own). `header` and `noise` are built in. */
|
|
97
|
+
includes = new Map();
|
|
98
|
+
lastStats = { triangles: 0, drawCalls: 0, shadowTriangles: 0, shadowDrawCalls: 0, dynamicShadowTriangles: 0, dynamicShadowDrawCalls: 0, cpuMs: 0, shadowMapPx: 0, instancesVisible: 0, shadowUpdates: 0 };
|
|
99
|
+
/** The depth mode of the main passes in effect (after the fallback from `'reversed'` to `'logarithmic'`). */
|
|
100
|
+
depthMode;
|
|
101
|
+
/** Reversed and logarithmic depth with a perspective view: distance of the near plane in metres. */
|
|
102
|
+
depthNearM;
|
|
103
|
+
/** Measuring hooks (`RenderProbe`). Null in normal use; set only while profiling and reset afterwards. */
|
|
104
|
+
probe = null;
|
|
105
|
+
shadowSoftnessM;
|
|
106
|
+
shadowMapSize;
|
|
107
|
+
shadowDistanceM;
|
|
108
|
+
frameBlock;
|
|
109
|
+
frameData;
|
|
110
|
+
shadowTarget;
|
|
111
|
+
shadowKey = '';
|
|
112
|
+
lightRender = new Float32Array(16);
|
|
113
|
+
lightSample = new Float32Array(16);
|
|
114
|
+
shadowPx = 0;
|
|
115
|
+
shadowTexelM = 1;
|
|
116
|
+
instancesVisible = 0;
|
|
117
|
+
lightFit = null;
|
|
118
|
+
/** The shadow mode in effect. */
|
|
119
|
+
shadowMode;
|
|
120
|
+
/**
|
|
121
|
+
* Shadow modes `'snapped'` and `'cascades'`: the scene's light horizon, used beyond the reach of the shadow map, or
|
|
122
|
+
* null for none (points out there are then lit unless the material bakes its own term). May be replaced at any
|
|
123
|
+
* time; the texture stays the caller's to delete.
|
|
124
|
+
*/
|
|
125
|
+
shadowHorizon = null;
|
|
126
|
+
areaState = null;
|
|
127
|
+
/** The radial up this renderer was made with, or null when up is +y. */
|
|
128
|
+
radialUp;
|
|
129
|
+
/** Mode `'single'` with dynamic casters: the depth map that holds only the static casters; made with the first dynamic layer. */
|
|
130
|
+
staticShadow = null;
|
|
131
|
+
/** Whether the last frame had a dynamic layer. */
|
|
132
|
+
hadDynamic = false;
|
|
133
|
+
/** Mode `'single'`: what the kept map was fitted to (view key, light direction in 1e-4 steps, `shadowKeyExtra`). */
|
|
134
|
+
singleKeyValid = false;
|
|
135
|
+
singleKeyView = '';
|
|
136
|
+
singleKeyLight = new Float64Array(3);
|
|
137
|
+
singleKeyExtra = '';
|
|
138
|
+
/** Instances kept by the static layers at the last view culling of a refit. */
|
|
139
|
+
staticVisible = 0;
|
|
140
|
+
/** The light's volume of the last refit (mode `'single'`), and the view's volume of the last culling with its key. */
|
|
141
|
+
lightVolume = null;
|
|
142
|
+
viewVolume = null;
|
|
143
|
+
viewVolumeKey = '';
|
|
144
|
+
/** Preallocated per-frame scratch, so a frame that changes nothing allocates nothing here. */
|
|
145
|
+
proj = { viewProj: new Float32Array(16), logScale: 0 };
|
|
146
|
+
camAxes = newOrthoFrame();
|
|
147
|
+
rays = newCornerRays();
|
|
148
|
+
sceneCenter = [0, 0, 0];
|
|
149
|
+
singleRender = new Float32Array(16);
|
|
150
|
+
singleSample = new Float32Array(16);
|
|
151
|
+
countMain = { tris: 0, calls: 0 };
|
|
152
|
+
countShadow = { tris: 0, calls: 0 };
|
|
153
|
+
countDynamic = { tris: 0, calls: 0 };
|
|
154
|
+
/** The draw context handed to the layers, refilled for every pass. */
|
|
155
|
+
ctx;
|
|
156
|
+
counting = this.countMain;
|
|
157
|
+
constructor(device, options) {
|
|
158
|
+
this.device = device;
|
|
159
|
+
this.shadowSoftnessM = options.shadowSoftnessM ?? 0.9;
|
|
160
|
+
this.shadowMapSize = options.shadowMapSize ?? 4096;
|
|
161
|
+
this.shadowDistanceM = options.shadowDistanceM ?? Infinity;
|
|
162
|
+
this.depthMode = options.depth !== undefined ? device.setDepthMode(options.depth) : device.depthMode;
|
|
163
|
+
if (options.samples !== undefined)
|
|
164
|
+
device.setMainSamples(options.samples);
|
|
165
|
+
this.depthNearM = options.depthNearM ?? 0.1;
|
|
166
|
+
const slots = options.frameSlots ?? { afterLight: 0, afterView: 0, glsl: { afterLight: '', afterView: '' } };
|
|
167
|
+
this.shadowMode = options.shadows?.mode ?? 'single';
|
|
168
|
+
// Local up: with `radialUp` one more member closes the Frame block; without it the header text is unchanged.
|
|
169
|
+
this.radialUp = options.radialUp ?? null;
|
|
170
|
+
const upDefines = this.radialUp ? LOCAL_UP_DEFINE : '';
|
|
171
|
+
const upMember = this.radialUp ? LOCAL_UP_MEMBER : '';
|
|
172
|
+
const upFloats = this.radialUp ? LOCAL_UP_FLOATS : 0;
|
|
173
|
+
if (this.shadowMode === 'single') {
|
|
174
|
+
this.includes.set('header', shaderHeader(slots.glsl, this.depthMode, this.radialUp ? { defines: upDefines, frameTail: upMember } : {}) + LOCAL_UP_GLSL + PIXEL_SIZE_GLSL);
|
|
175
|
+
this.frameData = new Float32Array(frameBlockFloats(slots) + upFloats);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
// One area fills the map and is addressed around its edges; several share it in a 2 × 2 grid of parts.
|
|
179
|
+
const o = options.shadows ?? {};
|
|
180
|
+
const count = this.shadowMode === 'snapped' ? 1 : Math.max(2, Math.min(SHADOW_AREAS_MAX, Math.round(o.cascades ?? SHADOW_AREAS_MAX)));
|
|
181
|
+
const px = Math.max(AREA_BLOCKS, Math.floor(this.shadowMapSize / (count === 1 ? 1 : 2) / AREA_BLOCKS) * AREA_BLOCKS);
|
|
182
|
+
this.shadowMapSize = count === 1 ? px : px * 2;
|
|
183
|
+
const reachM = o.reachM ?? (this.shadowMode === 'snapped' ? 2048 : Number.isFinite(this.shadowDistanceM) ? this.shadowDistanceM : 4000);
|
|
184
|
+
const firstReachM = Math.min(Math.max(1, o.firstReachM ?? 48), reachM);
|
|
185
|
+
this.areaState = {
|
|
186
|
+
mode: this.shadowMode,
|
|
187
|
+
areas: Array.from({ length: count }, newShadowArea),
|
|
188
|
+
px,
|
|
189
|
+
blockPx: px / AREA_BLOCKS,
|
|
190
|
+
reachM,
|
|
191
|
+
firstReachM,
|
|
192
|
+
step: -1,
|
|
193
|
+
viewKey: '',
|
|
194
|
+
tail: new Float32Array(shadowAreaFloats(count)),
|
|
195
|
+
noHorizon: device.pixelTexture([0, 0, 0, 0]),
|
|
196
|
+
};
|
|
197
|
+
const header = shaderHeader(slots.glsl, this.depthMode, { defines: `#define WH_SHADOW_AREAS ${count}\n` + upDefines, frameTail: shadowAreaMembers(count) + upMember });
|
|
198
|
+
this.includes.set('header', header + LOCAL_UP_GLSL + PIXEL_SIZE_GLSL + SHADOW_AREAS_GLSL);
|
|
199
|
+
this.frameData = new Float32Array(frameBlockFloats(slots) + shadowAreaFloats(count) + upFloats);
|
|
200
|
+
device.fixSampler('u_shadowHorizonMap', SHADOW_HORIZON_UNIT);
|
|
201
|
+
}
|
|
202
|
+
this.includes.set('noise', NOISE);
|
|
203
|
+
this.frameSlots = { afterLight: new Float32Array(slots.afterLight * 4), afterView: new Float32Array(slots.afterView * 4) };
|
|
204
|
+
this.frameBlock = device.uniformBlock(UBO_BINDING.Frame, this.frameData, true);
|
|
205
|
+
device.uniformBlock(UBO_BINDING.Palette, options.palette, false);
|
|
206
|
+
this.shadowTarget = device.depthTarget(this.shadowMapSize, this.areaState?.areas.length === 1);
|
|
207
|
+
// After a context loss the shadow map is empty: draw it again.
|
|
208
|
+
device.onRestored(() => this.invalidateShadows());
|
|
209
|
+
const self = this;
|
|
210
|
+
this.ctx = {
|
|
211
|
+
device,
|
|
212
|
+
pass: 'color',
|
|
213
|
+
view: null,
|
|
214
|
+
lightRender: this.lightRender,
|
|
215
|
+
count(triangles) {
|
|
216
|
+
self.counting.tris += triangles;
|
|
217
|
+
self.counting.calls++;
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/** Edge of the shadow map texture in pixels (`FrameStats.shadowMapPx` is the part of it in use). */
|
|
222
|
+
get shadowMapEdgePx() {
|
|
223
|
+
return this.shadowMapSize;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* With `radialUp`: the point up points away from, in render space (relative to `worldOrigin`). Null when up is +y.
|
|
227
|
+
* Layers that place or cull upright things on the CPU read it.
|
|
228
|
+
*/
|
|
229
|
+
upCenter() {
|
|
230
|
+
const u = this.radialUp;
|
|
231
|
+
if (!u)
|
|
232
|
+
return null;
|
|
233
|
+
return [u.center[0] - this.worldOrigin[0], u.center[1] - this.worldOrigin[1], u.center[2] - this.worldOrigin[2]];
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Replaces include lines with the registered snippets. `into` says where the program draws: `'main'` (default) for
|
|
237
|
+
* the main target, `'other'` for a depth target (the shadow pass) or a bake.
|
|
238
|
+
*
|
|
239
|
+
* In the logarithmic depth mode a fragment shader of the main target that starts from `<header>` is also wrapped
|
|
240
|
+
* (`LOG_DEPTH_PRELUDE`, `LOG_DEPTH_EPILOGUE`), so that it writes its depth itself. A source that never mentions
|
|
241
|
+
* `gl_Position` counts as a fragment shader. A fragment shader with its own `#version` line instead of `<header>`
|
|
242
|
+
* is left alone and keeps the depth of the pipeline: use it only for draws without the depth test.
|
|
243
|
+
*/
|
|
244
|
+
resolve(src, into = 'main') {
|
|
245
|
+
const expand = (afterHeader) => src.replace(/^#include <([\w-]+)>\n/gm, (_, name) => {
|
|
246
|
+
const text = this.includes.get(name);
|
|
247
|
+
if (text === undefined)
|
|
248
|
+
throw new Error(`Unknown shader include <${name}>`);
|
|
249
|
+
return name === 'header' ? text + afterHeader : text;
|
|
250
|
+
});
|
|
251
|
+
const plain = expand('');
|
|
252
|
+
if (this.depthMode !== 'logarithmic' || into !== 'main')
|
|
253
|
+
return plain;
|
|
254
|
+
// Only a fragment shader that starts from the header is wrapped (the wrapper reads the Frame block).
|
|
255
|
+
if (/\bgl_Position\b/.test(plain) || !/^#include <header>\n/m.test(src))
|
|
256
|
+
return plain;
|
|
257
|
+
return expand(LOG_DEPTH_PRELUDE) + LOG_DEPTH_EPILOGUE;
|
|
258
|
+
}
|
|
259
|
+
/** Compiles a material description. */
|
|
260
|
+
material(desc) {
|
|
261
|
+
return new Material(this.device, desc, (src, pass) => this.resolve(src, pass === 'shadow' ? 'other' : 'main'));
|
|
262
|
+
}
|
|
263
|
+
/** Uploads a mesh for a material (the material fixes attribute locations). */
|
|
264
|
+
createMesh(data, material, instances) {
|
|
265
|
+
return this.device.createMesh(data, material.attribs, instances);
|
|
266
|
+
}
|
|
267
|
+
createInstances(data) {
|
|
268
|
+
return this.device.createInstances(data);
|
|
269
|
+
}
|
|
270
|
+
createTexture(data) {
|
|
271
|
+
return this.device.createTexture(data);
|
|
272
|
+
}
|
|
273
|
+
/** Runs a texture bake on the graphics card and returns its targets. */
|
|
274
|
+
bake(desc) {
|
|
275
|
+
return this.device.bake(desc.name, this.resolve(desc.fs, 'other'), desc.width, desc.height, desc.inputs, desc.uniforms ?? {}, desc.targets);
|
|
276
|
+
}
|
|
277
|
+
add(layer) {
|
|
278
|
+
this.layers.push(layer);
|
|
279
|
+
}
|
|
280
|
+
/** Registers a layer to draw just before `before` (at the end when `before` is not registered). */
|
|
281
|
+
addBefore(layer, before) {
|
|
282
|
+
const i = this.layers.indexOf(before);
|
|
283
|
+
if (i < 0)
|
|
284
|
+
this.layers.push(layer);
|
|
285
|
+
else
|
|
286
|
+
this.layers.splice(i, 0, layer);
|
|
287
|
+
}
|
|
288
|
+
remove(layer) {
|
|
289
|
+
const i = this.layers.indexOf(layer);
|
|
290
|
+
if (i >= 0)
|
|
291
|
+
this.layers.splice(i, 1);
|
|
292
|
+
}
|
|
293
|
+
/** After a scene or light change forces a new shadow computation. */
|
|
294
|
+
invalidateShadows() {
|
|
295
|
+
this.shadowKey = '';
|
|
296
|
+
this.singleKeyValid = false;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Fits the light's orthographic projection to what the view sees: the projection of the visible volume (corner rays cut by
|
|
300
|
+
* the bottom and top of the scene) onto the plane perpendicular to the light, clipped by the scene extent and snapped
|
|
301
|
+
* to texels so shadows do not shimmer when panning.
|
|
302
|
+
*/
|
|
303
|
+
fitLight(view, bounds, toLight) {
|
|
304
|
+
const forward = [-toLight[0], -toLight[1], -toLight[2]];
|
|
305
|
+
const right = normalize(cross(forward, [0, 1, 0]));
|
|
306
|
+
const up = cross(right, forward);
|
|
307
|
+
const frame = { right, up, forward };
|
|
308
|
+
let vMinX = Infinity;
|
|
309
|
+
let vMaxX = -Infinity;
|
|
310
|
+
let vMinY = Infinity;
|
|
311
|
+
let vMaxY = -Infinity;
|
|
312
|
+
// A perspective corner ray starts at the eye: where it never reaches a plane (or reaches it behind the eye), it runs
|
|
313
|
+
// on to the far end of the scene (or to `shadowDistanceM`). An orthographic corner ray is a line and is used both ways.
|
|
314
|
+
const perspective = view.pixelGrowth > 0;
|
|
315
|
+
for (const ray of view.cornerRays(this.rays)) {
|
|
316
|
+
const reach = perspective ? Math.min(farthestCorner(ray.origin, bounds), this.shadowDistanceM) : 0;
|
|
317
|
+
// In perspective the eye itself (the apex of the visible volume) counts too, as the third target with t = 0.
|
|
318
|
+
for (let target = 0; target < (perspective ? 3 : 2); target++) {
|
|
319
|
+
const yTarget = target === 0 ? bounds.min[1] : target === 1 ? bounds.max[1] : NaN;
|
|
320
|
+
let t = (yTarget - ray.origin[1]) / ray.dir[1];
|
|
321
|
+
if (perspective)
|
|
322
|
+
t = Number.isNaN(yTarget) ? 0 : t >= 0 && t <= reach ? t : reach;
|
|
323
|
+
const p = [ray.origin[0] + ray.dir[0] * t, perspective ? ray.origin[1] + ray.dir[1] * t : yTarget, ray.origin[2] + ray.dir[2] * t];
|
|
324
|
+
const lx = dot(right, p);
|
|
325
|
+
const ly = dot(up, p);
|
|
326
|
+
vMinX = Math.min(vMinX, lx);
|
|
327
|
+
vMaxX = Math.max(vMaxX, lx);
|
|
328
|
+
vMinY = Math.min(vMinY, ly);
|
|
329
|
+
vMaxY = Math.max(vMaxY, ly);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
let sMinX = Infinity;
|
|
333
|
+
let sMaxX = -Infinity;
|
|
334
|
+
let sMinY = Infinity;
|
|
335
|
+
let sMaxY = -Infinity;
|
|
336
|
+
let sMinZ = Infinity;
|
|
337
|
+
let sMaxZ = -Infinity;
|
|
338
|
+
for (let k = 0; k < 8; k++) {
|
|
339
|
+
const p = [k & 1 ? bounds.max[0] : bounds.min[0], k & 2 ? bounds.max[1] : bounds.min[1], k & 4 ? bounds.max[2] : bounds.min[2]];
|
|
340
|
+
const lx = dot(right, p);
|
|
341
|
+
const ly = dot(up, p);
|
|
342
|
+
const lz = dot(forward, p);
|
|
343
|
+
sMinX = Math.min(sMinX, lx);
|
|
344
|
+
sMaxX = Math.max(sMaxX, lx);
|
|
345
|
+
sMinY = Math.min(sMinY, ly);
|
|
346
|
+
sMaxY = Math.max(sMaxY, ly);
|
|
347
|
+
sMinZ = Math.min(sMinZ, lz);
|
|
348
|
+
sMaxZ = Math.max(sMaxZ, lz);
|
|
349
|
+
}
|
|
350
|
+
const minX = Math.max(vMinX, sMinX);
|
|
351
|
+
const maxX = Math.min(vMaxX, sMaxX);
|
|
352
|
+
const minY = Math.max(vMinY, sMinY);
|
|
353
|
+
const maxY = Math.min(vMaxY, sMaxY);
|
|
354
|
+
const span = Math.max(maxX - minX, maxY - minY, SHADOW_SIZE_STEP_M);
|
|
355
|
+
const size = Math.ceil(span / SHADOW_SIZE_STEP_M) * SHADOW_SIZE_STEP_M + SHADOW_SIZE_STEP_M;
|
|
356
|
+
// Resolution: the penumbra should come out at a fixed number of texels, more is not needed.
|
|
357
|
+
const wanted = Math.ceil(size / (this.shadowSoftnessM / PENUMBRA_TEXELS));
|
|
358
|
+
this.shadowPx = Math.max(SHADOW_MIN_PX, Math.min(this.shadowMapSize, wanted));
|
|
359
|
+
const texel = size / this.shadowPx;
|
|
360
|
+
this.shadowTexelM = texel;
|
|
361
|
+
const wo = this.worldOrigin;
|
|
362
|
+
const atZero = wo[0] === 0 && wo[1] === 0 && wo[2] === 0;
|
|
363
|
+
const ox = atZero ? 0 : dot(right, wo);
|
|
364
|
+
const oy = atZero ? 0 : dot(up, wo);
|
|
365
|
+
const cx = Math.round(((minX + maxX) / 2 + ox) / texel) * texel - ox;
|
|
366
|
+
const cy = Math.round(((minY + maxY) / 2 + oy) / texel) * texel - oy;
|
|
367
|
+
return { frame, cx, cy, cz: (sMinZ + sMaxZ) / 2, half: size / 2, halfD: (sMaxZ - sMinZ) / 2 + 10 };
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* What the view sees and what the scene spans, along the axes of a light frame (render space): the rectangle of the
|
|
371
|
+
* light's plane the visible volume projects to, cut to the scene extent, the scene's range along the light, and the
|
|
372
|
+
* eye of a perspective view. The visible volume is taken as in `fitLight`; `reachM` limits a perspective view.
|
|
373
|
+
*/
|
|
374
|
+
viewFootprint(view, bounds, frame, reachM) {
|
|
375
|
+
const { right, up, forward } = frame;
|
|
376
|
+
let vMinX = Infinity;
|
|
377
|
+
let vMaxX = -Infinity;
|
|
378
|
+
let vMinY = Infinity;
|
|
379
|
+
let vMaxY = -Infinity;
|
|
380
|
+
const perspective = view.pixelGrowth > 0;
|
|
381
|
+
const rays = view.cornerRays(this.rays);
|
|
382
|
+
for (const ray of rays) {
|
|
383
|
+
const reach = perspective ? Math.min(farthestCorner(ray.origin, bounds), reachM) : 0;
|
|
384
|
+
for (let target = 0; target < (perspective ? 3 : 2); target++) {
|
|
385
|
+
const yTarget = target === 0 ? bounds.min[1] : target === 1 ? bounds.max[1] : NaN;
|
|
386
|
+
let t = (yTarget - ray.origin[1]) / ray.dir[1];
|
|
387
|
+
if (perspective)
|
|
388
|
+
t = Number.isNaN(yTarget) ? 0 : t >= 0 && t <= reach ? t : reach;
|
|
389
|
+
else if (!Number.isFinite(t))
|
|
390
|
+
continue;
|
|
391
|
+
const p = [ray.origin[0] + ray.dir[0] * t, perspective ? ray.origin[1] + ray.dir[1] * t : yTarget, ray.origin[2] + ray.dir[2] * t];
|
|
392
|
+
const lx = dot(right, p);
|
|
393
|
+
const ly = dot(up, p);
|
|
394
|
+
vMinX = Math.min(vMinX, lx);
|
|
395
|
+
vMaxX = Math.max(vMaxX, lx);
|
|
396
|
+
vMinY = Math.min(vMinY, ly);
|
|
397
|
+
vMaxY = Math.max(vMaxY, ly);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
let sMinX = Infinity;
|
|
401
|
+
let sMaxX = -Infinity;
|
|
402
|
+
let sMinY = Infinity;
|
|
403
|
+
let sMaxY = -Infinity;
|
|
404
|
+
let minZ = Infinity;
|
|
405
|
+
let maxZ = -Infinity;
|
|
406
|
+
for (let k = 0; k < 8; k++) {
|
|
407
|
+
const p = [k & 1 ? bounds.max[0] : bounds.min[0], k & 2 ? bounds.max[1] : bounds.min[1], k & 4 ? bounds.max[2] : bounds.min[2]];
|
|
408
|
+
const lx = dot(right, p);
|
|
409
|
+
const ly = dot(up, p);
|
|
410
|
+
const lz = dot(forward, p);
|
|
411
|
+
sMinX = Math.min(sMinX, lx);
|
|
412
|
+
sMaxX = Math.max(sMaxX, lx);
|
|
413
|
+
sMinY = Math.min(sMinY, ly);
|
|
414
|
+
sMaxY = Math.max(sMaxY, ly);
|
|
415
|
+
minZ = Math.min(minZ, lz);
|
|
416
|
+
maxZ = Math.max(maxZ, lz);
|
|
417
|
+
}
|
|
418
|
+
const eye = perspective ? [rays[0].origin[0], rays[0].origin[1], rays[0].origin[2]] : null;
|
|
419
|
+
if (!(vMinX <= vMaxX) || !(vMinY <= vMaxY))
|
|
420
|
+
return { minX: sMinX, maxX: sMaxX, minY: sMinY, maxY: sMaxY, minZ, maxZ, eye };
|
|
421
|
+
// A view that looks past the scene keeps its own rectangle: there is nothing to cut it to.
|
|
422
|
+
const cut = Math.max(vMinX, sMinX) <= Math.min(vMaxX, sMaxX) && Math.max(vMinY, sMinY) <= Math.min(vMaxY, sMaxY);
|
|
423
|
+
if (!cut)
|
|
424
|
+
return { minX: vMinX, maxX: vMaxX, minY: vMinY, maxY: vMaxY, minZ, maxZ, eye };
|
|
425
|
+
return { minX: Math.max(vMinX, sMinX), maxX: Math.min(vMaxX, sMaxX), minY: Math.max(vMinY, sMinY), maxY: Math.min(vMaxY, sMaxY), minZ, maxZ, eye };
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Shadow areas, first half of a frame: places every area around the focus of the view, decides what of the map
|
|
429
|
+
* must be drawn and fills the areas' part of the Frame block. Positions on the block grid are counted from the
|
|
430
|
+
* world origin (`worldOrigin`), so neither a moving render origin nor a pan shifts a texel against the world.
|
|
431
|
+
*/
|
|
432
|
+
planAreas(st, view, bounds, toLight) {
|
|
433
|
+
// The light's plane is laid by the direction as the cache key holds it (4 decimals), not by the exact one: a light
|
|
434
|
+
// that drifts by less than the key resolves must not turn the plane under a map that is kept as it is.
|
|
435
|
+
const lightKey = `${toLight[0].toFixed(4)},${toLight[1].toFixed(4)},${toLight[2].toFixed(4)}`;
|
|
436
|
+
const [kx, ky, kz] = lightKey.split(',').map(Number);
|
|
437
|
+
const frame = lightFrame(kx === 0 && ky === 0 && kz === 0 ? toLight : normalize([kx, ky, kz]));
|
|
438
|
+
const wo = this.worldOrigin;
|
|
439
|
+
const ox = dot(frame.right, wo);
|
|
440
|
+
const oy = dot(frame.up, wo);
|
|
441
|
+
const oz = dot(frame.forward, wo);
|
|
442
|
+
const fp = this.viewFootprint(view, bounds, frame, st.reachM);
|
|
443
|
+
// The depth range follows the scene extent in coarse steps, so that it rarely changes (a change redraws the map).
|
|
444
|
+
const q = AREA_DEPTH_STEP_M;
|
|
445
|
+
const zMin = Math.floor((fp.minZ + oz) / q) * q - 10;
|
|
446
|
+
const zMax = Math.ceil((fp.maxZ + oz) / q) * q + 10;
|
|
447
|
+
const key = `${lightKey}|${this.shadowKeyExtra}|${zMin},${zMax}`;
|
|
448
|
+
let redraw = key !== this.shadowKey;
|
|
449
|
+
for (const layer of this.layers)
|
|
450
|
+
if (layer.cullStale?.())
|
|
451
|
+
redraw = true;
|
|
452
|
+
// The focus (render space, in the light's plane) and the block size of every area.
|
|
453
|
+
const n = st.areas.length;
|
|
454
|
+
let fx = (fp.minX + fp.maxX) / 2;
|
|
455
|
+
let fy = (fp.minY + fp.maxY) / 2;
|
|
456
|
+
const blocks = [];
|
|
457
|
+
if (st.mode === 'snapped') {
|
|
458
|
+
// The finest map has the texel the softness asks for; from there the map grows with the view.
|
|
459
|
+
const base = (st.px * (this.shadowSoftnessM / PENUMBRA_TEXELS)) / AREA_BLOCKS;
|
|
460
|
+
const top = Math.max(0, Math.floor(2 * Math.log2(st.reachM / (base * AREA_REACH_BLOCKS))));
|
|
461
|
+
const halfSpan = Math.max(fp.maxX - fp.minX, fp.maxY - fp.minY) / 2;
|
|
462
|
+
const shown = 1 - AREA_BLEND_LAST;
|
|
463
|
+
st.step = ladderStep(halfSpan, base, top, shown, redraw && st.step >= 0 ? -1 : st.step);
|
|
464
|
+
const blockM = base * 2 ** (st.step / 2);
|
|
465
|
+
// A perspective view that sees more than the map holds keeps the map around the eye.
|
|
466
|
+
if (fp.eye && halfSpan > blockM * AREA_REACH_BLOCKS * shown) {
|
|
467
|
+
fx = dot(frame.right, fp.eye);
|
|
468
|
+
fy = dot(frame.up, fp.eye);
|
|
469
|
+
}
|
|
470
|
+
blocks.push(blockM);
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
if (fp.eye) {
|
|
474
|
+
fx = dot(frame.right, fp.eye);
|
|
475
|
+
fy = dot(frame.up, fp.eye);
|
|
476
|
+
}
|
|
477
|
+
for (let i = 0; i < n; i++)
|
|
478
|
+
blocks.push((st.firstReachM * (st.reachM / st.firstReachM) ** (i / (n - 1))) / AREA_REACH_BLOCKS);
|
|
479
|
+
}
|
|
480
|
+
const draws = [];
|
|
481
|
+
for (let i = 0; i < n; i++) {
|
|
482
|
+
const rects = followFocus(st.areas[i], fx + ox, fy + oy, blocks[i], redraw, n === 1);
|
|
483
|
+
if (rects.length > 0)
|
|
484
|
+
draws.push({ area: i, rects });
|
|
485
|
+
}
|
|
486
|
+
// The areas' part of the Frame block, in the member order of `shadowAreaMembers`.
|
|
487
|
+
const cz = (zMin + zMax) / 2 - oz;
|
|
488
|
+
const halfD = (zMax - zMin) / 2;
|
|
489
|
+
const t = st.tail;
|
|
490
|
+
const mapPx = this.shadowMapSize;
|
|
491
|
+
for (let i = 0; i < n; i++) {
|
|
492
|
+
const a = st.areas[i];
|
|
493
|
+
const size = a.blockM * AREA_BLOCKS;
|
|
494
|
+
const m = orthoMatrix(frame, a.bx * a.blockM + size / 2 - ox, a.by * a.blockM + size / 2 - oy, cz, size / 2, size / 2, halfD);
|
|
495
|
+
if (i === 0)
|
|
496
|
+
this.lightSample = m.slice();
|
|
497
|
+
// From clip space (−1…1) to 0…1 on all three axes.
|
|
498
|
+
for (let c = 0; c < 16; c += 4)
|
|
499
|
+
for (let r = 0; r < 3; r++)
|
|
500
|
+
m[c + r] = m[c + r] * 0.5;
|
|
501
|
+
m[12] = m[12] + 0.5;
|
|
502
|
+
m[13] = m[13] + 0.5;
|
|
503
|
+
m[14] = m[14] + 0.5;
|
|
504
|
+
t.set(m, i * 16);
|
|
505
|
+
const texel = size / st.px;
|
|
506
|
+
const soft = Math.min(Math.max(0.6, (this.shadowSoftnessM / texel - 1) / 3), 1.3);
|
|
507
|
+
const partX = n === 1 ? 0 : (i % 2) * st.px;
|
|
508
|
+
const partY = n === 1 ? 0 : Math.floor(i / 2) * st.px;
|
|
509
|
+
t.set([(partX + a.startX * st.blockPx) / mapPx, (partY + a.startY * st.blockPx) / mapPx, st.px / mapPx, soft / mapPx], n * 16 + i * 4);
|
|
510
|
+
const reach = a.blockM * AREA_REACH_BLOCKS;
|
|
511
|
+
const blend = (i === n - 1 ? AREA_BLEND_LAST : AREA_BLEND) * reach;
|
|
512
|
+
t.set([reach, 1 / blend, texel, 0], n * 20 + i * 4);
|
|
513
|
+
if (i === 0)
|
|
514
|
+
this.shadowTexelM = texel;
|
|
515
|
+
}
|
|
516
|
+
const o = n * 24;
|
|
517
|
+
t.set([fx, fy, 0, 0], o);
|
|
518
|
+
t.set([frame.right[0], frame.right[1], frame.right[2], 0], o + 4);
|
|
519
|
+
t.set([frame.up[0], frame.up[1], frame.up[2], 0], o + 8);
|
|
520
|
+
const h = this.shadowHorizon;
|
|
521
|
+
if (h) {
|
|
522
|
+
t.set([(wo[0] - h.originX) / h.sizeX, (wo[2] - h.originZ) / h.sizeZ, 1 / h.sizeX, 1 / h.sizeZ], o + 12);
|
|
523
|
+
t.set([h.baseY - wo[1], h.rangeM, 1 / Math.max(h.blendM ?? 2, 1e-3), 1], o + 16);
|
|
524
|
+
}
|
|
525
|
+
else
|
|
526
|
+
t.fill(0, o + 12, o + 20);
|
|
527
|
+
this.shadowPx = mapPx;
|
|
528
|
+
const viewKey = view.key();
|
|
529
|
+
return { frame, ox, oy, cz, halfD, draws, viewStale: redraw || draws.length > 0 || viewKey !== st.viewKey, key, viewKey };
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Shadow areas, second half of a frame: draws the planned rectangles into the map (each with the casters the
|
|
533
|
+
* light's volume over that rectangle selects), then lets the layers select for the view again.
|
|
534
|
+
* Returns the number of rectangles drawn.
|
|
535
|
+
*/
|
|
536
|
+
drawAreas(st, plan, view, stats) {
|
|
537
|
+
const device = this.device;
|
|
538
|
+
const n = st.areas.length;
|
|
539
|
+
let drawn = 0;
|
|
540
|
+
if (plan.draws.length > 0)
|
|
541
|
+
device.target(this.shadowTarget);
|
|
542
|
+
for (const d of plan.draws) {
|
|
543
|
+
const a = st.areas[d.area];
|
|
544
|
+
const partX = n === 1 ? 0 : (d.area % 2) * st.px;
|
|
545
|
+
const partY = n === 1 ? 0 : Math.floor(d.area / 2) * st.px;
|
|
546
|
+
for (const r of d.rects) {
|
|
547
|
+
const halfW = ((r.x1 - r.x0) * a.blockM) / 2;
|
|
548
|
+
const halfH = ((r.y1 - r.y0) * a.blockM) / 2;
|
|
549
|
+
const cx = (a.bx + r.x0) * a.blockM + halfW - plan.ox;
|
|
550
|
+
const cy = (a.by + r.y0) * a.blockM + halfH - plan.oy;
|
|
551
|
+
this.lightRender = orthoMatrix(plan.frame, cx, cy, plan.cz, halfW, halfH, plan.halfD);
|
|
552
|
+
const volume = { kind: 'box', axes: plan.frame, cx, cy, halfW, halfH };
|
|
553
|
+
for (const layer of this.layers)
|
|
554
|
+
layer.cull?.(volume, 'shadow', view);
|
|
555
|
+
const x = partX + r.tx * st.blockPx;
|
|
556
|
+
const y = partY + r.ty * st.blockPx;
|
|
557
|
+
const w = (r.x1 - r.x0) * st.blockPx;
|
|
558
|
+
const hgt = (r.y1 - r.y0) * st.blockPx;
|
|
559
|
+
device.clearDepthRect(x, y, w, hgt);
|
|
560
|
+
device.viewport(x, y, w, hgt);
|
|
561
|
+
this.drawPass('shadow', view, stats);
|
|
562
|
+
drawn++;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
this.shadowKey = plan.key;
|
|
566
|
+
if (plan.viewStale) {
|
|
567
|
+
const viewVolume = view.cullVolume();
|
|
568
|
+
let visible = 0;
|
|
569
|
+
for (const layer of this.layers)
|
|
570
|
+
visible += layer.cull?.(viewVolume, 'view', view) ?? 0;
|
|
571
|
+
this.instancesVisible = visible;
|
|
572
|
+
st.viewKey = plan.viewKey;
|
|
573
|
+
}
|
|
574
|
+
return drawn;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Draws one pass of every layer. `casters` (shadow pass only) limits it to the layers with that `shadowCasters`
|
|
578
|
+
* (unset counts as `'static'`); `'all'` draws every layer.
|
|
579
|
+
*/
|
|
580
|
+
drawPass(pass, view, stats, casters = 'all') {
|
|
581
|
+
const device = this.device;
|
|
582
|
+
const base = pass === 'shadow' ? SHADOW_STATE : BASE_STATE;
|
|
583
|
+
const ctx = this.ctx;
|
|
584
|
+
ctx.pass = pass;
|
|
585
|
+
ctx.view = view;
|
|
586
|
+
ctx.lightRender = this.lightRender;
|
|
587
|
+
this.counting = stats;
|
|
588
|
+
const probe = this.probe;
|
|
589
|
+
for (const layer of this.layers) {
|
|
590
|
+
if (casters !== 'all' && (layer.shadowCasters === 'dynamic') !== (casters === 'dynamic'))
|
|
591
|
+
continue;
|
|
592
|
+
if (probe) {
|
|
593
|
+
this.drawProbed(probe, layer, ctx, base, stats);
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
device.setState(base);
|
|
597
|
+
layer.draw(ctx);
|
|
598
|
+
}
|
|
599
|
+
device.setState(base);
|
|
600
|
+
}
|
|
601
|
+
/** `drawPass` for one layer while a probe is set: may leave the layer out, reports what it counted. */
|
|
602
|
+
drawProbed(probe, layer, ctx, base, stats) {
|
|
603
|
+
if (probe.draws && !probe.draws(layer, ctx.pass))
|
|
604
|
+
return;
|
|
605
|
+
const tris = stats.tris;
|
|
606
|
+
const calls = stats.calls;
|
|
607
|
+
this.device.setState(base);
|
|
608
|
+
layer.draw(ctx);
|
|
609
|
+
probe.drawn?.(layer, ctx.pass, stats.tris - tris, stats.calls - calls);
|
|
610
|
+
}
|
|
611
|
+
/** Renders one frame. Called synchronously, independent of the browser's timer. */
|
|
612
|
+
render(view) {
|
|
613
|
+
const device = this.device;
|
|
614
|
+
// Without a graphics context there is nothing to draw into; the frame after its restore draws everything again.
|
|
615
|
+
if (device.lost)
|
|
616
|
+
return this.lastStats;
|
|
617
|
+
const t0 = performance.now();
|
|
618
|
+
for (const hook of this.beforeFrame)
|
|
619
|
+
hook(view);
|
|
620
|
+
const bounds = this.bounds;
|
|
621
|
+
const bg = this.background;
|
|
622
|
+
// Code outside the device (texture bakes) may have changed the context state since the last frame.
|
|
623
|
+
device.setState(BASE_STATE, true);
|
|
624
|
+
if (!bounds) {
|
|
625
|
+
device.target(null);
|
|
626
|
+
device.viewport(0, 0, view.width, view.height);
|
|
627
|
+
device.clear(bg, true);
|
|
628
|
+
device.present();
|
|
629
|
+
return this.lastStats;
|
|
630
|
+
}
|
|
631
|
+
const toLight = this.lightDirection;
|
|
632
|
+
const mapSize = this.shadowMapSize;
|
|
633
|
+
const sceneCenter = this.sceneCenter;
|
|
634
|
+
sceneCenter[0] = (bounds.min[0] + bounds.max[0]) / 2;
|
|
635
|
+
sceneCenter[1] = (bounds.min[1] + bounds.max[1]) / 2;
|
|
636
|
+
sceneCenter[2] = (bounds.min[2] + bounds.max[2]) / 2;
|
|
637
|
+
const radius = Math.hypot(bounds.max[0] - bounds.min[0], bounds.max[1] - bounds.min[1], bounds.max[2] - bounds.min[2]) / 2;
|
|
638
|
+
// Standard depth: the view's own matrix, untouched. Reversed and logarithmic depth: see `depthViewProj`.
|
|
639
|
+
const proj = depthViewProj(view, this.depthMode, sceneCenter, radius + 10, this.depthNearM, this.proj);
|
|
640
|
+
const viewProj = proj.viewProj;
|
|
641
|
+
// Layers whose casters move: asked once per frame whether their shadow changed (and whether their culled sets
|
|
642
|
+
// are out of date). Without such layers nothing here is asked and the frame is exactly as before.
|
|
643
|
+
let hasDynamic = false;
|
|
644
|
+
let dynamicDirty = false;
|
|
645
|
+
for (const layer of this.layers) {
|
|
646
|
+
if (layer.shadowCasters !== 'dynamic')
|
|
647
|
+
continue;
|
|
648
|
+
hasDynamic = true;
|
|
649
|
+
if (layer.shadowDirty?.())
|
|
650
|
+
dynamicDirty = true;
|
|
651
|
+
if (layer.cullStale?.())
|
|
652
|
+
dynamicDirty = true;
|
|
653
|
+
}
|
|
654
|
+
// The shadow modes with areas plan their own updates; the single fitted map below is then skipped. They have no
|
|
655
|
+
// static map of their own: a dynamic layer's change redraws all of it, as `invalidateShadows` would.
|
|
656
|
+
const areaState = this.areaState;
|
|
657
|
+
if (areaState && dynamicDirty)
|
|
658
|
+
this.shadowKey = '';
|
|
659
|
+
const plan = areaState ? this.planAreas(areaState, view, bounds, toLight) : null;
|
|
660
|
+
// Shadows depend on the view, the light direction and whatever the owner adds. Animation time is not in the key.
|
|
661
|
+
let shadowStale = !plan && !this.singleKeyHolds(view, toLight);
|
|
662
|
+
if (!plan && !shadowStale)
|
|
663
|
+
for (const layer of this.layers)
|
|
664
|
+
if (layer.shadowCasters !== 'dynamic' && layer.cullStale?.())
|
|
665
|
+
shadowStale = true;
|
|
666
|
+
// The first dynamic layer (the kept static map is still empty) and the last one gone (the sampled map still
|
|
667
|
+
// holds its casters) both need a whole new map.
|
|
668
|
+
if (!plan && hasDynamic !== this.hadDynamic)
|
|
669
|
+
shadowStale = true;
|
|
670
|
+
this.hadDynamic = hasDynamic;
|
|
671
|
+
if (shadowStale) {
|
|
672
|
+
const fit = this.fitLight(view, bounds, toLight);
|
|
673
|
+
this.lightRender = orthoMatrix(fit.frame, fit.cx, fit.cy, fit.cz, fit.half, fit.half, fit.halfD, this.singleRender);
|
|
674
|
+
// Reading matrix: the same projection, just squeezed into the used part of the texture.
|
|
675
|
+
const k = this.shadowPx / mapSize;
|
|
676
|
+
const s = this.singleSample;
|
|
677
|
+
s.set(this.lightRender);
|
|
678
|
+
for (let col = 0; col < 16; col += 4) {
|
|
679
|
+
s[col] = s[col] * k;
|
|
680
|
+
s[col + 1] = s[col + 1] * k;
|
|
681
|
+
}
|
|
682
|
+
s[12] = s[12] + (k - 1);
|
|
683
|
+
s[13] = s[13] + (k - 1);
|
|
684
|
+
this.lightSample = s;
|
|
685
|
+
this.lightFit = fit;
|
|
686
|
+
this.lightVolume = { kind: 'box', axes: fit.frame, cx: fit.cx, cy: fit.cy, halfW: fit.half, halfH: fit.half };
|
|
687
|
+
}
|
|
688
|
+
// Frame block, in the member order of `shaderHeader`.
|
|
689
|
+
const f = this.frameData;
|
|
690
|
+
const cam = view.axes(this.camAxes);
|
|
691
|
+
f.set(viewProj, 0);
|
|
692
|
+
f.set(this.lightSample, 16);
|
|
693
|
+
f[32] = toLight[0];
|
|
694
|
+
f[33] = toLight[1];
|
|
695
|
+
f[34] = toLight[2];
|
|
696
|
+
f[35] = this.shadowPx / mapSize;
|
|
697
|
+
f.set(this.frameSlots.afterLight, 36);
|
|
698
|
+
let o = 36 + this.frameSlots.afterLight.length;
|
|
699
|
+
const softTexels = Math.max(0.6, (this.shadowSoftnessM / this.shadowTexelM - 1) / 3);
|
|
700
|
+
putVec4(f, o, view.metresPerPixel, this.shadowTexelM, Math.min(softTexels, 1.3), 1 / mapSize);
|
|
701
|
+
putVec4(f, o + 4, cam.right[0], cam.right[1], cam.right[2], view.pixelGrowth);
|
|
702
|
+
putVec4(f, o + 8, cam.up[0], cam.up[1], cam.up[2], proj.logScale);
|
|
703
|
+
putVec4(f, o + 12, cam.forward[0], cam.forward[1], cam.forward[2], view.refDepth);
|
|
704
|
+
o += 16;
|
|
705
|
+
f.set(this.frameSlots.afterView, o);
|
|
706
|
+
if (areaState)
|
|
707
|
+
f.set(areaState.tail, o + this.frameSlots.afterView.length);
|
|
708
|
+
// Local up: the last member, the centre in render space (a difference of doubles) and the base radius.
|
|
709
|
+
const ru = this.radialUp;
|
|
710
|
+
if (ru) {
|
|
711
|
+
const wo = this.worldOrigin;
|
|
712
|
+
putVec4(f, f.length - LOCAL_UP_FLOATS, ru.center[0] - wo[0], ru.center[1] - wo[1], ru.center[2] - wo[2], ru.radiusM);
|
|
713
|
+
}
|
|
714
|
+
this.frameBlock.update(f);
|
|
715
|
+
// Layers draw their own targets now, with the block of this frame and before any pass has begun.
|
|
716
|
+
const probe = this.probe;
|
|
717
|
+
for (const layer of this.layers)
|
|
718
|
+
if (layer.prepare && !(probe?.draws && !probe.draws(layer, 'prepare')))
|
|
719
|
+
layer.prepare(device, view);
|
|
720
|
+
// A layer that filled a target changed the draw state; the clears below need the depth mask of the base state.
|
|
721
|
+
device.setState(BASE_STATE);
|
|
722
|
+
const shadow = this.countShadow;
|
|
723
|
+
const dynamic = this.countDynamic;
|
|
724
|
+
shadow.tris = shadow.calls = 0;
|
|
725
|
+
dynamic.tris = dynamic.calls = 0;
|
|
726
|
+
let shadowUpdates = 0;
|
|
727
|
+
const lightVolume = this.lightVolume;
|
|
728
|
+
if (!plan && this.lightFit && lightVolume && (shadowStale || (hasDynamic && dynamicDirty))) {
|
|
729
|
+
// A refit culls and draws everything; a frame where only dynamic casters changed touches only those layers.
|
|
730
|
+
const all = shadowStale;
|
|
731
|
+
// Whatever casts shadows into the image may stand outside it, so the light's volume selects for the shadow pass.
|
|
732
|
+
for (const layer of this.layers)
|
|
733
|
+
if (all || layer.shadowCasters === 'dynamic')
|
|
734
|
+
layer.cull?.(lightVolume, 'shadow', view);
|
|
735
|
+
if (!hasDynamic) {
|
|
736
|
+
device.target(this.shadowTarget);
|
|
737
|
+
device.viewport(0, 0, mapSize, mapSize);
|
|
738
|
+
device.clear(null, true);
|
|
739
|
+
device.viewport(0, 0, this.shadowPx, this.shadowPx);
|
|
740
|
+
this.drawPass('shadow', view, shadow);
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
const kept = (this.staticShadow ??= device.depthTarget(mapSize));
|
|
744
|
+
if (all) {
|
|
745
|
+
// The static casters alone, into the map that is kept.
|
|
746
|
+
device.target(kept);
|
|
747
|
+
device.viewport(0, 0, mapSize, mapSize);
|
|
748
|
+
device.clear(null, true);
|
|
749
|
+
device.viewport(0, 0, this.shadowPx, this.shadowPx);
|
|
750
|
+
this.drawPass('shadow', view, shadow, 'static');
|
|
751
|
+
}
|
|
752
|
+
// The sampled map: the kept static depth, then the dynamic casters over it.
|
|
753
|
+
device.target(this.shadowTarget);
|
|
754
|
+
if (all) {
|
|
755
|
+
device.viewport(0, 0, mapSize, mapSize);
|
|
756
|
+
device.clear(null, true);
|
|
757
|
+
}
|
|
758
|
+
device.copyDepth(kept, this.shadowTarget, this.shadowPx);
|
|
759
|
+
device.viewport(0, 0, this.shadowPx, this.shadowPx);
|
|
760
|
+
this.drawPass('shadow', view, dynamic, 'dynamic');
|
|
761
|
+
shadow.tris += dynamic.tris;
|
|
762
|
+
shadow.calls += dynamic.calls;
|
|
763
|
+
}
|
|
764
|
+
if (all)
|
|
765
|
+
this.keepSingleKey(view, toLight);
|
|
766
|
+
// For the passes that follow only what is actually in the image.
|
|
767
|
+
const key = view.key();
|
|
768
|
+
if (all || !this.viewVolume || key !== this.viewVolumeKey) {
|
|
769
|
+
this.viewVolume = view.cullVolume();
|
|
770
|
+
this.viewVolumeKey = key;
|
|
771
|
+
}
|
|
772
|
+
const viewVolume = this.viewVolume;
|
|
773
|
+
let staticVisible = 0;
|
|
774
|
+
let dynamicVisible = 0;
|
|
775
|
+
for (const layer of this.layers) {
|
|
776
|
+
if (layer.shadowCasters === 'dynamic')
|
|
777
|
+
dynamicVisible += layer.cull?.(viewVolume, 'view', view) ?? 0;
|
|
778
|
+
else if (all)
|
|
779
|
+
staticVisible += layer.cull?.(viewVolume, 'view', view) ?? 0;
|
|
780
|
+
}
|
|
781
|
+
if (all)
|
|
782
|
+
this.staticVisible = staticVisible;
|
|
783
|
+
this.instancesVisible = this.staticVisible + dynamicVisible;
|
|
784
|
+
shadowUpdates = 1;
|
|
785
|
+
}
|
|
786
|
+
if (areaState && plan)
|
|
787
|
+
shadowUpdates = this.drawAreas(areaState, plan, view, shadow);
|
|
788
|
+
// Main passes.
|
|
789
|
+
const main = this.countMain;
|
|
790
|
+
main.tris = main.calls = 0;
|
|
791
|
+
device.target(null);
|
|
792
|
+
device.viewport(0, 0, view.width, view.height);
|
|
793
|
+
device.clear(bg, true);
|
|
794
|
+
if (areaState)
|
|
795
|
+
device.texture(SHADOW_HORIZON_UNIT, this.shadowHorizon?.texture ?? areaState.noHorizon);
|
|
796
|
+
device.texture(SHADOW_MAP_UNIT, this.shadowTarget.texture);
|
|
797
|
+
if (this.mode === 'data')
|
|
798
|
+
this.drawPass('data', view, main);
|
|
799
|
+
else {
|
|
800
|
+
this.drawPass('color', view, main);
|
|
801
|
+
this.drawPass('overlay', view, main);
|
|
802
|
+
}
|
|
803
|
+
device.unbindMesh();
|
|
804
|
+
// Reversed and logarithmic depth draw into the device's own target; this copies it to the drawing surface.
|
|
805
|
+
device.present();
|
|
806
|
+
this.lastStats = {
|
|
807
|
+
triangles: main.tris,
|
|
808
|
+
drawCalls: main.calls,
|
|
809
|
+
shadowTriangles: shadow.tris,
|
|
810
|
+
shadowDrawCalls: shadow.calls,
|
|
811
|
+
dynamicShadowTriangles: dynamic.tris,
|
|
812
|
+
dynamicShadowDrawCalls: dynamic.calls,
|
|
813
|
+
cpuMs: performance.now() - t0,
|
|
814
|
+
shadowMapPx: this.shadowPx,
|
|
815
|
+
instancesVisible: this.instancesVisible,
|
|
816
|
+
shadowUpdates,
|
|
817
|
+
};
|
|
818
|
+
return this.lastStats;
|
|
819
|
+
}
|
|
820
|
+
/** Mode `'single'`: is the kept map still fitted to this view, light direction and `shadowKeyExtra`? */
|
|
821
|
+
singleKeyHolds(view, toLight) {
|
|
822
|
+
if (!this.singleKeyValid)
|
|
823
|
+
return false;
|
|
824
|
+
const l = this.singleKeyLight;
|
|
825
|
+
// The light direction counts in steps of 1e-4, as it always did.
|
|
826
|
+
if (Math.round(toLight[0] * 1e4) !== l[0] || Math.round(toLight[1] * 1e4) !== l[1] || Math.round(toLight[2] * 1e4) !== l[2])
|
|
827
|
+
return false;
|
|
828
|
+
return this.shadowKeyExtra === this.singleKeyExtra && view.key() === this.singleKeyView;
|
|
829
|
+
}
|
|
830
|
+
keepSingleKey(view, toLight) {
|
|
831
|
+
this.singleKeyValid = true;
|
|
832
|
+
this.singleKeyView = view.key();
|
|
833
|
+
this.singleKeyLight[0] = Math.round(toLight[0] * 1e4);
|
|
834
|
+
this.singleKeyLight[1] = Math.round(toLight[1] * 1e4);
|
|
835
|
+
this.singleKeyLight[2] = Math.round(toLight[2] * 1e4);
|
|
836
|
+
this.singleKeyExtra = this.shadowKeyExtra;
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* Renders n frames and waits for the graphics. Returns the average time of one frame in ms including the graphics work.
|
|
840
|
+
* `beforeFrame` is called before each frame, typically advances the clocks by a fixed step so that animation is measured.
|
|
841
|
+
*/
|
|
842
|
+
benchmark(view, n, beforeFrame) {
|
|
843
|
+
this.render(view);
|
|
844
|
+
this.device.finish();
|
|
845
|
+
const t0 = performance.now();
|
|
846
|
+
for (let i = 0; i < n; i++) {
|
|
847
|
+
beforeFrame?.(i);
|
|
848
|
+
this.render(view);
|
|
849
|
+
this.device.flush();
|
|
850
|
+
}
|
|
851
|
+
this.device.finish();
|
|
852
|
+
return (performance.now() - t0) / n;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
/** Sets the shared uniforms and binds samplers for one draw. */
|
|
856
|
+
export function applyInputs(ctx, program, uniforms, textures) {
|
|
857
|
+
if (ctx.pass === 'shadow')
|
|
858
|
+
program.mat4('u_lightRender', ctx.lightRender);
|
|
859
|
+
else
|
|
860
|
+
program.int('u_shadowMap', SHADOW_MAP_UNIT);
|
|
861
|
+
let unit = SHADOW_MAP_UNIT + 1;
|
|
862
|
+
for (const name in textures) {
|
|
863
|
+
ctx.device.texture(unit, textures[name]);
|
|
864
|
+
program.int(name, unit++);
|
|
865
|
+
}
|
|
866
|
+
for (const name in uniforms)
|
|
867
|
+
program.set(name, uniforms[name]);
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* One mesh drawn with one material. In the shadow pass the program gets `u_lightRender`, in the others `u_shadowMap`.
|
|
871
|
+
*/
|
|
872
|
+
export class MeshLayer {
|
|
873
|
+
name;
|
|
874
|
+
material;
|
|
875
|
+
mesh;
|
|
876
|
+
passes;
|
|
877
|
+
uniforms;
|
|
878
|
+
textures;
|
|
879
|
+
state;
|
|
880
|
+
bind;
|
|
881
|
+
constructor(o) {
|
|
882
|
+
this.material = o.material;
|
|
883
|
+
this.mesh = o.mesh ?? null;
|
|
884
|
+
this.passes = new Set(o.passes ?? o.material.passes);
|
|
885
|
+
this.state = { ...o.material.state, ...o.state };
|
|
886
|
+
this.uniforms = o.uniforms ?? {};
|
|
887
|
+
this.textures = o.textures ?? {};
|
|
888
|
+
this.bind = o.bind;
|
|
889
|
+
if (o.name !== undefined)
|
|
890
|
+
this.name = o.name;
|
|
891
|
+
}
|
|
892
|
+
draw(ctx) {
|
|
893
|
+
const mesh = this.mesh;
|
|
894
|
+
if (!mesh)
|
|
895
|
+
return;
|
|
896
|
+
if (!this.begin(ctx))
|
|
897
|
+
return;
|
|
898
|
+
ctx.device.draw(mesh);
|
|
899
|
+
ctx.count(mesh.triangles);
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* Sets this layer's draw state, program, uniforms and textures for `ctx.pass`. Returns the program, or null when the
|
|
903
|
+
* layer does not draw in this pass. Subclasses that draw several meshes call it once and then draw each mesh.
|
|
904
|
+
*/
|
|
905
|
+
begin(ctx) {
|
|
906
|
+
if (!this.passes.has(ctx.pass))
|
|
907
|
+
return null;
|
|
908
|
+
const program = this.material.program(ctx.pass);
|
|
909
|
+
if (!program)
|
|
910
|
+
return null;
|
|
911
|
+
const state = this.state[ctx.pass];
|
|
912
|
+
if (state)
|
|
913
|
+
ctx.device.setState(state);
|
|
914
|
+
ctx.device.use(program);
|
|
915
|
+
applyInputs(ctx, program, this.uniforms, this.textures);
|
|
916
|
+
this.bind?.(program, ctx);
|
|
917
|
+
return program;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* Instanced batches with per-volume culling. An instance is an upright segment from its base point up by its height,
|
|
922
|
+
* widened by its radius; whatever reaches into the volume is kept.
|
|
923
|
+
*/
|
|
924
|
+
export class InstancedLayer {
|
|
925
|
+
name;
|
|
926
|
+
material;
|
|
927
|
+
batches = [];
|
|
928
|
+
passes;
|
|
929
|
+
uniforms;
|
|
930
|
+
/** Extra draw state per pass, on top of the material's state. May be replaced at any time. */
|
|
931
|
+
state;
|
|
932
|
+
device;
|
|
933
|
+
bounds;
|
|
934
|
+
levelOfDetail;
|
|
935
|
+
upCenter;
|
|
936
|
+
scratch = new Float32Array(0);
|
|
937
|
+
lodNow = 0;
|
|
938
|
+
constructor(device, o) {
|
|
939
|
+
this.device = device;
|
|
940
|
+
this.material = o.material;
|
|
941
|
+
this.bounds = o.bounds;
|
|
942
|
+
this.levelOfDetail = o.levelOfDetail ?? (() => 0);
|
|
943
|
+
this.upCenter = o.upCenter ?? null;
|
|
944
|
+
this.passes = new Set(o.passes ?? o.material.passes);
|
|
945
|
+
this.uniforms = o.uniforms ?? {};
|
|
946
|
+
this.state = {};
|
|
947
|
+
if (o.name !== undefined)
|
|
948
|
+
this.name = o.name;
|
|
949
|
+
}
|
|
950
|
+
cull(volume, purpose) {
|
|
951
|
+
if (purpose === 'view')
|
|
952
|
+
this.lodNow = this.levelOfDetail();
|
|
953
|
+
let total = 0;
|
|
954
|
+
for (const batch of this.batches) {
|
|
955
|
+
const stride = batch.instances.layout.stride / 4;
|
|
956
|
+
if (this.scratch.length < batch.all.length)
|
|
957
|
+
this.scratch = new Float32Array(batch.all.length);
|
|
958
|
+
const n = cullInstances(volume, batch.all, stride, this.bounds, this.scratch, this.upCenter ? this.upCenter() : null);
|
|
959
|
+
this.device.writeBuffer(batch.instances.buffer, this.scratch, n);
|
|
960
|
+
const count = n / stride;
|
|
961
|
+
for (const m of batch.lods)
|
|
962
|
+
m.instances = count;
|
|
963
|
+
total += count;
|
|
964
|
+
}
|
|
965
|
+
return total;
|
|
966
|
+
}
|
|
967
|
+
draw(ctx) {
|
|
968
|
+
if (!this.passes.has(ctx.pass))
|
|
969
|
+
return;
|
|
970
|
+
const program = this.material.program(ctx.pass);
|
|
971
|
+
if (!program)
|
|
972
|
+
return;
|
|
973
|
+
// The material's state, then the layer's own on top: the same fields end up set as with one merged object.
|
|
974
|
+
const ms = this.material.state[ctx.pass];
|
|
975
|
+
const ls = this.state[ctx.pass];
|
|
976
|
+
if (ms)
|
|
977
|
+
ctx.device.setState(ms);
|
|
978
|
+
if (ls)
|
|
979
|
+
ctx.device.setState(ls);
|
|
980
|
+
ctx.device.use(program);
|
|
981
|
+
applyInputs(ctx, program, this.uniforms, {});
|
|
982
|
+
const lod = ctx.pass === 'shadow' ? 0 : this.lodNow;
|
|
983
|
+
for (const batch of this.batches) {
|
|
984
|
+
const m = batch.lods[Math.min(lod, batch.lods.length - 1)];
|
|
985
|
+
if (m.instances === 0)
|
|
986
|
+
continue;
|
|
987
|
+
for (const name in batch.uniforms)
|
|
988
|
+
program.set(name, batch.uniforms[name]);
|
|
989
|
+
ctx.device.draw(m);
|
|
990
|
+
ctx.count(m.triangles * m.instances);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
/** Writes four floats at `o`. */
|
|
995
|
+
function putVec4(f, o, x, y, z, w) {
|
|
996
|
+
f[o] = x;
|
|
997
|
+
f[o + 1] = y;
|
|
998
|
+
f[o + 2] = z;
|
|
999
|
+
f[o + 3] = w;
|
|
1000
|
+
}
|
|
1001
|
+
/** Distance from a point to the farthest corner of the scene bounds. */
|
|
1002
|
+
function farthestCorner(p, b) {
|
|
1003
|
+
const dx = Math.max(Math.abs(p[0] - b.min[0]), Math.abs(p[0] - b.max[0]));
|
|
1004
|
+
const dy = Math.max(Math.abs(p[1] - b.min[1]), Math.abs(p[1] - b.max[1]));
|
|
1005
|
+
const dz = Math.max(Math.abs(p[2] - b.min[2]), Math.abs(p[2] - b.max[2]));
|
|
1006
|
+
return Math.hypot(dx, dy, dz);
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* Copies the instances that reach into the volume into `out`; returns the number of floats written. With `upCenter`
|
|
1010
|
+
* (a point in the space of the instance positions) every instance stands away from that point instead of along +y.
|
|
1011
|
+
*/
|
|
1012
|
+
export function cullInstances(volume, src, stride, b, out, upCenter = null) {
|
|
1013
|
+
if (upCenter)
|
|
1014
|
+
return cullInstancesRadial(volume, src, stride, b, out, upCenter);
|
|
1015
|
+
if (volume.kind === 'frustum')
|
|
1016
|
+
return cullInstancesFrustum(volume.planes, src, stride, b, out);
|
|
1017
|
+
const { right, up } = volume.axes;
|
|
1018
|
+
const { cx, cy, halfW, halfH } = volume;
|
|
1019
|
+
let n = 0;
|
|
1020
|
+
for (let i = 0; i < src.length; i += stride) {
|
|
1021
|
+
const x = src[i + b.position];
|
|
1022
|
+
const y = src[i + b.position + 1];
|
|
1023
|
+
const z = src[i + b.position + 2];
|
|
1024
|
+
const r = src[i + b.radius];
|
|
1025
|
+
const h = src[i + b.height];
|
|
1026
|
+
const lx = right[0] * x + right[1] * y + right[2] * z - cx;
|
|
1027
|
+
if (Math.abs(lx + right[1] * h * 0.5) > halfW + r + Math.abs(right[1]) * h * 0.5)
|
|
1028
|
+
continue;
|
|
1029
|
+
const ly0 = up[0] * x + up[1] * y + up[2] * z - cy;
|
|
1030
|
+
const ly1 = ly0 + up[1] * h;
|
|
1031
|
+
if (Math.max(ly0, ly1) < -halfH - r || Math.min(ly0, ly1) > halfH + r)
|
|
1032
|
+
continue;
|
|
1033
|
+
for (let k = 0; k < stride; k++)
|
|
1034
|
+
out[n + k] = src[i + k];
|
|
1035
|
+
n += stride;
|
|
1036
|
+
}
|
|
1037
|
+
return n;
|
|
1038
|
+
}
|
|
1039
|
+
/** `cullInstances` for instances that stand away from a point: the segment runs from the base along the local up. */
|
|
1040
|
+
function cullInstancesRadial(volume, src, stride, b, out, c) {
|
|
1041
|
+
let n = 0;
|
|
1042
|
+
outer: for (let i = 0; i < src.length; i += stride) {
|
|
1043
|
+
const x = src[i + b.position];
|
|
1044
|
+
const y = src[i + b.position + 1];
|
|
1045
|
+
const z = src[i + b.position + 2];
|
|
1046
|
+
const r = src[i + b.radius];
|
|
1047
|
+
const h = src[i + b.height];
|
|
1048
|
+
let ux = x - c[0];
|
|
1049
|
+
let uy = y - c[1];
|
|
1050
|
+
let uz = z - c[2];
|
|
1051
|
+
const l = Math.sqrt(ux * ux + uy * uy + uz * uz);
|
|
1052
|
+
// The segment from the base to the top, as a vector.
|
|
1053
|
+
const k = l > 0 ? h / l : 0;
|
|
1054
|
+
ux *= k;
|
|
1055
|
+
uy = l > 0 ? uy * k : h;
|
|
1056
|
+
uz *= k;
|
|
1057
|
+
if (volume.kind === 'frustum') {
|
|
1058
|
+
for (const [nx, ny, nz, d] of volume.planes) {
|
|
1059
|
+
const s0 = nx * x + ny * y + nz * z + d;
|
|
1060
|
+
if (Math.max(s0, s0 + nx * ux + ny * uy + nz * uz) < -r)
|
|
1061
|
+
continue outer;
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
else {
|
|
1065
|
+
const { right, up } = volume.axes;
|
|
1066
|
+
const lx0 = right[0] * x + right[1] * y + right[2] * z - volume.cx;
|
|
1067
|
+
const lx1 = lx0 + right[0] * ux + right[1] * uy + right[2] * uz;
|
|
1068
|
+
if (Math.max(lx0, lx1) < -volume.halfW - r || Math.min(lx0, lx1) > volume.halfW + r)
|
|
1069
|
+
continue;
|
|
1070
|
+
const ly0 = up[0] * x + up[1] * y + up[2] * z - volume.cy;
|
|
1071
|
+
const ly1 = ly0 + up[0] * ux + up[1] * uy + up[2] * uz;
|
|
1072
|
+
if (Math.max(ly0, ly1) < -volume.halfH - r || Math.min(ly0, ly1) > volume.halfH + r)
|
|
1073
|
+
continue;
|
|
1074
|
+
}
|
|
1075
|
+
for (let q = 0; q < stride; q++)
|
|
1076
|
+
out[n + q] = src[i + q];
|
|
1077
|
+
n += stride;
|
|
1078
|
+
}
|
|
1079
|
+
return n;
|
|
1080
|
+
}
|
|
1081
|
+
/** Frustum variant of `cullInstances`: the upright segment, widened by its radius, must not lie wholly outside a plane. */
|
|
1082
|
+
function cullInstancesFrustum(planes, src, stride, b, out) {
|
|
1083
|
+
let n = 0;
|
|
1084
|
+
outer: for (let i = 0; i < src.length; i += stride) {
|
|
1085
|
+
const x = src[i + b.position];
|
|
1086
|
+
const y = src[i + b.position + 1];
|
|
1087
|
+
const z = src[i + b.position + 2];
|
|
1088
|
+
const r = src[i + b.radius];
|
|
1089
|
+
const h = src[i + b.height];
|
|
1090
|
+
for (const [nx, ny, nz, d] of planes) {
|
|
1091
|
+
const s0 = nx * x + ny * y + nz * z + d;
|
|
1092
|
+
if (Math.max(s0, s0 + ny * h) < -r)
|
|
1093
|
+
continue outer;
|
|
1094
|
+
}
|
|
1095
|
+
for (let k = 0; k < stride; k++)
|
|
1096
|
+
out[n + k] = src[i + k];
|
|
1097
|
+
n += stride;
|
|
1098
|
+
}
|
|
1099
|
+
return n;
|
|
1100
|
+
}
|