Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clipping options to GLMakie #2783

Closed
wants to merge 12 commits into from
Closed
28 changes: 27 additions & 1 deletion GLMakie/assets/shader/dots.vert
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,37 @@ void colorize(sampler1D color, float intensity, vec2 color_norm){
vec4 _position(vec3 p){return vec4(p,1);}
vec4 _position(vec2 p){return vec4(p,0,1);}

struct Nothing{
bool _;
};

struct WorldAxisLimits{
vec3 min, max;
};

{{clip_planes_type}} clip_planes;

void set_clip(Nothing planes, vec4 world_pos){ return; }
void set_clip(WorldAxisLimits planes, vec4 world_pos)
{
// inside positive, outside negative?
vec3 min_dist = world_pos.xyz - planes.min;
vec3 max_dist = planes.max - world_pos.xyz;
gl_ClipDistance[0] = min_dist[0];
gl_ClipDistance[1] = max_dist[0];
gl_ClipDistance[2] = min_dist[1];
gl_ClipDistance[3] = max_dist[1];
gl_ClipDistance[4] = min_dist[2];
gl_ClipDistance[5] = max_dist[2];
}

uniform mat4 projectionview, model;

void main(){
colorize(color_map, color, color_norm);
o_objectid = uvec2(objectid, gl_VertexID+1);
gl_Position = projectionview * model * _position(vertex);
vec4 world_pos = model * _position(vertex);
gl_Position = projectionview * world_pos;
gl_Position.z += gl_Position.w * depth_shift;
set_clip(clip_planes, world_pos);
}
30 changes: 29 additions & 1 deletion GLMakie/assets/shader/heatmap.vert
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ ivec2 ind2sub(ivec2 dim, int linearindex){
return ivec2(linearindex % dim.x, linearindex / dim.x);
}


struct Nothing{
bool _;
};

struct WorldAxisLimits{
vec3 min, max;
};

{{clip_planes_type}} clip_planes;

void set_clip(Nothing planes, vec4 world_pos){ return; }
void set_clip(WorldAxisLimits planes, vec4 world_pos)
{
// inside positive, outside negative?
vec3 min_dist = world_pos.xyz - planes.min;
vec3 max_dist = planes.max - world_pos.xyz;
gl_ClipDistance[0] = min_dist[0];
gl_ClipDistance[1] = max_dist[0];
gl_ClipDistance[2] = min_dist[1];
gl_ClipDistance[3] = max_dist[1];
gl_ClipDistance[4] = min_dist[2];
gl_ClipDistance[5] = max_dist[2];
}

void main(){
//Outputs for ssao, which we don't use for 2d shaders like heatmap/image
o_view_pos = vec3(0);
Expand All @@ -38,6 +63,9 @@ void main(){
float x = texelFetch(position_x, index2D.x, 0).x;
float y = texelFetch(position_y, index2D.y, 0).x;

gl_Position = projection * view * model * vec4(x, y, 0, 1);
vec4 world_pos = model * vec4(x, y, 0, 1);
set_clip(clip_planes, world_pos);

gl_Position = projection * view * world_pos;
gl_Position.z += gl_Position.w * depth_shift;
}
16 changes: 16 additions & 0 deletions GLMakie/assets/shader/line_segment.geom
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ struct Nothing{ //Nothing type, to encode if some variable doesn't contain any d
bool _; //empty structs are not allowed
};

struct WorldAxisLimits{
vec3 min, max;
};

layout(lines) in;
layout(triangle_strip, max_vertices = 4) out;

uniform vec2 resolution;
uniform float pattern_length;
{{pattern_type}} pattern;
{{clip_planes_type}} clip_planes;

in vec4 g_color[];
in uvec2 g_id[];
Expand All @@ -35,6 +40,16 @@ vec3 screen_space(vec4 vertex)
return vec3(vertex.xy * resolution, vertex.z) / vertex.w;
}

void set_clip(Nothing planes, int idx){ return; };
void set_clip(WorldAxisLimits planes, int idx){
gl_ClipDistance[0] = gl_in[idx].gl_ClipDistance[0];
gl_ClipDistance[1] = gl_in[idx].gl_ClipDistance[1];
gl_ClipDistance[2] = gl_in[idx].gl_ClipDistance[2];
gl_ClipDistance[3] = gl_in[idx].gl_ClipDistance[3];
gl_ClipDistance[4] = gl_in[idx].gl_ClipDistance[4];
gl_ClipDistance[5] = gl_in[idx].gl_ClipDistance[5];
};

void emit_vertex(vec3 position, vec2 uv, int index)
{
vec4 inpos = gl_in[index].gl_Position;
Expand All @@ -43,6 +58,7 @@ void emit_vertex(vec3 position, vec2 uv, int index)
gl_Position = vec4((position.xy / resolution), position.z, 1.0);
f_id = g_id[index];
f_thickness = g_thickness[index];
set_clip(clip_planes, index);
EmitVertex();
}

Expand Down
11 changes: 10 additions & 1 deletion GLMakie/assets/shader/line_segment.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ struct Nothing{ //Nothing type, to encode if some variable doesn't contain any d
bool _; //empty structs are not allowed
};

struct WorldAxisLimits{
vec3 min, max;
};

in float lastlen;
{{vertex_type}} vertex;
{{thickness_type}} thickness;
Expand All @@ -16,6 +20,7 @@ in float lastlen;
uniform mat4 projectionview, model;
uniform uint objectid;
uniform float depth_shift;
{{clip_planes_type}} clip_planes;

out uvec2 g_id;
out vec4 g_color;
Expand All @@ -36,13 +41,17 @@ vec4 to_color(float color, sampler1D color_map, vec2 color_norm, int index){
return color_lookup(color, color_map, color_norm);
}

void set_clip(Nothing planes, vec4 world_pos);
void set_clip(WorldAxisLimits planes, vec4 world_pos);

void main()
{
int index = gl_VertexID;
g_id = uvec2(objectid, index+1);
g_color = to_color(color, color_map, color_norm, index);
g_thickness = thickness;
gl_Position = projectionview * model * to_vec4(vertex);
vec4 world_pos = model * to_vec4(vertex);
set_clip(clip_planes, world_pos);
gl_Position = projectionview * world_pos;
gl_Position.z += gl_Position.w * depth_shift;
}
26 changes: 22 additions & 4 deletions GLMakie/assets/shader/lines.geom
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ struct Nothing{ //Nothing type, to encode if some variable doesn't contain any d
bool _; //empty structs are not allowed
};

struct WorldAxisLimits{
vec3 min, max;
};

{{define_fast_path}}

layout(lines_adjacency) in;
Expand All @@ -29,6 +33,7 @@ out vec3 o_normal;
uniform vec2 resolution;
uniform float pattern_length;
uniform sampler1D pattern_sections;
{{clip_planes_type}} clip_planes;

float px2uv = 0.5 / pattern_length;

Expand All @@ -41,6 +46,18 @@ vec3 screen_space(vec4 vertex)
return vec3(vertex.xy * resolution, vertex.z) / vertex.w;
}

