wallhammer-web 0.0.0-stage → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (408) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +92 -2
  3. package/dist/base/data.d.ts +67 -0
  4. package/dist/base/data.js +10 -0
  5. package/dist/base/destructible.d.ts +167 -0
  6. package/dist/base/destructible.js +47 -0
  7. package/dist/base/env-slot.d.ts +101 -0
  8. package/dist/base/env-slot.js +8 -0
  9. package/dist/base/frames.d.ts +138 -0
  10. package/dist/base/frames.js +459 -0
  11. package/dist/base/index.d.ts +21 -0
  12. package/dist/base/index.js +11 -0
  13. package/dist/base/mat34.d.ts +46 -0
  14. package/dist/base/mat34.js +173 -0
  15. package/dist/base/material.d.ts +87 -0
  16. package/dist/base/material.js +6 -0
  17. package/dist/base/math.d.ts +27 -0
  18. package/dist/base/math.js +60 -0
  19. package/dist/base/progress.d.ts +38 -0
  20. package/dist/base/progress.js +81 -0
  21. package/dist/base/rig.d.ts +177 -0
  22. package/dist/base/rig.js +97 -0
  23. package/dist/base/trig.d.ts +29 -0
  24. package/dist/base/trig.js +169 -0
  25. package/dist/base/up.d.ts +26 -0
  26. package/dist/base/up.js +40 -0
  27. package/dist/base/view.d.ts +128 -0
  28. package/dist/base/view.js +136 -0
  29. package/dist/camera/camera.d.ts +99 -0
  30. package/dist/camera/camera.js +228 -0
  31. package/dist/camera/follow.d.ts +120 -0
  32. package/dist/camera/follow.js +320 -0
  33. package/dist/camera/free.d.ts +153 -0
  34. package/dist/camera/free.js +359 -0
  35. package/dist/camera/index.d.ts +10 -0
  36. package/dist/camera/index.js +6 -0
  37. package/dist/camera/orbit.d.ts +93 -0
  38. package/dist/camera/orbit.js +261 -0
  39. package/dist/camera/path.d.ts +48 -0
  40. package/dist/camera/path.js +179 -0
  41. package/dist/core/ballistic.d.ts +66 -0
  42. package/dist/core/ballistic.js +104 -0
  43. package/dist/core/bodies.d.ts +77 -0
  44. package/dist/core/bodies.js +125 -0
  45. package/dist/core/clock.d.ts +37 -0
  46. package/dist/core/clock.js +87 -0
  47. package/dist/core/cover-fade.d.ts +14 -0
  48. package/dist/core/cover-fade.js +38 -0
  49. package/dist/core/index.d.ts +13 -0
  50. package/dist/core/index.js +8 -0
  51. package/dist/core/kepler.d.ts +74 -0
  52. package/dist/core/kepler.js +108 -0
  53. package/dist/core/level-fade.d.ts +66 -0
  54. package/dist/core/level-fade.js +123 -0
  55. package/dist/core/palette.d.ts +15 -0
  56. package/dist/core/palette.js +47 -0
  57. package/dist/env/atmosphere.d.ts +129 -0
  58. package/dist/env/atmosphere.js +707 -0
  59. package/dist/env/cloud-field.d.ts +44 -0
  60. package/dist/env/cloud-field.js +111 -0
  61. package/dist/env/environment.d.ts +217 -0
  62. package/dist/env/environment.js +441 -0
  63. package/dist/env/frame-block.d.ts +29 -0
  64. package/dist/env/frame-block.js +152 -0
  65. package/dist/env/glsl.d.ts +35 -0
  66. package/dist/env/glsl.js +246 -0
  67. package/dist/env/index.d.ts +13 -0
  68. package/dist/env/index.js +9 -0
  69. package/dist/env/sky.d.ts +48 -0
  70. package/dist/env/sky.js +227 -0
  71. package/dist/env/sun.d.ts +31 -0
  72. package/dist/env/sun.js +42 -0
  73. package/dist/gen-arch/build.d.ts +17 -0
  74. package/dist/gen-arch/build.js +414 -0
  75. package/dist/gen-arch/damage/damaged.d.ts +44 -0
  76. package/dist/gen-arch/damage/damaged.js +1119 -0
  77. package/dist/gen-arch/damage/fall-material.d.ts +10 -0
  78. package/dist/gen-arch/damage/fall-material.js +47 -0
  79. package/dist/gen-arch/damage/fall.d.ts +78 -0
  80. package/dist/gen-arch/damage/fall.js +797 -0
  81. package/dist/gen-arch/damage/rubble.d.ts +22 -0
  82. package/dist/gen-arch/damage/rubble.js +188 -0
  83. package/dist/gen-arch/index.d.ts +25 -0
  84. package/dist/gen-arch/index.js +16 -0
  85. package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
  86. package/dist/gen-arch/interior/interior-plan.js +615 -0
  87. package/dist/gen-arch/interior/interior.d.ts +97 -0
  88. package/dist/gen-arch/interior/interior.js +966 -0
  89. package/dist/gen-arch/structure/building-shader.d.ts +25 -0
  90. package/dist/gen-arch/structure/building-shader.js +946 -0
  91. package/dist/gen-arch/structure/buildings.d.ts +248 -0
  92. package/dist/gen-arch/structure/buildings.js +294 -0
  93. package/dist/gen-arch/structure/dims.d.ts +77 -0
  94. package/dist/gen-arch/structure/dims.js +39 -0
  95. package/dist/gen-arch/structure/distant.d.ts +62 -0
  96. package/dist/gen-arch/structure/distant.js +316 -0
  97. package/dist/gen-arch/structure/env-slots.d.ts +18 -0
  98. package/dist/gen-arch/structure/env-slots.js +47 -0
  99. package/dist/gen-arch/structure/facade.d.ts +193 -0
  100. package/dist/gen-arch/structure/facade.js +159 -0
  101. package/dist/gen-arch/structure/parts.d.ts +31 -0
  102. package/dist/gen-arch/structure/parts.js +69 -0
  103. package/dist/gen-arch/structure/roof.d.ts +76 -0
  104. package/dist/gen-arch/structure/roof.js +568 -0
  105. package/dist/gen-arch/structure/structure-material.d.ts +23 -0
  106. package/dist/gen-arch/structure/structure-material.js +55 -0
  107. package/dist/gen-arch/structure/writer.d.ts +94 -0
  108. package/dist/gen-arch/structure/writer.js +195 -0
  109. package/dist/gen-core/cell.d.ts +138 -0
  110. package/dist/gen-core/cell.js +150 -0
  111. package/dist/gen-core/cube-sphere.d.ts +57 -0
  112. package/dist/gen-core/cube-sphere.js +159 -0
  113. package/dist/gen-core/field.d.ts +55 -0
  114. package/dist/gen-core/field.js +154 -0
  115. package/dist/gen-core/geometry.d.ts +31 -0
  116. package/dist/gen-core/geometry.js +231 -0
  117. package/dist/gen-core/hash.d.ts +18 -0
  118. package/dist/gen-core/hash.js +58 -0
  119. package/dist/gen-core/height-field.d.ts +33 -0
  120. package/dist/gen-core/height-field.js +137 -0
  121. package/dist/gen-core/index.d.ts +22 -0
  122. package/dist/gen-core/index.js +17 -0
  123. package/dist/gen-core/instance-cells.d.ts +37 -0
  124. package/dist/gen-core/instance-cells.js +72 -0
  125. package/dist/gen-core/near-scatter.d.ts +56 -0
  126. package/dist/gen-core/near-scatter.js +120 -0
  127. package/dist/gen-core/noise.d.ts +23 -0
  128. package/dist/gen-core/noise.js +72 -0
  129. package/dist/gen-core/occupancy.d.ts +21 -0
  130. package/dist/gen-core/occupancy.js +104 -0
  131. package/dist/gen-core/sphere-patch.d.ts +138 -0
  132. package/dist/gen-core/sphere-patch.js +544 -0
  133. package/dist/gen-core/strip-surface.d.ts +34 -0
  134. package/dist/gen-core/strip-surface.js +246 -0
  135. package/dist/gen-core/strip.d.ts +102 -0
  136. package/dist/gen-core/strip.js +817 -0
  137. package/dist/gen-ground/env-slots.d.ts +48 -0
  138. package/dist/gen-ground/env-slots.js +78 -0
  139. package/dist/gen-ground/ground.d.ts +78 -0
  140. package/dist/gen-ground/ground.js +208 -0
  141. package/dist/gen-ground/index.d.ts +9 -0
  142. package/dist/gen-ground/index.js +7 -0
  143. package/dist/gen-ground/markings.d.ts +46 -0
  144. package/dist/gen-ground/markings.js +124 -0
  145. package/dist/gen-ground/materials.d.ts +18 -0
  146. package/dist/gen-ground/materials.js +22 -0
  147. package/dist/gen-ground/terrain-shader.d.ts +5 -0
  148. package/dist/gen-ground/terrain-shader.js +151 -0
  149. package/dist/gen-mesh/bare-shapes.d.ts +194 -0
  150. package/dist/gen-mesh/bare-shapes.js +596 -0
  151. package/dist/gen-mesh/bisect-surface.d.ts +47 -0
  152. package/dist/gen-mesh/bisect-surface.js +261 -0
  153. package/dist/gen-mesh/box.d.ts +18 -0
  154. package/dist/gen-mesh/box.js +2 -0
  155. package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
  156. package/dist/gen-mesh/cell-morph-mesh.js +258 -0
  157. package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
  158. package/dist/gen-mesh/cell-surface-mesh.js +140 -0
  159. package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
  160. package/dist/gen-mesh/chunk-mesh.js +112 -0
  161. package/dist/gen-mesh/drape-mesh.d.ts +36 -0
  162. package/dist/gen-mesh/drape-mesh.js +98 -0
  163. package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
  164. package/dist/gen-mesh/height-field-mesh.js +98 -0
  165. package/dist/gen-mesh/index.d.ts +26 -0
  166. package/dist/gen-mesh/index.js +15 -0
  167. package/dist/gen-mesh/line-mesh.d.ts +25 -0
  168. package/dist/gen-mesh/line-mesh.js +62 -0
  169. package/dist/gen-mesh/mesh-builder.d.ts +115 -0
  170. package/dist/gen-mesh/mesh-builder.js +315 -0
  171. package/dist/gen-mesh/packed-mesh.d.ts +53 -0
  172. package/dist/gen-mesh/packed-mesh.js +205 -0
  173. package/dist/gen-mesh/prism-mesh.d.ts +33 -0
  174. package/dist/gen-mesh/prism-mesh.js +176 -0
  175. package/dist/gen-mesh/rig-mesh.d.ts +9 -0
  176. package/dist/gen-mesh/rig-mesh.js +135 -0
  177. package/dist/gen-mesh/strip-mesh.d.ts +45 -0
  178. package/dist/gen-mesh/strip-mesh.js +258 -0
  179. package/dist/gen-plan/drain.d.ts +58 -0
  180. package/dist/gen-plan/drain.js +270 -0
  181. package/dist/gen-plan/grid.d.ts +37 -0
  182. package/dist/gen-plan/grid.js +182 -0
  183. package/dist/gen-plan/horizon.d.ts +50 -0
  184. package/dist/gen-plan/horizon.js +185 -0
  185. package/dist/gen-plan/index.d.ts +15 -0
  186. package/dist/gen-plan/index.js +13 -0
  187. package/dist/gen-plan/line-tiles.d.ts +35 -0
  188. package/dist/gen-plan/line-tiles.js +190 -0
  189. package/dist/gen-plan/lines.d.ts +31 -0
  190. package/dist/gen-plan/lines.js +183 -0
  191. package/dist/gen-plan/plots.d.ts +40 -0
  192. package/dist/gen-plan/plots.js +282 -0
  193. package/dist/gen-plan/route.d.ts +55 -0
  194. package/dist/gen-plan/route.js +297 -0
  195. package/dist/gen-plan/sites.d.ts +32 -0
  196. package/dist/gen-plan/sites.js +209 -0
  197. package/dist/gen-tex/code-stamp.d.ts +31 -0
  198. package/dist/gen-tex/code-stamp.js +137 -0
  199. package/dist/gen-tex/index.d.ts +7 -0
  200. package/dist/gen-tex/index.js +5 -0
  201. package/dist/gen-tex/light-horizon.d.ts +47 -0
  202. package/dist/gen-tex/light-horizon.js +180 -0
  203. package/dist/gen-tex/textures.d.ts +8 -0
  204. package/dist/gen-tex/textures.js +49 -0
  205. package/dist/gen-tex/value-textures.d.ts +46 -0
  206. package/dist/gen-tex/value-textures.js +102 -0
  207. package/dist/gen-vegetation/env-slots.d.ts +20 -0
  208. package/dist/gen-vegetation/env-slots.js +29 -0
  209. package/dist/gen-vegetation/index.d.ts +8 -0
  210. package/dist/gen-vegetation/index.js +7 -0
  211. package/dist/gen-vegetation/materials.d.ts +7 -0
  212. package/dist/gen-vegetation/materials.js +17 -0
  213. package/dist/gen-vegetation/planting.d.ts +170 -0
  214. package/dist/gen-vegetation/planting.js +363 -0
  215. package/dist/gen-vegetation/tree-shader.d.ts +23 -0
  216. package/dist/gen-vegetation/tree-shader.js +229 -0
  217. package/dist/gen-vegetation/trees.d.ts +59 -0
  218. package/dist/gen-vegetation/trees.js +329 -0
  219. package/dist/gpu/device.d.ts +286 -0
  220. package/dist/gpu/device.js +1157 -0
  221. package/dist/gpu/gl.d.ts +134 -0
  222. package/dist/gpu/gl.js +276 -0
  223. package/dist/gpu/index.d.ts +4 -0
  224. package/dist/gpu/index.js +3 -0
  225. package/dist/index.d.ts +11 -0
  226. package/dist/index.js +13 -0
  227. package/dist/motion/graph.d.ts +244 -0
  228. package/dist/motion/graph.js +739 -0
  229. package/dist/motion/index.d.ts +8 -0
  230. package/dist/motion/index.js +8 -0
  231. package/dist/motion/pose-ops.d.ts +30 -0
  232. package/dist/motion/pose-ops.js +232 -0
  233. package/dist/motion/reach.d.ts +45 -0
  234. package/dist/motion/reach.js +264 -0
  235. package/dist/motion/skeleton.d.ts +49 -0
  236. package/dist/motion/skeleton.js +99 -0
  237. package/dist/platform/byte-store.d.ts +21 -0
  238. package/dist/platform/byte-store.js +137 -0
  239. package/dist/platform/canvas.d.ts +29 -0
  240. package/dist/platform/canvas.js +76 -0
  241. package/dist/platform/context-loss.d.ts +8 -0
  242. package/dist/platform/context-loss.js +17 -0
  243. package/dist/platform/free-input.d.ts +51 -0
  244. package/dist/platform/free-input.js +100 -0
  245. package/dist/platform/index.d.ts +18 -0
  246. package/dist/platform/index.js +10 -0
  247. package/dist/platform/input.d.ts +48 -0
  248. package/dist/platform/input.js +117 -0
  249. package/dist/platform/keys.d.ts +12 -0
  250. package/dist/platform/keys.js +29 -0
  251. package/dist/platform/loop.d.ts +41 -0
  252. package/dist/platform/loop.js +84 -0
  253. package/dist/platform/walk-input.d.ts +74 -0
  254. package/dist/platform/walk-input.js +226 -0
  255. package/dist/platform/worker.d.ts +51 -0
  256. package/dist/platform/worker.js +170 -0
  257. package/dist/render/bone-instances.d.ts +24 -0
  258. package/dist/render/bone-instances.js +74 -0
  259. package/dist/render/cells.d.ts +70 -0
  260. package/dist/render/cells.js +144 -0
  261. package/dist/render/glsl.d.ts +66 -0
  262. package/dist/render/glsl.js +173 -0
  263. package/dist/render/index.d.ts +26 -0
  264. package/dist/render/index.js +16 -0
  265. package/dist/render/instance-sets.d.ts +97 -0
  266. package/dist/render/instance-sets.js +241 -0
  267. package/dist/render/near-instances.d.ts +75 -0
  268. package/dist/render/near-instances.js +219 -0
  269. package/dist/render/particles.d.ts +63 -0
  270. package/dist/render/particles.js +215 -0
  271. package/dist/render/pieces.d.ts +57 -0
  272. package/dist/render/pieces.js +151 -0
  273. package/dist/render/point-lights.d.ts +97 -0
  274. package/dist/render/point-lights.js +243 -0
  275. package/dist/render/renderer.d.ts +447 -0
  276. package/dist/render/renderer.js +1100 -0
  277. package/dist/render/rig.d.ts +74 -0
  278. package/dist/render/rig.js +576 -0
  279. package/dist/render/shadow-areas.d.ts +49 -0
  280. package/dist/render/shadow-areas.js +125 -0
  281. package/dist/render/shadow-glsl.d.ts +12 -0
  282. package/dist/render/shadow-glsl.js +146 -0
  283. package/dist/render/transient.d.ts +61 -0
  284. package/dist/render/transient.js +213 -0
  285. package/dist/render/value-glsl.d.ts +17 -0
  286. package/dist/render/value-glsl.js +208 -0
  287. package/dist/render/value-layers.d.ts +180 -0
  288. package/dist/render/value-layers.js +333 -0
  289. package/dist/runtime/atmosphere.d.ts +37 -0
  290. package/dist/runtime/atmosphere.js +304 -0
  291. package/dist/runtime/cloud-shell.d.ts +29 -0
  292. package/dist/runtime/cloud-shell.js +158 -0
  293. package/dist/runtime/engine.d.ts +155 -0
  294. package/dist/runtime/engine.js +305 -0
  295. package/dist/runtime/environment-link.d.ts +22 -0
  296. package/dist/runtime/environment-link.js +49 -0
  297. package/dist/runtime/index.d.ts +13 -0
  298. package/dist/runtime/index.js +12 -0
  299. package/dist/runtime/precipitation-glsl.d.ts +2 -0
  300. package/dist/runtime/precipitation-glsl.js +70 -0
  301. package/dist/runtime/precipitation.d.ts +11 -0
  302. package/dist/runtime/precipitation.js +78 -0
  303. package/dist/runtime/sky.d.ts +30 -0
  304. package/dist/runtime/sky.js +114 -0
  305. package/dist/runtime/streaming.d.ts +245 -0
  306. package/dist/runtime/streaming.js +602 -0
  307. package/dist/sim/agenda.d.ts +167 -0
  308. package/dist/sim/agenda.js +425 -0
  309. package/dist/sim/bytes.d.ts +34 -0
  310. package/dist/sim/bytes.js +125 -0
  311. package/dist/sim/destruction/apply.d.ts +59 -0
  312. package/dist/sim/destruction/apply.js +243 -0
  313. package/dist/sim/destruction/cell-states.d.ts +54 -0
  314. package/dist/sim/destruction/cell-states.js +198 -0
  315. package/dist/sim/destruction/index.d.ts +11 -0
  316. package/dist/sim/destruction/index.js +10 -0
  317. package/dist/sim/destruction/int-geometry.d.ts +17 -0
  318. package/dist/sim/destruction/int-geometry.js +131 -0
  319. package/dist/sim/destruction/pick.d.ts +13 -0
  320. package/dist/sim/destruction/pick.js +59 -0
  321. package/dist/sim/destruction/rubble.d.ts +8 -0
  322. package/dist/sim/destruction/rubble.js +159 -0
  323. package/dist/sim/destruction/state.d.ts +15 -0
  324. package/dist/sim/destruction/state.js +83 -0
  325. package/dist/sim/destruction/structure.d.ts +30 -0
  326. package/dist/sim/destruction/structure.js +139 -0
  327. package/dist/sim/events.d.ts +37 -0
  328. package/dist/sim/events.js +106 -0
  329. package/dist/sim/fixed.d.ts +21 -0
  330. package/dist/sim/fixed.js +64 -0
  331. package/dist/sim/flow-probe.d.ts +58 -0
  332. package/dist/sim/flow-probe.js +158 -0
  333. package/dist/sim/flow.d.ts +257 -0
  334. package/dist/sim/flow.js +967 -0
  335. package/dist/sim/index.d.ts +28 -0
  336. package/dist/sim/index.js +20 -0
  337. package/dist/sim/interlock.d.ts +47 -0
  338. package/dist/sim/interlock.js +252 -0
  339. package/dist/sim/movers.d.ts +228 -0
  340. package/dist/sim/movers.js +1083 -0
  341. package/dist/sim/nav.d.ts +72 -0
  342. package/dist/sim/nav.js +292 -0
  343. package/dist/sim/paths.d.ts +80 -0
  344. package/dist/sim/paths.js +197 -0
  345. package/dist/sim/regions.d.ts +87 -0
  346. package/dist/sim/regions.js +517 -0
  347. package/dist/sim/simulation.d.ts +114 -0
  348. package/dist/sim/simulation.js +291 -0
  349. package/dist/sim/states.d.ts +23 -0
  350. package/dist/sim/states.js +54 -0
  351. package/dist/sim/table.d.ts +49 -0
  352. package/dist/sim/table.js +126 -0
  353. package/dist/sim/zones.d.ts +11 -0
  354. package/dist/sim/zones.js +196 -0
  355. package/dist/tools/collision-lines.d.ts +12 -0
  356. package/dist/tools/collision-lines.js +50 -0
  357. package/dist/tools/data-view-glsl.d.ts +3 -0
  358. package/dist/tools/data-view-glsl.js +63 -0
  359. package/dist/tools/data-view-layers.d.ts +20 -0
  360. package/dist/tools/data-view-layers.js +60 -0
  361. package/dist/tools/debug-view.d.ts +30 -0
  362. package/dist/tools/debug-view.js +4 -0
  363. package/dist/tools/index.d.ts +10 -0
  364. package/dist/tools/index.js +7 -0
  365. package/dist/tools/mesh-checks.d.ts +67 -0
  366. package/dist/tools/mesh-checks.js +188 -0
  367. package/dist/tools/profile.d.ts +85 -0
  368. package/dist/tools/profile.js +173 -0
  369. package/dist/world/cell-cache.d.ts +32 -0
  370. package/dist/world/cell-cache.js +60 -0
  371. package/dist/world/cell-format.d.ts +25 -0
  372. package/dist/world/cell-format.js +232 -0
  373. package/dist/world/collide.d.ts +192 -0
  374. package/dist/world/collide.js +822 -0
  375. package/dist/world/index.d.ts +16 -0
  376. package/dist/world/index.js +12 -0
  377. package/dist/world/mesh-solids.d.ts +48 -0
  378. package/dist/world/mesh-solids.js +169 -0
  379. package/dist/world/pick.d.ts +34 -0
  380. package/dist/world/pick.js +177 -0
  381. package/dist/world/providers.d.ts +66 -0
  382. package/dist/world/providers.js +83 -0
  383. package/dist/world/sphere.d.ts +69 -0
  384. package/dist/world/sphere.js +271 -0
  385. package/dist/world/streaming.d.ts +288 -0
  386. package/dist/world/streaming.js +801 -0
  387. package/dist/world/walker.d.ts +145 -0
  388. package/dist/world/walker.js +502 -0
  389. package/docs/architecture.md +111 -0
  390. package/docs/cameras.md +302 -0
  391. package/docs/concepts.md +123 -0
  392. package/docs/destruction.md +127 -0
  393. package/docs/frames.md +199 -0
  394. package/docs/generators.md +575 -0
  395. package/docs/getting-started.md +123 -0
  396. package/docs/headless.md +50 -0
  397. package/docs/motion.md +160 -0
  398. package/docs/planet.md +152 -0
  399. package/docs/recipes.md +521 -0
  400. package/docs/reference.md +364 -0
  401. package/docs/rendering.md +823 -0
  402. package/docs/rigs.md +203 -0
  403. package/docs/simulation.md +235 -0
  404. package/docs/streaming.md +590 -0
  405. package/docs/system.md +166 -0
  406. package/docs/tools.md +175 -0
  407. package/docs/walking.md +262 -0
  408. package/package.json +131 -3
