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.
Files changed (549) hide show
  1. package/.prettierrc.yaml +12 -0
  2. package/CONTRIBUTING.md +21 -14
  3. package/README.md +38 -42
  4. package/examples/animals.html +49 -0
  5. package/examples/animals.js +209 -0
  6. package/examples/crowds.html +50 -0
  7. package/examples/crowds.js +268 -0
  8. package/examples/effects.html +53 -0
  9. package/examples/effects.js +343 -0
  10. package/examples/figure-motion.html +55 -0
  11. package/examples/figure-motion.js +265 -0
  12. package/examples/figure.html +49 -0
  13. package/examples/figure.js +238 -0
  14. package/examples/fliers.html +53 -0
  15. package/examples/fliers.js +292 -0
  16. package/examples/fortification.html +54 -0
  17. package/examples/fortification.js +288 -0
  18. package/examples/house-kit.html +48 -0
  19. package/examples/house-kit.js +175 -0
  20. package/examples/index.html +26 -16
  21. package/examples/island.html +48 -0
  22. package/examples/island.js +134 -52
  23. package/examples/land.html +54 -0
  24. package/examples/land.js +392 -0
  25. package/examples/props-and-plants.html +53 -0
  26. package/examples/props-and-plants.js +275 -0
  27. package/examples/rooms.html +48 -0
  28. package/examples/rooms.js +242 -0
  29. package/examples/ships.html +51 -0
  30. package/examples/ships.js +235 -0
  31. package/examples/showcase.css +320 -0
  32. package/examples/small-world-land.js +151 -0
  33. package/examples/small-world-life.js +160 -0
  34. package/examples/small-world-town.js +127 -0
  35. package/examples/small-world.html +57 -0
  36. package/examples/small-world.js +226 -0
  37. package/examples/soundscape.html +138 -0
  38. package/examples/soundscape.js +467 -0
  39. package/examples/stage-kits.js +300 -0
  40. package/examples/stage.js +374 -0
  41. package/examples/towns.html +50 -0
  42. package/examples/towns.js +325 -0
  43. package/examples/viaduct-camera.js +8 -4
  44. package/examples/viaduct-landscape.js +28 -17
  45. package/examples/viaduct.html +45 -43
  46. package/examples/viaduct.js +68 -92
  47. package/geometry/animal-camel-body.js +410 -0
  48. package/geometry/animal-camel-head.js +118 -0
  49. package/geometry/animal-canine-body.js +361 -0
  50. package/geometry/animal-canine-breeds.js +316 -0
  51. package/geometry/animal-canine-head.js +337 -0
  52. package/geometry/animal-cattle-body.js +391 -0
  53. package/geometry/animal-cattle-coat.js +42 -0
  54. package/geometry/animal-cattle-head.js +152 -0
  55. package/geometry/animal-hen.js +248 -0
  56. package/geometry/animal-horse-body.js +446 -0
  57. package/geometry/animal-horse-bridle.js +171 -0
  58. package/geometry/animal-horse-coat.js +68 -0
  59. package/geometry/animal-horse-face.js +162 -0
  60. package/geometry/animal-horse-hair.js +304 -0
  61. package/geometry/animal-horse-head.js +133 -0
  62. package/geometry/animal-horse-tack.js +157 -0
  63. package/geometry/animal-options.js +59 -0
  64. package/geometry/animal-shapes.js +21 -0
  65. package/geometry/animal-skin.js +390 -0
  66. package/geometry/bedding.js +141 -0
  67. package/geometry/builder-cells.js +50 -0
  68. package/geometry/builder-frame.js +48 -0
  69. package/geometry/builder.js +200 -177
  70. package/geometry/card-emblem.js +437 -0
  71. package/geometry/catenary-canvas.js +396 -0
  72. package/geometry/cell-mask.js +25 -0
  73. package/geometry/cloth-folds.js +143 -0
  74. package/geometry/crawl-shapes.js +71 -0
  75. package/geometry/creased-sheet.js +40 -35
  76. package/geometry/crowd-dress.js +315 -0
  77. package/geometry/crowd-far.js +174 -0
  78. package/geometry/crowd-look.js +42 -0
  79. package/geometry/crowd-weld.js +218 -0
  80. package/geometry/ear.js +44 -39
  81. package/geometry/erosion.js +71 -45
  82. package/geometry/face-grid.js +85 -65
  83. package/geometry/field-map.js +59 -0
  84. package/geometry/figure-arms.js +339 -0
  85. package/geometry/figure-beard.js +250 -0
  86. package/geometry/figure-body.js +297 -0
  87. package/geometry/figure-bow.js +105 -0
  88. package/geometry/figure-cloak.js +189 -0
  89. package/geometry/figure-clothes.js +337 -0
  90. package/geometry/figure-face-grids.js +75 -0
  91. package/geometry/figure-gear.js +446 -0
  92. package/geometry/figure-hair-far.js +151 -0
  93. package/geometry/figure-hair-hangs.js +108 -0
  94. package/geometry/figure-hair-patch.js +66 -0
  95. package/geometry/figure-hair.js +320 -0
  96. package/geometry/figure-hands.js +321 -0
  97. package/geometry/figure-head-features.js +280 -0
  98. package/geometry/figure-head.js +375 -0
  99. package/geometry/figure-headwear.js +223 -0
  100. package/geometry/figure-legs.js +231 -0
  101. package/geometry/figure-limbs.js +39 -0
  102. package/geometry/figure-sculpt.js +82 -0
  103. package/geometry/figure-skin.js +97 -0
  104. package/geometry/figure-skirts.js +303 -0
  105. package/geometry/figure-trunk.js +411 -0
  106. package/geometry/flier-corvid-body.js +322 -0
  107. package/geometry/flier-corvid-wings.js +154 -0
  108. package/geometry/flier-long-body.js +195 -0
  109. package/geometry/flier-shapes.js +219 -0
  110. package/geometry/flier-soaring-bird.js +84 -0
  111. package/geometry/foliage.js +187 -14
  112. package/geometry/fort-city-walls.js +479 -0
  113. package/geometry/fort-gate-approach.js +233 -0
  114. package/geometry/fort-great-walls.js +280 -0
  115. package/geometry/fort-keep.js +297 -0
  116. package/geometry/fort-parts.js +261 -0
  117. package/geometry/fort-polyline.js +261 -0
  118. package/geometry/fort-portal.js +96 -0
  119. package/geometry/fort-ring-wall.js +267 -0
  120. package/geometry/fort-seawall.js +104 -0
  121. package/geometry/fort-terrace-gates.js +323 -0
  122. package/geometry/fort-terrace-plan.js +250 -0
  123. package/geometry/fort-terrace-wall.js +345 -0
  124. package/geometry/garment-tags.js +11 -3
  125. package/geometry/grid-gather.js +69 -0
  126. package/geometry/grid-sample.js +185 -0
  127. package/geometry/hair-gathers.js +57 -42
  128. package/geometry/hair-locks.js +42 -42
  129. package/geometry/hair-patch.js +24 -20
  130. package/geometry/hair-shell.js +43 -31
  131. package/geometry/head-presets.js +103 -0
  132. package/geometry/head-profile.js +26 -15
  133. package/geometry/head-shape.js +123 -95
  134. package/geometry/heightfield.js +50 -24
  135. package/geometry/horizon-angles.js +166 -0
  136. package/geometry/house-kit-body.js +195 -0
  137. package/geometry/house-kit-courtyard-parts.js +340 -0
  138. package/geometry/house-kit-courtyard-roof.js +223 -0
  139. package/geometry/house-kit-courtyard.js +409 -0
  140. package/geometry/house-kit-cut.js +29 -0
  141. package/geometry/house-kit-extras.js +192 -0
  142. package/geometry/house-kit-forms.js +298 -0
  143. package/geometry/house-kit-frame.js +74 -0
  144. package/geometry/house-kit-marks.js +93 -0
  145. package/geometry/house-kit-openings.js +193 -0
  146. package/geometry/house-kit-palette.js +54 -0
  147. package/geometry/house-kit-pool.js +71 -0
  148. package/geometry/house-kit-roofs.js +151 -0
  149. package/geometry/house-kit.js +202 -0
  150. package/geometry/index-parts.js +2 -2
  151. package/geometry/instance-visibility.js +12 -12
  152. package/geometry/instances.js +17 -17
  153. package/geometry/lamp-wash.js +65 -0
  154. package/geometry/land-clearings.js +83 -0
  155. package/geometry/land-ditches.js +290 -0
  156. package/geometry/land-dry-basin.js +327 -0
  157. package/geometry/land-dunes.js +150 -0
  158. package/geometry/land-earthworks.js +432 -0
  159. package/geometry/land-edge.js +292 -0
  160. package/geometry/land-mound-faces.js +349 -0
  161. package/geometry/land-near-relief.js +102 -0
  162. package/geometry/land-sheet.js +220 -0
  163. package/geometry/land-town-ground.js +256 -0
  164. package/geometry/land-waters.js +228 -0
  165. package/geometry/land-wildness.js +123 -0
  166. package/geometry/lathed-vessel.js +293 -0
  167. package/geometry/outline-grid.js +11 -10
  168. package/geometry/outline-segments.js +74 -0
  169. package/geometry/part-ranges.js +38 -3
  170. package/geometry/path-field.js +98 -34
  171. package/geometry/placement.js +52 -52
  172. package/geometry/planked-floor.js +149 -0
  173. package/geometry/plant-broadleaf.js +268 -0
  174. package/geometry/plant-shrub.js +198 -0
  175. package/geometry/plant-tints.js +80 -0
  176. package/geometry/plant-trees.js +453 -0
  177. package/geometry/plant-tussock.js +107 -0
  178. package/geometry/point-buckets.js +50 -0
  179. package/geometry/primitives.js +117 -59
  180. package/geometry/prop-battlefield.js +69 -0
  181. package/geometry/prop-camp.js +175 -0
  182. package/geometry/prop-door.js +26 -0
  183. package/geometry/prop-harbour.js +105 -0
  184. package/geometry/prop-haycock.js +60 -0
  185. package/geometry/prop-sacks.js +74 -0
  186. package/geometry/prop-sheaf.js +68 -0
  187. package/geometry/prop-street.js +69 -0
  188. package/geometry/prop-tents.js +396 -0
  189. package/geometry/prop-tombs.js +80 -0
  190. package/geometry/prop-trade.js +393 -0
  191. package/geometry/prop-vehicles.js +218 -0
  192. package/geometry/props.js +58 -0
  193. package/geometry/room-arcade.js +134 -0
  194. package/geometry/room-dome.js +188 -0
  195. package/geometry/room-flames.js +138 -0
  196. package/geometry/room-frame.js +106 -0
  197. package/geometry/room-furniture.js +260 -0
  198. package/geometry/room-hearth.js +98 -0
  199. package/geometry/room-kit.js +59 -0
  200. package/geometry/room-openings.js +142 -0
  201. package/geometry/room-stairs.js +180 -0
  202. package/geometry/room-surfaces.js +272 -0
  203. package/geometry/sagging-tube.js +27 -27
  204. package/geometry/sampled-surface.js +123 -77
  205. package/geometry/scatter-grid.js +49 -6
  206. package/geometry/sculpt.js +97 -93
  207. package/geometry/ship-fittings.js +350 -0
  208. package/geometry/ship-hull.js +460 -0
  209. package/geometry/ship-kit.js +208 -0
  210. package/geometry/ship-lines.js +69 -0
  211. package/geometry/ship-oars.js +150 -0
  212. package/geometry/ship-presets.js +131 -0
  213. package/geometry/ship-rig.js +374 -0
  214. package/geometry/ship-sails.js +148 -0
  215. package/geometry/shore-grid.js +81 -34
  216. package/geometry/strata-field.js +131 -98
  217. package/geometry/strata-tiles.js +108 -94
  218. package/geometry/stream.js +96 -59
  219. package/geometry/swept-tube.js +124 -0
  220. package/geometry/terrain-chunks.js +216 -123
  221. package/geometry/terrain-extension.js +29 -13
  222. package/geometry/town-courtyard-inn.js +272 -0
  223. package/geometry/town-ground.js +76 -0
  224. package/geometry/town-houses.js +343 -0
  225. package/geometry/town-lights.js +254 -0
  226. package/geometry/town-lots.js +172 -0
  227. package/geometry/town-plan.js +370 -0
  228. package/geometry/town-works.js +465 -0
  229. package/geometry/village-layout.js +181 -0
  230. package/geometry/village-yards.js +167 -0
  231. package/geometry/wall-runs.js +64 -0
  232. package/index.js +138 -2
  233. package/math/cell-edge.js +16 -16
  234. package/math/cell-key.js +6 -0
  235. package/math/circle-clearances.js +19 -0
  236. package/math/cloth-textures.js +6 -6
  237. package/math/cloud-textures.js +22 -26
  238. package/math/color.js +14 -14
  239. package/math/curve.js +26 -22
  240. package/math/lamp-texels.js +43 -0
  241. package/math/lit-share.js +22 -0
  242. package/math/mountain-ranges.js +379 -205
  243. package/math/noise.js +101 -49
  244. package/math/particle-noise.js +28 -28
  245. package/math/periodic.js +11 -5
  246. package/math/random.js +24 -15
  247. package/math/rectangles.js +58 -0
  248. package/math/rock-texture.js +22 -17
  249. package/math/scalar.js +5 -5
  250. package/math/surface-textures.js +74 -59
  251. package/math/wandering-path.js +40 -0
  252. package/math/ways.js +120 -0
  253. package/motion/action-dynamics.js +398 -0
  254. package/motion/animal-bearing.js +247 -0
  255. package/motion/animal-canine-bearing.js +178 -0
  256. package/motion/animal-canine-cast.js +131 -0
  257. package/motion/animal-cattle-cast.js +64 -0
  258. package/motion/animal-herd-cast.js +154 -0
  259. package/motion/animal-horse-still.js +161 -0
  260. package/motion/animal-troop-cast.js +43 -0
  261. package/motion/arm-reach.js +27 -26
  262. package/motion/ballistic-arc.js +191 -0
  263. package/motion/biped-cycle.js +46 -0
  264. package/motion/biped-leg.js +40 -0
  265. package/motion/biped-posture.js +459 -0
  266. package/motion/biped-stride.js +226 -140
  267. package/motion/boat-sailing.js +214 -0
  268. package/motion/contact-transition.js +32 -0
  269. package/motion/crowd-alarms.js +72 -42
  270. package/motion/crowd-attention.js +108 -0
  271. package/motion/crowd-beasts.js +203 -0
  272. package/motion/crowd-camp.js +295 -0
  273. package/motion/crowd-figure.js +100 -0
  274. package/motion/crowd-herd.js +86 -0
  275. package/motion/crowd-hold.js +130 -0
  276. package/motion/crowd-pose.js +296 -0
  277. package/motion/crowd-poses.js +481 -0
  278. package/motion/crowd-routine.js +126 -67
  279. package/motion/crowd-town.js +166 -0
  280. package/motion/crowd-water.js +143 -0
  281. package/motion/daily-clock.js +9 -9
  282. package/motion/deck-surface.js +40 -0
  283. package/motion/figure-acts.js +411 -0
  284. package/motion/figure-arms.js +437 -0
  285. package/motion/figure-blade-slide.js +149 -0
  286. package/motion/figure-body-place.js +180 -0
  287. package/motion/figure-bones.js +118 -0
  288. package/motion/figure-bow-draw.js +70 -0
  289. package/motion/figure-fingers.js +215 -0
  290. package/motion/figure-gaze.js +205 -0
  291. package/motion/figure-gestures.js +452 -0
  292. package/motion/figure-grip.js +134 -0
  293. package/motion/figure-head.js +287 -0
  294. package/motion/figure-legs.js +264 -0
  295. package/motion/figure-links.js +293 -0
  296. package/motion/figure-locomotion.js +391 -0
  297. package/motion/figure-mount-up.js +180 -0
  298. package/motion/figure-mount.js +201 -0
  299. package/motion/figure-pose-frame.js +375 -0
  300. package/motion/figure-pose-table.js +156 -0
  301. package/motion/figure-pose.js +343 -0
  302. package/motion/figure-seat.js +267 -0
  303. package/motion/figure-sway.js +125 -0
  304. package/motion/flier-corvid-pose.js +74 -0
  305. package/motion/flier-flight-air.js +122 -0
  306. package/motion/flier-flight-echelon.js +164 -0
  307. package/motion/flier-flight-roost.js +131 -0
  308. package/motion/flier-swarm-flight.js +252 -0
  309. package/motion/formation.js +193 -0
  310. package/motion/ground-lookahead.js +5 -5
  311. package/motion/instance-sight.js +15 -8
  312. package/motion/melee-contact.js +143 -0
  313. package/motion/moorings.js +48 -0
  314. package/motion/path-steering.js +358 -0
  315. package/motion/player.js +41 -32
  316. package/motion/quadruped.js +244 -168
  317. package/motion/spaces.js +90 -38
  318. package/motion/speech-cadence.js +304 -0
  319. package/motion/vessel-motion.js +57 -0
  320. package/motion/walk-grid.js +235 -0
  321. package/motion/weather.js +133 -26
  322. package/motion/wind.js +58 -13
  323. package/package.json +33 -6
  324. package/rendering/alpha-mips.js +340 -0
  325. package/rendering/animal-camel-figure.js +29 -0
  326. package/rendering/animal-canine-figure.js +59 -0
  327. package/rendering/animal-cattle-figure.js +36 -0
  328. package/rendering/animal-horse-figure.js +49 -0
  329. package/rendering/atmosphere.js +86 -0
  330. package/rendering/camera-track.js +156 -0
  331. package/rendering/cart-track.js +92 -0
  332. package/rendering/caster-motion.js +6 -5
  333. package/rendering/cell-lod-options.js +46 -0
  334. package/rendering/cell-lod.js +754 -113
  335. package/rendering/cloth-material.js +140 -0
  336. package/rendering/cloud-dome.js +30 -30
  337. package/rendering/cloud-field.js +2 -2
  338. package/rendering/compilation-schedule.js +79 -14
  339. package/rendering/contact-shadows.js +7 -4
  340. package/rendering/context.js +57 -6
  341. package/rendering/crowd-batches.js +420 -0
  342. package/rendering/crowd-herd.js +198 -0
  343. package/rendering/crowd-rig.js +132 -0
  344. package/rendering/crowd-skinning.js +62 -19
  345. package/rendering/crowd-update.js +419 -0
  346. package/rendering/crowd-visibility.js +107 -0
  347. package/rendering/crust.js +18 -12
  348. package/rendering/day-cycle-defaults.js +200 -0
  349. package/rendering/day-cycle.js +487 -0
  350. package/rendering/daylight.js +4 -6
  351. package/rendering/detail-range.js +29 -0
  352. package/rendering/dispose-meshes.js +18 -0
  353. package/rendering/dof.js +20 -13
  354. package/rendering/effects-frame.js +149 -0
  355. package/rendering/effects-water-rises.js +221 -0
  356. package/rendering/energy-stream.js +128 -0
  357. package/rendering/eye-adapt.js +14 -14
  358. package/rendering/facade-glass-material.js +105 -0
  359. package/rendering/far-shadow.js +89 -62
  360. package/rendering/field-surface.js +138 -0
  361. package/rendering/figure-shield.js +105 -0
  362. package/rendering/figure-skeleton.js +329 -0
  363. package/rendering/fire-light-watch.js +197 -0
  364. package/rendering/fire-pools.js +148 -0
  365. package/rendering/flame-sources.js +86 -0
  366. package/rendering/flier-bird-looks.js +97 -0
  367. package/rendering/flier-corvid-figure.js +67 -0
  368. package/rendering/flier-long-body-company.js +169 -0
  369. package/rendering/flier-swarm.js +186 -0
  370. package/rendering/flocks.js +82 -78
  371. package/rendering/garment-material.js +147 -51
  372. package/rendering/glow-lines.js +13 -13
  373. package/rendering/glsl-literal.js +19 -0
  374. package/rendering/ground-marks.js +316 -0
  375. package/rendering/hair-strands.js +45 -31
  376. package/rendering/height-haze.js +80 -33
  377. package/rendering/height-probe.js +164 -22
  378. package/rendering/house-kit-meshes.js +124 -0
  379. package/rendering/interior-bake.js +807 -0
  380. package/rendering/label-layout.js +56 -0
  381. package/rendering/label-range.js +41 -0
  382. package/rendering/lamp-light-map.js +159 -0
  383. package/rendering/lamp-maps.js +62 -0
  384. package/rendering/lamp-wash.js +51 -0
  385. package/rendering/light-budget.js +71 -55
  386. package/rendering/light-points.js +21 -5
  387. package/rendering/local-lights.js +39 -8
  388. package/rendering/material-lod.js +49 -50
  389. package/rendering/memory-estimate.js +84 -0
  390. package/rendering/moored-fleet.js +186 -0
  391. package/rendering/part-costs.js +104 -0
  392. package/rendering/particle-bursts.js +386 -0
  393. package/rendering/particle-emitters.js +135 -99
  394. package/rendering/particle-light-bursts.js +189 -0
  395. package/rendering/particles.js +212 -101
  396. package/rendering/pen-flocks.js +118 -0
  397. package/rendering/piece-batch.js +298 -11
  398. package/rendering/planar-reflection-options.js +159 -0
  399. package/rendering/planar-reflection.js +806 -103
  400. package/rendering/plant-cells.js +122 -0
  401. package/rendering/plant-reeds.js +113 -0
  402. package/rendering/plant-shade-trees.js +239 -0
  403. package/rendering/plant-tuft-patch.js +122 -0
  404. package/rendering/post-shaders.js +98 -31
  405. package/rendering/post.js +107 -87
  406. package/rendering/pressed-cover.js +135 -0
  407. package/rendering/prop-dig.js +136 -0
  408. package/rendering/rain-streaks.js +67 -0
  409. package/rendering/room-light.js +250 -0
  410. package/rendering/room-materials.js +78 -0
  411. package/rendering/room-space.js +111 -0
  412. package/rendering/scene-lights.js +106 -0
  413. package/rendering/sea-shaders.js +94 -37
  414. package/rendering/settlement-approach.js +226 -0
  415. package/rendering/settlement-dissolve.js +89 -0
  416. package/rendering/settlement-quarter-warm.js +55 -0
  417. package/rendering/settlement-quarter.js +477 -0
  418. package/rendering/settlement-switch.js +42 -0
  419. package/rendering/settlement-town.js +110 -0
  420. package/rendering/shader-warmup.js +101 -44
  421. package/rendering/shadow-alignment.js +4 -4
  422. package/rendering/shadow-depth.js +47 -25
  423. package/rendering/ship-meshes.js +150 -0
  424. package/rendering/shot-cuts.js +68 -0
  425. package/rendering/sky-occlusion.js +56 -29
  426. package/rendering/sky-pmrem.js +216 -0
  427. package/rendering/sky.js +126 -54
  428. package/rendering/smoke-shadows.js +201 -0
  429. package/rendering/stage-framing.js +182 -0
  430. package/rendering/stage-lights.js +46 -39
  431. package/rendering/stain-material.js +74 -0
  432. package/rendering/static-point-shadow.js +65 -26
  433. package/rendering/still-casters.js +77 -27
  434. package/rendering/strata-material.js +12 -12
  435. package/rendering/stream-flow.js +10 -10
  436. package/rendering/street-lamps.js +166 -0
  437. package/rendering/sunbeam-motes.js +670 -0
  438. package/rendering/surface-light.js +65 -6
  439. package/rendering/surface-material.js +306 -0
  440. package/rendering/surface-projection.js +32 -0
  441. package/rendering/surface-snippets.js +314 -0
  442. package/rendering/taa.js +75 -44
  443. package/rendering/talus.js +15 -15
  444. package/rendering/terrain-rock.js +7 -7
  445. package/rendering/texture-fills.js +8 -8
  446. package/rendering/textures.js +28 -28
  447. package/rendering/tree-impostor.js +929 -0
  448. package/rendering/vehicle-train.js +92 -0
  449. package/rendering/viewport.js +67 -4
  450. package/rendering/wakes.js +24 -24
  451. package/rendering/water-material.js +304 -103
  452. package/rendering/waypoint-beacons.js +191 -0
  453. package/rendering/world-matrices.js +38 -40
  454. package/ruff.toml +20 -0
  455. package/runtime/cue-cursor.js +21 -0
  456. package/runtime/device-quality.js +13 -2
  457. package/runtime/device.js +73 -0
  458. package/runtime/diagnostic-log.js +12 -4
  459. package/runtime/effect-sites.js +85 -0
  460. package/runtime/figure-orders.js +59 -0
  461. package/runtime/first-use.js +36 -28
  462. package/runtime/foley-script.js +124 -0
  463. package/runtime/foley-strides.js +333 -0
  464. package/runtime/frame-pacing.js +47 -0
  465. package/runtime/frame-yield.js +18 -0
  466. package/runtime/gpu-timing.js +15 -11
  467. package/runtime/grid-cache.js +16 -6
  468. package/runtime/grid-rows.js +44 -5
  469. package/runtime/hand-over.js +47 -24
  470. package/runtime/land-bake.js +78 -0
  471. package/runtime/lazy-sets.js +105 -0
  472. package/runtime/pool.js +73 -17
  473. package/runtime/quality-fit.js +60 -28
  474. package/runtime/region-pass-lay.js +40 -0
  475. package/runtime/region-pass.js +78 -0
  476. package/runtime/search.js +26 -22
  477. package/runtime/settlement-reach.js +42 -0
  478. package/runtime/sound-calls.js +113 -0
  479. package/runtime/sound-impacts.js +297 -0
  480. package/runtime/sound-voices.js +194 -0
  481. package/runtime/soundscape.js +282 -0
  482. package/runtime/spatial-audio.js +79 -30
  483. package/runtime/steady-frames.js +221 -0
  484. package/runtime/withdrawable-task.js +46 -0
  485. package/runtime/worker.js +7 -0
  486. package/scripts/app-parts.mjs +118 -0
  487. package/scripts/bake-revisions.mjs +81 -0
  488. package/scripts/browser-scripts.mjs +64 -0
  489. package/scripts/bundle.mjs +192 -0
  490. package/scripts/check-app.sh +26 -0
  491. package/scripts/eslint-config.mjs +119 -0
  492. package/scripts/format.sh +92 -0
  493. package/scripts/keep-builds.mjs +158 -0
  494. package/scripts/layers.mjs +112 -0
  495. package/scripts/lint.sh +49 -0
  496. package/scripts/module-graph.mjs +123 -0
  497. package/scripts/pre-commit.sh +38 -0
  498. package/scripts/serve.py +5 -3
  499. package/scripts/site.mjs +358 -0
  500. package/scripts/test-runner.sh +42 -0
  501. package/scripts/vendor.mjs +221 -0
  502. package/scripts/verify-publish.mjs +175 -0
  503. package/scripts/world.mk +59 -0
  504. package/tools/README.md +73 -0
  505. package/tools/contact-sheet.cjs +137 -0
  506. package/tools/cpu-profile.cjs +275 -0
  507. package/tools/cpu-profile.test.cjs +98 -0
  508. package/tools/first-draws.cjs +395 -0
  509. package/tools/flight-guard.cjs +482 -0
  510. package/tools/flight-guard.test.cjs +153 -0
  511. package/tools/flight-profile.cjs +145 -0
  512. package/tools/frame-bench.cjs +336 -0
  513. package/tools/frame-parts.cjs +365 -0
  514. package/tools/ios-sim.sh +145 -0
  515. package/tools/load-audit.cjs +201 -0
  516. package/tools/look-compare.cjs +304 -0
  517. package/tools/look-guard.cjs +407 -0
  518. package/tools/look-guard.test.cjs +93 -0
  519. package/tools/look-stats.cjs +185 -0
  520. package/tools/look-stats.test.cjs +71 -0
  521. package/tools/memory-audit.cjs +323 -0
  522. package/tools/page-driver.cjs +320 -0
  523. package/tools/page-driver.test.cjs +219 -0
  524. package/tools/program-census-report.cjs +191 -0
  525. package/tools/program-census.cjs +465 -0
  526. package/tools/program-census.test.cjs +332 -0
  527. package/tools/remote-runner.sh +265 -0
  528. package/tools/runners.test.cjs +59 -0
  529. package/tools/scene-audit-beat.cjs +344 -0
  530. package/tools/scene-audit-sheet.cjs +282 -0
  531. package/tools/scene-audit.cjs +471 -0
  532. package/tools/scene-audit.test.cjs +201 -0
  533. package/tools/scene-batch.cjs +219 -0
  534. package/tools/scene-bench.cjs +149 -0
  535. package/tools/scene-bench.test.cjs +33 -0
  536. package/tools/scene-run.cjs +443 -0
  537. package/tools/scene-run.test.cjs +94 -0
  538. package/tools/servers.sh +59 -0
  539. package/tools/shader-arrival.cjs +316 -0
  540. package/tools/shader-renewal.cjs +53 -0
  541. package/tools/shader-renewal.test.cjs +115 -0
  542. package/tools/speed-guard.cjs +258 -0
  543. package/tools/stills.cjs +492 -0
  544. package/tools/stills.test.cjs +49 -0
  545. package/tools/turns.sh +7 -0
  546. package/tools/yaml.cjs +158 -0
  547. package/examples/viaduct.css +0 -75
  548. package/rendering/light-shafts.js +0 -131
  549. package/rendering/road-surface.js +0 -63
