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,568 @@
|
|
|
1
|
+
// Roofs in three levels according to the research:
|
|
2
|
+
// 1. footprint close to a rectangle: ridge on a rotated bounding rectangle (gable or hip),
|
|
3
|
+
// 2. other footprints: height grows with distance from the eave, so the ridge comes out in the middle of the wings,
|
|
4
|
+
// 3. flat roof as a fallback and for large and flat buildings.
|
|
5
|
+
// All three return triangles and a function of the wall's top edge height, by which the walls are closed up.
|
|
6
|
+
import earcut, { deviation } from 'earcut';
|
|
7
|
+
import { distToSegment } from '../../gen-core/index.js';
|
|
8
|
+
/** Roof height above the eave for a given footprint span, with the slope within limits and the eave above the minimum. */
|
|
9
|
+
function resolvePitch(p, run) {
|
|
10
|
+
const slope = Math.min(p.maxSlope, Math.max(p.minSlope, (p.ridgeY - p.eaveWanted) / run));
|
|
11
|
+
const eaveY = Math.max(p.minEaveY, p.ridgeY - slope * run);
|
|
12
|
+
return { eaveY, roofHeight: slope * run };
|
|
13
|
+
}
|
|
14
|
+
/** Largest allowed deviation of the triangulation area from the footprint area. */
|
|
15
|
+
const MAX_DEVIATION = 0.01;
|
|
16
|
+
/** Longest edge of the level 2 auxiliary mesh in metres. */
|
|
17
|
+
const FIELD_EDGE_M = 1.5;
|
|
18
|
+
/** Shorter footprint edges (teeth and projections) are collapsed to a single point for the roof shape, otherwise each tooth would get its own hip. */
|
|
19
|
+
const FIELD_MIN_EDGE_M = 1.7;
|
|
20
|
+
/** How far beyond the wall a point still counts as "on the eave" (after outline simplification some vertices lie just outside). */
|
|
21
|
+
const FIELD_OUTSIDE_M = 1.2;
|
|
22
|
+
const EPS = 1e-4;
|
|
23
|
+
function flatten(rings) {
|
|
24
|
+
const coords = [];
|
|
25
|
+
const holes = [];
|
|
26
|
+
rings.forEach((r, k) => {
|
|
27
|
+
if (k > 0)
|
|
28
|
+
holes.push(coords.length / 2);
|
|
29
|
+
for (const v of r)
|
|
30
|
+
coords.push(v);
|
|
31
|
+
});
|
|
32
|
+
return { coords, holes };
|
|
33
|
+
}
|
|
34
|
+
/** Footprint triangulation. Returns null when the area check fails. */
|
|
35
|
+
function triangulate(rings) {
|
|
36
|
+
const { coords, holes } = flatten(rings);
|
|
37
|
+
const indices = earcut(coords, holes, 2);
|
|
38
|
+
if (indices.length === 0)
|
|
39
|
+
return null;
|
|
40
|
+
if (deviation(coords, holes, 2, indices) > MAX_DEVIATION)
|
|
41
|
+
return null;
|
|
42
|
+
return { coords, indices };
|
|
43
|
+
}
|
|
44
|
+
/** Splits planar triangles (6 numbers per triangle) by a line. Each resulting one lies entirely on one side. */
|
|
45
|
+
function splitByLine(tris, line) {
|
|
46
|
+
const out = [];
|
|
47
|
+
const side = (x, z) => line.nx * x + line.nz * z - line.off;
|
|
48
|
+
for (let t = 0; t < tris.length; t += 6) {
|
|
49
|
+
const p = [tris[t], tris[t + 1], tris[t + 2], tris[t + 3], tris[t + 4], tris[t + 5]];
|
|
50
|
+
const s = [side(p[0], p[1]), side(p[2], p[3]), side(p[4], p[5])];
|
|
51
|
+
const pos = s.filter((v) => v > EPS).length;
|
|
52
|
+
const neg = s.filter((v) => v < -EPS).length;
|
|
53
|
+
if (pos === 0 || neg === 0) {
|
|
54
|
+
out.push(...p);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
// Find the vertex that is alone on its side and rotate the triangle so it comes first.
|
|
58
|
+
let lone = 0;
|
|
59
|
+
for (let k = 0; k < 3; k++) {
|
|
60
|
+
const a = s[k];
|
|
61
|
+
const b = s[(k + 1) % 3];
|
|
62
|
+
const c = s[(k + 2) % 3];
|
|
63
|
+
if ((a > EPS && b <= EPS && c <= EPS) || (a < -EPS && b >= -EPS && c >= -EPS))
|
|
64
|
+
lone = k;
|
|
65
|
+
}
|
|
66
|
+
const ax = p[lone * 2];
|
|
67
|
+
const az = p[lone * 2 + 1];
|
|
68
|
+
const bx = p[((lone + 1) % 3) * 2];
|
|
69
|
+
const bz = p[((lone + 1) % 3) * 2 + 1];
|
|
70
|
+
const cx = p[((lone + 2) % 3) * 2];
|
|
71
|
+
const cz = p[((lone + 2) % 3) * 2 + 1];
|
|
72
|
+
const sa = s[lone];
|
|
73
|
+
const sb = s[(lone + 1) % 3];
|
|
74
|
+
const sc = s[(lone + 2) % 3];
|
|
75
|
+
const tb = sa / (sa - sb);
|
|
76
|
+
const tc = sa / (sa - sc);
|
|
77
|
+
const mbx = ax + (bx - ax) * tb;
|
|
78
|
+
const mbz = az + (bz - az) * tb;
|
|
79
|
+
const mcx = ax + (cx - ax) * tc;
|
|
80
|
+
const mcz = az + (cz - az) * tc;
|
|
81
|
+
out.push(ax, az, mbx, mbz, mcx, mcz);
|
|
82
|
+
if (Math.abs(sb) > EPS)
|
|
83
|
+
out.push(mbx, mbz, bx, bz, cx, cz);
|
|
84
|
+
if (Math.abs(sc) > EPS)
|
|
85
|
+
out.push(mbx, mbz, cx, cz, mcx, mcz);
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
/** Lifts planar triangles to a height and flips them facing up. Discards degenerate ones. */
|
|
90
|
+
function lift(tris2, topAt) {
|
|
91
|
+
const out = [];
|
|
92
|
+
for (let t = 0; t < tris2.length; t += 6) {
|
|
93
|
+
const ax = tris2[t];
|
|
94
|
+
const az = tris2[t + 1];
|
|
95
|
+
let bx = tris2[t + 2];
|
|
96
|
+
let bz = tris2[t + 3];
|
|
97
|
+
let cx = tris2[t + 4];
|
|
98
|
+
let cz = tris2[t + 5];
|
|
99
|
+
// Facing up means negative orientation in the footprint (x, z).
|
|
100
|
+
const area2 = (bx - ax) * (cz - az) - (bz - az) * (cx - ax);
|
|
101
|
+
if (Math.abs(area2) < 1e-5)
|
|
102
|
+
continue;
|
|
103
|
+
if (area2 > 0) {
|
|
104
|
+
[bx, cx] = [cx, bx];
|
|
105
|
+
[bz, cz] = [cz, bz];
|
|
106
|
+
}
|
|
107
|
+
out.push(ax, topAt(ax, az), az, bx, topAt(bx, bz), bz, cx, topAt(cx, cz), cz);
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
/** Level 3: flat roof at height y. Also used when the triangulation fails the check. */
|
|
112
|
+
export function flatRoof(rings, y) {
|
|
113
|
+
const { coords, holes } = flatten(rings);
|
|
114
|
+
const indices = earcut(coords, holes, 2);
|
|
115
|
+
const tris2 = [];
|
|
116
|
+
for (const i of indices)
|
|
117
|
+
tris2.push(coords[i * 2], coords[i * 2 + 1]);
|
|
118
|
+
const topAt = () => y;
|
|
119
|
+
return { tris: lift(tris2, topAt), topAt, creases: [], eaveY: y, tier: 3 };
|
|
120
|
+
}
|
|
121
|
+
/** Ready-made profiles: straight faces, a quarter circle (dome) and a two-slope face (steep below, flat above). */
|
|
122
|
+
export const ROOF_PROFILE = {
|
|
123
|
+
straight: [
|
|
124
|
+
[0, 0],
|
|
125
|
+
[1, 1],
|
|
126
|
+
],
|
|
127
|
+
// h = sqrt(1 - (1 - k)^2), rounded to 3 places.
|
|
128
|
+
dome: [
|
|
129
|
+
[0, 0],
|
|
130
|
+
[0.1, 0.436],
|
|
131
|
+
[0.25, 0.661],
|
|
132
|
+
[0.45, 0.835],
|
|
133
|
+
[0.7, 0.954],
|
|
134
|
+
[1, 1],
|
|
135
|
+
],
|
|
136
|
+
mansard: [
|
|
137
|
+
[0, 0],
|
|
138
|
+
[0.22, 0.62],
|
|
139
|
+
[1, 1],
|
|
140
|
+
],
|
|
141
|
+
};
|
|
142
|
+
/** Profile height at k (clamped to 0..1), linear between breakpoints. */
|
|
143
|
+
function profileAt(p, k) {
|
|
144
|
+
if (k <= 0)
|
|
145
|
+
return 0;
|
|
146
|
+
if (k >= 1)
|
|
147
|
+
return 1;
|
|
148
|
+
for (let i = 1; i < p.length; i++) {
|
|
149
|
+
const [k1, h1] = p[i];
|
|
150
|
+
if (k <= k1) {
|
|
151
|
+
const [k0, h0] = p[i - 1];
|
|
152
|
+
return h0 + ((h1 - h0) * (k - k0)) / (k1 - k0);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return 1;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* A radial roof that rises from every edge of a convex footprint to one top point: with the straight profile a pyramid
|
|
159
|
+
* (or a tall pointed roof), with a round profile a dome. The level lines are the ring shrunk about the top point.
|
|
160
|
+
* Returns null for more than one ring, a ring that is not convex, a top point not strictly inside, or a top lower
|
|
161
|
+
* than 0.1 above the eave. The eave is taken as given (no slope limits).
|
|
162
|
+
*/
|
|
163
|
+
export function apexRoof(rings, o) {
|
|
164
|
+
if (rings.length !== 1 || o.topY < o.eaveY + 0.1)
|
|
165
|
+
return null;
|
|
166
|
+
const ring = rings[0];
|
|
167
|
+
const n = ring.length / 2;
|
|
168
|
+
if (n < 3)
|
|
169
|
+
return null;
|
|
170
|
+
let area2 = 0;
|
|
171
|
+
for (let i = 0; i < n; i++) {
|
|
172
|
+
const j = (i + 1) % n;
|
|
173
|
+
area2 += ring[i * 2] * ring[j * 2 + 1] - ring[j * 2] * ring[i * 2 + 1];
|
|
174
|
+
}
|
|
175
|
+
if (Math.abs(area2) < 1e-6)
|
|
176
|
+
return null;
|
|
177
|
+
const orient = Math.sign(area2);
|
|
178
|
+
// Per edge: inward unit normal, offset, and the top point's distance to the edge line.
|
|
179
|
+
const enx = [];
|
|
180
|
+
const enz = [];
|
|
181
|
+
const eoff = [];
|
|
182
|
+
const ed = [];
|
|
183
|
+
for (let i = 0; i < n; i++) {
|
|
184
|
+
const j = (i + 1) % n;
|
|
185
|
+
const h = (i + 2) % n;
|
|
186
|
+
const ax = ring[i * 2];
|
|
187
|
+
const az = ring[i * 2 + 1];
|
|
188
|
+
const ex = ring[j * 2] - ax;
|
|
189
|
+
const ez = ring[j * 2 + 1] - az;
|
|
190
|
+
const fx = ring[h * 2] - ring[j * 2];
|
|
191
|
+
const fz = ring[h * 2 + 1] - ring[j * 2 + 1];
|
|
192
|
+
if ((ex * fz - ez * fx) * orient < -1e-6)
|
|
193
|
+
return null;
|
|
194
|
+
const len = Math.hypot(ex, ez);
|
|
195
|
+
if (len < 1e-6)
|
|
196
|
+
return null;
|
|
197
|
+
// Inward normal: to the left of the edge for a positive ring, to the right for a negative one.
|
|
198
|
+
const nx = (-ez / len) * orient;
|
|
199
|
+
const nz = (ex / len) * orient;
|
|
200
|
+
const d = (o.cx - ax) * nx + (o.cz - az) * nz;
|
|
201
|
+
if (d < 1e-3)
|
|
202
|
+
return null;
|
|
203
|
+
enx.push(nx);
|
|
204
|
+
enz.push(nz);
|
|
205
|
+
eoff.push(ax * nx + az * nz);
|
|
206
|
+
ed.push(d);
|
|
207
|
+
}
|
|
208
|
+
const rise = o.topY - o.eaveY;
|
|
209
|
+
const kAt = (x, z) => {
|
|
210
|
+
let k = 1;
|
|
211
|
+
for (let i = 0; i < n; i++)
|
|
212
|
+
k = Math.min(k, (x * enx[i] + z * enz[i] - eoff[i]) / ed[i]);
|
|
213
|
+
return Math.max(0, k);
|
|
214
|
+
};
|
|
215
|
+
const topAt = (x, z) => o.eaveY + rise * profileAt(o.profile, kAt(x, z));
|
|
216
|
+
const tris = [];
|
|
217
|
+
const at = (i, s) => [o.cx + (ring[i * 2] - o.cx) * s, o.cz + (ring[i * 2 + 1] - o.cz) * s];
|
|
218
|
+
const tri = (a, ya, b, yb, c, yc) => {
|
|
219
|
+
const t2 = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
|
220
|
+
if (Math.abs(t2) < 1e-5)
|
|
221
|
+
return;
|
|
222
|
+
// Facing up means negative orientation in the footprint (x, z).
|
|
223
|
+
if (t2 > 0)
|
|
224
|
+
tris.push(a[0], ya, a[1], c[0], yc, c[1], b[0], yb, b[1]);
|
|
225
|
+
else
|
|
226
|
+
tris.push(a[0], ya, a[1], b[0], yb, b[1], c[0], yc, c[1]);
|
|
227
|
+
};
|
|
228
|
+
for (let i = 0; i < n; i++) {
|
|
229
|
+
const j = (i + 1) % n;
|
|
230
|
+
for (let s = 1; s < o.profile.length; s++) {
|
|
231
|
+
const [k0, h0] = o.profile[s - 1];
|
|
232
|
+
const [k1, h1] = o.profile[s];
|
|
233
|
+
const y0 = o.eaveY + rise * h0;
|
|
234
|
+
const y1 = o.eaveY + rise * h1;
|
|
235
|
+
const a0 = at(i, 1 - k0);
|
|
236
|
+
const b0 = at(j, 1 - k0);
|
|
237
|
+
const a1 = at(i, 1 - k1);
|
|
238
|
+
const b1 = at(j, 1 - k1);
|
|
239
|
+
tri(a0, y0, b0, y0, b1, y1);
|
|
240
|
+
if (k1 < 1)
|
|
241
|
+
tri(a0, y0, b1, y1, a1, y1);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return { tris, topAt, creases: [], eaveY: o.eaveY, tier: 1 };
|
|
245
|
+
}
|
|
246
|
+
/** Level 1: ridge on the axis of a rotated rectangle. Height is a linear function of the distance from the ridge. */
|
|
247
|
+
export function ridgeRoof(rings, o) {
|
|
248
|
+
const tri = triangulate(rings);
|
|
249
|
+
if (!tri)
|
|
250
|
+
return null;
|
|
251
|
+
const { box, profile } = o;
|
|
252
|
+
// Ridge axis (a) and fall axis (b).
|
|
253
|
+
const ax = o.ridgeAcross ? -box.uz : box.ux;
|
|
254
|
+
const az = o.ridgeAcross ? box.ux : box.uz;
|
|
255
|
+
const bx = -az;
|
|
256
|
+
const bz = ax;
|
|
257
|
+
// Actual footprint extent in both axes, measured from the rectangle centre.
|
|
258
|
+
let halfV = 0;
|
|
259
|
+
let halfU = 0;
|
|
260
|
+
for (let i = 0; i < tri.coords.length; i += 2) {
|
|
261
|
+
const dx = tri.coords[i] - box.cx;
|
|
262
|
+
const dz = tri.coords[i + 1] - box.cz;
|
|
263
|
+
halfV = Math.max(halfV, Math.abs(dx * bx + dz * bz));
|
|
264
|
+
halfU = Math.max(halfU, Math.abs(dx * ax + dz * az));
|
|
265
|
+
}
|
|
266
|
+
if (halfV < 0.5)
|
|
267
|
+
return null;
|
|
268
|
+
const { eaveY, roofHeight } = resolvePitch(o.pitch, halfV);
|
|
269
|
+
const run = Math.min(halfV, halfU * 0.8);
|
|
270
|
+
const topAt = (x, z) => {
|
|
271
|
+
const dx = x - box.cx;
|
|
272
|
+
const dz = z - box.cz;
|
|
273
|
+
const v = Math.abs(dx * bx + dz * bz);
|
|
274
|
+
let k = 1 - v / halfV;
|
|
275
|
+
if (o.hipped)
|
|
276
|
+
k = Math.min(k, (halfU - Math.abs(dx * ax + dz * az)) / run);
|
|
277
|
+
if (profile)
|
|
278
|
+
return eaveY + roofHeight * profileAt(profile, k);
|
|
279
|
+
return eaveY + roofHeight * Math.max(0, k);
|
|
280
|
+
};
|
|
281
|
+
const c0 = box.cx * bx + box.cz * bz;
|
|
282
|
+
const creases = [{ nx: bx, nz: bz, off: c0 }];
|
|
283
|
+
if (o.hipped) {
|
|
284
|
+
// Four hips: places where the height from the side equals the height from the end.
|
|
285
|
+
const cu = box.cx * ax + box.cz * az;
|
|
286
|
+
for (const su of [-1, 1]) {
|
|
287
|
+
for (const sv of [-1, 1]) {
|
|
288
|
+
const nx = (su * ax) / run + (sv * bx) / halfV;
|
|
289
|
+
const nz = (su * az) / run + (sv * bz) / halfV;
|
|
290
|
+
creases.push({ nx, nz, off: halfU / run - 1 + (su * cu) / run + (sv * c0) / halfV });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (profile) {
|
|
295
|
+
// The faces break where the profile does: level lines at every inner breakpoint, on the sides and, hipped, at the ends.
|
|
296
|
+
const cu = box.cx * ax + box.cz * az;
|
|
297
|
+
for (let i = 1; i + 1 < profile.length; i++) {
|
|
298
|
+
const ki = profile[i][0];
|
|
299
|
+
for (const s of [-1, 1])
|
|
300
|
+
creases.push({ nx: bx, nz: bz, off: c0 + s * (1 - ki) * halfV });
|
|
301
|
+
if (o.hipped)
|
|
302
|
+
for (const s of [-1, 1])
|
|
303
|
+
creases.push({ nx: ax, nz: az, off: cu + s * (halfU - ki * run) });
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
let tris2 = [];
|
|
307
|
+
for (const i of tri.indices)
|
|
308
|
+
tris2.push(tri.coords[i * 2], tri.coords[i * 2 + 1]);
|
|
309
|
+
for (const line of creases)
|
|
310
|
+
tris2 = splitByLine(tris2, line);
|
|
311
|
+
return { tris: lift(tris2, topAt), topAt, creases, eaveY, tier: 1 };
|
|
312
|
+
}
|
|
313
|
+
/** Collapses short edges of a ring to their midpoint. Returns a new ring, does not change the original. */
|
|
314
|
+
function collapseShortEdges(ring, minEdge) {
|
|
315
|
+
let pts = Array.from(ring);
|
|
316
|
+
for (let guard = 0; guard < 200 && pts.length > 8; guard++) {
|
|
317
|
+
const n = pts.length / 2;
|
|
318
|
+
let shortest = minEdge;
|
|
319
|
+
let at = -1;
|
|
320
|
+
for (let i = 0; i < n; i++) {
|
|
321
|
+
const j = (i + 1) % n;
|
|
322
|
+
const l = Math.hypot(pts[j * 2] - pts[i * 2], pts[j * 2 + 1] - pts[i * 2 + 1]);
|
|
323
|
+
if (l < shortest) {
|
|
324
|
+
shortest = l;
|
|
325
|
+
at = i;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (at < 0)
|
|
329
|
+
break;
|
|
330
|
+
const j = (at + 1) % n;
|
|
331
|
+
const mx = (pts[at * 2] + pts[j * 2]) / 2;
|
|
332
|
+
const mz = (pts[at * 2 + 1] + pts[j * 2 + 1]) / 2;
|
|
333
|
+
const next = [];
|
|
334
|
+
for (let i = 0; i < n; i++) {
|
|
335
|
+
if (i === j)
|
|
336
|
+
continue;
|
|
337
|
+
if (i === at)
|
|
338
|
+
next.push(mx, mz);
|
|
339
|
+
else
|
|
340
|
+
next.push(pts[i * 2], pts[i * 2 + 1]);
|
|
341
|
+
}
|
|
342
|
+
pts = next;
|
|
343
|
+
}
|
|
344
|
+
return pts;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Level 2: a plane rises from every footprint edge and the roof is their intersection. The ridge comes out in the middle of each wing
|
|
348
|
+
* and also follows curved and closed footprints. The eave is at the same height everywhere, so the walls have no gables.
|
|
349
|
+
* The mesh breaks exactly where the nearest edge changes, so hips and ridges are sharp.
|
|
350
|
+
*/
|
|
351
|
+
export function fieldRoof(rings, pitch, maxTriangles = Infinity) {
|
|
352
|
+
const tri = triangulate(rings);
|
|
353
|
+
if (!tri)
|
|
354
|
+
return null;
|
|
355
|
+
const edges = [];
|
|
356
|
+
for (const full of rings) {
|
|
357
|
+
const r = collapseShortEdges(full, FIELD_MIN_EDGE_M);
|
|
358
|
+
const n = r.length / 2;
|
|
359
|
+
const first = edges.length;
|
|
360
|
+
for (let i = 0; i < n; i++) {
|
|
361
|
+
const j = (i + 1) % n;
|
|
362
|
+
const ax = r[i * 2];
|
|
363
|
+
const az = r[i * 2 + 1];
|
|
364
|
+
const ex = r[j * 2] - ax;
|
|
365
|
+
const ez = r[j * 2 + 1] - az;
|
|
366
|
+
const len = Math.hypot(ex, ez) || 1e-6;
|
|
367
|
+
edges.push({ ax, az, dx: ex / len, dz: ez / len, len, next: first + j, reflexEnd: false });
|
|
368
|
+
}
|
|
369
|
+
for (let i = 0; i < n; i++) {
|
|
370
|
+
const e = edges[first + i];
|
|
371
|
+
const f = edges[e.next];
|
|
372
|
+
e.reflexEnd = e.dx * f.dz - e.dz * f.dx < -1e-3;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/** Perpendicular distance from the edge line, positive inside the building. Linear along each segment. */
|
|
376
|
+
const planeDist = (s, x, z) => {
|
|
377
|
+
const e = edges[s];
|
|
378
|
+
return -(x - e.ax) * e.dz + (z - e.az) * e.dx;
|
|
379
|
+
};
|
|
380
|
+
const SLAB_TOLERANCE = 0.02;
|
|
381
|
+
/** Roof height in distance units and the plane that determines it at a point. */
|
|
382
|
+
const nearest = (x, z) => {
|
|
383
|
+
let best = Infinity;
|
|
384
|
+
let label = 0;
|
|
385
|
+
for (let s = 0; s < edges.length; s++) {
|
|
386
|
+
const e = edges[s];
|
|
387
|
+
const t = (x - e.ax) * e.dx + (z - e.az) * e.dz;
|
|
388
|
+
if (t >= -SLAB_TOLERANCE && t <= e.len + SLAB_TOLERANCE) {
|
|
389
|
+
// Just beyond the wall the point is at the eave, far beyond the wall it is a different, opposite wall and the plain distance applies.
|
|
390
|
+
const signed = planeDist(s, x, z);
|
|
391
|
+
const d = signed >= -FIELD_OUTSIDE_M ? Math.max(signed, 0) : -signed;
|
|
392
|
+
if (d < best - 1e-7) {
|
|
393
|
+
best = d;
|
|
394
|
+
label = s;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
else if (t > e.len && e.reflexEnd) {
|
|
398
|
+
// Wedge beyond a concave corner: both neighbouring planes continue and meet in the valley.
|
|
399
|
+
const f = edges[e.next];
|
|
400
|
+
if ((x - f.ax) * f.dx + (z - f.az) * f.dz < 0) {
|
|
401
|
+
const d0 = planeDist(s, x, z);
|
|
402
|
+
const d1 = planeDist(e.next, x, z);
|
|
403
|
+
const d = Math.max(d0, d1, 0);
|
|
404
|
+
if (d < best - 1e-7) {
|
|
405
|
+
best = d;
|
|
406
|
+
label = d0 >= d1 ? s : e.next;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (best === Infinity) {
|
|
412
|
+
// The point fell into no strip (numerical bad luck at an almost straight corner): plain distance to the edges.
|
|
413
|
+
for (let s = 0; s < edges.length; s++) {
|
|
414
|
+
const e = edges[s];
|
|
415
|
+
const d = distToSegment(x, z, e.ax, e.az, e.ax + e.dx * e.len, e.az + e.dz * e.len);
|
|
416
|
+
if (d < best) {
|
|
417
|
+
best = d;
|
|
418
|
+
label = s;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return [best, label];
|
|
423
|
+
};
|
|
424
|
+
// Shared vertices so the edge splitting connects between neighbouring triangles.
|
|
425
|
+
const vx = [];
|
|
426
|
+
const vz = [];
|
|
427
|
+
for (let i = 0; i < tri.coords.length; i += 2) {
|
|
428
|
+
vx.push(tri.coords[i]);
|
|
429
|
+
vz.push(tri.coords[i + 1]);
|
|
430
|
+
}
|
|
431
|
+
let idx = tri.indices.slice();
|
|
432
|
+
const edgeKey = (a, b) => (a < b ? a * 1048576 + b : b * 1048576 + a);
|
|
433
|
+
// 1. Refinement: every edge longer than the limit is halved, while any remain. A round that would leave more
|
|
434
|
+
// triangles than the budget is dropped (its new vertices too) and refinement stops there.
|
|
435
|
+
for (let iter = 0; iter < 10; iter++) {
|
|
436
|
+
const verticesBefore = vx.length;
|
|
437
|
+
const mids = new Map();
|
|
438
|
+
const mid = (a, b) => {
|
|
439
|
+
if (Math.hypot(vx[a] - vx[b], vz[a] - vz[b]) <= FIELD_EDGE_M)
|
|
440
|
+
return -1;
|
|
441
|
+
const k = edgeKey(a, b);
|
|
442
|
+
let m = mids.get(k);
|
|
443
|
+
if (m === undefined) {
|
|
444
|
+
m = vx.length;
|
|
445
|
+
vx.push((vx[a] + vx[b]) / 2);
|
|
446
|
+
vz.push((vz[a] + vz[b]) / 2);
|
|
447
|
+
mids.set(k, m);
|
|
448
|
+
}
|
|
449
|
+
return m;
|
|
450
|
+
};
|
|
451
|
+
const next = [];
|
|
452
|
+
for (let t = 0; t < idx.length; t += 3) {
|
|
453
|
+
const v = [idx[t], idx[t + 1], idx[t + 2]];
|
|
454
|
+
const m = [mid(v[0], v[1]), mid(v[1], v[2]), mid(v[2], v[0])];
|
|
455
|
+
const count = m.filter((x) => x >= 0).length;
|
|
456
|
+
if (count === 0)
|
|
457
|
+
next.push(v[0], v[1], v[2]);
|
|
458
|
+
else if (count === 3) {
|
|
459
|
+
next.push(v[0], m[0], m[2], m[0], v[1], m[1], m[2], m[1], v[2], m[0], m[1], m[2]);
|
|
460
|
+
}
|
|
461
|
+
else if (count === 1) {
|
|
462
|
+
const k = m.findIndex((x) => x >= 0);
|
|
463
|
+
const a = v[k];
|
|
464
|
+
const b = v[(k + 1) % 3];
|
|
465
|
+
const c = v[(k + 2) % 3];
|
|
466
|
+
next.push(a, m[k], c, m[k], b, c);
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
// Two split edges: k is the unsplit one, the opposite vertex is between two midpoints.
|
|
470
|
+
const k = m.findIndex((x) => x < 0);
|
|
471
|
+
const a = v[k];
|
|
472
|
+
const b = v[(k + 1) % 3];
|
|
473
|
+
const c = v[(k + 2) % 3];
|
|
474
|
+
const mbc = m[(k + 1) % 3];
|
|
475
|
+
const mca = m[(k + 2) % 3];
|
|
476
|
+
next.push(mca, mbc, c, a, b, mbc, a, mbc, mca);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
if (next.length / 3 > maxTriangles) {
|
|
480
|
+
vx.length = verticesBefore;
|
|
481
|
+
vz.length = verticesBefore;
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
idx = next;
|
|
485
|
+
if (mids.size === 0)
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
// 2. Distance and nearest edge for each vertex.
|
|
489
|
+
const dist = [];
|
|
490
|
+
const label = [];
|
|
491
|
+
for (let i = 0; i < vx.length; i++) {
|
|
492
|
+
const [d, l] = nearest(vx[i], vz[i]);
|
|
493
|
+
dist.push(d);
|
|
494
|
+
label.push(l);
|
|
495
|
+
}
|
|
496
|
+
const addVertex = (x, z) => {
|
|
497
|
+
const [d, l] = nearest(x, z);
|
|
498
|
+
vx.push(x);
|
|
499
|
+
vz.push(z);
|
|
500
|
+
dist.push(d);
|
|
501
|
+
label.push(l);
|
|
502
|
+
return vx.length - 1;
|
|
503
|
+
};
|
|
504
|
+
// 3. Break: on an edge whose ends are determined by different planes, find the point where both planes are at the same height.
|
|
505
|
+
const cuts = new Map();
|
|
506
|
+
const cut = (a, b) => {
|
|
507
|
+
const k = edgeKey(a, b);
|
|
508
|
+
const known = cuts.get(k);
|
|
509
|
+
if (known !== undefined)
|
|
510
|
+
return known;
|
|
511
|
+
const [p, q] = a < b ? [a, b] : [b, a];
|
|
512
|
+
// The difference of distances from both planes is linear along the edge, so the intersection can be computed directly.
|
|
513
|
+
const g0 = planeDist(label[p], vx[p], vz[p]) - planeDist(label[q], vx[p], vz[p]);
|
|
514
|
+
const g1 = planeDist(label[p], vx[q], vz[q]) - planeDist(label[q], vx[q], vz[q]);
|
|
515
|
+
const t = g0 * g1 < 0 ? g0 / (g0 - g1) : 0.5;
|
|
516
|
+
const m = addVertex(vx[p] + (vx[q] - vx[p]) * t, vz[p] + (vz[q] - vz[p]) * t);
|
|
517
|
+
cuts.set(k, m);
|
|
518
|
+
return m;
|
|
519
|
+
};
|
|
520
|
+
const out = [];
|
|
521
|
+
for (let t = 0; t < idx.length; t += 3) {
|
|
522
|
+
const a = idx[t];
|
|
523
|
+
const b = idx[t + 1];
|
|
524
|
+
const c = idx[t + 2];
|
|
525
|
+
const la = label[a];
|
|
526
|
+
const lb = label[b];
|
|
527
|
+
const lc = label[c];
|
|
528
|
+
if (la === lb && lb === lc) {
|
|
529
|
+
out.push(a, b, c);
|
|
530
|
+
}
|
|
531
|
+
else if (la !== lb && lb !== lc && la !== lc) {
|
|
532
|
+
const mab = cut(a, b);
|
|
533
|
+
const mbc = cut(b, c);
|
|
534
|
+
const mca = cut(c, a);
|
|
535
|
+
const m = addVertex((vx[mab] + vx[mbc] + vx[mca]) / 3, (vz[mab] + vz[mbc] + vz[mca]) / 3);
|
|
536
|
+
out.push(a, mab, m, mab, b, m, b, mbc, m, mbc, c, m, c, mca, m, mca, a, m);
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
// One vertex has a different nearest edge than the other two. Rotate so it comes first.
|
|
540
|
+
const [p, q, r] = la !== lb && la !== lc ? [a, b, c] : lb !== la && lb !== lc ? [b, c, a] : [c, a, b];
|
|
541
|
+
const mpq = cut(p, q);
|
|
542
|
+
const mrp = cut(r, p);
|
|
543
|
+
out.push(p, mpq, mrp, mpq, q, r, mpq, r, mrp);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
let dMax = 0;
|
|
547
|
+
for (const d of dist)
|
|
548
|
+
dMax = Math.max(dMax, d);
|
|
549
|
+
if (dMax < 0.5)
|
|
550
|
+
return null;
|
|
551
|
+
const { eaveY, roofHeight } = resolvePitch(pitch, dMax);
|
|
552
|
+
const slope = roofHeight / dMax;
|
|
553
|
+
const tris = [];
|
|
554
|
+
for (let t = 0; t < out.length; t += 3) {
|
|
555
|
+
const a = out[t];
|
|
556
|
+
let b = out[t + 1];
|
|
557
|
+
let c = out[t + 2];
|
|
558
|
+
const area2 = (vx[b] - vx[a]) * (vz[c] - vz[a]) - (vz[b] - vz[a]) * (vx[c] - vx[a]);
|
|
559
|
+
if (Math.abs(area2) < 1e-5)
|
|
560
|
+
continue;
|
|
561
|
+
if (area2 > 0)
|
|
562
|
+
[b, c] = [c, b];
|
|
563
|
+
for (const i of [a, b, c])
|
|
564
|
+
tris.push(vx[i], eaveY + slope * dist[i], vz[i]);
|
|
565
|
+
}
|
|
566
|
+
// Walls are closed up to the roof height above the outline: after outline simplification it is not exactly at the eave everywhere.
|
|
567
|
+
return { tris, topAt: (x, z) => eaveY + slope * nearest(x, z)[0], creases: [], eaveY, tier: 2 };
|
|
568
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { MaterialDesc, Uniforms } from '../../base/index.ts';
|
|
2
|
+
import type { StructureShaderOptions } from './building-shader.ts';
|
|
3
|
+
import type { ArchShadeDims } from './dims.ts';
|
|
4
|
+
export declare const DEPTH_FS = "#version 300 es\nprecision highp float;\nvoid main() {}";
|
|
5
|
+
/** Palette rows the structure shader takes from outside. */
|
|
6
|
+
export interface StructureRows {
|
|
7
|
+
glass: number;
|
|
8
|
+
glassLit: number;
|
|
9
|
+
plinth: number;
|
|
10
|
+
}
|
|
11
|
+
/** Name suffix of a shader variant: `-framed`, `-finishes`, both, or nothing. */
|
|
12
|
+
export declare function variantSuffix(o: StructureShaderOptions): string;
|
|
13
|
+
/**
|
|
14
|
+
* Material of the structure mesh. `damage: true` gives the variant that shades the vertex damage channel (cracks,
|
|
15
|
+
* breaches, cut faces, rubble); draw damaged structures and rubble with it and keep intact ones on the plain one.
|
|
16
|
+
* `framed` draws the mesh in its own frame (uniform `u_frame`, set per draw), `finishes` draws wall finishes, roof
|
|
17
|
+
* covers and shutters (see `StructureShaderOptions`); each variant has its own name.
|
|
18
|
+
*/
|
|
19
|
+
export declare function structureMaterial(dims: ArchShadeDims, options?: {
|
|
20
|
+
damage?: boolean;
|
|
21
|
+
} & StructureShaderOptions): MaterialDesc;
|
|
22
|
+
/** Uniforms of the structure material. */
|
|
23
|
+
export declare function structureUniforms(rows: StructureRows): Uniforms;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Material of the structure mesh: lit in the colour pass, casts shadows with the plain depth program (the damage
|
|
2
|
+
// variant with a depth program that also grows rubble in). The material of falling parts is in
|
|
3
|
+
// ../damage/fall-material.ts and shares the plain depth fragment program.
|
|
4
|
+
import { BUILDING_ATTRIBS } from './buildings.js';
|
|
5
|
+
import { buildingFs, buildingVs } from './building-shader.js';
|
|
6
|
+
/** Rubble that is still arriving grows up from its base (writer.ts, growIn); the same rule as BUILDING_VS. */
|
|
7
|
+
const GROW_DEPTH_VS = /* glsl */ `#include <header>
|
|
8
|
+
uniform mat4 u_lightRender;
|
|
9
|
+
in vec3 a_pos;
|
|
10
|
+
in vec4 a_fac;
|
|
11
|
+
void main() {
|
|
12
|
+
vec3 pos = a_pos;
|
|
13
|
+
if (a_fac.y < 0.0) {
|
|
14
|
+
float g = clamp((u_time.x - a_fac.x) / -a_fac.y, 0.0, 1.0);
|
|
15
|
+
pos.y = mix(a_fac.z, a_pos.y, g * g * (3.0 - 2.0 * g));
|
|
16
|
+
}
|
|
17
|
+
gl_Position = u_lightRender * vec4(pos, 1.0);
|
|
18
|
+
}`;
|
|
19
|
+
export const DEPTH_FS = /* glsl */ `#version 300 es
|
|
20
|
+
precision highp float;
|
|
21
|
+
void main() {}`;
|
|
22
|
+
/** The plain depth stage with the frame of the mesh (`u_frame`, see `StructureShaderOptions`). */
|
|
23
|
+
const FRAMED_DEPTH_VS = /* glsl */ `#include <header>
|
|
24
|
+
uniform mat4 u_lightRender;
|
|
25
|
+
uniform mat4 u_frame;
|
|
26
|
+
in vec3 a_pos;
|
|
27
|
+
void main() {
|
|
28
|
+
gl_Position = u_lightRender * (u_frame * vec4(a_pos, 1.0));
|
|
29
|
+
}`;
|
|
30
|
+
/** GROW_DEPTH_VS with the frame of the mesh. */
|
|
31
|
+
const FRAMED_GROW_DEPTH_VS = GROW_DEPTH_VS.replace('in vec3 a_pos;\n', 'uniform mat4 u_frame;\nin vec3 a_pos;\n').replace('gl_Position = u_lightRender * vec4(pos, 1.0);', 'gl_Position = u_lightRender * (u_frame * vec4(pos, 1.0));');
|
|
32
|
+
/** Name suffix of a shader variant: `-framed`, `-finishes`, both, or nothing. */
|
|
33
|
+
export function variantSuffix(o) {
|
|
34
|
+
return `${o.framed ? '-framed' : ''}${o.finishes ? '-finishes' : ''}`;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Material of the structure mesh. `damage: true` gives the variant that shades the vertex damage channel (cracks,
|
|
38
|
+
* breaches, cut faces, rubble); draw damaged structures and rubble with it and keep intact ones on the plain one.
|
|
39
|
+
* `framed` draws the mesh in its own frame (uniform `u_frame`, set per draw), `finishes` draws wall finishes, roof
|
|
40
|
+
* covers and shutters (see `StructureShaderOptions`); each variant has its own name.
|
|
41
|
+
*/
|
|
42
|
+
export function structureMaterial(dims, options = {}) {
|
|
43
|
+
const damage = options.damage ?? false;
|
|
44
|
+
const o = { framed: options.framed === true, finishes: options.finishes === true };
|
|
45
|
+
const shadow = damage ? { vs: o.framed ? FRAMED_GROW_DEPTH_VS : GROW_DEPTH_VS, fs: DEPTH_FS } : o.framed ? { vs: FRAMED_DEPTH_VS, fs: DEPTH_FS } : 'depth';
|
|
46
|
+
return {
|
|
47
|
+
name: (damage ? 'structures-damaged' : 'structures') + variantSuffix(o),
|
|
48
|
+
attribs: BUILDING_ATTRIBS,
|
|
49
|
+
passes: { shadow, color: { vs: buildingVs(o), fs: buildingFs(dims, damage, o) } },
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** Uniforms of the structure material. */
|
|
53
|
+
export function structureUniforms(rows) {
|
|
54
|
+
return { u_rows: { ivec4: [rows.glass, rows.glassLit, rows.plinth, 0] } };
|
|
55
|
+
}
|