/* *
 *
 *  (c) 2010-2026 Highsoft AS
 *  Author: Torstein Hønsi
 *
 *  A commercial license may be required depending on use.
 *  See www.highcharts.com/license
 *
 *
 * */
'use strict';
/* *
 *
 *  Shader Code
 *
 * */
export default "\n\nstruct VertexInput {\n    @location(0) pos: vec3f\n}\n\nstruct VertexOutput {\n    @builtin(position) pos: vec4f,\n    @location(0) originalPos: vec3f,\n    @location(1) valExtremes: vec2f,\n}\n\n@group(0) @binding(0) var<uniform> uExtremes: vec4f;\n@group(0) @binding(1) var<uniform> uValueExtremes: vec2f;\n@group(0) @binding(9) var<uniform> uIsInverted: u32;\n\n@vertex\nfn vertexMain(input: VertexInput) -> VertexOutput {\n    var output: VertexOutput;\n    let pos = input.pos;\n\n    let xMin = uExtremes[0];\n    let xMax = uExtremes[1];\n    let yMin = uExtremes[2];\n    let yMax = uExtremes[3];\n\n    var posX: f32;\n    var posY: f32;\n    if (uIsInverted > 0u) {\n        posX = (1.0 - (pos.y - yMin) / (yMax - yMin)) * 2.0 - 1.0;\n        posY = (1.0 - (pos.x - xMin) / (xMax - xMin)) * 2.0 - 1.0;\n    } else {\n        posX = (pos.x - xMin) / (xMax - xMin) * 2.0 - 1.0;\n        posY = (pos.y - yMin) / (yMax - yMin) * 2.0 - 1.0;\n    }\n\n    output.valExtremes = uValueExtremes;\n    output.originalPos = pos.xyz;\n    output.pos = vec4f(posX, posY, 0, 1);\n\n    return output;\n}\n\n// ------------------------------------------------\n\nstruct FragmentInput {\n    @location(0) originalPos: vec3f,\n    @location(1) valExtremes: vec2f\n}\n\n@group(0) @binding(2) var<storage> uColorStops: array<vec4<f32>>;\n@group(0) @binding(3) var<uniform> uColorStopsCount: u32;\n@group(0) @binding(4) var<uniform> uContourInterval: f32;\n@group(0) @binding(5) var<uniform> uContourOffset: f32;\n@group(0) @binding(6) var<uniform> uSmoothColoring: u32;\n@group(0) @binding(7) var<uniform> uContourLineWidth: f32;\n@group(0) @binding(8) var<uniform> uContourLineColor: vec3f;\n\nfn getColor(value: f32) -> vec3<f32> {\n    let stopCount = uColorStopsCount;\n\n    if (stopCount == 0u) {\n        return vec3<f32>(1.0, 1.0, 1.0);\n    }\n\n    for (var i: u32 = 0u; i < stopCount - 1u; i = i + 1u) {\n        if (value < uColorStops[i + 1u].x) {\n            let t = (value - uColorStops[i].x) /\n                (uColorStops[i + 1u].x - uColorStops[i].x);\n\n            return mix(uColorStops[i].yzw, uColorStops[i + 1u].yzw, t);\n        }\n    }\n\n    return uColorStops[stopCount - 1u].yzw;\n}\n\n@fragment\nfn fragmentMain(input: FragmentInput) -> @location(0) vec4f {\n    let val = input.originalPos.z;\n\n    // Contour lines\n    let lineWidth: f32 = uContourLineWidth;\n\n    let val_dx: f32 = dpdx(val);\n    let val_dy: f32 = dpdy(val);\n    let gradient: f32 = length(vec2f(val_dx, val_dy));\n\n    let epsilon: f32 = max(uContourInterval * 1.0e-6, 1.0e-12);\n    let adjustedLineWidth: f32 = lineWidth * gradient + epsilon;\n\n    let adjustedVal: f32 = val - uContourOffset;\n    let valDiv: f32 = adjustedVal / uContourInterval;\n    let valMod: f32 = adjustedVal - uContourInterval * floor(valDiv);\n\n    let lineMask: f32 = smoothstep(0.0, adjustedLineWidth, valMod) * (\n        1.0 - smoothstep(\n            uContourInterval - adjustedLineWidth,\n            uContourInterval,\n            valMod\n        )\n    );\n\n    // Background color\n    let minHeight: f32 = input.valExtremes.x;\n    let maxHeight: f32 = input.valExtremes.y;\n\n    var bgColor: vec3f;\n    if (uSmoothColoring > 0u) {\n        bgColor = getColor((val - minHeight) / (maxHeight - minHeight));\n    } else {\n        let adjustedVal: f32 = val - uContourOffset;\n        let averageValInBand: f32 = floor(\n            adjustedVal / uContourInterval\n        ) * uContourInterval + uContourOffset + uContourInterval / 2.0;\n\n        bgColor = getColor(\n            (averageValInBand - minHeight) /\n            (maxHeight - minHeight)\n        );\n    }\n\n    // Mix\n    var pixelColor = bgColor;\n    if (lineWidth > 0.0) {\n        pixelColor = mix(uContourLineColor, pixelColor, lineMask);\n    }\n\n    return vec4(pixelColor, 1.0);\n}\n\n";
