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,229 @@
|
|
|
1
|
+
// Trees: instances of several lumpy shapes. The canopy is dark and voluminous, the lit side has lighter tufts of foliage.
|
|
2
|
+
/** Shared transform of an instance vertex into the world, also used by the shadow pass. */
|
|
3
|
+
export const TREE_TRANSFORM = /* glsl */ `
|
|
4
|
+
in vec3 a_pos; // canopy and trunk at unit height 0–1, horizontally in canopy radii
|
|
5
|
+
in vec3 a_normal;
|
|
6
|
+
in vec4 a_shade; // x vertex shading 0–1, y kind: 0 canopy, 1 trunk, z wind flutter 0–1, w phase 0–1
|
|
7
|
+
in vec4 i_base; // xyz tree base, w canopy radius
|
|
8
|
+
in vec4 i_shape; // x height, y cos of rotation, z sin of rotation, w hue 0–1
|
|
9
|
+
in vec4 i_extra; // x canopy start (share of height), y flexibility in wind, z phase 0–1, w reserve
|
|
10
|
+
uniform float u_trunkReach; // how far the trunk reaches into the canopy (share of the canopy): a part in summer, almost to the top for a bare tree
|
|
11
|
+
// Share of the tree height at which the vertex lies. The canopy occupies the span from its start upward, the trunk reaches a bit into it.
|
|
12
|
+
float treeHeightFraction() {
|
|
13
|
+
float cb = i_extra.x;
|
|
14
|
+
return mix(cb + a_pos.y * (1.0 - cb), a_pos.y * (cb + u_trunkReach * (1.0 - cb)), a_shade.y);
|
|
15
|
+
}
|
|
16
|
+
vec3 treeLocal() {
|
|
17
|
+
return vec3(a_pos.x * i_base.w, treeHeightFraction() * i_shape.x, a_pos.z * i_base.w);
|
|
18
|
+
}
|
|
19
|
+
vec3 treeRotate(vec3 v) {
|
|
20
|
+
#ifdef WH_LOCAL_UP
|
|
21
|
+
// Up is radial: the instance stands in the local frame of its base.
|
|
22
|
+
return localFrame(i_base.xyz) * vec3(v.x * i_shape.y - v.z * i_shape.z, v.y, v.x * i_shape.z + v.z * i_shape.y);
|
|
23
|
+
#else
|
|
24
|
+
return vec3(v.x * i_shape.y - v.z * i_shape.z, v.y, v.x * i_shape.z + v.z * i_shape.y);
|
|
25
|
+
#endif
|
|
26
|
+
}
|
|
27
|
+
// Position in tree space for noise: every tree has its pattern shifted by its hue.
|
|
28
|
+
vec3 treeObj(vec3 local) {
|
|
29
|
+
return local + vec3(i_shape.w * 37.0, 0.0, i_shape.w * 11.0);
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
/**
|
|
33
|
+
* Foliage by season: leafiness, autumn colour and freshness of one tree. Every tree has its own offset
|
|
34
|
+
* (leafs out, changes colour and sheds slightly at a different time). A bare canopy is a sparse lace of branches. Returns coverage by foliage and branches
|
|
35
|
+
* for alpha-to-coverage; outside winter it is 1 everywhere and nothing turns transparent.
|
|
36
|
+
*/
|
|
37
|
+
export const TREE_SEASON = /* glsl */ `
|
|
38
|
+
uniform int u_evergreen;
|
|
39
|
+
const float TWIG_COVER = 0.55;
|
|
40
|
+
float treeHashA(float tint, float phase) { return fract(tint * 7.31 + phase * 3.73); }
|
|
41
|
+
float treeHashB(float tint, float phase) { return fract(tint * 13.7 + phase * 5.11); }
|
|
42
|
+
float treeDensity(float tint, float phase) {
|
|
43
|
+
if (u_evergreen != 0) return 1.0;
|
|
44
|
+
float h = treeHashA(tint, phase);
|
|
45
|
+
return smoothstep(h * 0.4, h * 0.4 + 0.6, u_season.x);
|
|
46
|
+
}
|
|
47
|
+
// x foliage coverage, y total coverage (foliage or branches). visC and visF: how sharply the coarse (tufts around a metre)
|
|
48
|
+
// and the fine canopy detail are visible. What is not visible turns into average coverage, which alpha-to-coverage spreads over the samples.
|
|
49
|
+
vec2 crownCover(vec3 obj, float dens, float visC, float visF) {
|
|
50
|
+
if (dens >= 0.999) return vec2(1.0);
|
|
51
|
+
float coarse = vnoise3(obj * 0.9, 44u);
|
|
52
|
+
float fine = vnoise3(obj * 2.6, 45u);
|
|
53
|
+
float nz = smoothstep(0.2, 0.8, mix(coarse, 0.55 * coarse + 0.45 * fine, visF));
|
|
54
|
+
float edge = mix(0.22, 0.05, visF);
|
|
55
|
+
float leafA = mix(dens, 1.0 - smoothstep(dens - edge, dens + edge, nz), visC);
|
|
56
|
+
// Branches up close as sharp clusters (no sieve), from afar average coverage.
|
|
57
|
+
float tw = smoothstep(0.5, 0.56, vnoise3(obj * 3.4 + 7.0, 46u));
|
|
58
|
+
float twigA = mix(TWIG_COVER * 0.6, tw * 0.95, visF);
|
|
59
|
+
return vec2(leafA, max(leafA, twigA));
|
|
60
|
+
}
|
|
61
|
+
`;
|
|
62
|
+
/**
|
|
63
|
+
* Wind in the canopies. A tree is anchored at the ground and bends more the higher the top is. Gusts travel across the landscape
|
|
64
|
+
* with the wind, every tree sways to them at its own rhythm and the canopy surface flutters slightly.
|
|
65
|
+
* The largest horizontal deflection is 1.03 × deflection × flexibility + 1.46 × flutter, the check of
|
|
66
|
+
* canopy setback from walls relies on it (see environment.ts).
|
|
67
|
+
*/
|
|
68
|
+
export const TREE_WIND = /* glsl */ `
|
|
69
|
+
vec3 treeWind(float heightFraction) {
|
|
70
|
+
if (u_wind.z <= 0.0) return vec3(0.0);
|
|
71
|
+
const float TAU = 6.2831853;
|
|
72
|
+
vec2 dir = u_wind.xy;
|
|
73
|
+
vec2 side = vec2(-dir.y, dir.x);
|
|
74
|
+
float t = u_time.x;
|
|
75
|
+
float own = i_extra.z * TAU;
|
|
76
|
+
// Gust: a wave travelling with the wind. It is undulated across so its front is not ruler-straight.
|
|
77
|
+
#ifdef WH_LOCAL_UP
|
|
78
|
+
// Up is radial: gusts are placed by the position relative to the centre, and the bend lies in the local horizon.
|
|
79
|
+
vec2 at = (i_base.xyz - u_upCenter.xyz).xz;
|
|
80
|
+
#else
|
|
81
|
+
vec2 at = i_base.xz;
|
|
82
|
+
#endif
|
|
83
|
+
float across = dot(at, side);
|
|
84
|
+
float along = dot(at, dir) - u_gust.y * t;
|
|
85
|
+
float gust = 0.5 + 0.5 * sin(along * u_gust.x + 1.4 * sin(across * 0.023) + across * u_gust.x * 0.31);
|
|
86
|
+
float lean = 0.4 + 0.45 * gust + 0.15 * sin(t * 1.9 + own);
|
|
87
|
+
float sway = 0.25 * sin(t * 1.3 + own * 1.7);
|
|
88
|
+
vec2 bend = (dir * lean + side * sway) * (u_wind.z * i_extra.y * heightFraction * heightFraction);
|
|
89
|
+
float ph = a_shade.w * TAU + own;
|
|
90
|
+
vec3 flutter = vec3(sin(t * 5.3 + ph), 0.5 * sin(t * 4.1 + ph * 1.7), sin(t * 4.7 + ph * 2.3));
|
|
91
|
+
#ifdef WH_LOCAL_UP
|
|
92
|
+
return localFrame(i_base.xyz) * (vec3(bend.x, 0.0, bend.y) + flutter * (u_wind.w * a_shade.z * (0.6 + 0.4 * gust)));
|
|
93
|
+
#else
|
|
94
|
+
return vec3(bend.x, 0.0, bend.y) + flutter * (u_wind.w * a_shade.z * (0.6 + 0.4 * gust));
|
|
95
|
+
#endif
|
|
96
|
+
}
|
|
97
|
+
`;
|
|
98
|
+
export const TREE_VS = /* glsl */ `#include <header>
|
|
99
|
+
${TREE_TRANSFORM}${TREE_WIND}
|
|
100
|
+
out vec3 v_world;
|
|
101
|
+
out vec3 v_normal;
|
|
102
|
+
out vec3 v_obj;
|
|
103
|
+
out vec2 v_shade;
|
|
104
|
+
flat out float v_tint;
|
|
105
|
+
flat out float v_phase;
|
|
106
|
+
void main() {
|
|
107
|
+
vec3 local = treeLocal();
|
|
108
|
+
// Light and shadows are computed from the rest position, wind moves only the outline. The shadow map is thus independent of time.
|
|
109
|
+
v_world = i_base.xyz + treeRotate(local);
|
|
110
|
+
// Normal under non-uniform scale: divided by the axis scale.
|
|
111
|
+
v_normal = treeRotate(vec3(a_normal.x / i_base.w, a_normal.y / (i_shape.x * (1.0 - i_extra.x)), a_normal.z / i_base.w));
|
|
112
|
+
v_obj = treeObj(local);
|
|
113
|
+
v_shade = a_shade.xy;
|
|
114
|
+
v_tint = i_shape.w;
|
|
115
|
+
v_phase = i_extra.z;
|
|
116
|
+
gl_Position = u_viewProj * vec4(v_world + treeWind(local.y / i_shape.x), 1.0);
|
|
117
|
+
}`;
|
|
118
|
+
export const TREE_FS = /* glsl */ `#include <header>
|
|
119
|
+
#include <noise>
|
|
120
|
+
#include <lighting>
|
|
121
|
+
#include <snow>
|
|
122
|
+
${TREE_SEASON}
|
|
123
|
+
uniform ivec4 u_rows; // palette rows: dark canopy, middle, light foliage, trunk
|
|
124
|
+
uniform int u_rowAlt; // second hue of light foliage
|
|
125
|
+
uniform ivec4 u_seasonRows; // palette rows: autumn yellow, orange, rust, bare branches
|
|
126
|
+
uniform int u_springRow; // spring foliage
|
|
127
|
+
in vec3 v_world;
|
|
128
|
+
in vec3 v_normal;
|
|
129
|
+
in vec3 v_obj;
|
|
130
|
+
in vec2 v_shade;
|
|
131
|
+
flat in float v_tint;
|
|
132
|
+
flat in float v_phase;
|
|
133
|
+
out vec4 o_color;
|
|
134
|
+
|
|
135
|
+
void main() {
|
|
136
|
+
vec3 n = normalize(v_normal);
|
|
137
|
+
if (v_shade.y > 0.5) {
|
|
138
|
+
float sunT = sunVisibility(v_world, n, 2.0);
|
|
139
|
+
vec3 bark = u_palette[u_rows.w].rgb * (1.0 - 0.15 * u_weather.y);
|
|
140
|
+
bark = mix(bark, u_snow.rgb, snowOn(v_world, n, 0.0));
|
|
141
|
+
o_color = vec4(applyFog(toDisplay(bark * u_warmTint.rgb * lightOn(n, sunT, 0.6)), v_world), 1.0);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// Season: foliage density and autumn colour of this tree.
|
|
145
|
+
float dens = treeDensity(v_tint, v_phase);
|
|
146
|
+
vec2 cover = crownCover(v_obj, dens, detailVisible(1.1), detailVisible(0.35));
|
|
147
|
+
if (cover.y < 0.03) discard;
|
|
148
|
+
float leafShare = cover.x / cover.y;
|
|
149
|
+
float aut = 0.0;
|
|
150
|
+
vec3 autCol = vec3(0.0);
|
|
151
|
+
if (u_evergreen == 0) {
|
|
152
|
+
float hb = treeHashB(v_tint, v_phase);
|
|
153
|
+
// Every tree changes colour at a different time, the latest ones stay half green even at the peak of autumn.
|
|
154
|
+
aut = smoothstep(hb * 0.75, hb * 0.75 + 0.45, u_season.y);
|
|
155
|
+
vec3 y = u_palette[u_seasonRows.x].rgb, o = u_palette[u_seasonRows.y].rgb, r = u_palette[u_seasonRows.z].rgb;
|
|
156
|
+
autCol = hb < 0.45 ? mix(y, o, smoothstep(0.3, 0.45, hb)) : mix(o, r, smoothstep(0.6, 0.85, hb));
|
|
157
|
+
}
|
|
158
|
+
// Foliage tufts: noise in tree space. From afar it turns into the average.
|
|
159
|
+
float clump = 0.6 * vnoise3(v_obj * 0.9, 41u) + 0.4 * vnoise3(v_obj * 2.6, 42u);
|
|
160
|
+
clump = mix(0.5, clump, detailVisible(0.7));
|
|
161
|
+
float fine = (vnoise3(v_obj * 9.0, 43u) - 0.5) * detailVisible(0.25);
|
|
162
|
+
|
|
163
|
+
float sun = sunVisibility(v_world, n, 3.0);
|
|
164
|
+
// Foliage lets light through, hence a softer transition than at a wall.
|
|
165
|
+
float ndl = clamp((dot(n, u_sunDir.xyz) + 0.3) / 1.3, 0.0, 1.0);
|
|
166
|
+
float lit = ndl * sun;
|
|
167
|
+
float shade = v_shade.x * (0.7 + 0.6 * clump);
|
|
168
|
+
|
|
169
|
+
vec3 deep = u_palette[u_rows.x].rgb;
|
|
170
|
+
vec3 mid = u_palette[u_rows.y].rgb;
|
|
171
|
+
vec3 leaf = mix(u_palette[u_rows.z].rgb, u_palette[u_rowAlt].rgb, v_tint * 0.6);
|
|
172
|
+
if (u_evergreen == 0) {
|
|
173
|
+
vec3 spring = u_palette[u_springRow].rgb;
|
|
174
|
+
leaf = mix(leaf, spring, u_season.z * 0.45);
|
|
175
|
+
mid = mix(mid, spring * 0.72, u_season.z * 0.25);
|
|
176
|
+
deep = mix(deep, autCol * 0.38, aut * 0.55);
|
|
177
|
+
mid = mix(mid, autCol * 0.66, aut);
|
|
178
|
+
leaf = mix(leaf, autCol, aut);
|
|
179
|
+
}
|
|
180
|
+
// Canopy colour goes from almost black in the notches through dark green to lighter foliage only on lit bumps.
|
|
181
|
+
vec3 col = mix(deep, mid, smoothstep(0.1, 0.8, shade));
|
|
182
|
+
col = mix(col, leaf, smoothstep(0.22, 0.9, lit * (0.55 + clump)) * 0.85);
|
|
183
|
+
col *= 1.0 + 0.5 * fine;
|
|
184
|
+
col *= 0.96 + 0.24 * v_tint;
|
|
185
|
+
// Bare branches: grey-brown lace, darker inside the canopy.
|
|
186
|
+
vec3 twig = u_palette[u_seasonRows.w].rgb * (0.7 + 0.3 * clump) * mix(0.75, 1.05, v_shade.x);
|
|
187
|
+
col = mix(twig, col, leafShare);
|
|
188
|
+
col *= 1.0 - 0.12 * u_weather.y;
|
|
189
|
+
// Snow on the upper side of the canopy. Needles and branches hold it, dense foliage less.
|
|
190
|
+
float snow = snowOn(v_world, n, 0.08) * mix(0.9, 0.6, leafShare * float(u_evergreen == 0)) * smoothstep(0.35, 0.75, clump + 0.25);
|
|
191
|
+
col = mix(col, u_snow.rgb * (0.92 + 0.12 * clump), snow);
|
|
192
|
+
|
|
193
|
+
#ifdef WH_LOCAL_UP
|
|
194
|
+
float nUp = dot(n, localUp(v_world));
|
|
195
|
+
#else
|
|
196
|
+
float nUp = n.y;
|
|
197
|
+
#endif
|
|
198
|
+
vec3 fill = (u_ambient.rgb + u_skyFill.rgb * (nUp * 0.5 + 0.5)) * mix(0.45, 1.0, v_shade.x);
|
|
199
|
+
vec3 light = u_sunColor.rgb * lit * mix(0.6, 1.0, v_shade.x) + fill;
|
|
200
|
+
o_color = vec4(applyFog(toDisplay(col * u_warmTint.rgb * light), v_world), cover.y);
|
|
201
|
+
}`;
|
|
202
|
+
// --- Shadow pass of the trees ---------------------------------------------------------------------------
|
|
203
|
+
export const DEPTH_TREE_VS = /* glsl */ `#include <header>
|
|
204
|
+
uniform mat4 u_lightRender;
|
|
205
|
+
${TREE_TRANSFORM}
|
|
206
|
+
out vec3 v_obj;
|
|
207
|
+
flat out vec3 v_tree; // hue, phase, trunk
|
|
208
|
+
void main() {
|
|
209
|
+
vec3 local = treeLocal();
|
|
210
|
+
v_obj = treeObj(local);
|
|
211
|
+
v_tree = vec3(i_shape.w, i_extra.z, a_shade.y);
|
|
212
|
+
gl_Position = u_lightRender * vec4(i_base.xyz + treeRotate(local), 1.0);
|
|
213
|
+
}`;
|
|
214
|
+
/**
|
|
215
|
+
* Shadow of a sparse canopy: the fragment is discarded with a probability given by foliage and branch coverage. The threshold is a hash
|
|
216
|
+
* of the texel position, so the shadow is stable and soft shading turns it into a penumbra.
|
|
217
|
+
*/
|
|
218
|
+
export const DEPTH_TREE_FS = /* glsl */ `#include <header>
|
|
219
|
+
#include <noise>
|
|
220
|
+
${TREE_SEASON}
|
|
221
|
+
in vec3 v_obj;
|
|
222
|
+
flat in vec3 v_tree;
|
|
223
|
+
void main() {
|
|
224
|
+
if (v_tree.z > 0.5) return;
|
|
225
|
+
float dens = treeDensity(v_tree.x, v_tree.y);
|
|
226
|
+
if (dens >= 0.999) return;
|
|
227
|
+
float a = crownCover(v_obj, dens, 1.0, 1.0).y;
|
|
228
|
+
if (a < hash2(ivec2(gl_FragCoord.xy), 17u)) discard;
|
|
229
|
+
}`;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { MeshData, Uniforms, VertexLayout } from '../base/index.ts';
|
|
2
|
+
import type { PlantedTree, TreeSpecies } from './planting.ts';
|
|
3
|
+
export declare const TREE_ATTRIBS: {
|
|
4
|
+
readonly a_pos: 0;
|
|
5
|
+
readonly a_normal: 1;
|
|
6
|
+
readonly a_shade: 2;
|
|
7
|
+
readonly i_base: 3;
|
|
8
|
+
readonly i_shape: 4;
|
|
9
|
+
readonly i_extra: 5;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Floats per instance: base xyz, canopy radius | height, cos and sin of rotation, hue |
|
|
13
|
+
* canopy start (share of height), flexibility in wind, phase, reserve.
|
|
14
|
+
*/
|
|
15
|
+
export declare const TREE_INSTANCE_FLOATS = 12;
|
|
16
|
+
/** Colours of one species as palette row indices (the project's palette decides which rows). */
|
|
17
|
+
export interface TreeMaterial {
|
|
18
|
+
/** Notches and underside of the canopy. */
|
|
19
|
+
deep: number;
|
|
20
|
+
mid: number;
|
|
21
|
+
/** Lit foliage and its second hue, between which it changes according to the group's hue. */
|
|
22
|
+
leaf: number;
|
|
23
|
+
leafAlt: number;
|
|
24
|
+
trunk: number;
|
|
25
|
+
/** Evergreen: does not shed in winter and does not change colour in autumn. */
|
|
26
|
+
evergreen?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** Vertex layout of a shape: position, normal, shading (attribute locations in `TREE_ATTRIBS`). */
|
|
29
|
+
export declare const TREE_VERTEX_LAYOUT: VertexLayout;
|
|
30
|
+
/** Per-instance layout, `TREE_INSTANCE_FLOATS` floats (see `packTreeInstances`). */
|
|
31
|
+
export declare const TREE_INSTANCE_LAYOUT: VertexLayout;
|
|
32
|
+
/** Where culling finds the base point, canopy radius and height inside one instance (float offsets). */
|
|
33
|
+
export declare const TREE_INSTANCE_BOUNDS: {
|
|
34
|
+
readonly position: 0;
|
|
35
|
+
readonly radius: 3;
|
|
36
|
+
readonly height: 4;
|
|
37
|
+
};
|
|
38
|
+
/** One group of trees of the same shape: the shape in several levels of detail and the instances that share it. */
|
|
39
|
+
export interface TreeBatch {
|
|
40
|
+
species: TreeSpecies;
|
|
41
|
+
/** Meshes from the coarsest to the finest. */
|
|
42
|
+
lods: MeshData[];
|
|
43
|
+
/** All instances of the group, in the order of `TREE_INSTANCE_LAYOUT`. */
|
|
44
|
+
instances: Float32Array;
|
|
45
|
+
/** Palette rows: dark canopy, middle, foliage, trunk, second foliage hue. */
|
|
46
|
+
rows: [number, number, number, number, number];
|
|
47
|
+
/** Evergreen group (by material). */
|
|
48
|
+
evergreen: boolean;
|
|
49
|
+
}
|
|
50
|
+
/** Number of levels of detail the generator supports. */
|
|
51
|
+
export declare const TREE_LOD_COUNT: number;
|
|
52
|
+
/** Builds a group: shape given by species and seed, all levels of detail, instances in the order of TREE_INSTANCE_FLOATS. */
|
|
53
|
+
export declare function buildTreeBatch(species: TreeSpecies, seed: number, instances: Float32Array, material: TreeMaterial): TreeBatch;
|
|
54
|
+
/** Uniforms of one batch for the vegetation material at the given broadleaf foliage density (0–1). */
|
|
55
|
+
export declare function treeBatchUniforms(batch: TreeBatch, foliage: number): Uniforms;
|
|
56
|
+
/** Writes trees into the instance array. */
|
|
57
|
+
export declare function packTreeInstances(trees: readonly PlantedTree[]): Float32Array;
|
|
58
|
+
/** Splits trees into groups by species and shape variant and builds a mesh for each. Skips empty groups. */
|
|
59
|
+
export declare function buildTreeBatches(trees: readonly PlantedTree[], seed: number, materials: Readonly<Record<TreeSpecies, TreeMaterial>>): TreeBatch[];
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// Trees: several species with distinct silhouettes, generated by code and drawn as instances.
|
|
2
|
+
// Broadleaf canopies are unions of spheres (distinct bumps and dark notches), a conifer is a cone made of tiers.
|
|
3
|
+
// The canopy and the trunk are in the mesh at unit height 0–1; where the canopy starts on a tree is given by the instance,
|
|
4
|
+
// so the same shape serves a forest tree with a canopy from the ground and a park tree with a bare trunk.
|
|
5
|
+
// For each vertex the generator marks how much it may flutter in the wind (trunk not at all, canopy edge the most).
|
|
6
|
+
import { hashInts, unit } from '../gen-core/index.js';
|
|
7
|
+
import { lerp } from '../base/index.js';
|
|
8
|
+
export const TREE_ATTRIBS = { a_pos: 0, a_normal: 1, a_shade: 2, i_base: 3, i_shape: 4, i_extra: 5 };
|
|
9
|
+
/**
|
|
10
|
+
* Floats per instance: base xyz, canopy radius | height, cos and sin of rotation, hue |
|
|
11
|
+
* canopy start (share of height), flexibility in wind, phase, reserve.
|
|
12
|
+
*/
|
|
13
|
+
export const TREE_INSTANCE_FLOATS = 12;
|
|
14
|
+
/** Floats per vertex: position 3, normal 3, shading, kind (0 canopy, 1 trunk), flutter 0–1, phase 0–1. */
|
|
15
|
+
const VERTEX_FLOATS = 10;
|
|
16
|
+
const TRUNK_SIDES = 5;
|
|
17
|
+
/** How much wider the trunk is at the ground than at the top. */
|
|
18
|
+
const TRUNK_FLARE = 1.5;
|
|
19
|
+
/** How many different shapes each species has. Trees choose among them by their phase. */
|
|
20
|
+
const VARIANTS = { round: 2, tall: 2, conifer: 1, birch: 1, shrub: 1 };
|
|
21
|
+
const LUMP_STYLE = {
|
|
22
|
+
round: { lumps: 9, core: 0.66, offMin: 0.4, offMax: 0.56, rMin: 0.46, rMax: 0.6, yBias: 0.2, flatBottom: -0.5, flatSquash: 0.3, taper: 0, trunk: 0.075 },
|
|
23
|
+
tall: { lumps: 12, core: 0.6, offMin: 0.34, offMax: 0.44, rMin: 0.42, rMax: 0.52, yBias: 0.0, flatBottom: -0.72, flatSquash: 0.45, taper: 0.3, trunk: 0.08 },
|
|
24
|
+
birch: { lumps: 8, core: 0.5, offMin: 0.32, offMax: 0.42, rMin: 0.4, rMax: 0.5, yBias: -0.05, flatBottom: -0.8, flatSquash: 0.5, taper: 0.5, trunk: 0.06 },
|
|
25
|
+
shrub: { lumps: 7, core: 0.62, offMin: 0.38, offMax: 0.52, rMin: 0.44, rMax: 0.58, yBias: 0.3, flatBottom: -0.1, flatSquash: 0, taper: 0, trunk: 0 },
|
|
26
|
+
};
|
|
27
|
+
/** Conifer: tiers, cone envelope and trunk. */
|
|
28
|
+
const CONIFER = { tiers: 4, tiersTop: 0.84, overlap: 0.07, envelopePower: 0.85, innerRadius: 0.42, wobble: 0.16, trunk: 0.07 };
|
|
29
|
+
function icosphere(subdivisions) {
|
|
30
|
+
const t = (1 + Math.sqrt(5)) / 2;
|
|
31
|
+
let pos = [-1, t, 0, 1, t, 0, -1, -t, 0, 1, -t, 0, 0, -1, t, 0, 1, t, 0, -1, -t, 0, 1, -t, t, 0, -1, t, 0, 1, -t, 0, -1, -t, 0, 1];
|
|
32
|
+
let idx = [0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11, 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8, 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1];
|
|
33
|
+
for (let s = 0; s < subdivisions; s++) {
|
|
34
|
+
const mids = new Map();
|
|
35
|
+
const mid = (a, b) => {
|
|
36
|
+
const k = a < b ? a * 65536 + b : b * 65536 + a;
|
|
37
|
+
let m = mids.get(k);
|
|
38
|
+
if (m === undefined) {
|
|
39
|
+
m = pos.length / 3;
|
|
40
|
+
pos.push((pos[a * 3] + pos[b * 3]) / 2, (pos[a * 3 + 1] + pos[b * 3 + 1]) / 2, (pos[a * 3 + 2] + pos[b * 3 + 2]) / 2);
|
|
41
|
+
mids.set(k, m);
|
|
42
|
+
}
|
|
43
|
+
return m;
|
|
44
|
+
};
|
|
45
|
+
const next = [];
|
|
46
|
+
for (let i = 0; i < idx.length; i += 3) {
|
|
47
|
+
const a = idx[i];
|
|
48
|
+
const b = idx[i + 1];
|
|
49
|
+
const c = idx[i + 2];
|
|
50
|
+
const ab = mid(a, b);
|
|
51
|
+
const bc = mid(b, c);
|
|
52
|
+
const ca = mid(c, a);
|
|
53
|
+
next.push(a, ab, ca, b, bc, ab, c, ca, bc, ab, bc, ca);
|
|
54
|
+
}
|
|
55
|
+
idx = next;
|
|
56
|
+
}
|
|
57
|
+
const out = [];
|
|
58
|
+
for (let i = 0; i < pos.length; i += 3) {
|
|
59
|
+
const l = Math.hypot(pos[i], pos[i + 1], pos[i + 2]);
|
|
60
|
+
out.push(pos[i] / l, pos[i + 1] / l, pos[i + 2] / l);
|
|
61
|
+
}
|
|
62
|
+
pos = out;
|
|
63
|
+
return { pos, idx };
|
|
64
|
+
}
|
|
65
|
+
function smooth01(t) {
|
|
66
|
+
const c = t < 0 ? 0 : t > 1 ? 1 : t;
|
|
67
|
+
return c * c * (3 - 2 * c);
|
|
68
|
+
}
|
|
69
|
+
/** Adds a trunk: a five-sided truncated pyramid at unit height. Returns vertices and indices to attach. */
|
|
70
|
+
function trunk(firstVertex, radius) {
|
|
71
|
+
const verts = [];
|
|
72
|
+
const idx = [];
|
|
73
|
+
for (let s = 0; s < TRUNK_SIDES; s++) {
|
|
74
|
+
const a = (s / TRUNK_SIDES) * Math.PI * 2;
|
|
75
|
+
for (let level = 0; level < 2; level++) {
|
|
76
|
+
const rr = radius * (level === 0 ? TRUNK_FLARE : 1);
|
|
77
|
+
verts.push(Math.cos(a) * rr, level, Math.sin(a) * rr, Math.cos(a), 0, Math.sin(a), 0.6, 1, 0, 0);
|
|
78
|
+
}
|
|
79
|
+
const b0 = firstVertex + s * 2;
|
|
80
|
+
const t0 = b0 + 1;
|
|
81
|
+
const b1 = firstVertex + ((s + 1) % TRUNK_SIDES) * 2;
|
|
82
|
+
const t1 = b1 + 1;
|
|
83
|
+
idx.push(b0, t0, b1, b1, t0, t1);
|
|
84
|
+
}
|
|
85
|
+
return { verts, idx };
|
|
86
|
+
}
|
|
87
|
+
/** Broadleaf canopy or shrub. The same seed always gives the same shape. */
|
|
88
|
+
function buildLumpShape(style, seed, subdivisions) {
|
|
89
|
+
const sphere = icosphere(subdivisions);
|
|
90
|
+
const lumps = [{ cx: 0, cy: 0, cz: 0, r: style.core }];
|
|
91
|
+
for (let k = 0; k < style.lumps; k++) {
|
|
92
|
+
const u = unit(hashInts(seed, k, 1)) * 2 - 1;
|
|
93
|
+
const a = unit(hashInts(seed, k, 2)) * Math.PI * 2;
|
|
94
|
+
// Bumps more at the top and around the perimeter than at the bottom.
|
|
95
|
+
const y = Math.min(1, Math.max(-1, u * 0.75 + style.yBias));
|
|
96
|
+
const h = Math.sqrt(Math.max(0, 1 - y * y));
|
|
97
|
+
const off = style.offMin + (style.offMax - style.offMin) * unit(hashInts(seed, k, 3));
|
|
98
|
+
const r = style.rMin + (style.rMax - style.rMin) * unit(hashInts(seed, k, 4));
|
|
99
|
+
lumps.push({ cx: Math.cos(a) * h * off, cy: y * off, cz: Math.sin(a) * h * off, r });
|
|
100
|
+
}
|
|
101
|
+
const n = sphere.pos.length / 3;
|
|
102
|
+
const radius = new Float32Array(n);
|
|
103
|
+
const winner = new Int32Array(n);
|
|
104
|
+
let rMax = 0;
|
|
105
|
+
let rMin = Infinity;
|
|
106
|
+
for (let i = 0; i < n; i++) {
|
|
107
|
+
const dx = sphere.pos[i * 3];
|
|
108
|
+
const dy = sphere.pos[i * 3 + 1];
|
|
109
|
+
const dz = sphere.pos[i * 3 + 2];
|
|
110
|
+
let best = 0;
|
|
111
|
+
let bestK = 0;
|
|
112
|
+
lumps.forEach((l, k) => {
|
|
113
|
+
const b = dx * l.cx + dy * l.cy + dz * l.cz;
|
|
114
|
+
const c = l.cx * l.cx + l.cy * l.cy + l.cz * l.cz - l.r * l.r;
|
|
115
|
+
const t = b + Math.sqrt(Math.max(0, b * b - c));
|
|
116
|
+
if (t > best) {
|
|
117
|
+
best = t;
|
|
118
|
+
bestK = k;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
radius[i] = best;
|
|
122
|
+
winner[i] = bestK;
|
|
123
|
+
rMax = Math.max(rMax, best);
|
|
124
|
+
rMin = Math.min(rMin, best);
|
|
125
|
+
}
|
|
126
|
+
// First positions in sphere space (−1 to 1), then the height is converted to 0–1.
|
|
127
|
+
const px = new Float32Array(n);
|
|
128
|
+
const py = new Float32Array(n);
|
|
129
|
+
const pz = new Float32Array(n);
|
|
130
|
+
let yLo = Infinity;
|
|
131
|
+
let yHi = -Infinity;
|
|
132
|
+
for (let i = 0; i < n; i++) {
|
|
133
|
+
const r = radius[i] / rMax;
|
|
134
|
+
let y = sphere.pos[i * 3 + 1] * r;
|
|
135
|
+
if (y < style.flatBottom)
|
|
136
|
+
y = style.flatBottom + (y - style.flatBottom) * style.flatSquash;
|
|
137
|
+
const narrow = 1 - style.taper * smooth01((y + 0.2) / 1.2);
|
|
138
|
+
px[i] = sphere.pos[i * 3] * r * narrow;
|
|
139
|
+
pz[i] = sphere.pos[i * 3 + 2] * r * narrow;
|
|
140
|
+
py[i] = y;
|
|
141
|
+
yLo = Math.min(yLo, y);
|
|
142
|
+
yHi = Math.max(yHi, y);
|
|
143
|
+
}
|
|
144
|
+
const ySpan = yHi - yLo;
|
|
145
|
+
const verts = [];
|
|
146
|
+
for (let i = 0; i < n; i++) {
|
|
147
|
+
const dx = sphere.pos[i * 3];
|
|
148
|
+
const dy = sphere.pos[i * 3 + 1];
|
|
149
|
+
const dz = sphere.pos[i * 3 + 2];
|
|
150
|
+
const l = lumps[winner[i]];
|
|
151
|
+
// Normal: a mix of the direction from the canopy centre and from the centre of the winning bump.
|
|
152
|
+
let nx = dx * 0.35 + (dx * radius[i] - l.cx) * 0.65;
|
|
153
|
+
let ny = dy * 0.35 + (dy * radius[i] - l.cy) * 0.65;
|
|
154
|
+
let nz = dz * 0.35 + (dz * radius[i] - l.cz) * 0.65;
|
|
155
|
+
const nl = Math.hypot(nx, ny, nz) || 1;
|
|
156
|
+
nx /= nl;
|
|
157
|
+
ny /= nl;
|
|
158
|
+
nz /= nl;
|
|
159
|
+
// Shading: notches between bumps and the underside of the canopy are darker.
|
|
160
|
+
const crease = (radius[i] - rMin) / (rMax - rMin || 1);
|
|
161
|
+
const up = dy * 0.5 + 0.5;
|
|
162
|
+
const shade = (0.35 + 0.65 * Math.min(1, crease * 1.4)) * (0.45 + 0.55 * Math.min(1, up * 1.3));
|
|
163
|
+
const flutter = 0.35 + 0.65 * Math.min(1, Math.hypot(px[i], pz[i]));
|
|
164
|
+
const phase = unit(hashInts(seed, winner[i], 9));
|
|
165
|
+
// The vertical component of the normal is divided by the axis scale: height 0–1 corresponds to the ySpan range in sphere space.
|
|
166
|
+
verts.push(px[i], (py[i] - yLo) / ySpan, pz[i], nx, ny * ySpan, nz, shade, 0, flutter, phase);
|
|
167
|
+
}
|
|
168
|
+
const idx = sphere.idx.slice();
|
|
169
|
+
if (style.trunk > 0) {
|
|
170
|
+
const t = trunk(n, style.trunk);
|
|
171
|
+
verts.push(...t.verts);
|
|
172
|
+
idx.push(...t.idx);
|
|
173
|
+
}
|
|
174
|
+
return { vertices: Float32Array.from(verts), indices: Uint16Array.from(idx) };
|
|
175
|
+
}
|
|
176
|
+
/** Conifer: an outline of tiers revolved around the axis. Each tier is a cone whose lower edge overhangs the tier below. */
|
|
177
|
+
function buildConiferShape(seed, sides) {
|
|
178
|
+
const C = CONIFER;
|
|
179
|
+
const envelope = (y) => Math.pow(Math.max(0, 1 - y), C.envelopePower);
|
|
180
|
+
// Outline in the (radius, height) plane from the bottom centre to the tip, counter-clockwise around the mass.
|
|
181
|
+
const profile = [{ r: 0, y: 0, tip: false }];
|
|
182
|
+
for (let k = 0; k < C.tiers; k++) {
|
|
183
|
+
const yb = (k / C.tiers) * C.tiersTop;
|
|
184
|
+
profile.push({ r: envelope(yb), y: yb, tip: true });
|
|
185
|
+
if (k < C.tiers - 1) {
|
|
186
|
+
const yt = ((k + 1) / C.tiers) * C.tiersTop + C.overlap;
|
|
187
|
+
profile.push({ r: envelope(yt) * C.innerRadius, y: yt, tip: false });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
profile.push({ r: 0, y: 1, tip: false });
|
|
191
|
+
const verts = [];
|
|
192
|
+
const idx = [];
|
|
193
|
+
const ringRadius = (p, s) => {
|
|
194
|
+
const pt = profile[p];
|
|
195
|
+
// The tier edges are not a circle: each lobe has a different length.
|
|
196
|
+
return pt.tip ? pt.r * (1 - C.wobble * unit(hashInts(seed, p, s % sides))) : pt.r;
|
|
197
|
+
};
|
|
198
|
+
for (let p = 0; p + 1 < profile.length; p++) {
|
|
199
|
+
const a = profile[p];
|
|
200
|
+
const b = profile[p + 1];
|
|
201
|
+
// Outward normal of an outline segment: the segment direction rotated by a quarter turn to the right.
|
|
202
|
+
const nr = b.y - a.y;
|
|
203
|
+
const ny = -(b.r - a.r);
|
|
204
|
+
const nl = Math.hypot(nr, ny) || 1;
|
|
205
|
+
const first = verts.length / VERTEX_FLOATS;
|
|
206
|
+
for (let s = 0; s <= sides; s++) {
|
|
207
|
+
const ang = (s / sides) * Math.PI * 2;
|
|
208
|
+
const cos = Math.cos(ang);
|
|
209
|
+
const sin = Math.sin(ang);
|
|
210
|
+
for (const [pi, pt] of [
|
|
211
|
+
[p, a],
|
|
212
|
+
[p + 1, b],
|
|
213
|
+
]) {
|
|
214
|
+
const r = ringRadius(pi, s);
|
|
215
|
+
const shade = pt.tip ? 1 : pt.r === 0 && pt.y === 1 ? 0.95 : 0.3;
|
|
216
|
+
const flutter = pt.tip ? 1 : 0.25;
|
|
217
|
+
verts.push(cos * r, pt.y, sin * r, (cos * nr) / nl, ny / nl, (sin * nr) / nl, shade, 0, flutter, unit(hashInts(seed, 77, s % sides)));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
for (let s = 0; s < sides; s++) {
|
|
221
|
+
const a0 = first + s * 2;
|
|
222
|
+
const b0 = a0 + 1;
|
|
223
|
+
const a1 = a0 + 2;
|
|
224
|
+
const b1 = a0 + 3;
|
|
225
|
+
// Counter-clockwise when viewed from outside.
|
|
226
|
+
idx.push(a0, b0, a1, a1, b0, b1);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const t = trunk(verts.length / VERTEX_FLOATS, C.trunk);
|
|
230
|
+
verts.push(...t.verts);
|
|
231
|
+
idx.push(...t.idx);
|
|
232
|
+
return { vertices: Float32Array.from(verts), indices: Uint16Array.from(idx) };
|
|
233
|
+
}
|
|
234
|
+
/** Levels of detail: sphere subdivision count for broadleaf and side count for conifer, from the coarsest. */
|
|
235
|
+
const LOD_SUBDIVISIONS = [1, 2];
|
|
236
|
+
const LOD_CONIFER_SIDES = [6, 10];
|
|
237
|
+
function buildShape(species, seed, lod) {
|
|
238
|
+
if (species === 'conifer')
|
|
239
|
+
return buildConiferShape(seed, LOD_CONIFER_SIDES[Math.min(lod, LOD_CONIFER_SIDES.length - 1)]);
|
|
240
|
+
return buildLumpShape(LUMP_STYLE[species], seed, LOD_SUBDIVISIONS[Math.min(lod, LOD_SUBDIVISIONS.length - 1)]);
|
|
241
|
+
}
|
|
242
|
+
/** Vertex layout of a shape: position, normal, shading (attribute locations in `TREE_ATTRIBS`). */
|
|
243
|
+
export const TREE_VERTEX_LAYOUT = {
|
|
244
|
+
stride: VERTEX_FLOATS * 4,
|
|
245
|
+
attribs: [
|
|
246
|
+
{ name: 'a_pos', size: 3, type: 'f32', offset: 0 },
|
|
247
|
+
{ name: 'a_normal', size: 3, type: 'f32', offset: 12 },
|
|
248
|
+
{ name: 'a_shade', size: 4, type: 'f32', offset: 24 },
|
|
249
|
+
],
|
|
250
|
+
};
|
|
251
|
+
/** Per-instance layout, `TREE_INSTANCE_FLOATS` floats (see `packTreeInstances`). */
|
|
252
|
+
export const TREE_INSTANCE_LAYOUT = {
|
|
253
|
+
stride: TREE_INSTANCE_FLOATS * 4,
|
|
254
|
+
attribs: [
|
|
255
|
+
{ name: 'i_base', size: 4, type: 'f32', offset: 0 },
|
|
256
|
+
{ name: 'i_shape', size: 4, type: 'f32', offset: 16 },
|
|
257
|
+
{ name: 'i_extra', size: 4, type: 'f32', offset: 32 },
|
|
258
|
+
],
|
|
259
|
+
};
|
|
260
|
+
/** Where culling finds the base point, canopy radius and height inside one instance (float offsets). */
|
|
261
|
+
export const TREE_INSTANCE_BOUNDS = { position: 0, radius: 3, height: 4 };
|
|
262
|
+
/** Number of levels of detail the generator supports. */
|
|
263
|
+
export const TREE_LOD_COUNT = LOD_SUBDIVISIONS.length;
|
|
264
|
+
/** Builds a group: shape given by species and seed, all levels of detail, instances in the order of TREE_INSTANCE_FLOATS. */
|
|
265
|
+
export function buildTreeBatch(species, seed, instances, material) {
|
|
266
|
+
const lods = [];
|
|
267
|
+
for (let lod = 0; lod < TREE_LOD_COUNT; lod++) {
|
|
268
|
+
const shape = buildShape(species, seed, lod);
|
|
269
|
+
lods.push({ layout: TREE_VERTEX_LAYOUT, vertices: shape.vertices, indices: shape.indices, count: shape.indices.length });
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
species,
|
|
273
|
+
lods,
|
|
274
|
+
instances,
|
|
275
|
+
rows: [material.deep, material.mid, material.leaf, material.trunk, material.leafAlt],
|
|
276
|
+
evergreen: material.evergreen ?? false,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
/** How far into the canopy the trunk reaches: a part in a leafy canopy, almost to the top for a bare tree. */
|
|
280
|
+
const TRUNK_REACH_LEAFY = 0.25;
|
|
281
|
+
const TRUNK_REACH_BARE = 0.72;
|
|
282
|
+
/** Uniforms of one batch for the vegetation material at the given broadleaf foliage density (0–1). */
|
|
283
|
+
export function treeBatchUniforms(batch, foliage) {
|
|
284
|
+
const r = batch.rows;
|
|
285
|
+
return {
|
|
286
|
+
u_rows: { ivec4: [r[0], r[1], r[2], r[3]] },
|
|
287
|
+
u_rowAlt: { int: r[4] },
|
|
288
|
+
u_evergreen: { int: batch.evergreen ? 1 : 0 },
|
|
289
|
+
u_trunkReach: { float: batch.evergreen ? TRUNK_REACH_LEAFY : lerp(TRUNK_REACH_BARE, TRUNK_REACH_LEAFY, foliage) },
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
/** Writes trees into the instance array. */
|
|
293
|
+
export function packTreeInstances(trees) {
|
|
294
|
+
const out = new Float32Array(trees.length * TREE_INSTANCE_FLOATS);
|
|
295
|
+
trees.forEach((t, i) => {
|
|
296
|
+
const o = i * TREE_INSTANCE_FLOATS;
|
|
297
|
+
out[o] = t.x;
|
|
298
|
+
out[o + 1] = t.y;
|
|
299
|
+
out[o + 2] = t.z;
|
|
300
|
+
out[o + 3] = t.radius;
|
|
301
|
+
out[o + 4] = t.height;
|
|
302
|
+
out[o + 5] = Math.cos(t.yaw);
|
|
303
|
+
out[o + 6] = Math.sin(t.yaw);
|
|
304
|
+
out[o + 7] = t.tint;
|
|
305
|
+
out[o + 8] = t.crownBase;
|
|
306
|
+
out[o + 9] = t.flex;
|
|
307
|
+
out[o + 10] = t.phase;
|
|
308
|
+
out[o + 11] = 0;
|
|
309
|
+
});
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
/** Splits trees into groups by species and shape variant and builds a mesh for each. Skips empty groups. */
|
|
313
|
+
export function buildTreeBatches(trees, seed, materials) {
|
|
314
|
+
const groups = new Map();
|
|
315
|
+
for (const t of trees) {
|
|
316
|
+
const variant = Math.min(VARIANTS[t.species] - 1, Math.floor(t.phase * VARIANTS[t.species]));
|
|
317
|
+
const key = `${t.species}:${variant}`;
|
|
318
|
+
let g = groups.get(key);
|
|
319
|
+
if (!g) {
|
|
320
|
+
g = { species: t.species, variant, trees: [] };
|
|
321
|
+
groups.set(key, g);
|
|
322
|
+
}
|
|
323
|
+
g.trees.push(t);
|
|
324
|
+
}
|
|
325
|
+
const speciesOrder = ['round', 'tall', 'conifer', 'birch', 'shrub'];
|
|
326
|
+
return [...groups.values()]
|
|
327
|
+
.sort((a, b) => speciesOrder.indexOf(a.species) - speciesOrder.indexOf(b.species) || a.variant - b.variant)
|
|
328
|
+
.map((g) => buildTreeBatch(g.species, hashInts(seed, speciesOrder.indexOf(g.species), g.variant), packTreeInstances(g.trees), materials[g.species]));
|
|
329
|
+
}
|