webglVectorRenderer.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. isSkippedControl(scene: MapScene, sequence: number): boolean {
  305. return scene.skippedControlSequences.includes(sequence)
  306. }
  307. pushCourseLeg(
  308. positions: number[],
  309. colors: number[],
  310. leg: ProjectedCourseLeg,
  311. controlRadiusMeters: number,
  312. color: RgbaColor,
  313. scene: MapScene,
  314. ): void {
  315. const trimmed = this.getTrimmedCourseLeg(leg, controlRadiusMeters, scene)
  316. if (!trimmed) {
  317. return
  318. }
  319. this.pushSegment(positions, colors, trimmed.from, trimmed.to, this.getMetric(scene, controlRadiusMeters * LEG_WIDTH_RATIO), color, scene)
  320. }
  321. pushCourseLegHighlight(
  322. positions: number[],
  323. colors: number[],
  324. leg: ProjectedCourseLeg,
  325. controlRadiusMeters: number,
  326. scene: MapScene,
  327. ): void {
  328. const trimmed = this.getTrimmedCourseLeg(leg, controlRadiusMeters, scene)
  329. if (!trimmed) {
  330. return
  331. }
  332. this.pushSegment(
  333. positions,
  334. colors,
  335. trimmed.from,
  336. trimmed.to,
  337. this.getMetric(scene, controlRadiusMeters * LEG_WIDTH_RATIO * 1.5),
  338. ACTIVE_LEG_COLOR,
  339. scene,
  340. )
  341. }
  342. pushActiveControlPulse(
  343. positions: number[],
  344. colors: number[],
  345. centerX: number,
  346. centerY: number,
  347. controlRadiusMeters: number,
  348. scene: MapScene,
  349. pulseFrame: number,
  350. pulseColor?: RgbaColor,
  351. ): void {
  352. const pulse = (Math.sin(pulseFrame * ACTIVE_CONTROL_PULSE_SPEED) + 1) / 2
  353. const pulseScale = ACTIVE_CONTROL_PULSE_MIN_SCALE + (ACTIVE_CONTROL_PULSE_MAX_SCALE - ACTIVE_CONTROL_PULSE_MIN_SCALE) * pulse
  354. const pulseWidthScale = pulseScale - ACTIVE_CONTROL_PULSE_WIDTH_RATIO
  355. const baseColor = pulseColor || ACTIVE_CONTROL_COLOR
  356. const glowAlpha = Math.min(1, baseColor[3] * (0.46 + pulse * 0.5))
  357. const glowColor: RgbaColor = [baseColor[0], baseColor[1], baseColor[2], glowAlpha]
  358. this.pushRing(
  359. positions,
  360. colors,
  361. centerX,
  362. centerY,
  363. this.getMetric(scene, controlRadiusMeters * pulseScale),
  364. this.getMetric(scene, controlRadiusMeters * Math.max(1, pulseWidthScale)),
  365. glowColor,
  366. scene,
  367. )
  368. }
  369. pushActiveStartPulse(
  370. positions: number[],
  371. colors: number[],
  372. centerX: number,
  373. centerY: number,
  374. headingDeg: number | null,
  375. controlRadiusMeters: number,
  376. scene: MapScene,
  377. pulseFrame: number,
  378. ): void {
  379. const pulse = (Math.sin(pulseFrame * ACTIVE_CONTROL_PULSE_SPEED) + 1) / 2
  380. const pulseScale = ACTIVE_CONTROL_PULSE_MIN_SCALE + (ACTIVE_CONTROL_PULSE_MAX_SCALE - ACTIVE_CONTROL_PULSE_MIN_SCALE) * pulse
  381. const pulseWidthScale = pulseScale - ACTIVE_CONTROL_PULSE_WIDTH_RATIO
  382. const glowAlpha = 0.24 + pulse * 0.34
  383. const glowColor: RgbaColor = [0.36, 1, 0.96, glowAlpha]
  384. const headingRad = ((headingDeg === null ? 0 : headingDeg) - 90) * Math.PI / 180
  385. const ringCenterX = centerX + Math.cos(headingRad) * this.getMetric(scene, controlRadiusMeters * 0.04)
  386. const ringCenterY = centerY + Math.sin(headingRad) * this.getMetric(scene, controlRadiusMeters * 0.04)
  387. this.pushRing(
  388. positions,
  389. colors,
  390. ringCenterX,
  391. ringCenterY,
  392. this.getMetric(scene, controlRadiusMeters * pulseScale),
  393. this.getMetric(scene, controlRadiusMeters * Math.max(1, pulseWidthScale)),
  394. glowColor,
  395. scene,
  396. )
  397. }
  398. getStartColor(scene: MapScene): RgbaColor {
  399. if (scene.activeStart) {
  400. return ACTIVE_CONTROL_COLOR
  401. }
  402. if (scene.completedStart) {
  403. return COMPLETED_ROUTE_COLOR
  404. }
  405. return COURSE_COLOR
  406. }
  407. getControlColor(scene: MapScene, sequence: number): RgbaColor {
  408. if (scene.activeControlSequences.includes(sequence)) {
  409. return scene.controlVisualMode === 'multi-target' ? MULTI_ACTIVE_CONTROL_COLOR : ACTIVE_CONTROL_COLOR
  410. }
  411. if (scene.completedControlSequences.includes(sequence) || this.isSkippedControl(scene, sequence)) {
  412. return COMPLETED_ROUTE_COLOR
  413. }
  414. return COURSE_COLOR
  415. }
  416. getFinishColor(scene: MapScene): RgbaColor {
  417. if (scene.focusedFinish) {
  418. return FOCUSED_CONTROL_COLOR
  419. }
  420. if (scene.activeFinish) {
  421. return ACTIVE_CONTROL_COLOR
  422. }
  423. if (scene.completedFinish) {
  424. return COMPLETED_ROUTE_COLOR
  425. }
  426. return COURSE_COLOR
  427. }
  428. pushGuidanceFlow(
  429. positions: number[],
  430. colors: number[],
  431. leg: ProjectedCourseLeg,
  432. controlRadiusMeters: number,
  433. scene: MapScene,
  434. pulseFrame: number,
  435. ): void {
  436. const trimmed = this.getTrimmedCourseLeg(leg, controlRadiusMeters, scene)
  437. if (!trimmed) {
  438. return
  439. }
  440. const dx = trimmed.to.x - trimmed.from.x
  441. const dy = trimmed.to.y - trimmed.from.y
  442. const length = Math.sqrt(dx * dx + dy * dy)
  443. if (!length) {
  444. return
  445. }
  446. for (let index = 0; index < GUIDE_FLOW_COUNT; index += 1) {
  447. const progress = (pulseFrame * GUIDE_FLOW_SPEED + index / GUIDE_FLOW_COUNT) % 1
  448. const tailProgress = Math.max(0, progress - GUIDE_FLOW_TRAIL)
  449. const head = {
  450. x: trimmed.from.x + dx * progress,
  451. y: trimmed.from.y + dy * progress,
  452. }
  453. const tail = {
  454. x: trimmed.from.x + dx * tailProgress,
  455. y: trimmed.from.y + dy * tailProgress,
  456. }
  457. const eased = progress * progress
  458. const width = this.getMetric(
  459. scene,
  460. controlRadiusMeters * (GUIDE_FLOW_MIN_WIDTH_RATIO + (GUIDE_FLOW_MAX_WIDTH_RATIO - GUIDE_FLOW_MIN_WIDTH_RATIO) * eased),
  461. )
  462. const outerColor = this.getGuideFlowOuterColor(eased)
  463. const innerColor = this.getGuideFlowInnerColor(eased)
  464. const headRadius = this.getMetric(scene, controlRadiusMeters * GUIDE_FLOW_HEAD_RADIUS_RATIO * (0.72 + eased * 0.42))
  465. this.pushSegment(positions, colors, tail, head, width * 1.9, outerColor, scene)
  466. this.pushSegment(positions, colors, tail, head, width, innerColor, scene)
  467. this.pushCircle(positions, colors, head.x, head.y, headRadius * 1.35, outerColor, scene)
  468. this.pushCircle(positions, colors, head.x, head.y, headRadius, innerColor, scene)
  469. }
  470. }
  471. getTrimmedCourseLeg(
  472. leg: ProjectedCourseLeg,
  473. controlRadiusMeters: number,
  474. scene: MapScene,
  475. ): { from: { x: number; y: number }; to: { x: number; y: number } } | null {
  476. return this.trimSegment(
  477. leg.from,
  478. leg.to,
  479. this.getLegTrim(leg.fromKind, controlRadiusMeters, scene),
  480. this.getLegTrim(leg.toKind, controlRadiusMeters, scene),
  481. )
  482. }
  483. getGuideFlowOuterColor(progress: number): RgbaColor {
  484. return [0.28, 0.92, 1, 0.14 + progress * 0.22]
  485. }
  486. getGuideFlowInnerColor(progress: number): RgbaColor {
  487. return [0.94, 0.99, 1, 0.38 + progress * 0.42]
  488. }
  489. getLegTrim(kind: ProjectedCourseLeg['fromKind'], controlRadiusMeters: number, scene: MapScene): number {
  490. if (kind === 'start') {
  491. return this.getMetric(scene, controlRadiusMeters * (1 - START_RING_WIDTH_RATIO / 2))
  492. }
  493. if (kind === 'finish') {
  494. return this.getMetric(scene, controlRadiusMeters * (1 - FINISH_RING_WIDTH_RATIO / 2))
  495. }
  496. return this.getMetric(scene, controlRadiusMeters * LEG_TRIM_TO_RING_CENTER_RATIO)
  497. }
  498. trimSegment(
  499. from: { x: number; y: number },
  500. to: { x: number; y: number },
  501. fromTrim: number,
  502. toTrim: number,
  503. ): { from: { x: number; y: number }; to: { x: number; y: number } } | null {
  504. const dx = to.x - from.x
  505. const dy = to.y - from.y
  506. const length = Math.sqrt(dx * dx + dy * dy)
  507. if (!length || length <= fromTrim + toTrim) {
  508. return null
  509. }
  510. const ux = dx / length
  511. const uy = dy / length
  512. return {
  513. from: {
  514. x: from.x + ux * fromTrim,
  515. y: from.y + uy * fromTrim,
  516. },
  517. to: {
  518. x: to.x - ux * toTrim,
  519. y: to.y - uy * toTrim,
  520. },
  521. }
  522. }
  523. pushStartTriangle(
  524. positions: number[],
  525. colors: number[],
  526. centerX: number,
  527. centerY: number,
  528. headingDeg: number | null,
  529. controlRadiusMeters: number,
  530. color: RgbaColor,
  531. scene: MapScene,
  532. ): void {
  533. const startRadius = this.getMetric(scene, controlRadiusMeters)
  534. const startRingWidth = this.getMetric(scene, controlRadiusMeters * START_RING_WIDTH_RATIO)
  535. const headingRad = ((headingDeg === null ? 0 : headingDeg) - 90) * Math.PI / 180
  536. const vertices = [0, 1, 2].map((index) => {
  537. const angle = headingRad + index * (Math.PI * 2 / 3)
  538. return {
  539. x: centerX + Math.cos(angle) * startRadius,
  540. y: centerY + Math.sin(angle) * startRadius,
  541. }
  542. })
  543. this.pushSegment(positions, colors, vertices[0], vertices[1], startRingWidth, color, scene)
  544. this.pushSegment(positions, colors, vertices[1], vertices[2], startRingWidth, color, scene)
  545. this.pushSegment(positions, colors, vertices[2], vertices[0], startRingWidth, color, scene)
  546. }
  547. pushRing(
  548. positions: number[],
  549. colors: number[],
  550. centerX: number,
  551. centerY: number,
  552. outerRadius: number,
  553. innerRadius: number,
  554. color: RgbaColor,
  555. scene: MapScene,
  556. ): void {
  557. const segments = 36
  558. for (let index = 0; index < segments; index += 1) {
  559. const startAngle = index / segments * Math.PI * 2
  560. const endAngle = (index + 1) / segments * Math.PI * 2
  561. const outerStart = this.toClip(centerX + Math.cos(startAngle) * outerRadius, centerY + Math.sin(startAngle) * outerRadius, scene)
  562. const outerEnd = this.toClip(centerX + Math.cos(endAngle) * outerRadius, centerY + Math.sin(endAngle) * outerRadius, scene)
  563. const innerStart = this.toClip(centerX + Math.cos(startAngle) * innerRadius, centerY + Math.sin(startAngle) * innerRadius, scene)
  564. const innerEnd = this.toClip(centerX + Math.cos(endAngle) * innerRadius, centerY + Math.sin(endAngle) * innerRadius, scene)
  565. this.pushTriangle(positions, colors, outerStart, outerEnd, innerStart, color)
  566. this.pushTriangle(positions, colors, innerStart, outerEnd, innerEnd, color)
  567. }
  568. }
  569. pushSegment(
  570. positions: number[],
  571. colors: number[],
  572. start: { x: number; y: number },
  573. end: { x: number; y: number },
  574. width: number,
  575. color: RgbaColor,
  576. scene: MapScene,
  577. ): void {
  578. const deltaX = end.x - start.x
  579. const deltaY = end.y - start.y
  580. const length = Math.sqrt(deltaX * deltaX + deltaY * deltaY)
  581. if (!length) {
  582. return
  583. }
  584. const normalX = -deltaY / length * (width / 2)
  585. const normalY = deltaX / length * (width / 2)
  586. const topLeft = this.toClip(start.x + normalX, start.y + normalY, scene)
  587. const topRight = this.toClip(end.x + normalX, end.y + normalY, scene)
  588. const bottomLeft = this.toClip(start.x - normalX, start.y - normalY, scene)
  589. const bottomRight = this.toClip(end.x - normalX, end.y - normalY, scene)
  590. this.pushTriangle(positions, colors, topLeft, topRight, bottomLeft, color)
  591. this.pushTriangle(positions, colors, bottomLeft, topRight, bottomRight, color)
  592. }
  593. pushCircle(
  594. positions: number[],
  595. colors: number[],
  596. centerX: number,
  597. centerY: number,
  598. radius: number,
  599. color: RgbaColor,
  600. scene: MapScene,
  601. ): void {
  602. const segments = 20
  603. const center = this.toClip(centerX, centerY, scene)
  604. for (let index = 0; index < segments; index += 1) {
  605. const startAngle = index / segments * Math.PI * 2
  606. const endAngle = (index + 1) / segments * Math.PI * 2
  607. const start = this.toClip(centerX + Math.cos(startAngle) * radius, centerY + Math.sin(startAngle) * radius, scene)
  608. const end = this.toClip(centerX + Math.cos(endAngle) * radius, centerY + Math.sin(endAngle) * radius, scene)
  609. this.pushTriangle(positions, colors, center, start, end, color)
  610. }
  611. }
  612. pushTriangle(
  613. positions: number[],
  614. colors: number[],
  615. first: { x: number; y: number },
  616. second: { x: number; y: number },
  617. third: { x: number; y: number },
  618. color: RgbaColor,
  619. ): void {
  620. positions.push(first.x, first.y, second.x, second.y, third.x, third.y)
  621. for (let index = 0; index < 3; index += 1) {
  622. colors.push(color[0], color[1], color[2], color[3])
  623. }
  624. }
  625. toClip(x: number, y: number, scene: MapScene): { x: number; y: number } {
  626. const previewScale = scene.previewScale || 1
  627. const originX = scene.previewOriginX || scene.viewportWidth / 2
  628. const originY = scene.previewOriginY || scene.viewportHeight / 2
  629. const scaledX = originX + (x - originX) * previewScale
  630. const scaledY = originY + (y - originY) * previewScale
  631. return {
  632. x: scaledX / scene.viewportWidth * 2 - 1,
  633. y: 1 - scaledY / scene.viewportHeight * 2,
  634. }
  635. }
  636. }