roamwright 0.4.0 → 0.54.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/.prettierrc.yaml +12 -0
- package/CONTRIBUTING.md +21 -14
- package/README.md +38 -42
- package/examples/animals.html +49 -0
- package/examples/animals.js +209 -0
- package/examples/crowds.html +50 -0
- package/examples/crowds.js +268 -0
- package/examples/effects.html +53 -0
- package/examples/effects.js +343 -0
- package/examples/figure-motion.html +55 -0
- package/examples/figure-motion.js +265 -0
- package/examples/figure.html +49 -0
- package/examples/figure.js +238 -0
- package/examples/fliers.html +53 -0
- package/examples/fliers.js +292 -0
- package/examples/fortification.html +54 -0
- package/examples/fortification.js +288 -0
- package/examples/house-kit.html +48 -0
- package/examples/house-kit.js +175 -0
- package/examples/index.html +26 -16
- package/examples/island.html +48 -0
- package/examples/island.js +134 -52
- package/examples/land.html +54 -0
- package/examples/land.js +392 -0
- package/examples/props-and-plants.html +53 -0
- package/examples/props-and-plants.js +275 -0
- package/examples/rooms.html +48 -0
- package/examples/rooms.js +242 -0
- package/examples/ships.html +51 -0
- package/examples/ships.js +235 -0
- package/examples/showcase.css +320 -0
- package/examples/small-world-land.js +151 -0
- package/examples/small-world-life.js +160 -0
- package/examples/small-world-town.js +127 -0
- package/examples/small-world.html +57 -0
- package/examples/small-world.js +226 -0
- package/examples/soundscape.html +138 -0
- package/examples/soundscape.js +467 -0
- package/examples/stage-kits.js +300 -0
- package/examples/stage.js +374 -0
- package/examples/towns.html +50 -0
- package/examples/towns.js +325 -0
- package/examples/viaduct-camera.js +8 -4
- package/examples/viaduct-landscape.js +28 -17
- package/examples/viaduct.html +45 -43
- package/examples/viaduct.js +68 -92
- package/geometry/animal-camel-body.js +410 -0
- package/geometry/animal-camel-head.js +118 -0
- package/geometry/animal-canine-body.js +361 -0
- package/geometry/animal-canine-breeds.js +316 -0
- package/geometry/animal-canine-head.js +337 -0
- package/geometry/animal-cattle-body.js +391 -0
- package/geometry/animal-cattle-coat.js +42 -0
- package/geometry/animal-cattle-head.js +152 -0
- package/geometry/animal-hen.js +248 -0
- package/geometry/animal-horse-body.js +446 -0
- package/geometry/animal-horse-bridle.js +171 -0
- package/geometry/animal-horse-coat.js +68 -0
- package/geometry/animal-horse-face.js +162 -0
- package/geometry/animal-horse-hair.js +304 -0
- package/geometry/animal-horse-head.js +133 -0
- package/geometry/animal-horse-tack.js +157 -0
- package/geometry/animal-options.js +59 -0
- package/geometry/animal-shapes.js +21 -0
- package/geometry/animal-skin.js +390 -0
- package/geometry/bedding.js +141 -0
- package/geometry/builder-cells.js +50 -0
- package/geometry/builder-frame.js +48 -0
- package/geometry/builder.js +200 -177
- package/geometry/card-emblem.js +437 -0
- package/geometry/catenary-canvas.js +396 -0
- package/geometry/cell-mask.js +25 -0
- package/geometry/cloth-folds.js +143 -0
- package/geometry/crawl-shapes.js +71 -0
- package/geometry/creased-sheet.js +40 -35
- package/geometry/crowd-dress.js +315 -0
- package/geometry/crowd-far.js +174 -0
- package/geometry/crowd-look.js +42 -0
- package/geometry/crowd-weld.js +218 -0
- package/geometry/ear.js +44 -39
- package/geometry/erosion.js +71 -45
- package/geometry/face-grid.js +85 -65
- package/geometry/field-map.js +59 -0
- package/geometry/figure-arms.js +339 -0
- package/geometry/figure-beard.js +250 -0
- package/geometry/figure-body.js +297 -0
- package/geometry/figure-bow.js +105 -0
- package/geometry/figure-cloak.js +189 -0
- package/geometry/figure-clothes.js +337 -0
- package/geometry/figure-face-grids.js +75 -0
- package/geometry/figure-gear.js +446 -0
- package/geometry/figure-hair-far.js +151 -0
- package/geometry/figure-hair-hangs.js +108 -0
- package/geometry/figure-hair-patch.js +66 -0
- package/geometry/figure-hair.js +320 -0
- package/geometry/figure-hands.js +321 -0
- package/geometry/figure-head-features.js +280 -0
- package/geometry/figure-head.js +375 -0
- package/geometry/figure-headwear.js +223 -0
- package/geometry/figure-legs.js +231 -0
- package/geometry/figure-limbs.js +39 -0
- package/geometry/figure-sculpt.js +82 -0
- package/geometry/figure-skin.js +97 -0
- package/geometry/figure-skirts.js +303 -0
- package/geometry/figure-trunk.js +411 -0
- package/geometry/flier-corvid-body.js +322 -0
- package/geometry/flier-corvid-wings.js +154 -0
- package/geometry/flier-long-body.js +195 -0
- package/geometry/flier-shapes.js +219 -0
- package/geometry/flier-soaring-bird.js +84 -0
- package/geometry/foliage.js +187 -14
- package/geometry/fort-city-walls.js +479 -0
- package/geometry/fort-gate-approach.js +233 -0
- package/geometry/fort-great-walls.js +280 -0
- package/geometry/fort-keep.js +297 -0
- package/geometry/fort-parts.js +261 -0
- package/geometry/fort-polyline.js +261 -0
- package/geometry/fort-portal.js +96 -0
- package/geometry/fort-ring-wall.js +267 -0
- package/geometry/fort-seawall.js +104 -0
- package/geometry/fort-terrace-gates.js +323 -0
- package/geometry/fort-terrace-plan.js +250 -0
- package/geometry/fort-terrace-wall.js +345 -0
- package/geometry/garment-tags.js +11 -3
- package/geometry/grid-gather.js +69 -0
- package/geometry/grid-sample.js +185 -0
- package/geometry/hair-gathers.js +57 -42
- package/geometry/hair-locks.js +42 -42
- package/geometry/hair-patch.js +24 -20
- package/geometry/hair-shell.js +43 -31
- package/geometry/head-presets.js +103 -0
- package/geometry/head-profile.js +26 -15
- package/geometry/head-shape.js +123 -95
- package/geometry/heightfield.js +50 -24
- package/geometry/horizon-angles.js +166 -0
- package/geometry/house-kit-body.js +195 -0
- package/geometry/house-kit-courtyard-parts.js +340 -0
- package/geometry/house-kit-courtyard-roof.js +223 -0
- package/geometry/house-kit-courtyard.js +409 -0
- package/geometry/house-kit-cut.js +29 -0
- package/geometry/house-kit-extras.js +192 -0
- package/geometry/house-kit-forms.js +298 -0
- package/geometry/house-kit-frame.js +74 -0
- package/geometry/house-kit-marks.js +93 -0
- package/geometry/house-kit-openings.js +193 -0
- package/geometry/house-kit-palette.js +54 -0
- package/geometry/house-kit-pool.js +71 -0
- package/geometry/house-kit-roofs.js +151 -0
- package/geometry/house-kit.js +202 -0
- package/geometry/index-parts.js +2 -2
- package/geometry/instance-visibility.js +12 -12
- package/geometry/instances.js +17 -17
- package/geometry/lamp-wash.js +65 -0
- package/geometry/land-clearings.js +83 -0
- package/geometry/land-ditches.js +290 -0
- package/geometry/land-dry-basin.js +327 -0
- package/geometry/land-dunes.js +150 -0
- package/geometry/land-earthworks.js +432 -0
- package/geometry/land-edge.js +292 -0
- package/geometry/land-mound-faces.js +349 -0
- package/geometry/land-near-relief.js +102 -0
- package/geometry/land-sheet.js +220 -0
- package/geometry/land-town-ground.js +256 -0
- package/geometry/land-waters.js +228 -0
- package/geometry/land-wildness.js +123 -0
- package/geometry/lathed-vessel.js +293 -0
- package/geometry/outline-grid.js +11 -10
- package/geometry/outline-segments.js +74 -0
- package/geometry/part-ranges.js +38 -3
- package/geometry/path-field.js +98 -34
- package/geometry/placement.js +52 -52
- package/geometry/planked-floor.js +149 -0
- package/geometry/plant-broadleaf.js +268 -0
- package/geometry/plant-shrub.js +198 -0
- package/geometry/plant-tints.js +80 -0
- package/geometry/plant-trees.js +453 -0
- package/geometry/plant-tussock.js +107 -0
- package/geometry/point-buckets.js +50 -0
- package/geometry/primitives.js +117 -59
- package/geometry/prop-battlefield.js +69 -0
- package/geometry/prop-camp.js +175 -0
- package/geometry/prop-door.js +26 -0
- package/geometry/prop-harbour.js +105 -0
- package/geometry/prop-haycock.js +60 -0
- package/geometry/prop-sacks.js +74 -0
- package/geometry/prop-sheaf.js +68 -0
- package/geometry/prop-street.js +69 -0
- package/geometry/prop-tents.js +396 -0
- package/geometry/prop-tombs.js +80 -0
- package/geometry/prop-trade.js +393 -0
- package/geometry/prop-vehicles.js +218 -0
- package/geometry/props.js +58 -0
- package/geometry/room-arcade.js +134 -0
- package/geometry/room-dome.js +188 -0
- package/geometry/room-flames.js +138 -0
- package/geometry/room-frame.js +106 -0
- package/geometry/room-furniture.js +260 -0
- package/geometry/room-hearth.js +98 -0
- package/geometry/room-kit.js +59 -0
- package/geometry/room-openings.js +142 -0
- package/geometry/room-stairs.js +180 -0
- package/geometry/room-surfaces.js +272 -0
- package/geometry/sagging-tube.js +27 -27
- package/geometry/sampled-surface.js +123 -77
- package/geometry/scatter-grid.js +49 -6
- package/geometry/sculpt.js +97 -93
- package/geometry/ship-fittings.js +350 -0
- package/geometry/ship-hull.js +460 -0
- package/geometry/ship-kit.js +208 -0
- package/geometry/ship-lines.js +69 -0
- package/geometry/ship-oars.js +150 -0
- package/geometry/ship-presets.js +131 -0
- package/geometry/ship-rig.js +374 -0
- package/geometry/ship-sails.js +148 -0
- package/geometry/shore-grid.js +81 -34
- package/geometry/strata-field.js +131 -98
- package/geometry/strata-tiles.js +108 -94
- package/geometry/stream.js +96 -59
- package/geometry/swept-tube.js +124 -0
- package/geometry/terrain-chunks.js +216 -123
- package/geometry/terrain-extension.js +29 -13
- package/geometry/town-courtyard-inn.js +272 -0
- package/geometry/town-ground.js +76 -0
- package/geometry/town-houses.js +343 -0
- package/geometry/town-lights.js +254 -0
- package/geometry/town-lots.js +172 -0
- package/geometry/town-plan.js +370 -0
- package/geometry/town-works.js +465 -0
- package/geometry/village-layout.js +181 -0
- package/geometry/village-yards.js +167 -0
- package/geometry/wall-runs.js +64 -0
- package/index.js +138 -2
- package/math/cell-edge.js +16 -16
- package/math/cell-key.js +6 -0
- package/math/circle-clearances.js +19 -0
- package/math/cloth-textures.js +6 -6
- package/math/cloud-textures.js +22 -26
- package/math/color.js +14 -14
- package/math/curve.js +26 -22
- package/math/lamp-texels.js +43 -0
- package/math/lit-share.js +22 -0
- package/math/mountain-ranges.js +379 -205
- package/math/noise.js +101 -49
- package/math/particle-noise.js +28 -28
- package/math/periodic.js +11 -5
- package/math/random.js +24 -15
- package/math/rectangles.js +58 -0
- package/math/rock-texture.js +22 -17
- package/math/scalar.js +5 -5
- package/math/surface-textures.js +74 -59
- package/math/wandering-path.js +40 -0
- package/math/ways.js +120 -0
- package/motion/action-dynamics.js +398 -0
- package/motion/animal-bearing.js +247 -0
- package/motion/animal-canine-bearing.js +178 -0
- package/motion/animal-canine-cast.js +131 -0
- package/motion/animal-cattle-cast.js +64 -0
- package/motion/animal-herd-cast.js +154 -0
- package/motion/animal-horse-still.js +161 -0
- package/motion/animal-troop-cast.js +43 -0
- package/motion/arm-reach.js +27 -26
- package/motion/ballistic-arc.js +191 -0
- package/motion/biped-cycle.js +46 -0
- package/motion/biped-leg.js +40 -0
- package/motion/biped-posture.js +459 -0
- package/motion/biped-stride.js +226 -140
- package/motion/boat-sailing.js +214 -0
- package/motion/contact-transition.js +32 -0
- package/motion/crowd-alarms.js +72 -42
- package/motion/crowd-attention.js +108 -0
- package/motion/crowd-beasts.js +203 -0
- package/motion/crowd-camp.js +295 -0
- package/motion/crowd-figure.js +100 -0
- package/motion/crowd-herd.js +86 -0
- package/motion/crowd-hold.js +130 -0
- package/motion/crowd-pose.js +296 -0
- package/motion/crowd-poses.js +481 -0
- package/motion/crowd-routine.js +126 -67
- package/motion/crowd-town.js +166 -0
- package/motion/crowd-water.js +143 -0
- package/motion/daily-clock.js +9 -9
- package/motion/deck-surface.js +40 -0
- package/motion/figure-acts.js +411 -0
- package/motion/figure-arms.js +437 -0
- package/motion/figure-blade-slide.js +149 -0
- package/motion/figure-body-place.js +180 -0
- package/motion/figure-bones.js +118 -0
- package/motion/figure-bow-draw.js +70 -0
- package/motion/figure-fingers.js +215 -0
- package/motion/figure-gaze.js +205 -0
- package/motion/figure-gestures.js +452 -0
- package/motion/figure-grip.js +134 -0
- package/motion/figure-head.js +287 -0
- package/motion/figure-legs.js +264 -0
- package/motion/figure-links.js +293 -0
- package/motion/figure-locomotion.js +391 -0
- package/motion/figure-mount-up.js +180 -0
- package/motion/figure-mount.js +201 -0
- package/motion/figure-pose-frame.js +375 -0
- package/motion/figure-pose-table.js +156 -0
- package/motion/figure-pose.js +343 -0
- package/motion/figure-seat.js +267 -0
- package/motion/figure-sway.js +125 -0
- package/motion/flier-corvid-pose.js +74 -0
- package/motion/flier-flight-air.js +122 -0
- package/motion/flier-flight-echelon.js +164 -0
- package/motion/flier-flight-roost.js +131 -0
- package/motion/flier-swarm-flight.js +252 -0
- package/motion/formation.js +193 -0
- package/motion/ground-lookahead.js +5 -5
- package/motion/instance-sight.js +15 -8
- package/motion/melee-contact.js +143 -0
- package/motion/moorings.js +48 -0
- package/motion/path-steering.js +358 -0
- package/motion/player.js +41 -32
- package/motion/quadruped.js +244 -168
- package/motion/spaces.js +90 -38
- package/motion/speech-cadence.js +304 -0
- package/motion/vessel-motion.js +57 -0
- package/motion/walk-grid.js +235 -0
- package/motion/weather.js +133 -26
- package/motion/wind.js +58 -13
- package/package.json +33 -6
- package/rendering/alpha-mips.js +340 -0
- package/rendering/animal-camel-figure.js +29 -0
- package/rendering/animal-canine-figure.js +59 -0
- package/rendering/animal-cattle-figure.js +36 -0
- package/rendering/animal-horse-figure.js +49 -0
- package/rendering/atmosphere.js +86 -0
- package/rendering/camera-track.js +156 -0
- package/rendering/cart-track.js +92 -0
- package/rendering/caster-motion.js +6 -5
- package/rendering/cell-lod-options.js +46 -0
- package/rendering/cell-lod.js +754 -113
- package/rendering/cloth-material.js +140 -0
- package/rendering/cloud-dome.js +30 -30
- package/rendering/cloud-field.js +2 -2
- package/rendering/compilation-schedule.js +79 -14
- package/rendering/contact-shadows.js +7 -4
- package/rendering/context.js +57 -6
- package/rendering/crowd-batches.js +420 -0
- package/rendering/crowd-herd.js +198 -0
- package/rendering/crowd-rig.js +132 -0
- package/rendering/crowd-skinning.js +62 -19
- package/rendering/crowd-update.js +419 -0
- package/rendering/crowd-visibility.js +107 -0
- package/rendering/crust.js +18 -12
- package/rendering/day-cycle-defaults.js +200 -0
- package/rendering/day-cycle.js +487 -0
- package/rendering/daylight.js +4 -6
- package/rendering/detail-range.js +29 -0
- package/rendering/dispose-meshes.js +18 -0
- package/rendering/dof.js +20 -13
- package/rendering/effects-frame.js +149 -0
- package/rendering/effects-water-rises.js +221 -0
- package/rendering/energy-stream.js +128 -0
- package/rendering/eye-adapt.js +14 -14
- package/rendering/facade-glass-material.js +105 -0
- package/rendering/far-shadow.js +89 -62
- package/rendering/field-surface.js +138 -0
- package/rendering/figure-shield.js +105 -0
- package/rendering/figure-skeleton.js +329 -0
- package/rendering/fire-light-watch.js +197 -0
- package/rendering/fire-pools.js +148 -0
- package/rendering/flame-sources.js +86 -0
- package/rendering/flier-bird-looks.js +97 -0
- package/rendering/flier-corvid-figure.js +67 -0
- package/rendering/flier-long-body-company.js +169 -0
- package/rendering/flier-swarm.js +186 -0
- package/rendering/flocks.js +82 -78
- package/rendering/garment-material.js +147 -51
- package/rendering/glow-lines.js +13 -13
- package/rendering/glsl-literal.js +19 -0
- package/rendering/ground-marks.js +316 -0
- package/rendering/hair-strands.js +45 -31
- package/rendering/height-haze.js +80 -33
- package/rendering/height-probe.js +164 -22
- package/rendering/house-kit-meshes.js +124 -0
- package/rendering/interior-bake.js +807 -0
- package/rendering/label-layout.js +56 -0
- package/rendering/label-range.js +41 -0
- package/rendering/lamp-light-map.js +159 -0
- package/rendering/lamp-maps.js +62 -0
- package/rendering/lamp-wash.js +51 -0
- package/rendering/light-budget.js +71 -55
- package/rendering/light-points.js +21 -5
- package/rendering/local-lights.js +39 -8
- package/rendering/material-lod.js +49 -50
- package/rendering/memory-estimate.js +84 -0
- package/rendering/moored-fleet.js +186 -0
- package/rendering/part-costs.js +104 -0
- package/rendering/particle-bursts.js +386 -0
- package/rendering/particle-emitters.js +135 -99
- package/rendering/particle-light-bursts.js +189 -0
- package/rendering/particles.js +212 -101
- package/rendering/pen-flocks.js +118 -0
- package/rendering/piece-batch.js +298 -11
- package/rendering/planar-reflection-options.js +159 -0
- package/rendering/planar-reflection.js +806 -103
- package/rendering/plant-cells.js +122 -0
- package/rendering/plant-reeds.js +113 -0
- package/rendering/plant-shade-trees.js +239 -0
- package/rendering/plant-tuft-patch.js +122 -0
- package/rendering/post-shaders.js +98 -31
- package/rendering/post.js +107 -87
- package/rendering/pressed-cover.js +135 -0
- package/rendering/prop-dig.js +136 -0
- package/rendering/rain-streaks.js +67 -0
- package/rendering/room-light.js +250 -0
- package/rendering/room-materials.js +78 -0
- package/rendering/room-space.js +111 -0
- package/rendering/scene-lights.js +106 -0
- package/rendering/sea-shaders.js +94 -37
- package/rendering/settlement-approach.js +226 -0
- package/rendering/settlement-dissolve.js +89 -0
- package/rendering/settlement-quarter-warm.js +55 -0
- package/rendering/settlement-quarter.js +477 -0
- package/rendering/settlement-switch.js +42 -0
- package/rendering/settlement-town.js +110 -0
- package/rendering/shader-warmup.js +101 -44
- package/rendering/shadow-alignment.js +4 -4
- package/rendering/shadow-depth.js +47 -25
- package/rendering/ship-meshes.js +150 -0
- package/rendering/shot-cuts.js +68 -0
- package/rendering/sky-occlusion.js +56 -29
- package/rendering/sky-pmrem.js +216 -0
- package/rendering/sky.js +126 -54
- package/rendering/smoke-shadows.js +201 -0
- package/rendering/stage-framing.js +182 -0
- package/rendering/stage-lights.js +46 -39
- package/rendering/stain-material.js +74 -0
- package/rendering/static-point-shadow.js +65 -26
- package/rendering/still-casters.js +77 -27
- package/rendering/strata-material.js +12 -12
- package/rendering/stream-flow.js +10 -10
- package/rendering/street-lamps.js +166 -0
- package/rendering/sunbeam-motes.js +670 -0
- package/rendering/surface-light.js +65 -6
- package/rendering/surface-material.js +306 -0
- package/rendering/surface-projection.js +32 -0
- package/rendering/surface-snippets.js +314 -0
- package/rendering/taa.js +75 -44
- package/rendering/talus.js +15 -15
- package/rendering/terrain-rock.js +7 -7
- package/rendering/texture-fills.js +8 -8
- package/rendering/textures.js +28 -28
- package/rendering/tree-impostor.js +929 -0
- package/rendering/vehicle-train.js +92 -0
- package/rendering/viewport.js +67 -4
- package/rendering/wakes.js +24 -24
- package/rendering/water-material.js +304 -103
- package/rendering/waypoint-beacons.js +191 -0
- package/rendering/world-matrices.js +38 -40
- package/ruff.toml +20 -0
- package/runtime/cue-cursor.js +21 -0
- package/runtime/device-quality.js +13 -2
- package/runtime/device.js +73 -0
- package/runtime/diagnostic-log.js +12 -4
- package/runtime/effect-sites.js +85 -0
- package/runtime/figure-orders.js +59 -0
- package/runtime/first-use.js +36 -28
- package/runtime/foley-script.js +124 -0
- package/runtime/foley-strides.js +333 -0
- package/runtime/frame-pacing.js +47 -0
- package/runtime/frame-yield.js +18 -0
- package/runtime/gpu-timing.js +15 -11
- package/runtime/grid-cache.js +16 -6
- package/runtime/grid-rows.js +44 -5
- package/runtime/hand-over.js +47 -24
- package/runtime/land-bake.js +78 -0
- package/runtime/lazy-sets.js +105 -0
- package/runtime/pool.js +73 -17
- package/runtime/quality-fit.js +60 -28
- package/runtime/region-pass-lay.js +40 -0
- package/runtime/region-pass.js +78 -0
- package/runtime/search.js +26 -22
- package/runtime/settlement-reach.js +42 -0
- package/runtime/sound-calls.js +113 -0
- package/runtime/sound-impacts.js +297 -0
- package/runtime/sound-voices.js +194 -0
- package/runtime/soundscape.js +282 -0
- package/runtime/spatial-audio.js +79 -30
- package/runtime/steady-frames.js +221 -0
- package/runtime/withdrawable-task.js +46 -0
- package/runtime/worker.js +7 -0
- package/scripts/app-parts.mjs +118 -0
- package/scripts/bake-revisions.mjs +81 -0
- package/scripts/browser-scripts.mjs +64 -0
- package/scripts/bundle.mjs +192 -0
- package/scripts/check-app.sh +26 -0
- package/scripts/eslint-config.mjs +119 -0
- package/scripts/format.sh +92 -0
- package/scripts/keep-builds.mjs +158 -0
- package/scripts/layers.mjs +112 -0
- package/scripts/lint.sh +49 -0
- package/scripts/module-graph.mjs +123 -0
- package/scripts/pre-commit.sh +38 -0
- package/scripts/serve.py +5 -3
- package/scripts/site.mjs +358 -0
- package/scripts/test-runner.sh +42 -0
- package/scripts/vendor.mjs +221 -0
- package/scripts/verify-publish.mjs +175 -0
- package/scripts/world.mk +59 -0
- package/tools/README.md +73 -0
- package/tools/contact-sheet.cjs +137 -0
- package/tools/cpu-profile.cjs +275 -0
- package/tools/cpu-profile.test.cjs +98 -0
- package/tools/first-draws.cjs +395 -0
- package/tools/flight-guard.cjs +482 -0
- package/tools/flight-guard.test.cjs +153 -0
- package/tools/flight-profile.cjs +145 -0
- package/tools/frame-bench.cjs +336 -0
- package/tools/frame-parts.cjs +365 -0
- package/tools/ios-sim.sh +145 -0
- package/tools/load-audit.cjs +201 -0
- package/tools/look-compare.cjs +304 -0
- package/tools/look-guard.cjs +407 -0
- package/tools/look-guard.test.cjs +93 -0
- package/tools/look-stats.cjs +185 -0
- package/tools/look-stats.test.cjs +71 -0
- package/tools/memory-audit.cjs +323 -0
- package/tools/page-driver.cjs +320 -0
- package/tools/page-driver.test.cjs +219 -0
- package/tools/program-census-report.cjs +191 -0
- package/tools/program-census.cjs +465 -0
- package/tools/program-census.test.cjs +332 -0
- package/tools/remote-runner.sh +265 -0
- package/tools/runners.test.cjs +59 -0
- package/tools/scene-audit-beat.cjs +344 -0
- package/tools/scene-audit-sheet.cjs +282 -0
- package/tools/scene-audit.cjs +471 -0
- package/tools/scene-audit.test.cjs +201 -0
- package/tools/scene-batch.cjs +219 -0
- package/tools/scene-bench.cjs +149 -0
- package/tools/scene-bench.test.cjs +33 -0
- package/tools/scene-run.cjs +443 -0
- package/tools/scene-run.test.cjs +94 -0
- package/tools/servers.sh +59 -0
- package/tools/shader-arrival.cjs +316 -0
- package/tools/shader-renewal.cjs +53 -0
- package/tools/shader-renewal.test.cjs +115 -0
- package/tools/speed-guard.cjs +258 -0
- package/tools/stills.cjs +492 -0
- package/tools/stills.test.cjs +49 -0
- package/tools/turns.sh +7 -0
- package/tools/yaml.cjs +158 -0
- package/examples/viaduct.css +0 -75
- package/rendering/light-shafts.js +0 -131
- package/rendering/road-surface.js +0 -63
|
@@ -0,0 +1,929 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Eight-heading real-prototype tree impostor atlas builder and instanced billboard renderer.
|
|
3
|
+
* Captures 3D tree geometries at 2x supersampling into a tintable luminance + view-aligned normal + alpha atlas
|
|
4
|
+
* with coverage-preserving mipmaps, widens distant crowns slightly into continuous canopy, and blends smoothly
|
|
5
|
+
* past ~600 m toward analytic species silhouettes ('spire', 'round', 'mound') so small mips never square off.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as THREE from 'three';
|
|
10
|
+
import { TAU, clamp } from '../math/scalar.js';
|
|
11
|
+
import { buildCoveragePreservingMipmaps } from './alpha-mips.js';
|
|
12
|
+
import { thinSurfaceLight } from './surface-light.js';
|
|
13
|
+
|
|
14
|
+
const MAX_SPECIES = 16;
|
|
15
|
+
const DEFAULT_HEADINGS = 8;
|
|
16
|
+
const DEFAULT_TILE_SIZE = 64;
|
|
17
|
+
const DEFAULT_SUPERSAMPLE = 2;
|
|
18
|
+
const DEFAULT_MARGIN = 0.09;
|
|
19
|
+
const DEFAULT_ANALYTIC_DISTANCE = 600;
|
|
20
|
+
const DEFAULT_ALPHA_CUTOFF = 0.5;
|
|
21
|
+
|
|
22
|
+
// Numeric IDs for distant analytic crown silhouettes.
|
|
23
|
+
const SILHOUETTE_IDS = Object.freeze({
|
|
24
|
+
spire: 0,
|
|
25
|
+
round: 1,
|
|
26
|
+
mound: 2,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const DEFAULT_CROWN_WIDTH_BY_SILHOUETTE = [1.12, 1.15, 1.16];
|
|
30
|
+
|
|
31
|
+
// Deterministic integer hash in [0, 1) for sub-leaf edge perforation and per-instance variation.
|
|
32
|
+
export function hashCoords2D(ix, iz, salt = 0) {
|
|
33
|
+
let h = (Math.imul(ix | 0, 374761393) + Math.imul(iz | 0, 668265263) + Math.imul(salt | 0, 2147483647)) | 0;
|
|
34
|
+
h = Math.imul(h ^ (h >>> 13), 1274126177);
|
|
35
|
+
return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function luminanceLinear(r, g, b) {
|
|
39
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// The colours of a prototype's vertices as linear floats, three to a vertex, whatever store they come in. A store of whole
|
|
43
|
+
// numbers (bytes, as a compact builder keeps them) is read as the card reads it, each divided by the store's largest value, and a
|
|
44
|
+
// fourth component is skipped: read raw, a byte store made every colour of a tree a hundredfold too bright.
|
|
45
|
+
function unpackColours(colourAttribute) {
|
|
46
|
+
if (!colourAttribute) return null;
|
|
47
|
+
const { array, itemSize = 3 } = colourAttribute,
|
|
48
|
+
count = Math.floor(array.length / itemSize),
|
|
49
|
+
largest = array instanceof Uint8Array || array instanceof Uint8ClampedArray ? 255 : array instanceof Uint16Array ? 65535 : 1,
|
|
50
|
+
scale = 1 / largest,
|
|
51
|
+
rgb = new Float32Array(count * 3);
|
|
52
|
+
for (let i = 0; i < count; i++) {
|
|
53
|
+
rgb[i * 3] = array[i * itemSize] * scale;
|
|
54
|
+
rgb[i * 3 + 1] = array[i * itemSize + 1] * scale;
|
|
55
|
+
rgb[i * 3 + 2] = array[i * itemSize + 2] * scale;
|
|
56
|
+
}
|
|
57
|
+
return rgb;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function resolveSilhouetteId(silhouette) {
|
|
61
|
+
if (typeof silhouette === 'number') return clamp(silhouette | 0, 0, 2);
|
|
62
|
+
if (silhouette === 'spire') return SILHOUETTE_IDS.spire;
|
|
63
|
+
if (silhouette === 'mound') return SILHOUETTE_IDS.mound;
|
|
64
|
+
return SILHOUETTE_IDS.round;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Measures cylindrical bounds, crown base share, and separate linear RGB base colours for bark and foliage.
|
|
68
|
+
function inspectPrototype(entry) {
|
|
69
|
+
const geometry = entry?.isBufferGeometry ? entry : entry?.isMesh ? entry.geometry : entry?.geometry;
|
|
70
|
+
if (!geometry?.attributes?.position) {
|
|
71
|
+
throw new TypeError('Each tree impostor prototype requires a BufferGeometry with a position attribute');
|
|
72
|
+
}
|
|
73
|
+
const positions = geometry.attributes.position.array,
|
|
74
|
+
colors = unpackColours(geometry.attributes.color),
|
|
75
|
+
leaves = geometry.attributes.aLeaf?.array ?? null,
|
|
76
|
+
vertexCount = geometry.attributes.position.count;
|
|
77
|
+
|
|
78
|
+
let yMin = Infinity,
|
|
79
|
+
yMax = -Infinity,
|
|
80
|
+
maxRadius = 0;
|
|
81
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
82
|
+
const px = positions[i * 3],
|
|
83
|
+
py = positions[i * 3 + 1],
|
|
84
|
+
pz = positions[i * 3 + 2],
|
|
85
|
+
r = Math.hypot(px, pz);
|
|
86
|
+
if (py < yMin) yMin = py;
|
|
87
|
+
if (py > yMax) yMax = py;
|
|
88
|
+
if (r > maxRadius) maxRadius = r;
|
|
89
|
+
}
|
|
90
|
+
if (!Number.isFinite(yMin) || yMax <= yMin) {
|
|
91
|
+
yMin = 0;
|
|
92
|
+
yMax = 1;
|
|
93
|
+
maxRadius = 0.5;
|
|
94
|
+
}
|
|
95
|
+
const height = Math.max(0.25, yMax - yMin);
|
|
96
|
+
|
|
97
|
+
let crownR = 0,
|
|
98
|
+
crownG = 0,
|
|
99
|
+
crownB = 0,
|
|
100
|
+
crownCount = 0,
|
|
101
|
+
trunkR = 0,
|
|
102
|
+
trunkG = 0,
|
|
103
|
+
trunkB = 0,
|
|
104
|
+
trunkCount = 0,
|
|
105
|
+
lowestLeafY = yMax;
|
|
106
|
+
|
|
107
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
108
|
+
const py = positions[i * 3 + 1],
|
|
109
|
+
cr = colors ? colors[i * 3] : 0.25,
|
|
110
|
+
cg = colors ? colors[i * 3 + 1] : 0.38,
|
|
111
|
+
cb = colors ? colors[i * 3 + 2] : 0.2;
|
|
112
|
+
const isLeaf = leaves ? leaves[i] > 0.5 : py > yMin + height * 0.42 && cg > cr * 1.02;
|
|
113
|
+
if (isLeaf) {
|
|
114
|
+
crownR += cr;
|
|
115
|
+
crownG += cg;
|
|
116
|
+
crownB += cb;
|
|
117
|
+
crownCount++;
|
|
118
|
+
if (py < lowestLeafY) lowestLeafY = py;
|
|
119
|
+
} else {
|
|
120
|
+
trunkR += cr;
|
|
121
|
+
trunkG += cg;
|
|
122
|
+
trunkB += cb;
|
|
123
|
+
trunkCount++;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!crownCount && trunkCount) {
|
|
128
|
+
crownR = trunkR;
|
|
129
|
+
crownG = trunkG;
|
|
130
|
+
crownB = trunkB;
|
|
131
|
+
crownCount = trunkCount;
|
|
132
|
+
} else if (!trunkCount && crownCount) {
|
|
133
|
+
trunkR = crownR * 0.45;
|
|
134
|
+
trunkG = crownG * 0.36;
|
|
135
|
+
trunkB = crownB * 0.26;
|
|
136
|
+
trunkCount = crownCount;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const meanCrown = crownCount ? [crownR / crownCount, crownG / crownCount, crownB / crownCount] : [0.22, 0.34, 0.18],
|
|
140
|
+
meanTrunk = trunkCount ? [trunkR / trunkCount, trunkG / trunkCount, trunkB / trunkCount] : [0.14, 0.1, 0.07];
|
|
141
|
+
if (entry?.crownColor) {
|
|
142
|
+
meanCrown[0] = entry.crownColor[0];
|
|
143
|
+
meanCrown[1] = entry.crownColor[1];
|
|
144
|
+
meanCrown[2] = entry.crownColor[2];
|
|
145
|
+
}
|
|
146
|
+
if (entry?.trunkColor) {
|
|
147
|
+
meanTrunk[0] = entry.trunkColor[0];
|
|
148
|
+
meanTrunk[1] = entry.trunkColor[1];
|
|
149
|
+
meanTrunk[2] = entry.trunkColor[2];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const crownLum = Math.max(0.01, luminanceLinear(meanCrown[0], meanCrown[1], meanCrown[2])),
|
|
153
|
+
trunkLum = Math.max(0.01, luminanceLinear(meanTrunk[0], meanTrunk[1], meanTrunk[2])),
|
|
154
|
+
referenceLum = Math.max(crownLum, trunkLum, 0.02);
|
|
155
|
+
|
|
156
|
+
// Scale base colours by (referenceLum / channelMeanLum) so multiplying by (atlasR * 2.0) = (texelLum / referenceLum)
|
|
157
|
+
// recovers the exact linear RGB brightness of both foliage and bark.
|
|
158
|
+
const crownScale = referenceLum / crownLum,
|
|
159
|
+
trunkScale = referenceLum / trunkLum,
|
|
160
|
+
silhouetteId = resolveSilhouetteId(entry?.silhouette),
|
|
161
|
+
inferredCrownStart = crownCount ? clamp((lowestLeafY - yMin) / height, 0.08, 0.78) : 0.2,
|
|
162
|
+
crownStart = clamp(entry?.crownStart ?? inferredCrownStart, 0.05, 0.85),
|
|
163
|
+
crownWidthScale = clamp(entry?.crownWidthScale ?? DEFAULT_CROWN_WIDTH_BY_SILHOUETTE[silhouetteId], 0.9, 1.35),
|
|
164
|
+
foliageGain = entry?.foliageGain ?? 1.06;
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
geometry,
|
|
168
|
+
colors,
|
|
169
|
+
yMin,
|
|
170
|
+
yMax,
|
|
171
|
+
height,
|
|
172
|
+
maxRadius: Math.max(0.2, maxRadius),
|
|
173
|
+
referenceLum,
|
|
174
|
+
crownStart,
|
|
175
|
+
crownWidthScale,
|
|
176
|
+
foliageGain,
|
|
177
|
+
silhouetteId,
|
|
178
|
+
silhouette: entry?.silhouette ?? (silhouetteId === 0 ? 'spire' : silhouetteId === 2 ? 'mound' : 'round'),
|
|
179
|
+
crownColor: new THREE.Vector3(meanCrown[0] * crownScale, meanCrown[1] * crownScale, meanCrown[2] * crownScale),
|
|
180
|
+
trunkColor: new THREE.Vector3(meanTrunk[0] * trunkScale, meanTrunk[1] * trunkScale, meanTrunk[2] * trunkScale),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Orthographically rasterizes one prototype at heading angle `theta` into supersampled scratch buffers.
|
|
185
|
+
function rasterizeHeading(spec, theta, margin, capSize, depthBuf, lumBuf, nxBuf, nyBuf, hitBuf) {
|
|
186
|
+
depthBuf.fill(-Infinity);
|
|
187
|
+
lumBuf.fill(0);
|
|
188
|
+
nxBuf.fill(0);
|
|
189
|
+
nyBuf.fill(0);
|
|
190
|
+
hitBuf.fill(0);
|
|
191
|
+
|
|
192
|
+
const { geometry, colors, yMin, yMax, height, maxRadius, crownStart } = spec,
|
|
193
|
+
spanY = Math.max(0.25, height / (1 - 2 * margin)),
|
|
194
|
+
spanX = Math.max(0.25, (maxRadius * 2) / (1 - 2 * margin)),
|
|
195
|
+
yOrigin = yMin - margin * spanY,
|
|
196
|
+
crownCenterY = yMin + height * (crownStart + (1 - crownStart) * 0.52),
|
|
197
|
+
crownHalfH = Math.max(0.25, height * (1 - crownStart) * 0.55),
|
|
198
|
+
cosT = Math.cos(theta),
|
|
199
|
+
sinT = Math.sin(theta);
|
|
200
|
+
|
|
201
|
+
const posAttr = geometry.attributes.position,
|
|
202
|
+
normAttr = geometry.attributes.normal,
|
|
203
|
+
leafAttr = geometry.attributes.aLeaf,
|
|
204
|
+
positions = posAttr.array,
|
|
205
|
+
normals = normAttr?.array ?? null,
|
|
206
|
+
leaves = leafAttr?.array ?? null,
|
|
207
|
+
indices = geometry.index?.array ?? null,
|
|
208
|
+
triCount = indices ? (indices.length / 3) | 0 : (posAttr.count / 3) | 0;
|
|
209
|
+
|
|
210
|
+
for (let tri = 0; tri < triCount; tri++) {
|
|
211
|
+
const i0 = indices ? indices[tri * 3] : tri * 3,
|
|
212
|
+
i1 = indices ? indices[tri * 3 + 1] : tri * 3 + 1,
|
|
213
|
+
i2 = indices ? indices[tri * 3 + 2] : tri * 3 + 2;
|
|
214
|
+
|
|
215
|
+
const p0x = positions[i0 * 3],
|
|
216
|
+
p0y = positions[i0 * 3 + 1],
|
|
217
|
+
p0z = positions[i0 * 3 + 2],
|
|
218
|
+
p1x = positions[i1 * 3],
|
|
219
|
+
p1y = positions[i1 * 3 + 1],
|
|
220
|
+
p1z = positions[i1 * 3 + 2],
|
|
221
|
+
p2x = positions[i2 * 3],
|
|
222
|
+
p2y = positions[i2 * 3 + 1],
|
|
223
|
+
p2z = positions[i2 * 3 + 2];
|
|
224
|
+
|
|
225
|
+
// View basis aligned with heading theta: right = (cos, 0, -sin), up = (0, 1, 0), forward = (sin, 0, cos).
|
|
226
|
+
const r0x = p0x * cosT - p0z * sinT,
|
|
227
|
+
r0z = p0x * sinT + p0z * cosT,
|
|
228
|
+
r1x = p1x * cosT - p1z * sinT,
|
|
229
|
+
r1z = p1x * sinT + p1z * cosT,
|
|
230
|
+
r2x = p2x * cosT - p2z * sinT,
|
|
231
|
+
r2z = p2x * sinT + p2z * cosT;
|
|
232
|
+
|
|
233
|
+
const sx0 = (0.5 + r0x / spanX) * capSize,
|
|
234
|
+
sy0 = ((p0y - yOrigin) / spanY) * capSize,
|
|
235
|
+
sx1 = (0.5 + r1x / spanX) * capSize,
|
|
236
|
+
sy1 = ((p1y - yOrigin) / spanY) * capSize,
|
|
237
|
+
sx2 = (0.5 + r2x / spanX) * capSize,
|
|
238
|
+
sy2 = ((p2y - yOrigin) / spanY) * capSize;
|
|
239
|
+
|
|
240
|
+
const area = (sx1 - sx0) * (sy2 - sy0) - (sy1 - sy0) * (sx2 - sx0);
|
|
241
|
+
const isLeaf = leaves ? leaves[i0] + leaves[i1] + leaves[i2] > 1.0 : (p0y + p1y + p2y) / 3 > yMin + height * 0.4;
|
|
242
|
+
if (Math.abs(area) < 1e-5) continue;
|
|
243
|
+
|
|
244
|
+
const minX = Math.max(0, Math.floor(Math.min(sx0, sx1, sx2))),
|
|
245
|
+
maxX = Math.min(capSize - 1, Math.ceil(Math.max(sx0, sx1, sx2))),
|
|
246
|
+
minY = Math.max(0, Math.floor(Math.min(sy0, sy1, sy2))),
|
|
247
|
+
maxY = Math.min(capSize - 1, Math.ceil(Math.max(sy0, sy1, sy2)));
|
|
248
|
+
if (minX > maxX || minY > maxY) continue;
|
|
249
|
+
|
|
250
|
+
const invArea = 1 / area;
|
|
251
|
+
const l0 = colors ? luminanceLinear(colors[i0 * 3], colors[i0 * 3 + 1], colors[i0 * 3 + 2]) : 0.25,
|
|
252
|
+
l1 = colors ? luminanceLinear(colors[i1 * 3], colors[i1 * 3 + 1], colors[i1 * 3 + 2]) : 0.25,
|
|
253
|
+
l2 = colors ? luminanceLinear(colors[i2 * 3], colors[i2 * 3 + 1], colors[i2 * 3 + 2]) : 0.25;
|
|
254
|
+
|
|
255
|
+
// Transform vertex normals into the heading's (right, up, forward) frame; fall back to geometric face normal.
|
|
256
|
+
let fnx = (p1y - p0y) * (r2z - r0z) - (r1z - r0z) * (p2y - p0y),
|
|
257
|
+
fny = (r1z - r0z) * (r2x - r0x) - (r1x - r0x) * (r2z - r0z),
|
|
258
|
+
fnz = (r1x - r0x) * (p2y - p0y) - (p1y - p0y) * (r2x - r0x);
|
|
259
|
+
const fnLen = Math.hypot(fnx, fny, fnz) || 1;
|
|
260
|
+
fnx /= fnLen;
|
|
261
|
+
fny /= fnLen;
|
|
262
|
+
fnz /= fnLen;
|
|
263
|
+
if (fnz < 0) {
|
|
264
|
+
fnx = -fnx;
|
|
265
|
+
fny = -fny;
|
|
266
|
+
fnz = -fnz;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let vn0x = fnx,
|
|
270
|
+
vn0y = fny,
|
|
271
|
+
vn0z = fnz,
|
|
272
|
+
vn1x = fnx,
|
|
273
|
+
vn1y = fny,
|
|
274
|
+
vn1z = fnz,
|
|
275
|
+
vn2x = fnx,
|
|
276
|
+
vn2y = fny,
|
|
277
|
+
vn2z = fnz;
|
|
278
|
+
if (normals) {
|
|
279
|
+
const sign = area < 0 && isLeaf ? -1 : 1;
|
|
280
|
+
vn0x = (normals[i0 * 3] * cosT - normals[i0 * 3 + 2] * sinT) * sign;
|
|
281
|
+
vn0y = normals[i0 * 3 + 1] * sign;
|
|
282
|
+
vn0z = (normals[i0 * 3] * sinT + normals[i0 * 3 + 2] * cosT) * sign;
|
|
283
|
+
vn1x = (normals[i1 * 3] * cosT - normals[i1 * 3 + 2] * sinT) * sign;
|
|
284
|
+
vn1y = normals[i1 * 3 + 1] * sign;
|
|
285
|
+
vn1z = (normals[i1 * 3] * sinT + normals[i1 * 3 + 2] * cosT) * sign;
|
|
286
|
+
vn2x = (normals[i2 * 3] * cosT - normals[i2 * 3 + 2] * sinT) * sign;
|
|
287
|
+
vn2y = normals[i2 * 3 + 1] * sign;
|
|
288
|
+
vn2z = (normals[i2 * 3] * sinT + normals[i2 * 3 + 2] * cosT) * sign;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
for (let py = minY; py <= maxY; py++) {
|
|
292
|
+
const cy = py + 0.5;
|
|
293
|
+
for (let px = minX; px <= maxX; px++) {
|
|
294
|
+
const cx = px + 0.5,
|
|
295
|
+
w0 = ((sx1 - cx) * (sy2 - cy) - (sy1 - cy) * (sx2 - cx)) * invArea,
|
|
296
|
+
w1 = ((sx2 - cx) * (sy0 - cy) - (sy2 - cy) * (sx0 - cx)) * invArea,
|
|
297
|
+
w2 = 1 - w0 - w1;
|
|
298
|
+
if (w0 < -1e-4 || w1 < -1e-4 || w2 < -1e-4) continue;
|
|
299
|
+
|
|
300
|
+
const z = w0 * r0z + w1 * r1z + w2 * r2z,
|
|
301
|
+
idx = py * capSize + px;
|
|
302
|
+
if (z <= depthBuf[idx]) continue;
|
|
303
|
+
|
|
304
|
+
let nx = w0 * vn0x + w1 * vn1x + w2 * vn2x,
|
|
305
|
+
ny = w0 * vn0y + w1 * vn1y + w2 * vn2y,
|
|
306
|
+
nz = w0 * vn0z + w1 * vn1z + w2 * vn2z;
|
|
307
|
+
const nLen = Math.hypot(nx, ny, nz) || 1;
|
|
308
|
+
nx /= nLen;
|
|
309
|
+
ny /= nLen;
|
|
310
|
+
nz /= nLen;
|
|
311
|
+
if (nz < 0) {
|
|
312
|
+
nx = -nx;
|
|
313
|
+
ny = -ny;
|
|
314
|
+
nz = -nz;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (isLeaf) {
|
|
318
|
+
// Perforate grazing leaf-blob rims like the 3D foliage shader so crowns keep airy sky gaps.
|
|
319
|
+
const rim = 1 - nz,
|
|
320
|
+
leafNoise = hashCoords2D(px + tri * 3, py + tri * 7, 911);
|
|
321
|
+
if (leafNoise < rim * rim * 0.52 - 0.08) continue;
|
|
322
|
+
|
|
323
|
+
// Blend geometric normal with volumetric crown normal so sunward and shaded sides separate cleanly.
|
|
324
|
+
const wx = w0 * r0x + w1 * r1x + w2 * r2x,
|
|
325
|
+
wy = w0 * p0y + w1 * p1y + w2 * p2y,
|
|
326
|
+
mx = wx / maxRadius,
|
|
327
|
+
my = (wy - crownCenterY) / crownHalfH,
|
|
328
|
+
mz = Math.max(0.15, z / maxRadius),
|
|
329
|
+
mLen = Math.hypot(mx, my, mz) || 1;
|
|
330
|
+
nx = nx * 0.48 + (mx / mLen) * 0.52;
|
|
331
|
+
ny = ny * 0.48 + (my / mLen) * 0.52;
|
|
332
|
+
nz = nz * 0.48 + (mz / mLen) * 0.52;
|
|
333
|
+
const bLen = Math.hypot(nx, ny, nz) || 1;
|
|
334
|
+
nx /= bLen;
|
|
335
|
+
ny /= bLen;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
depthBuf[idx] = z;
|
|
339
|
+
lumBuf[idx] = (w0 * l0 + w1 * l1 + w2 * l2) * (isLeaf ? spec.foliageGain : 1);
|
|
340
|
+
nxBuf[idx] = nx;
|
|
341
|
+
nyBuf[idx] = ny;
|
|
342
|
+
hitBuf[idx] = 1;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return { spanX, spanY, yOrigin, yMin, yMax };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Captures real 3D tree prototype geometries from 8 horizontal headings at 2x supersampling into a single
|
|
352
|
+
* RGBA8 atlas (`R`: relative luminance, `G`/`B`: view-aligned normal XY, `A`: coverage-preserved alpha).
|
|
353
|
+
*
|
|
354
|
+
* @param {Array<THREE.BufferGeometry | THREE.Mesh | object>} prototypes Tree prototypes to bake into atlas rows.
|
|
355
|
+
* @param {object} [options]
|
|
356
|
+
* @param {number} [options.headings=8] Number of horizontal headings around `2 * PI`.
|
|
357
|
+
* @param {number} [options.tileSize=64] Output texel width and height of each heading tile at mip 0.
|
|
358
|
+
* @param {number} [options.supersample=2] Supersampling factor per axis during prototype rasterization.
|
|
359
|
+
* @param {number} [options.referenceThreshold=0.5] Alpha cutoff preserved across mip levels.
|
|
360
|
+
* @param {number} [options.dilatePasses=5] Outward border dilation passes per tile.
|
|
361
|
+
* @param {number} [options.margin=0.09] Fractional empty border inside each tile so mips never bleed across tiles.
|
|
362
|
+
* @returns {object} Baked impostor atlas `{ texture, mipmaps, width, height, tileSize, headings, species, dispose }`.
|
|
363
|
+
*/
|
|
364
|
+
export function createTreeImpostorAtlas(prototypes, options = {}) {
|
|
365
|
+
if (!Array.isArray(prototypes) || !prototypes.length) {
|
|
366
|
+
throw new RangeError('createTreeImpostorAtlas requires at least one prototype');
|
|
367
|
+
}
|
|
368
|
+
if (prototypes.length > MAX_SPECIES) {
|
|
369
|
+
throw new RangeError(`createTreeImpostorAtlas supports at most ${MAX_SPECIES} prototypes per atlas`);
|
|
370
|
+
}
|
|
371
|
+
const headings = Math.max(1, options.headings ?? DEFAULT_HEADINGS),
|
|
372
|
+
tileSize = Math.max(8, options.tileSize ?? DEFAULT_TILE_SIZE),
|
|
373
|
+
supersample = Math.max(1, options.supersample ?? DEFAULT_SUPERSAMPLE),
|
|
374
|
+
referenceThreshold = options.referenceThreshold ?? DEFAULT_ALPHA_CUTOFF,
|
|
375
|
+
dilatePasses = options.dilatePasses ?? 5,
|
|
376
|
+
margin = clamp(options.margin ?? DEFAULT_MARGIN, 0.02, 0.25),
|
|
377
|
+
capSize = tileSize * supersample,
|
|
378
|
+
speciesCount = prototypes.length,
|
|
379
|
+
atlasWidth = tileSize * headings,
|
|
380
|
+
atlasHeight = tileSize * speciesCount,
|
|
381
|
+
atlasRgba = new Uint8Array(atlasWidth * atlasHeight * 4),
|
|
382
|
+
invSampleArea = 1 / (supersample * supersample);
|
|
383
|
+
|
|
384
|
+
const depthBuf = new Float32Array(capSize * capSize),
|
|
385
|
+
lumBuf = new Float32Array(capSize * capSize),
|
|
386
|
+
nxBuf = new Float32Array(capSize * capSize),
|
|
387
|
+
nyBuf = new Float32Array(capSize * capSize),
|
|
388
|
+
hitBuf = new Uint8Array(capSize * capSize),
|
|
389
|
+
species = [];
|
|
390
|
+
|
|
391
|
+
for (let s = 0; s < speciesCount; s++) {
|
|
392
|
+
const spec = inspectPrototype(prototypes[s]);
|
|
393
|
+
let bounds = null;
|
|
394
|
+
for (let h = 0; h < headings; h++) {
|
|
395
|
+
const theta = (h / headings) * TAU;
|
|
396
|
+
bounds = rasterizeHeading(spec, theta, margin, capSize, depthBuf, lumBuf, nxBuf, nyBuf, hitBuf);
|
|
397
|
+
const tileX0 = h * tileSize,
|
|
398
|
+
tileY0 = s * tileSize;
|
|
399
|
+
|
|
400
|
+
for (let ty = 0; ty < tileSize; ty++) {
|
|
401
|
+
for (let tx = 0; tx < tileSize; tx++) {
|
|
402
|
+
let hits = 0,
|
|
403
|
+
sumLum = 0,
|
|
404
|
+
sumNx = 0,
|
|
405
|
+
sumNy = 0;
|
|
406
|
+
const syBase = ty * supersample,
|
|
407
|
+
sxBase = tx * supersample;
|
|
408
|
+
for (let sy = 0; sy < supersample; sy++) {
|
|
409
|
+
let capIdx = (syBase + sy) * capSize + sxBase;
|
|
410
|
+
for (let sx = 0; sx < supersample; sx++, capIdx++) {
|
|
411
|
+
if (!hitBuf[capIdx]) continue;
|
|
412
|
+
hits++;
|
|
413
|
+
sumLum += lumBuf[capIdx];
|
|
414
|
+
sumNx += nxBuf[capIdx];
|
|
415
|
+
sumNy += nyBuf[capIdx];
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const outOffset = ((tileY0 + ty) * atlasWidth + (tileX0 + tx)) * 4;
|
|
419
|
+
if (hits > 0) {
|
|
420
|
+
const meanLum = sumLum / hits;
|
|
421
|
+
let nx = sumNx / hits,
|
|
422
|
+
ny = sumNy / hits;
|
|
423
|
+
const xySq = nx * nx + ny * ny;
|
|
424
|
+
if (xySq > 0.96) {
|
|
425
|
+
const inv = Math.sqrt(0.96 / xySq);
|
|
426
|
+
nx *= inv;
|
|
427
|
+
ny *= inv;
|
|
428
|
+
}
|
|
429
|
+
atlasRgba[outOffset] = clamp(Math.round((meanLum / spec.referenceLum) * 127.5), 0, 255);
|
|
430
|
+
atlasRgba[outOffset + 1] = clamp(Math.round((nx * 0.5 + 0.5) * 255), 0, 255);
|
|
431
|
+
atlasRgba[outOffset + 2] = clamp(Math.round((ny * 0.5 + 0.5) * 255), 0, 255);
|
|
432
|
+
atlasRgba[outOffset + 3] = clamp(Math.round(hits * invSampleArea * 255), 0, 255);
|
|
433
|
+
} else {
|
|
434
|
+
atlasRgba[outOffset] = 0;
|
|
435
|
+
atlasRgba[outOffset + 1] = 128;
|
|
436
|
+
atlasRgba[outOffset + 2] = 192;
|
|
437
|
+
atlasRgba[outOffset + 3] = 0;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
species.push({
|
|
443
|
+
width: bounds.spanX,
|
|
444
|
+
height: bounds.spanY,
|
|
445
|
+
yOrigin: bounds.yOrigin,
|
|
446
|
+
yMin: bounds.yMin,
|
|
447
|
+
yMax: bounds.yMax,
|
|
448
|
+
margin,
|
|
449
|
+
crownStart: spec.crownStart,
|
|
450
|
+
crownWidthScale: spec.crownWidthScale,
|
|
451
|
+
silhouette: spec.silhouette,
|
|
452
|
+
silhouetteId: spec.silhouetteId,
|
|
453
|
+
crownColor: spec.crownColor,
|
|
454
|
+
trunkColor: spec.trunkColor,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const mipmaps = buildCoveragePreservingMipmaps(atlasRgba, atlasWidth, atlasHeight, {
|
|
459
|
+
referenceThreshold,
|
|
460
|
+
dilatePasses,
|
|
461
|
+
tileWidth: tileSize,
|
|
462
|
+
tileHeight: tileSize,
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
const texture = new THREE.DataTexture(mipmaps[0].data, atlasWidth, atlasHeight, THREE.RGBAFormat, THREE.UnsignedByteType);
|
|
466
|
+
texture.mipmaps = mipmaps;
|
|
467
|
+
texture.generateMipmaps = false;
|
|
468
|
+
texture.minFilter = THREE.LinearMipmapLinearFilter;
|
|
469
|
+
texture.magFilter = THREE.LinearFilter;
|
|
470
|
+
texture.wrapS = THREE.ClampToEdgeWrapping;
|
|
471
|
+
texture.wrapT = THREE.ClampToEdgeWrapping;
|
|
472
|
+
texture.colorSpace = THREE.NoColorSpace;
|
|
473
|
+
texture.flipY = false;
|
|
474
|
+
texture.needsUpdate = true;
|
|
475
|
+
|
|
476
|
+
return {
|
|
477
|
+
texture,
|
|
478
|
+
mipmaps,
|
|
479
|
+
width: atlasWidth,
|
|
480
|
+
height: atlasHeight,
|
|
481
|
+
tileSize,
|
|
482
|
+
headings,
|
|
483
|
+
species,
|
|
484
|
+
dispose() {
|
|
485
|
+
texture.dispose();
|
|
486
|
+
},
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function buildUniformArrays(atlas, analyticDistance, alphaCutoff) {
|
|
491
|
+
const boundsArray = Array.from({ length: MAX_SPECIES }, () => new THREE.Vector4(1, 1, 0, 1)),
|
|
492
|
+
shapeArray = Array.from({ length: MAX_SPECIES }, () => new THREE.Vector4(0.3, 1, 1, 0.09)),
|
|
493
|
+
crownArray = Array.from({ length: MAX_SPECIES }, () => new THREE.Vector3(0.22, 0.34, 0.18)),
|
|
494
|
+
trunkArray = Array.from({ length: MAX_SPECIES }, () => new THREE.Vector3(0.14, 0.1, 0.07));
|
|
495
|
+
for (let i = 0; i < atlas.species.length; i++) {
|
|
496
|
+
const s = atlas.species[i];
|
|
497
|
+
boundsArray[i].set(s.width, s.height, s.yOrigin, s.crownWidthScale);
|
|
498
|
+
shapeArray[i].set(s.crownStart, s.silhouetteId, 1, s.margin);
|
|
499
|
+
crownArray[i].copy(s.crownColor);
|
|
500
|
+
trunkArray[i].copy(s.trunkColor);
|
|
501
|
+
}
|
|
502
|
+
return {
|
|
503
|
+
uImpAtlas: { value: atlas.texture },
|
|
504
|
+
uImpGrid: { value: new THREE.Vector4(atlas.headings, atlas.species.length, atlas.tileSize, analyticDistance) },
|
|
505
|
+
uImpLens: { value: new THREE.Vector2(1, alphaCutoff) },
|
|
506
|
+
uImpBounds: { value: boundsArray },
|
|
507
|
+
uImpShape: { value: shapeArray },
|
|
508
|
+
uImpCrown: { value: crownArray },
|
|
509
|
+
uImpTrunk: { value: trunkArray },
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const IMPOSTOR_VERTEX_PARS = /* glsl */ `
|
|
514
|
+
attribute vec4 aImpostorPos;
|
|
515
|
+
attribute vec4 aImpostorMeta;
|
|
516
|
+
uniform vec4 uImpGrid;
|
|
517
|
+
uniform vec4 uImpBounds[16];
|
|
518
|
+
uniform vec4 uImpShape[16];
|
|
519
|
+
uniform vec3 uImpCrown[16];
|
|
520
|
+
uniform vec3 uImpTrunk[16];
|
|
521
|
+
varying vec2 vImpUv;
|
|
522
|
+
varying vec4 vImpHeading;
|
|
523
|
+
varying vec4 vImpShape;
|
|
524
|
+
varying vec3 vImpCrownColor;
|
|
525
|
+
varying vec3 vImpTrunkColor;
|
|
526
|
+
varying vec3 vImpRightView;
|
|
527
|
+
varying vec3 vImpUpView;
|
|
528
|
+
varying vec3 vImpFwdView;
|
|
529
|
+
`;
|
|
530
|
+
|
|
531
|
+
const IMPOSTOR_VERTEX_MAIN = /* glsl */ `
|
|
532
|
+
int impSpecies = clamp(int(aImpostorMeta.z + 0.5), 0, 15);
|
|
533
|
+
vec4 impBounds = uImpBounds[impSpecies];
|
|
534
|
+
vec4 impShape = uImpShape[impSpecies];
|
|
535
|
+
vImpCrownColor = uImpCrown[impSpecies];
|
|
536
|
+
vImpTrunkColor = uImpTrunk[impSpecies];
|
|
537
|
+
vImpUv = uv;
|
|
538
|
+
|
|
539
|
+
vec3 impBaseWorld = (modelMatrix * vec4(aImpostorPos.xyz, 1.0)).xyz;
|
|
540
|
+
vec3 toEye = cameraPosition - impBaseWorld;
|
|
541
|
+
vec2 flatToEye = toEye.xz;
|
|
542
|
+
float flatLen = length(flatToEye);
|
|
543
|
+
vec2 fwdXZ = flatLen > 1e-4 ? flatToEye / flatLen : vec2(0.0, 1.0);
|
|
544
|
+
vec3 worldRight = vec3(fwdXZ.y, 0.0, -fwdXZ.x);
|
|
545
|
+
vec3 eyeDir = normalize(length(toEye) > 1e-4 ? toEye : vec3(0.0, 0.0, 1.0));
|
|
546
|
+
vec3 viewUp = normalize(cross(eyeDir, worldRight));
|
|
547
|
+
vec3 worldUp = normalize(mix(vec3(0.0, 1.0, 0.0), viewUp, 0.32));
|
|
548
|
+
vec3 worldFwd = normalize(cross(worldRight, worldUp));
|
|
549
|
+
|
|
550
|
+
float camAzimuth = atan(fwdXZ.x, fwdXZ.y);
|
|
551
|
+
float localAzimuth = mod(camAzimuth - aImpostorMeta.y, 6.28318530718);
|
|
552
|
+
float headingCoord = (localAzimuth / 6.28318530718) * uImpGrid.x;
|
|
553
|
+
float h0 = mod(floor(headingCoord), uImpGrid.x);
|
|
554
|
+
float h1 = mod(h0 + 1.0, uImpGrid.x);
|
|
555
|
+
vImpHeading = vec4(h0, h1, fract(headingCoord), float(impSpecies));
|
|
556
|
+
vImpShape = vec4(impShape.x, impShape.y, aImpostorMeta.w, impShape.w);
|
|
557
|
+
|
|
558
|
+
float crownWiden = mix(1.0, impBounds.w, smoothstep(impShape.x * 0.6, impShape.x + 0.18, uv.y));
|
|
559
|
+
float quadX = position.x * impBounds.x * aImpostorPos.w * crownWiden;
|
|
560
|
+
float quadY = (impBounds.z + position.y * impBounds.y) * aImpostorMeta.x;
|
|
561
|
+
vec3 impWorldPos = impBaseWorld + worldRight * quadX + worldUp * quadY;
|
|
562
|
+
|
|
563
|
+
vImpRightView = normalize((viewMatrix * vec4(worldRight, 0.0)).xyz);
|
|
564
|
+
vImpUpView = normalize((viewMatrix * vec4(worldUp, 0.0)).xyz);
|
|
565
|
+
vImpFwdView = normalize((viewMatrix * vec4(worldFwd, 0.0)).xyz);
|
|
566
|
+
transformedNormal = vImpFwdView;
|
|
567
|
+
`;
|
|
568
|
+
|
|
569
|
+
const IMPOSTOR_FRAGMENT_PARS = /* glsl */ `
|
|
570
|
+
uniform sampler2D uImpAtlas;
|
|
571
|
+
uniform vec4 uImpGrid;
|
|
572
|
+
uniform vec2 uImpLens;
|
|
573
|
+
varying vec2 vImpUv;
|
|
574
|
+
varying vec4 vImpHeading;
|
|
575
|
+
varying vec4 vImpShape;
|
|
576
|
+
varying vec3 vImpCrownColor;
|
|
577
|
+
varying vec3 vImpTrunkColor;
|
|
578
|
+
varying vec3 vImpRightView;
|
|
579
|
+
varying vec3 vImpUpView;
|
|
580
|
+
varying vec3 vImpFwdView;
|
|
581
|
+
vec3 gImpNormal = vec3(0.0, 0.0, 1.0);
|
|
582
|
+
float gImpCrownShare = 1.0;
|
|
583
|
+
|
|
584
|
+
void evaluateAnalyticSpecies(vec2 uv, float crownStart, float kind, float seed, float margin, out float outAlpha, out vec3 outNormal, out float outShade) {
|
|
585
|
+
float span = max(0.2, 1.0 - 2.0 * margin);
|
|
586
|
+
float ny = clamp((uv.y - margin) / span, 0.0, 1.0);
|
|
587
|
+
float cx = ((uv.x - 0.5) / (0.5 * span));
|
|
588
|
+
float ripple = 0.055 * sin(ny * 19.0 + seed * 31.0) + 0.04 * sin(cx * 11.0 - ny * 13.0 + seed * 17.0);
|
|
589
|
+
float aa = clamp(max(fwidth(cx), fwidth(ny)) * 0.9, 0.025, 0.18);
|
|
590
|
+
float stemWidth = 0.085 * (1.0 - 0.45 * ny);
|
|
591
|
+
float stemAlpha = (1.0 - smoothstep(stemWidth - aa, stemWidth + aa, abs(cx))) * (1.0 - smoothstep(crownStart + 0.14, crownStart + 0.28, ny));
|
|
592
|
+
float crownAlpha = 0.0;
|
|
593
|
+
vec3 crownNormal = vec3(0.0, 0.25, 0.968);
|
|
594
|
+
|
|
595
|
+
if (kind < 0.5) {
|
|
596
|
+
float t = clamp((ny - crownStart * 0.85) / max(0.15, 0.98 - crownStart * 0.85), 0.0, 1.0);
|
|
597
|
+
float whorls = 1.0 + 0.065 * sin(t * 24.0 + seed * 6.28318);
|
|
598
|
+
float halfW = (0.88 * pow(1.0 - t, 0.78) + 0.03) * whorls * (1.0 + ripple * 1.25);
|
|
599
|
+
float skirt = smoothstep(crownStart * 0.7, crownStart * 0.85 + 0.06, ny) * (1.0 - smoothstep(0.95, 1.0, ny));
|
|
600
|
+
crownAlpha = (1.0 - smoothstep(halfW - aa, halfW + aa, abs(cx))) * skirt;
|
|
601
|
+
float rx = clamp(cx / max(halfW, 0.08), -0.95, 0.95);
|
|
602
|
+
crownNormal = normalize(vec3(rx * 0.85, 0.28 + 0.22 * t, sqrt(max(0.08, 1.0 - rx * rx * 0.72))));
|
|
603
|
+
} else if (kind < 1.5) {
|
|
604
|
+
float yc = mix(crownStart, 0.96, 0.5);
|
|
605
|
+
float ry = max(0.16, (0.96 - crownStart) * 0.5);
|
|
606
|
+
float rx = 0.86 * (1.0 + ripple);
|
|
607
|
+
float dx = cx / rx;
|
|
608
|
+
float dy = (ny - yc) / ry;
|
|
609
|
+
float r = sqrt(dx * dx + dy * dy);
|
|
610
|
+
crownAlpha = 1.0 - smoothstep(1.0 - aa, 1.0 + aa, r);
|
|
611
|
+
float nr2 = min(0.92, dx * dx + dy * dy);
|
|
612
|
+
crownNormal = normalize(vec3(dx * 0.82, dy * 0.76 + 0.18, sqrt(max(0.08, 1.0 - nr2))));
|
|
613
|
+
} else {
|
|
614
|
+
float yc = mix(crownStart, 0.93, 0.58);
|
|
615
|
+
float ryUp = max(0.12, (0.94 - yc) * 0.92);
|
|
616
|
+
float ryDown = max(0.14, (yc - crownStart) * 0.95);
|
|
617
|
+
float dy = (ny - yc) / (ny > yc ? ryUp : ryDown);
|
|
618
|
+
float dx = cx / (0.92 * (1.0 + ripple * 1.15));
|
|
619
|
+
float r = sqrt(dx * dx + dy * dy);
|
|
620
|
+
crownAlpha = 1.0 - smoothstep(1.0 - aa, 1.0 + aa, r);
|
|
621
|
+
float nr2 = min(0.92, dx * dx * 0.7 + dy * dy);
|
|
622
|
+
crownNormal = normalize(vec3(dx * 0.72, dy * 0.84 + 0.34, sqrt(max(0.08, 1.0 - nr2))));
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
outAlpha = max(crownAlpha, stemAlpha);
|
|
626
|
+
outNormal = crownAlpha >= stemAlpha ? crownNormal : normalize(vec3(clamp(cx / max(stemWidth, 0.04), -0.9, 0.9), 0.05, 0.6));
|
|
627
|
+
outShade = mix(0.46, 0.34 + 0.34 * smoothstep(crownStart, 0.95, ny) - 0.08 * abs(cx), step(stemAlpha, crownAlpha));
|
|
628
|
+
}
|
|
629
|
+
`;
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Creates a fog-, shadow-, and light-aware `MeshStandardMaterial` configured for 8-heading tree impostors.
|
|
633
|
+
*
|
|
634
|
+
* @param {object} atlas Atlas returned by `createTreeImpostorAtlas`.
|
|
635
|
+
* @param {object} [options]
|
|
636
|
+
* @param {number} [options.analyticDistance=600] Distance in metres (at unit lens magnification) around which mips blend into analytic species outlines.
|
|
637
|
+
* @param {number} [options.alphaCutoff=0.5] Normalized alpha cutoff matching the atlas's coverage-preserving mips.
|
|
638
|
+
* @param {boolean} [options.alphaToCoverage=true] Enable MSAA alpha-to-coverage alongside `fwidth`-sharpened alpha.
|
|
639
|
+
* @param {number} [options.roughness=0.95] Surface roughness.
|
|
640
|
+
* @returns {THREE.MeshStandardMaterial} Patched material with `.userData.impostorUniforms`.
|
|
641
|
+
*/
|
|
642
|
+
export function createTreeImpostorMaterial(atlas, options = {}) {
|
|
643
|
+
const analyticDistance = options.analyticDistance ?? DEFAULT_ANALYTIC_DISTANCE,
|
|
644
|
+
alphaCutoff = options.alphaCutoff ?? DEFAULT_ALPHA_CUTOFF,
|
|
645
|
+
alphaToCoverage = options.alphaToCoverage ?? true,
|
|
646
|
+
uniforms = buildUniformArrays(atlas, analyticDistance, alphaCutoff);
|
|
647
|
+
|
|
648
|
+
const material = new THREE.MeshStandardMaterial({
|
|
649
|
+
roughness: options.roughness ?? 0.95,
|
|
650
|
+
metalness: 0,
|
|
651
|
+
alphaToCoverage,
|
|
652
|
+
});
|
|
653
|
+
material.userData.impostorUniforms = uniforms;
|
|
654
|
+
|
|
655
|
+
material.onBeforeCompile = shader => {
|
|
656
|
+
Object.assign(shader.uniforms, uniforms);
|
|
657
|
+
shader.vertexShader = shader.vertexShader
|
|
658
|
+
.replace('#include <common>', '#include <common>\n' + IMPOSTOR_VERTEX_PARS)
|
|
659
|
+
.replace('#include <normal_vertex>', '#include <normal_vertex>\n' + IMPOSTOR_VERTEX_MAIN)
|
|
660
|
+
.replace(
|
|
661
|
+
'#include <project_vertex>',
|
|
662
|
+
/* glsl */ `
|
|
663
|
+
vec4 mvPosition = viewMatrix * vec4(impWorldPos, 1.0);
|
|
664
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
665
|
+
if (aImpostorMeta.x <= 0.0 || aImpostorPos.w <= 0.0) {
|
|
666
|
+
gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
|
|
667
|
+
}`,
|
|
668
|
+
)
|
|
669
|
+
.replace(
|
|
670
|
+
'#include <worldpos_vertex>',
|
|
671
|
+
/* glsl */ `
|
|
672
|
+
#if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined( USE_SHADOWMAP ) || defined( USE_TRANSMISSION ) || NUM_SPOT_LIGHT_COORDS > 0
|
|
673
|
+
vec4 worldPosition = vec4(impWorldPos, 1.0);
|
|
674
|
+
#endif`,
|
|
675
|
+
);
|
|
676
|
+
|
|
677
|
+
shader.fragmentShader = shader.fragmentShader
|
|
678
|
+
.replace('#include <common>', '#include <common>\n' + IMPOSTOR_FRAGMENT_PARS)
|
|
679
|
+
.replace(
|
|
680
|
+
'#include <color_fragment>',
|
|
681
|
+
/* glsl */ `
|
|
682
|
+
#include <color_fragment>
|
|
683
|
+
{
|
|
684
|
+
float uvFoot = max(fwidth(vImpUv.x), fwidth(vImpUv.y));
|
|
685
|
+
float inset = clamp(uvFoot * 0.65, 0.5 / uImpGrid.z, 0.22);
|
|
686
|
+
vec2 localUv = clamp(vImpUv, vec2(inset), vec2(1.0 - inset));
|
|
687
|
+
vec2 tileScale = vec2(1.0 / uImpGrid.x, 1.0 / uImpGrid.y);
|
|
688
|
+
vec4 tap0 = texture2D(uImpAtlas, (vec2(vImpHeading.x, vImpHeading.w) + localUv) * tileScale);
|
|
689
|
+
vec4 tap1 = texture2D(uImpAtlas, (vec2(vImpHeading.y, vImpHeading.w) + localUv) * tileScale);
|
|
690
|
+
float hBlend = smoothstep(0.32, 0.68, vImpHeading.z);
|
|
691
|
+
vec4 atlasSample = mix(tap0, tap1, hBlend);
|
|
692
|
+
atlasSample.a = min(1.0, atlasSample.a * (1.0 + 0.52 * hBlend * (1.0 - hBlend)));
|
|
693
|
+
|
|
694
|
+
vec2 nXY = atlasSample.gb * 2.0 - 1.0;
|
|
695
|
+
float nZ = sqrt(max(0.04, 1.0 - min(0.96, dot(nXY, nXY))));
|
|
696
|
+
vec3 atlasNormal = normalize(vec3(nXY, nZ));
|
|
697
|
+
float atlasShade = atlasSample.r;
|
|
698
|
+
float rawAlpha = atlasSample.a;
|
|
699
|
+
|
|
700
|
+
float effectiveDist = length(vViewPosition) * uImpLens.x;
|
|
701
|
+
float analyticMix = smoothstep(uImpGrid.w * 0.76, uImpGrid.w * 1.24, effectiveDist);
|
|
702
|
+
if (analyticMix > 0.001) {
|
|
703
|
+
float silAlpha, silShade;
|
|
704
|
+
vec3 silNormal;
|
|
705
|
+
evaluateAnalyticSpecies(vImpUv, vImpShape.x, vImpShape.y, vImpShape.z, vImpShape.w, silAlpha, silNormal, silShade);
|
|
706
|
+
rawAlpha = mix(rawAlpha, silAlpha, analyticMix);
|
|
707
|
+
atlasNormal = normalize(mix(atlasNormal, silNormal, analyticMix * 0.85));
|
|
708
|
+
atlasShade = mix(atlasShade, silShade, analyticMix * 0.65);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
float fw = max(fwidth(rawAlpha), 0.045);
|
|
712
|
+
float sharpAlpha = clamp((rawAlpha - uImpLens.y) / fw + 0.5, 0.0, 1.0);
|
|
713
|
+
if (sharpAlpha < ${alphaToCoverage ? '0.32' : '0.5'}) discard;
|
|
714
|
+
diffuseColor.a = sharpAlpha;
|
|
715
|
+
|
|
716
|
+
float span = max(0.2, 1.0 - 2.0 * vImpShape.w);
|
|
717
|
+
float normY = clamp((vImpUv.y - vImpShape.w) / span, 0.0, 1.0);
|
|
718
|
+
gImpCrownShare = smoothstep(vImpShape.x - 0.06, vImpShape.x + 0.1, normY);
|
|
719
|
+
vec3 basePalette = mix(vImpTrunkColor, vImpCrownColor, gImpCrownShare);
|
|
720
|
+
diffuseColor.rgb *= basePalette * (atlasShade * 2.0);
|
|
721
|
+
gImpNormal = normalize(vImpRightView * atlasNormal.x + vImpUpView * atlasNormal.y + vImpFwdView * atlasNormal.z);
|
|
722
|
+
}`,
|
|
723
|
+
)
|
|
724
|
+
.replace(
|
|
725
|
+
'#include <normal_fragment_maps>',
|
|
726
|
+
/* glsl */ `
|
|
727
|
+
#include <normal_fragment_maps>
|
|
728
|
+
normal = gImpNormal;`,
|
|
729
|
+
)
|
|
730
|
+
.replace('#include <opaque_fragment>', thinSurfaceLight({ sky: 0.36, sun: 0.26, glow: 0.28, where: 'gImpCrownShare' }));
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
material.customProgramCacheKey = () => `rw-tree-impostor:${alphaToCoverage ? 'a2c' : 'cut'}`;
|
|
734
|
+
return material;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function createDepthMaterial(uniforms) {
|
|
738
|
+
const depthMat = new THREE.MeshDepthMaterial();
|
|
739
|
+
depthMat.onBeforeCompile = shader => {
|
|
740
|
+
Object.assign(shader.uniforms, uniforms);
|
|
741
|
+
shader.vertexShader = shader.vertexShader
|
|
742
|
+
.replace('#include <common>', '#include <common>\n' + IMPOSTOR_VERTEX_PARS)
|
|
743
|
+
.replace('#include <begin_vertex>', '#include <begin_vertex>\nvec3 transformedNormal;\n' + IMPOSTOR_VERTEX_MAIN)
|
|
744
|
+
.replace(
|
|
745
|
+
'#include <project_vertex>',
|
|
746
|
+
/* glsl */ `
|
|
747
|
+
vec4 mvPosition = viewMatrix * vec4(impWorldPos, 1.0);
|
|
748
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
749
|
+
if (aImpostorMeta.x <= 0.0 || aImpostorPos.w <= 0.0) {
|
|
750
|
+
gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
|
|
751
|
+
}`,
|
|
752
|
+
);
|
|
753
|
+
shader.fragmentShader = shader.fragmentShader.replace('#include <common>', '#include <common>\n' + IMPOSTOR_FRAGMENT_PARS).replace(
|
|
754
|
+
'#include <clipping_planes_fragment>',
|
|
755
|
+
/* glsl */ `
|
|
756
|
+
#include <clipping_planes_fragment>
|
|
757
|
+
{
|
|
758
|
+
vec2 tileScale = vec2(1.0 / uImpGrid.x, 1.0 / uImpGrid.y);
|
|
759
|
+
float a = texture2D(uImpAtlas, (vec2(vImpHeading.x, vImpHeading.w) + vImpUv) * tileScale).a;
|
|
760
|
+
if (a < uImpLens.y) discard;
|
|
761
|
+
}`,
|
|
762
|
+
);
|
|
763
|
+
};
|
|
764
|
+
depthMat.customProgramCacheKey = () => 'rw-tree-impostor-depth';
|
|
765
|
+
return depthMat;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
function createImpostorQuadGeometry() {
|
|
769
|
+
const geometry = new THREE.BufferGeometry();
|
|
770
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute([-0.5, 0, 0, 0.5, 0, 0, 0.5, 1, 0, -0.5, 1, 0], 3));
|
|
771
|
+
geometry.setAttribute('normal', new THREE.Float32BufferAttribute([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], 3));
|
|
772
|
+
geometry.setAttribute('uv', new THREE.Float32BufferAttribute([0, 0, 1, 0, 1, 1, 0, 1], 2));
|
|
773
|
+
geometry.setIndex([0, 1, 2, 0, 2, 3]);
|
|
774
|
+
return geometry;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Creates a reusable instanced billboard batch for distant trees backed by an 8-heading impostor atlas.
|
|
779
|
+
* Keeps one zero-scale sentinel instance (`count = 1`) when empty so startup shader compilation warms its program
|
|
780
|
+
* once behind loading bars with zero runtime shader recompiles.
|
|
781
|
+
*
|
|
782
|
+
* @param {object} atlas Atlas returned by `createTreeImpostorAtlas`.
|
|
783
|
+
* @param {object} [options]
|
|
784
|
+
* @param {number} [options.capacity=1024] Maximum number of tree instances in this batch.
|
|
785
|
+
* @param {THREE.MeshStandardMaterial} [options.material] Optional shared material from `createTreeImpostorMaterial(atlas)`.
|
|
786
|
+
* @param {boolean} [options.castShadow=false] Whether impostor billboards render into directional shadow maps.
|
|
787
|
+
* @param {boolean} [options.receiveShadow=true] Whether impostor billboards receive directional shadows.
|
|
788
|
+
* @param {number} [options.analyticDistance=600] Distance in metres past which crowns blend into analytic silhouettes.
|
|
789
|
+
* @param {number} [options.alphaCutoff=0.5] Normalized alpha cutoff.
|
|
790
|
+
* @param {boolean} [options.alphaToCoverage=true] Enable MSAA alpha-to-coverage.
|
|
791
|
+
* @param {THREE.Sphere} [options.bounds] Optional world-space bounding sphere for frustum culling.
|
|
792
|
+
* @returns {object} Batch controller `{ mesh, material, capacity, setInstance, setFromMatrix, commit, setLensScale, dispose }`.
|
|
793
|
+
*/
|
|
794
|
+
export function createTreeImpostorBatch(atlas, options = {}) {
|
|
795
|
+
const capacity = Math.max(1, options.capacity ?? 1024),
|
|
796
|
+
ownsMaterial = !options.material,
|
|
797
|
+
material =
|
|
798
|
+
options.material ??
|
|
799
|
+
createTreeImpostorMaterial(atlas, {
|
|
800
|
+
analyticDistance: options.analyticDistance,
|
|
801
|
+
alphaCutoff: options.alphaCutoff,
|
|
802
|
+
alphaToCoverage: options.alphaToCoverage,
|
|
803
|
+
}),
|
|
804
|
+
geometry = createImpostorQuadGeometry(),
|
|
805
|
+
posData = new Float32Array(capacity * 4),
|
|
806
|
+
metaData = new Float32Array(capacity * 4),
|
|
807
|
+
colorData = new Float32Array(capacity * 3).fill(1),
|
|
808
|
+
posAttr = new THREE.InstancedBufferAttribute(posData, 4),
|
|
809
|
+
metaAttr = new THREE.InstancedBufferAttribute(metaData, 4),
|
|
810
|
+
colorAttr = new THREE.InstancedBufferAttribute(colorData, 3);
|
|
811
|
+
|
|
812
|
+
posAttr.setUsage(THREE.DynamicDrawUsage);
|
|
813
|
+
metaAttr.setUsage(THREE.DynamicDrawUsage);
|
|
814
|
+
colorAttr.setUsage(THREE.DynamicDrawUsage);
|
|
815
|
+
geometry.setAttribute('aImpostorPos', posAttr);
|
|
816
|
+
geometry.setAttribute('aImpostorMeta', metaAttr);
|
|
817
|
+
|
|
818
|
+
const mesh = new THREE.InstancedMesh(geometry, material, capacity);
|
|
819
|
+
mesh.instanceColor = colorAttr;
|
|
820
|
+
mesh.castShadow = options.castShadow ?? false;
|
|
821
|
+
mesh.receiveShadow = options.receiveShadow ?? true;
|
|
822
|
+
mesh.matrixAutoUpdate = false;
|
|
823
|
+
if (mesh.castShadow) {
|
|
824
|
+
mesh.customDepthMaterial = createDepthMaterial(material.userData.impostorUniforms);
|
|
825
|
+
}
|
|
826
|
+
if (options.bounds) {
|
|
827
|
+
mesh.boundingSphere = options.bounds.clone();
|
|
828
|
+
geometry.boundingSphere = mesh.boundingSphere;
|
|
829
|
+
} else {
|
|
830
|
+
mesh.frustumCulled = false;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// Keep slot 0 as a zero-scale sentinel (`count = 1`) while empty so shader warmup compiles this mesh at startup.
|
|
834
|
+
mesh.count = 1;
|
|
835
|
+
let activeCount = 0;
|
|
836
|
+
|
|
837
|
+
function setInstance(slot, x, y, z, scaleX, scaleY, yaw, species, hash, r = 1, g = 1, b = 1) {
|
|
838
|
+
const p = slot * 4,
|
|
839
|
+
c = slot * 3;
|
|
840
|
+
posData[p] = x;
|
|
841
|
+
posData[p + 1] = y;
|
|
842
|
+
posData[p + 2] = z;
|
|
843
|
+
posData[p + 3] = scaleX;
|
|
844
|
+
metaData[p] = scaleY;
|
|
845
|
+
metaData[p + 1] = yaw;
|
|
846
|
+
metaData[p + 2] = species;
|
|
847
|
+
metaData[p + 3] = hash;
|
|
848
|
+
colorData[c] = r;
|
|
849
|
+
colorData[c + 1] = g;
|
|
850
|
+
colorData[c + 2] = b;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
function setFromMatrix(slot, matrices, matrixOffset, colors, colorOffset, species, hash) {
|
|
854
|
+
const m0 = matrices[matrixOffset],
|
|
855
|
+
m1 = matrices[matrixOffset + 1],
|
|
856
|
+
m2 = matrices[matrixOffset + 2],
|
|
857
|
+
m4 = matrices[matrixOffset + 4],
|
|
858
|
+
m5 = matrices[matrixOffset + 5],
|
|
859
|
+
m6 = matrices[matrixOffset + 6],
|
|
860
|
+
m8 = matrices[matrixOffset + 8],
|
|
861
|
+
m10 = matrices[matrixOffset + 10],
|
|
862
|
+
scaleX = Math.hypot(m0, m1, m2),
|
|
863
|
+
scaleY = Math.hypot(m4, m5, m6),
|
|
864
|
+
yaw = Math.atan2(m8, m10);
|
|
865
|
+
const r = colors ? colors[colorOffset] : 1,
|
|
866
|
+
g = colors ? colors[colorOffset + 1] : 1,
|
|
867
|
+
b = colors ? colors[colorOffset + 2] : 1;
|
|
868
|
+
setInstance(
|
|
869
|
+
slot,
|
|
870
|
+
matrices[matrixOffset + 12],
|
|
871
|
+
matrices[matrixOffset + 13],
|
|
872
|
+
matrices[matrixOffset + 14],
|
|
873
|
+
scaleX,
|
|
874
|
+
scaleY,
|
|
875
|
+
yaw,
|
|
876
|
+
species,
|
|
877
|
+
hash,
|
|
878
|
+
r,
|
|
879
|
+
g,
|
|
880
|
+
b,
|
|
881
|
+
);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
function commit(count) {
|
|
885
|
+
activeCount = clamp(count | 0, 0, capacity);
|
|
886
|
+
if (activeCount === 0) {
|
|
887
|
+
posData[3] = 0;
|
|
888
|
+
metaData[0] = 0;
|
|
889
|
+
mesh.count = 1;
|
|
890
|
+
for (const attr of [posAttr, metaAttr]) {
|
|
891
|
+
attr.clearUpdateRanges();
|
|
892
|
+
attr.addUpdateRange(0, attr.itemSize);
|
|
893
|
+
attr.needsUpdate = true;
|
|
894
|
+
}
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
mesh.count = activeCount;
|
|
898
|
+
for (const attr of [posAttr, metaAttr, colorAttr]) {
|
|
899
|
+
attr.clearUpdateRanges();
|
|
900
|
+
attr.addUpdateRange(0, activeCount * attr.itemSize);
|
|
901
|
+
attr.needsUpdate = true;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
function setLensScale(lensScale) {
|
|
906
|
+
const uniforms = material.userData.impostorUniforms;
|
|
907
|
+
if (uniforms) uniforms.uImpLens.value.x = 1 / Math.max(0.25, lensScale);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
function dispose() {
|
|
911
|
+
geometry.dispose();
|
|
912
|
+
if (mesh.customDepthMaterial) mesh.customDepthMaterial.dispose();
|
|
913
|
+
if (ownsMaterial) material.dispose();
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
return {
|
|
917
|
+
mesh,
|
|
918
|
+
material,
|
|
919
|
+
get count() {
|
|
920
|
+
return activeCount;
|
|
921
|
+
},
|
|
922
|
+
capacity,
|
|
923
|
+
setInstance,
|
|
924
|
+
setFromMatrix,
|
|
925
|
+
commit,
|
|
926
|
+
setLensScale,
|
|
927
|
+
dispose,
|
|
928
|
+
};
|
|
929
|
+
}
|