@@ -0,0 +1,261 @@
1
+ // A height grid as a mesh with as few triangles as a given error allows (right-triangle bisection, RTIN): the square
2
+ // is two right triangles, and a triangle is cut in two at the middle of its long edge while the surface there (or
3
+ // anywhere inside it) lies farther from the flat triangle than the tolerance. Flat ground ends up as a few large
4
+ // triangles, broken ground keeps the full grid. Cuts are always matched on both sides of an edge, so the mesh has no
5
+ // gaps inside the square.
6
+ //
7
+ // Two squares next to each other may be cut differently along their shared edge. A skirt hides that: every outer edge
8
+ // gets a strip hanging down from it, shaded as the surface above it. A side can instead be marked as a cut through
9
+ // the ground (the edge of the world): its strip then reaches down to a given height and carries the cut kind.
10
+ //
11
+ // The mesh comes out packed (packed-mesh.ts): positions are 16-bit steps from the square's corner, indices 16-bit
12
+ // whenever the vertices allow it.
13
+ import { PACKED_POSITION, PACK_STEPS_PER_EDGE } from './packed-mesh.js';
14
+ /** Attribute locations: those of the height field mesh, with the packed position in place of `a_pos`. */
15
+ export const BISECT_SURFACE_ATTRIBS = { [PACKED_POSITION]: 0, a_normal: 1 };
16
+ /** Packed position 4× int16; normal 3× int16 (not normalized, ×32767) and the kind (0 surface, 1 cut) as the fourth. */
17
+ export const BISECT_SURFACE_LAYOUT = {
18
+ stride: 16,
19
+ attribs: [
20
+ { name: PACKED_POSITION, size: 4, type: 'i16', offset: 0 },
21
+ { name: 'a_normal', size: 4, type: 'i16', offset: 8 },
22
+ ],
23
+ };
24
+ const KIND_TOP = 0;
25
+ const KIND_CUT = 1;
26
+ /**
27
+ * Mesh of a square height grid within `toleranceM`. `heights` holds `(quads + 3)²` samples, row by row along x, with
28
+ * one extra sample all round: sample `(i + 1, j + 1)` is the grid point `(i, j)`; the margin is used for normals only,
29
+ * so squares that share an edge and sample the same ground agree on the normals there.
30
+ */
31
+ export function bisectSurface(heights, o) {
32
+ const n = o.quads;
33
+ if (n < 2 || n > 256 || (n & (n - 1)) !== 0)
34
+ throw new Error('bisectSurface: quads must be a power of two from 2 to 256');
35
+ const size = n + 1;
36
+ const wide = n + 3;
37
+ if (heights.length !== wide * wide)
38
+ throw new Error(`bisectSurface: ${wide * wide} heights expected, got ${heights.length}`);
39
+ const hAt = (i, j) => heights[(j + 1) * wide + i + 1];
40
+ const spacing = o.edgeM / n;
41
+ const stepsPerQuad = PACK_STEPS_PER_EDGE / n;
42
+ const stepXZ = o.edgeM / PACK_STEPS_PER_EDGE;
43
+ // 1. Error of every grid point that is the middle of a long edge: the height difference there, or the largest
44
+ // error of the triangles below it. Triangles are numbered as a binary heap (the two halves of a triangle follow
45
+ // from its number), smallest last.
46
+ const numTriangles = n * n * 2 - 2;
47
+ const numParents = numTriangles - n * n;
48
+ const errors = new Float32Array(size * size);
49
+ const coords = new Uint16Array(numTriangles * 4);
50
+ for (let i = 0; i < numTriangles; i++) {
51
+ let id = i + 2;
52
+ let ax = 0;
53
+ let ay = 0;
54
+ let bx = 0;
55
+ let by = 0;
56
+ let cx = 0;
57
+ let cy = 0;
58
+ if (id & 1)
59
+ bx = by = cx = n;
60
+ else
61
+ ax = ay = cy = n;
62
+ while ((id >>= 1) > 1) {
63
+ const mx = (ax + bx) >> 1;
64
+ const my = (ay + by) >> 1;
65
+ if (id & 1) {
66
+ bx = ax;
67
+ by = ay;
68
+ ax = cx;
69
+ ay = cy;
70
+ }
71
+ else {
72
+ ax = bx;
73
+ ay = by;
74
+ bx = cx;
75
+ by = cy;
76
+ }
77
+ cx = mx;
78
+ cy = my;
79
+ }
80
+ coords.set([ax, ay, bx, by], i * 4);
81
+ }
82
+ for (let i = numTriangles - 1; i >= 0; i--) {
83
+ const k = i * 4;
84
+ const ax = coords[k];
85
+ const ay = coords[k + 1];
86
+ const bx = coords[k + 2];
87
+ const by = coords[k + 3];
88
+ const mx = (ax + bx) >> 1;
89
+ const my = (ay + by) >> 1;
90
+ const cx = mx + my - ay;
91
+ const cy = my + ax - mx;
92
+ const middle = my * size + mx;
93
+ let e = Math.max(errors[middle], Math.abs((hAt(ax, ay) + hAt(bx, by)) / 2 - hAt(mx, my)));
94
+ if (i < numParents) {
95
+ const left = ((ay + cy) >> 1) * size + ((ax + cx) >> 1);
96
+ const right = ((by + cy) >> 1) * size + ((bx + cx) >> 1);
97
+ e = Math.max(e, errors[left], errors[right]);
98
+ }
99
+ errors[middle] = e;
100
+ }
101
+ // 2. Triangles within the tolerance, top down. Vertices are numbered in the order they are first used.
102
+ const clip = o.clip;
103
+ const clipSteps = clip ? clip.map((m) => Math.round(m / stepXZ)) : null;
104
+ // A vertex is known by where it ends up: grid points that clipping moves onto one point are one vertex.
105
+ const vertexAt = new Map();
106
+ const px = [];
107
+ const pz = [];
108
+ const grid = [];
109
+ const vertex = (i, j) => {
110
+ let x = i * stepsPerQuad;
111
+ let z = j * stepsPerQuad;
112
+ if (clipSteps) {
113
+ x = Math.min(clipSteps[2], Math.max(clipSteps[0], x));
114
+ z = Math.min(clipSteps[3], Math.max(clipSteps[1], z));
115
+ }
116
+ const key = x * 32768 + z;
117
+ let v = vertexAt.get(key);
118
+ if (v === undefined) {
119
+ v = grid.length;
120
+ vertexAt.set(key, v);
121
+ grid.push(j * size + i);
122
+ px.push(x);
123
+ pz.push(z);
124
+ }
125
+ return v;
126
+ };
127
+ const tris = [];
128
+ const emit = (ax, ay, bx, by, cx, cy) => {
129
+ const a = vertex(ax, ay);
130
+ const b = vertex(bx, by);
131
+ const c = vertex(cx, cy);
132
+ // Twice the area seen from above; clipping may have left none.
133
+ const area = (px[b] - px[a]) * (pz[c] - pz[a]) - (pz[b] - pz[a]) * (px[c] - px[a]);
134
+ if (area !== 0)
135
+ tris.push(a, b, c);
136
+ };
137
+ const tolerance = Math.max(0, o.toleranceM);
138
+ const split = (ax, ay, bx, by, cx, cy) => {
139
+ const mx = (ax + bx) >> 1;
140
+ const my = (ay + by) >> 1;
141
+ if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > tolerance) {
142
+ split(cx, cy, ax, ay, mx, my);
143
+ split(bx, by, cx, cy, mx, my);
144
+ }
145
+ else
146
+ emit(ax, ay, bx, by, cx, cy);
147
+ };
148
+ split(0, 0, n, n, n, 0);
149
+ split(n, n, 0, 0, 0, n);
150
+ const surfaceTriangles = tris.length / 3;
151
+ // 3. Outer edges: those used by one triangle only. The edge runs the way its triangle does, so the outside is
152
+ // always on the same hand and the strip faces outward.
153
+ const top = grid.length;
154
+ const edgeUse = new Map();
155
+ const edgeKey = (a, b) => (a < b ? a * top + b : b * top + a);
156
+ for (let t = 0; t < tris.length; t += 3) {
157
+ for (let e = 0; e < 3; e++) {
158
+ const k = edgeKey(tris[t + e], tris[t + ((e + 1) % 3)]);
159
+ edgeUse.set(k, (edgeUse.get(k) ?? 0) + 1);
160
+ }
161
+ }
162
+ const lo = clipSteps ? [clipSteps[0], clipSteps[1]] : [0, 0];
163
+ /** Side of an outer edge: 0 low x, 1 high x, 2 low z, 3 high z. */
164
+ const sideOf = (a, b) => {
165
+ if (px[a] === px[b])
166
+ return px[a] === lo[0] ? 0 : 1;
167
+ return pz[a] === lo[1] ? 2 : 3;
168
+ };
169
+ const SIDE_NORMAL = [
170
+ [-1, 0],
171
+ [1, 0],
172
+ [0, -1],
173
+ [0, 1],
174
+ ];
175
+ // 4. Vertex data. Heights and normals come from the grid point a vertex was made for.
176
+ const yStep = (h) => Math.max(-32768, Math.min(32767, Math.round((h - o.heightOrigin) / o.stepY)));
177
+ const normalOf = (g) => {
178
+ const i = g % size;
179
+ const j = (g - i) / size;
180
+ const dx = (hAt(i + 1, j) - hAt(i - 1, j)) / (2 * spacing);
181
+ const dz = (hAt(i, j + 1) - hAt(i, j - 1)) / (2 * spacing);
182
+ const l = Math.hypot(dx, 1, dz);
183
+ return [-dx / l, 1 / l, -dz / l];
184
+ };
185
+ const rows = [];
186
+ const put = (x, y, z, nx, ny, nz, kind) => {
187
+ rows.push(x, y, z, 0, Math.round(nx * 32767), Math.round(ny * 32767), Math.round(nz * 32767), kind);
188
+ return rows.length / 8 - 1;
189
+ };
190
+ let minY = Infinity;
191
+ let maxY = -Infinity;
192
+ const ySteps = new Int32Array(top);
193
+ const normals = [];
194
+ for (let v = 0; v < top; v++) {
195
+ const g = grid[v];
196
+ const h = hAt(g % size, Math.floor(g / size));
197
+ if (h < minY)
198
+ minY = h;
199
+ if (h > maxY)
200
+ maxY = h;
201
+ ySteps[v] = yStep(h);
202
+ const nrm = normalOf(g);
203
+ normals.push(nrm);
204
+ put(px[v], ySteps[v], pz[v], nrm[0], nrm[1], nrm[2], KIND_TOP);
205
+ }
206
+ if (top === 0) {
207
+ minY = 0;
208
+ maxY = 0;
209
+ }
210
+ const indices = tris.slice();
211
+ if (o.skirtM > 0 || o.cuts?.some((c) => c !== null && c !== undefined)) {
212
+ const drop = Math.max(1, Math.round(o.skirtM / o.stepY));
213
+ /** Lower vertex of an ordinary skirt under a surface vertex, made once. */
214
+ const below = new Int32Array(top).fill(-1);
215
+ for (let t = 0; t < tris.length; t += 3) {
216
+ for (let e = 0; e < 3; e++) {
217
+ const u = tris[t + e];
218
+ const v = tris[t + ((e + 1) % 3)];
219
+ if (edgeUse.get(edgeKey(u, v)) !== 1)
220
+ continue;
221
+ const side = sideOf(u, v);
222
+ const cut = o.cuts?.[side] ?? null;
223
+ let u0 = u;
224
+ let v0 = v;
225
+ let u1;
226
+ let v1;
227
+ if (cut === null) {
228
+ if (o.skirtM <= 0)
229
+ continue;
230
+ for (const w of [u, v]) {
231
+ if (below[w] < 0) {
232
+ const nrm = normals[w];
233
+ below[w] = put(px[w], Math.max(-32768, ySteps[w] - drop), pz[w], nrm[0], nrm[1], nrm[2], KIND_TOP);
234
+ }
235
+ }
236
+ u1 = below[u];
237
+ v1 = below[v];
238
+ }
239
+ else {
240
+ // A cut has its own vertices: its normal points out of the side and its kind differs from the surface.
241
+ const [nx, nz] = SIDE_NORMAL[side];
242
+ const base = yStep(cut);
243
+ u0 = put(px[u], ySteps[u], pz[u], nx, 0, nz, KIND_CUT);
244
+ u1 = put(px[u], Math.min(ySteps[u], base), pz[u], nx, 0, nz, KIND_CUT);
245
+ v0 = put(px[v], ySteps[v], pz[v], nx, 0, nz, KIND_CUT);
246
+ v1 = put(px[v], Math.min(ySteps[v], base), pz[v], nx, 0, nz, KIND_CUT);
247
+ }
248
+ indices.push(u0, u1, v0, v0, u1, v1);
249
+ }
250
+ }
251
+ }
252
+ const count = rows.length / 8;
253
+ const vertices = new Int16Array(rows);
254
+ const mesh = {
255
+ layout: BISECT_SURFACE_LAYOUT,
256
+ vertices,
257
+ indices: count <= 65536 ? Uint16Array.from(indices) : Uint32Array.from(indices),
258
+ count: indices.length,
259
+ };
260
+ return { mesh, triangles: surfaceTriangles, gridTriangles: n * n * 2, minY, maxY };
261
+ }
@@ -0,0 +1,18 @@
1
+ /** Single-coloured box standing on a surface, optionally tilted along its longer axis to follow a slope. */
2
+ export interface PropBox {
3
+ cx: number;
4
+ cz: number;
5
+ /** Height of the bottom face at the box centre. */
6
+ baseY: number;
7
+ /** Unit direction of the longer axis. */
8
+ ux: number;
9
+ uz: number;
10
+ halfL: number;
11
+ halfW: number;
12
+ height: number;
13
+ /** Rise along the longer axis (height change per metre), so the box lies on a slope. */
14
+ slope: number;
15
+ /** Palette row. */
16
+ color: number;
17
+ hash: number;
18
+ }
@@ -0,0 +1,2 @@
1
+ // Box primitive shared by generators: a single-coloured oriented box with an optional tilt.
2
+ export {};
@@ -0,0 +1,52 @@
1
+ import type { MeshData, VertexLayout } from '../base/data.ts';
2
+ import type { CellSurfaceOptions } from './cell-surface-mesh.ts';
3
+ /** Locations a material should give the attributes. The first three are those of the plain cell surface. */
4
+ export declare const CELL_MORPH_ATTRIBS: {
5
+ readonly a_pos: 0;
6
+ readonly a_normal: 1;
7
+ readonly a_color: 2;
8
+ readonly a_coarseShift: 3;
9
+ readonly a_coarseNormal: 4;
10
+ readonly a_coarseColor: 5;
11
+ };
12
+ /**
13
+ * The cell surface layout (position 3× float, normal 3× int16 with the kind 0 surface / 1 skirt, colour 4× u8) and
14
+ * then: `a_coarseShift`, metres along "up" from this vertex to the parent's surface; `a_coarseNormal`, the parent's
15
+ * normal there (3× int16 ×32767, the fourth is 0); `a_coarseColor`, the parent's colour there (4× u8 normalized).
16
+ */
17
+ export declare const CELL_MORPH_LAYOUT: VertexLayout;
18
+ /**
19
+ * GLSL for a vertex shader (paste it after the includes; it declares its own uniforms). `cellMorph(p)` is the blend
20
+ * factor at a render-space point: 0 = the cell's own shape, 1 = the parent's shape. The distance is measured as the
21
+ * streaming manager measures it for tickets: along the surface, combined with the viewer's height above it.
22
+ *
23
+ * vec3 p = a_pos + u_cellOffset;
24
+ * float m = cellMorph(p);
25
+ * p += u_morphUp * (a_coarseShift * m);
26
+ * vec3 n = mix(a_normal.xyz, a_coarseNormal.xyz, m);
27
+ * vec4 c = mix(a_color, a_coarseColor, m);
28
+ *
29
+ * The streamed world sets `u_morphEye`, `u_morphUp` (per frame) and `u_cellMorph` (per cell) when its `morph` option
30
+ * is on. Without them every factor is 0 and the mesh draws as the plain cell surface.
31
+ */
32
+ export declare const CELL_MORPH_GLSL = "\nuniform vec4 u_morphEye; // xyz: the viewer in render space, w: its height above the surface\nuniform vec3 u_morphUp; // unit \"up\" at the viewer\nuniform vec2 u_cellMorph; // x: distance where the blend starts, y: 1 / length of the blend (0 = no blend)\nfloat cellMorph(vec3 p) {\n vec3 d = p - u_morphEye.xyz;\n d -= u_morphUp * dot(d, u_morphUp);\n float dist = sqrt(dot(d, d) + u_morphEye.w * u_morphEye.w);\n return clamp((dist - u_cellMorph.x) * u_cellMorph.y, 0.0, 1.0);\n}\n";
33
+ /**
34
+ * Options of the cell surface. `quads` must be even. `colorAt` must give a vertex the same colour at every level
35
+ * (it may use the position and height, not the normal of this level): the parent's colour at a shared vertex is taken
36
+ * from this cell. `skirtDepth` is the least depth of the skirt (0 = no skirt).
37
+ */
38
+ export type CellMorphOptions = CellSurfaceOptions;
39
+ export interface CellMorphSurface {
40
+ mesh: MeshData;
41
+ /** Lowest and highest point the mesh can show at any blend factor (the skirt included). */
42
+ minY: number;
43
+ maxY: number;
44
+ /**
45
+ * Estimated largest distance in metres between this surface and the one its children would show: from the second
46
+ * differences of the height grid. Use it as `CellContent.error`.
47
+ */
48
+ error: number;
49
+ /** Depth the skirt was given. */
50
+ skirtDepth: number;
51
+ }
52
+ export declare function cellMorphMesh(o: CellMorphOptions): CellMorphSurface;
@@ -0,0 +1,258 @@
1
+ // Surface mesh of one cell that can blend into the shape of its parent (the cell one level coarser). It is the cell
2
+ // surface mesh (same positions, normals, colours and vertex order) with three more attributes per vertex: how far the
3
+ // parent's surface lies from this one at the vertex, and the parent's normal and colour there. A vertex shader mixes
4
+ // the two by distance, so a cell that has just replaced its parent starts in exactly the parent's shape and grows its
5
+ // own detail as the viewer comes closer; nothing jumps when the level of detail changes.
6
+ //
7
+ // The parent's surface is not sampled again. The parent's vertices are every second vertex of this cell, and heights
8
+ // are a pure function of the world point, so the parent's mesh is this cell's own grid thinned by two
9
+ // (`HeightField.coarseSurface`). Along a cell border the coarse values depend only on the border's own vertices, so
10
+ // neighbouring cells agree there exactly, whatever their parents are.
11
+ //
12
+ // The skirt is as deep as the cell needs: at least `skirtDepth`, and at least twice the largest step this border can
13
+ // show against a neighbour of another level: the distance to the parent's surface, the estimated distance to the
14
+ // children's surface, and the distance from the parent's surface to the surface two levels coarser. The last one is
15
+ // the step of a cell loaded away from the viewer (around a predicted point): it shows its parent's shape there, beside
16
+ // cells of the parent's level that already blend into theirs.
17
+ import { HeightField } from '../gen-core/height-field.js';
18
+ /** Locations a material should give the attributes. The first three are those of the plain cell surface. */
19
+ export const CELL_MORPH_ATTRIBS = { a_pos: 0, a_normal: 1, a_color: 2, a_coarseShift: 3, a_coarseNormal: 4, a_coarseColor: 5 };
20
+ /**
21
+ * The cell surface layout (position 3× float, normal 3× int16 with the kind 0 surface / 1 skirt, colour 4× u8) and
22
+ * then: `a_coarseShift`, metres along "up" from this vertex to the parent's surface; `a_coarseNormal`, the parent's
23
+ * normal there (3× int16 ×32767, the fourth is 0); `a_coarseColor`, the parent's colour there (4× u8 normalized).
24
+ */
25
+ export const CELL_MORPH_LAYOUT = {
26
+ stride: 40,
27
+ attribs: [
28
+ { name: 'a_pos', size: 3, type: 'f32', offset: 0 },
29
+ { name: 'a_normal', size: 4, type: 'i16', offset: 12 },
30
+ { name: 'a_color', size: 4, type: 'u8', normalized: true, offset: 20 },
31
+ { name: 'a_coarseShift', size: 1, type: 'f32', offset: 24 },
32
+ { name: 'a_coarseNormal', size: 4, type: 'i16', offset: 28 },
33
+ { name: 'a_coarseColor', size: 4, type: 'u8', normalized: true, offset: 36 },
34
+ ],
35
+ };
36
+ /**
37
+ * GLSL for a vertex shader (paste it after the includes; it declares its own uniforms). `cellMorph(p)` is the blend
38
+ * factor at a render-space point: 0 = the cell's own shape, 1 = the parent's shape. The distance is measured as the
39
+ * streaming manager measures it for tickets: along the surface, combined with the viewer's height above it.
40
+ *
41
+ * vec3 p = a_pos + u_cellOffset;
42
+ * float m = cellMorph(p);
43
+ * p += u_morphUp * (a_coarseShift * m);
44
+ * vec3 n = mix(a_normal.xyz, a_coarseNormal.xyz, m);
45
+ * vec4 c = mix(a_color, a_coarseColor, m);
46
+ *
47
+ * The streamed world sets `u_morphEye`, `u_morphUp` (per frame) and `u_cellMorph` (per cell) when its `morph` option
48
+ * is on. Without them every factor is 0 and the mesh draws as the plain cell surface.
49
+ */
50
+ export const CELL_MORPH_GLSL = /* glsl */ `
51
+ uniform vec4 u_morphEye; // xyz: the viewer in render space, w: its height above the surface
52
+ uniform vec3 u_morphUp; // unit "up" at the viewer
53
+ uniform vec2 u_cellMorph; // x: distance where the blend starts, y: 1 / length of the blend (0 = no blend)
54
+ float cellMorph(vec3 p) {
55
+ vec3 d = p - u_morphEye.xyz;
56
+ d -= u_morphUp * dot(d, u_morphUp);
57
+ float dist = sqrt(dot(d, d) + u_morphEye.w * u_morphEye.w);
58
+ return clamp((dist - u_cellMorph.x) * u_cellMorph.y, 0.0, 1.0);
59
+ }
60
+ `;
61
+ const KIND_TOP = 0;
62
+ const KIND_SKIRT = 1;
63
+ const MARGIN = 2;
64
+ export function cellMorphMesh(o) {
65
+ const q = o.quads;
66
+ if (q < 2 || q % 2 !== 0)
67
+ throw new Error(`cellMorphMesh: quads must be even, got ${q}`);
68
+ const n = q + 1;
69
+ const step = o.size / q;
70
+ // Heights with two samples of margin on each side: the parent's normals are central differences over twice the step.
71
+ const m = n + 2 * MARGIN;
72
+ const h = new Float64Array(m * m);
73
+ for (let j = 0; j < m; j++) {
74
+ const z = o.originZ + (j - MARGIN) * step;
75
+ for (let i = 0; i < m; i++)
76
+ h[j * m + i] = o.heightAt(o.originX + (i - MARGIN) * step, z);
77
+ }
78
+ const H = (i, j) => h[(j + MARGIN) * m + (i + MARGIN)];
79
+ // Own surface: heights, normals and colours exactly as the plain cell surface computes them.
80
+ const own = new Float64Array(n * n);
81
+ const normals = new Int16Array(n * n * 3);
82
+ const colors = new Uint8Array(n * n * 4);
83
+ // The parent's normal at its own vertices (every second vertex of this cell); the rest is filled by thinning.
84
+ const cnx = new Float64Array(n * n);
85
+ const cny = new Float64Array(n * n);
86
+ const cnz = new Float64Array(n * n);
87
+ let error = 0;
88
+ for (let j = 0; j < n; j++) {
89
+ for (let i = 0; i < n; i++) {
90
+ const k = j * n + i;
91
+ const y = H(i, j);
92
+ own[k] = y;
93
+ const dx = (H(i + 1, j) - H(i - 1, j)) / (2 * step);
94
+ const dz = (H(i, j + 1) - H(i, j - 1)) / (2 * step);
95
+ // Square root is exact in IEEE 754; no transcendental function enters the output.
96
+ const l = Math.sqrt(dx * dx + 1 + dz * dz);
97
+ const nx = -dx / l;
98
+ const ny = 1 / l;
99
+ const nz = -dz / l;
100
+ normals[k * 3] = Math.round(nx * 32767);
101
+ normals[k * 3 + 1] = Math.round(ny * 32767);
102
+ normals[k * 3 + 2] = Math.round(nz * 32767);
103
+ const c = o.colorAt ? o.colorAt(o.originX + i * step, o.originZ + j * step, y, nx, ny, nz) : [128, 128, 128, 255];
104
+ for (let ch = 0; ch < 4; ch++)
105
+ colors[k * 4 + ch] = c[ch];
106
+ if (i % 2 === 0 && j % 2 === 0) {
107
+ const pdx = (H(i + 2, j) - H(i - 2, j)) / (4 * step);
108
+ const pdz = (H(i, j + 2) - H(i, j - 2)) / (4 * step);
109
+ const pl = Math.sqrt(pdx * pdx + 1 + pdz * pdz);
110
+ cnx[k] = -pdx / pl;
111
+ cny[k] = 1 / pl;
112
+ cnz[k] = -pdz / pl;
113
+ }
114
+ // A straight edge between two vertices misses the middle of a curve by an eighth of the second difference.
115
+ const bend = Math.max(Math.abs(H(i - 1, j) - 2 * y + H(i + 1, j)), Math.abs(H(i, j - 1) - 2 * y + H(i, j + 1))) / 8;
116
+ if (bend > error)
117
+ error = bend;
118
+ }
119
+ }
120
+ // The middle of a quad lies on its diagonal; a twisted quad misses the mean of its four corners by a quarter of the twist.
121
+ for (let j = 0; j < q; j++) {
122
+ for (let i = 0; i < q; i++) {
123
+ const twist = Math.abs(H(i, j) + H(i + 1, j + 1) - H(i + 1, j) - H(i, j + 1)) / 4;
124
+ if (twist > error)
125
+ error = twist;
126
+ }
127
+ }
128
+ const coarseY = HeightField.coarseSurface(n, n, own, 2);
129
+ const coarseNx = HeightField.coarseSurface(n, n, cnx, 2);
130
+ const coarseNy = HeightField.coarseSurface(n, n, cny, 2);
131
+ const coarseNz = HeightField.coarseSurface(n, n, cnz, 2);
132
+ const channel = new Float64Array(n * n);
133
+ const coarseColors = new Uint8Array(n * n * 4);
134
+ for (let ch = 0; ch < 4; ch++) {
135
+ for (let k = 0; k < n * n; k++)
136
+ channel[k] = colors[k * 4 + ch];
137
+ const thin = HeightField.coarseSurface(n, n, channel, 2);
138
+ for (let k = 0; k < n * n; k++)
139
+ coarseColors[k * 4 + ch] = Math.round(thin[k]);
140
+ }
141
+ // Skirt depth: the largest step this border can show against another level, doubled, and never under the least depth.
142
+ // The surface two levels coarser is this grid thinned by four (the cell's origin lies on that grid too); a cell whose
143
+ // quads are not a multiple of four goes without that term.
144
+ const coarserY = q % 4 === 0 ? HeightField.coarseSurface(n, n, own, 4) : null;
145
+ let borderGap = 0;
146
+ const gapAt = (i, j, along) => {
147
+ const k = j * n + i;
148
+ const g = Math.max(Math.abs(coarseY[k] - own[k]), along / 8, coarserY ? Math.abs(coarserY[k] - coarseY[k]) : 0);
149
+ if (g > borderGap)
150
+ borderGap = g;
151
+ };
152
+ for (let k = 0; k < n; k++) {
153
+ const bendI = (j) => Math.abs(H(k - 1, j) - 2 * H(k, j) + H(k + 1, j));
154
+ const bendJ = (i) => Math.abs(H(i, k - 1) - 2 * H(i, k) + H(i, k + 1));
155
+ gapAt(k, 0, bendI(0));
156
+ gapAt(k, q, bendI(q));
157
+ gapAt(0, k, bendJ(0));
158
+ gapAt(q, k, bendJ(q));
159
+ }
160
+ const skirt = o.skirtDepth > 0;
161
+ const skirtDepth = skirt ? Math.max(o.skirtDepth, 2 * borderGap) : 0;
162
+ const vertCount = n * n + (skirt ? 8 * n : 0);
163
+ const STRIDE = CELL_MORPH_LAYOUT.stride;
164
+ const buf = new ArrayBuffer(vertCount * STRIDE);
165
+ const view = new DataView(buf);
166
+ let minY = Infinity;
167
+ let maxY = -Infinity;
168
+ const put = (v, i, j, drop, kind) => {
169
+ const k = j * n + i;
170
+ const off = v * STRIDE;
171
+ const y = own[k] - drop;
172
+ const shift = coarseY[k] - own[k];
173
+ view.setFloat32(off, i * step, true);
174
+ view.setFloat32(off + 4, y, true);
175
+ view.setFloat32(off + 8, j * step, true);
176
+ view.setInt16(off + 12, normals[k * 3], true);
177
+ view.setInt16(off + 14, normals[k * 3 + 1], true);
178
+ view.setInt16(off + 16, normals[k * 3 + 2], true);
179
+ view.setInt16(off + 18, kind, true);
180
+ for (let c = 0; c < 4; c++)
181
+ view.setUint8(off + 20 + c, colors[k * 4 + c]);
182
+ view.setFloat32(off + 24, shift, true);
183
+ const cl = Math.sqrt(coarseNx[k] * coarseNx[k] + coarseNy[k] * coarseNy[k] + coarseNz[k] * coarseNz[k]);
184
+ view.setInt16(off + 28, Math.round((coarseNx[k] / cl) * 32767), true);
185
+ view.setInt16(off + 30, Math.round((coarseNy[k] / cl) * 32767), true);
186
+ view.setInt16(off + 32, Math.round((coarseNz[k] / cl) * 32767), true);
187
+ view.setInt16(off + 34, 0, true);
188
+ for (let c = 0; c < 4; c++)
189
+ view.setUint8(off + 36 + c, coarseColors[k * 4 + c]);
190
+ const lo = Math.fround(Math.min(y, y + shift));
191
+ const hi = Math.fround(Math.max(y, y + shift));
192
+ if (lo < minY)
193
+ minY = lo;
194
+ if (hi > maxY)
195
+ maxY = hi;
196
+ };
197
+ for (let j = 0; j < n; j++)
198
+ for (let i = 0; i < n; i++)
199
+ put(j * n + i, i, j, 0, KIND_TOP);
200
+ const quadCount = q * q + (skirt ? 4 * q : 0);
201
+ const big = vertCount > 65535;
202
+ const idx = big ? new Uint32Array(quadCount * 6) : new Uint16Array(quadCount * 6);
203
+ let w = 0;
204
+ for (let j = 0; j < q; j++) {
205
+ for (let i = 0; i < q; i++) {
206
+ const a = j * n + i;
207
+ const b = a + 1;
208
+ const c = a + n;
209
+ const d = c + 1;
210
+ idx[w++] = a;
211
+ idx[w++] = c;
212
+ idx[w++] = b;
213
+ idx[w++] = b;
214
+ idx[w++] = c;
215
+ idx[w++] = d;
216
+ }
217
+ }
218
+ if (skirt) {
219
+ // Four sides, each a strip of vertex pairs: the border vertex and one below it. Both carry the border vertex's
220
+ // blend attributes, so the skirt follows the border at every blend factor.
221
+ let v = n * n;
222
+ const side = (at, flip) => {
223
+ const start = v;
224
+ for (let k = 0; k < n; k++) {
225
+ const [i, j] = at(k);
226
+ put(v++, i, j, 0, KIND_SKIRT);
227
+ put(v++, i, j, skirtDepth, KIND_SKIRT);
228
+ }
229
+ for (let k = 0; k < q; k++) {
230
+ const t0 = start + k * 2;
231
+ const b0 = t0 + 1;
232
+ const t1 = t0 + 2;
233
+ const b1 = t0 + 3;
234
+ if (flip) {
235
+ idx[w++] = t0;
236
+ idx[w++] = t1;
237
+ idx[w++] = b0;
238
+ idx[w++] = t1;
239
+ idx[w++] = b1;
240
+ idx[w++] = b0;
241
+ }
242
+ else {
243
+ idx[w++] = t0;
244
+ idx[w++] = b0;
245
+ idx[w++] = t1;
246
+ idx[w++] = t1;
247
+ idx[w++] = b0;
248
+ idx[w++] = b1;
249
+ }
250
+ }
251
+ };
252
+ side((k) => [k, 0], true); // north
253
+ side((k) => [k, q], false); // south
254
+ side((k) => [0, k], false); // west
255
+ side((k) => [q, k], true); // east
256
+ }
257
+ return { mesh: { layout: CELL_MORPH_LAYOUT, vertices: new Uint8Array(buf), indices: idx, count: w }, minY, maxY, error, skirtDepth };
258
+ }
@@ -0,0 +1,32 @@
1
+ import type { MeshData, VertexLayout } from '../base/data.ts';
2
+ /** Locations a material should give the attributes. */
3
+ export declare const CELL_SURFACE_ATTRIBS: {
4
+ readonly a_pos: 0;
5
+ readonly a_normal: 1;
6
+ readonly a_color: 2;
7
+ };
8
+ /**
9
+ * Position 3× float relative to the cell origin; normal 3× int16 (×32767) and the kind (0 surface, 1 skirt) as the
10
+ * fourth int16; colour 4× u8 normalized (the alpha channel is free for the content: a surface class or a flag).
11
+ */
12
+ export declare const CELL_SURFACE_LAYOUT: VertexLayout;
13
+ export interface CellSurfaceOptions {
14
+ /** World position of the cell origin (double). The mesh is relative to it. */
15
+ originX: number;
16
+ originZ: number;
17
+ /** Edge of the cell in metres and the number of quads along it. */
18
+ size: number;
19
+ quads: number;
20
+ /** Height at a world point (double in, any number out). Must be a pure function of the point. */
21
+ heightAt: (x: number, z: number) => number;
22
+ /** RGBA bytes of a surface point from its world position, height and unit normal. Default mid grey. */
23
+ colorAt?: (x: number, z: number, y: number, nx: number, ny: number, nz: number) => readonly [number, number, number, number];
24
+ /** How far the skirt hangs below the perimeter, in metres. 0 = no skirt. */
25
+ skirtDepth: number;
26
+ }
27
+ export interface CellSurface {
28
+ mesh: MeshData;
29
+ minY: number;
30
+ maxY: number;
31
+ }
32
+ export declare function cellSurfaceMesh(o: CellSurfaceOptions): CellSurface;