Skip to content

Commit

Permalink
fix #1969, interpolate cmap correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed May 24, 2022
1 parent 1f0be77 commit 1fb2317
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
40 changes: 22 additions & 18 deletions GLMakie/assets/shader/heatmap.frag
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,29 @@ float range_01(float val, float from, float to){
#define ALIASING_CONST 0.70710678118654757
#define M_PI 3.1415926535897932384626433832795

float aastep(float threshold1, float threshold2, float value) {
float afwidth = length(vec2(dFdx(value), dFdy(value))) * ALIASING_CONST;
return smoothstep(threshold1-afwidth, threshold1+afwidth, value)-smoothstep(threshold2-afwidth, threshold2+afwidth, value);
}
float aastep(float threshold1, float value) {
float afwidth = length(vec2(dFdx(value), dFdy(value))) * ALIASING_CONST;
return smoothstep(threshold1-afwidth, threshold1+afwidth, value);
}
void write2framebuffer(vec4 color, uvec2 id);

// It seems texture(color_map, i0) doesn't actually correctly interpolate between the values.
// TODO, further investigate texture, since it's likely not broken, but rather not used correctly.
// Meanwhile, we interpolate manually from the colormap
vec4 get_color_from_cmap(float value, sampler1D color_map, vec2 colorrange) {
float cmin = colorrange.x;
float cmax = colorrange.y;
float i01 = clamp((value - cmin) / (cmax - cmin), 0.0, 1.0);
int len = textureSize(color_map, 0);

float i1len = i01 * (len - 1);
int down = int(floor(i1len));
int up = int(ceil(i1len));
if (down == up) {
return texelFetch(color_map, up, 0);
}
float interp_val = i1len - down;
vec4 downc = texelFetch(color_map, down, 0);
vec4 upc = texelFetch(color_map, up, 0);
return mix(downc, upc, interp_val);
}

vec4 get_color(sampler2D intensity, vec2 uv, vec2 color_norm, sampler1D color_map){
float i = float(getindex(intensity, uv).x);
if (isnan(i)) {
Expand All @@ -43,16 +56,7 @@ vec4 get_color(sampler2D intensity, vec2 uv, vec2 color_norm, sampler1D color_ma
} else if (i > color_norm.y) {
return highclip;
}
i = range_01(i, color_norm.x, color_norm.y);
vec4 color = texture(color_map, clamp(i, 0.0, 1.0));
if(stroke_width > 0.0){
float lines = i * levels;
lines = abs(fract(lines - 0.5));
float half_stroke = stroke_width * 0.5;
lines = aastep(0.5 - half_stroke, 0.5 + half_stroke, lines);
color = mix(color, stroke_color, lines);
}
return color;
return get_color_from_cmap(i, color_map, color_norm);
}

void main(){
Expand Down
23 changes: 22 additions & 1 deletion GLMakie/assets/shader/mesh.frag
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ uniform vec4 highclip;
uniform vec4 lowclip;
uniform vec4 nan_color;

// It seems texture(color_map, i0) doesn't actually correctly interpolate between the values.
// TODO, further investigate texture, since it's likely not broken, but rather not used correctly.
// Meanwhile, we interpolate manually from the colormap
vec4 get_color_from_cmap(float value, sampler1D color_map, vec2 colorrange) {
float cmin = colorrange.x;
float cmax = colorrange.y;
float i01 = clamp((value - cmin) / (cmax - cmin), 0.0, 1.0);
int len = textureSize(color_map, 0);

float i1len = i01 * (len - 1);
int down = int(floor(i1len));
int up = int(ceil(i1len));
if (down == up) {
return texelFetch(color_map, up, 0);
}
float interp_val = i1len - down;
vec4 downc = texelFetch(color_map, down, 0);
vec4 upc = texelFetch(color_map, up, 0);
return mix(downc, upc, interp_val);
}

vec4 get_color(sampler2D intensity, vec2 uv, vec2 color_norm, sampler1D color_map, Nothing matcap){
float i = texture(intensity, uv).x;
if (isnan(i)) {
Expand All @@ -51,7 +72,7 @@ vec4 get_color(sampler2D intensity, vec2 uv, vec2 color_norm, sampler1D color_ma
} else if (i > color_norm.y) {
return highclip;
}
return color_lookup(i, color_map, color_norm);
return get_color_from_cmap(i, color_map, color_norm);
}

vec4 matcap_color(sampler2D matcap){
Expand Down
5 changes: 5 additions & 0 deletions GLMakie/src/drawing_primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ function draw_atomic(screen::GLScreen, scene::Scene, x::Image)
end
gl_attributes[:color] = x[3]
gl_attributes[:shading] = false
if gl_attributes[:color_map][] isa Vector
cmap = pop!(gl_attributes, :color_map)
gl_attributes[:color_map] = Texture(cmap; mipmap=true, minfilter=:linear)
end

connect_camera!(gl_attributes, scene.camera)
return mesh_inner(mesh, transform_func_obs(x), gl_attributes)
end
Expand Down

0 comments on commit 1fb2317

Please sign in to comment.