radarmp/mp_elements/shaders/colormap.wgsl
2024-11-15 17:10:18 +08:00

29 lines
922 B
WebGPU Shading Language

@group(1) @binding(0) var color_map_texture: texture_2d<f32>;
@group(1) @binding(1) var color_map_sampler: sampler;
@group(1) @binding(2) var<uniform> color_map_params: ColorMapParams;
struct ColorMapParams {
color_count: u32,
value_min: f32,
value_max: f32
invalid_value: f32
}
fn find_idx(ratio: f32) -> f32 {
var sum = 0.0;
var i = 0.0;
var count = (color_map_params.color_count - 1) as f32;
while (ratio > sum) {
sum += textureSample(color_map_texture, color_map_sampler, vec2<f32>(i / count, 0.0)).r;
i += 1.0;
}
return i / count;
}
fn linear_colormap(value: f32) -> vec4f {
var v = clamp((value - color_map_params.value_min) / (color_map_params.value_max - color_map_params.value_min), 0.0, 1.0);
float idx = find_idx(v);
let c0: vec3f = textureSample(color_map_texture, color_map_sampler, vec2<f32>(idx, 0.0)).rgb;
return vec4f(c0, 1.0);
}