@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,320 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/iso-city/roads
3
+ *
4
+ * The city's streets as something to drive: every open segment carries a
5
+ * lane each way (traffic keeps right), each lane runs from the edge of one
6
+ * junction to the edge of the next, and a turn joins a lane into one
7
+ * leaving the junction it ends at: straight across, a tight arc to the
8
+ * right, a wide one to the left. Routes are found with A* over lanes, so a
9
+ * car never reverses along its own street; it goes round the block.
10
+ *
11
+ * Headings are radians from +x towards +z. A lane's right is the kerb.
12
+ */
13
+
14
+ import { PITCH, ROAD } from './plan.js';
15
+
16
+ /** A lane's centre, from the street's centre line. */
17
+ export const LANE = 0.25;
18
+ /** Half a junction's width: lanes stop at its edge. */
19
+ export const BOX = ROAD / 2;
20
+ /** The least a pickup can be ahead of a car on its own lane and still be reached without going round. */
21
+ export const AHEAD = 0.6;
22
+
23
+ /**
24
+ * @typedef {object} Lane
25
+ * @property {number} id Its index.
26
+ * @property {number} edge The street segment it is on.
27
+ * @property {number} from The junction it leaves.
28
+ * @property {number} to The junction it runs to.
29
+ * @property {number} dx Its direction, x.
30
+ * @property {number} dz Its direction, z.
31
+ * @property {number} heading Its heading, radians.
32
+ * @property {number} x The x of its centre line where it starts.
33
+ * @property {number} z The z of its centre line where it starts.
34
+ * @property {number} length Its length.
35
+ * @property {number} reverse The lane the other way on the same segment.
36
+ */
37
+
38
+ /**
39
+ * @typedef {object} Turn
40
+ * @property {'straight' | 'right' | 'left' | 'back'} kind Which way it goes.
41
+ * @property {number} node The junction it crosses.
42
+ * @property {number} length Its length.
43
+ * @property {(t: number) => { x: number, z: number, heading: number }} at The point `t` along it.
44
+ */
45
+
46
+ /**
47
+ * @typedef {object} Roads
48
+ * @property {{ x: number, z: number }[]} nodes The junctions.
49
+ * @property {Lane[]} lanes Every lane.
50
+ * @property {number[][]} out The lanes leaving each junction.
51
+ * @property {number[]} degree How many streets meet at each junction.
52
+ * @property {{ id: number, a: number, b: number, axis: 'x' | 'z' }[]} edges The open segments.
53
+ * @property {(a: number, b: number) => Turn} turn The turn from lane `a` into lane `b` (cached).
54
+ */
55
+
56
+ /**
57
+ * Builds the lanes of a city's streets.
58
+ *
59
+ * @param {{ nodes: { x: number, z: number }[], edges: { id: number, a: number, b: number, axis: 'x' | 'z' }[] }} plan The plan's junctions and open segments.
60
+ * @returns {Roads} The roads.
61
+ */
62
+ export function buildRoads(plan) {
63
+ /** @type {Lane[]} */
64
+ const lanes = [];
65
+ /** @type {number[][]} */
66
+ const out = plan.nodes.map(() => []);
67
+ const degree = plan.nodes.map(() => 0);
68
+ for (const e of plan.edges) {
69
+ const first = lanes.length;
70
+ for (const [from, to] of [[e.a, e.b], [e.b, e.a]]) {
71
+ const a = plan.nodes[from];
72
+ const b = plan.nodes[to];
73
+ const length = Math.hypot(b.x - a.x, b.z - a.z);
74
+ const dx = (b.x - a.x) / length;
75
+ const dz = (b.z - a.z) / length;
76
+ const id = lanes.length;
77
+ lanes.push({
78
+ id,
79
+ edge: e.id,
80
+ from,
81
+ to,
82
+ dx,
83
+ dz,
84
+ heading: Math.atan2(dz, dx),
85
+ x: a.x + dx * BOX - dz * LANE,
86
+ z: a.z + dz * BOX + dx * LANE,
87
+ length: length - BOX * 2,
88
+ reverse: id === first ? first + 1 : first,
89
+ });
90
+ out[from].push(id);
91
+ }
92
+ degree[e.a]++;
93
+ degree[e.b]++;
94
+ }
95
+ /** @type {Map<number, Turn>} */
96
+ const turns = new Map();
97
+ return {
98
+ nodes: plan.nodes,
99
+ lanes,
100
+ out,
101
+ degree,
102
+ edges: plan.edges,
103
+ turn(a, b) {
104
+ const key = a * lanes.length + b;
105
+ let t = turns.get(key);
106
+ if (!t) {
107
+ t = makeTurn(plan.nodes[lanes[a].to], lanes[a], lanes[b]);
108
+ turns.set(key, t);
109
+ }
110
+ return t;
111
+ },
112
+ };
113
+ }
114
+
115
+ /**
116
+ * The point `s` along a lane, `side` from the street's centre line.
117
+ *
118
+ * @param {Lane} lane The lane.
119
+ * @param {number} s How far along, from its start.
120
+ * @param {number} [side=LANE] How far right of the centre line (LANE is the lane's middle).
121
+ * @returns {{ x: number, z: number, heading: number }} The point and the lane's heading.
122
+ */
123
+ export function lanePoint(lane, s, side = LANE) {
124
+ const off = side - LANE;
125
+ return { x: lane.x + lane.dx * s - lane.dz * off, z: lane.z + lane.dz * s + lane.dx * off, heading: lane.heading };
126
+ }
127
+
128
+ /**
129
+ * The turn from one lane into the next across the junction between them.
130
+ *
131
+ * @param {{ x: number, z: number }} node The junction.
132
+ * @param {Lane} a The lane in.
133
+ * @param {Lane} b The lane out.
134
+ * @returns {Turn} The turn.
135
+ */
136
+ export function makeTurn(node, a, b) {
137
+ const entry = { x: node.x - a.dx * BOX - a.dz * LANE, z: node.z - a.dz * BOX + a.dx * LANE };
138
+ const exit = { x: node.x + b.dx * BOX - b.dz * LANE, z: node.z + b.dz * BOX + b.dx * LANE };
139
+ const along = a.dx * b.dx + a.dz * b.dz;
140
+ const crossed = a.dx * b.dz - a.dz * b.dx;
141
+ if (along > 0.5) {
142
+ const length = Math.hypot(exit.x - entry.x, exit.z - entry.z);
143
+ return {
144
+ kind: 'straight',
145
+ node: a.to,
146
+ length,
147
+ at: (t) => ({ x: entry.x + a.dx * t, z: entry.z + a.dz * t, heading: a.heading }),
148
+ };
149
+ }
150
+ // An arc: right turns hug the near corner, left turns sweep wide, and a
151
+ // turn back (only where nothing else leaves) loops within the junction.
152
+ const kind = along < -0.5 ? 'back' : crossed > 0 ? 'right' : 'left';
153
+ const sign = kind === 'right' ? 1 : -1;
154
+ const radius = kind === 'right' ? BOX - LANE : kind === 'left' ? BOX + LANE : LANE;
155
+ const sweep = kind === 'back' ? Math.PI : Math.PI / 2;
156
+ const cx = entry.x + radius * Math.cos(a.heading + (sign * Math.PI) / 2);
157
+ const cz = entry.z + radius * Math.sin(a.heading + (sign * Math.PI) / 2);
158
+ const start = a.heading - (sign * Math.PI) / 2;
159
+ return {
160
+ kind,
161
+ node: a.to,
162
+ length: radius * sweep,
163
+ at(t) {
164
+ const turned = (sign * t) / radius;
165
+ return { x: cx + radius * Math.cos(start + turned), z: cz + radius * Math.sin(start + turned), heading: a.heading + turned };
166
+ },
167
+ };
168
+ }
169
+
170
+ /**
171
+ * The shortest way by road from a point on one lane to a point on another
172
+ * (A*, over lanes, never doubling back along a street unless nothing else
173
+ * leaves a junction).
174
+ *
175
+ * @param {Roads} roads The roads.
176
+ * @param {{ lane: number, s: number }} from Where it starts.
177
+ * @param {{ lane: number, s: number }} to Where it ends.
178
+ * @returns {{ lanes: number[], length: number } | null} The lanes in order
179
+ * (the first is `from`'s, the last `to`'s) and the distance; null when
180
+ * there is no way.
181
+ */
182
+ export function findRoute(roads, from, to) {
183
+ const { lanes } = roads;
184
+ if (from.lane === to.lane && to.s >= from.s + AHEAD) return { lanes: [from.lane], length: to.s - from.s };
185
+ const goal = lanes[to.lane];
186
+ /**
187
+ * A lower bound on the distance from a lane's start to the goal.
188
+ *
189
+ * @param {number} id The lane.
190
+ * @returns {number} The straight-line distance to the goal point.
191
+ */
192
+ const guess = (id) => {
193
+ const p = lanes[id];
194
+ const g = lanePoint(goal, to.s);
195
+ return Math.hypot(g.x - p.x, g.z - p.z);
196
+ };
197
+ /** Best known distance to each lane's start, and the lane before it. */
198
+ const best = new Map();
199
+ const prev = new Map();
200
+ /** @type {{ id: number, g: number, f: number }[]} */
201
+ const open = [];
202
+ const startLeft = lanes[from.lane].length - from.s;
203
+ /**
204
+ * Offers a lane's start at a distance, keeping it if it is the best yet.
205
+ *
206
+ * @param {number} id The lane.
207
+ * @param {number} g The distance to its start.
208
+ * @param {number} before The lane it was reached from.
209
+ */
210
+ const offer = (id, g, before) => {
211
+ if (best.has(id) && best.get(id) <= g) return;
212
+ best.set(id, g);
213
+ prev.set(id, before);
214
+ open.push({ id, g, f: g + guess(id) });
215
+ };
216
+ for (const id of exits(roads, from.lane)) offer(id, startLeft + roads.turn(from.lane, id).length, -1);
217
+ while (open.length) {
218
+ let k = 0;
219
+ for (let i = 1; i < open.length; i++) if (open[i].f < open[k].f) k = i;
220
+ const { id, g } = open.splice(k, 1)[0];
221
+ if (g > best.get(id)) continue;
222
+ if (id === to.lane) {
223
+ const path = [id];
224
+ let at = id;
225
+ while (prev.get(at) !== -1) {
226
+ at = prev.get(at);
227
+ path.unshift(at);
228
+ }
229
+ path.unshift(from.lane);
230
+ return { lanes: path, length: g + to.s };
231
+ }
232
+ for (const next of exits(roads, id)) offer(next, g + lanes[id].length + roads.turn(id, next).length, id);
233
+ }
234
+ return null;
235
+ }
236
+
237
+ /**
238
+ * The lanes a car on a lane may turn into at its end: every one leaving
239
+ * that junction but the way back, unless the way back is all there is.
240
+ *
241
+ * @param {Roads} roads The roads.
242
+ * @param {number} id The lane.
243
+ * @returns {number[]} The lanes.
244
+ */
245
+ export function exits(roads, id) {
246
+ const lane = roads.lanes[id];
247
+ const all = roads.out[lane.to];
248
+ const onward = all.filter((m) => m !== lane.reverse);
249
+ return onward.length ? onward : all;
250
+ }
251
+
252
+ /**
253
+ * The street nearest a point on the ground: where along it, and the lane
254
+ * whose kerb is on the point's side.
255
+ *
256
+ * @param {Roads} roads The roads.
257
+ * @param {number} x The point's x.
258
+ * @param {number} z The point's z.
259
+ * @param {number} [reach=2.2] How far from a street's centre line still counts.
260
+ * @returns {{ lane: number, s: number, distance: number } | null} The lane and how far
261
+ * along it, kept clear of the junctions; null when no street is within reach.
262
+ */
263
+ export function nearestStreet(roads, x, z, reach = 2.2) {
264
+ const margin = BOX + 0.4;
265
+ /** @type {{ lane: number, s: number, distance: number } | null} */
266
+ let found = null;
267
+ for (const lane of roads.lanes) {
268
+ // Each segment is looked at once, from the lane that runs towards +x or +z.
269
+ if (lane.dx + lane.dz < 0) continue;
270
+ const a = roads.nodes[lane.from];
271
+ const along = (x - a.x) * lane.dx + (z - a.z) * lane.dz;
272
+ const t = Math.min(PITCH - margin, Math.max(margin, along));
273
+ const px = a.x + lane.dx * t;
274
+ const pz = a.z + lane.dz * t;
275
+ const distance = Math.hypot(x - px, z - pz);
276
+ if (distance > reach || (found && distance >= found.distance)) continue;
277
+ // Right of this lane (its kerb) is +z for a lane along x, −x for one along z.
278
+ const right = (x - px) * -lane.dz + (z - pz) * lane.dx;
279
+ found = right >= 0 ? { lane: lane.id, s: t - BOX, distance } : { lane: lane.reverse, s: PITCH - t - BOX, distance };
280
+ }
281
+ return found;
282
+ }
283
+
284
+ /**
285
+ * Points along a route, for drawing it: from `s` on its first lane, through
286
+ * each turn, to `end` on its last.
287
+ *
288
+ * @param {Roads} roads The roads.
289
+ * @param {number[]} route The lanes.
290
+ * @param {number} s Where it starts on the first.
291
+ * @param {number} end Where it ends on the last.
292
+ * @param {number} [step=0.12] The most between two points.
293
+ * @returns {{ x: number, z: number }[]} The points.
294
+ */
295
+ export function routePoints(roads, route, s, end, step = 0.12) {
296
+ /** @type {{ x: number, z: number }[]} */
297
+ const points = [];
298
+ /**
299
+ * Adds points along a stretch.
300
+ *
301
+ * @param {(t: number) => { x: number, z: number }} at The point `t` along it.
302
+ * @param {number} from Where to start.
303
+ * @param {number} to Where to stop.
304
+ */
305
+ const walk = (at, from, to) => {
306
+ const count = Math.max(1, Math.ceil((to - from) / step));
307
+ for (let i = points.length ? 1 : 0; i <= count; i++) {
308
+ const p = at(from + ((to - from) * i) / count);
309
+ points.push({ x: p.x, z: p.z });
310
+ }
311
+ };
312
+ route.forEach((id, i) => {
313
+ const lane = roads.lanes[id];
314
+ const from = i === 0 ? s : 0;
315
+ const to = i === route.length - 1 ? end : lane.length;
316
+ if (to > from) walk((t) => lanePoint(lane, t), from, to);
317
+ if (i < route.length - 1) walk(roads.turn(id, route[i + 1]).at, 0, roads.turn(id, route[i + 1]).length);
318
+ });
319
+ return points;
320
+ }
@@ -0,0 +1,27 @@
1
+ import type { Vec2, Vec3 } from '../mesh.js';
2
+ import type { CityPlan, Rect } from './plan.js';
3
+ import type { Look } from './look.js';
4
+
5
+ /** The materials, as the shader reads them from a vertex's tag. */
6
+ export const MAT: Readonly<{
7
+ plain: number; wall: number; glass: number; body: number; head: number; tail: number; sign: number; accent: number;
8
+ lamp: number; water: number; route: number; blob: number; glow: number; beam: number; ring: number;
9
+ }>;
10
+
11
+ /** A rectangle with rounded corners, as a polygon (x, z); `steps` segments a corner (default 4). */
12
+ export function roundedRect(r: Rect, radius: number, steps?: number): Vec2[];
13
+
14
+ /** The static city as one opaque mesh, and the lamps' pools of light. */
15
+ export function cityMesh(plan: CityPlan, look: Look): { solid: Float32Array; glow: Float32Array };
16
+
17
+ /** The car in its own frame (x forward, 0.44 long): the body, and what it lays on the road (shadow, beams). */
18
+ export function carMesh(look: Look): { body: Float32Array; effects: Float32Array };
19
+
20
+ /** A person, standing. */
21
+ export function personMesh(look: Look): Float32Array;
22
+
23
+ /** The pin (its white dot turned towards `eye`) and its ring. */
24
+ export function pinMesh(eye: Vec3): { body: Float32Array; ring: Float32Array };
25
+
26
+ /** A ribbon along a route (default 0.09 wide, 0.014 up): uv x the distance along, y −1 to 1 across. */
27
+ export function ribbonMesh(points: { x: number; z: number }[], width?: number, y?: number): Float32Array;