@arraypress/effects-astro 0.0.0-stage → 0.1.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 (314) hide show
  1. package/AGENTS.md +85 -0
  2. package/COMPONENTS.md +1264 -0
  3. package/LICENSE +139 -0
  4. package/README.md +197 -2
  5. package/package.json +60 -4
  6. package/src/components/AsciiRain.astro +36 -0
  7. package/src/components/BorderSweep.astro +39 -0
  8. package/src/components/Caustics.astro +39 -0
  9. package/src/components/ColorPanels.astro +62 -0
  10. package/src/components/Cursor.astro +97 -0
  11. package/src/components/DotGrid.astro +34 -0
  12. package/src/components/DotSurface.astro +35 -0
  13. package/src/components/EffectCanvas.astro +152 -0
  14. package/src/components/FitText.astro +35 -0
  15. package/src/components/FlutedGlass.astro +36 -0
  16. package/src/components/Glass.astro +69 -0
  17. package/src/components/GlassGlyph.astro +38 -0
  18. package/src/components/GlowField.astro +177 -0
  19. package/src/components/Grain.astro +98 -0
  20. package/src/components/HaloRing.astro +38 -0
  21. package/src/components/HighlightSweep.astro +74 -0
  22. package/src/components/ImageRipple.astro +56 -0
  23. package/src/components/Ink.astro +37 -0
  24. package/src/components/InkField.astro +45 -0
  25. package/src/components/IsoCity.astro +54 -0
  26. package/src/components/LightField.astro +40 -0
  27. package/src/components/LiquidMetal.astro +41 -0
  28. package/src/components/Magnetic.astro +52 -0
  29. package/src/components/Marquee.astro +216 -0
  30. package/src/components/MosaicReveal.astro +196 -0
  31. package/src/components/NebulaLogo.astro +38 -0
  32. package/src/components/NeonSwarm.astro +38 -0
  33. package/src/components/Ocean.astro +40 -0
  34. package/src/components/OutlineWordmark.astro +222 -0
  35. package/src/components/PaperBackground.astro +40 -0
  36. package/src/components/PaperShader.astro +55 -0
  37. package/src/components/Parallax.astro +49 -0
  38. package/src/components/ParticleLogo.astro +37 -0
  39. package/src/components/PixelGlitchText.astro +70 -0
  40. package/src/components/PointCloud.astro +37 -0
  41. package/src/components/ProductStack.astro +298 -0
  42. package/src/components/PulseMark.astro +189 -0
  43. package/src/components/RadiantGlass.astro +38 -0
  44. package/src/components/RainOnGlass.astro +71 -0
  45. package/src/components/Reveal.astro +115 -0
  46. package/src/components/ScrambleText.astro +52 -0
  47. package/src/components/ScrollReveal.astro +38 -0
  48. package/src/components/ShaderBackground.astro +55 -0
  49. package/src/components/SignalLines.astro +41 -0
  50. package/src/components/SilkRibbon.astro +37 -0
  51. package/src/components/SpotlightText.astro +131 -0
  52. package/src/components/Tilt.astro +79 -0
  53. package/src/components/Topography.astro +38 -0
  54. package/src/components/Typewriter.astro +66 -0
  55. package/src/components/VariableType.astro +59 -0
  56. package/src/components/VoxelLogo.astro +46 -0
  57. package/src/components/WaveBars.astro +41 -0
  58. package/src/components/WorldRoutes.astro +41 -0
  59. package/src/components/style.js +19 -0
  60. package/src/index.d.ts +143 -0
  61. package/src/index.js +105 -0
  62. package/src/runtime/ascii-rain.d.ts +53 -0
  63. package/src/runtime/ascii-rain.js +355 -0
  64. package/src/runtime/audio-meter.d.ts +65 -0
  65. package/src/runtime/audio-meter.js +282 -0
  66. package/src/runtime/audio.d.ts +26 -0
  67. package/src/runtime/audio.js +453 -0
  68. package/src/runtime/blur.d.ts +2 -0
  69. package/src/runtime/blur.js +56 -0
  70. package/src/runtime/canvas2d.d.ts +23 -0
  71. package/src/runtime/canvas2d.js +105 -0
  72. package/src/runtime/caustics.d.ts +40 -0
  73. package/src/runtime/caustics.js +286 -0
  74. package/src/runtime/color.d.ts +33 -0
  75. package/src/runtime/color.js +213 -0
  76. package/src/runtime/curl.d.ts +5 -0
  77. package/src/runtime/curl.js +81 -0
  78. package/src/runtime/cursor.d.ts +9 -0
  79. package/src/runtime/cursor.js +80 -0
  80. package/src/runtime/device.d.ts +14 -0
  81. package/src/runtime/device.js +73 -0
  82. package/src/runtime/dot-grid.d.ts +27 -0
  83. package/src/runtime/dot-grid.js +245 -0
  84. package/src/runtime/dot-surface.d.ts +46 -0
  85. package/src/runtime/dot-surface.js +429 -0
  86. package/src/runtime/feedback.d.ts +18 -0
  87. package/src/runtime/feedback.js +131 -0
  88. package/src/runtime/fit-text.d.ts +5 -0
  89. package/src/runtime/fit-text.js +60 -0
  90. package/src/runtime/fluid.d.ts +55 -0
  91. package/src/runtime/fluid.js +504 -0
  92. package/src/runtime/fluted-glass.d.ts +27 -0
  93. package/src/runtime/fluted-glass.js +157 -0
  94. package/src/runtime/gl.d.ts +19 -0
  95. package/src/runtime/gl.js +104 -0
  96. package/src/runtime/glass-glyph.d.ts +49 -0
  97. package/src/runtime/glass-glyph.js +355 -0
  98. package/src/runtime/glass-sheen.d.ts +2 -0
  99. package/src/runtime/glass-sheen.js +30 -0
  100. package/src/runtime/glsl.d.ts +17 -0
  101. package/src/runtime/glsl.js +78 -0
  102. package/src/runtime/glyphs.d.ts +22 -0
  103. package/src/runtime/glyphs.js +107 -0
  104. package/src/runtime/halo-ring.d.ts +47 -0
  105. package/src/runtime/halo-ring.js +334 -0
  106. package/src/runtime/height-field.d.ts +50 -0
  107. package/src/runtime/height-field.js +176 -0
  108. package/src/runtime/image-ripple.d.ts +25 -0
  109. package/src/runtime/image-ripple.js +353 -0
  110. package/src/runtime/image.d.ts +11 -0
  111. package/src/runtime/image.js +81 -0
  112. package/src/runtime/in-view.d.ts +6 -0
  113. package/src/runtime/in-view.js +36 -0
  114. package/src/runtime/ink-field/shaders.js +258 -0
  115. package/src/runtime/ink-field/sim.js +582 -0
  116. package/src/runtime/ink-field.d.ts +39 -0
  117. package/src/runtime/ink-field.js +311 -0
  118. package/src/runtime/ink.d.ts +31 -0
  119. package/src/runtime/ink.js +109 -0
  120. package/src/runtime/instancing.d.ts +12 -0
  121. package/src/runtime/instancing.js +37 -0
  122. package/src/runtime/iso-camera.d.ts +35 -0
  123. package/src/runtime/iso-camera.js +85 -0
  124. package/src/runtime/iso-city/figures.d.ts +30 -0
  125. package/src/runtime/iso-city/figures.js +136 -0
  126. package/src/runtime/iso-city/look.d.ts +34 -0
  127. package/src/runtime/iso-city/look.js +105 -0
  128. package/src/runtime/iso-city/plan.d.ts +59 -0
  129. package/src/runtime/iso-city/plan.js +463 -0
  130. package/src/runtime/iso-city/roads.d.ts +64 -0
  131. package/src/runtime/iso-city/roads.js +320 -0
  132. package/src/runtime/iso-city/scene.d.ts +27 -0
  133. package/src/runtime/iso-city/scene.js +432 -0
  134. package/src/runtime/iso-city/shaders.d.ts +6 -0
  135. package/src/runtime/iso-city/shaders.js +233 -0
  136. package/src/runtime/iso-city/shadows.d.ts +13 -0
  137. package/src/runtime/iso-city/shadows.js +148 -0
  138. package/src/runtime/iso-city/traffic.d.ts +80 -0
  139. package/src/runtime/iso-city/traffic.js +457 -0
  140. package/src/runtime/iso-city.d.ts +37 -0
  141. package/src/runtime/iso-city.js +458 -0
  142. package/src/runtime/lens-map.d.ts +11 -0
  143. package/src/runtime/lens-map.js +146 -0
  144. package/src/runtime/light-field.d.ts +29 -0
  145. package/src/runtime/light-field.js +292 -0
  146. package/src/runtime/liquid-glass.d.ts +48 -0
  147. package/src/runtime/liquid-glass.js +423 -0
  148. package/src/runtime/liquid-metal/beads.js +217 -0
  149. package/src/runtime/liquid-metal/field.js +88 -0
  150. package/src/runtime/liquid-metal/look.js +64 -0
  151. package/src/runtime/liquid-metal/shaders.js +255 -0
  152. package/src/runtime/liquid-metal/wave.js +53 -0
  153. package/src/runtime/liquid-metal.d.ts +46 -0
  154. package/src/runtime/liquid-metal.js +366 -0
  155. package/src/runtime/loop.d.ts +7 -0
  156. package/src/runtime/loop.js +112 -0
  157. package/src/runtime/magnetic.d.ts +9 -0
  158. package/src/runtime/magnetic.js +46 -0
  159. package/src/runtime/marquee.d.ts +2 -0
  160. package/src/runtime/marquee.js +27 -0
  161. package/src/runtime/mesh.d.ts +30 -0
  162. package/src/runtime/mesh.js +217 -0
  163. package/src/runtime/mosaic-reveal.d.ts +34 -0
  164. package/src/runtime/mosaic-reveal.js +504 -0
  165. package/src/runtime/motion.d.ts +2 -0
  166. package/src/runtime/motion.js +21 -0
  167. package/src/runtime/mount.d.ts +13 -0
  168. package/src/runtime/mount.js +120 -0
  169. package/src/runtime/nebula-logo.d.ts +31 -0
  170. package/src/runtime/nebula-logo.js +282 -0
  171. package/src/runtime/neon-swarm.d.ts +31 -0
  172. package/src/runtime/neon-swarm.js +258 -0
  173. package/src/runtime/ocean/presets.d.ts +54 -0
  174. package/src/runtime/ocean/presets.js +184 -0
  175. package/src/runtime/ocean/shader.d.ts +14 -0
  176. package/src/runtime/ocean/shader.js +389 -0
  177. package/src/runtime/ocean/view.d.ts +24 -0
  178. package/src/runtime/ocean/view.js +98 -0
  179. package/src/runtime/ocean/waves.d.ts +29 -0
  180. package/src/runtime/ocean/waves.js +119 -0
  181. package/src/runtime/ocean.d.ts +31 -0
  182. package/src/runtime/ocean.js +173 -0
  183. package/src/runtime/palettes.d.ts +37 -0
  184. package/src/runtime/palettes.js +75 -0
  185. package/src/runtime/paper-look.d.ts +42 -0
  186. package/src/runtime/paper-look.js +91 -0
  187. package/src/runtime/paper-looks.d.ts +17 -0
  188. package/src/runtime/paper-looks.js +21 -0
  189. package/src/runtime/paper.d.ts +37 -0
  190. package/src/runtime/paper.js +303 -0
  191. package/src/runtime/particle-logo.d.ts +31 -0
  192. package/src/runtime/particle-logo.js +196 -0
  193. package/src/runtime/ping-pong.d.ts +43 -0
  194. package/src/runtime/ping-pong.js +175 -0
  195. package/src/runtime/pixel-glitch.d.ts +17 -0
  196. package/src/runtime/pixel-glitch.js +287 -0
  197. package/src/runtime/point-cloud.d.ts +33 -0
  198. package/src/runtime/point-cloud.js +287 -0
  199. package/src/runtime/pointer.d.ts +27 -0
  200. package/src/runtime/pointer.js +128 -0
  201. package/src/runtime/points.d.ts +25 -0
  202. package/src/runtime/points.js +130 -0
  203. package/src/runtime/pools.d.ts +2 -0
  204. package/src/runtime/pools.js +25 -0
  205. package/src/runtime/product-stack.d.ts +24 -0
  206. package/src/runtime/product-stack.js +210 -0
  207. package/src/runtime/radiant-glass.d.ts +29 -0
  208. package/src/runtime/radiant-glass.js +282 -0
  209. package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
  210. package/src/runtime/rain-on-glass/backdrop.js +185 -0
  211. package/src/runtime/rain-on-glass/shader.d.ts +2 -0
  212. package/src/runtime/rain-on-glass/shader.js +168 -0
  213. package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
  214. package/src/runtime/rain-on-glass/wipe.js +65 -0
  215. package/src/runtime/rain-on-glass.d.ts +27 -0
  216. package/src/runtime/rain-on-glass.js +210 -0
  217. package/src/runtime/ramp.d.ts +2 -0
  218. package/src/runtime/ramp.js +30 -0
  219. package/src/runtime/random.d.ts +2 -0
  220. package/src/runtime/random.js +22 -0
  221. package/src/runtime/scroll-reveal.d.ts +22 -0
  222. package/src/runtime/scroll-reveal.js +73 -0
  223. package/src/runtime/shader-background.d.ts +42 -0
  224. package/src/runtime/shader-background.js +261 -0
  225. package/src/runtime/shader-backgrounds/band-lens.js +72 -0
  226. package/src/runtime/shader-backgrounds/common.js +53 -0
  227. package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
  228. package/src/runtime/shader-backgrounds/grid.js +33 -0
  229. package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
  230. package/src/runtime/shader-backgrounds/silk.js +57 -0
  231. package/src/runtime/shader-backgrounds/waves.js +31 -0
  232. package/src/runtime/shape.d.ts +42 -0
  233. package/src/runtime/shape.js +313 -0
  234. package/src/runtime/silk-ribbon.d.ts +23 -0
  235. package/src/runtime/silk-ribbon.js +206 -0
  236. package/src/runtime/slideshow.d.ts +27 -0
  237. package/src/runtime/slideshow.js +234 -0
  238. package/src/runtime/spotlight.d.ts +9 -0
  239. package/src/runtime/spotlight.js +97 -0
  240. package/src/runtime/stack-poses.d.ts +53 -0
  241. package/src/runtime/stack-poses.js +273 -0
  242. package/src/runtime/stage.d.ts +47 -0
  243. package/src/runtime/stage.js +187 -0
  244. package/src/runtime/swarm.d.ts +49 -0
  245. package/src/runtime/swarm.js +199 -0
  246. package/src/runtime/text-motion.d.ts +41 -0
  247. package/src/runtime/text-motion.js +165 -0
  248. package/src/runtime/text.d.ts +11 -0
  249. package/src/runtime/text.js +90 -0
  250. package/src/runtime/tilt.d.ts +19 -0
  251. package/src/runtime/tilt.js +116 -0
  252. package/src/runtime/topography.d.ts +71 -0
  253. package/src/runtime/topography.js +650 -0
  254. package/src/runtime/trackers.d.ts +21 -0
  255. package/src/runtime/trackers.js +76 -0
  256. package/src/runtime/variable-type.d.ts +43 -0
  257. package/src/runtime/variable-type.js +358 -0
  258. package/src/runtime/voxel-logo.d.ts +31 -0
  259. package/src/runtime/voxel-logo.js +237 -0
  260. package/src/runtime/waves.d.ts +70 -0
  261. package/src/runtime/waves.js +799 -0
  262. package/src/runtime/world-labels.d.ts +29 -0
  263. package/src/runtime/world-labels.js +59 -0
  264. package/src/runtime/world-land.d.ts +8 -0
  265. package/src/runtime/world-land.js +105 -0
  266. package/src/runtime/world-map.d.ts +69 -0
  267. package/src/runtime/world-map.js +308 -0
  268. package/src/runtime/world-plan.d.ts +101 -0
  269. package/src/runtime/world-plan.js +333 -0
  270. package/src/runtime/world-routes.d.ts +75 -0
  271. package/src/runtime/world-routes.js +673 -0
  272. package/src/styles/border-sweep.css +82 -0
  273. package/src/styles/glass.css +104 -0
  274. package/src/styles/motion.css +218 -0
  275. package/src/styles/reveal.css +51 -0
  276. package/src/vendor/paper/LICENSE +201 -0
  277. package/src/vendor/paper/NOTICE +5 -0
  278. package/src/vendor/paper/README.md +8 -0
  279. package/src/vendor/paper/get-shader-color-from-string.js +100 -0
  280. package/src/vendor/paper/get-shader-noise-texture.js +17 -0
  281. package/src/vendor/paper/presets.js +3678 -0
  282. package/src/vendor/paper/shader-sizing.js +37 -0
  283. package/src/vendor/paper/shader-utils.js +130 -0
  284. package/src/vendor/paper/shaders/color-panels.js +223 -0
  285. package/src/vendor/paper/shaders/dithering.js +270 -0
  286. package/src/vendor/paper/shaders/dot-grid.js +109 -0
  287. package/src/vendor/paper/shaders/dot-orbit.js +122 -0
  288. package/src/vendor/paper/shaders/fluted-glass.js +360 -0
  289. package/src/vendor/paper/shaders/gem-smoke.js +572 -0
  290. package/src/vendor/paper/shaders/god-rays.js +132 -0
  291. package/src/vendor/paper/shaders/grain-gradient.js +300 -0
  292. package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
  293. package/src/vendor/paper/shaders/halftone-dots.js +320 -0
  294. package/src/vendor/paper/shaders/heatmap.js +383 -0
  295. package/src/vendor/paper/shaders/image-dithering.js +198 -0
  296. package/src/vendor/paper/shaders/lens-distortion.js +275 -0
  297. package/src/vendor/paper/shaders/liquid-metal.js +632 -0
  298. package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
  299. package/src/vendor/paper/shaders/metaballs.js +110 -0
  300. package/src/vendor/paper/shaders/neuro-noise.js +76 -0
  301. package/src/vendor/paper/shaders/paper-texture.js +526 -0
  302. package/src/vendor/paper/shaders/perlin-noise.js +166 -0
  303. package/src/vendor/paper/shaders/pulsing-border.js +238 -0
  304. package/src/vendor/paper/shaders/simplex-noise.js +91 -0
  305. package/src/vendor/paper/shaders/smoke-ring.js +132 -0
  306. package/src/vendor/paper/shaders/spiral.js +82 -0
  307. package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
  308. package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
  309. package/src/vendor/paper/shaders/swirl.js +103 -0
  310. package/src/vendor/paper/shaders/voronoi.js +138 -0
  311. package/src/vendor/paper/shaders/warp.js +129 -0
  312. package/src/vendor/paper/shaders/water.js +121 -0
  313. package/src/vendor/paper/shaders/waves.js +65 -0
  314. package/src/vendor/paper/vertex-shader.js +159 -0
