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,797 @@
|
|
|
1
|
+
// Falling parts of a structure: turns the parts an event took away into pieces that move on closed-form tracks
|
|
2
|
+
// (core/ballistic.ts) and tells when each part's rubble should grow in. Presentation only: the destruction state is
|
|
3
|
+
// already final when this runs, and nothing here is saved or read back.
|
|
4
|
+
//
|
|
5
|
+
// A piece is the part's own geometry, cut out of the structure drawn part by part in the state it had before the
|
|
6
|
+
// event (`buildBuildingMesh(..., { byParts: true, states })`): its façade with windows, its roof covering, its damage
|
|
7
|
+
// rung, byte for byte. Every triangle goes to the part whose solid holds a point just behind its face. Solid parts
|
|
8
|
+
// (masonry, floors, partitions) whose faces cover their outline also get a closed shell slightly inside it, drawn only
|
|
9
|
+
// once the piece moves, so a piece seen from behind or from a side is a solid with broken edges. Until its time in the event's
|
|
10
|
+
// timeline a piece stands exactly where it stood, so nothing changes when the event happens except the parts that
|
|
11
|
+
// go at once.
|
|
12
|
+
//
|
|
13
|
+
// Blown parts (hit by the event) are pushed out from the event centre and up, spinning. Parts that lost their
|
|
14
|
+
// support let go at their timeline time, drift toward the side their rubble falls to and tilt. A part wider than
|
|
15
|
+
// `SPLIT_M` is cut along a grid into pieces with slightly different tracks. A piece lands on the highest surviving
|
|
16
|
+
// part under its centre or on the ground, crumbles into it and its rubble grows in there.
|
|
17
|
+
//
|
|
18
|
+
// Budget: at most `budget` pieces fly. The rest stand until their time and then go up as dust: hidden pieces (inside
|
|
19
|
+
// the outline, or with nothing drawn) first, then those with the least drawn surface.
|
|
20
|
+
import { DAMAGE, PART_FLAG, entryDir } from '../../base/index.js';
|
|
21
|
+
import { TRACK_FLOATS, trackAttribs, trackFlightTime, trackLifetime, writeTrack } from '../../core/index.js';
|
|
22
|
+
import { hashInts, unit } from '../../gen-core/index.js';
|
|
23
|
+
import { STRUCTURE_MATERIAL } from './damaged.js';
|
|
24
|
+
import { FINISH } from '../interior/interior.js';
|
|
25
|
+
import { ROOM_TYPE } from '../interior/interior-plan.js';
|
|
26
|
+
import { BUILDING_ATTRIBS, BUILDING_LAYOUT, DAMAGE_STEP, KIND, VertexWriter, WIN_UNIT } from '../structure/writer.js';
|
|
27
|
+
const SRC = BUILDING_LAYOUT.stride;
|
|
28
|
+
const STRIDE = SRC + TRACK_FLOATS * 4;
|
|
29
|
+
/** Vertex layout of falling pieces: the structure vertex, then the track. */
|
|
30
|
+
export const FALL_LAYOUT = { stride: STRIDE, attribs: [...BUILDING_LAYOUT.attribs, ...trackAttribs(SRC)] };
|
|
31
|
+
export const FALL_ATTRIBS = { ...BUILDING_ATTRIBS, a_track0: 9, a_track1: 10, a_track2: 11, a_track3: 12 };
|
|
32
|
+
/** Eight fall directions of the damage entries (x, z), as in sim/destruction's octants. */
|
|
33
|
+
const DIRS = [
|
|
34
|
+
[1, 0],
|
|
35
|
+
[0.7071, 0.7071],
|
|
36
|
+
[0, 1],
|
|
37
|
+
[-0.7071, 0.7071],
|
|
38
|
+
[-1, 0],
|
|
39
|
+
[-0.7071, -0.7071],
|
|
40
|
+
[0, -1],
|
|
41
|
+
[0.7071, -0.7071],
|
|
42
|
+
];
|
|
43
|
+
/** Rubble grows in over this long, starting when its piece lands. */
|
|
44
|
+
const GROW_S = 0.7;
|
|
45
|
+
/** Horizontal speed (m/s, times 0.5–1.5) with which a piece that lost its support leans away from the structure. */
|
|
46
|
+
const LEAN_MS = 0.7;
|
|
47
|
+
/** Pieces that lose support let go spread over this long (s), so neighbours do not move as one sheet. */
|
|
48
|
+
const STAGGER_S = 0.14;
|
|
49
|
+
/** A piece counts as inside a standing part from this far below that part's top (m). */
|
|
50
|
+
const BLOCK_M = 0.2;
|
|
51
|
+
/** Shares of the horizontal velocity tried when a piece would land in or fly through a standing part. */
|
|
52
|
+
const CLEAR_SCALES = [1, 0.6, 0.3, -0.4, 0];
|
|
53
|
+
/** Parts wider than this are cut into pieces of about `SPLIT_M` (single surfaces such as roof sections: `SPLIT_THIN_M`). */
|
|
54
|
+
const SPLIT_M = 3.6;
|
|
55
|
+
const SPLIT_THIN_M = 2.6;
|
|
56
|
+
/** How far behind a face the point lies that decides its part (metres), and the farthest a part may be. */
|
|
57
|
+
const PROBE_M = 0.03;
|
|
58
|
+
const REACH_M = 1.0;
|
|
59
|
+
/** The shell of cut faces lies this far inside the outline. */
|
|
60
|
+
const SHELL_INSET_M = 0.02;
|
|
61
|
+
const GRID_M = 2;
|
|
62
|
+
/** Daylight share (0–255) the broad faces of a shell are lit with: they fly in the open. */
|
|
63
|
+
const SHELL_DAYLIGHT = 110;
|
|
64
|
+
export function fallPieces(o) {
|
|
65
|
+
const { desc, state, before } = o;
|
|
66
|
+
if (before.indices)
|
|
67
|
+
throw new Error('fallPieces: indexed meshes are not supported');
|
|
68
|
+
const src = new Uint8Array(before.vertices.buffer, before.vertices.byteOffset, before.count * SRC);
|
|
69
|
+
const f32 = new Float32Array(before.vertices.buffer, before.vertices.byteOffset, (before.count * SRC) / 4);
|
|
70
|
+
const i8 = new Int8Array(before.vertices.buffer, before.vertices.byteOffset, before.count * SRC);
|
|
71
|
+
const falling = new Map();
|
|
72
|
+
for (const c of o.changes)
|
|
73
|
+
falling.set(c.part, c);
|
|
74
|
+
// Solids of every part, in a grid over x, z.
|
|
75
|
+
const solids = desc.parts.map(solidOf);
|
|
76
|
+
const grid = new Map();
|
|
77
|
+
const cell = (x, z) => (Math.floor(x / GRID_M) + 32768) * 65536 + (Math.floor(z / GRID_M) + 32768);
|
|
78
|
+
for (const s of solids) {
|
|
79
|
+
let x0 = Infinity;
|
|
80
|
+
let x1 = -Infinity;
|
|
81
|
+
let z0 = Infinity;
|
|
82
|
+
let z1 = -Infinity;
|
|
83
|
+
for (let k = 0; k < s.ring.length; k += 2) {
|
|
84
|
+
x0 = Math.min(x0, s.ring[k]);
|
|
85
|
+
x1 = Math.max(x1, s.ring[k]);
|
|
86
|
+
z0 = Math.min(z0, s.ring[k + 1]);
|
|
87
|
+
z1 = Math.max(z1, s.ring[k + 1]);
|
|
88
|
+
}
|
|
89
|
+
for (let gx = Math.floor((x0 - REACH_M) / GRID_M); gx <= Math.floor((x1 + REACH_M) / GRID_M); gx++) {
|
|
90
|
+
for (let gz = Math.floor((z0 - REACH_M) / GRID_M); gz <= Math.floor((z1 + REACH_M) / GRID_M); gz++) {
|
|
91
|
+
const key = (gx + 32768) * 65536 + (gz + 32768);
|
|
92
|
+
const l = grid.get(key);
|
|
93
|
+
if (l)
|
|
94
|
+
l.push(s);
|
|
95
|
+
else
|
|
96
|
+
grid.set(key, [s]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Every triangle to the part whose solid holds the point just behind its face.
|
|
101
|
+
const trisOf = new Map();
|
|
102
|
+
const tris = before.count / 3;
|
|
103
|
+
for (let t = 0; t < tris; t++) {
|
|
104
|
+
const v0 = t * 3;
|
|
105
|
+
let px = 0;
|
|
106
|
+
let py = 0;
|
|
107
|
+
let pz = 0;
|
|
108
|
+
for (let k = 0; k < 3; k++) {
|
|
109
|
+
px += f32[((v0 + k) * SRC) / 4] / 3;
|
|
110
|
+
py += f32[((v0 + k) * SRC) / 4 + 1] / 3;
|
|
111
|
+
pz += f32[((v0 + k) * SRC) / 4 + 2] / 3;
|
|
112
|
+
}
|
|
113
|
+
const o2 = (v0 + 2) * SRC + 12;
|
|
114
|
+
let nx = i8[o2];
|
|
115
|
+
let ny = i8[o2 + 1];
|
|
116
|
+
let nz = i8[o2 + 2];
|
|
117
|
+
const nl = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;
|
|
118
|
+
nx /= nl;
|
|
119
|
+
ny /= nl;
|
|
120
|
+
nz /= nl;
|
|
121
|
+
px -= nx * PROBE_M;
|
|
122
|
+
py -= ny * PROBE_M;
|
|
123
|
+
pz -= nz * PROBE_M;
|
|
124
|
+
let best = null;
|
|
125
|
+
let bestD = REACH_M;
|
|
126
|
+
for (const s of grid.get(cell(px, pz)) ?? []) {
|
|
127
|
+
const d = outside(s, px, py, pz);
|
|
128
|
+
if (d < bestD - 1e-6 || (best && d <= bestD + 1e-6 && s.volume < best.volume)) {
|
|
129
|
+
best = s;
|
|
130
|
+
bestD = Math.min(bestD, d);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!best || !falling.has(best.part.key))
|
|
134
|
+
continue;
|
|
135
|
+
const l = trisOf.get(best.part.key);
|
|
136
|
+
if (l)
|
|
137
|
+
l.push(t);
|
|
138
|
+
else
|
|
139
|
+
trisOf.set(best.part.key, [t]);
|
|
140
|
+
}
|
|
141
|
+
// Pieces: one per part, or a grid of them for a wide part.
|
|
142
|
+
const byKey = new Map(solids.map((s) => [s.part.key, s]));
|
|
143
|
+
const pieces = [];
|
|
144
|
+
for (const c of o.changes) {
|
|
145
|
+
const s = byKey.get(c.part);
|
|
146
|
+
if (!s)
|
|
147
|
+
continue;
|
|
148
|
+
const own = trisOf.get(c.part) ?? [];
|
|
149
|
+
const solidMaterial = s.part.material === STRUCTURE_MATERIAL.masonry || s.part.material === STRUCTURE_MATERIAL.floor || s.part.material === STRUCTURE_MATERIAL.partition;
|
|
150
|
+
// Frame along the longest outline edge.
|
|
151
|
+
let ux = 1;
|
|
152
|
+
let uz = 0;
|
|
153
|
+
let longest = 0;
|
|
154
|
+
const n = s.ring.length / 2;
|
|
155
|
+
for (let k = 0; k < n; k++) {
|
|
156
|
+
const j = (k + 1) % n;
|
|
157
|
+
const dx = s.ring[j * 2] - s.ring[k * 2];
|
|
158
|
+
const dz = s.ring[j * 2 + 1] - s.ring[k * 2 + 1];
|
|
159
|
+
const l = Math.sqrt(dx * dx + dz * dz);
|
|
160
|
+
if (l > longest) {
|
|
161
|
+
longest = l;
|
|
162
|
+
ux = dx / l;
|
|
163
|
+
uz = dz / l;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// A solid part gets a shell only where its own faces cover its outline's long side (a wall bay, a slab); a part
|
|
167
|
+
// whose solid is only a bounding box of its surface (a gable triangle) would show the box.
|
|
168
|
+
let shell = solidMaterial && own.length === 0;
|
|
169
|
+
if (solidMaterial && own.length > 0) {
|
|
170
|
+
const h = s.y1 - s.y0;
|
|
171
|
+
let front = 0;
|
|
172
|
+
let back = 0;
|
|
173
|
+
let flat = 0;
|
|
174
|
+
for (const t of own) {
|
|
175
|
+
const [area, nx, ny, nz] = triArea(f32, t);
|
|
176
|
+
const across = -nx * uz + nz * ux;
|
|
177
|
+
if (Math.abs(ny) > 0.7)
|
|
178
|
+
flat += area;
|
|
179
|
+
else if (across > 0.7)
|
|
180
|
+
front += area;
|
|
181
|
+
else if (across < -0.7)
|
|
182
|
+
back += area;
|
|
183
|
+
}
|
|
184
|
+
const side = Math.max(front, back);
|
|
185
|
+
shell = side >= 0.7 * longest * h || flat >= 0.7 * (s.volume / Math.max(h, 0.01));
|
|
186
|
+
}
|
|
187
|
+
const U = (x, z) => x * ux + z * uz;
|
|
188
|
+
const V = (x, z) => -x * uz + z * ux;
|
|
189
|
+
let u0 = Infinity;
|
|
190
|
+
let u1 = -Infinity;
|
|
191
|
+
let w0 = Infinity;
|
|
192
|
+
let w1 = -Infinity;
|
|
193
|
+
for (let k = 0; k < n; k++) {
|
|
194
|
+
const u = U(s.ring[k * 2], s.ring[k * 2 + 1]);
|
|
195
|
+
const v = V(s.ring[k * 2], s.ring[k * 2 + 1]);
|
|
196
|
+
u0 = Math.min(u0, u);
|
|
197
|
+
u1 = Math.max(u1, u);
|
|
198
|
+
w0 = Math.min(w0, v);
|
|
199
|
+
w1 = Math.max(w1, v);
|
|
200
|
+
}
|
|
201
|
+
const split = shell ? SPLIT_M : SPLIT_THIN_M;
|
|
202
|
+
const nu = u1 - u0 > split * 1.15 ? Math.ceil((u1 - u0) / split) : 1;
|
|
203
|
+
const nv = w1 - w0 > split * 1.15 ? Math.ceil((w1 - w0) / split) : 1;
|
|
204
|
+
const ringP = [];
|
|
205
|
+
for (let k = 0; k < n; k++)
|
|
206
|
+
ringP.push([s.ring[k * 2], s.ring[k * 2 + 1]]);
|
|
207
|
+
for (let a = 0; a < nu; a++) {
|
|
208
|
+
for (let b = 0; b < nv; b++) {
|
|
209
|
+
const cu0 = nu === 1 ? -Infinity : u0 + ((u1 - u0) * a) / nu;
|
|
210
|
+
const cu1 = nu === 1 ? Infinity : u0 + ((u1 - u0) * (a + 1)) / nu;
|
|
211
|
+
const cv0 = nv === 1 ? -Infinity : w0 + ((w1 - w0) * b) / nv;
|
|
212
|
+
const cv1 = nv === 1 ? Infinity : w0 + ((w1 - w0) * (b + 1)) / nv;
|
|
213
|
+
const inCell = (p) => [U(p[0], p[1]) - cu0, cu1 - U(p[0], p[1]), V(p[0], p[1]) - cv0, cv1 - V(p[0], p[1])];
|
|
214
|
+
let poly = ringP;
|
|
215
|
+
for (let h = 0; h < 4; h++)
|
|
216
|
+
poly = clipPoly(poly, (p) => inCell(p)[h]);
|
|
217
|
+
if (poly.length < 3)
|
|
218
|
+
continue;
|
|
219
|
+
const piece = { part: s.part, change: c, sub: a * nv + b, poly, cx: 0, cz: 0, thin: 0, size: 0, surface: 0, whole: [], clipped: [], shell, hidden: false };
|
|
220
|
+
for (const p of poly) {
|
|
221
|
+
piece.cx += p[0] / poly.length;
|
|
222
|
+
piece.cz += p[1] / poly.length;
|
|
223
|
+
}
|
|
224
|
+
let pu0 = Infinity;
|
|
225
|
+
let pu1 = -Infinity;
|
|
226
|
+
let pv0 = Infinity;
|
|
227
|
+
let pv1 = -Infinity;
|
|
228
|
+
for (const p of poly) {
|
|
229
|
+
pu0 = Math.min(pu0, U(p[0], p[1]));
|
|
230
|
+
pu1 = Math.max(pu1, U(p[0], p[1]));
|
|
231
|
+
pv0 = Math.min(pv0, V(p[0], p[1]));
|
|
232
|
+
pv1 = Math.max(pv1, V(p[0], p[1]));
|
|
233
|
+
}
|
|
234
|
+
const h = s.y1 - s.y0;
|
|
235
|
+
piece.thin = Math.min(h, Math.min(pu1 - pu0, pv1 - pv0) + 0.1);
|
|
236
|
+
piece.size = (pu1 - pu0) * (pv1 - pv0) * Math.max(h, 0.2);
|
|
237
|
+
if (nu * nv === 1) {
|
|
238
|
+
piece.whole = own;
|
|
239
|
+
for (const t of own)
|
|
240
|
+
piece.surface += triArea(f32, t)[0];
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
for (const t of own) {
|
|
244
|
+
const w = clipTri(t, (p) => inCell(p), f32);
|
|
245
|
+
if (w.length < 3)
|
|
246
|
+
continue;
|
|
247
|
+
piece.clipped.push({ tri: t, w });
|
|
248
|
+
// Share of the triangle kept: area of the clipped polygon in barycentric coordinates (the triangle is 1/2).
|
|
249
|
+
let a = 0;
|
|
250
|
+
for (let k = 0; k < w.length; k++) {
|
|
251
|
+
const p = w[k];
|
|
252
|
+
const q = w[(k + 1) % w.length];
|
|
253
|
+
a += p[0] * q[1] - q[0] * p[1];
|
|
254
|
+
}
|
|
255
|
+
piece.surface += triArea(f32, t)[0] * Math.min(1, Math.abs(a));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (piece.whole.length === 0 && piece.clipped.length === 0 && !shell)
|
|
259
|
+
continue;
|
|
260
|
+
piece.hidden = (s.part.flags & PART_FLAG.inner) !== 0 || (piece.whole.length === 0 && piece.clipped.length === 0);
|
|
261
|
+
pieces.push(piece);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// Budget: hidden and small pieces go to dust first.
|
|
266
|
+
const order = pieces.map((_, i) => i).sort((a, b) => score(pieces[b]) - score(pieces[a]) || a - b);
|
|
267
|
+
const flies = new Uint8Array(pieces.length);
|
|
268
|
+
order.slice(0, Math.max(0, o.budget)).forEach((i) => (flies[i] = 1));
|
|
269
|
+
// Tracks, puffs, arrivals.
|
|
270
|
+
const alive = desc.parts.filter((p) => {
|
|
271
|
+
if (state.ruin)
|
|
272
|
+
return (p.flags & PART_FLAG.indestructible) !== 0;
|
|
273
|
+
const e = state.parts.get(p.key);
|
|
274
|
+
return e === undefined || (e & 3) !== DAMAGE.gone;
|
|
275
|
+
});
|
|
276
|
+
// Standing parts, for where a piece may land and what it must not fly into (grid lookups, no search).
|
|
277
|
+
const standing = new Set(alive.map((p) => p.key));
|
|
278
|
+
/** Highest standing top under (x, z) that is not above `below`, else the ground. */
|
|
279
|
+
const supportAt = (x, z, below) => {
|
|
280
|
+
let f = o.groundAt(x, z);
|
|
281
|
+
for (const q of grid.get(cell(x, z)) ?? []) {
|
|
282
|
+
if (!standing.has(q.part.key) || q.y1 > below + 0.01 || q.y1 <= f)
|
|
283
|
+
continue;
|
|
284
|
+
if (insideCm(q.part, x * 100, z * 100))
|
|
285
|
+
f = q.y1;
|
|
286
|
+
}
|
|
287
|
+
return f;
|
|
288
|
+
};
|
|
289
|
+
/** A standing part holds the point (x, y, z): a piece there would be inside it. */
|
|
290
|
+
const blocked = (x, y, z) => {
|
|
291
|
+
for (const q of grid.get(cell(x, z)) ?? []) {
|
|
292
|
+
if (!standing.has(q.part.key) || q.y1 <= y + BLOCK_M || q.y0 >= y + BLOCK_M)
|
|
293
|
+
continue;
|
|
294
|
+
if (insideCm(q.part, x * 100, z * 100))
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
return false;
|
|
298
|
+
};
|
|
299
|
+
// Centre of the structure (bounding box of its parts): pieces that lose support lean away from it.
|
|
300
|
+
let sx0 = Infinity;
|
|
301
|
+
let sx1 = -Infinity;
|
|
302
|
+
let sz0 = Infinity;
|
|
303
|
+
let sz1 = -Infinity;
|
|
304
|
+
for (const q of solids) {
|
|
305
|
+
for (let k = 0; k < q.ring.length; k += 2) {
|
|
306
|
+
sx0 = Math.min(sx0, q.ring[k]);
|
|
307
|
+
sx1 = Math.max(sx1, q.ring[k]);
|
|
308
|
+
sz0 = Math.min(sz0, q.ring[k + 1]);
|
|
309
|
+
sz1 = Math.max(sz1, q.ring[k + 1]);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const scx = (sx0 + sx1) / 2;
|
|
313
|
+
const scz = (sz0 + sz1) / 2;
|
|
314
|
+
/**
|
|
315
|
+
* Keeps a piece out of standing parts: the landing spot (and the middle of the flight) must not lie inside one. The
|
|
316
|
+
* horizontal velocity is tried at full, reduced, turned back and zero, and the landing height follows the spot.
|
|
317
|
+
*/
|
|
318
|
+
const clear = (t, x, z, below, thin) => {
|
|
319
|
+
for (const k of CLEAR_SCALES) {
|
|
320
|
+
let c = { ...t, vx: t.vx * k, vz: t.vz * k };
|
|
321
|
+
for (let it = 0; it < 2; it++) {
|
|
322
|
+
const fl = trackFlightTime(c);
|
|
323
|
+
c = { ...c, land: supportAt(x + c.vx * fl, z + c.vz * fl, below) + thin * 0.5 };
|
|
324
|
+
}
|
|
325
|
+
if (k === 0)
|
|
326
|
+
return c;
|
|
327
|
+
const fl = trackFlightTime(c);
|
|
328
|
+
const h = fl / 2;
|
|
329
|
+
const midY = Math.max(c.land, c.y + c.vy * h - 4.905 * h * h);
|
|
330
|
+
if (!blocked(x + c.vx * fl, c.land, z + c.vz * fl) && !blocked(x + c.vx * h, midY, z + c.vz * h))
|
|
331
|
+
return c;
|
|
332
|
+
}
|
|
333
|
+
return t;
|
|
334
|
+
};
|
|
335
|
+
const puffs = [];
|
|
336
|
+
const arrivals = new Map();
|
|
337
|
+
let start = Infinity;
|
|
338
|
+
let end = -Infinity;
|
|
339
|
+
pieces.forEach((pc, i) => {
|
|
340
|
+
const s = byKey.get(pc.part.key);
|
|
341
|
+
const ev = pc.change.event ?? o.event;
|
|
342
|
+
const r = (k) => unit(hashInts(ev.seed, desc.id, pc.part.key, pc.sub, k));
|
|
343
|
+
const y0 = s.y0;
|
|
344
|
+
const y1 = s.y1;
|
|
345
|
+
const cy = (y0 + y1) / 2;
|
|
346
|
+
const { cx, cz, thin } = pc;
|
|
347
|
+
let floor = o.groundAt(cx, cz);
|
|
348
|
+
for (const q of alive) {
|
|
349
|
+
if (q.piece.y1 / 100 > y0 + 0.01 || q.piece.y1 / 100 <= floor)
|
|
350
|
+
continue;
|
|
351
|
+
if (insideCm(q, cx * 100, cz * 100))
|
|
352
|
+
floor = q.piece.y1 / 100;
|
|
353
|
+
}
|
|
354
|
+
const at = o.at + pc.change.delay;
|
|
355
|
+
let track;
|
|
356
|
+
if (pc.change.hit) {
|
|
357
|
+
let dx = cx - ev.x;
|
|
358
|
+
let dz = cz - ev.z;
|
|
359
|
+
const dy = cy - ev.y;
|
|
360
|
+
const dist = Math.sqrt(dx * dx + dz * dz + dy * dy);
|
|
361
|
+
const l = Math.sqrt(dx * dx + dz * dz) || 1;
|
|
362
|
+
dx /= l;
|
|
363
|
+
dz /= l;
|
|
364
|
+
const near = Math.max(0, 1 - dist / Math.max(ev.radius, 0.5));
|
|
365
|
+
const speed = (2 + 7 * Math.min(1, ev.energy / 2000) * (0.4 + 0.6 * near)) * (0.8 + 0.4 * r(1));
|
|
366
|
+
const side = (r(2) - 0.5) * 0.5;
|
|
367
|
+
const ground = o.groundAt(cx + dx * speed * 0.8, cz + dz * speed * 0.8);
|
|
368
|
+
track = { start: at, x: cx, y: cy, z: cz, vx: (dx - dz * side) * speed, vy: 1.5 + 3 * r(3) * (0.5 + near), vz: (dz + dx * side) * speed, ax: -dz, ay: 0, az: dx, spin: (r(4) < 0.5 ? -1 : 1) * (2 + 5 * r(5)), land: ground + thin * 0.5 };
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
const e = state.ruin ? 0 : (state.parts.get(pc.part.key) ?? 0);
|
|
372
|
+
const dir = state.ruin ? 8 : entryDir(e);
|
|
373
|
+
const [fx, fz] = dir < 8 ? DIRS[dir] : [0, 0];
|
|
374
|
+
const drift = dir < 8 ? 0.6 + 0.8 * r(1) : 0.15 * r(1);
|
|
375
|
+
const tilt = (r(4) < 0.5 ? -1 : 1) * (0.4 + 1.0 * r(5));
|
|
376
|
+
// Lean away from the structure's centre, so falling pieces spread out instead of dropping through each other.
|
|
377
|
+
let ox = cx - scx;
|
|
378
|
+
let oz = cz - scz;
|
|
379
|
+
const ol = Math.sqrt(ox * ox + oz * oz);
|
|
380
|
+
const lean = ol > 0.5 ? LEAN_MS * (0.5 + r(8)) : 0;
|
|
381
|
+
ox = ol > 0.5 ? ox / ol : 0;
|
|
382
|
+
oz = ol > 0.5 ? oz / ol : 0;
|
|
383
|
+
// Without a fall side the tilt axis is a hashed horizontal direction.
|
|
384
|
+
const ang = r(6) * Math.PI;
|
|
385
|
+
const ax = fz !== 0 || fx !== 0 ? -fz : Math.cos(ang);
|
|
386
|
+
const az = fz !== 0 || fx !== 0 ? fx : Math.sin(ang);
|
|
387
|
+
track = {
|
|
388
|
+
start: at + STAGGER_S * r(7),
|
|
389
|
+
x: cx,
|
|
390
|
+
y: cy,
|
|
391
|
+
z: cz,
|
|
392
|
+
vx: fx * drift + ox * lean + (r(2) - 0.5) * 0.4,
|
|
393
|
+
vy: 0,
|
|
394
|
+
vz: fz * drift + oz * lean + (r(3) - 0.5) * 0.4,
|
|
395
|
+
ax,
|
|
396
|
+
ay: 0,
|
|
397
|
+
az,
|
|
398
|
+
spin: tilt,
|
|
399
|
+
land: floor + thin * 0.5,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
if (!flies[i])
|
|
403
|
+
track = { ...track, vx: 0, vy: 0, vz: 0, spin: 0, dust: true };
|
|
404
|
+
else
|
|
405
|
+
track = clear(track, cx, cz, y0, thin);
|
|
406
|
+
pc.track = track;
|
|
407
|
+
const fl = trackFlightTime(track);
|
|
408
|
+
if (track.dust) {
|
|
409
|
+
puffs.push({ x: cx, y: cy, z: cz, start: track.start, size: Math.max(1.2, Math.min(4, Math.sqrt(pc.size / Math.max(thin, 0.2)))) });
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
puffs.push({ x: cx + track.vx * fl, y: track.land - 0.3, z: cz + track.vz * fl, start: track.start + fl, size: Math.max(0.8, Math.min(3.5, Math.cbrt(pc.size) * 1.1)) });
|
|
413
|
+
}
|
|
414
|
+
const arrive = track.start + fl;
|
|
415
|
+
const prev = arrivals.get(pc.part.key);
|
|
416
|
+
if (!prev || arrive > prev.start)
|
|
417
|
+
arrivals.set(pc.part.key, { start: arrive, seconds: GROW_S });
|
|
418
|
+
start = Math.min(start, track.start);
|
|
419
|
+
end = Math.max(end, track.start + trackLifetime(track), arrive + GROW_S);
|
|
420
|
+
});
|
|
421
|
+
// Parts with nothing to draw still get their rubble on time.
|
|
422
|
+
for (const c of o.changes) {
|
|
423
|
+
if (!arrivals.has(c.part))
|
|
424
|
+
arrivals.set(c.part, { start: o.at + c.delay, seconds: GROW_S });
|
|
425
|
+
start = Math.min(start, o.at + c.delay);
|
|
426
|
+
end = Math.max(end, o.at + c.delay + GROW_S);
|
|
427
|
+
}
|
|
428
|
+
// Vertices: the source bytes (whole triangles), clipped triangles (interpolated position and surface coordinates,
|
|
429
|
+
// the rest from the provoking vertex), the shell; each with the piece's track.
|
|
430
|
+
// Colours and storey data of a shell come from a wall triangle of its own part, else from any wall of the structure.
|
|
431
|
+
const isWall = (t) => (i8[t * 3 * SRC + 15] & 7) === KIND.wall;
|
|
432
|
+
let wallTri = 0;
|
|
433
|
+
for (let t = 0; t < tris; t++) {
|
|
434
|
+
if (isWall(t)) {
|
|
435
|
+
wallTri = t;
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
const ownWall = (pc) => pc.whole.find(isWall) ?? pc.clipped.find((c) => isWall(c.tri))?.tri;
|
|
440
|
+
const shellW = new VertexWriter();
|
|
441
|
+
const shellRange = [];
|
|
442
|
+
for (const pc of pieces) {
|
|
443
|
+
const from = shellW.count;
|
|
444
|
+
if (pc.shell)
|
|
445
|
+
writeShell(shellW, pc, byKey.get(pc.part.key), src, f32, ownWall(pc) ?? wallTri, before.count > 0);
|
|
446
|
+
shellRange.push([from, shellW.count]);
|
|
447
|
+
}
|
|
448
|
+
const shellBytes = shellW.bytes();
|
|
449
|
+
let total = 0;
|
|
450
|
+
pieces.forEach((pc, i) => {
|
|
451
|
+
total += pc.whole.length * 3 + shellRange[i][1] - shellRange[i][0];
|
|
452
|
+
for (const c of pc.clipped)
|
|
453
|
+
total += (c.w.length - 2) * 3;
|
|
454
|
+
});
|
|
455
|
+
const out = new ArrayBuffer(total * STRIDE);
|
|
456
|
+
const ob = new Uint8Array(out);
|
|
457
|
+
const of = new Float32Array(out);
|
|
458
|
+
let v = 0;
|
|
459
|
+
const trackAt = (t) => writeTrack(of, (v * STRIDE + SRC) / 4, t);
|
|
460
|
+
const tracks = [];
|
|
461
|
+
const flyingFirst = pieces.map((_, i) => i).sort((a, b) => flies[b] - flies[a] || a - b);
|
|
462
|
+
for (const i of flyingFirst) {
|
|
463
|
+
const pc = pieces[i];
|
|
464
|
+
const t = pc.track;
|
|
465
|
+
tracks.push(t);
|
|
466
|
+
for (const tri of pc.whole) {
|
|
467
|
+
for (let k = 0; k < 3; k++) {
|
|
468
|
+
ob.set(src.subarray((tri * 3 + k) * SRC, (tri * 3 + k + 1) * SRC), v * STRIDE);
|
|
469
|
+
trackAt(t);
|
|
470
|
+
v++;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
for (const c of pc.clipped) {
|
|
474
|
+
const put = (w) => {
|
|
475
|
+
const prov = (c.tri * 3 + 2) * SRC;
|
|
476
|
+
ob.set(src.subarray(prov, prov + SRC), v * STRIDE);
|
|
477
|
+
const of0 = (v * STRIDE) / 4;
|
|
478
|
+
for (const fi of [0, 1, 2, 4, 5, 6, 7]) {
|
|
479
|
+
let x = 0;
|
|
480
|
+
for (let k = 0; k < 3; k++)
|
|
481
|
+
x += w[k] * f32[((c.tri * 3 + k) * SRC) / 4 + fi];
|
|
482
|
+
of[of0 + fi] = x;
|
|
483
|
+
}
|
|
484
|
+
trackAt(t);
|
|
485
|
+
v++;
|
|
486
|
+
};
|
|
487
|
+
for (let k = 1; k + 1 < c.w.length; k++) {
|
|
488
|
+
put(c.w[0]);
|
|
489
|
+
put(c.w[k]);
|
|
490
|
+
put(c.w[k + 1]);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
const [a, b] = shellRange[i];
|
|
494
|
+
for (let k = a; k < b; k++) {
|
|
495
|
+
ob.set(shellBytes.subarray(k * SRC, (k + 1) * SRC), v * STRIDE);
|
|
496
|
+
writeTrack(of, (v * STRIDE + SRC) / 4, t, true);
|
|
497
|
+
v++;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
const flying = flies.reduce((n, x) => n + x, 0);
|
|
501
|
+
return {
|
|
502
|
+
mesh: { layout: FALL_LAYOUT, vertices: new Float32Array(out), indices: null, count: v },
|
|
503
|
+
pieces: flying,
|
|
504
|
+
dust: pieces.length - flying,
|
|
505
|
+
puffs,
|
|
506
|
+
arrivals,
|
|
507
|
+
start: Number.isFinite(start) ? start : o.at,
|
|
508
|
+
end: Number.isFinite(end) ? end : o.at,
|
|
509
|
+
tracks,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Moves a prepared fall `dt` seconds later on the clock: track starts in every vertex, puffs, arrivals and the span.
|
|
514
|
+
* For a fall prepared at the event's clock time and shown a little later (the preparation ran over a few tasks), so the
|
|
515
|
+
* collapse starts when it first shows. Shapes and the timeline relative to the event stay the same.
|
|
516
|
+
*/
|
|
517
|
+
export function retimeFall(out, dt) {
|
|
518
|
+
if (dt === 0)
|
|
519
|
+
return;
|
|
520
|
+
const f32 = new Float32Array(out.mesh.vertices.buffer, out.mesh.vertices.byteOffset, (out.mesh.count * STRIDE) / 4);
|
|
521
|
+
for (let v = 0; v < out.mesh.count; v++) {
|
|
522
|
+
const k = (v * STRIDE + SRC) / 4 + 3;
|
|
523
|
+
f32[k] = f32[k] + dt;
|
|
524
|
+
}
|
|
525
|
+
out.puffs = out.puffs.map((p) => ({ ...p, start: p.start + dt }));
|
|
526
|
+
for (const a of out.arrivals.values())
|
|
527
|
+
a.start += dt;
|
|
528
|
+
out.tracks = out.tracks.map((t) => ({ ...t, start: t.start + dt }));
|
|
529
|
+
out.start += dt;
|
|
530
|
+
out.end += dt;
|
|
531
|
+
}
|
|
532
|
+
/** Area and unit normal of a source triangle (from its positions). */
|
|
533
|
+
function triArea(f32, t) {
|
|
534
|
+
const o = (k) => ((t * 3 + k) * SRC) / 4;
|
|
535
|
+
const ax = f32[o(1)] - f32[o(0)];
|
|
536
|
+
const ay = f32[o(1) + 1] - f32[o(0) + 1];
|
|
537
|
+
const az = f32[o(1) + 2] - f32[o(0) + 2];
|
|
538
|
+
const bx = f32[o(2)] - f32[o(0)];
|
|
539
|
+
const by = f32[o(2) + 1] - f32[o(0) + 1];
|
|
540
|
+
const bz = f32[o(2) + 2] - f32[o(0) + 2];
|
|
541
|
+
const cx = ay * bz - az * by;
|
|
542
|
+
const cy = az * bx - ax * bz;
|
|
543
|
+
const cz = ax * by - ay * bx;
|
|
544
|
+
const l = Math.sqrt(cx * cx + cy * cy + cz * cz);
|
|
545
|
+
return l < 1e-12 ? [0, 0, 1, 0] : [l / 2, cx / l, cy / l, cz / l];
|
|
546
|
+
}
|
|
547
|
+
/** Which pieces fly when the budget is short: the most drawn surface first; hidden pieces last, by size. */
|
|
548
|
+
function score(p) {
|
|
549
|
+
return p.hidden ? 1e-3 * p.size : p.surface;
|
|
550
|
+
}
|
|
551
|
+
function solidOf(part) {
|
|
552
|
+
const ring = [];
|
|
553
|
+
const n = part.piece.xz.length / 2;
|
|
554
|
+
let cx = 0;
|
|
555
|
+
let cz = 0;
|
|
556
|
+
for (let k = 0; k < n; k++) {
|
|
557
|
+
ring.push(part.piece.xz[k * 2] / 100, part.piece.xz[k * 2 + 1] / 100);
|
|
558
|
+
cx += ring[k * 2] / n;
|
|
559
|
+
cz += ring[k * 2 + 1] / n;
|
|
560
|
+
}
|
|
561
|
+
const nx = [];
|
|
562
|
+
const nz = [];
|
|
563
|
+
const d = [];
|
|
564
|
+
let area = 0;
|
|
565
|
+
for (let k = 0; k < n; k++) {
|
|
566
|
+
const j = (k + 1) % n;
|
|
567
|
+
const ax = ring[k * 2];
|
|
568
|
+
const az = ring[k * 2 + 1];
|
|
569
|
+
const bx = ring[j * 2];
|
|
570
|
+
const bz = ring[j * 2 + 1];
|
|
571
|
+
area += ax * bz - bx * az;
|
|
572
|
+
const l = Math.sqrt((bx - ax) ** 2 + (bz - az) ** 2);
|
|
573
|
+
if (l < 1e-6)
|
|
574
|
+
continue;
|
|
575
|
+
let ex = (bz - az) / l;
|
|
576
|
+
let ez = -(bx - ax) / l;
|
|
577
|
+
if ((ax + bx) / 2 * ex + (az + bz) / 2 * ez - (cx * ex + cz * ez) < 0) {
|
|
578
|
+
ex = -ex;
|
|
579
|
+
ez = -ez;
|
|
580
|
+
}
|
|
581
|
+
nx.push(ex);
|
|
582
|
+
nz.push(ez);
|
|
583
|
+
d.push(ax * ex + az * ez);
|
|
584
|
+
}
|
|
585
|
+
const y0 = part.piece.y0 / 100;
|
|
586
|
+
const y1 = part.piece.y1 / 100;
|
|
587
|
+
return { part, ring, nx, nz, d, y0, y1, volume: (Math.abs(area) / 2) * Math.max(0.01, y1 - y0) };
|
|
588
|
+
}
|
|
589
|
+
/** Distance of a point outside a solid (0 inside). */
|
|
590
|
+
function outside(s, x, y, z) {
|
|
591
|
+
let m = Math.max(s.y0 - y, y - s.y1);
|
|
592
|
+
for (let k = 0; k < s.nx.length; k++)
|
|
593
|
+
m = Math.max(m, x * s.nx[k] + z * s.nz[k] - s.d[k]);
|
|
594
|
+
return Math.max(0, m);
|
|
595
|
+
}
|
|
596
|
+
/** Is the point (cm) inside the convex outline of the part? */
|
|
597
|
+
function insideCm(p, x, z) {
|
|
598
|
+
const xz = p.piece.xz;
|
|
599
|
+
const n = xz.length / 2;
|
|
600
|
+
let sign = 0;
|
|
601
|
+
for (let k = 0; k < n; k++) {
|
|
602
|
+
const j = (k + 1) % n;
|
|
603
|
+
const cr = (xz[j * 2] - xz[k * 2]) * (z - xz[k * 2 + 1]) - (xz[j * 2 + 1] - xz[k * 2 + 1]) * (x - xz[k * 2]);
|
|
604
|
+
if (cr === 0)
|
|
605
|
+
continue;
|
|
606
|
+
const s = cr > 0 ? 1 : -1;
|
|
607
|
+
if (sign === 0)
|
|
608
|
+
sign = s;
|
|
609
|
+
else if (s !== sign)
|
|
610
|
+
return false;
|
|
611
|
+
}
|
|
612
|
+
return true;
|
|
613
|
+
}
|
|
614
|
+
/** Keeps the part of a convex polygon where `side(p) >= 0` (side is affine). */
|
|
615
|
+
function clipPoly(poly, side) {
|
|
616
|
+
const out = [];
|
|
617
|
+
for (let k = 0; k < poly.length; k++) {
|
|
618
|
+
const a = poly[k];
|
|
619
|
+
const b = poly[(k + 1) % poly.length];
|
|
620
|
+
const sa = side(a);
|
|
621
|
+
const sb = side(b);
|
|
622
|
+
if (sa >= 0)
|
|
623
|
+
out.push(a);
|
|
624
|
+
if ((sa >= 0) !== (sb >= 0)) {
|
|
625
|
+
const t = sa / (sa - sb);
|
|
626
|
+
out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return out;
|
|
630
|
+
}
|
|
631
|
+
/** Clips a source triangle to a cell (four affine sides in x, z); the result as barycentric weights. */
|
|
632
|
+
function clipTri(t, sides, f32) {
|
|
633
|
+
const P = (k) => [f32[((t * 3 + k) * SRC) / 4], f32[((t * 3 + k) * SRC) / 4 + 2]];
|
|
634
|
+
const p = [P(0), P(1), P(2)];
|
|
635
|
+
let poly = [
|
|
636
|
+
[1, 0, 0],
|
|
637
|
+
[0, 1, 0],
|
|
638
|
+
[0, 0, 1],
|
|
639
|
+
];
|
|
640
|
+
const at = (w) => [w[0] * p[0][0] + w[1] * p[1][0] + w[2] * p[2][0], w[0] * p[0][1] + w[1] * p[1][1] + w[2] * p[2][1]];
|
|
641
|
+
for (let h = 0; h < 4; h++) {
|
|
642
|
+
const out = [];
|
|
643
|
+
for (let k = 0; k < poly.length; k++) {
|
|
644
|
+
const a = poly[k];
|
|
645
|
+
const b = poly[(k + 1) % poly.length];
|
|
646
|
+
const sa = sides(at(a))[h];
|
|
647
|
+
const sb = sides(at(b))[h];
|
|
648
|
+
if (sa >= 0)
|
|
649
|
+
out.push(a);
|
|
650
|
+
if ((sa >= 0) !== (sb >= 0)) {
|
|
651
|
+
const u = sa / (sa - sb);
|
|
652
|
+
out.push([a[0] + (b[0] - a[0]) * u, a[1] + (b[1] - a[1]) * u, a[2] + (b[2] - a[2]) * u]);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
poly = out;
|
|
656
|
+
if (poly.length < 3)
|
|
657
|
+
return [];
|
|
658
|
+
}
|
|
659
|
+
return poly;
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* A closed shell of cut faces just inside the piece's outline, from its bottom to its top: sides, top and bottom.
|
|
663
|
+
* Colours and storey data come from one of the part's own vertices.
|
|
664
|
+
*/
|
|
665
|
+
function writeShell(w, pc, s, src, f32, tri, hasSource) {
|
|
666
|
+
const o = tri * 3 * SRC;
|
|
667
|
+
const fo = o / 4;
|
|
668
|
+
const wallColor = hasSource ? src[o + 48] : 0;
|
|
669
|
+
const mat = { seed: hasSource ? f32[fo + 10] : 0, bayWidth: hasSource ? f32[fo + 11] : 3, wallColor, roofColor: hasSource ? src[o + 49] : 0, windowStyle: hasSource ? src[o + 50] : 0, trimColor: wallColor };
|
|
670
|
+
const floorHeight = hasSource ? f32[fo + 8] : 3;
|
|
671
|
+
const floors = hasSource ? f32[fo + 9] : 1;
|
|
672
|
+
const poly = inset(pc.poly, SHELL_INSET_M);
|
|
673
|
+
if (poly.length < 3)
|
|
674
|
+
return;
|
|
675
|
+
const y0 = s.y0 + SHELL_INSET_M;
|
|
676
|
+
const y1 = s.y1 - SHELL_INSET_M;
|
|
677
|
+
if (y1 <= y0)
|
|
678
|
+
return;
|
|
679
|
+
// Narrow faces (the thickness of a wall or a slab where it broke) show the broken core. Broad faces show the
|
|
680
|
+
// surface the part had inside: a slab its floor on top and its ceiling below, a partition and the back of a wall
|
|
681
|
+
// room paint (KIND.interior, the same shading as the interior of a damaged structure, lit as a room in full
|
|
682
|
+
// daylight that never has its lamp on). The room hash comes from the part, so all pieces of a slab match.
|
|
683
|
+
const CORE_M = 0.8;
|
|
684
|
+
const isFloor = s.part.material === STRUCTURE_MATERIAL.floor;
|
|
685
|
+
const room = unit(hashInts(0x5e11, s.part.key)) * 0.999;
|
|
686
|
+
const faFor = (finish) => ({
|
|
687
|
+
doorU: 2,
|
|
688
|
+
doorW: 0,
|
|
689
|
+
doorY: 1,
|
|
690
|
+
groundFloorH: 2,
|
|
691
|
+
win: [SHELL_DAYLIGHT * WIN_UNIT, 128 * WIN_UNIT, 128 * WIN_UNIT, 0],
|
|
692
|
+
bits: [finish, ROOM_TYPE.room, 0, 0],
|
|
693
|
+
cols: [0, 0, 0, 0],
|
|
694
|
+
});
|
|
695
|
+
const core = KIND.trim + DAMAGE_STEP * 3;
|
|
696
|
+
// Paint stays clean (cracks at room-paint scale read as a glaze); floors and ceilings get the light cracks.
|
|
697
|
+
const surface = KIND.interior;
|
|
698
|
+
const cracked = KIND.interior + DAMAGE_STEP * 1;
|
|
699
|
+
const faCore = faFor(0);
|
|
700
|
+
const put = (x, y, z, nx, ny, nz, kind, s0, s1, fa) => kind === core
|
|
701
|
+
? w.push(x, y, z, nx, ny, nz, kind, 0, 0, y - s.y0, 100, mat, floors, floorHeight)
|
|
702
|
+
: w.push(x, y, z, nx, ny, nz, kind, s0, s1, floorHeight, room, mat, floors, floorHeight, fa);
|
|
703
|
+
let cx = 0;
|
|
704
|
+
let cz = 0;
|
|
705
|
+
for (const p of poly) {
|
|
706
|
+
cx += p[0] / poly.length;
|
|
707
|
+
cz += p[1] / poly.length;
|
|
708
|
+
}
|
|
709
|
+
const paint = faFor(FINISH.paint);
|
|
710
|
+
for (let k = 0; k < poly.length; k++) {
|
|
711
|
+
const a = poly[k];
|
|
712
|
+
const b = poly[(k + 1) % poly.length];
|
|
713
|
+
let nx = b[1] - a[1];
|
|
714
|
+
let nz = -(b[0] - a[0]);
|
|
715
|
+
const l = Math.sqrt(nx * nx + nz * nz) || 1;
|
|
716
|
+
nx /= l;
|
|
717
|
+
nz /= l;
|
|
718
|
+
if ((a[0] + b[0]) / 2 * nx + (a[1] + b[1]) / 2 * nz < cx * nx + cz * nz) {
|
|
719
|
+
nx = -nx;
|
|
720
|
+
nz = -nz;
|
|
721
|
+
}
|
|
722
|
+
const narrow = Math.min(l, y1 - y0) < CORE_M;
|
|
723
|
+
const kind = narrow || isFloor ? core : surface;
|
|
724
|
+
const fa = kind === core ? faCore : paint;
|
|
725
|
+
// Pattern coordinates on a side: along the edge and height above the part's bottom.
|
|
726
|
+
const ua = a[0] * -nz + a[1] * nx;
|
|
727
|
+
const ub = b[0] * -nz + b[1] * nx;
|
|
728
|
+
put(a[0], y0, a[1], nx, 0, nz, kind, ua, y0 - s.y0, fa);
|
|
729
|
+
put(b[0], y0, b[1], nx, 0, nz, kind, ub, y0 - s.y0, fa);
|
|
730
|
+
put(b[0], y1, b[1], nx, 0, nz, kind, ub, y1 - s.y0, fa);
|
|
731
|
+
put(a[0], y0, a[1], nx, 0, nz, kind, ua, y0 - s.y0, fa);
|
|
732
|
+
put(b[0], y1, b[1], nx, 0, nz, kind, ub, y1 - s.y0, fa);
|
|
733
|
+
put(a[0], y1, a[1], nx, 0, nz, kind, ua, y1 - s.y0, fa);
|
|
734
|
+
}
|
|
735
|
+
let edge = 0;
|
|
736
|
+
for (let k = 0; k < poly.length; k++)
|
|
737
|
+
edge = Math.max(edge, Math.hypot(poly[(k + 1) % poly.length][0] - poly[k][0], poly[(k + 1) % poly.length][1] - poly[k][1]));
|
|
738
|
+
const flatNarrow = polyArea(poly) / Math.max(edge, 1e-3) < CORE_M;
|
|
739
|
+
for (const [y, ny] of [
|
|
740
|
+
[y1, 1],
|
|
741
|
+
[y0, -1],
|
|
742
|
+
]) {
|
|
743
|
+
const kind = flatNarrow || !isFloor ? core : cracked;
|
|
744
|
+
const fa = kind === core ? faCore : faFor(ny > 0 ? FINISH.floor : FINISH.ceiling);
|
|
745
|
+
for (let k = 1; k + 1 < poly.length; k++) {
|
|
746
|
+
put(poly[0][0], y, poly[0][1], 0, ny, 0, kind, poly[0][0], poly[0][1], fa);
|
|
747
|
+
put(poly[k][0], y, poly[k][1], 0, ny, 0, kind, poly[k][0], poly[k][1], fa);
|
|
748
|
+
put(poly[k + 1][0], y, poly[k + 1][1], 0, ny, 0, kind, poly[k + 1][0], poly[k + 1][1], fa);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
function polyArea(poly) {
|
|
753
|
+
let a = 0;
|
|
754
|
+
for (let k = 0; k < poly.length; k++) {
|
|
755
|
+
const p = poly[k];
|
|
756
|
+
const q = poly[(k + 1) % poly.length];
|
|
757
|
+
a += p[0] * q[1] - q[0] * p[1];
|
|
758
|
+
}
|
|
759
|
+
return Math.abs(a) / 2;
|
|
760
|
+
}
|
|
761
|
+
/** A convex polygon moved inward by `d` along every edge (corners where the moved edges meet). */
|
|
762
|
+
function inset(poly, d) {
|
|
763
|
+
const n = poly.length;
|
|
764
|
+
let cx = 0;
|
|
765
|
+
let cz = 0;
|
|
766
|
+
for (const p of poly) {
|
|
767
|
+
cx += p[0] / n;
|
|
768
|
+
cz += p[1] / n;
|
|
769
|
+
}
|
|
770
|
+
const lines = [];
|
|
771
|
+
for (let k = 0; k < n; k++) {
|
|
772
|
+
const a = poly[k];
|
|
773
|
+
const b = poly[(k + 1) % n];
|
|
774
|
+
let nx = b[1] - a[1];
|
|
775
|
+
let nz = -(b[0] - a[0]);
|
|
776
|
+
const l = Math.sqrt(nx * nx + nz * nz);
|
|
777
|
+
if (l < 1e-6)
|
|
778
|
+
continue;
|
|
779
|
+
nx /= l;
|
|
780
|
+
nz /= l;
|
|
781
|
+
if (a[0] * nx + a[1] * nz < cx * nx + cz * nz) {
|
|
782
|
+
nx = -nx;
|
|
783
|
+
nz = -nz;
|
|
784
|
+
}
|
|
785
|
+
lines.push([nx, nz, a[0] * nx + a[1] * nz - d]);
|
|
786
|
+
}
|
|
787
|
+
const out = [];
|
|
788
|
+
for (let k = 0; k < lines.length; k++) {
|
|
789
|
+
const [ax, az, ad] = lines[(k + lines.length - 1) % lines.length];
|
|
790
|
+
const [bx, bz, bd] = lines[k];
|
|
791
|
+
const det = ax * bz - az * bx;
|
|
792
|
+
if (Math.abs(det) < 1e-9)
|
|
793
|
+
continue;
|
|
794
|
+
out.push([(ad * bz - az * bd) / det, (ax * bd - ad * bx) / det]);
|
|
795
|
+
}
|
|
796
|
+
return out;
|
|
797
|
+
}
|