wallhammer-web 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (408) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -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,544 @@
1
+ // Surface mesh of one cell of a sphere made of six quadtrees (see cube-sphere.ts): a square grid on a face of the
2
+ // cube, every vertex pushed out to the base radius plus the height the content gives for that point of the sphere.
3
+ //
4
+ // - Content is a function of the point on the sphere, never of the face or the cell: heights are asked for at points
5
+ // that carry the same bits from every cell that reaches them, so cells agree on their shared border, across the
6
+ // edges of the cube as well.
7
+ // - Normals come from the grid, with a margin of samples around the cell, so neighbours of one face agree. On an edge
8
+ // of the cube the two faces' grids do not continue each other; there the normal is taken from four samples placed
9
+ // by the point's direction alone (`directionTangents`), which again gives both faces the same bits.
10
+ // - Like the cell morph mesh of gen-mesh, every vertex also carries the shape of the cell one level coarser: how far
11
+ // along its own "up" the parent's surface lies (`a_coarseShift`), and the parent's normal and colour there. The
12
+ // parent's surface is flat triangles between every second vertex, so the shift includes the bulge of the sphere
13
+ // between them: a vertex moved by the whole shift lies exactly on the parent's triangle.
14
+ // - A skirt hangs from the border toward the centre and covers the step against a neighbour of another level.
15
+ //
16
+ // Positions are relative to the cell origin, the middle of the cell on the base sphere; axes are the world's.
17
+ import { CUBE_FACE_AXES, cubeDirection, cubeDirectionWarped, cubeFaceCoords, cubeGrid, cubeWarp, directionTangents } from './cube-sphere.js';
18
+ import { HeightField } from './height-field.js';
19
+ /** Locations a material should give the attributes. */
20
+ export const SPHERE_PATCH_ATTRIBS = { a_pos: 0, a_normal: 1, a_color: 2, a_coarseShift: 3, a_coarseNormal: 4, a_coarseColor: 5 };
21
+ /**
22
+ * Position 3× float relative to the cell origin; normal 3× int16 (×32767, world axes) and the kind (0 surface,
23
+ * 1 skirt) as the fourth; colour 4× u8 normalized; `a_coarseShift`, metres along the vertex's own "up" to the
24
+ * parent's surface; `a_coarseNormal` and `a_coarseColor`, the parent's normal and colour there. The same structure as
25
+ * the cell morph layout of gen-mesh.
26
+ */
27
+ export const SPHERE_PATCH_LAYOUT = {
28
+ stride: 40,
29
+ attribs: [
30
+ { name: 'a_pos', size: 3, type: 'f32', offset: 0 },
31
+ { name: 'a_normal', size: 4, type: 'i16', offset: 12 },
32
+ { name: 'a_color', size: 4, type: 'u8', normalized: true, offset: 20 },
33
+ { name: 'a_coarseShift', size: 1, type: 'f32', offset: 24 },
34
+ { name: 'a_coarseNormal', size: 4, type: 'i16', offset: 28 },
35
+ { name: 'a_coarseColor', size: 4, type: 'u8', normalized: true, offset: 36 },
36
+ ],
37
+ };
38
+ /** Locations for a patch made with `coverAt`: the plain ones and the two cover attributes. */
39
+ export const SPHERE_PATCH_COVER_ATTRIBS = { ...SPHERE_PATCH_ATTRIBS, a_cover: 6, a_coarseCover: 7 };
40
+ /**
41
+ * Layout of a patch made with `coverAt`: the plain layout followed by `a_cover` (4× u8 normalized: rgb what the
42
+ * content on the surface looks like from afar, a the share of the surface it hides) and `a_coarseCover`, the same
43
+ * of the cell one level coarser at this vertex. A shader blends the two by the morph factor like the colours.
44
+ */
45
+ export const SPHERE_PATCH_COVER_LAYOUT = {
46
+ stride: 48,
47
+ attribs: [
48
+ ...SPHERE_PATCH_LAYOUT.attribs,
49
+ { name: 'a_cover', size: 4, type: 'u8', normalized: true, offset: 40 },
50
+ { name: 'a_coarseCover', size: 4, type: 'u8', normalized: true, offset: 44 },
51
+ ],
52
+ };
53
+ /** Locations for a plain patch made with `extraAt`: the plain ones and the two extra attributes. */
54
+ export const SPHERE_PATCH_EXTRA_ATTRIBS = { ...SPHERE_PATCH_ATTRIBS, a_extra: 6, a_coarseExtra: 7 };
55
+ /** Locations for a patch made with both `coverAt` and `extraAt`. */
56
+ export const SPHERE_PATCH_COVER_EXTRA_ATTRIBS = { ...SPHERE_PATCH_COVER_ATTRIBS, a_extra: 8, a_coarseExtra: 9 };
57
+ /**
58
+ * Layout of a patch made with `extraAt` (no `coverAt`): the plain layout followed by `a_extra` (4× u8 normalized, the
59
+ * bytes `extraAt` gave) and `a_coarseExtra`, the same of the cell one level coarser, thinned like the colour.
60
+ */
61
+ export const SPHERE_PATCH_EXTRA_LAYOUT = {
62
+ stride: 48,
63
+ attribs: [
64
+ ...SPHERE_PATCH_LAYOUT.attribs,
65
+ { name: 'a_extra', size: 4, type: 'u8', normalized: true, offset: 40 },
66
+ { name: 'a_coarseExtra', size: 4, type: 'u8', normalized: true, offset: 44 },
67
+ ],
68
+ };
69
+ /** Layout of a patch made with `coverAt` and `extraAt`: the cover layout followed by `a_extra` and `a_coarseExtra`. */
70
+ export const SPHERE_PATCH_COVER_EXTRA_LAYOUT = {
71
+ stride: 56,
72
+ attribs: [
73
+ ...SPHERE_PATCH_COVER_LAYOUT.attribs,
74
+ { name: 'a_extra', size: 4, type: 'u8', normalized: true, offset: 48 },
75
+ { name: 'a_coarseExtra', size: 4, type: 'u8', normalized: true, offset: 52 },
76
+ ],
77
+ };
78
+ /**
79
+ * GLSL for a vertex shader of a renderer with radial up (paste it after `#include <header>`; it declares its own
80
+ * uniforms, the ones the streamed world sets with its `morph` option). `spherePatchMorph(p)` is the blend factor at a
81
+ * render-space point: 0 = the cell's own shape, 1 = the parent's. The distance is measured as the streaming manager
82
+ * measures it: along the sphere, combined with the viewer's height.
83
+ *
84
+ * vec3 p = a_pos + u_cellOffset;
85
+ * float m = spherePatchMorph(p);
86
+ * p += localUp(p) * (a_coarseShift * m);
87
+ * vec3 n = normalize(mix(a_normal.xyz, a_coarseNormal.xyz, m));
88
+ * vec4 c = mix(a_color, a_coarseColor, m);
89
+ */
90
+ export const SPHERE_PATCH_GLSL = /* glsl */ `
91
+ uniform vec4 u_morphEye; // xyz: the viewer in render space, w: its height above the surface
92
+ uniform vec2 u_cellMorph; // x: distance where the blend starts, y: 1 / length of the blend (0 = no blend)
93
+ float spherePatchMorph(vec3 p) {
94
+ vec3 d = p - u_morphEye.xyz;
95
+ #ifdef WH_LOCAL_UP
96
+ // Angle between the viewer and the point as seen from the centre, from small differences only.
97
+ vec3 e = u_morphEye.xyz - u_upCenter.xyz;
98
+ float along = u_upCenter.w * atan(length(cross(e, d)), dot(e, e + d));
99
+ #else
100
+ float along = length(d.xz);
101
+ #endif
102
+ float dist = sqrt(along * along + u_morphEye.w * u_morphEye.w);
103
+ return clamp((dist - u_cellMorph.x) * u_cellMorph.y, 0.0, 1.0);
104
+ }
105
+ `;
106
+ const KIND_TOP = 0;
107
+ const KIND_SKIRT = 1;
108
+ const MARGIN = 2;
109
+ const HALF_PI = 1.5707963267948966;
110
+ export function spherePatchMesh(o) {
111
+ const q = o.quads;
112
+ if (q < 2 || q % 2 !== 0)
113
+ throw new Error(`spherePatchMesh: quads must be even, got ${q}`);
114
+ if (!CUBE_FACE_AXES[o.face])
115
+ throw new Error(`spherePatchMesh: not a face of the cube: ${o.face}`);
116
+ const R = o.radiusM;
117
+ const n = q + 1;
118
+ const cells = 2 ** o.level;
119
+ /** Grid intervals across the whole face at this level. */
120
+ const N = q * cells;
121
+ const gx0 = o.x * q;
122
+ const gy0 = o.y * q;
123
+ // Samples on the cell's grid with a margin of two on each side, as far as the face reaches.
124
+ const m = n + 2 * MARGIN;
125
+ const dir = new Float64Array(m * m * 3);
126
+ const rad = new Float64Array(m * m).fill(NaN);
127
+ const pos = new Float64Array(m * m * 3);
128
+ const wu = new Float64Array(m);
129
+ const wv = new Float64Array(m);
130
+ for (let k = 0; k < m; k++) {
131
+ wu[k] = cubeWarp(cubeGrid(gx0 + k - MARGIN, N));
132
+ wv[k] = cubeWarp(cubeGrid(gy0 + k - MARGIN, N));
133
+ }
134
+ const d3 = [0, 0, 0];
135
+ for (let j = 0; j < m; j++) {
136
+ const gj = gy0 + j - MARGIN;
137
+ if (gj < 0 || gj > N)
138
+ continue;
139
+ for (let i = 0; i < m; i++) {
140
+ const gi = gx0 + i - MARGIN;
141
+ if (gi < 0 || gi > N)
142
+ continue;
143
+ const k = j * m + i;
144
+ cubeDirectionWarped(o.face, wu[i], wv[j], d3);
145
+ const r = R + o.heightAt(d3[0] * R, d3[1] * R, d3[2] * R);
146
+ dir.set(d3, k * 3);
147
+ rad[k] = r;
148
+ pos[k * 3] = d3[0] * r;
149
+ pos[k * 3 + 1] = d3[1] * r;
150
+ pos[k * 3 + 2] = d3[2] * r;
151
+ }
152
+ }
153
+ /** Index into the sample arrays of grid vertex (i, j) of the cell (−2 … n + 1). */
154
+ const at = (i, j) => (j + MARGIN) * m + (i + MARGIN);
155
+ const has = (i, j) => i >= -MARGIN && j >= -MARGIN && i < n + MARGIN && j < n + MARGIN && !Number.isNaN(rad[at(i, j)]);
156
+ // Normal from the grid: (P(i+s, j) − P(i−s, j)) × (P(i, j+s) − P(i, j−s)); u × v points outward.
157
+ const out3 = [0, 0, 0];
158
+ const gridNormal = (i, j, s) => {
159
+ const a = at(i + s, j) * 3;
160
+ const b = at(i - s, j) * 3;
161
+ const c = at(i, j + s) * 3;
162
+ const d = at(i, j - s) * 3;
163
+ return unitCross(pos[a] - pos[b], pos[a + 1] - pos[b + 1], pos[a + 2] - pos[b + 2], pos[c] - pos[d], pos[c + 1] - pos[d + 1], pos[c + 2] - pos[d + 2], out3);
164
+ };
165
+ // Normal from four samples placed by the direction alone, `eps` radians away: for the edges of the cube.
166
+ const q4 = new Float64Array(12);
167
+ const freeNormal = (k, eps) => {
168
+ const dx = dir[k * 3];
169
+ const dy = dir[k * 3 + 1];
170
+ const dz = dir[k * 3 + 2];
171
+ const { t1, t2 } = directionTangents([dx, dy, dz]);
172
+ for (let s = 0; s < 4; s++) {
173
+ const t = s < 2 ? t1 : t2;
174
+ const e = s % 2 === 0 ? eps : -eps;
175
+ let x = dx + t[0] * e;
176
+ let y = dy + t[1] * e;
177
+ let z = dz + t[2] * e;
178
+ const l = Math.sqrt(x * x + y * y + z * z);
179
+ x /= l;
180
+ y /= l;
181
+ z /= l;
182
+ const r = R + o.heightAt(x * R, y * R, z * R);
183
+ q4[s * 3] = x * r;
184
+ q4[s * 3 + 1] = y * r;
185
+ q4[s * 3 + 2] = z * r;
186
+ }
187
+ return unitCross(q4[0] - q4[3], q4[1] - q4[4], q4[2] - q4[5], q4[6] - q4[9], q4[7] - q4[10], q4[8] - q4[11], out3);
188
+ };
189
+ /** Nominal angle between two grid lines at this level. */
190
+ const eps = HALF_PI / N;
191
+ const own = new Float64Array(n * n);
192
+ const normals = new Int16Array(n * n * 3);
193
+ const colors = new Uint8Array(n * n * 4);
194
+ const covers = o.coverAt ? new Uint8Array(n * n * 4) : null;
195
+ const extras = o.extraAt ? new Uint8Array(n * n * 4) : null;
196
+ const cnx = new Float64Array(n * n);
197
+ const cny = new Float64Array(n * n);
198
+ const cnz = new Float64Array(n * n);
199
+ let error = 0;
200
+ // A straight edge between two vertices misses the middle of a curve by an eighth of the second difference; here in
201
+ // space, so the bulge of the sphere counts as well as the content.
202
+ const bend = (a, c, b) => Math.hypot(pos[a * 3] - 2 * pos[c * 3] + pos[b * 3], pos[a * 3 + 1] - 2 * pos[c * 3 + 1] + pos[b * 3 + 1], pos[a * 3 + 2] - 2 * pos[c * 3 + 2] + pos[b * 3 + 2]) / 8;
203
+ for (let j = 0; j < n; j++) {
204
+ for (let i = 0; i < n; i++) {
205
+ const k = j * n + i;
206
+ const s = at(i, j);
207
+ own[k] = rad[s];
208
+ const gi = gx0 + i;
209
+ const gj = gy0 + j;
210
+ const onEdge = gi === 0 || gi === N || gj === 0 || gj === N;
211
+ const nrm = onEdge ? freeNormal(s, eps) : gridNormal(i, j, 1);
212
+ normals[k * 3] = Math.round(nrm[0] * 32767);
213
+ normals[k * 3 + 1] = Math.round(nrm[1] * 32767);
214
+ normals[k * 3 + 2] = Math.round(nrm[2] * 32767);
215
+ const c = o.colorAt ? o.colorAt(dir[s * 3] * R, dir[s * 3 + 1] * R, dir[s * 3 + 2] * R, rad[s] - R) : [128, 128, 128, 255];
216
+ for (let ch = 0; ch < 4; ch++)
217
+ colors[k * 4 + ch] = c[ch];
218
+ if (covers && o.coverAt) {
219
+ const cv = o.coverAt(dir[s * 3] * R, dir[s * 3 + 1] * R, dir[s * 3 + 2] * R, rad[s] - R);
220
+ for (let ch = 0; ch < 4; ch++)
221
+ covers[k * 4 + ch] = cv[ch];
222
+ }
223
+ if (extras && o.extraAt) {
224
+ const ex = o.extraAt(dir[s * 3] * R, dir[s * 3 + 1] * R, dir[s * 3 + 2] * R, rad[s] - R);
225
+ for (let ch = 0; ch < 4; ch++)
226
+ extras[k * 4 + ch] = ex[ch];
227
+ }
228
+ if (i % 2 === 0 && j % 2 === 0) {
229
+ const pn = onEdge ? freeNormal(s, 2 * eps) : gridNormal(i, j, 2);
230
+ cnx[k] = pn[0];
231
+ cny[k] = pn[1];
232
+ cnz[k] = pn[2];
233
+ }
234
+ if (has(i - 1, j) && has(i + 1, j))
235
+ error = Math.max(error, bend(at(i - 1, j), s, at(i + 1, j)));
236
+ if (has(i, j - 1) && has(i, j + 1))
237
+ error = Math.max(error, bend(at(i, j - 1), s, at(i, j + 1)));
238
+ }
239
+ }
240
+ // The middle of a quad lies on its diagonal; a twisted quad misses the mean of its corners by a quarter of the twist.
241
+ for (let j = 0; j < q; j++) {
242
+ for (let i = 0; i < q; i++) {
243
+ const a = at(i, j) * 3;
244
+ const b = at(i + 1, j) * 3;
245
+ const c = at(i, j + 1) * 3;
246
+ const d = at(i + 1, j + 1) * 3;
247
+ const twist = Math.hypot(pos[a] + pos[d] - pos[b] - pos[c], pos[a + 1] + pos[d + 1] - pos[b + 1] - pos[c + 1], pos[a + 2] + pos[d + 2] - pos[b + 2] - pos[c + 2]) / 4;
248
+ if (twist > error)
249
+ error = twist;
250
+ }
251
+ }
252
+ // The parent's surface at every vertex: where the line from the centre through the vertex meets the parent's
253
+ // triangle (the parent's vertices are every second vertex, its quads are split by the same diagonal).
254
+ //
255
+ // A vertex on a line of the parent's grid lies on the parent's edge between the two vertices of that line. Only those
256
+ // two enter there, through their mean and their difference, so the result has the same bits whichever of them comes
257
+ // first: cells that share the line agree, and across an edge of the cube the neighbour walks it the other way.
258
+ const edgeRadius = (a, b, s) => {
259
+ const mx = (pos[a] + pos[b]) * 0.5;
260
+ const my = (pos[a + 1] + pos[b + 1]) * 0.5;
261
+ const mz = (pos[a + 2] + pos[b + 2]) * 0.5;
262
+ const ex = pos[b] - pos[a];
263
+ const ey = pos[b + 1] - pos[a + 1];
264
+ const ez = pos[b + 2] - pos[a + 2];
265
+ const wx = my * ez - mz * ey;
266
+ const wy = mz * ex - mx * ez;
267
+ const wz = mx * ey - my * ex;
268
+ // Across the edge, in the plane of the edge and the centre.
269
+ const nx = ey * wz - ez * wy;
270
+ const ny = ez * wx - ex * wz;
271
+ const nz = ex * wy - ey * wx;
272
+ return (nx * mx + ny * my + nz * mz) / (nx * dir[s] + ny * dir[s + 1] + nz * dir[s + 2]);
273
+ };
274
+ const coarse = new Float64Array(n * n);
275
+ for (let j = 0; j < n; j++) {
276
+ const j0 = Math.min(j - (j % 2), q - 2);
277
+ for (let i = 0; i < n; i++) {
278
+ const k = j * n + i;
279
+ if (i % 2 === 0 && j % 2 === 0) {
280
+ coarse[k] = own[k];
281
+ continue;
282
+ }
283
+ if (i % 2 === 0 || j % 2 === 0) {
284
+ const along = i % 2 !== 0;
285
+ coarse[k] = edgeRadius((along ? at(i - 1, j) : at(i, j - 1)) * 3, (along ? at(i + 1, j) : at(i, j + 1)) * 3, at(i, j) * 3);
286
+ continue;
287
+ }
288
+ // Inside a quad of the parent. Which of its two triangles the line from the centre passes through is decided by
289
+ // the plane through the centre and the quad's diagonal; the diagonal of the grid is not that plane's arc, so
290
+ // the indices alone do not tell.
291
+ const i0 = Math.min(i - (i % 2), q - 2);
292
+ const qa = at(i0, j0) * 3;
293
+ const qb = at(i0 + 2, j0) * 3;
294
+ const qc = at(i0, j0 + 2) * 3;
295
+ const lower = sameSide(pos, qa, qb, qc, dir[at(i, j) * 3], dir[at(i, j) * 3 + 1], dir[at(i, j) * 3 + 2]);
296
+ const a = lower ? qa : at(i0 + 2, j0 + 2) * 3;
297
+ const b = lower ? qb : qc;
298
+ const c = lower ? qc : qb;
299
+ const mn = unitCross(pos[b] - pos[a], pos[b + 1] - pos[a + 1], pos[b + 2] - pos[a + 2], pos[c] - pos[a], pos[c + 1] - pos[a + 1], pos[c + 2] - pos[a + 2], out3);
300
+ const s = at(i, j) * 3;
301
+ coarse[k] = (mn[0] * pos[a] + mn[1] * pos[a + 1] + mn[2] * pos[a + 2]) / (mn[0] * dir[s] + mn[1] * dir[s + 1] + mn[2] * dir[s + 2]);
302
+ }
303
+ }
304
+ const coarseNx = HeightField.coarseSurface(n, n, cnx, 2);
305
+ const coarseNy = HeightField.coarseSurface(n, n, cny, 2);
306
+ const coarseNz = HeightField.coarseSurface(n, n, cnz, 2);
307
+ // On a line of the parent's grid: the mean of its two vertices as a sum, the same bits whichever comes first.
308
+ for (let j = 0; j < n; j++) {
309
+ for (let i = 0; i < n; i++) {
310
+ if ((i % 2 === 0) === (j % 2 === 0))
311
+ continue;
312
+ const k = j * n + i;
313
+ const a = i % 2 !== 0 ? k - 1 : k - n;
314
+ const b = i % 2 !== 0 ? k + 1 : k + n;
315
+ coarseNx[k] = (cnx[a] + cnx[b]) * 0.5;
316
+ coarseNy[k] = (cny[a] + cny[b]) * 0.5;
317
+ coarseNz[k] = (cnz[a] + cnz[b]) * 0.5;
318
+ }
319
+ }
320
+ const channel = new Float64Array(n * n);
321
+ const coarseColors = new Uint8Array(n * n * 4);
322
+ for (let ch = 0; ch < 4; ch++) {
323
+ for (let k = 0; k < n * n; k++)
324
+ channel[k] = colors[k * 4 + ch];
325
+ const thin = HeightField.coarseSurface(n, n, channel, 2);
326
+ for (let k = 0; k < n * n; k++)
327
+ coarseColors[k * 4 + ch] = Math.round(thin[k]);
328
+ }
329
+ // The cover of the coarser level, thinned the same way as its colour.
330
+ const coarseCovers = covers ? new Uint8Array(n * n * 4) : null;
331
+ if (covers && coarseCovers) {
332
+ for (let ch = 0; ch < 4; ch++) {
333
+ for (let k = 0; k < n * n; k++)
334
+ channel[k] = covers[k * 4 + ch];
335
+ const thin = HeightField.coarseSurface(n, n, channel, 2);
336
+ for (let k = 0; k < n * n; k++)
337
+ coarseCovers[k * 4 + ch] = Math.round(thin[k]);
338
+ }
339
+ }
340
+ // The extra bytes of the coarser level, thinned the same way as the colour.
341
+ const coarseExtras = extras ? new Uint8Array(n * n * 4) : null;
342
+ if (extras && coarseExtras) {
343
+ for (let ch = 0; ch < 4; ch++) {
344
+ for (let k = 0; k < n * n; k++)
345
+ channel[k] = extras[k * 4 + ch];
346
+ const thin = HeightField.coarseSurface(n, n, channel, 2);
347
+ for (let k = 0; k < n * n; k++)
348
+ coarseExtras[k * 4 + ch] = Math.round(thin[k]);
349
+ }
350
+ }
351
+ // Skirt depth: twice the largest step this border can show against another level, and never under the least depth.
352
+ let borderGap = 0;
353
+ for (let k = 0; k < n; k++) {
354
+ for (const [i, j, alongI] of [[k, 0, true], [k, q, true], [0, k, false], [q, k, false]]) {
355
+ let g = Math.abs(coarse[j * n + i] - own[j * n + i]);
356
+ const a = alongI ? [i - 1, j] : [i, j - 1];
357
+ const b = alongI ? [i + 1, j] : [i, j + 1];
358
+ if (has(a[0], a[1]) && has(b[0], b[1]))
359
+ g = Math.max(g, bend(at(a[0], a[1]), at(i, j), at(b[0], b[1])));
360
+ if (g > borderGap)
361
+ borderGap = g;
362
+ }
363
+ }
364
+ const skirt = o.skirtDepth > 0;
365
+ const skirtDepth = skirt ? Math.max(o.skirtDepth, 2 * borderGap) : 0;
366
+ const cellsAcross = 2 * cells;
367
+ const mid = cubeDirection(o.face, cubeGrid(2 * o.x + 1, cellsAcross), cubeGrid(2 * o.y + 1, cellsAcross));
368
+ const origin = [mid[0] * R, mid[1] * R, mid[2] * R];
369
+ const vertCount = n * n + (skirt ? 8 * n : 0);
370
+ const layout = extras ? (covers ? SPHERE_PATCH_COVER_EXTRA_LAYOUT : SPHERE_PATCH_EXTRA_LAYOUT) : covers ? SPHERE_PATCH_COVER_LAYOUT : SPHERE_PATCH_LAYOUT;
371
+ const extraOff = covers ? 48 : 40;
372
+ const STRIDE = layout.stride;
373
+ const buf = new ArrayBuffer(vertCount * STRIDE);
374
+ const view = new DataView(buf);
375
+ const min = [Infinity, Infinity, Infinity];
376
+ const max = [-Infinity, -Infinity, -Infinity];
377
+ let minHeight = Infinity;
378
+ let maxHeight = -Infinity;
379
+ const put = (v, i, j, drop, kind) => {
380
+ const k = j * n + i;
381
+ const s = at(i, j) * 3;
382
+ const off = v * STRIDE;
383
+ const r = own[k] - drop;
384
+ const shift = Math.fround(coarse[k] - own[k]);
385
+ for (let c = 0; c < 3; c++) {
386
+ const d = dir[s + c];
387
+ const p = Math.fround(d * r - origin[c]);
388
+ view.setFloat32(off + c * 4, p, true);
389
+ // The vertex as drawn at blend 0 and at blend 1, with a millimetre for the rounding of the shader's arithmetic.
390
+ const moved = p + d * shift;
391
+ if (Math.min(p, moved) - 1e-3 < min[c])
392
+ min[c] = Math.min(p, moved) - 1e-3;
393
+ if (Math.max(p, moved) + 1e-3 > max[c])
394
+ max[c] = Math.max(p, moved) + 1e-3;
395
+ }
396
+ view.setInt16(off + 12, normals[k * 3], true);
397
+ view.setInt16(off + 14, normals[k * 3 + 1], true);
398
+ view.setInt16(off + 16, normals[k * 3 + 2], true);
399
+ view.setInt16(off + 18, kind, true);
400
+ for (let c = 0; c < 4; c++)
401
+ view.setUint8(off + 20 + c, colors[k * 4 + c]);
402
+ view.setFloat32(off + 24, shift, true);
403
+ const cl = Math.sqrt(coarseNx[k] * coarseNx[k] + coarseNy[k] * coarseNy[k] + coarseNz[k] * coarseNz[k]) || 1;
404
+ view.setInt16(off + 28, Math.round((coarseNx[k] / cl) * 32767), true);
405
+ view.setInt16(off + 30, Math.round((coarseNy[k] / cl) * 32767), true);
406
+ view.setInt16(off + 32, Math.round((coarseNz[k] / cl) * 32767), true);
407
+ view.setInt16(off + 34, 0, true);
408
+ for (let c = 0; c < 4; c++)
409
+ view.setUint8(off + 36 + c, coarseColors[k * 4 + c]);
410
+ if (covers && coarseCovers) {
411
+ for (let c = 0; c < 4; c++) {
412
+ view.setUint8(off + 40 + c, covers[k * 4 + c]);
413
+ view.setUint8(off + 44 + c, coarseCovers[k * 4 + c]);
414
+ }
415
+ }
416
+ if (extras && coarseExtras) {
417
+ for (let c = 0; c < 4; c++) {
418
+ view.setUint8(off + extraOff + c, extras[k * 4 + c]);
419
+ view.setUint8(off + extraOff + 4 + c, coarseExtras[k * 4 + c]);
420
+ }
421
+ }
422
+ if (kind === KIND_TOP) {
423
+ minHeight = Math.min(minHeight, own[k] - R, coarse[k] - R);
424
+ maxHeight = Math.max(maxHeight, own[k] - R, coarse[k] - R);
425
+ }
426
+ };
427
+ for (let j = 0; j < n; j++)
428
+ for (let i = 0; i < n; i++)
429
+ put(j * n + i, i, j, 0, KIND_TOP);
430
+ const quadCount = q * q + (skirt ? 4 * q : 0);
431
+ const big = vertCount > 65535;
432
+ const idx = big ? new Uint32Array(quadCount * 6) : new Uint16Array(quadCount * 6);
433
+ let w = 0;
434
+ for (let j = 0; j < q; j++) {
435
+ for (let i = 0; i < q; i++) {
436
+ const a = j * n + i;
437
+ const b = a + 1;
438
+ const c = a + n;
439
+ const d = c + 1;
440
+ // u × v points outward: (a, b, c) winds anticlockwise seen from outside.
441
+ idx[w++] = a;
442
+ idx[w++] = b;
443
+ idx[w++] = c;
444
+ idx[w++] = b;
445
+ idx[w++] = d;
446
+ idx[w++] = c;
447
+ }
448
+ }
449
+ if (skirt) {
450
+ // Four sides, each a strip of vertex pairs: the border vertex and one below it, both with the border vertex's
451
+ // blend attributes, so the skirt follows the border at every blend factor. Each strip faces away from the cell.
452
+ let v = n * n;
453
+ const side = (vertex, flip) => {
454
+ const start = v;
455
+ for (let k = 0; k < n; k++) {
456
+ const [i, j] = vertex(k);
457
+ put(v++, i, j, 0, KIND_SKIRT);
458
+ put(v++, i, j, skirtDepth, KIND_SKIRT);
459
+ }
460
+ for (let k = 0; k < q; k++) {
461
+ const t0 = start + k * 2;
462
+ const b0 = t0 + 1;
463
+ const t1 = t0 + 2;
464
+ const b1 = t0 + 3;
465
+ if (flip) {
466
+ idx[w++] = t0;
467
+ idx[w++] = b0;
468
+ idx[w++] = t1;
469
+ idx[w++] = t1;
470
+ idx[w++] = b0;
471
+ idx[w++] = b1;
472
+ }
473
+ else {
474
+ idx[w++] = t0;
475
+ idx[w++] = t1;
476
+ idx[w++] = b0;
477
+ idx[w++] = t1;
478
+ idx[w++] = b1;
479
+ idx[w++] = b0;
480
+ }
481
+ }
482
+ };
483
+ side((k) => [k, 0], true);
484
+ side((k) => [k, q], false);
485
+ side((k) => [0, k], false);
486
+ side((k) => [q, k], true);
487
+ }
488
+ return { mesh: { layout, vertices: new Uint8Array(buf), indices: idx, count: w }, origin, bounds: { min, max }, minHeight, maxHeight, error, skirtDepth };
489
+ }
490
+ /**
491
+ * Distance from the sphere's centre at which the line along the unit direction `dir` meets the surface a patch draws:
492
+ * the flat triangles of its own grid (`step` 1) or of the cell one level coarser (`step` 2, the shape the patch
493
+ * blends into). For standing something on the mesh of a level instead of on the ideal surface, where it would float
494
+ * above a coarse level or sink into it. `dir` should point into the cell; outside it the nearest quad is used.
495
+ */
496
+ export function spherePatchRadius(o, dir, step = 1) {
497
+ const R = o.radiusM;
498
+ const N = o.quads * 2 ** o.level;
499
+ const gx0 = o.x * o.quads;
500
+ const gy0 = o.y * o.quads;
501
+ const [s, t] = cubeFaceCoords(o.face, dir);
502
+ const count = o.quads / step;
503
+ const gu = (((s + 1) / 2) * N - gx0) / step;
504
+ const gv = (((t + 1) / 2) * N - gy0) / step;
505
+ const i = Math.min(count - 1, Math.max(0, Math.floor(gu)));
506
+ const j = Math.min(count - 1, Math.max(0, Math.floor(gv)));
507
+ const vertex = (a, b) => {
508
+ const d = cubeDirection(o.face, cubeGrid(gx0 + a * step, N), cubeGrid(gy0 + b * step, N));
509
+ const r = R + o.heightAt(d[0] * R, d[1] * R, d[2] * R);
510
+ return [d[0] * r, d[1] * r, d[2] * r];
511
+ };
512
+ // The same split of a quad as the mesh, along the diagonal from (i + 1, j) to (i, j + 1). The line meets the triangle
513
+ // on its side of the plane through the centre and that diagonal.
514
+ const v00 = vertex(i, j);
515
+ const v10 = vertex(i + 1, j);
516
+ const v01 = vertex(i, j + 1);
517
+ const lower = sameSide([...v00, ...v10, ...v01], 0, 3, 6, dir[0], dir[1], dir[2]);
518
+ const A = lower ? v00 : vertex(i + 1, j + 1);
519
+ const B = lower ? v10 : v01;
520
+ const C = lower ? v01 : v10;
521
+ const n = unitCross(B[0] - A[0], B[1] - A[1], B[2] - A[2], C[0] - A[0], C[1] - A[1], C[2] - A[2], [0, 0, 0]);
522
+ return (n[0] * A[0] + n[1] * A[1] + n[2] * A[2]) / (n[0] * dir[0] + n[1] * dir[1] + n[2] * dir[2]);
523
+ }
524
+ /**
525
+ * True when the direction (dx, dy, dz) lies on the same side as point `a` of the plane through the centre and the
526
+ * points `b` and `c` (offsets into `p`), or in that plane.
527
+ */
528
+ function sameSide(p, a, b, c, dx, dy, dz) {
529
+ const wx = p[b + 1] * p[c + 2] - p[b + 2] * p[c + 1];
530
+ const wy = p[b + 2] * p[c] - p[b] * p[c + 2];
531
+ const wz = p[b] * p[c + 1] - p[b + 1] * p[c];
532
+ return (dx * wx + dy * wy + dz * wz) * (p[a] * wx + p[a + 1] * wy + p[a + 2] * wz) >= 0;
533
+ }
534
+ /** Unit vector along a × b, written into `out`. */
535
+ function unitCross(ax, ay, az, bx, by, bz, out) {
536
+ const x = ay * bz - az * by;
537
+ const y = az * bx - ax * bz;
538
+ const z = ax * by - ay * bx;
539
+ const l = Math.sqrt(x * x + y * y + z * z) || 1;
540
+ out[0] = x / l;
541
+ out[1] = y / l;
542
+ out[2] = z / l;
543
+ return out;
544
+ }
@@ -0,0 +1,34 @@
1
+ import type { CellId, FlatWorld } from './cell.ts';
2
+ type P3 = readonly [number, number, number];
3
+ export interface StripSurface {
4
+ /** Radius of the base sphere; 0 = the plane. */
5
+ readonly radiusM: number;
6
+ /** The cell of `level` that holds the base point p. */
7
+ cellAt(level: number, p: P3): CellId;
8
+ /** What the cell's meshes are relative to (the bits the surface mesh uses). */
9
+ origin(cell: CellId): [number, number, number];
10
+ /** Nominal vertex spacing of a level in metres. */
11
+ spacingM(level: number): number;
12
+ /**
13
+ * Ascending shares t ∈ [0, 1] of the straight way a → b: where it enters and leaves `cell` (first and last) and,
14
+ * between them, where it meets an edge of the triangles the cell's level draws (both families of grid lines and the
15
+ * diagonal each quad is split by). Empty when the way misses the cell. Every piece between two shares belongs to
16
+ * the cell: a piece on a border belongs to the cell `cellAt` gives for its middle.
17
+ */
18
+ cuts(cell: CellId, a: P3, b: P3): number[];
19
+ /** Height above the base surface of the flat triangles `level` draws over base point p, in whichever cell holds it. */
20
+ heightOn(level: number, p: P3): number;
21
+ }
22
+ /** The plane of a flat world whose cells are drawn with `quads` intervals along their edge (`cellMorphMesh`). */
23
+ export declare function flatStripSurface(o: {
24
+ shape: FlatWorld;
25
+ quads: number;
26
+ heightAt: (x: number, z: number) => number;
27
+ }): StripSurface;
28
+ /** The sphere of six quadtrees whose cells are drawn by `spherePatchMesh` with `quads` intervals along their edge. */
29
+ export declare function sphereStripSurface(o: {
30
+ radiusM: number;
31
+ quads: number;
32
+ heightAt: (x: number, y: number, z: number) => number;
33
+ }): StripSurface;
34
+ export {};