Skip to content

Commit 00620fb

Browse files
authored
Add dashed lines for Vulkan
- Add new technique for doing dashed lines in Vulkan. - Note: We can't control dashed line width in Vulkan like we could with the old technique - Note: We do not support the incomplete, experimental Vulkan backend in Blender 4.3 and 4.4. Proper Vulkan support begins with Blender 4.5.
1 parent a0163e0 commit 00620fb

File tree

1 file changed

+63
-34
lines changed

1 file changed

+63
-34
lines changed

mesh_merge_tool/shaders.py

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# Blender versions higher than 4.0 don't support 3D_UNIFORM_COLOR but versions below 3.4 require it
1111
# Blender versions below 4.5 don't support POINT_UNIFORM_COLOR
12-
if bpy.app.version[0] == 5:
12+
if bpy.app.version[0] >= 5:
1313
line_type = 'POLYLINE_UNIFORM_COLOR'
1414
point_type = 'POINT_UNIFORM_COLOR'
1515
elif bpy.app.version[0] == 4:
@@ -31,39 +31,70 @@
3131
except:
3232
# Assume Opengl for older versions
3333
backend = 'OPENGL'
34-
# Pray that Metal works with Opengl code
35-
if backend != 'VULKAN':
36-
backend = 'OPENGL'
37-
3834

39-
vertex_shader = '''
40-
uniform mat4 u_ViewProjectionMatrix;
35+
# Dashed lines
36+
# gpu.types.GPUShader is deprecated on Vulkan in 4.5 and completely removed in 5.0
37+
if backend == 'VULKAN' or bpy.app.version[0] >= 5:
38+
vert_out = gpu.types.GPUStageInterfaceInfo("my_interface")
39+
vert_out.smooth('FLOAT', "v_ArcLength")
40+
41+
shader_info = gpu.types.GPUShaderCreateInfo()
42+
shader_info.push_constant('MAT4', "u_ViewProjectionMatrix")
43+
shader_info.push_constant('FLOAT', "u_Scale")
44+
shader_info.push_constant('VEC4', "u_Color")
45+
shader_info.vertex_in(0, 'VEC3', "position")
46+
shader_info.vertex_in(1, 'FLOAT', "arcLength")
47+
shader_info.vertex_out(vert_out)
48+
shader_info.fragment_out(0, 'VEC4', "FragColor")
49+
50+
shader_info.vertex_source(
51+
"void main()"
52+
"{"
53+
" v_ArcLength = arcLength;"
54+
" gl_Position = u_ViewProjectionMatrix * vec4(position, 1.0f);"
55+
"}"
56+
)
57+
58+
shader_info.fragment_source(
59+
"void main()"
60+
"{"
61+
" if (step(sin(v_ArcLength * u_Scale), 0.5) == 1) discard;"
62+
" FragColor = vec4(u_Color);"
63+
"}"
64+
)
65+
66+
shader_v = gpu.shader.create_from_info(shader_info)
67+
del vert_out
68+
del shader_info
69+
else:
70+
vertex_shader = '''
71+
uniform mat4 u_ViewProjectionMatrix;
4172
42-
in vec3 position;
43-
in float arcLength;
73+
in vec3 position;
74+
in float arcLength;
4475
45-
out float v_ArcLength;
76+
out float v_ArcLength;
4677
47-
void main()
48-
{
49-
v_ArcLength = arcLength;
50-
gl_Position = u_ViewProjectionMatrix * vec4(position, 1.0f);
51-
}
52-
'''
78+
void main()
79+
{
80+
v_ArcLength = arcLength;
81+
gl_Position = u_ViewProjectionMatrix * vec4(position, 1.0f);
82+
}
83+
'''
5384

54-
fragment_shader = '''
55-
uniform float u_Scale;
56-
uniform vec4 u_Color;
85+
fragment_shader = '''
86+
uniform float u_Scale;
87+
uniform vec4 u_Color;
5788
58-
in float v_ArcLength;
59-
out vec4 FragColor;
89+
in float v_ArcLength;
90+
out vec4 FragColor;
6091
61-
void main()
62-
{
63-
if (step(sin(v_ArcLength * u_Scale), 0.5) == 1) discard;
64-
FragColor = vec4(u_Color);
65-
}
66-
'''
92+
void main()
93+
{
94+
if (step(sin(v_ArcLength * u_Scale), 0.5) == 1) discard;
95+
FragColor = vec4(u_Color);
96+
}
97+
'''
6798

6899

69100
class DrawPoint():
@@ -190,7 +221,10 @@ def draw_callback_3d(self, context):
190221
tool_line = DrawLine()
191222
tool_line.add(shader_line, line_coords, self.prefs.line_width, self.prefs.line_color)
192223
else:
193-
shader_dashed = gpu.types.GPUShader(vertex_shader, fragment_shader)
224+
if backend == 'VULKAN' or bpy.app.version[0] >= 5:
225+
shader_dashed = shader_v
226+
else:
227+
shader_dashed = gpu.types.GPUShader(vertex_shader, fragment_shader)
194228
tool_line = DrawLineDashed()
195229
tool_line.add(shader_dashed, line_coords, self.prefs.line_width, self.prefs.line_color)
196230

@@ -213,11 +247,6 @@ def draw_callback_3d(self, context):
213247
end_point.add(shader_point, self.end_comp_transformed, self.prefs.point_size, self.prefs.start_color)
214248
else:
215249
end_point.add(shader_point, self.end_comp_transformed, self.prefs.point_size, self.prefs.end_color)
216-
# if self.merge_location in ('FIRST', 'CENTER'): # There has to be a better way of doing this.
217-
# point_color = self.prefs.start_color
218-
# else:
219-
# point_color = self.prefs.end_color
220-
# end_point.add(shader_point, self.end_comp_transformed, self.prefs.point_size, point_color)
221250

222251
# Middle point
223252
if self.merge_location == 'CENTER':
@@ -260,4 +289,4 @@ def draw_callback_2d(self, context):
260289
# Have to add 1 for some reason in order to get proper number of segments.
261290
# This could potentially also be a ratio with the radius.
262291
circ_segments = 8 + 1
263-
draw_circle_2d(self.m_coord, self.prefs.circ_color, self.prefs.circ_radius, segments=circ_segments)
292+
draw_circle_2d(self.m_coord, self.prefs.circ_color, self.prefs.circ_radius, segments=circ_segments)

0 commit comments

Comments
 (0)