webglTileRenderer.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import { rotateScreenPoint, type ScreenPoint } from '../camera/camera'
  2. import { type TileStore, type TileStoreEntry } from '../tile/tileStore'
  3. import { TileLayer } from '../layer/tileLayer'
  4. import { buildCamera, type MapScene } from './mapRenderer'
  5. interface TextureRecord {
  6. key: string
  7. texture: any
  8. }
  9. function createShader(gl: any, type: number, source: string): any {
  10. const shader = gl.createShader(type)
  11. if (!shader) {
  12. throw new Error('WebGL shader 创建失败')
  13. }
  14. gl.shaderSource(shader, source)
  15. gl.compileShader(shader)
  16. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  17. const message = gl.getShaderInfoLog(shader) || 'unknown shader error'
  18. gl.deleteShader(shader)
  19. throw new Error(message)
  20. }
  21. return shader
  22. }
  23. function createProgram(gl: any, vertexSource: string, fragmentSource: string): any {
  24. const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource)
  25. const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource)
  26. const program = gl.createProgram()
  27. if (!program) {
  28. throw new Error('WebGL program 创建失败')
  29. }
  30. gl.attachShader(program, vertexShader)
  31. gl.attachShader(program, fragmentShader)
  32. gl.linkProgram(program)
  33. gl.deleteShader(vertexShader)
  34. gl.deleteShader(fragmentShader)
  35. if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
  36. const message = gl.getProgramInfoLog(program) || 'unknown program error'
  37. gl.deleteProgram(program)
  38. throw new Error(message)
  39. }
  40. return program
  41. }
  42. export class WebGLTileRenderer {
  43. canvas: any
  44. gl: any
  45. tileLayer: TileLayer
  46. tileStore: TileStore
  47. osmTileLayer: TileLayer
  48. osmTileStore: TileStore
  49. dpr: number
  50. program: any
  51. positionBuffer: any
  52. texCoordBuffer: any
  53. positionLocation: number
  54. texCoordLocation: number
  55. opacityLocation: any
  56. textureCache: Map<string, TextureRecord>
  57. constructor(tileLayer: TileLayer, tileStore: TileStore, osmTileLayer: TileLayer, osmTileStore: TileStore) {
  58. this.canvas = null
  59. this.gl = null
  60. this.tileLayer = tileLayer
  61. this.tileStore = tileStore
  62. this.osmTileLayer = osmTileLayer
  63. this.osmTileStore = osmTileStore
  64. this.dpr = 1
  65. this.program = null
  66. this.positionBuffer = null
  67. this.texCoordBuffer = null
  68. this.positionLocation = -1
  69. this.texCoordLocation = -1
  70. this.opacityLocation = null
  71. this.textureCache = new Map<string, TextureRecord>()
  72. }
  73. attachCanvas(canvasNode: any, width: number, height: number, dpr: number): void {
  74. this.canvas = canvasNode
  75. this.dpr = dpr || 1
  76. canvasNode.width = Math.max(1, Math.floor(width * this.dpr))
  77. canvasNode.height = Math.max(1, Math.floor(height * this.dpr))
  78. const gl = canvasNode.getContext('webgl') || canvasNode.getContext('experimental-webgl')
  79. if (!gl) {
  80. throw new Error('当前环境不支持 WebGL')
  81. }
  82. this.gl = gl
  83. this.program = createProgram(
  84. gl,
  85. 'attribute vec2 a_position; attribute vec2 a_texCoord; varying vec2 v_texCoord; void main() { gl_Position = vec4(a_position, 0.0, 1.0); v_texCoord = a_texCoord; }',
  86. 'precision mediump float; varying vec2 v_texCoord; uniform sampler2D u_texture; uniform float u_opacity; void main() { vec4 color = texture2D(u_texture, v_texCoord); gl_FragColor = vec4(color.rgb, color.a * u_opacity); }',
  87. )
  88. this.positionBuffer = gl.createBuffer()
  89. this.texCoordBuffer = gl.createBuffer()
  90. this.positionLocation = gl.getAttribLocation(this.program, 'a_position')
  91. this.texCoordLocation = gl.getAttribLocation(this.program, 'a_texCoord')
  92. this.opacityLocation = gl.getUniformLocation(this.program, 'u_opacity')
  93. gl.viewport(0, 0, canvasNode.width, canvasNode.height)
  94. gl.disable(gl.DEPTH_TEST)
  95. gl.enable(gl.BLEND)
  96. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
  97. this.tileStore.attachCanvas(canvasNode)
  98. this.osmTileStore.attachCanvas(canvasNode)
  99. }
  100. destroy(): void {
  101. if (this.gl) {
  102. this.textureCache.forEach((record) => {
  103. this.gl && this.gl.deleteTexture(record.texture)
  104. })
  105. if (this.program) {
  106. this.gl.deleteProgram(this.program)
  107. }
  108. if (this.positionBuffer) {
  109. this.gl.deleteBuffer(this.positionBuffer)
  110. }
  111. if (this.texCoordBuffer) {
  112. this.gl.deleteBuffer(this.texCoordBuffer)
  113. }
  114. }
  115. this.textureCache.clear()
  116. this.program = null
  117. this.positionBuffer = null
  118. this.texCoordBuffer = null
  119. this.gl = null
  120. this.canvas = null
  121. }
  122. render(scene: MapScene): void {
  123. if (!this.gl || !this.program || !this.positionBuffer || !this.texCoordBuffer) {
  124. return
  125. }
  126. const gl = this.gl
  127. gl.viewport(0, 0, this.canvas.width, this.canvas.height)
  128. gl.clearColor(0.8588, 0.9333, 0.8314, 1)
  129. gl.clear(gl.COLOR_BUFFER_BIT)
  130. gl.useProgram(this.program)
  131. if (scene.osmReferenceEnabled) {
  132. this.renderTilePass(
  133. {
  134. ...scene,
  135. tileSource: scene.osmTileSource,
  136. tileBoundsByZoom: null,
  137. },
  138. 1,
  139. this.osmTileLayer,
  140. this.osmTileStore,
  141. )
  142. }
  143. this.renderTilePass(
  144. scene,
  145. scene.osmReferenceEnabled ? scene.overlayOpacity : 1,
  146. this.tileLayer,
  147. this.tileStore,
  148. )
  149. }
  150. renderTilePass(scene: MapScene, opacity: number, tileLayer: TileLayer, tileStore: TileStore): void {
  151. const camera = buildCamera(scene)
  152. const tiles = tileLayer.prepareTiles(scene, camera, tileStore)
  153. for (const tile of tiles) {
  154. const readyEntry = tileStore.getEntry(tile.url)
  155. if (readyEntry && readyEntry.status === 'ready' && readyEntry.image) {
  156. this.drawEntry(readyEntry, tile.url, 0, 0, readyEntry.image.width || 256, readyEntry.image.height || 256, tile.leftPx, tile.topPx, tile.sizePx, tile.sizePx, scene, opacity)
  157. tileLayer.lastReadyTileCount += 1
  158. continue
  159. }
  160. const parentFallback = tileStore.getParentFallbackSlice(tile, scene)
  161. if (parentFallback) {
  162. this.drawEntry(
  163. parentFallback.entry,
  164. tile.url + '|parent',
  165. parentFallback.sourceX,
  166. parentFallback.sourceY,
  167. parentFallback.sourceWidth,
  168. parentFallback.sourceHeight,
  169. tile.leftPx,
  170. tile.topPx,
  171. tile.sizePx,
  172. tile.sizePx,
  173. scene,
  174. opacity,
  175. )
  176. }
  177. const childFallback = tileStore.getChildFallback(tile, scene)
  178. if (!childFallback) {
  179. continue
  180. }
  181. const cellWidth = tile.sizePx / childFallback.division
  182. const cellHeight = tile.sizePx / childFallback.division
  183. for (const child of childFallback.children) {
  184. this.drawEntry(
  185. child.entry,
  186. tile.url + '|child|' + child.offsetX + '|' + child.offsetY,
  187. 0,
  188. 0,
  189. child.entry.image.width || 256,
  190. child.entry.image.height || 256,
  191. tile.leftPx + child.offsetX * cellWidth,
  192. tile.topPx + child.offsetY * cellHeight,
  193. cellWidth,
  194. cellHeight,
  195. scene,
  196. opacity,
  197. )
  198. }
  199. }
  200. }
  201. drawEntry(
  202. entry: TileStoreEntry,
  203. cacheKey: string,
  204. sourceX: number,
  205. sourceY: number,
  206. sourceWidth: number,
  207. sourceHeight: number,
  208. drawLeft: number,
  209. drawTop: number,
  210. drawWidth: number,
  211. drawHeight: number,
  212. scene: MapScene,
  213. opacity: number,
  214. ): void {
  215. if (!this.gl || !entry.image) {
  216. return
  217. }
  218. const texture = this.getTexture(cacheKey, entry)
  219. if (!texture) {
  220. return
  221. }
  222. const gl = this.gl
  223. const imageWidth = entry.image.width || 256
  224. const imageHeight = entry.image.height || 256
  225. const texLeft = sourceX / imageWidth
  226. const texTop = sourceY / imageHeight
  227. const texRight = (sourceX + sourceWidth) / imageWidth
  228. const texBottom = (sourceY + sourceHeight) / imageHeight
  229. const topLeft = this.transformToClip(drawLeft, drawTop, scene)
  230. const topRight = this.transformToClip(drawLeft + drawWidth, drawTop, scene)
  231. const bottomLeft = this.transformToClip(drawLeft, drawTop + drawHeight, scene)
  232. const bottomRight = this.transformToClip(drawLeft + drawWidth, drawTop + drawHeight, scene)
  233. const positions = new Float32Array([
  234. topLeft.x, topLeft.y,
  235. topRight.x, topRight.y,
  236. bottomLeft.x, bottomLeft.y,
  237. bottomLeft.x, bottomLeft.y,
  238. topRight.x, topRight.y,
  239. bottomRight.x, bottomRight.y,
  240. ])
  241. const texCoords = new Float32Array([
  242. texLeft, texTop,
  243. texRight, texTop,
  244. texLeft, texBottom,
  245. texLeft, texBottom,
  246. texRight, texTop,
  247. texRight, texBottom,
  248. ])
  249. gl.uniform1f(this.opacityLocation, opacity)
  250. gl.bindTexture(gl.TEXTURE_2D, texture.texture)
  251. gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer)
  252. gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STREAM_DRAW)
  253. gl.enableVertexAttribArray(this.positionLocation)
  254. gl.vertexAttribPointer(this.positionLocation, 2, gl.FLOAT, false, 0, 0)
  255. gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer)
  256. gl.bufferData(gl.ARRAY_BUFFER, texCoords, gl.STREAM_DRAW)
  257. gl.enableVertexAttribArray(this.texCoordLocation)
  258. gl.vertexAttribPointer(this.texCoordLocation, 2, gl.FLOAT, false, 0, 0)
  259. gl.drawArrays(gl.TRIANGLES, 0, 6)
  260. }
  261. getTexture(cacheKey: string, entry: TileStoreEntry): TextureRecord | null {
  262. if (!this.gl || !entry.image) {
  263. return null
  264. }
  265. const key = cacheKey + '|' + entry.sourcePath
  266. const existing = this.textureCache.get(key)
  267. if (existing) {
  268. return existing
  269. }
  270. const texture = this.gl.createTexture()
  271. if (!texture) {
  272. return null
  273. }
  274. this.gl.bindTexture(this.gl.TEXTURE_2D, texture)
  275. this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE)
  276. this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE)
  277. this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR)
  278. this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR)
  279. this.gl.pixelStorei(this.gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1)
  280. this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, entry.image)
  281. const record = { key, texture }
  282. this.textureCache.set(key, record)
  283. return record
  284. }
  285. transformToClip(x: number, y: number, scene: MapScene): ScreenPoint {
  286. const rotated = rotateScreenPoint(
  287. { x, y },
  288. scene.viewportWidth / 2,
  289. scene.viewportHeight / 2,
  290. scene.rotationRad || 0,
  291. )
  292. const translated = {
  293. x: rotated.x + scene.translateX,
  294. y: rotated.y + scene.translateY,
  295. }
  296. const previewed = this.applyPreview(translated.x, translated.y, scene)
  297. return {
  298. x: this.toClipX(previewed.x, scene.viewportWidth),
  299. y: this.toClipY(previewed.y, scene.viewportHeight),
  300. }
  301. }
  302. applyPreview(x: number, y: number, scene: MapScene): { x: number; y: number } {
  303. const scale = scene.previewScale || 1
  304. const originX = scene.previewOriginX || scene.viewportWidth / 2
  305. const originY = scene.previewOriginY || scene.viewportHeight / 2
  306. return {
  307. x: originX + (x - originX) * scale,
  308. y: originY + (y - originY) * scale,
  309. }
  310. }
  311. toClipX(x: number, width: number): number {
  312. return x / width * 2 - 1
  313. }
  314. toClipY(y: number, height: number): number {
  315. return 1 - y / height * 2
  316. }
  317. }