webglMapRenderer.ts 4.8 KB

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