@@ -1,117 +1,275 @@
1
1
  import * as THREE from 'three';
2
+ import { createTreeImpostorBatch, createTreeImpostorMaterial, hashCoords2D } from './tree-impostor.js';
3
+ import { resolveCellLodOptions } from './cell-lod-options.js';
4
+
5
+ const TIER_HIDDEN = 0;
6
+ const TIER_NEAR = 1;
7
+ const TIER_FAR = 2;
8
+ const TIER_IMPOSTOR = 3;
9
+
10
+ const MODE_NONE = 0;
11
+ const MODE_ALL_FAR = 1;
12
+ const MODE_ALL_IMPOSTOR = 2;
13
+ const MODE_MIXED = 3;
2
14
 
3
15
  /**
4
16
  * Level of detail for scattered things laid out in cells (trees, scrub, rocks, reeds). Each cell has a detailed instanced mesh for
5
- * near and, most of them, a low-detail instanced copy for far. Hundreds of far cells hold a handful of instances each, and each would
6
- * be a draw call in every pass, so the far copies are batched: one instanced mesh per kind and square of ground. In a batch every
7
- * instance knows its cell and its place in it, and the vertex shader hides it unless its cell shows its far copy and it is among the
8
- * cell's first `count` (the far thinning), read from a small table written each frame. What is drawn is exactly what the cells drew
9
- * one by one, and a batch none of whose cells shows is hidden, so no pass runs its vertices only to drop them.
17
+ * near and, most of them, a low-detail instanced copy for far (plus optional 8-heading billboard impostors beyond the far mesh).
18
+ * Hundreds of far cells hold a handful of instances each, and each would be a draw call in every pass, so the far copies are batched:
19
+ * one instanced mesh per kind and square of ground.
10
20
  *
11
- * A cell is a record: `mesh` (its near InstancedMesh), `lo` (its far InstancedMesh, or none), `cx`, `cz`, `r` (where it lies and its
12
- * radius, metres), optionally `until` (metres past which it is not drawn at all), `near` (metres past which it shows its far copy
13
- * sooner than the caller's), `full` (its instances before thinning). The batching writes `far`, `farN`, `farBatch` and `lod` on it.
21
+ * Each shadow-casting far batch mesh sets `mesh.userData.partShown = true` to mark that inactive instances are culled by its
22
+ * `customDepthMaterial` / `customDistanceMaterial` (or packed dynamically in `compact` mode), so external depth passes (such as
23
+ * sky-occlusion renderers) can recognise partially populated batches.
14
24
  *
15
- * @param {object} [options]
16
- * @param {number} [options.square=500] Metres: far copies are batched by kind within squares of this side.
17
- * @param {number} [options.tableWidth=64] Texels across the table of what each cell shows.
18
- * @param {number} [options.thinFrom=0.45] Beyond this share of the drawing distance a thinned cell keeps `thinTo` of its instances.
19
- * @param {number} [options.thinTo=0.55]
25
+ * When `instanceJitter > 0` or `impostorAtlas` is supplied, cells opt into per-instance hashed handover distances (`near -> far -> impostor`)
26
+ * so each tree transitions at its own deterministic jittered distance and no hard LOD ring ever shows. `update` also accepts
27
+ * `{ lensScale, holdHandover, farDistance, impostorDistance }` so narrow-FOV camera shots promote background stands and hold active
28
+ * LOD tiers steady between cuts. See `DEFAULT_CELL_LOD_OPTIONS` (`rendering/cell-lod-options.js`) for every option and its units.
29
+ *
30
+ * @param {Partial<import('./cell-lod-options.js').DEFAULT_CELL_LOD_OPTIONS>} [options] Cell-LOD configuration overrides.
20
31
  */
