#pragma once #include #include #include namespace mbgl { namespace shaders { template <> struct ShaderSource { static constexpr const char* name = "HillshadePrepareShader"; static const std::array attributes; static constexpr std::array instanceAttributes{}; static const std::array textures; static constexpr auto vertex = R"( struct VertexInput { @location(5) position: vec2, @location(6) texcoord: vec2, }; struct VertexOutput { @builtin(position) position: vec4, @location(0) tex_coord: vec2, }; struct HillshadePrepareDrawableUBO { matrix: mat4x4, }; struct HillshadePrepareTilePropsUBO { unpack: vec4, dimension: vec2, zoom: f32, maxzoom: f32, }; @group(0) @binding(2) var drawable: HillshadePrepareDrawableUBO; @group(0) @binding(4) var tileProps: HillshadePrepareTilePropsUBO; @vertex fn main(in: VertexInput) -> VertexOutput { var out: VertexOutput; out.position = drawable.matrix * vec4(f32(in.position.x), f32(in.position.y), 0.0, 1.0); let epsilon = vec2(1.0, 1.0) / tileProps.dimension; let scale = (tileProps.dimension.x - 2.0) / tileProps.dimension.x; out.tex_coord = vec2(f32(in.texcoord.x), f32(in.texcoord.y)) / 8192.0 * scale + epsilon; return out; } )"; static constexpr auto fragment = R"( struct FragmentInput { @location(0) tex_coord: vec2, }; struct HillshadePrepareTilePropsUBO { unpack: vec4, dimension: vec2, zoom: f32, maxzoom: f32, }; @group(0) @binding(4) var tileProps: HillshadePrepareTilePropsUBO; @group(1) @binding(0) var texture_sampler: sampler; @group(1) @binding(1) var dem_texture: texture_2d; fn getElevation(coord: vec2) -> f32 { var data = textureSample(dem_texture, texture_sampler, coord) * 255.0; data.a = -1.0; return dot(data, tileProps.unpack); } @fragment fn main(in: FragmentInput) -> @location(0) vec4 { #ifdef OVERDRAW_INSPECTOR return vec4(1.0, 1.0, 1.0, 1.0); #endif let epsilon = vec2(1.0, 1.0) / tileProps.dimension; let tileSize = tileProps.dimension.x - 2.0; let a = getElevation(in.tex_coord + vec2(-epsilon.x, -epsilon.y)); let b = getElevation(in.tex_coord + vec2(0.0, -epsilon.y)); let c = getElevation(in.tex_coord + vec2(epsilon.x, -epsilon.y)); let d = getElevation(in.tex_coord + vec2(-epsilon.x, 0.0)); let f = getElevation(in.tex_coord + vec2(epsilon.x, 0.0)); let g = getElevation(in.tex_coord + vec2(-epsilon.x, epsilon.y)); let h = getElevation(in.tex_coord + vec2(0.0, epsilon.y)); let i = getElevation(in.tex_coord + vec2(epsilon.x, epsilon.y)); var exaggerationFactor = 0.3; if (tileProps.zoom < 4.5) { exaggerationFactor = 0.35; } if (tileProps.zoom < 2.0) { exaggerationFactor = 0.4; } var exaggeration = 0.0; if (tileProps.zoom < 15.0) { exaggeration = (tileProps.zoom - 15.0) * exaggerationFactor; } let denom = pow(2.0, exaggeration + (28.2562 - tileProps.zoom)); let deriv = vec2((c + f + f + i) - (a + d + d + g), (g + h + h + i) - (a + b + b + c)) * tileSize / denom; let color = clamp(vec4(deriv.x / 8.0 + 0.5, deriv.y / 8.0 + 0.5, 1.0, 1.0), vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0)); return color; } )"; }; } // namespace shaders } // namespace mbgl