From bb43dff935889f3efa112dbe21528b709b3f434d Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Wed, 26 Apr 2017 20:55:14 +0200 Subject: [PATCH] Add 3D version of dashed line shader. This is actually nearly same code as 2D version, maybe we can deduplicate that later? --- source/blender/gpu/CMakeLists.txt | 2 ++ source/blender/gpu/GPU_shader.h | 1 + source/blender/gpu/intern/gpu_shader.c | 4 +++ .../gpu_shader_3D_line_dashed_frag.glsl | 24 ++++++++++++++ .../gpu_shader_3D_line_dashed_vert.glsl | 32 +++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 source/blender/gpu/shaders/gpu_shader_3D_line_dashed_frag.glsl create mode 100644 source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index ea142361d81..73557db147b 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -150,6 +150,8 @@ data_to_c_simple(shaders/gpu_shader_3D_image_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_normal_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_flat_color_vert.glsl SRC) +data_to_c_simple(shaders/gpu_shader_3D_line_dashed_vert.glsl SRC) +data_to_c_simple(shaders/gpu_shader_3D_line_dashed_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_smooth_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_normal_smooth_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_smooth_color_frag.glsl SRC) diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h index 7e4524142d3..e88bffb98ed 100644 --- a/source/blender/gpu/GPU_shader.h +++ b/source/blender/gpu/GPU_shader.h @@ -143,6 +143,7 @@ typedef enum GPUBuiltinShader { GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR, /* lines */ GPU_SHADER_2D_LINE_DASHED_COLOR, + GPU_SHADER_3D_LINE_DASHED_COLOR, /* lamp drawing */ GPU_SHADER_3D_GROUNDPOINT, GPU_SHADER_3D_GROUNDLINE, diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c index 33be3b74aff..ca92175cb3d 100644 --- a/source/blender/gpu/intern/gpu_shader.c +++ b/source/blender/gpu/intern/gpu_shader.c @@ -109,6 +109,8 @@ extern char datatoc_gpu_shader_2D_point_uniform_size_varying_color_outline_aa_ve extern char datatoc_gpu_shader_2D_line_dashed_vert_glsl[]; extern char datatoc_gpu_shader_2D_line_dashed_frag_glsl[]; +extern char datatoc_gpu_shader_3D_line_dashed_vert_glsl[]; +extern char datatoc_gpu_shader_3D_line_dashed_frag_glsl[]; extern char datatoc_gpu_shader_edges_front_back_persp_vert_glsl[]; extern char datatoc_gpu_shader_edges_front_back_persp_geom_glsl[]; @@ -709,6 +711,8 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader) [GPU_SHADER_2D_LINE_DASHED_COLOR] = { datatoc_gpu_shader_2D_line_dashed_vert_glsl, datatoc_gpu_shader_2D_line_dashed_frag_glsl }, + [GPU_SHADER_3D_LINE_DASHED_COLOR] = { datatoc_gpu_shader_3D_line_dashed_vert_glsl, + datatoc_gpu_shader_3D_line_dashed_frag_glsl }, [GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR] = { datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl, diff --git a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_frag.glsl b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_frag.glsl new file mode 100644 index 00000000000..2c13284cf34 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_frag.glsl @@ -0,0 +1,24 @@ + +/* Same code as 2D version actually, maybe we should deduplicate that? */ + +#if __VERSION__ == 120 + noperspective varying float distance_along_line; + #define fragColor gl_FragColor +#else + noperspective in float distance_along_line; + out vec4 fragColor; +#endif + +uniform float dash_width; +uniform float dash_width_on; +uniform vec4 color1; +uniform vec4 color2; + +void main() +{ + if (mod(distance_along_line, dash_width) <= dash_width_on) { + fragColor = color1; + } else { + fragColor = color2; + } +} diff --git a/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl new file mode 100644 index 00000000000..f3e91b6cc03 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_3D_line_dashed_vert.glsl @@ -0,0 +1,32 @@ + +/* Note: nearly the same code as for 2D version... Maybe we could deduplicate? */ + +uniform mat4 ModelViewProjectionMatrix; +uniform vec2 viewport_size; + +#if __VERSION__ == 120 + attribute vec3 pos; + attribute vec3 line_origin; // = pos for one vertex of the line + noperspective varying float distance_along_line; +#else + in vec3 pos; + in vec3 line_origin; + noperspective out float distance_along_line; +#endif + +void main() +{ + gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0); + + vec4 point = ModelViewProjectionMatrix * vec4(line_origin, 1.0); + vec2 ref_point = (point.xy / point.w) * 0.5 + 0.5; // <- device coordinates in [0..1] range. + ref_point = ref_point * viewport_size; // <- fragment coordinates. + + vec2 curr_point = (gl_Position.xy / gl_Position.w) * 0.5 + 0.5; // <- device coordinates in [0..1] range. + curr_point = curr_point * viewport_size; // <- fragment coordinates. + + distance_along_line = distance(ref_point, curr_point); + + /* Note: we could also use similar approach as diag_stripes_frag, but this would give us dashed 'anchored' + * to the screen, and not to one end of the line... */ +}