void set_clip(Nothing planes, int idx){ return; };
void set_clip(WorldAxisLimits planes, int idx){
gl_ClipDistance[0] = gl_in[idx].gl_ClipDistance[0];
gl_ClipDistance[1] = gl_in[idx].gl_ClipDistance[1];
gl_ClipDistance[2] = gl_in[idx].gl_ClipDistance[2];
gl_ClipDistance[3] = gl_in[idx].gl_ClipDistance[3];
gl_ClipDistance[4] = gl_in[idx].gl_ClipDistance[4];
gl_ClipDistance[5] = gl_in[idx].gl_ClipDistance[5];
};



////////////////////////////////////////////////////////////////////////////////
/// Emit Vertex Methods
////////////////////////////////////////////////////////////////////////////////
Expand All @@ -56,6 +73,7 @@ void emit_vertex(vec3 position, vec2 uv, int index)
gl_Position = vec4((position.xy / resolution), position.z, 1.0);
f_id = g_id[index];
f_thickness = g_thickness[index];
set_clip(clip_planes, index);
EmitVertex();
}

Expand Down Expand Up @@ -315,10 +333,10 @@ void draw_patterned_line(bool isvalid[4])

// get the four vertices passed to the shader
// without FAST_PATH the conversions happen on the CPU
vec3 p0 = gl_in[0].gl_Position.xyz; // start of previous segment
vec3 p1 = gl_in[1].gl_Position.xyz; // end of previous segment, start of current segment
vec3 p2 = gl_in[2].gl_Position.xyz; // end of current segment, start of next segment
vec3 p3 = gl_in[3].gl_Position.xyz; // end of next segment
vec3 p0 = screen_space(gl_in[0].gl_Position); // start of previous segment
vec3 p1 = screen_space(gl_in[1].gl_Position); // end of previous segment, start of current segment
vec3 p2 = screen_space(gl_in[2].gl_Position); // end of current segment, start of next segment
vec3 p3 = screen_space(gl_in[3].gl_Position); // end of next segment

// linewidth with padding for anti aliasing
float thickness_aa1 = g_thickness[1] + AA_THICKNESS;
Expand Down
19 changes: 13 additions & 6 deletions GLMakie/assets/shader/lines.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ struct Nothing{ //Nothing type, to encode if some variable doesn't contain any d
bool _; //empty structs are not allowed
};

struct WorldAxisLimits{
vec3 min, max;
};

{{define_fast_path}}

{{vertex_type}} vertex;
Expand All @@ -17,6 +21,10 @@ in float lastlen;
{{intensity_type}} intensity;
{{color_norm_type}} color_norm;
{{thickness_type}} thickness;
{{clip_planes_type}} clip_planes;

void set_clip(Nothing planes, vec4 world_pos);
void set_clip(WorldAxisLimits planes, vec4 world_pos);

