|
| 1 | +import {Texture} from './texture'; |
| 2 | +import type {StencilMode} from '../gl/stencil_mode'; |
| 3 | +import {DepthMode} from '../gl/depth_mode'; |
| 4 | +import {CullFaceMode} from '../gl/cull_face_mode'; |
| 5 | +import {type ColorMode} from '../gl/color_mode'; |
| 6 | +import { |
| 7 | + colorReliefUniformValues |
| 8 | +} from './program/color_relief_program'; |
| 9 | + |
| 10 | +import type {Painter, RenderOptions} from './painter'; |
| 11 | +import type {SourceCache} from '../source/source_cache'; |
| 12 | +import type {ColorReliefStyleLayer} from '../style/style_layer/color_relief_style_layer'; |
| 13 | +import type {OverscaledTileID} from '../source/tile_id'; |
| 14 | + |
| 15 | +export function drawColorRelief(painter: Painter, sourceCache: SourceCache, layer: ColorReliefStyleLayer, tileIDs: Array<OverscaledTileID>, renderOptions: RenderOptions) { |
| 16 | + if (painter.renderPass !== 'translucent') return; |
| 17 | + if (!tileIDs.length) return; |
| 18 | + |
| 19 | + const {isRenderingToTexture} = renderOptions; |
| 20 | + const projection = painter.style.projection; |
| 21 | + const useSubdivision = projection.useSubdivision; |
| 22 | + |
| 23 | + const depthMode = painter.getDepthModeForSublayer(0, DepthMode.ReadOnly); |
| 24 | + const colorMode = painter.colorModeForRenderPass(); |
| 25 | + |
| 26 | + // Globe (or any projection with subdivision) needs two-pass rendering to avoid artifacts when rendering texture tiles. |
| 27 | + // See comments in draw_raster.ts for more details. |
| 28 | + if (useSubdivision) { |
| 29 | + // Two-pass rendering |
| 30 | + const [stencilBorderless, stencilBorders, coords] = painter.stencilConfigForOverlapTwoPass(tileIDs); |
| 31 | + renderColorRelief(painter, sourceCache, layer, coords, stencilBorderless, depthMode, colorMode, false, isRenderingToTexture); // draw without borders |
| 32 | + renderColorRelief(painter, sourceCache, layer, coords, stencilBorders, depthMode, colorMode, true, isRenderingToTexture); // draw with borders |
| 33 | + } else { |
| 34 | + // Simple rendering |
| 35 | + const [stencil, coords] = painter.getStencilConfigForOverlapAndUpdateStencilID(tileIDs); |
| 36 | + renderColorRelief(painter, sourceCache, layer, coords, stencil, depthMode, colorMode, false, isRenderingToTexture); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function renderColorRelief( |
| 41 | + painter: Painter, |
| 42 | + sourceCache: SourceCache, |
| 43 | + layer: ColorReliefStyleLayer, |
| 44 | + coords: Array<OverscaledTileID>, |
| 45 | + stencilModes: {[_: number]: Readonly<StencilMode>}, |
| 46 | + depthMode: Readonly<DepthMode>, |
| 47 | + colorMode: Readonly<ColorMode>, |
| 48 | + useBorder: boolean, |
| 49 | + isRenderingToTexture: boolean |
| 50 | +) { |
| 51 | + const projection = painter.style.projection; |
| 52 | + const context = painter.context; |
| 53 | + const transform = painter.transform; |
| 54 | + const gl = context.gl; |
| 55 | + const program = painter.useProgram('colorRelief'); |
| 56 | + const align = !painter.options.moving; |
| 57 | + |
| 58 | + let firstTile = true; |
| 59 | + |
| 60 | + for (const coord of coords) { |
| 61 | + const tile = sourceCache.getTile(coord); |
| 62 | + const dem = tile.dem; |
| 63 | + if(firstTile) { |
| 64 | + const maxLength = gl.getParameter(gl.MAX_TEXTURE_SIZE); |
| 65 | + const {elevationTexture, colorTexture} = layer.getColorRampTextures(context, maxLength, dem.getUnpackVector()); |
| 66 | + context.activeTexture.set(gl.TEXTURE1); |
| 67 | + elevationTexture.bind(gl.NEAREST, gl.CLAMP_TO_EDGE); |
| 68 | + context.activeTexture.set(gl.TEXTURE4); |
| 69 | + colorTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE); |
| 70 | + firstTile = false; |
| 71 | + } |
| 72 | + |
| 73 | + if (!dem || !dem.data) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + const textureStride = dem.stride; |
| 78 | + |
| 79 | + const pixelData = dem.getPixels(); |
| 80 | + context.activeTexture.set(gl.TEXTURE0); |
| 81 | + |
| 82 | + context.pixelStoreUnpackPremultiplyAlpha.set(false); |
| 83 | + tile.demTexture = tile.demTexture || painter.getTileTexture(textureStride); |
| 84 | + if (tile.demTexture) { |
| 85 | + const demTexture = tile.demTexture; |
| 86 | + demTexture.update(pixelData, {premultiply: false}); |
| 87 | + demTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE); |
| 88 | + } else { |
| 89 | + tile.demTexture = new Texture(context, pixelData, gl.RGBA, {premultiply: false}); |
| 90 | + tile.demTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE); |
| 91 | + } |
| 92 | + |
| 93 | + const mesh = projection.getMeshFromTileID(context, coord.canonical, useBorder, true, 'raster'); |
| 94 | + |
| 95 | + const terrainData = painter.style.map.terrain?.getTerrainData(coord); |
| 96 | + |
| 97 | + const projectionData = transform.getProjectionData({ |
| 98 | + overscaledTileID: coord, |
| 99 | + aligned: align, |
| 100 | + applyGlobeMatrix: !isRenderingToTexture, |
| 101 | + applyTerrainMatrix: true |
| 102 | + }); |
| 103 | + |
| 104 | + program.draw(context, gl.TRIANGLES, depthMode, stencilModes[coord.overscaledZ], colorMode, CullFaceMode.backCCW, |
| 105 | + colorReliefUniformValues(layer, tile.dem), terrainData, projectionData, layer.id, mesh.vertexBuffer, mesh.indexBuffer, mesh.segments); |
| 106 | + } |
| 107 | +} |
0 commit comments