webglVectorRenderer.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. import { getTileSizePx, type CameraState } from '../camera/camera'
  2. import { worldTileToLonLat } from '../../utils/projection'
  3. import { type MapScene } from './mapRenderer'
  4. import { CourseLayer, type ProjectedCourseLayers, type ProjectedCourseLeg } from '../layer/courseLayer'
  5. import { TrackLayer } from '../layer/trackLayer'
  6. import { GpsLayer } from '../layer/gpsLayer'
  7. const COURSE_COLOR: [number, number, number, number] = [0.8, 0.0, 0.42, 0.96]
  8. const COMPLETED_ROUTE_COLOR: [number, number, number, number] = [0.48, 0.5, 0.54, 0.82]
  9. const ACTIVE_CONTROL_COLOR: [number, number, number, number] = [0.22, 1, 0.95, 1]
  10. const MULTI_ACTIVE_CONTROL_COLOR: [number, number, number, number] = [1, 0.8, 0.2, 0.98]
  11. const FOCUSED_CONTROL_COLOR: [number, number, number, number] = [0.98, 0.96, 0.98, 1]
  12. const MULTI_ACTIVE_PULSE_COLOR: [number, number, number, number] = [0.18, 1, 0.96, 0.86]
  13. const FOCUSED_PULSE_COLOR: [number, number, number, number] = [1, 0.36, 0.84, 0.88]
  14. const ACTIVE_LEG_COLOR: [number, number, number, number] = [0.18, 1, 0.94, 0.5]
  15. const EARTH_CIRCUMFERENCE_METERS = 40075016.686
  16. const CONTROL_RING_WIDTH_RATIO = 0.2
  17. const FINISH_INNER_RADIUS_RATIO = 0.6
  18. const FINISH_RING_WIDTH_RATIO = 0.2
  19. const START_RING_WIDTH_RATIO = 0.2
  20. const LEG_WIDTH_RATIO = 0.2
  21. const LEG_TRIM_TO_RING_CENTER_RATIO = 1 - CONTROL_RING_WIDTH_RATIO / 2
  22. const ACTIVE_CONTROL_PULSE_SPEED = 0.18
  23. const ACTIVE_CONTROL_PULSE_MIN_SCALE = 1.12
  24. const ACTIVE_CONTROL_PULSE_MAX_SCALE = 1.46
  25. const ACTIVE_CONTROL_PULSE_WIDTH_RATIO = 0.12
  26. const GUIDE_FLOW_COUNT = 5
  27. const GUIDE_FLOW_SPEED = 0.02
  28. const GUIDE_FLOW_TRAIL = 0.16
  29. const GUIDE_FLOW_MIN_WIDTH_RATIO = 0.12
  30. const GUIDE_FLOW_MAX_WIDTH_RATIO = 0.22
  31. const GUIDE_FLOW_HEAD_RADIUS_RATIO = 0.18
  32. type RgbaColor = [number, number, number, number]
  33. function createShader(gl: any, type: number, source: string): any {
  34. const shader = gl.createShader(type)
  35. if (!shader) {
  36. throw new Error('WebGL shader 创建失败')
  37. }
  38. gl.shaderSource(shader, source)
  39. gl.compileShader(shader)
  40. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  41. const message = gl.getShaderInfoLog(shader) || 'unknown shader error'
  42. gl.deleteShader(shader)
  43. throw new Error(message)
  44. }
  45. return shader
  46. }
  47. function createProgram(gl: any, vertexSource: string, fragmentSource: string): any {
  48. const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource)
  49. const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource)
  50. const program = gl.createProgram()
  51. if (!program) {
  52. throw new Error('WebGL program 创建失败')
  53. }
  54. gl.attachShader(program, vertexShader)
  55. gl.attachShader(program, fragmentShader)
  56. gl.linkProgram(program)
  57. gl.deleteShader(vertexShader)
  58. gl.deleteShader(fragmentShader)
  59. if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
  60. const message = gl.getProgramInfoLog(program) || 'unknown program error'
  61. gl.deleteProgram(program)
  62. throw new Error(message)
  63. }
  64. return program
  65. }
  66. export class WebGLVectorRenderer {
  67. canvas: any
  68. gl: any
  69. dpr: number
  70. courseLayer: CourseLayer
  71. trackLayer: TrackLayer
  72. gpsLayer: GpsLayer
  73. program: any
  74. positionBuffer: any
  75. colorBuffer: any
  76. positionLocation: number
  77. colorLocation: number
  78. constructor(courseLayer: CourseLayer, trackLayer: TrackLayer, gpsLayer: GpsLayer) {
  79. this.canvas = null
  80. this.gl = null
  81. this.dpr = 1
  82. this.courseLayer = courseLayer
  83. this.trackLayer = trackLayer
  84. this.gpsLayer = gpsLayer
  85. this.program = null
  86. this.positionBuffer = null
  87. this.colorBuffer = null
  88. this.positionLocation = -1
  89. this.colorLocation = -1
  90. }
  91. attachCanvas(canvasNode: any, width: number, height: number, dpr: number): void {
  92. this.canvas = canvasNode
  93. this.dpr = dpr || 1
  94. canvasNode.width = Math.max(1, Math.floor(width * this.dpr))
  95. canvasNode.height = Math.max(1, Math.floor(height * this.dpr))
  96. this.attachContext(canvasNode.getContext('webgl') || canvasNode.getContext('experimental-webgl'), canvasNode)
  97. }
  98. attachContext(gl: any, canvasNode: any): void {
  99. if (!gl) {
  100. throw new Error('当前环境不支持 WebGL Vector Layer')
  101. }
  102. this.canvas = canvasNode
  103. this.gl = gl
  104. this.program = createProgram(
  105. gl,
  106. 'attribute vec2 a_position; attribute vec4 a_color; varying vec4 v_color; void main() { gl_Position = vec4(a_position, 0.0, 1.0); v_color = a_color; }',
  107. 'precision mediump float; varying vec4 v_color; void main() { gl_FragColor = v_color; }',
  108. )
  109. this.positionBuffer = gl.createBuffer()
  110. this.colorBuffer = gl.createBuffer()
  111. this.positionLocation = gl.getAttribLocation(this.program, 'a_position')
  112. this.colorLocation = gl.getAttribLocation(this.program, 'a_color')
  113. gl.viewport(0, 0, canvasNode.width, canvasNode.height)
  114. gl.enable(gl.BLEND)
  115. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
  116. }
  117. destroy(): void {
  118. if (this.gl) {
  119. if (this.program) {
  120. this.gl.deleteProgram(this.program)
  121. }
  122. if (this.positionBuffer) {
  123. this.gl.deleteBuffer(this.positionBuffer)
  124. }
  125. if (this.colorBuffer) {
  126. this.gl.deleteBuffer(this.colorBuffer)
  127. }
  128. }
  129. this.program = null
  130. this.positionBuffer = null
  131. this.colorBuffer = null
  132. this.gl = null
  133. this.canvas = null
  134. }
  135. render(scene: MapScene, pulseFrame: number): void {
  136. if (!this.gl || !this.program || !this.positionBuffer || !this.colorBuffer || !this.canvas) {
  137. return
  138. }
  139. const gl = this.gl
  140. const course = this.courseLayer.projectCourse(scene)
  141. const trackPoints = this.trackLayer.projectPoints(scene)
  142. const gpsPoint = this.gpsLayer.projectPoint(scene)
  143. const positions: number[] = []
  144. const colors: number[] = []
  145. if (course) {
  146. this.pushCourse(positions, colors, course, scene, pulseFrame)
  147. }
  148. for (let index = 1; index < trackPoints.length; index += 1) {
  149. this.pushSegment(positions, colors, trackPoints[index - 1], trackPoints[index], 6, [0.09, 0.43, 0.36, 0.96], scene)
  150. }
  151. if (gpsPoint) {
  152. this.pushCircle(positions, colors, gpsPoint.x, gpsPoint.y, this.gpsLayer.getPulseRadius(pulseFrame), [0.13, 0.62, 0.74, 0.22], scene)
  153. this.pushCircle(positions, colors, gpsPoint.x, gpsPoint.y, 13, [1, 1, 1, 0.95], scene)
  154. this.pushCircle(positions, colors, gpsPoint.x, gpsPoint.y, 9, [0.13, 0.63, 0.74, 1], scene)
  155. }
  156. if (!positions.length) {
  157. return
  158. }
  159. gl.viewport(0, 0, this.canvas.width, this.canvas.height)
  160. gl.useProgram(this.program)
  161. gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer)
  162. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STREAM_DRAW)
  163. gl.enableVertexAttribArray(this.positionLocation)
  164. gl.vertexAttribPointer(this.positionLocation, 2, gl.FLOAT, false, 0, 0)
  165. gl.bindBuffer(gl.ARRAY_BUFFER, this.colorBuffer)
  166. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STREAM_DRAW)
  167. gl.enableVertexAttribArray(this.colorLocation)
  168. gl.vertexAttribPointer(this.colorLocation, 4, gl.FLOAT, false, 0, 0)
  169. gl.drawArrays(gl.TRIANGLES, 0, positions.length / 2)
  170. }
  171. getPixelsPerMeter(scene: MapScene): number {
  172. const camera: CameraState = {
  173. centerWorldX: scene.exactCenterWorldX,
  174. centerWorldY: scene.exactCenterWorldY,
  175. viewportWidth: scene.viewportWidth,
  176. viewportHeight: scene.viewportHeight,
  177. visibleColumns: scene.visibleColumns,
  178. }
  179. const tileSizePx = getTileSizePx(camera)
  180. const centerLonLat = worldTileToLonLat({ x: scene.exactCenterWorldX, y: scene.exactCenterWorldY }, scene.zoom)
  181. const metersPerTile = Math.cos(centerLonLat.lat * Math.PI / 180) * EARTH_CIRCUMFERENCE_METERS / Math.pow(2, scene.zoom)
  182. if (!tileSizePx || !metersPerTile) {
  183. return 0
  184. }
  185. return tileSizePx / metersPerTile
  186. }
  187. getMetric(scene: MapScene, meters: number): number {
  188. return meters * this.getPixelsPerMeter(scene)
  189. }
  190. getControlRadiusMeters(scene: MapScene): number {
  191. return scene.cpRadiusMeters > 0 ? scene.cpRadiusMeters : 5
  192. }
  193. pushCourse(
  194. positions: number[],
  195. colors: number[],
  196. course: ProjectedCourseLayers,
  197. scene: MapScene,
  198. pulseFrame: number,
  199. ): void {
  200. const controlRadiusMeters = this.getControlRadiusMeters(scene)
  201. if (scene.revealFullCourse && scene.showCourseLegs) {
  202. for (let index = 0; index < course.legs.length; index += 1) {
  203. const leg = course.legs[index]
  204. this.pushCourseLeg(positions, colors, leg, controlRadiusMeters, this.getLegColor(scene, index), scene)
  205. if (scene.guidanceLegAnimationEnabled && scene.activeLegIndices.includes(index)) {
  206. this.pushCourseLegHighlight(positions, colors, leg, controlRadiusMeters, scene)
  207. }
  208. }
  209. const guideLeg = this.getGuideLeg(course, scene)
  210. if (guideLeg) {
  211. this.pushGuidanceFlow(positions, colors, guideLeg, controlRadiusMeters, scene, pulseFrame)
  212. }
  213. }
  214. for (const start of course.starts) {
  215. if (scene.activeStart) {
  216. this.pushActiveStartPulse(positions, colors, start.point.x, start.point.y, start.headingDeg, controlRadiusMeters, scene, pulseFrame)
  217. }
  218. this.pushStartTriangle(positions, colors, start.point.x, start.point.y, start.headingDeg, controlRadiusMeters, this.getStartColor(scene), scene)
  219. }
  220. if (!scene.revealFullCourse) {
  221. return
  222. }
  223. for (const control of course.controls) {
  224. if (scene.activeControlSequences.includes(control.sequence)) {
  225. if (scene.controlVisualMode === 'single-target') {
  226. this.pushActiveControlPulse(positions, colors, control.point.x, control.point.y, controlRadiusMeters, scene, pulseFrame)
  227. } else {
  228. this.pushActiveControlPulse(positions, colors, control.point.x, control.point.y, controlRadiusMeters, scene, pulseFrame, MULTI_ACTIVE_PULSE_COLOR)
  229. this.pushActiveControlPulse(positions, colors, control.point.x, control.point.y, controlRadiusMeters * 1.2, scene, pulseFrame + 9, [0.9, 1, 1, 0.52])
  230. }
  231. }
  232. this.pushRing(
  233. positions,
  234. colors,
  235. control.point.x,
  236. control.point.y,
  237. this.getMetric(scene, controlRadiusMeters),
  238. this.getMetric(scene, controlRadiusMeters * (1 - CONTROL_RING_WIDTH_RATIO)),
  239. this.getControlColor(scene, control.sequence),
  240. scene,
  241. )
  242. if (scene.focusedControlSequences.includes(control.sequence)) {
  243. this.pushActiveControlPulse(positions, colors, control.point.x, control.point.y, controlRadiusMeters * 1.02, scene, pulseFrame, FOCUSED_PULSE_COLOR)
  244. this.pushActiveControlPulse(positions, colors, control.point.x, control.point.y, controlRadiusMeters * 1.32, scene, pulseFrame + 15, [1, 0.86, 0.94, 0.5])
  245. this.pushRing(
  246. positions,
  247. colors,
  248. control.point.x,
  249. control.point.y,
  250. this.getMetric(scene, controlRadiusMeters * 1.24),
  251. this.getMetric(scene, controlRadiusMeters * 1.06),
  252. FOCUSED_CONTROL_COLOR,
  253. scene,
  254. )
  255. }
  256. }
  257. for (const finish of course.finishes) {
  258. if (scene.activeFinish) {
  259. this.pushActiveControlPulse(positions, colors, finish.point.x, finish.point.y, controlRadiusMeters, scene, pulseFrame)
  260. }
  261. if (scene.focusedFinish) {
  262. this.pushActiveControlPulse(positions, colors, finish.point.x, finish.point.y, controlRadiusMeters * 1.04, scene, pulseFrame, FOCUSED_PULSE_COLOR)
  263. this.pushActiveControlPulse(positions, colors, finish.point.x, finish.point.y, controlRadiusMeters * 1.34, scene, pulseFrame + 12, [1, 0.86, 0.94, 0.46])
  264. }
  265. const finishColor = this.getFinishColor(scene)
  266. this.pushRing(
  267. positions,
  268. colors,
  269. finish.point.x,
  270. finish.point.y,
  271. this.getMetric(scene, controlRadiusMeters),
  272. this.getMetric(scene, controlRadiusMeters * (1 - FINISH_RING_WIDTH_RATIO)),
  273. finishColor,
  274. scene,
  275. )
  276. this.pushRing(
  277. positions,
  278. colors,
  279. finish.point.x,
  280. finish.point.y,
  281. this.getMetric(scene, controlRadiusMeters * FINISH_INNER_RADIUS_RATIO),
  282. this.getMetric(scene, controlRadiusMeters * FINISH_INNER_RADIUS_RATIO * (1 - FINISH_RING_WIDTH_RATIO / FINISH_INNER_RADIUS_RATIO)),
  283. finishColor,
  284. scene,
  285. )
  286. }
  287. }
  288. getGuideLeg(course: ProjectedCourseLayers, scene: MapScene): ProjectedCourseLeg | null {
  289. if (!scene.guidanceLegAnimationEnabled) {
  290. return null
  291. }
  292. const activeIndex = scene.activeLegIndices.length ? scene.activeLegIndices[0] : -1
  293. if (activeIndex >= 0 && activeIndex < course.legs.length) {
  294. return course.legs[activeIndex]
  295. }
  296. return null
  297. }
  298. getLegColor(scene: MapScene, index: number): RgbaColor {
  299. return this.isCompletedLeg(scene, index) ? COMPLETED_ROUTE_COLOR : COURSE_COLOR
  300. }
  301. isCompletedLeg(scene: MapScene, index: number): boolean {
  302. return scene.completedLegIndices.includes(index)
  303. }
  304. pushCourseLeg(
  305. positions: number[],
  306. colors: number[],
  307. leg: ProjectedCourseLeg,
  308. controlRadiusMeters: number,
  309. color: RgbaColor,
  310. scene: MapScene,
  311. ): void {
  312. const trimmed = this.getTrimmedCourseLeg(leg, controlRadiusMeters, scene)
  313. if (!trimmed) {
  314. return
  315. }
  316. this.pushSegment(positions, colors, trimmed.from, trimmed.to, this.getMetric(scene, controlRadiusMeters * LEG_WIDTH_RATIO), color, scene)
  317. }
  318. pushCourseLegHighlight(
  319. positions: number[],
  320. colors: number[],
  321. leg: ProjectedCourseLeg,
  322. controlRadiusMeters: number,
  323. scene: MapScene,
  324. ): void {
  325. const trimmed = this.getTrimmedCourseLeg(leg, controlRadiusMeters, scene)
  326. if (!trimmed) {
  327. return
  328. }
  329. this.pushSegment(
  330. positions,
  331. colors,
  332. trimmed.from,
  333. trimmed.to,
  334. this.getMetric(scene, controlRadiusMeters * LEG_WIDTH_RATIO * 1.5),
  335. ACTIVE_LEG_COLOR,
  336. scene,
  337. )
  338. }
  339. pushActiveControlPulse(
  340. positions: number[],
  341. colors: number[],
  342. centerX: number,
  343. centerY: number,
  344. controlRadiusMeters: number,
  345. scene: MapScene,
  346. pulseFrame: number,
  347. pulseColor?: RgbaColor,
  348. ): void {
  349. const pulse = (Math.sin(pulseFrame * ACTIVE_CONTROL_PULSE_SPEED) + 1) / 2
  350. const pulseScale = ACTIVE_CONTROL_PULSE_MIN_SCALE + (ACTIVE_CONTROL_PULSE_MAX_SCALE - ACTIVE_CONTROL_PULSE_MIN_SCALE) * pulse
  351. const pulseWidthScale = pulseScale - ACTIVE_CONTROL_PULSE_WIDTH_RATIO
  352. const baseColor = pulseColor || ACTIVE_CONTROL_COLOR
  353. const glowAlpha = Math.min(1, baseColor[3] * (0.46 + pulse * 0.5))
  354. const glowColor: RgbaColor = [baseColor[0], baseColor[1], baseColor[2], glowAlpha]
  355. this.pushRing(
  356. positions,
  357. colors,
  358. centerX,
  359. centerY,
  360. this.getMetric(scene, controlRadiusMeters * pulseScale),
  361. this.getMetric(scene, controlRadiusMeters * Math.max(1, pulseWidthScale)),
  362. glowColor,
  363. scene,
  364. )
  365. }
  366. pushActiveStartPulse(
  367. positions: number[],
  368. colors: number[],
  369. centerX: number,
  370. centerY: number,
  371. headingDeg: number | null,
  372. controlRadiusMeters: number,
  373. scene: MapScene,
  374. pulseFrame: number,
  375. ): void {
  376. const pulse = (Math.sin(pulseFrame * ACTIVE_CONTROL_PULSE_SPEED) + 1) / 2
  377. const pulseScale = ACTIVE_CONTROL_PULSE_MIN_SCALE + (ACTIVE_CONTROL_PULSE_MAX_SCALE - ACTIVE_CONTROL_PULSE_MIN_SCALE) * pulse
  378. const pulseWidthScale = pulseScale - ACTIVE_CONTROL_PULSE_WIDTH_RATIO
  379. const glowAlpha = 0.24 + pulse * 0.34
  380. const glowColor: RgbaColor = [0.36, 1, 0.96, glowAlpha]
  381. const headingRad = ((headingDeg === null ? 0 : headingDeg) - 90) * Math.PI / 180
  382. const ringCenterX = centerX + Math.cos(headingRad) * this.getMetric(scene, controlRadiusMeters * 0.04)
  383. const ringCenterY = centerY + Math.sin(headingRad) * this.getMetric(scene, controlRadiusMeters * 0.04)
  384. this.pushRing(
  385. positions,
  386. colors,
  387. ringCenterX,
  388. ringCenterY,
  389. this.getMetric(scene, controlRadiusMeters * pulseScale),
  390. this.getMetric(scene, controlRadiusMeters * Math.max(1, pulseWidthScale)),
  391. glowColor,
  392. scene,
  393. )
  394. }
  395. getStartColor(scene: MapScene): RgbaColor {
  396. if (scene.activeStart) {
  397. return ACTIVE_CONTROL_COLOR
  398. }
  399. if (scene.completedStart) {
  400. return COMPLETED_ROUTE_COLOR
  401. }
  402. return COURSE_COLOR
  403. }
  404. getControlColor(scene: MapScene, sequence: number): RgbaColor {
  405. if (scene.activeControlSequences.includes(sequence)) {
  406. return scene.controlVisualMode === 'multi-target' ? MULTI_ACTIVE_CONTROL_COLOR : ACTIVE_CONTROL_COLOR
  407. }
  408. if (scene.completedControlSequences.includes(sequence)) {
  409. return COMPLETED_ROUTE_COLOR
  410. }
  411. return COURSE_COLOR
  412. }
  413. getFinishColor(scene: MapScene): RgbaColor {
  414. if (scene.focusedFinish) {
  415. return FOCUSED_CONTROL_COLOR
  416. }
  417. if (scene.activeFinish) {
  418. return ACTIVE_CONTROL_COLOR
  419. }
  420. if (scene.completedFinish) {
  421. return COMPLETED_ROUTE_COLOR
  422. }
  423. return COURSE_COLOR
  424. }
  425. pushGuidanceFlow(
  426. positions: number[],
  427. colors: number[],
  428. leg: ProjectedCourseLeg,
  429. controlRadiusMeters: number,
  430. scene: MapScene,
  431. pulseFrame: number,
  432. ): void {
  433. const trimmed = this.getTrimmedCourseLeg(leg, controlRadiusMeters, scene)
  434. if (!trimmed) {
  435. return
  436. }
  437. const dx = trimmed.to.x - trimmed.from.x
  438. const dy = trimmed.to.y - trimmed.from.y
  439. const length = Math.sqrt(dx * dx + dy * dy)
  440. if (!length) {
  441. return
  442. }
  443. for (let index = 0; index < GUIDE_FLOW_COUNT; index += 1) {
  444. const progress = (pulseFrame * GUIDE_FLOW_SPEED + index / GUIDE_FLOW_COUNT) % 1
  445. const tailProgress = Math.max(0, progress - GUIDE_FLOW_TRAIL)
  446. const head = {
  447. x: trimmed.from.x + dx * progress,
  448. y: trimmed.from.y + dy * progress,
  449. }
  450. const tail = {
  451. x: trimmed.from.x + dx * tailProgress,
  452. y: trimmed.from.y + dy * tailProgress,
  453. }
  454. const eased = progress * progress
  455. const width = this.getMetric(
  456. scene,
  457. controlRadiusMeters * (GUIDE_FLOW_MIN_WIDTH_RATIO + (GUIDE_FLOW_MAX_WIDTH_RATIO - GUIDE_FLOW_MIN_WIDTH_RATIO) * eased),
  458. )
  459. const outerColor = this.getGuideFlowOuterColor(eased)
  460. const innerColor = this.getGuideFlowInnerColor(eased)
  461. const headRadius = this.getMetric(scene, controlRadiusMeters * GUIDE_FLOW_HEAD_RADIUS_RATIO * (0.72 + eased * 0.42))
  462. this.pushSegment(positions, colors, tail, head, width * 1.9, outerColor, scene)
  463. this.pushSegment(positions, colors, tail, head, width, innerColor, scene)
  464. this.pushCircle(positions, colors, head.x, head.y, headRadius * 1.35, outerColor, scene)
  465. this.pushCircle(positions, colors, head.x, head.y, headRadius, innerColor, scene)
  466. }
  467. }
  468. getTrimmedCourseLeg(
  469. leg: ProjectedCourseLeg,
  470. controlRadiusMeters: number,
  471. scene: MapScene,
  472. ): { from: { x: number; y: number }; to: { x: number; y: number } } | null {
  473. return this.trimSegment(
  474. leg.from,
  475. leg.to,
  476. this.getLegTrim(leg.fromKind, controlRadiusMeters, scene),
  477. this.getLegTrim(leg.toKind, controlRadiusMeters, scene),
  478. )
  479. }
  480. getGuideFlowOuterColor(progress: number): RgbaColor {
  481. return [0.28, 0.92, 1, 0.14 + progress * 0.22]
  482. }
  483. getGuideFlowInnerColor(progress: number): RgbaColor {
  484. return [0.94, 0.99, 1, 0.38 + progress * 0.42]
  485. }
  486. getLegTrim(kind: ProjectedCourseLeg['fromKind'], controlRadiusMeters: number, scene: MapScene): number {
  487. if (kind === 'start') {
  488. return this.getMetric(scene, controlRadiusMeters * (1 - START_RING_WIDTH_RATIO / 2))
  489. }
  490. if (kind === 'finish') {
  491. return this.getMetric(scene, controlRadiusMeters * (1 - FINISH_RING_WIDTH_RATIO / 2))
  492. }
  493. return this.getMetric(scene, controlRadiusMeters * LEG_TRIM_TO_RING_CENTER_RATIO)
  494. }
  495. trimSegment(
  496. from: { x: number; y: number },
  497. to: { x: number; y: number },
  498. fromTrim: number,
  499. toTrim: number,
  500. ): { from: { x: number; y: number }; to: { x: number; y: number } } | null {
  501. const dx = to.x - from.x
  502. const dy = to.y - from.y
  503. const length = Math.sqrt(dx * dx + dy * dy)
  504. if (!length || length <= fromTrim + toTrim) {
  505. return null
  506. }
  507. const ux = dx / length
  508. const uy = dy / length
  509. return {
  510. from: {
  511. x: from.x + ux * fromTrim,
  512. y: from.y + uy * fromTrim,
  513. },
  514. to: {
  515. x: to.x - ux * toTrim,
  516. y: to.y - uy * toTrim,
  517. },
  518. }
  519. }
  520. pushStartTriangle(
  521. positions: number[],
  522. colors: number[],
  523. centerX: number,
  524. centerY: number,
  525. headingDeg: number | null,
  526. controlRadiusMeters: number,
  527. color: RgbaColor,
  528. scene: MapScene,
  529. ): void {
  530. const startRadius = this.getMetric(scene, controlRadiusMeters)
  531. const startRingWidth = this.getMetric(scene, controlRadiusMeters * START_RING_WIDTH_RATIO)
  532. const headingRad = ((headingDeg === null ? 0 : headingDeg) - 90) * Math.PI / 180
  533. const vertices = [0, 1, 2].map((index) => {
  534. const angle = headingRad + index * (Math.PI * 2 / 3)
  535. return {
  536. x: centerX + Math.cos(angle) * startRadius,
  537. y: centerY + Math.sin(angle) * startRadius,
  538. }
  539. })
  540. this.pushSegment(positions, colors, vertices[0], vertices[1], startRingWidth, color, scene)
  541. this.pushSegment(positions, colors, vertices[1], vertices[2], startRingWidth, color, scene)
  542. this.pushSegment(positions, colors, vertices[2], vertices[0], startRingWidth, color, scene)
  543. }
  544. pushRing(
  545. positions: number[],
  546. colors: number[],
  547. centerX: number,
  548. centerY: number,
  549. outerRadius: number,
  550. innerRadius: number,
  551. color: RgbaColor,
  552. scene: MapScene,
  553. ): void {
  554. const segments = 36
  555. for (let index = 0; index < segments; index += 1) {
  556. const startAngle = index / segments * Math.PI * 2
  557. const endAngle = (index + 1) / segments * Math.PI * 2
  558. const outerStart = this.toClip(centerX + Math.cos(startAngle) * outerRadius, centerY + Math.sin(startAngle) * outerRadius, scene)
  559. const outerEnd = this.toClip(centerX + Math.cos(endAngle) * outerRadius, centerY + Math.sin(endAngle) * outerRadius, scene)
  560. const innerStart = this.toClip(centerX + Math.cos(startAngle) * innerRadius, centerY + Math.sin(startAngle) * innerRadius, scene)
  561. const innerEnd = this.toClip(centerX + Math.cos(endAngle) * innerRadius, centerY + Math.sin(endAngle) * innerRadius, scene)
  562. this.pushTriangle(positions, colors, outerStart, outerEnd, innerStart, color)
  563. this.pushTriangle(positions, colors, innerStart, outerEnd, innerEnd, color)
  564. }
  565. }
  566. pushSegment(
  567. positions: number[],
  568. colors: number[],
  569. start: { x: number; y: number },
  570. end: { x: number; y: number },
  571. width: number,
  572. color: RgbaColor,
  573. scene: MapScene,
  574. ): void {
  575. const deltaX = end.x - start.x
  576. const deltaY = end.y - start.y
  577. const length = Math.sqrt(deltaX * deltaX + deltaY * deltaY)
  578. if (!length) {
  579. return
  580. }
  581. const normalX = -deltaY / length * (width / 2)
  582. const normalY = deltaX / length * (width / 2)
  583. const topLeft = this.toClip(start.x + normalX, start.y + normalY, scene)
  584. const topRight = this.toClip(end.x + normalX, end.y + normalY, scene)
  585. const bottomLeft = this.toClip(start.x - normalX, start.y - normalY, scene)
  586. const bottomRight = this.toClip(end.x - normalX, end.y - normalY, scene)
  587. this.pushTriangle(positions, colors, topLeft, topRight, bottomLeft, color)
  588. this.pushTriangle(positions, colors, bottomLeft, topRight, bottomRight, color)
  589. }
  590. pushCircle(
  591. positions: number[],
  592. colors: number[],
  593. centerX: number,
  594. centerY: number,
  595. radius: number,
  596. color: RgbaColor,
  597. scene: MapScene,
  598. ): void {
  599. const segments = 20
  600. const center = this.toClip(centerX, centerY, scene)
  601. for (let index = 0; index < segments; index += 1) {
  602. const startAngle = index / segments * Math.PI * 2
  603. const endAngle = (index + 1) / segments * Math.PI * 2
  604. const start = this.toClip(centerX + Math.cos(startAngle) * radius, centerY + Math.sin(startAngle) * radius, scene)
  605. const end = this.toClip(centerX + Math.cos(endAngle) * radius, centerY + Math.sin(endAngle) * radius, scene)
  606. this.pushTriangle(positions, colors, center, start, end, color)
  607. }
  608. }
  609. pushTriangle(
  610. positions: number[],
  611. colors: number[],
  612. first: { x: number; y: number },
  613. second: { x: number; y: number },
  614. third: { x: number; y: number },
  615. color: RgbaColor,
  616. ): void {
  617. positions.push(first.x, first.y, second.x, second.y, third.x, third.y)
  618. for (let index = 0; index < 3; index += 1) {
  619. colors.push(color[0], color[1], color[2], color[3])
  620. }
  621. }
  622. toClip(x: number, y: number, scene: MapScene): { x: number; y: number } {
  623. const previewScale = scene.previewScale || 1
  624. const originX = scene.previewOriginX || scene.viewportWidth / 2
  625. const originY = scene.previewOriginY || scene.viewportHeight / 2
  626. const scaledX = originX + (x - originX) * previewScale
  627. const scaledY = originY + (y - originY) * previewScale
  628. return {
  629. x: scaledX / scene.viewportWidth * 2 - 1,
  630. y: 1 - scaledY / scene.viewportHeight * 2,
  631. }
  632. }
  633. }