webglMapRenderer.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { TrackLayer } from '../layer/trackLayer'
  2. import { GpsLayer } from '../layer/gpsLayer'
  3. import { TileLayer } from '../layer/tileLayer'
  4. import { TileStore, type TileStoreCallbacks } from '../tile/tileStore'
  5. import { type MapRenderer, type MapRendererStats, type MapScene } from './mapRenderer'
  6. import { WebGLTileRenderer } from './webglTileRenderer'
  7. import { WebGLVectorRenderer } from './webglVectorRenderer'
  8. const RENDER_FRAME_MS = 16
  9. const ANIMATION_FRAME_MS = 33
  10. export class WebGLMapRenderer implements MapRenderer {
  11. tileStore: TileStore
  12. tileLayer: TileLayer
  13. trackLayer: TrackLayer
  14. gpsLayer: GpsLayer
  15. tileRenderer: WebGLTileRenderer
  16. vectorRenderer: WebGLVectorRenderer
  17. scene: MapScene | null
  18. renderTimer: number
  19. animationTimer: number
  20. destroyed: boolean
  21. animationPaused: boolean
  22. pulseFrame: number
  23. lastStats: MapRendererStats
  24. onStats?: (stats: MapRendererStats) => void
  25. onTileError?: (message: string) => void
  26. constructor(onStats?: (stats: MapRendererStats) => void, onTileError?: (message: string) => void) {
  27. this.onStats = onStats
  28. this.onTileError = onTileError
  29. this.tileStore = new TileStore({
  30. onTileReady: () => {
  31. this.scheduleRender()
  32. },
  33. onTileError: (message) => {
  34. if (this.onTileError) {
  35. this.onTileError(message)
  36. }
  37. this.scheduleRender()
  38. },
  39. } satisfies TileStoreCallbacks)
  40. this.tileLayer = new TileLayer()
  41. this.trackLayer = new TrackLayer()
  42. this.gpsLayer = new GpsLayer()
  43. this.tileRenderer = new WebGLTileRenderer(this.tileLayer, this.tileStore)
  44. this.vectorRenderer = new WebGLVectorRenderer(this.trackLayer, this.gpsLayer)
  45. this.scene = null
  46. this.renderTimer = 0
  47. this.animationTimer = 0
  48. this.destroyed = false
  49. this.animationPaused = false
  50. this.pulseFrame = 0
  51. this.lastStats = {
  52. visibleTileCount: 0,
  53. readyTileCount: 0,
  54. memoryTileCount: 0,
  55. diskTileCount: 0,
  56. memoryHitCount: 0,
  57. diskHitCount: 0,
  58. networkFetchCount: 0,
  59. }
  60. }
  61. attachCanvas(canvasNode: any, width: number, height: number, dpr: number): void {
  62. this.tileRenderer.attachCanvas(canvasNode, width, height, dpr)
  63. this.vectorRenderer.attachContext(this.tileRenderer.gl, canvasNode)
  64. this.startAnimation()
  65. this.scheduleRender()
  66. }
  67. updateScene(scene: MapScene): void {
  68. this.scene = scene
  69. this.scheduleRender()
  70. }
  71. setAnimationPaused(paused: boolean): void {
  72. this.animationPaused = paused
  73. if (!paused) {
  74. this.scheduleRender()
  75. }
  76. }
  77. destroy(): void {
  78. this.destroyed = true
  79. if (this.renderTimer) {
  80. clearTimeout(this.renderTimer)
  81. this.renderTimer = 0
  82. }
  83. if (this.animationTimer) {
  84. clearTimeout(this.animationTimer)
  85. this.animationTimer = 0
  86. }
  87. this.vectorRenderer.destroy()
  88. this.tileRenderer.destroy()
  89. this.tileStore.destroy()
  90. this.scene = null
  91. }
  92. startAnimation(): void {
  93. if (this.animationTimer) {
  94. return
  95. }
  96. const tick = () => {
  97. if (this.destroyed) {
  98. this.animationTimer = 0
  99. return
  100. }
  101. if (!this.animationPaused) {
  102. this.pulseFrame = (this.pulseFrame + 1) % 360
  103. this.scheduleRender()
  104. }
  105. this.animationTimer = setTimeout(tick, ANIMATION_FRAME_MS) as unknown as number
  106. }
  107. tick()
  108. }
  109. scheduleRender(): void {
  110. if (this.renderTimer || !this.scene || this.destroyed) {
  111. return
  112. }
  113. this.renderTimer = setTimeout(() => {
  114. this.renderTimer = 0
  115. this.renderFrame()
  116. }, RENDER_FRAME_MS) as unknown as number
  117. }
  118. renderFrame(): void {
  119. if (!this.scene) {
  120. return
  121. }
  122. this.tileRenderer.render(this.scene)
  123. this.vectorRenderer.render(this.scene, this.pulseFrame)
  124. this.emitStats(this.tileStore.getStats(this.tileLayer.lastVisibleTileCount, this.tileLayer.lastReadyTileCount))
  125. }
  126. emitStats(stats: MapRendererStats): void {
  127. if (
  128. stats.visibleTileCount === this.lastStats.visibleTileCount
  129. && stats.readyTileCount === this.lastStats.readyTileCount
  130. && stats.memoryTileCount === this.lastStats.memoryTileCount
  131. && stats.diskTileCount === this.lastStats.diskTileCount
  132. && stats.memoryHitCount === this.lastStats.memoryHitCount
  133. && stats.diskHitCount === this.lastStats.diskHitCount
  134. && stats.networkFetchCount === this.lastStats.networkFetchCount
  135. ) {
  136. return
  137. }
  138. this.lastStats = stats
  139. if (this.onStats) {
  140. this.onStats(stats)
  141. }
  142. }
  143. }