wallhammer-web 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Surface mesh of one cell, sampled from a function of world coordinates. Content is a function of the world, not of
|
|
2
|
+
// the cell it is generated in: heights are sampled at world points computed in double, and normals come from a grid
|
|
3
|
+
// with a one-sample margin around the cell, so the border vertices of two neighbouring cells carry identical
|
|
4
|
+
// heights, normals and colours. A skirt hangs from the perimeter and hides the cracks against a coarser neighbour.
|
|
5
|
+
/** Locations a material should give the attributes. */
|
|
6
|
+
export const CELL_SURFACE_ATTRIBS = { a_pos: 0, a_normal: 1, a_color: 2 };
|
|
7
|
+
/**
|
|
8
|
+
* Position 3× float relative to the cell origin; normal 3× int16 (×32767) and the kind (0 surface, 1 skirt) as the
|
|
9
|
+
* fourth int16; colour 4× u8 normalized (the alpha channel is free for the content: a surface class or a flag).
|
|
10
|
+
*/
|
|
11
|
+
export const CELL_SURFACE_LAYOUT = {
|
|
12
|
+
stride: 24,
|
|
13
|
+
attribs: [
|
|
14
|
+
{ name: 'a_pos', size: 3, type: 'f32', offset: 0 },
|
|
15
|
+
{ name: 'a_normal', size: 4, type: 'i16', offset: 12 },
|
|
16
|
+
{ name: 'a_color', size: 4, type: 'u8', normalized: true, offset: 20 },
|
|
17
|
+
],
|
|
18
|
+
};
|
|
19
|
+
const KIND_TOP = 0;
|
|
20
|
+
const KIND_SKIRT = 1;
|
|
21
|
+
export function cellSurfaceMesh(o) {
|
|
22
|
+
const q = o.quads;
|
|
23
|
+
const n = q + 1;
|
|
24
|
+
const step = o.size / q;
|
|
25
|
+
// Heights on (n + 2)² samples: one sample of margin on each side for the central differences of the normals.
|
|
26
|
+
const m = n + 2;
|
|
27
|
+
const h = new Float64Array(m * m);
|
|
28
|
+
for (let j = 0; j < m; j++) {
|
|
29
|
+
const z = o.originZ + (j - 1) * step;
|
|
30
|
+
for (let i = 0; i < m; i++)
|
|
31
|
+
h[j * m + i] = o.heightAt(o.originX + (i - 1) * step, z);
|
|
32
|
+
}
|
|
33
|
+
const H = (i, j) => h[(j + 1) * m + (i + 1)];
|
|
34
|
+
const skirt = o.skirtDepth > 0;
|
|
35
|
+
const vertCount = n * n + (skirt ? 8 * n : 0);
|
|
36
|
+
const STRIDE = CELL_SURFACE_LAYOUT.stride;
|
|
37
|
+
const buf = new ArrayBuffer(vertCount * STRIDE);
|
|
38
|
+
const view = new DataView(buf);
|
|
39
|
+
const normals = new Int16Array(n * n * 3);
|
|
40
|
+
const colors = new Uint8Array(n * n * 4);
|
|
41
|
+
let minY = Infinity;
|
|
42
|
+
let maxY = -Infinity;
|
|
43
|
+
const put = (v, x, y, z, k, kind) => {
|
|
44
|
+
const off = v * STRIDE;
|
|
45
|
+
view.setFloat32(off, x, true);
|
|
46
|
+
view.setFloat32(off + 4, y, true);
|
|
47
|
+
view.setFloat32(off + 8, z, true);
|
|
48
|
+
view.setInt16(off + 12, normals[k * 3], true);
|
|
49
|
+
view.setInt16(off + 14, normals[k * 3 + 1], true);
|
|
50
|
+
view.setInt16(off + 16, normals[k * 3 + 2], true);
|
|
51
|
+
view.setInt16(off + 18, kind, true);
|
|
52
|
+
for (let c = 0; c < 4; c++)
|
|
53
|
+
view.setUint8(off + 20 + c, colors[k * 4 + c]);
|
|
54
|
+
};
|
|
55
|
+
for (let j = 0; j < n; j++) {
|
|
56
|
+
for (let i = 0; i < n; i++) {
|
|
57
|
+
const y = H(i, j);
|
|
58
|
+
const dx = (H(i + 1, j) - H(i - 1, j)) / (2 * step);
|
|
59
|
+
const dz = (H(i, j + 1) - H(i, j - 1)) / (2 * step);
|
|
60
|
+
// Square root is exact in IEEE 754; no transcendental function enters the output.
|
|
61
|
+
const l = Math.sqrt(dx * dx + 1 + dz * dz);
|
|
62
|
+
const nx = -dx / l;
|
|
63
|
+
const ny = 1 / l;
|
|
64
|
+
const nz = -dz / l;
|
|
65
|
+
const k = j * n + i;
|
|
66
|
+
normals[k * 3] = Math.round(nx * 32767);
|
|
67
|
+
normals[k * 3 + 1] = Math.round(ny * 32767);
|
|
68
|
+
normals[k * 3 + 2] = Math.round(nz * 32767);
|
|
69
|
+
const c = o.colorAt ? o.colorAt(o.originX + i * step, o.originZ + j * step, y, nx, ny, nz) : [128, 128, 128, 255];
|
|
70
|
+
for (let ch = 0; ch < 4; ch++)
|
|
71
|
+
colors[k * 4 + ch] = c[ch];
|
|
72
|
+
put(k, i * step, y, j * step, k, KIND_TOP);
|
|
73
|
+
const yf = Math.fround(y);
|
|
74
|
+
if (yf < minY)
|
|
75
|
+
minY = yf;
|
|
76
|
+
if (yf > maxY)
|
|
77
|
+
maxY = yf;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const quadCount = q * q + (skirt ? 4 * q : 0);
|
|
81
|
+
const big = vertCount > 65535;
|
|
82
|
+
const idx = big ? new Uint32Array(quadCount * 6) : new Uint16Array(quadCount * 6);
|
|
83
|
+
let w = 0;
|
|
84
|
+
for (let j = 0; j < q; j++) {
|
|
85
|
+
for (let i = 0; i < q; i++) {
|
|
86
|
+
const a = j * n + i;
|
|
87
|
+
const b = a + 1;
|
|
88
|
+
const c = a + n;
|
|
89
|
+
const d = c + 1;
|
|
90
|
+
idx[w++] = a;
|
|
91
|
+
idx[w++] = c;
|
|
92
|
+
idx[w++] = b;
|
|
93
|
+
idx[w++] = b;
|
|
94
|
+
idx[w++] = c;
|
|
95
|
+
idx[w++] = d;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (skirt) {
|
|
99
|
+
// Four sides, each a strip of vertex pairs: the border vertex (same normal and colour) and one below it.
|
|
100
|
+
let v = n * n;
|
|
101
|
+
const side = (at, flip) => {
|
|
102
|
+
const start = v;
|
|
103
|
+
for (let k = 0; k < n; k++) {
|
|
104
|
+
const [i, j] = at(k);
|
|
105
|
+
const kk = j * n + i;
|
|
106
|
+
const y = H(i, j);
|
|
107
|
+
put(v++, i * step, y, j * step, kk, KIND_SKIRT);
|
|
108
|
+
put(v++, i * step, y - o.skirtDepth, j * step, kk, KIND_SKIRT);
|
|
109
|
+
}
|
|
110
|
+
for (let k = 0; k < q; k++) {
|
|
111
|
+
const t0 = start + k * 2;
|
|
112
|
+
const b0 = t0 + 1;
|
|
113
|
+
const t1 = t0 + 2;
|
|
114
|
+
const b1 = t0 + 3;
|
|
115
|
+
if (flip) {
|
|
116
|
+
idx[w++] = t0;
|
|
117
|
+
idx[w++] = t1;
|
|
118
|
+
idx[w++] = b0;
|
|
119
|
+
idx[w++] = t1;
|
|
120
|
+
idx[w++] = b1;
|
|
121
|
+
idx[w++] = b0;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
idx[w++] = t0;
|
|
125
|
+
idx[w++] = b0;
|
|
126
|
+
idx[w++] = t1;
|
|
127
|
+
idx[w++] = t1;
|
|
128
|
+
idx[w++] = b0;
|
|
129
|
+
idx[w++] = b1;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
side((k) => [k, 0], true); // north
|
|
134
|
+
side((k) => [k, q], false); // south
|
|
135
|
+
side((k) => [0, k], false); // west
|
|
136
|
+
side((k) => [q, k], true); // east
|
|
137
|
+
minY -= o.skirtDepth;
|
|
138
|
+
}
|
|
139
|
+
return { mesh: { layout: CELL_SURFACE_LAYOUT, vertices: new Uint8Array(buf), indices: idx, count: w }, minY, maxY };
|
|
140
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { MeshData } from '../base/data.ts';
|
|
2
|
+
/** One chunk: the merged triangles of one cell and the box that holds them. */
|
|
3
|
+
export interface MeshChunk {
|
|
4
|
+
/** The cell key (`ChunkOptions.cellKey`). */
|
|
5
|
+
key: string;
|
|
6
|
+
min: [number, number, number];
|
|
7
|
+
max: [number, number, number];
|
|
8
|
+
data: MeshData;
|
|
9
|
+
triangles: number;
|
|
10
|
+
}
|
|
11
|
+
export interface ChunkOptions {
|
|
12
|
+
/** Edge of the square cells on x and z, metres. Default 64. Ignored when `cellKey` is given. */
|
|
13
|
+
cellM?: number;
|
|
14
|
+
/** Cell of a triangle centroid, as a key; cells may differ in size (for example finer near the middle). */
|
|
15
|
+
cellKey?: (x: number, y: number, z: number) => string;
|
|
16
|
+
/** Name of the position attribute (three `f32`). Default `'a_pos'`. */
|
|
17
|
+
position?: string;
|
|
18
|
+
/** Margin added to every side of a chunk box, metres (for vertex motion in the shader, normal offsets). Default 0. */
|
|
19
|
+
padM?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Cuts meshes into chunks by triangle centroid and merges each cell. All meshes must share one vertex layout (same
|
|
23
|
+
* stride and attributes) and be triangle lists. Chunks come in the order their cells were first met.
|
|
24
|
+
*/
|
|
25
|
+
export declare function chunkMeshes(meshes: readonly MeshData[], o?: ChunkOptions): MeshChunk[];
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Cutting meshes into spatial chunks: every triangle goes to the cell of its centroid, the triangles of one cell from
|
|
2
|
+
// all meshes are merged into one mesh with its own box. A static scene built as many separate meshes then becomes
|
|
3
|
+
// one draw per visible cell (`PieceLayer`), culled per box against the view and the light.
|
|
4
|
+
//
|
|
5
|
+
// Any interleaved vertex layout works, as long as all meshes share it and positions are three floats; whole vertex
|
|
6
|
+
// records are copied byte for byte. A vertex used by triangles in two cells is copied into both.
|
|
7
|
+
/** A byte buffer that grows by doubling. */
|
|
8
|
+
class Bytes {
|
|
9
|
+
buf = new Uint8Array(1024);
|
|
10
|
+
length = 0;
|
|
11
|
+
push(src, at, n) {
|
|
12
|
+
if (this.length + n > this.buf.length) {
|
|
13
|
+
let size = this.buf.length * 2;
|
|
14
|
+
while (size < this.length + n)
|
|
15
|
+
size *= 2;
|
|
16
|
+
const next = new Uint8Array(size);
|
|
17
|
+
next.set(this.buf.subarray(0, this.length));
|
|
18
|
+
this.buf = next;
|
|
19
|
+
}
|
|
20
|
+
this.buf.set(src.subarray(at, at + n), this.length);
|
|
21
|
+
this.length += n;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Cuts meshes into chunks by triangle centroid and merges each cell. All meshes must share one vertex layout (same
|
|
26
|
+
* stride and attributes) and be triangle lists. Chunks come in the order their cells were first met.
|
|
27
|
+
*/
|
|
28
|
+
export function chunkMeshes(meshes, o = {}) {
|
|
29
|
+
const cellM = o.cellM ?? 64;
|
|
30
|
+
const keyOf = o.cellKey ?? ((x, _y, z) => `${Math.floor(x / cellM)},${Math.floor(z / cellM)}`);
|
|
31
|
+
const posName = o.position ?? 'a_pos';
|
|
32
|
+
const pad = o.padM ?? 0;
|
|
33
|
+
const first = meshes.find((m) => m.count > 0);
|
|
34
|
+
if (!first)
|
|
35
|
+
return [];
|
|
36
|
+
const layout = first.layout;
|
|
37
|
+
const stride = layout.stride;
|
|
38
|
+
const pos = layout.attribs.find((a) => a.name === posName);
|
|
39
|
+
if (!pos || pos.type !== 'f32' || pos.size < 3)
|
|
40
|
+
throw new Error(`chunkMeshes: position attribute ${posName} must be three f32`);
|
|
41
|
+
const acc = new Map();
|
|
42
|
+
for (const m of meshes) {
|
|
43
|
+
if (m.count === 0)
|
|
44
|
+
continue;
|
|
45
|
+
if (m.layout.stride !== stride || !sameAttribs(m, first))
|
|
46
|
+
throw new Error('chunkMeshes: all meshes must share one vertex layout');
|
|
47
|
+
if ((m.primitive ?? 'triangles') !== 'triangles')
|
|
48
|
+
throw new Error('chunkMeshes: only triangle lists can be chunked');
|
|
49
|
+
const bytes = new Uint8Array(m.vertices.buffer, m.vertices.byteOffset, m.vertices.byteLength);
|
|
50
|
+
const view = new DataView(m.vertices.buffer, m.vertices.byteOffset, m.vertices.byteLength);
|
|
51
|
+
const px = (v, k) => view.getFloat32(v * stride + pos.offset + k * 4, true);
|
|
52
|
+
const ind = m.indices;
|
|
53
|
+
const at = (t) => (ind ? ind[t] : t);
|
|
54
|
+
for (let t = 0; t + 2 < m.count; t += 3) {
|
|
55
|
+
const a = at(t), b = at(t + 1), c = at(t + 2);
|
|
56
|
+
const cx = (px(a, 0) + px(b, 0) + px(c, 0)) / 3;
|
|
57
|
+
const cy = (px(a, 1) + px(b, 1) + px(c, 1)) / 3;
|
|
58
|
+
const cz = (px(a, 2) + px(b, 2) + px(c, 2)) / 3;
|
|
59
|
+
const key = keyOf(cx, cy, cz);
|
|
60
|
+
let ch = acc.get(key);
|
|
61
|
+
if (!ch) {
|
|
62
|
+
ch = { bytes: new Bytes(), idx: [], vertices: 0, min: [Infinity, Infinity, Infinity], max: [-Infinity, -Infinity, -Infinity], remap: new Map(), source: null };
|
|
63
|
+
acc.set(key, ch);
|
|
64
|
+
}
|
|
65
|
+
if (ch.source !== m) {
|
|
66
|
+
ch.source = m;
|
|
67
|
+
ch.remap = new Map();
|
|
68
|
+
}
|
|
69
|
+
for (const v of [a, b, c]) {
|
|
70
|
+
let i = ch.remap.get(v);
|
|
71
|
+
if (i === undefined) {
|
|
72
|
+
i = ch.vertices++;
|
|
73
|
+
ch.remap.set(v, i);
|
|
74
|
+
ch.bytes.push(bytes, v * stride, stride);
|
|
75
|
+
for (let k = 0; k < 3; k++) {
|
|
76
|
+
const p = px(v, k);
|
|
77
|
+
if (p < ch.min[k])
|
|
78
|
+
ch.min[k] = p;
|
|
79
|
+
if (p > ch.max[k])
|
|
80
|
+
ch.max[k] = p;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
ch.idx.push(i);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const out = [];
|
|
88
|
+
for (const [key, ch] of acc) {
|
|
89
|
+
const vertices = ch.bytes.buf.slice(0, ch.bytes.length);
|
|
90
|
+
const indices = ch.vertices <= 65536 ? new Uint16Array(ch.idx) : new Uint32Array(ch.idx);
|
|
91
|
+
for (let k = 0; k < 3; k++) {
|
|
92
|
+
ch.min[k] = ch.min[k] - pad;
|
|
93
|
+
ch.max[k] = ch.max[k] + pad;
|
|
94
|
+
}
|
|
95
|
+
out.push({ key, min: ch.min, max: ch.max, data: { layout, vertices, indices, count: ch.idx.length }, triangles: ch.idx.length / 3 });
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
function sameAttribs(a, b) {
|
|
100
|
+
if (a.layout === b.layout)
|
|
101
|
+
return true;
|
|
102
|
+
const x = a.layout.attribs;
|
|
103
|
+
const y = b.layout.attribs;
|
|
104
|
+
if (x.length !== y.length)
|
|
105
|
+
return false;
|
|
106
|
+
for (let i = 0; i < x.length; i++) {
|
|
107
|
+
const p = x[i], q = y[i];
|
|
108
|
+
if (p.name !== q.name || p.size !== q.size || p.type !== q.type || p.offset !== q.offset || !!p.normalized !== !!q.normalized || !!p.integer !== !!q.integer)
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { MeshData, VertexLayout } from '../base/data.ts';
|
|
2
|
+
import type { Vec3 } from '../base/math.ts';
|
|
3
|
+
import type { HeightField } from '../gen-core/height-field.ts';
|
|
4
|
+
/** Position 3× float, attribute `a_pos`. */
|
|
5
|
+
export declare const DRAPE_LAYOUT: VertexLayout;
|
|
6
|
+
/** Attribute location a material should give the sheet. */
|
|
7
|
+
export declare const DRAPE_ATTRIBS: {
|
|
8
|
+
readonly a_pos: 0;
|
|
9
|
+
};
|
|
10
|
+
export interface DrapeOptions {
|
|
11
|
+
/** Rectangle to cover, in the field's own coordinates (x from 0 east, z from 0 south). Clipped to the field. Default: all of it. */
|
|
12
|
+
rect?: {
|
|
13
|
+
x0: number;
|
|
14
|
+
z0: number;
|
|
15
|
+
x1: number;
|
|
16
|
+
z1: number;
|
|
17
|
+
};
|
|
18
|
+
/** Lift above the surface in metres. Default 0.15. */
|
|
19
|
+
liftM?: number;
|
|
20
|
+
/** Quads per tile edge, at most 255 (a tile's indices are 16-bit). Default 64. */
|
|
21
|
+
tileQuads?: number;
|
|
22
|
+
/** Use every n-th vertex of the field. Default 1 (the field's own density). */
|
|
23
|
+
every?: number;
|
|
24
|
+
/** Where the field's origin lies in the coordinates the sheet is drawn in. Default [0, 0]. */
|
|
25
|
+
origin?: readonly [number, number];
|
|
26
|
+
}
|
|
27
|
+
/** One tile of the sheet and the box that holds it. */
|
|
28
|
+
export interface DrapeTile {
|
|
29
|
+
mesh: MeshData;
|
|
30
|
+
min: Vec3;
|
|
31
|
+
max: Vec3;
|
|
32
|
+
}
|
|
33
|
+
/** The smallest `every` with which a sheet over `rect` (default the whole field) has at most `maxVertices` vertices. */
|
|
34
|
+
export declare function drapeEvery(field: HeightField, maxVertices: number, rect?: DrapeOptions['rect']): number;
|
|
35
|
+
/** Tiles of a sheet over the field. An empty list when the rectangle misses the field. */
|
|
36
|
+
export declare function drapeTiles(field: HeightField, o?: DrapeOptions): DrapeTile[];
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// A sheet laid on a height field, in tiles: the surface of the field again, lifted a little, with positions only.
|
|
2
|
+
// A layer that shows something on the ground (a raster of values, a mask) draws it over the scene. At full density
|
|
3
|
+
// the sheet has the field's own vertices and triangles, so it follows the drawn surface exactly; a thinned sheet
|
|
4
|
+
// takes the highest height around each vertex, so it never sinks below the surface.
|
|
5
|
+
/** Position 3× float, attribute `a_pos`. */
|
|
6
|
+
export const DRAPE_LAYOUT = { stride: 12, attribs: [{ name: 'a_pos', size: 3, type: 'f32', offset: 0 }] };
|
|
7
|
+
/** Attribute location a material should give the sheet. */
|
|
8
|
+
export const DRAPE_ATTRIBS = { a_pos: 0 };
|
|
9
|
+
/** The smallest `every` with which a sheet over `rect` (default the whole field) has at most `maxVertices` vertices. */
|
|
10
|
+
export function drapeEvery(field, maxVertices, rect) {
|
|
11
|
+
const w = rect ? Math.min(field.sizeX, rect.x1) - Math.max(0, rect.x0) : field.sizeX;
|
|
12
|
+
const d = rect ? Math.min(field.sizeZ, rect.z1) - Math.max(0, rect.z0) : field.sizeZ;
|
|
13
|
+
const full = (Math.max(0, w) / field.step + 1) * (Math.max(0, d) / field.step + 1);
|
|
14
|
+
return Math.max(1, Math.ceil(Math.sqrt(full / Math.max(1, maxVertices))));
|
|
15
|
+
}
|
|
16
|
+
/** Tiles of a sheet over the field. An empty list when the rectangle misses the field. */
|
|
17
|
+
export function drapeTiles(field, o = {}) {
|
|
18
|
+
const { cols, rows, step, heights } = field;
|
|
19
|
+
const every = Math.max(1, Math.floor(o.every ?? 1));
|
|
20
|
+
const lift = o.liftM ?? 0.15;
|
|
21
|
+
const tileQuads = Math.max(1, Math.min(255, Math.floor(o.tileQuads ?? 64)));
|
|
22
|
+
const ox = o.origin?.[0] ?? 0;
|
|
23
|
+
const oz = o.origin?.[1] ?? 0;
|
|
24
|
+
const rect = o.rect ?? { x0: 0, z0: 0, x1: field.sizeX, z1: field.sizeZ };
|
|
25
|
+
// The sheet's own grid: vertex (ci, cj) stands on field vertex (ci · every, cj · every), the last one on the edge.
|
|
26
|
+
const lastCi = Math.ceil((cols - 1) / every);
|
|
27
|
+
const lastCj = Math.ceil((rows - 1) / every);
|
|
28
|
+
const cell = step * every;
|
|
29
|
+
const ci0 = Math.max(0, Math.min(lastCi, Math.floor(rect.x0 / cell)));
|
|
30
|
+
const cj0 = Math.max(0, Math.min(lastCj, Math.floor(rect.z0 / cell)));
|
|
31
|
+
const ci1 = Math.max(0, Math.min(lastCi, Math.ceil(rect.x1 / cell)));
|
|
32
|
+
const cj1 = Math.max(0, Math.min(lastCj, Math.ceil(rect.z1 / cell)));
|
|
33
|
+
if (ci1 <= ci0 || cj1 <= cj0 || rect.x1 <= 0 || rect.z1 <= 0 || rect.x0 >= field.sizeX || rect.z0 >= field.sizeZ)
|
|
34
|
+
return [];
|
|
35
|
+
const fi = (ci) => Math.min(cols - 1, ci * every);
|
|
36
|
+
const fj = (cj) => Math.min(rows - 1, cj * every);
|
|
37
|
+
const heightOf = (ci, cj) => {
|
|
38
|
+
const i = fi(ci);
|
|
39
|
+
const j = fj(cj);
|
|
40
|
+
if (every === 1)
|
|
41
|
+
return heights[j * cols + i];
|
|
42
|
+
// The highest field vertex this sheet vertex stands for (one sheet cell to every side).
|
|
43
|
+
let h = -Infinity;
|
|
44
|
+
for (let jj = Math.max(0, j - every); jj <= Math.min(rows - 1, j + every); jj++) {
|
|
45
|
+
for (let ii = Math.max(0, i - every); ii <= Math.min(cols - 1, i + every); ii++)
|
|
46
|
+
h = Math.max(h, heights[jj * cols + ii]);
|
|
47
|
+
}
|
|
48
|
+
return h;
|
|
49
|
+
};
|
|
50
|
+
const tiles = [];
|
|
51
|
+
for (let tj = cj0; tj < cj1; tj += tileQuads) {
|
|
52
|
+
for (let ti = ci0; ti < ci1; ti += tileQuads) {
|
|
53
|
+
const i1 = Math.min(ci1, ti + tileQuads);
|
|
54
|
+
const j1 = Math.min(cj1, tj + tileQuads);
|
|
55
|
+
const w = i1 - ti + 1;
|
|
56
|
+
const h = j1 - tj + 1;
|
|
57
|
+
const v = new Float32Array(w * h * 3);
|
|
58
|
+
let minY = Infinity;
|
|
59
|
+
let maxY = -Infinity;
|
|
60
|
+
for (let j = 0; j < h; j++) {
|
|
61
|
+
for (let i = 0; i < w; i++) {
|
|
62
|
+
const y = heightOf(ti + i, tj + j) + lift;
|
|
63
|
+
const k = (j * w + i) * 3;
|
|
64
|
+
v[k] = fi(ti + i) * step + ox;
|
|
65
|
+
v[k + 1] = y;
|
|
66
|
+
v[k + 2] = fj(tj + j) * step + oz;
|
|
67
|
+
if (y < minY)
|
|
68
|
+
minY = y;
|
|
69
|
+
if (y > maxY)
|
|
70
|
+
maxY = y;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// The same split of a cell as `heightFieldMesh` (the b–c diagonal), so the two surfaces agree.
|
|
74
|
+
const idx = new Uint16Array((w - 1) * (h - 1) * 6);
|
|
75
|
+
let n = 0;
|
|
76
|
+
for (let j = 0; j < h - 1; j++) {
|
|
77
|
+
for (let i = 0; i < w - 1; i++) {
|
|
78
|
+
const a = j * w + i;
|
|
79
|
+
const b = a + 1;
|
|
80
|
+
const c = a + w;
|
|
81
|
+
const d = c + 1;
|
|
82
|
+
idx[n++] = a;
|
|
83
|
+
idx[n++] = c;
|
|
84
|
+
idx[n++] = b;
|
|
85
|
+
idx[n++] = b;
|
|
86
|
+
idx[n++] = c;
|
|
87
|
+
idx[n++] = d;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
tiles.push({
|
|
91
|
+
mesh: { layout: DRAPE_LAYOUT, vertices: v, indices: idx, count: n },
|
|
92
|
+
min: [fi(ti) * step + ox, minY, fj(tj) * step + oz],
|
|
93
|
+
max: [fi(i1) * step + ox, maxY, fj(j1) * step + oz],
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return tiles;
|
|
98
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { MeshData, VertexLayout } from '../base/data.ts';
|
|
2
|
+
import type { HeightField } from '../gen-core/height-field.ts';
|
|
3
|
+
/** Attribute names and the locations a material should give them. */
|
|
4
|
+
export declare const HEIGHT_FIELD_ATTRIBS: {
|
|
5
|
+
readonly a_pos: 0;
|
|
6
|
+
readonly a_normal: 1;
|
|
7
|
+
};
|
|
8
|
+
/** Position 3× float; normal 3× int16 (not normalized, ×32767) and the kind (0 surface, 1 skirt) as the fourth int16. */
|
|
9
|
+
export declare const HEIGHT_FIELD_LAYOUT: VertexLayout;
|
|
10
|
+
/**
|
|
11
|
+
* Mesh of the surface and of the vertical cut down to level baseY. The normal has 16 bits: with eight, the slope of
|
|
12
|
+
* gentle hills changes in visible jumps.
|
|
13
|
+
*/
|
|
14
|
+
export declare function heightFieldMesh(field: HeightField, baseY: number): MeshData;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Mesh of a height field: the surface plus a vertical skirt along its perimeter down to a base level, so the field
|
|
2
|
+
// reads as a solid block when seen from the side.
|
|
3
|
+
/** Attribute names and the locations a material should give them. */
|
|
4
|
+
export const HEIGHT_FIELD_ATTRIBS = { a_pos: 0, a_normal: 1 };
|
|
5
|
+
/** Position 3× float; normal 3× int16 (not normalized, ×32767) and the kind (0 surface, 1 skirt) as the fourth int16. */
|
|
6
|
+
export const HEIGHT_FIELD_LAYOUT = {
|
|
7
|
+
stride: 20,
|
|
8
|
+
attribs: [
|
|
9
|
+
{ name: 'a_pos', size: 3, type: 'f32', offset: 0 },
|
|
10
|
+
{ name: 'a_normal', size: 4, type: 'i16', offset: 12 },
|
|
11
|
+
],
|
|
12
|
+
};
|
|
13
|
+
/** Kind of surface, stored in the fourth component of the normal. */
|
|
14
|
+
const KIND_TOP = 0;
|
|
15
|
+
const KIND_SKIRT = 1;
|
|
16
|
+
/**
|
|
17
|
+
* Mesh of the surface and of the vertical cut down to level baseY. The normal has 16 bits: with eight, the slope of
|
|
18
|
+
* gentle hills changes in visible jumps.
|
|
19
|
+
*/
|
|
20
|
+
export function heightFieldMesh(field, baseY) {
|
|
21
|
+
const { cols, rows, step, heights } = field;
|
|
22
|
+
const skirtVerts = 2 * (2 * cols + 2 * rows);
|
|
23
|
+
const vertCount = cols * rows + skirtVerts;
|
|
24
|
+
const STRIDE = HEIGHT_FIELD_LAYOUT.stride;
|
|
25
|
+
const buf = new ArrayBuffer(vertCount * STRIDE);
|
|
26
|
+
const view = new DataView(buf);
|
|
27
|
+
const put = (v, x, y, z, nx, ny, nz, kind) => {
|
|
28
|
+
const o = v * STRIDE;
|
|
29
|
+
view.setFloat32(o, x, true);
|
|
30
|
+
view.setFloat32(o + 4, y, true);
|
|
31
|
+
view.setFloat32(o + 8, z, true);
|
|
32
|
+
view.setInt16(o + 12, Math.round(nx * 32767), true);
|
|
33
|
+
view.setInt16(o + 14, Math.round(ny * 32767), true);
|
|
34
|
+
view.setInt16(o + 16, Math.round(nz * 32767), true);
|
|
35
|
+
view.setInt16(o + 18, kind, true);
|
|
36
|
+
};
|
|
37
|
+
const h = (i, j) => heights[Math.min(rows - 1, Math.max(0, j)) * cols + Math.min(cols - 1, Math.max(0, i))];
|
|
38
|
+
for (let j = 0; j < rows; j++) {
|
|
39
|
+
for (let i = 0; i < cols; i++) {
|
|
40
|
+
const [nx, ny, nz] = field.vertexNormal(i, j);
|
|
41
|
+
put(j * cols + i, i * step, h(i, j), j * step, nx, ny, nz, KIND_TOP);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const quadCount = (cols - 1) * (rows - 1) + 2 * (cols - 1) + 2 * (rows - 1);
|
|
45
|
+
const idx = new Uint32Array(quadCount * 6);
|
|
46
|
+
let n = 0;
|
|
47
|
+
for (let j = 0; j < rows - 1; j++) {
|
|
48
|
+
for (let i = 0; i < cols - 1; i++) {
|
|
49
|
+
const a = j * cols + i;
|
|
50
|
+
const b = a + 1;
|
|
51
|
+
const c = a + cols;
|
|
52
|
+
const d = c + 1;
|
|
53
|
+
idx[n++] = a;
|
|
54
|
+
idx[n++] = c;
|
|
55
|
+
idx[n++] = b;
|
|
56
|
+
idx[n++] = b;
|
|
57
|
+
idx[n++] = c;
|
|
58
|
+
idx[n++] = d;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Perimeter cut: four sides, each as a strip of vertex pairs (terrain on top, base at the bottom).
|
|
62
|
+
let v = cols * rows;
|
|
63
|
+
const side = (count, at, nx, nz, flip) => {
|
|
64
|
+
const start = v;
|
|
65
|
+
for (let k = 0; k < count; k++) {
|
|
66
|
+
const [i, j] = at(k);
|
|
67
|
+
put(v++, i * step, h(i, j), j * step, nx, 0, nz, KIND_SKIRT);
|
|
68
|
+
put(v++, i * step, baseY, j * step, nx, 0, nz, KIND_SKIRT);
|
|
69
|
+
}
|
|
70
|
+
for (let k = 0; k < count - 1; k++) {
|
|
71
|
+
const t0 = start + k * 2;
|
|
72
|
+
const b0 = t0 + 1;
|
|
73
|
+
const t1 = t0 + 2;
|
|
74
|
+
const b1 = t0 + 3;
|
|
75
|
+
if (flip) {
|
|
76
|
+
idx[n++] = t0;
|
|
77
|
+
idx[n++] = t1;
|
|
78
|
+
idx[n++] = b0;
|
|
79
|
+
idx[n++] = t1;
|
|
80
|
+
idx[n++] = b1;
|
|
81
|
+
idx[n++] = b0;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
idx[n++] = t0;
|
|
85
|
+
idx[n++] = b0;
|
|
86
|
+
idx[n++] = t1;
|
|
87
|
+
idx[n++] = t1;
|
|
88
|
+
idx[n++] = b0;
|
|
89
|
+
idx[n++] = b1;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
side(cols, (k) => [k, 0], 0, -1, true); // north
|
|
94
|
+
side(cols, (k) => [k, rows - 1], 0, 1, false); // south
|
|
95
|
+
side(rows, (k) => [0, k], -1, 0, false); // west
|
|
96
|
+
side(rows, (k) => [cols - 1, k], 1, 0, true); // east
|
|
97
|
+
return { layout: HEIGHT_FIELD_LAYOUT, vertices: new Uint8Array(buf), indices: idx, count: n };
|
|
98
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export { HEIGHT_FIELD_ATTRIBS, HEIGHT_FIELD_LAYOUT, heightFieldMesh } from './height-field-mesh.ts';
|
|
2
|
+
export { LINE_LAYOUT, lineMesh, spaceLineMesh } from './line-mesh.ts';
|
|
3
|
+
export type { SpaceLinePath } from './line-mesh.ts';
|
|
4
|
+
export type { LinePath } from './line-mesh.ts';
|
|
5
|
+
export type { PropBox } from './box.ts';
|
|
6
|
+
export { CELL_SURFACE_ATTRIBS, CELL_SURFACE_LAYOUT, cellSurfaceMesh } from './cell-surface-mesh.ts';
|
|
7
|
+
export type { CellSurface, CellSurfaceOptions } from './cell-surface-mesh.ts';
|
|
8
|
+
export { CELL_MORPH_ATTRIBS, CELL_MORPH_GLSL, CELL_MORPH_LAYOUT, cellMorphMesh } from './cell-morph-mesh.ts';
|
|
9
|
+
export type { CellMorphOptions, CellMorphSurface } from './cell-morph-mesh.ts';
|
|
10
|
+
export { rigMesh, rigShadowMesh } from './rig-mesh.ts';
|
|
11
|
+
export { DRAPE_ATTRIBS, DRAPE_LAYOUT, drapeEvery, drapeTiles } from './drape-mesh.ts';
|
|
12
|
+
export type { DrapeOptions, DrapeTile } from './drape-mesh.ts';
|
|
13
|
+
export { PRISM_ATTRIBS, PRISM_LAYOUT, prismMesh, triangulateRing } from './prism-mesh.ts';
|
|
14
|
+
export type { PrismMeshOptions, PrismShape } from './prism-mesh.ts';
|
|
15
|
+
export { PACKED_POSITION, PACK_STEPS_PER_EDGE, packFrame, packMesh, packUniform, packedLayout, packedMaterial } from './packed-mesh.ts';
|
|
16
|
+
export type { PackFrame, PackOptions, PackedMesh } from './packed-mesh.ts';
|
|
17
|
+
export { BISECT_SURFACE_ATTRIBS, BISECT_SURFACE_LAYOUT, bisectSurface } from './bisect-surface.ts';
|
|
18
|
+
export type { BisectSurface, BisectSurfaceOptions } from './bisect-surface.ts';
|
|
19
|
+
export { STRIP_ATTRIBS, STRIP_GLSL, STRIP_LAYOUT, stripGrow, stripMesh } from './strip-mesh.ts';
|
|
20
|
+
export type { StripMesh, StripMeshOptions } from './strip-mesh.ts';
|
|
21
|
+
export { chunkMeshes } from './chunk-mesh.ts';
|
|
22
|
+
export type { ChunkOptions, MeshChunk } from './chunk-mesh.ts';
|
|
23
|
+
export { MeshBuilder, faceNormal } from './mesh-builder.ts';
|
|
24
|
+
export type { MeshBounds, MeshBuildOptions, VertexEdit, VertexFormat } from './mesh-builder.ts';
|
|
25
|
+
export { box, cylinder, extrude, gridSurface, pathFrames, polygon, revolve, smoothPath, sweep } from './bare-shapes.ts';
|
|
26
|
+
export type { CylinderOptions, ExtrudeOptions, GridOptions, GridSmooth, PathFrame, PolygonOptions, RevolveOptions, ShapePart, ShapePayload, ShapeVertex, ShapeWarp, SweepOptions } from './bare-shapes.ts';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Layer 3, gen-mesh: mesh generators and shapes shared by them. Height field mesh, line mesh, the box primitive, rig
|
|
2
|
+
// part meshes.
|
|
3
|
+
export { HEIGHT_FIELD_ATTRIBS, HEIGHT_FIELD_LAYOUT, heightFieldMesh } from './height-field-mesh.js';
|
|
4
|
+
export { LINE_LAYOUT, lineMesh, spaceLineMesh } from './line-mesh.js';
|
|
5
|
+
export { CELL_SURFACE_ATTRIBS, CELL_SURFACE_LAYOUT, cellSurfaceMesh } from './cell-surface-mesh.js';
|
|
6
|
+
export { CELL_MORPH_ATTRIBS, CELL_MORPH_GLSL, CELL_MORPH_LAYOUT, cellMorphMesh } from './cell-morph-mesh.js';
|
|
7
|
+
export { rigMesh, rigShadowMesh } from './rig-mesh.js';
|
|
8
|
+
export { DRAPE_ATTRIBS, DRAPE_LAYOUT, drapeEvery, drapeTiles } from './drape-mesh.js';
|
|
9
|
+
export { PRISM_ATTRIBS, PRISM_LAYOUT, prismMesh, triangulateRing } from './prism-mesh.js';
|
|
10
|
+
export { PACKED_POSITION, PACK_STEPS_PER_EDGE, packFrame, packMesh, packUniform, packedLayout, packedMaterial } from './packed-mesh.js';
|
|
11
|
+
export { BISECT_SURFACE_ATTRIBS, BISECT_SURFACE_LAYOUT, bisectSurface } from './bisect-surface.js';
|
|
12
|
+
export { STRIP_ATTRIBS, STRIP_GLSL, STRIP_LAYOUT, stripGrow, stripMesh } from './strip-mesh.js';
|
|
13
|
+
export { chunkMeshes } from './chunk-mesh.js';
|
|
14
|
+
export { MeshBuilder, faceNormal } from './mesh-builder.js';
|
|
15
|
+
export { box, cylinder, extrude, gridSurface, pathFrames, polygon, revolve, smoothPath, sweep } from './bare-shapes.js';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { MeshData, VertexLayout } from '../base/data.ts';
|
|
2
|
+
/** Polyline in the x-z plane, flat array x0, z0, x1, z1, … */
|
|
3
|
+
export interface LinePath {
|
|
4
|
+
points: ArrayLike<number>;
|
|
5
|
+
/** Connect the last point to the first (outline). */
|
|
6
|
+
closed?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** Position 3× float, attribute `a_pos`. */
|
|
9
|
+
export declare const LINE_LAYOUT: VertexLayout;
|
|
10
|
+
/**
|
|
11
|
+
* Lines laid on a surface given by `heightAt`. Segments are split by `maxSegmentM` so the line follows the slope,
|
|
12
|
+
* and lifted by `liftM` above the surface.
|
|
13
|
+
*/
|
|
14
|
+
export declare function lineMesh(paths: readonly LinePath[], heightAt: (x: number, z: number) => number, liftM?: number, maxSegmentM?: number): MeshData;
|
|
15
|
+
/** Polyline in space, flat array x0, y0, z0, x1, y1, z1, … */
|
|
16
|
+
export interface SpaceLinePath {
|
|
17
|
+
points: ArrayLike<number>;
|
|
18
|
+
/** Connect the last point to the first. */
|
|
19
|
+
closed?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Polylines in space as a line mesh (one pixel wide, `LINE_LAYOUT`), taken as they are: no draping, no splitting.
|
|
23
|
+
* For wireframes and debugging overlays of upright shapes.
|
|
24
|
+
*/
|
|
25
|
+
export declare function spaceLineMesh(paths: readonly SpaceLinePath[]): MeshData;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Polylines laid on a surface as a line mesh (one pixel wide), for outlines and debugging overlays.
|
|
2
|
+
/** Position 3× float, attribute `a_pos`. */
|
|
3
|
+
export const LINE_LAYOUT = { stride: 12, attribs: [{ name: 'a_pos', size: 3, type: 'f32', offset: 0 }] };
|
|
4
|
+
/**
|
|
5
|
+
* Lines laid on a surface given by `heightAt`. Segments are split by `maxSegmentM` so the line follows the slope,
|
|
6
|
+
* and lifted by `liftM` above the surface.
|
|
7
|
+
*/
|
|
8
|
+
export function lineMesh(paths, heightAt, liftM = 0.3, maxSegmentM = 2) {
|
|
9
|
+
const out = [];
|
|
10
|
+
const vertex = (x, z) => {
|
|
11
|
+
out.push(x, heightAt(x, z) + liftM, z);
|
|
12
|
+
};
|
|
13
|
+
for (const path of paths) {
|
|
14
|
+
const p = path.points;
|
|
15
|
+
const n = p.length / 2;
|
|
16
|
+
const segments = path.closed ? n : n - 1;
|
|
17
|
+
for (let k = 0; k < segments; k++) {
|
|
18
|
+
const ax = p[k * 2];
|
|
19
|
+
const az = p[k * 2 + 1];
|
|
20
|
+
const bx = p[((k + 1) % n) * 2];
|
|
21
|
+
const bz = p[((k + 1) % n) * 2 + 1];
|
|
22
|
+
const parts = Math.max(1, Math.ceil(Math.hypot(bx - ax, bz - az) / maxSegmentM));
|
|
23
|
+
for (let i = 0; i < parts; i++) {
|
|
24
|
+
vertex(ax + ((bx - ax) * i) / parts, az + ((bz - az) * i) / parts);
|
|
25
|
+
vertex(ax + ((bx - ax) * (i + 1)) / parts, az + ((bz - az) * (i + 1)) / parts);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { layout: LINE_LAYOUT, vertices: new Float32Array(out), indices: null, count: out.length / 3, primitive: 'lines' };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Polylines in space as a line mesh (one pixel wide, `LINE_LAYOUT`), taken as they are: no draping, no splitting.
|
|
33
|
+
* For wireframes and debugging overlays of upright shapes.
|
|
34
|
+
*/
|
|
35
|
+
export function spaceLineMesh(paths) {
|
|
36
|
+
let segments = 0;
|
|
37
|
+
for (const path of paths) {
|
|
38
|
+
const n = Math.floor(path.points.length / 3);
|
|
39
|
+
if (n >= 2)
|
|
40
|
+
segments += path.closed && n > 2 ? n : n - 1;
|
|
41
|
+
}
|
|
42
|
+
const out = new Float32Array(segments * 6);
|
|
43
|
+
let o = 0;
|
|
44
|
+
for (const path of paths) {
|
|
45
|
+
const p = path.points;
|
|
46
|
+
const n = Math.floor(p.length / 3);
|
|
47
|
+
if (n < 2)
|
|
48
|
+
continue;
|
|
49
|
+
const count = path.closed && n > 2 ? n : n - 1;
|
|
50
|
+
for (let k = 0; k < count; k++) {
|
|
51
|
+
const a = k * 3;
|
|
52
|
+
const b = ((k + 1) % n) * 3;
|
|
53
|
+
out[o++] = p[a];
|
|
54
|
+
out[o++] = p[a + 1];
|
|
55
|
+
out[o++] = p[a + 2];
|
|
56
|
+
out[o++] = p[b];
|
|
57
|
+
out[o++] = p[b + 1];
|
|
58
|
+
out[o++] = p[b + 2];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return { layout: LINE_LAYOUT, vertices: out, indices: null, count: segments * 2, primitive: 'lines' };
|
|
62
|
+
}
|