vec4 _color(vec3 color, Nothing intensity, Nothing color_map, Nothing color_norm, int index, int len);
vec4 _color(vec4 color, Nothing intensity, Nothing color_map, Nothing color_norm, int index, int len);
Expand All @@ -26,6 +34,7 @@ vec4 _color(Nothing color, sampler1D intensity, sampler1D color_map, vec2 color_
uniform mat4 projectionview, model;
uniform uint objectid;
uniform int total_length;
uniform float depth_shift;

out uvec2 g_id;
out vec4 g_color;
Expand All @@ -42,7 +51,6 @@ vec4 to_vec4(vec2 v){return vec4(v, 0, 1);}
int get_valid_vertex(float se){return int(se);}
int get_valid_vertex(Nothing se){return 1;}

uniform float depth_shift;

void main()
{
Expand All @@ -53,10 +61,9 @@ void main()
g_thickness = thickness;

g_color = _color(color, intensity, color_map, color_norm, index, total_length);
#ifdef FAST_PATH
gl_Position = projectionview * model * to_vec4(vertex);
#else
gl_Position = to_vec4(vertex);
#endif

vec4 world_pos = model * to_vec4(vertex);
set_clip(clip_planes, world_pos);
gl_Position = projectionview * world_pos;
gl_Position.z += gl_Position.w * depth_shift;
}
2 changes: 2 additions & 0 deletions GLMakie/assets/shader/sprites.geom
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ in vec3 g_position[];
in vec4 g_rotation[];
in vec4 g_offset_width[];
in uvec2 g_id[];
in float g_clipDistance[];

flat out int f_primitive_index;
flat out float f_viewport_from_u_scale;
Expand Down Expand Up @@ -80,6 +81,7 @@ void emit_vertex(vec4 vertex, vec2 uv)
{
gl_Position = vertex;
gl_Position.z += gl_Position.w * depth_shift;
gl_ClipDistance[0] = g_clipDistance[0];
f_uv = uv;
f_uv_texture_bbox = g_uv_texture_bbox[0];
f_primitive_index = g_primitive_index[0];
Expand Down
20 changes: 19 additions & 1 deletion GLMakie/assets/shader/sprites.vert
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ struct Grid3D{
ivec3 dims;
};

struct WorldAxisLimits{
vec3 min, max;
};

{{clip_planes_type}} clip_planes;

// pos = inside, 0 border, neg = outside
float min_clip(Nothing planes, vec4 pos) { return 1.0; }
float min_clip(WorldAxisLimits planes, vec4 world_pos){
vec3 min_dist = world_pos.xyz - planes.min;
vec3 max_dist = planes.max - world_pos.xyz;
min_dist = min(min_dist, max_dist);
return min(min_dist.x, min(min_dist.y, min_dist.z));
}

{{uv_offset_width_type}} uv_offset_width;
//{{uv_x_type}} uv_width;
{{position_type}} position;
Expand Down Expand Up @@ -86,6 +101,7 @@ out vec4 g_rotation;
out vec4 g_color;
out vec4 g_stroke_color;
out vec4 g_glow_color;
out float g_clipDistance;

vec4 to_vec4(vec3 x){return vec4(x, 1.0);}
vec4 to_vec4(vec4 x){return x;}
Expand All @@ -95,7 +111,9 @@ void main(){
g_primitive_index = index;
vec3 pos;
{{position_calc}}
vec4 p = preprojection * model * vec4(pos, 1);
vec4 world_pos = model * vec4(pos, 1);
g_clipDistance = min_clip(clip_planes, world_pos);
vec4 p = preprojection * world_pos;
g_position = p.xyz / p.w + mat3(model) * marker_offset;
g_offset_width.xy = quad_offset.xy;
g_offset_width.zw = scale.xy;
Expand Down
21 changes: 21 additions & 0 deletions GLMakie/assets/shader/util.vert
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,25 @@ uniform vec3 lightposition;
uniform vec3 eyeposition;
uniform float depth_shift;

struct WorldAxisLimits{
vec3 min, max;
};

{{clip_planes_type}} clip_planes;

void set_clip(Nothing planes, vec4 world_pos){ return; }
void set_clip(WorldAxisLimits planes, vec4 world_pos)
{
// inside positive, outside negative?
vec3 min_dist = world_pos.xyz - planes.min;
vec3 max_dist = planes.max - world_pos.xyz;
gl_ClipDistance[0] = min_dist[0];
gl_ClipDistance[1] = max_dist[0];
gl_ClipDistance[2] = min_dist[1];
gl_ClipDistance[3] = max_dist[1];
gl_ClipDistance[4] = min_dist[2];
gl_ClipDistance[5] = max_dist[2];
}

void render(vec4 position_world, vec3 normal, mat4 view, mat4 projection, vec3 lightposition)
{
Expand All @@ -235,6 +254,8 @@ void render(vec4 position_world, vec3 normal, mat4 view, mat4 projection, vec3 l
// (by definition `view * eyeposition = 0`)
o_camdir = normalize(-view_pos).xyz;
o_view_pos = view_pos.xyz / view_pos.w;

set_clip(clip_planes, position_world);
}

//
Expand Down
Loading