@@ -0,0 +1,463 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/iso-city/plan
3
+ *
4
+ * The city's layout, from a seed: a grid of streets with a few segments
5
+ * closed into pedestrian plazas (never leaving a dead end), and the blocks
6
+ * between them filled with buildings of varied heights and rooftops, or
7
+ * laid out as parks with paths, round trees and sometimes a pond. Lamps
8
+ * stand at the block corners.
9
+ *
10
+ * Pure data, in world units (y up, the city's streets on x and z from 0
11
+ * to `size × PITCH`): the scene module turns it into meshes, the roads
12
+ * module into lanes. The same seed and size always give the same city.
13
+ */
14
+
15
+ import { seededRandom } from '../random.js';
16
+
17
+ /** World units from one street's centre line to the next. */
18
+ export const PITCH = 4;
19
+ /** A street's width: two lanes. */
20
+ export const ROAD = 1;
21
+ /** The pavement's width round each block. */
22
+ export const CURB = 0.18;
23
+ /** The height of pavements and plazas above the road. */
24
+ export const SLAB = 0.045;
25
+ /** The height of a storey. */
26
+ export const STOREY = 0.2;
27
+
28
+ /**
29
+ * @typedef {{ x0: number, z0: number, x1: number, z1: number }} Rect
30
+ * @typedef {{ id: number, i: number, j: number, x: number, z: number }} Node
31
+ * @typedef {{ id: number, a: number, b: number, axis: 'x' | 'z' }} Edge
32
+ * @typedef {Rect & { y0: number, y1: number }} Part
33
+ * @typedef {{ kind: 'box' | 'hut', x0: number, z0: number, x1: number, z1: number, y0: number, y1: number }
34
+ * | { kind: 'tank', x: number, z: number, r: number, y0: number, y1: number }
35
+ * | { kind: 'garden', x0: number, z0: number, x1: number, z1: number, y0: number }
36
+ * | { kind: 'solar', x0: number, z0: number, x1: number, z1: number, y0: number, rows: number }} Roof
37
+ * @typedef {{ parts: Part[], roof: Roof[], tone: number, seed: number, glass: number }} Building
38
+ * @typedef {{ x: number, z: number, r: number, h: number, tone: number }} Tree
39
+ * @typedef {Rect & { kind: 'build' | 'park', plaza: Rect | null }} Block
40
+ * @typedef {{ x: number, z: number, r: number }} Pond
41
+ * @typedef {Rect & { axis: 'x' | 'z' }} Path
42
+ */
43
+
44
+ /**
45
+ * @typedef {object} CityPlan
46
+ * @property {number} size Blocks across.
47
+ * @property {number} extent The street grid's span, world units (size × PITCH).
48
+ * @property {Node[]} nodes The junctions, row by row: id = j × (size + 1) + i.
49
+ * @property {Edge[]} edges The street segments that are open.
50
+ * @property {Edge[]} closed The segments closed into plazas.
51
+ * @property {Block[]} blocks The blocks (two merged across a closed segment count as one).
52
+ * @property {Building[]} buildings Every building.
53
+ * @property {Tree[]} trees Every tree.
54
+ * @property {Pond[]} ponds The parks' ponds.
55
+ * @property {Path[]} paths The parks' paths.
56
+ * @property {{ x: number, z: number }[]} lamps The street lamps.
57
+ */
58
+
59
+ /**
60
+ * Plans a city.
61
+ *
62
+ * @param {{ seed?: number, size?: number }} [options] The seed (default 7)
63
+ * and the blocks across (default 6, 2–12).
64
+ * @returns {CityPlan} The plan.
65
+ */
66
+ export function planCity({ seed = 7, size = 6 } = {}) {
67
+ const n = Math.max(2, Math.min(12, Math.round(size)));
68
+ const random = seededRandom(seed * 7919 + n * 104729 + 1);
69
+ const extent = n * PITCH;
70
+ /**
71
+ * A random number in a range.
72
+ *
73
+ * @param {number} lo The least.
74
+ * @param {number} hi The most.
75
+ * @returns {number} Between them.
76
+ */
77
+ const range = (lo, hi) => lo + random() * (hi - lo);
78
+
79
+ /** @type {Node[]} */
80
+ const nodes = [];
81
+ for (let j = 0; j <= n; j++) for (let i = 0; i <= n; i++) nodes.push({ id: j * (n + 1) + i, i, j, x: i * PITCH, z: j * PITCH });
82
+ /**
83
+ * A junction's id.
84
+ *
85
+ * @param {number} i Column.
86
+ * @param {number} j Row.
87
+ * @returns {number} The id.
88
+ */
89
+ const at = (i, j) => j * (n + 1) + i;
90
+ /** @type {Edge[]} */
91
+ const all = [];
92
+ for (let j = 0; j <= n; j++) {
93
+ for (let i = 0; i <= n; i++) {
94
+ if (i < n) all.push({ id: all.length, a: at(i, j), b: at(i + 1, j), axis: 'x' });
95
+ if (j < n) all.push({ id: all.length, a: at(i, j), b: at(i, j + 1), axis: 'z' });
96
+ }
97
+ }
98
+
99
+ // Close a few inner segments into plazas: each merges the two blocks
100
+ // either side, a block merges once at most, and no junction is left
101
+ // with fewer than two streets or cut off from the rest.
102
+ const open = new Set(all.map((e) => e.id));
103
+ const degree = new Array(nodes.length).fill(0);
104
+ for (const e of all) {
105
+ degree[e.a]++;
106
+ degree[e.b]++;
107
+ }
108
+ /** Which blocks have merged, by `bi,bj`. */
109
+ const merged = new Set();
110
+ /** @type {Edge[]} */
111
+ const closed = [];
112
+ /**
113
+ * The two blocks either side of an inner segment.
114
+ *
115
+ * @param {Edge} e The segment.
116
+ * @returns {[string, string] | null} Their keys, or null for a segment on the edge of the grid.
117
+ */
118
+ const sides = (e) => {
119
+ const { i, j } = nodes[e.a];
120
+ if (e.axis === 'x') return j > 0 && j < n ? [`${i},${j - 1}`, `${i},${j}`] : null;
121
+ return i > 0 && i < n ? [`${i - 1},${j}`, `${i},${j}`] : null;
122
+ };
123
+ const order = shuffle(all.map((e) => e.id), random);
124
+ const want = Math.round(n * n * 0.09);
125
+ for (const id of order) {
126
+ if (closed.length >= want) break;
127
+ const e = all[id];
128
+ const pair = sides(e);
129
+ if (!pair || merged.has(pair[0]) || merged.has(pair[1]) || degree[e.a] < 3 || degree[e.b] < 3) continue;
130
+ open.delete(id);
131
+ if (!connected(nodes.length, all.filter((x) => open.has(x.id)))) {
132
+ open.add(id);
133
+ continue;
134
+ }
135
+ degree[e.a]--;
136
+ degree[e.b]--;
137
+ merged.add(pair[0]);
138
+ merged.add(pair[1]);
139
+ closed.push(e);
140
+ }
141
+ const edges = all.filter((e) => open.has(e.id));
142
+
143
+ // The blocks, two merged across each closed segment.
144
+ /** @type {Block[]} */
145
+ const blocks = [];
146
+ /**
147
+ * A block's bounds, inside the streets round it.
148
+ *
149
+ * @param {number} bi Column.
150
+ * @param {number} bj Row.
151
+ * @returns {Rect} Its bounds.
152
+ */
153
+ const bounds = (bi, bj) => ({ x0: bi * PITCH + ROAD / 2, z0: bj * PITCH + ROAD / 2, x1: (bi + 1) * PITCH - ROAD / 2, z1: (bj + 1) * PITCH - ROAD / 2 });
154
+ const centre = extent / 2;
155
+ for (const e of closed) {
156
+ const [a, b] = /** @type {[string, string]} */ (sides(e)).map((k) => bounds(...(/** @type {[number, number]} */ (k.split(',').map(Number)))));
157
+ const rect = { x0: Math.min(a.x0, b.x0), z0: Math.min(a.z0, b.z0), x1: Math.max(a.x1, b.x1), z1: Math.max(a.z1, b.z1) };
158
+ const plaza = e.axis === 'x' ? { x0: rect.x0, z0: a.z1, x1: rect.x1, z1: b.z0 } : { x0: a.x1, z0: rect.z0, x1: b.x0, z1: rect.z1 };
159
+ blocks.push({ ...rect, kind: random() < 0.3 ? 'park' : 'build', plaza });
160
+ }
161
+ for (let bj = 0; bj < n; bj++) {
162
+ for (let bi = 0; bi < n; bi++) {
163
+ if (merged.has(`${bi},${bj}`)) continue;
164
+ blocks.push({ ...bounds(bi, bj), kind: random() < 0.17 ? 'park' : 'build', plaza: null });
165
+ }
166
+ }
167
+ if (!blocks.some((b) => b.kind === 'park')) {
168
+ // At least one park: the block nearest the middle but one.
169
+ const sorted = [...blocks].sort((p, q) => dist(p, centre) - dist(q, centre));
170
+ (sorted[1] ?? sorted[0]).kind = 'park';
171
+ }
172
+
173
+ /** @type {Building[]} */
174
+ const buildings = [];
175
+ /** @type {Tree[]} */
176
+ const trees = [];
177
+ /** @type {Pond[]} */
178
+ const ponds = [];
179
+ /** @type {Path[]} */
180
+ const paths = [];
181
+ /** @type {{ x: number, z: number }[]} */
182
+ const lamps = [];
183
+
184
+ for (const block of blocks) {
185
+ // Lamps at the corners.
186
+ for (const [x, z] of [[block.x0, block.z0], [block.x1, block.z0], [block.x1, block.z1], [block.x0, block.z1]]) {
187
+ if (random() < 0.8) lamps.push({ x: x + (x === block.x0 ? 0.1 : -0.1), z: z + (z === block.z0 ? 0.1 : -0.1) });
188
+ }
189
+ const halves = block.plaza ? splitAround(block, block.plaza) : [block];
190
+ if (block.plaza) {
191
+ // A tree-lined plaza down the middle.
192
+ const p = block.plaza;
193
+ const along = p.x1 - p.x0 > p.z1 - p.z0;
194
+ const length = along ? p.x1 - p.x0 : p.z1 - p.z0;
195
+ const count = Math.floor(length / 0.75);
196
+ for (let k = 0; k < count; k++) {
197
+ const t = (k + 0.5) / count;
198
+ const x = along ? p.x0 + t * (p.x1 - p.x0) : (p.x0 + p.x1) / 2;
199
+ const z = along ? (p.z0 + p.z1) / 2 : p.z0 + t * (p.z1 - p.z0);
200
+ trees.push({ x, z, r: range(0.13, 0.17), h: SLAB, tone: random() });
201
+ }
202
+ }
203
+ for (const area of halves) {
204
+ const inner = { x0: area.x0 + CURB, z0: area.z0 + CURB, x1: area.x1 - CURB, z1: area.z1 - CURB };
205
+ if (block.kind === 'park') park(inner);
206
+ else for (const lot of lots(inner)) building(lot);
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Lays out a park: a path or two across it, trees, sometimes a pond.
212
+ *
213
+ * @param {Rect} r The park's grass.
214
+ */
215
+ function park(r) {
216
+ const cx = (r.x0 + r.x1) / 2;
217
+ const cz = (r.z0 + r.z1) / 2;
218
+ const w = 0.16;
219
+ const cross = random() < 0.6;
220
+ paths.push({ x0: r.x0, z0: cz - w / 2, x1: r.x1, z1: cz + w / 2, axis: 'x' });
221
+ if (cross) paths.push({ x0: cx - w / 2, z0: r.z0, x1: cx + w / 2, z1: r.z1, axis: 'z' });
222
+ /** @type {Pond | null} */
223
+ let pond = null;
224
+ if (random() < 0.45) {
225
+ const qx = random() < 0.5 ? -1 : 1;
226
+ const qz = cross ? (random() < 0.5 ? -1 : 1) : random() < 0.5 ? -1 : 1;
227
+ const rad = range(0.38, 0.5);
228
+ pond = { x: cx + qx * (r.x1 - r.x0) * 0.25, z: cz + qz * (r.z1 - r.z0) * 0.25, r: rad };
229
+ ponds.push(pond);
230
+ }
231
+ const step = 0.42;
232
+ for (let z = r.z0 + 0.22; z < r.z1 - 0.15; z += step) {
233
+ for (let x = r.x0 + 0.22; x < r.x1 - 0.15; x += step) {
234
+ const tx = x + range(-0.12, 0.12);
235
+ const tz = z + range(-0.12, 0.12);
236
+ if (Math.abs(tz - cz) < 0.26 || (cross && Math.abs(tx - cx) < 0.26)) continue;
237
+ if (pond && Math.hypot(tx - pond.x, tz - pond.z) < pond.r + 0.22) continue;
238
+ if (random() < 0.32) continue;
239
+ trees.push({ x: tx, z: tz, r: range(0.15, 0.24), h: SLAB + 0.01, tone: random() });
240
+ }
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Splits a block's ground into lots: one, two halves, three or four.
246
+ *
247
+ * @param {Rect} r The ground inside the pavement.
248
+ * @returns {Rect[]} The lots.
249
+ */
250
+ function lots(r) {
251
+ const mx = (r.x0 + r.x1) / 2 + range(-0.25, 0.25);
252
+ const mz = (r.z0 + r.z1) / 2 + range(-0.25, 0.25);
253
+ const pick = random();
254
+ if (pick < 0.22) return [r];
255
+ if (pick < 0.58) {
256
+ return random() < 0.5
257
+ ? [{ ...r, x1: mx }, { ...r, x0: mx }]
258
+ : [{ ...r, z1: mz }, { ...r, z0: mz }];
259
+ }
260
+ if (pick < 0.84) {
261
+ return random() < 0.5
262
+ ? [{ ...r, x1: mx }, { x0: mx, z0: r.z0, x1: r.x1, z1: mz }, { x0: mx, z0: mz, x1: r.x1, z1: r.z1 }]
263
+ : [{ ...r, z1: mz }, { x0: r.x0, z0: mz, x1: mx, z1: r.z1 }, { x0: mx, z0: mz, x1: r.x1, z1: r.z1 }];
264
+ }
265
+ return [
266
+ { x0: r.x0, z0: r.z0, x1: mx, z1: mz },
267
+ { x0: mx, z0: r.z0, x1: r.x1, z1: mz },
268
+ { x0: r.x0, z0: mz, x1: mx, z1: r.z1 },
269
+ { x0: mx, z0: mz, x1: r.x1, z1: r.z1 },
270
+ ];
271
+ }
272
+
273
+ /**
274
+ * Puts a building on a lot: a body, sometimes a set-back upper tier,
275
+ * and things on the roof.
276
+ *
277
+ * @param {Rect} lot The lot.
278
+ */
279
+ function building(lot) {
280
+ const gap = 0.05;
281
+ const r = { x0: lot.x0 + gap, z0: lot.z0 + gap, x1: lot.x1 - gap, z1: lot.z1 - gap };
282
+ const w = r.x1 - r.x0;
283
+ const d = r.z1 - r.z0;
284
+ const near = Math.max(0, 1 - Math.hypot((r.x0 + r.x1) / 2 - centre, (r.z0 + r.z1) / 2 - centre) / (extent * 0.6));
285
+ const tall = random() < 0.03 + near * 0.08 && w * d > 0.6;
286
+ const height = tall ? range(1.6, 2.4) : 0.36 + (0.35 + 0.65 * near) * random() ** 1.8 * 1.1;
287
+ const floors = Math.max(2, Math.round(height / STOREY));
288
+ const top = SLAB + floors * STOREY + 0.06;
289
+ /** @type {Part[]} */
290
+ const parts = [{ ...r, y0: SLAB, y1: top }];
291
+ if (floors >= 6 && random() < 0.55 && w > 0.7 && d > 0.7) {
292
+ const inset = () => range(0.1, Math.min(w, d) * 0.22);
293
+ const upper = Math.max(1, Math.round(range(1, 3)));
294
+ parts.push({ x0: r.x0 + inset(), z0: r.z0 + inset(), x1: r.x1 - inset(), z1: r.z1 - inset(), y0: top, y1: top + upper * STOREY + 0.04 });
295
+ }
296
+ const roofPart = parts[parts.length - 1];
297
+ buildings.push({ parts, roof: roofOf(roofPart, floors), tone: random() * 2 - 1, seed: random(), glass: random() });
298
+ }
299
+
300
+ /**
301
+ * Things on a roof, kept apart: units, a tank, a stair hut, a garden or solar panels.
302
+ *
303
+ * @param {Part} p The roof's part.
304
+ * @param {number} floors The building's storeys.
305
+ * @returns {Roof[]} What stands on it.
306
+ */
307
+ function roofOf(p, floors) {
308
+ /** @type {Roof[]} */
309
+ const out = [];
310
+ /** @type {Rect[]} */
311
+ const used = [];
312
+ const edge = 0.08;
313
+ const y = p.y1;
314
+ /**
315
+ * Finds room for a footprint on the roof.
316
+ *
317
+ * @param {number} w Its width.
318
+ * @param {number} d Its depth.
319
+ * @returns {Rect | null} Where it goes, or null when there is no room.
320
+ */
321
+ const place = (w, d) => {
322
+ for (let tries = 0; tries < 8; tries++) {
323
+ const x0 = range(p.x0 + edge, p.x1 - edge - w);
324
+ const z0 = range(p.z0 + edge, p.z1 - edge - d);
325
+ if (x0 < p.x0 + edge || z0 < p.z0 + edge) return null;
326
+ const rect = { x0, z0, x1: x0 + w, z1: z0 + d };
327
+ if (used.every((u) => rect.x1 + 0.03 < u.x0 || rect.x0 > u.x1 + 0.03 || rect.z1 + 0.03 < u.z0 || rect.z0 > u.z1 + 0.03)) {
328
+ used.push(rect);
329
+ return rect;
330
+ }
331
+ }
332
+ return null;
333
+ };
334
+ const low = floors <= 4;
335
+ const pick = random();
336
+ if (low && pick < 0.16) {
337
+ out.push({ kind: 'garden', x0: p.x0 + edge, z0: p.z0 + edge, x1: p.x1 - edge, z1: p.z1 - edge, y0: y });
338
+ return out;
339
+ }
340
+ if (low && pick < 0.32) {
341
+ const rect = place(Math.min(0.5, p.x1 - p.x0 - 0.2), Math.min(0.42, p.z1 - p.z0 - 0.2));
342
+ if (rect) out.push({ kind: 'solar', ...rect, y0: y, rows: 2 + Math.floor(random() * 2) });
343
+ }
344
+ if (random() < 0.45) {
345
+ const rect = place(range(0.2, 0.3), range(0.16, 0.24));
346
+ if (rect) out.push({ kind: 'hut', ...rect, y0: y, y1: y + 0.13 });
347
+ }
348
+ if (random() < 0.32) {
349
+ const r = range(0.06, 0.085);
350
+ const rect = place(r * 2, r * 2);
351
+ if (rect) out.push({ kind: 'tank', x: (rect.x0 + rect.x1) / 2, z: (rect.z0 + rect.z1) / 2, r, y0: y, y1: y + range(0.11, 0.16) });
352
+ }
353
+ const units = 1 + Math.floor(random() * 3);
354
+ for (let k = 0; k < units; k++) {
355
+ const rect = place(range(0.08, 0.15), range(0.08, 0.13));
356
+ if (rect) out.push({ kind: 'box', ...rect, y0: y, y1: y + range(0.045, 0.08) });
357
+ }
358
+ return out;
359
+ }
360
+
361
+ return { size: n, extent, nodes, edges, closed, blocks, buildings, trees, ponds, paths, lamps };
362
+ }
363
+
364
+ /**
365
+ * A block's distance from a point on the city's diagonal.
366
+ *
367
+ * @param {Rect} r The block.
368
+ * @param {number} c The point's x and z.
369
+ * @returns {number} The distance from the block's centre.
370
+ */
371
+ function dist(r, c) {
372
+ return Math.hypot((r.x0 + r.x1) / 2 - c, (r.z0 + r.z1) / 2 - c);
373
+ }
374
+
375
+ /**
376
+ * The two halves of a merged block either side of its plaza.
377
+ *
378
+ * @param {Rect} r The merged block.
379
+ * @param {Rect} p Its plaza.
380
+ * @returns {Rect[]} The halves.
381
+ */
382
+ function splitAround(r, p) {
383
+ return p.x1 - p.x0 >= p.z1 - p.z0
384
+ ? [{ ...r, z1: p.z0 }, { ...r, z0: p.z1 }]
385
+ : [{ ...r, x1: p.x0 }, { ...r, x0: p.x1 }];
386
+ }
387
+
388
+ /**
389
+ * Shuffles a list in place (Fisher–Yates), the same way for the same generator.
390
+ *
391
+ * @template T
392
+ * @param {T[]} list The list.
393
+ * @param {() => number} random The generator.
394
+ * @returns {T[]} The list.
395
+ */
396
+ function shuffle(list, random) {
397
+ for (let i = list.length - 1; i > 0; i--) {
398
+ const k = Math.floor(random() * (i + 1));
399
+ [list[i], list[k]] = [list[k], list[i]];
400
+ }
401
+ return list;
402
+ }
403
+
404
+ /**
405
+ * Whether every junction can reach every other along the open segments.
406
+ *
407
+ * @param {number} count How many junctions.
408
+ * @param {{ a: number, b: number }[]} edges The open segments.
409
+ * @returns {boolean} True when they are all connected.
410
+ */
411
+ export function connected(count, edges) {
412
+ /** @type {number[][]} */
413
+ const next = Array.from({ length: count }, () => []);
414
+ for (const e of edges) {
415
+ next[e.a].push(e.b);
416
+ next[e.b].push(e.a);
417
+ }
418
+ const seen = new Set([0]);
419
+ const queue = [0];
420
+ while (queue.length) {
421
+ for (const m of next[/** @type {number} */ (queue.pop())]) {
422
+ if (!seen.has(m)) {
423
+ seen.add(m);
424
+ queue.push(m);
425
+ }
426
+ }
427
+ }
428
+ return seen.size === count;
429
+ }
430
+
431
+ /**
432
+ * The first building a line of sight meets: where it enters the nearest
433
+ * part's box, walking from `from` towards `to`.
434
+ *
435
+ * @param {CityPlan} plan The plan.
436
+ * @param {[number, number, number]} from Where the line starts (towards the viewer).
437
+ * @param {[number, number, number]} to Where it ends (on the ground, say).
438
+ * @returns {[number, number, number] | null} The point it enters, or null when it meets none.
439
+ */
440
+ export function firstHit(plan, from, to) {
441
+ const d = [to[0] - from[0], to[1] - from[1], to[2] - from[2]];
442
+ let best = Infinity;
443
+ for (const b of plan.buildings) {
444
+ for (const p of b.parts) {
445
+ let near = 0;
446
+ let far = 1;
447
+ const lo = [p.x0, p.y0, p.z0];
448
+ const hi = [p.x1, p.y1, p.z1];
449
+ for (let i = 0; i < 3 && near <= far; i++) {
450
+ if (Math.abs(d[i]) < 1e-9) {
451
+ if (from[i] < lo[i] || from[i] > hi[i]) near = 2;
452
+ continue;
453
+ }
454
+ const t0 = (lo[i] - from[i]) / d[i];
455
+ const t1 = (hi[i] - from[i]) / d[i];
456
+ near = Math.max(near, Math.min(t0, t1));
457
+ far = Math.min(far, Math.max(t0, t1));
458
+ }
459
+ if (near <= far && near < best) best = near;
460
+ }
461
+ }
462
+ return best === Infinity ? null : [from[0] + d[0] * best, from[1] + d[1] * best, from[2] + d[2] * best];
463
+ }
@@ -0,0 +1,64 @@
1
+ /** A lane's centre, from the street's centre line. */
2
+ export const LANE: number;
3
+ /** Half a junction's width: lanes stop at its edge. */
4
+ export const BOX: number;
5
+ /** The least a pickup can be ahead of a car on its own lane and still be reached without going round. */
6
+ export const AHEAD: number;
7
+
8
+ /** One direction of a street segment, from the edge of one junction to the edge of the next. Headings are radians from +x towards +z. */
9
+ export interface Lane {
10
+ id: number;
11
+ edge: number;
12
+ from: number;
13
+ to: number;
14
+ dx: number;
15
+ dz: number;
16
+ heading: number;
17
+ /** Where its centre line starts. */
18
+ x: number;
19
+ z: number;
20
+ length: number;
21
+ /** The lane the other way on the same segment. */
22
+ reverse: number;
23
+ }
24
+
25
+ /** A way across a junction from one lane into another. */
26
+ export interface Turn {
27
+ kind: 'straight' | 'right' | 'left' | 'back';
28
+ node: number;
29
+ length: number;
30
+ at(t: number): { x: number; z: number; heading: number };
31
+ }
32
+
33
+ export interface Roads {
34
+ nodes: { x: number; z: number }[];
35
+ lanes: Lane[];
36
+ /** The lanes leaving each junction. */
37
+ out: number[][];
38
+ /** How many streets meet at each junction. */
39
+ degree: number[];
40
+ edges: { id: number; a: number; b: number; axis: 'x' | 'z' }[];
41
+ /** The turn from lane `a` into lane `b` (cached). */
42
+ turn(a: number, b: number): Turn;
43
+ }
44
+
45
+ /** Builds a lane each way (keeping right) on every open segment. */
46
+ export function buildRoads(plan: { nodes: { x: number; z: number }[]; edges: { id: number; a: number; b: number; axis: 'x' | 'z' }[] }): Roads;
47
+
48
+ /** The point `s` along a lane, `side` (default LANE, the lane's middle) right of the street's centre line. */
49
+ export function lanePoint(lane: Lane, s: number, side?: number): { x: number; z: number; heading: number };
50
+
51
+ /** The turn from lane `a` into lane `b` across junction `node`: straight, a tight right, a wide left, or back. */
52
+ export function makeTurn(node: { x: number; z: number }, a: Lane, b: Lane): Turn;
53
+
54
+ /** The shortest way by road (A* over lanes, no doubling back) between two points on lanes; null when there is none. */
55
+ export function findRoute(roads: Roads, from: { lane: number; s: number }, to: { lane: number; s: number }): { lanes: number[]; length: number } | null;
56
+
57
+ /** The lanes a car may take at the end of a lane: all but the way back, unless that is all there is. */
58
+ export function exits(roads: Roads, id: number): number[];
59
+
60
+ /** The street nearest a point, kept clear of junctions, as the lane whose kerb is on the point's side; null beyond `reach` (default 2.2). */
61
+ export function nearestStreet(roads: Roads, x: number, z: number, reach?: number): { lane: number; s: number; distance: number } | null;
62
+
63
+ /** Points along a route (no further apart than `step`, default 0.12), from `s` on its first lane to `end` on its last. */
64
+ export function routePoints(roads: Roads, route: number[], s: number, end: number, step?: number): { x: number; z: number }[];