use super::CodePiece; use crate::impl_code_piece; use glsl::parser::Parse; use glsl::syntax::ShaderStage; use glsl::syntax::TranslationUnit; use glsl::transpiler::glsl::show_translation_unit; pub struct ColorMap(pub ShaderStage); impl ColorMap { pub fn new() -> Self { let raw = { " uniform sampler1D colormap; uniform vec4 colormap_conf; float find_idx(float ratio) { float i = 0; float count = colormap_conf.z - 1.0; float sum = 0.0; while (ratio > sum) { sum += texture(colormap, float(i++) / count).w; } return i / count; } vec4 linear_colormap(float value) { float vmin = colormap_conf.x; float vmax = colormap_conf.y; float count = colormap_conf.z - 1.0; float invalid = colormap_conf.w; if (abs(value - invalid) < 0.0001 || value < vmin || value > vmax) { return vec4(0.0, 0.0, 0.0, 0.0); } float v = clamp((value - vmin), vmin, vmax) / (vmax - vmin); float idx = find_idx(v); vec4 result = texture(colormap, idx); return vec4(result.rgb, 1.0); } " }; Self(ShaderStage::parse(raw).unwrap()) } } impl_code_piece!(ColorMap, 0);