21
- export function createCellLod({ square = 500, tableWidth = 64, thinFrom = 0.45, thinTo = 0.55 } = {}) {
32
+ export function createCellLod(options = {}) {
33
+ const {
34
+ square,
35
+ compact,
36
+ tableWidth,
37
+ thinFrom,
38
+ thinTo,
39
+ thinImpostors,
40
+ jitter,
41
+ defaultJitter,
42
+ nearHysteresis,
43
+ farHysteresis,
44
+ impostorHysteresis,
45
+ farDistanceShare,
46
+ impostorDistanceShare,
47
+ impostorLensCap,
48
+ impostorAtlas,
49
+ impostorSquare,
50
+ } = resolveCellLodOptions(options);
22
51
  const table = { data: null, tex: null },
23
52
  shown = { value: null }, // the table, as the uniform the far materials and the far shadow body share
24
- batches = [];
25
- let batchShows = null,
26
- depth = null, distanceMaterial = null;
53
+ batches = [],
54
+ members = [],
55
+ impostorBatches = [],
56
+ impostorMembers = [];
57
+ let activeAtlas = impostorAtlas,
58
+ sharedImpostorMaterial = null,
59
+ drawEnds = null,
60
+ dirty = null,
61
+ impDirty = null,
62
+ depth = null,
63
+ distanceMaterial = null,
64
+ perInstanceTable = false,
65
+ hasSnapshot = false;
27
66
 
28
67
  // Hides, in the vertex shader, a copy whose cell is not showing its far copy or that lies past the cell's `count`.
29
- function hideUnshown(sh) {
30
- sh.uniforms.uFarShown = shown;
31
- sh.vertexShader = sh.vertexShader
68
+ function hideUnshown(shader) {
69
+ shader.uniforms.uFarShown = shown;
70
+ shader.vertexShader = shader.vertexShader
32
71
  .replace('#include <common>', '#include <common>\nattribute vec2 aFar; uniform sampler2D uFarShown;')
33
72
  .replace(
34
- '#include <project_vertex>',
35
- `/* glsl */
36
- #include <project_vertex>
37
- if (aFar.y >= texelFetch(uFarShown, ivec2(int(aFar.x) % ${tableWidth}, int(aFar.x) / ${tableWidth}), 0).r) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);`,
73
+ /void\s+main\s*\(\s*(?:void)?\s*\)\s*\{/,
74
+ match => `${match}
75
+ if (aFar.y >= texelFetch(uFarShown, ivec2(int(aFar.x) % ${tableWidth}, int(aFar.x) / ${tableWidth}), 0).r) {
76
+ gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
77
+ return;
78
+ }`,
38
79
  );
39
80
  }
81
+
40
82
  // A far copy's material: the cell's own, cloned once per material, with its own patch kept and the hiding added.
41
83
  function farMaterial(base, cache) {
42
84
  if (cache.has(base)) return cache.get(base);
43
- const m = base.clone(),
85
+ const material = base.clone(),
44
86
  inner = base.onBeforeCompile,
45
87
  key = base.customProgramCacheKey();
46
- m.onBeforeCompile = (sh, r) => {
47
- inner.call(base, sh, r);
48
- hideUnshown(sh);
88
+ material.onBeforeCompile = (shader, r) => {
89
+ inner.call(base, shader, r);
90
+ hideUnshown(shader);
49
91
  };
50
- m.customProgramCacheKey = () => key + ':far-batch';
51
- cache.set(base, m);
52
- return m;
92
+ material.customProgramCacheKey = () => key + ':far-batch';
93
+ cache.set(base, material);
94
+ return material;
53
95
  }
96
+
54
97
  // The shadow body of far copies that cast: the far shape itself, hidden as in the picture, so a cell casts what it shows.
55
98
  function depthMaterial() {
56
99
  if (depth) return depth;
57
- depth = new THREE.MeshDepthMaterial({ depthPacking: THREE.RGBADepthPacking });
58
- depth.onBeforeCompile = hideUnshown;
59
- depth.customProgramCacheKey = () => 'far-batch-depth';
100
+ depth = new THREE.MeshDepthMaterial();
101
+ if (!compact) {
102
+ depth.onBeforeCompile = hideUnshown;
103
+ depth.customProgramCacheKey = () => 'far-batch-depth';
104
+ }
60
105
  return depth;
61
106
  }
62
107
 
63
- /** Batches the far copies of `cells` (taken out of their parents) into `parent`; returns the batches. */
64
- function batch(cellList, parent) {
65
- const cells = cellList.filter(t => t.lo && t.lo.isInstancedMesh),
108
+ // Extracts immutable source attributes and deterministic per-instance world-space hashes once per cell.
109
+ function ensureInstanceMetadata(cell, at) {
110
+ if (at.px) return;
111
+ const srcMat = at.sourceMatrix.array,
112
+ total = at.total,
113
+ px = new Float32Array(total),
114
+ py = new Float32Array(total),
115
+ pz = new Float32Array(total),
116
+ hNear = new Float32Array(total),
117
+ hFar = new Float32Array(total),
118
+ hImp = new Float32Array(total);
119
+ for (let k = 0; k < total; k++) {
120
+ const mOff = k * 16,
121
+ x = srcMat[mOff + 12],
122
+ y = srcMat[mOff + 13],
123
+ z = srcMat[mOff + 14],
124
+ ix = Math.round(x * 13),
125
+ iz = Math.round(z * 13);
126
+ px[k] = x;
127
+ py[k] = y;
128
+ pz[k] = z;
129
+ hNear[k] = hashCoords2D(ix, iz, 101 + k);
130
+ hFar[k] = hashCoords2D(ix, iz, 307 + k);
131
+ hImp[k] = hashCoords2D(ix, iz, 613 + k);
132
+ }
133
+ at.px = px;
134
+ at.py = py;
135
+ at.pz = pz;
136
+ at.hNear = hNear;
137
+ at.hFar = hFar;
138
+ at.hImp = hImp;
139
+ at.tiers = new Uint8Array(total);
140
+ }
141
+
142
+ // Where a cell lies and how far it is drawn, kept on it in a record of one shape.
143
+ function lodOf(cell) {
144
+ if (cell.lod) return cell.lod;
145
+ // A cell whose near object is not instanced (a group, or one mesh on its own) has no copies to hand over one by one: it
146
+ // keeps no source matrices, and update shows it whole or not at all, by distance.
147
+ const sourceMatrix = cell.mesh?.instanceMatrix ?? null,
148
+ sourceColor = sourceMatrix ? (cell.mesh.instanceColor ?? null) : null;
149
+ cell.lod = {
150
+ cx: cell.cx,
151
+ cy: cell.cy ?? 0,
152
+ cz: cell.cz,
153
+ r: cell.r,
154
+ until: cell.until ?? Infinity,
155
+ near: cell.near ?? Infinity,
156
+ batch: cell.farBatch ?? -1,
157
+ impBatch: cell.impBatch ?? -1,
158
+ species: typeof cell.impostor === 'number' ? cell.impostor : -1,
159
+ sourceMatrix,
160
+ sourceColor,
161
+ total: sourceMatrix ? sourceMatrix.count : 0,
162
+ px: null,
163
+ py: null,
164
+ pz: null,
165
+ hNear: null,
166
+ hFar: null,
167
+ hImp: null,
168
+ tiers: null,
169
+ nearMatrix: null,
170
+ nearColor: null,
171
+ loMatrix: null,
172
+ loColor: null,
173
+ mode: MODE_NONE,
174
+ activeLimit: 0,
175
+ nearCount: 0,
176
+ farCount: 0,
177
+ impCount: 0,
178
+ };
179
+ return cell.lod;
180
+ }
181
+
182
+ /**
183
+ * Batches the far copies of `cellList` (taken out of their parents) into `parent` and, when an `impostorAtlas`
184
+ * is configured, builds spatial impostor batches for cells tagged with an `impostor` species index.
185
+ *
186
+ * @param {Array<object>} cellList Cells to batch.
187
+ * @param {THREE.Object3D} parent Group receiving the merged far and impostor batches.
188
+ * @param {object} [batchOptions]
189
+ * @param {object} [batchOptions.impostorAtlas] Optional atlas overriding `options.impostorAtlas`.
190
+ * @returns {Array<THREE.InstancedMesh>} The created far batch meshes.
191
+ */
192
+ function batch(cellList, parent, batchOptions = {}) {
193
+ if (batchOptions.impostorAtlas) activeAtlas = batchOptions.impostorAtlas;
194
+ hasSnapshot = false;
195
+
196
+ const cells = cellList.filter(cell => cell.lo && cell.lo.isInstancedMesh),
66
197
  groups = new Map(),
67
- materials = new Map();
68
- table.data = new Float32Array(tableWidth * Math.ceil(cells.length / tableWidth));
69
- table.tex = new THREE.DataTexture(table.data, tableWidth, table.data.length / tableWidth, THREE.RedFormat, THREE.FloatType);
70
- table.tex.needsUpdate = true;
71
- shown.value = table.tex;
72
- cells.forEach((t, slot) => {
73
- const lo = t.lo,
198
+ materials = new Map(),
199
+ anyImpostor = Boolean(activeAtlas) && cellList.some(cell => typeof cell.impostor === 'number' && cell.impostor >= 0);
200
+
201
+ perInstanceTable = !compact && (jitter > 0 || anyImpostor);
202
+ let totalFarSlots = cells.length;
203
+ if (perInstanceTable) {
204
+ totalFarSlots = 0;
205
+ for (let i = 0; i < cells.length; i++) {
206
+ cells[i].farOffset = totalFarSlots;
207
+ totalFarSlots += cells[i].lo.instanceMatrix.count;
208
+ }
209
+ }
210
+
211
+ table.data = new Float32Array(tableWidth * Math.max(1, Math.ceil(totalFarSlots / tableWidth)));
212
+ if (!compact && cells.length) {
213
+ table.tex = new THREE.DataTexture(table.data, tableWidth, table.data.length / tableWidth, THREE.RedFormat, THREE.FloatType);
214
+ table.tex.needsUpdate = true;
215
+ shown.value = table.tex;
216
+ }
217
+
218
+ cells.forEach((cell, slot) => {
219
+ lodOf(cell);
220
+ const lo = cell.lo,
74
221
  key = [
75
222
  lo.geometry.uuid,
76
223
  lo.material.uuid,
77
224
  lo.layers.mask,
78
225
  lo.receiveShadow,
79
- t.mesh.castShadow,
80
- Math.floor(t.cx / square),
81
- Math.floor(t.cz / square),
226
+ cell.mesh.castShadow,
227
+ Math.floor(cell.cx / square),
228
+ Math.floor(cell.cz / square),
82
229
  ].join('|');
83
- let g = groups.get(key);
84
- if (!g) groups.set(key, (g = { lo, casts: t.mesh.castShadow, cells: [] }));
85
- g.cells.push(t);
86
- t.far = slot;
87
- t.farN = lo.instanceMatrix.count;
88
- t.lo = null;
230
+ let group = groups.get(key);
231
+ if (!group) groups.set(key, (group = { lo, casts: cell.mesh.castShadow, cells: [] }));
232
+ group.cells.push(cell);
233
+ cell.far = slot;
234
+ cell.farN = lo.instanceMatrix.count;
235
+ cell.lo = null;
89
236
  lo.removeFromParent();
90
237
  });
238
+
91
239
  for (const { lo, casts, cells: list } of groups.values()) {
92
- for (const t of list) t.farBatch = batches.length;
93
- const n = list.reduce((s, t) => s + t.farN, 0),
94
- geo = new THREE.BufferGeometry();
95
- for (const name in lo.geometry.attributes) geo.setAttribute(name, lo.geometry.attributes[name]);
96
- geo.setIndex(lo.geometry.index);
97
- geo.boundingSphere = lo.geometry.boundingSphere;
98
- geo.boundingBox = lo.geometry.boundingBox;
99
- const ids = new Float32Array(n * 2),
100
- mesh = new THREE.InstancedMesh(geo, farMaterial(lo.material, materials), n);
101
- if (lo.instanceColor) mesh.instanceColor = new THREE.InstancedBufferAttribute(new Float32Array(n * 3), 3);
240
+ const instanceCount = list.reduce((total, cell) => total + cell.farN, 0),
241
+ geometry = new THREE.BufferGeometry();
242
+ for (const name in lo.geometry.attributes) geometry.setAttribute(name, lo.geometry.attributes[name]);
243
+ geometry.setIndex(lo.geometry.index);
244
+ geometry.boundingSphere = lo.geometry.boundingSphere;
245
+ geometry.boundingBox = lo.geometry.boundingBox;
246
+ const ids = compact ? null : new Float32Array(instanceCount * 2),
247
+ mesh = new THREE.InstancedMesh(geometry, compact ? lo.material : farMaterial(lo.material, materials), instanceCount);
248
+ if (lo.instanceColor) mesh.instanceColor = new THREE.InstancedBufferAttribute(new Float32Array(instanceCount * 3), 3);
102
249
  let at = 0;
103
- for (const t of list) {
104
- const src = t.mesh.instanceMatrix.array,
105
- col = t.mesh.instanceColor?.array;
106
- mesh.instanceMatrix.array.set(src.subarray(0, t.farN * 16), at * 16);
107
- if (col && mesh.instanceColor) mesh.instanceColor.array.set(col.subarray(0, t.farN * 3), at * 3);
108
- for (let i = 0; i < t.farN; i++) {
109
- ids[(at + i) * 2] = t.far;
110
- ids[(at + i) * 2 + 1] = i;
111
- }
112
- at += t.farN;
113
- }
114
- geo.setAttribute('aFar', new THREE.InstancedBufferAttribute(ids, 2));
250
+ for (const cell of list) {
251
+ const rec = lodOf(cell);
252
+ cell.farBatch = batches.length;
253
+ cell.farStart = at;
254
+ rec.batch = cell.farBatch;
255
+ const src = rec.sourceMatrix.array,
256
+ col = rec.sourceColor?.array;
257
+ mesh.instanceMatrix.array.set(src.subarray(0, cell.farN * 16), at * 16);
258
+ if (col && mesh.instanceColor) mesh.instanceColor.array.set(col.subarray(0, cell.farN * 3), at * 3);
259
+ if (ids) {
260
+ for (let i = 0; i < cell.farN; i++) {
261
+ ids[(at + i) * 2] = perInstanceTable ? cell.farOffset + i : cell.far;
262
+ ids[(at + i) * 2 + 1] = perInstanceTable ? 0.5 : i;
263
+ }
264
+ }
265
+ at += cell.farN;
266
+ }
267
+ if (ids) geometry.setAttribute('aFar', new THREE.InstancedBufferAttribute(ids, 2));
268
+ if (compact) {
269
+ mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
270
+ mesh.instanceColor?.setUsage(THREE.DynamicDrawUsage);
271
+ members.push(list);
272
+ }
115
273
  mesh.layers.mask = lo.layers.mask;
116
274
  mesh.receiveShadow = lo.receiveShadow;
117
275
  mesh.castShadow = casts; // as the near copies do
@@ -119,66 +277,549 @@ export function createCellLod({ square = 500, tableWidth = 64, thinFrom = 0.45,
119
277
  mesh.customDepthMaterial = depthMaterial();
120
278
  if (!distanceMaterial) {
121
279
  distanceMaterial = new THREE.MeshDistanceMaterial();
122
- distanceMaterial.onBeforeCompile = hideUnshown;
123
- distanceMaterial.customProgramCacheKey = () => 'far-batch-distance';
280
+ if (!compact) {
281
+ distanceMaterial.onBeforeCompile = hideUnshown;
282
+ distanceMaterial.customProgramCacheKey = () => 'far-batch-distance';
283
+ }
124
284
  }
125
285
  mesh.customDistanceMaterial = distanceMaterial;
126
- mesh.userData.partShown = true; // a pass drawing heights with a plain depth material would show every copy
286
+ mesh.userData.partShown = true;
127
287
  }
128
288
  mesh.matrixAutoUpdate = false;
129
289
  mesh.computeBoundingSphere();
130
290
  parent.add(mesh);
131
291
  batches.push(mesh);
132
292
  }
133
- batchShows = new Int32Array(batches.length);
293
+ drawEnds = new Int32Array(batches.length);
294
+ if (compact) dirty = new Uint8Array(batches.length).fill(1);
295
+
296
+ // Build spatial billboard impostor batches for cells tagged with `cell.impostor`.
297
+ if (activeAtlas) {
298
+ const impSquares = new Map();
299
+ let maxTreeSpan = 16;
300
+ for (const s of activeAtlas.species) {
301
+ maxTreeSpan = Math.max(maxTreeSpan, s.height, s.width * s.crownWidthScale);
302
+ }
303
+ for (const cell of cellList) {
304
+ if (typeof cell.impostor !== 'number' || cell.impostor < 0) continue;
305
+ const rec = lodOf(cell);
306
+ rec.species = cell.impostor;
307
+ ensureInstanceMetadata(cell, rec);
308
+ if (cell.farN === undefined) cell.farN = rec.total;
309
+ const sqKey = `${Math.floor(rec.cx / impostorSquare)}:${Math.floor(rec.cz / impostorSquare)}`;
310
+ let group = impSquares.get(sqKey);
311
+ if (!group) impSquares.set(sqKey, (group = []));
312
+ group.push(cell);
313
+ }
314
+ if (impSquares.size) {
315
+ sharedImpostorMaterial ??= createTreeImpostorMaterial(activeAtlas);
316
+ for (const list of impSquares.values()) {
317
+ let capacity = 0,
318
+ minX = Infinity,
319
+ maxX = -Infinity,
320
+ minY = Infinity,
321
+ maxY = -Infinity,
322
+ minZ = Infinity,
323
+ maxZ = -Infinity;
324
+ for (const cell of list) {
325
+ const rec = cell.lod;
326
+ capacity += rec.total;
327
+ minX = Math.min(minX, rec.cx - rec.r);
328
+ maxX = Math.max(maxX, rec.cx + rec.r);
329
+ minY = Math.min(minY, rec.cy - rec.r);
330
+ maxY = Math.max(maxY, rec.cy + rec.r);
331
+ minZ = Math.min(minZ, rec.cz - rec.r);
332
+ maxZ = Math.max(maxZ, rec.cz + rec.r);
333
+ }
334
+ const center = new THREE.Vector3((minX + maxX) * 0.5, (minY + maxY) * 0.5, (minZ + maxZ) * 0.5),
335
+ radius = Math.hypot(maxX - minX, maxY - minY, maxZ - minZ) * 0.5 + maxTreeSpan * 2,
336
+ impBatch = createTreeImpostorBatch(activeAtlas, {
337
+ capacity,
338
+ material: sharedImpostorMaterial,
339
+ bounds: new THREE.Sphere(center, radius),
340
+ });
341
+ const impIndex = impostorBatches.length;
342
+ for (const cell of list) {
343
+ cell.impBatch = impIndex;
344
+ cell.lod.impBatch = impIndex;
345
+ }
346
+ parent.add(impBatch.mesh);
347
+ impostorBatches.push(impBatch);
348
+ impostorMembers.push(list);
349
+ }
350
+ impDirty = new Uint8Array(impostorBatches.length).fill(1);
351
+ }
352
+ }
353
+
134
354
  return batches;
135
355
  }
136
356
 
137
- // Where a cell lies and how far it is drawn, kept on it in a record of one shape: cells come from builders of many shapes,
138
- // and a number read from objects of so many shapes is made anew at every read. A cell does not move.
139
- const lodOf = t =>
140
- (t.lod ??= { cx: t.cx, cy: t.cy ?? 0, cz: t.cz, r: t.r, until: t.until ?? Infinity, near: t.near ?? Infinity, batch: t.farBatch ?? -1 });
357
+ // Packs visible far instances of `batchIndex` from each cell's immutable `sourceMatrix` / `sourceColor`.
358
+ function packVisible(batchIndex) {
359
+ const mesh = batches[batchIndex],
360
+ dstMat = mesh.instanceMatrix.array,
361
+ dstCol = mesh.instanceColor?.array ?? null;
362
+ let end = 0;
363
+ for (const cell of members[batchIndex]) {
364
+ const rec = cell.lod;
365
+ if (!rec || !rec.farCount) continue;
366
+ const srcMat = rec.sourceMatrix.array,
367
+ srcCol = rec.sourceColor?.array ?? null;
368
+ if (rec.mode === MODE_ALL_FAR) {
369
+ const count = Math.min(rec.farCount, cell.farN);
370
+ dstMat.set(srcMat.subarray(0, count * 16), end * 16);
371
+ if (dstCol) {
372
+ if (srcCol) dstCol.set(srcCol.subarray(0, count * 3), end * 3);
373
+ else dstCol.fill(0, end * 3, (end + count) * 3);
374
+ }
375
+ end += count;
376
+ } else if (rec.mode === MODE_MIXED) {
377
+ const limit = Math.min(rec.activeLimit, cell.farN),
378
+ tiers = rec.tiers;
379
+ for (let k = 0; k < limit; k++) {
380
+ if (tiers[k] !== TIER_FAR) continue;
381
+ const mOff = k * 16,
382
+ dOff = end * 16;
383
+ for (let e = 0; e < 16; e++) dstMat[dOff + e] = srcMat[mOff + e];
384
+ if (dstCol) {
385
+ const cOff = k * 3,
386
+ dcOff = end * 3;
387
+ if (srcCol) {
388
+ dstCol[dcOff] = srcCol[cOff];
389
+ dstCol[dcOff + 1] = srcCol[cOff + 1];
390
+ dstCol[dcOff + 2] = srcCol[cOff + 2];
391
+ } else {
392
+ dstCol[dcOff] = dstCol[dcOff + 1] = dstCol[dcOff + 2] = 0;
393
+ }
394
+ }
395
+ end++;
396
+ }
397
+ }
398
+ }
399
+ mesh.count = end;
400
+ if (!end) return;
401
+ for (const attribute of [mesh.instanceMatrix, mesh.instanceColor]) {
402
+ if (!attribute) continue;
403
+ attribute.clearUpdateRanges();
404
+ attribute.addUpdateRange(0, end * attribute.itemSize);
405
+ attribute.needsUpdate = true;
406
+ }
407
+ }
408
+
409
+ // Packs visible billboard impostor instances of `impBatchIndex` from each member cell.
410
+ function packImpostorVisible(impBatchIndex) {
411
+ const impBatch = impostorBatches[impBatchIndex];
412
+ let end = 0;
413
+ for (const cell of impostorMembers[impBatchIndex]) {
414
+ const rec = cell.lod;
415
+ if (!rec || !rec.impCount) continue;
416
+ const srcMat = rec.sourceMatrix.array,
417
+ srcCol = rec.sourceColor?.array ?? null,
418
+ species = rec.species,
419
+ hashes = rec.hImp,
420
+ limit = rec.activeLimit;
421
+ if (rec.mode === MODE_ALL_IMPOSTOR) {
422
+ for (let k = 0; k < limit; k++) {
423
+ impBatch.setFromMatrix(end++, srcMat, k * 16, srcCol, k * 3, species, hashes[k]);
424
+ }
425
+ } else if (rec.mode === MODE_MIXED) {
426
+ const tiers = rec.tiers;
427
+ for (let k = 0; k < limit; k++) {
428
+ if (tiers[k] !== TIER_IMPOSTOR) continue;
429
+ impBatch.setFromMatrix(end++, srcMat, k * 16, srcCol, k * 3, species, hashes[k]);
430
+ }
431
+ }
432
+ }
433
+ impBatch.commit(end);
434
+ }
435
+
436
+ // Updates `cell.mesh` (and optional unbatched `cell.lo`) when `rec` uses per-instance hashed handovers.
437
+ function syncUnbatchedMeshes(cell, rec, limit, nearChanged, farChanged) {
438
+ if (rec.nearCount === 0) {
439
+ if (cell.mesh.instanceMatrix !== rec.sourceMatrix) {
440
+ cell.mesh.instanceMatrix = rec.sourceMatrix;
441
+ if (rec.sourceColor) cell.mesh.instanceColor = rec.sourceColor;
442
+ }
443
+ cell.mesh.count = limit;
444
+ cell.mesh.visible = false;
445
+ } else if (rec.nearCount === limit && rec.mode !== MODE_MIXED) {
446
+ if (cell.mesh.instanceMatrix !== rec.sourceMatrix) {
447
+ cell.mesh.instanceMatrix = rec.sourceMatrix;
448
+ if (rec.sourceColor) cell.mesh.instanceColor = rec.sourceColor;
449
+ }
450
+ cell.mesh.count = limit;
451
+ cell.mesh.visible = true;
452
+ } else {
453
+ if (!rec.nearMatrix) {
454
+ rec.nearMatrix = new THREE.InstancedBufferAttribute(new Float32Array(rec.total * 16), 16).setUsage(THREE.DynamicDrawUsage);
455
+ if (rec.sourceColor) {
456
+ rec.nearColor = new THREE.InstancedBufferAttribute(new Float32Array(rec.total * 3), 3).setUsage(THREE.DynamicDrawUsage);
457
+ }
458
+ }
459
+ if (nearChanged || cell.mesh.instanceMatrix !== rec.nearMatrix) {
460
+ const srcMat = rec.sourceMatrix.array,
461
+ srcCol = rec.sourceColor?.array ?? null,
462
+ dstMat = rec.nearMatrix.array,
463
+ dstCol = rec.nearColor?.array ?? null,
464
+ tiers = rec.tiers;
465
+ let out = 0;
466
+ for (let k = 0; k < limit; k++) {
467
+ if (tiers[k] !== TIER_NEAR) continue;
468
+ const mOff = k * 16,
469
+ dOff = out * 16;
470
+ for (let e = 0; e < 16; e++) dstMat[dOff + e] = srcMat[mOff + e];
471
+ if (dstCol && srcCol) {
472
+ dstCol[out * 3] = srcCol[k * 3];
473
+ dstCol[out * 3 + 1] = srcCol[k * 3 + 1];
474
+ dstCol[out * 3 + 2] = srcCol[k * 3 + 2];
475
+ }
476
+ out++;
477
+ }
478
+ rec.nearMatrix.clearUpdateRanges();
479
+ rec.nearMatrix.addUpdateRange(0, out * 16);
480
+ rec.nearMatrix.needsUpdate = true;
481
+ if (rec.nearColor) {
482
+ rec.nearColor.clearUpdateRanges();
483
+ rec.nearColor.addUpdateRange(0, out * 3);
484
+ rec.nearColor.needsUpdate = true;
485
+ }
486
+ }
487
+ cell.mesh.instanceMatrix = rec.nearMatrix;
488
+ if (rec.nearColor) cell.mesh.instanceColor = rec.nearColor;
489
+ cell.mesh.count = rec.nearCount;
490
+ cell.mesh.visible = true;
491
+ }
492
+
493
+ if (cell.lo) {
494
+ if (!cell.lo.instanceMatrix) {
495
+ // A far object that is not instanced cannot show a chosen share of the copies: its near mesh shows them all.
496
+ cell.lo.visible = false;
497
+ return;
498
+ }
499
+ if (rec.farCount === 0) {
500
+ cell.lo.visible = false;
501
+ } else if (rec.farCount === limit && rec.mode === MODE_ALL_FAR) {
502
+ if (cell.lo.instanceMatrix !== rec.sourceMatrix) {
503
+ cell.lo.instanceMatrix = rec.sourceMatrix;
504
+ if (rec.sourceColor) cell.lo.instanceColor = rec.sourceColor;
505
+ }
506
+ cell.lo.count = limit;
507
+ cell.lo.visible = true;
508
+ } else {
509
+ if (!rec.loMatrix) {
510
+ rec.loMatrix = new THREE.InstancedBufferAttribute(new Float32Array(rec.total * 16), 16).setUsage(THREE.DynamicDrawUsage);
511
+ if (rec.sourceColor) {
512
+ rec.loColor = new THREE.InstancedBufferAttribute(new Float32Array(rec.total * 3), 3).setUsage(THREE.DynamicDrawUsage);
513
+ }
514
+ }
515
+ if (farChanged || cell.lo.instanceMatrix !== rec.loMatrix) {
516
+ const srcMat = rec.sourceMatrix.array,
517
+ srcCol = rec.sourceColor?.array ?? null,
518
+ dstMat = rec.loMatrix.array,
519
+ dstCol = rec.loColor?.array ?? null,
520
+ tiers = rec.tiers;
521
+ let out = 0;
522
+ for (let k = 0; k < limit; k++) {
523
+ if (tiers[k] !== TIER_FAR) continue;
524
+ const mOff = k * 16,
525
+ dOff = out * 16;
526
+ for (let e = 0; e < 16; e++) dstMat[dOff + e] = srcMat[mOff + e];
527
+ if (dstCol && srcCol) {
528
+ dstCol[out * 3] = srcCol[k * 3];
529
+ dstCol[out * 3 + 1] = srcCol[k * 3 + 1];
530
+ dstCol[out * 3 + 2] = srcCol[k * 3 + 2];
531
+ }
532
+ out++;
533
+ }
534
+ rec.loMatrix.clearUpdateRanges();
535
+ rec.loMatrix.addUpdateRange(0, out * 16);
536
+ rec.loMatrix.needsUpdate = true;
537
+ if (rec.loColor) {
538
+ rec.loColor.clearUpdateRanges();
539
+ rec.loColor.addUpdateRange(0, out * 3);
540
+ rec.loColor.needsUpdate = true;
541
+ }
542
+ }
543
+ cell.lo.instanceMatrix = rec.loMatrix;
544
+ if (rec.loColor) cell.lo.instanceColor = rec.loColor;
545
+ cell.lo.count = rec.farCount;
546
+ cell.lo.visible = true;
547
+ }
548
+ }
549
+ }
141
550
 
142
551
  /**
143
- * Each frame: near cells show their detailed mesh, far ones their batched copy, thinned beyond `thinFrom` of the drawing
144
- * distance. (cx, cz): the eye; `distance` metres past which nothing is drawn, `detail` metres within which the near mesh is,
145
- * `density` the share of a thinned cell's instances kept. Optional eye height `cy` includes altitude above the cell's
146
- * centre in the near/far decision. The visibility range stays horizontal. `rangeScale` scales both thresholds, including the radius.
552
+ * Each frame: near cells show their detailed mesh, far ones their batched copy (and optional billboard impostors beyond).
553
+ *
554
+ * @param {Array<object>} cells Cell list.
555
+ * @param {number} cx Camera world X.
556
+ * @param {number} cz Camera world Z.
557
+ * @param {number} distance Metres past which standard cells are culled.
558
+ * @param {number} detail Metres within which the detailed `near` mesh is drawn.
559
+ * @param {number} density Share of instances kept (`0..1`).
560
+ * @param {number} [cy] Optional camera world Y to include altitude in the `near -> far` distance.
561
+ * @param {number} [rangeScale=1] Quality multiplier scaling all thresholds and cell radii.
562
+ * @param {object} [updateOptions]
563
+ * @param {number} [updateOptions.lensScale=1] Perspective magnification (`detailLens(camera, refFov)`) scaling LOD handover distances.
564
+ * @param {boolean} [updateOptions.holdHandover=false] When `true`, locks active LOD tiers during a held scene shot until the next cut.
565
+ * @param {number} [updateOptions.farDistance] Metres at which impostor-enabled cells hand over `far -> impostor` (defaults to `distance * 0.3`).
566
+ * @param {number} [updateOptions.impostorDistance] Metres past which impostor-enabled cells cull (defaults to `distance * 2.2`).
147
567
  */
148
- function update(cells, cx, cz, distance, detail, density, cy, rangeScale = 1) {
568
+ function update(cells, cx, cz, distance, detail, density, cy, rangeScale = 1, updateOptions) {
569
+ const lensScale = Math.max(0.25, updateOptions?.lensScale ?? 1),
570
+ holdHandover = Boolean(updateOptions?.holdHandover);
571
+ if (sharedImpostorMaterial) {
572
+ sharedImpostorMaterial.userData.impostorUniforms.uImpLens.value.x = 1 / lensScale;
573
+ }
574
+ if (holdHandover && hasSnapshot) return;
575
+
149
576
  let changed = false;
150
- if (batchShows) batchShows.fill(0);
577
+ if (drawEnds) drawEnds.fill(0);
578
+ const hasOptions = updateOptions !== undefined,
579
+ baseFarToImp = updateOptions?.farDistance ?? distance * farDistanceShare,
580
+ baseImpMax = updateOptions?.impostorDistance ?? distance * impostorDistanceShare,
581
+ impLensBoost = Math.max(1, Math.min(lensScale, impostorLensCap));
582
+
151
583
  for (let i = 0; i < cells.length; i++) {
152
- const t = cells[i],
153
- at = lodOf(t),
584
+ const cell = cells[i],
585
+ at = lodOf(cell),
154
586
  dx = cx - at.cx,
155
587
  dz = cz - at.cz,
156
588
  horizontalSq = dx * dx + dz * dz,
157
- d = Math.sqrt(horizontalSq) / rangeScale - at.r,
158
- hasFar = t.lo || t.far !== undefined;
159
- const vis = d < Math.min(distance, at.until),
160
- nearDistance = cy === undefined ? d : Math.sqrt(horizontalSq + (cy - at.cy) ** 2) / rangeScale - at.r,
161
- hi = !hasFar || nearDistance < Math.min(detail, at.near);
162
- t.mesh.visible = vis && hi;
163
- if (t.lo) t.lo.visible = vis && !hi;
164
- let count = t.farN;
165
- if (t.full !== undefined) {
166
- count = Math.max(1, (t.full * (d < distance * thinFrom ? 1 : thinTo) * density) | 0);
167
- t.mesh.count = count;
168
- if (t.lo) t.lo.count = count;
169
- }
170
- if (t.far !== undefined) {
171
- const v = vis && !hi ? count : 0;
172
- if (v > 0) batchShows[at.batch]++;
173
- if (table.data[t.far] !== v) {
174
- table.data[t.far] = v;
175
- changed = true;
589
+ rawHorizDist = Math.sqrt(horizontalSq),
590
+ detailDistance = rawHorizDist / rangeScale - at.r,
591
+ hasFar = cell.far !== undefined || Boolean(cell.lo?.instanceMatrix),
592
+ hasImp = at.impBatch >= 0 && at.species >= 0;
593
+
594
+ if (!at.sourceMatrix) {
595
+ // A whole object (a group or one mesh on its own): shown while it is within range, as it always was.
596
+ const maxDist = Math.min(distance * (lensScale > 1 ? Math.min(lensScale, impostorLensCap) : 1), at.until);
597
+ cell.mesh.visible = detailDistance < maxDist;
598
+ continue;
599
+ }
600
+
601
+ // Preserve 100% identical behaviour for non-jittered, non-impostor cells.
602
+ if (!hasImp && jitter === 0) {
603
+ const maxDist = Math.min(distance * (lensScale > 1 ? Math.min(lensScale, impostorLensCap) : 1), at.until),
604
+ vis = detailDistance < maxDist,
605
+ nearDistance = cy === undefined ? detailDistance : Math.sqrt(horizontalSq + (cy - at.cy) ** 2) / rangeScale - at.r,
606
+ nearThreshold = Math.min(detail, at.near) * lensScale,
607
+ hi = !hasFar || nearDistance < nearThreshold;
608
+ cell.mesh.visible = vis && hi;
609
+ if (cell.lo) cell.lo.visible = vis && !hi;
610
+ let count = cell.farN ?? at.total;
611
+ if (cell.full !== undefined) {
612
+ count = Math.max(1, (cell.full * (detailDistance < distance * thinFrom ? 1 : thinTo) * density) | 0);
613
+ cell.mesh.count = count;
614
+ if (cell.lo) cell.lo.count = count;
615
+ }
616
+ if (cell.far !== undefined) {
617
+ const v = vis && !hi ? count : 0;
618
+ at.farCount = v;
619
+ at.mode = v > 0 ? MODE_ALL_FAR : MODE_NONE;
620
+ if (v > 0) drawEnds[at.batch] = Math.max(drawEnds[at.batch], cell.farStart + Math.min(cell.farN, v));
621
+ if (!perInstanceTable) {
622
+ if (table.data[cell.far] !== v) {
623
+ table.data[cell.far] = v;
624
+ if (compact) dirty[at.batch] = 1;
625
+ changed = true;
626
+ }
627
+ } else {
628
+ const limit = Math.min(cell.farN, v);
629
+ for (let k = 0; k < cell.farN; k++) {
630
+ const show = k < limit ? 1 : 0,
631
+ tIdx = cell.farOffset + k;
632
+ if (table.data[tIdx] !== show) {
633
+ table.data[tIdx] = show;
634
+ changed = true;
635
+ }
636
+ }
637
+ }
176
638
  }
639
+ continue;
640
+ }
641
+
642
+ // Per-instance hashed handover path (`near -> far -> impostor`).
643
+ ensureInstanceMetadata(cell, at);
644
+ const total = at.total,
645
+ // Impostors cost 2 triangles each, so impostor-backed forest cells keep full canopy density unless thinImpostors is set.
646
+ thinShare = (!hasImp || thinImpostors) && cell.full !== undefined && detailDistance >= distance * thinFrom ? thinTo : 1,
647
+ limit = cell.full !== undefined ? Math.max(1, Math.min(total, (cell.full * thinShare * density) | 0)) : total;
648
+
649
+ const worldNearEdge = Math.min(detail, at.near) * lensScale * rangeScale,
650
+ worldFarEdge = (hasImp ? baseFarToImp * lensScale : Math.min(distance * Math.max(1, lensScale), at.until)) * rangeScale,
651
+ worldImpEdge = (hasImp ? Math.min(baseImpMax * impLensBoost, at.until) : worldFarEdge) * rangeScale,
652
+ effJitter = jitter > 0 ? jitter : defaultJitter,
653
+ nearMin = worldNearEdge > 0 ? worldNearEdge * (1 - effJitter) - nearHysteresis * lensScale : -Infinity,
654
+ nearMax = worldNearEdge > 0 ? worldNearEdge * (1 + effJitter) + nearHysteresis * lensScale : -Infinity,
655
+ farMin = hasFar ? worldFarEdge * (1 - effJitter) - farHysteresis * lensScale : nearMin,
656
+ farMax = hasFar ? worldFarEdge * (1 + effJitter) + farHysteresis * lensScale : nearMax,
657
+ impMin = hasImp ? worldImpEdge * (1 - effJitter * 0.5) - impostorHysteresis : farMin,
658
+ impMax = hasImp ? worldImpEdge * (1 + effJitter * 0.5) + impostorHysteresis : farMax;
659
+
660
+ const cellDist3D = cy === undefined ? rawHorizDist : Math.sqrt(horizontalSq + (cy - at.cy) ** 2),
661
+ prevMode = at.mode,
662
+ prevNear = at.nearCount,
663
+ prevFar = at.farCount,
664
+ prevImp = at.impCount,
665
+ prevLimit = at.activeLimit;
666
+
667
+ let nextMode = MODE_NONE,
668
+ nearCount = 0,
669
+ farCount = 0,
670
+ impCount = 0,
671
+ nearChanged = prevLimit !== limit,
672
+ farChanged = prevLimit !== limit,
673
+ impChanged = prevLimit !== limit;
674
+
675
+ if (rawHorizDist - at.r > impMax) {
676
+ // Entire cell lies past outer visibility range.
677
+ nextMode = MODE_NONE;
678
+ if (prevMode !== MODE_NONE) {
679
+ at.tiers.fill(TIER_HIDDEN, 0, limit);
680
+ nearChanged = prevNear > 0;
681
+ farChanged = prevFar > 0;
682
+ impChanged = prevImp > 0;
683
+ }
684
+ } else if ((!hasFar && !hasImp) || (worldNearEdge > 0 && cellDist3D + at.r < nearMin)) {
685
+ // Entire cell is strictly within `near`.
686
+ nextMode = MODE_NONE;
687
+ nearCount = limit;
688
+ if (prevNear !== limit || prevMode !== MODE_NONE) {
689
+ at.tiers.fill(TIER_NEAR, 0, limit);
690
+ nearChanged = true;
691
+ farChanged = prevFar > 0;
692
+ impChanged = prevImp > 0;
693
+ }
694
+ } else if (hasFar && cellDist3D - at.r > nearMax && rawHorizDist + at.r < farMin) {
695
+ // Entire cell is strictly within `far`.
696
+ nextMode = MODE_ALL_FAR;
697
+ farCount = limit;
698
+ if (prevMode !== MODE_ALL_FAR || prevFar !== limit) {
699
+ at.tiers.fill(TIER_FAR, 0, limit);
700
+ nearChanged = prevNear > 0;
701
+ farChanged = true;
702
+ impChanged = prevImp > 0;
703
+ }
704
+ } else if (hasImp && (hasFar ? rawHorizDist - at.r > farMax : cellDist3D - at.r > nearMax) && rawHorizDist + at.r < impMin) {
705
+ // Entire cell is strictly within `impostor`.
706
+ nextMode = MODE_ALL_IMPOSTOR;
707
+ impCount = limit;
708
+ if (prevMode !== MODE_ALL_IMPOSTOR || prevImp !== limit) {
709
+ at.tiers.fill(TIER_IMPOSTOR, 0, limit);
710
+ nearChanged = prevNear > 0;
711
+ farChanged = prevFar > 0;
712
+ impChanged = true;
713
+ }
714
+ } else {
715
+ // Straddling cell: evaluate each instance against its own hashed handover distances with hysteresis.
716
+ nextMode = MODE_MIXED;
717
+ const px = at.px,
718
+ py = at.py,
719
+ pz = at.pz,
720
+ hNear = at.hNear,
721
+ hFar = at.hFar,
722
+ hImp = at.hImp,
723
+ tiers = at.tiers,
724
+ nearHyst = nearHysteresis * lensScale,
725
+ farHyst = farHysteresis * lensScale;
726
+
727
+ for (let k = 0; k < limit; k++) {
728
+ const ix = cx - px[k],
729
+ iz = cz - pz[k],
730
+ d2Sq = ix * ix + iz * iz,
731
+ d3Sq = cy === undefined ? d2Sq : d2Sq + (cy - py[k]) * (cy - py[k]),
732
+ was = tiers[k];
733
+
734
+ let tier = TIER_HIDDEN;
735
+ if (worldNearEdge > 0) {
736
+ const myNear = worldNearEdge * (1 + effJitter * (2 * hNear[k] - 1)) + (was === TIER_NEAR ? nearHyst : -nearHyst);
737
+ if (myNear > 0 && d3Sq < myNear * myNear) tier = TIER_NEAR;
738
+ }
739
+ if (tier === TIER_HIDDEN && hasFar) {
740
+ const myFar = worldFarEdge * (1 + effJitter * (2 * hFar[k] - 1)) + (was === TIER_FAR ? farHyst : -farHyst);
741
+ if (myFar > 0 && d2Sq < myFar * myFar) tier = TIER_FAR;
742
+ }
743
+ if (tier === TIER_HIDDEN && hasImp) {
744
+ const myImp =
745
+ worldImpEdge * (1 + effJitter * 0.5 * (2 * hImp[k] - 1)) + (was === TIER_IMPOSTOR ? impostorHysteresis : -impostorHysteresis);
746
+ if (myImp > 0 && d2Sq < myImp * myImp) tier = TIER_IMPOSTOR;
747
+ }
748
+
749
+ if (tier !== was) {
750
+ tiers[k] = tier;
751
+ if (was === TIER_NEAR || tier === TIER_NEAR) nearChanged = true;
752
+ if (was === TIER_FAR || tier === TIER_FAR) farChanged = true;
753
+ if (was === TIER_IMPOSTOR || tier === TIER_IMPOSTOR) impChanged = true;
754
+ }
755
+ if (tier === TIER_NEAR) nearCount++;
756
+ else if (tier === TIER_FAR) farCount++;
757
+ else if (tier === TIER_IMPOSTOR) impCount++;
758
+ }
759
+ if (farCount === limit) nextMode = MODE_ALL_FAR;
760
+ else if (impCount === limit) nextMode = MODE_ALL_IMPOSTOR;
761
+ else if (nearCount === limit || (nearCount === 0 && farCount === 0 && impCount === 0)) nextMode = MODE_NONE;
762
+ }
763
+
764
+ at.mode = nextMode;
765
+ at.activeLimit = limit;
766
+ at.nearCount = nearCount;
767
+ at.farCount = farCount;
768
+ at.impCount = impCount;
769
+
770
+ syncUnbatchedMeshes(cell, at, limit, nearChanged, farChanged);
771
+
772
+ if (cell.far !== undefined) {
773
+ if (farCount > 0) drawEnds[at.batch] = Math.max(drawEnds[at.batch], cell.farStart + Math.min(cell.farN, limit));
774
+ if (compact) {
775
+ if (farChanged || prevMode !== nextMode) dirty[at.batch] = 1;
776
+ } else if (perInstanceTable) {
777
+ const tiers = at.tiers,
778
+ cap = Math.min(cell.farN, limit);
779
+ for (let k = 0; k < cell.farN; k++) {
780
+ const show = k < cap && (nextMode === MODE_ALL_FAR || tiers[k] === TIER_FAR) ? 1 : 0,
781
+ tIdx = cell.farOffset + k;
782
+ if (table.data[tIdx] !== show) {
783
+ table.data[tIdx] = show;
784
+ changed = true;
785
+ }
786
+ }
787
+ }
788
+ }
789
+
790
+ if (hasImp && (impChanged || prevMode !== nextMode)) {
791
+ impDirty[at.impBatch] = 1;
792
+ }
793
+
794
+ void hasOptions;
795
+ }
796
+
797
+ if (changed && table.tex) table.tex.needsUpdate = true;
798
+ for (let i = 0; i < batches.length; i++) {
799
+ if (!compact) {
800
+ batches[i].count = drawEnds[i];
801
+ batches[i].visible = drawEnds[i] > 0;
802
+ } else {
803
+ if (dirty[i]) {
804
+ packVisible(i);
805
+ dirty[i] = 0;
806
+ }
807
+ batches[i].visible = batches[i].count > 0;
808
+ }
809
+ }
810
+ for (let i = 0; i < impostorBatches.length; i++) {
811
+ if (impDirty[i]) {
812
+ packImpostorVisible(i);
813
+ impDirty[i] = 0;
177
814
  }
178
815
  }
179
- if (changed) table.tex.needsUpdate = true;
180
- for (let i = 0; i < batches.length; i++) batches[i].visible = batchShows[i] > 0;
816
+ hasSnapshot = true;
817
+ }
818
+
819
+ /** Clears held handover state so the next `update` recomputes immediately even if `holdHandover` is set. */
820
+ function resetHandover() {
821
+ hasSnapshot = false;
181
822
  }
182
823
 
183
- return { batch, update, batches };
824
+ return { batch, update, batches, impostorBatches, resetHandover };
184
825
  }