Skip to content

Commit 2b7a055

Browse files
committed
🐛 fix: improve point and line visibility for Vulkan backend - Fixes #517
1 parent 3fb273c commit 2b7a055

File tree

9 files changed

+355
-49
lines changed

9 files changed

+355
-49
lines changed

model/arc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,15 @@ def update(self):
102102
mat = self.wp.matrix_basis @ mat_local
103103
coords = [(mat @ Vector((*co, 0)))[:] for co in coords]
104104

105-
kwargs = {"pos": coords}
106-
self._batch = batch_for_shader(self._shader, "LINE_STRIP", kwargs)
105+
from ..utilities.draw import draw_thick_line_strip_3d, draw_thick_dashed_line_strip_3d
106+
if self.is_dashed():
107+
thick_coords, indices = draw_thick_dashed_line_strip_3d(coords, self.line_width)
108+
else:
109+
thick_coords, indices = draw_thick_line_strip_3d(coords, self.line_width)
110+
kwargs = {"pos": thick_coords}
111+
self._batch = batch_for_shader(self._shader, "TRIS", kwargs, indices=indices)
112+
113+
107114
self.is_dirty = False
108115

109116
def create_slvs_data(self, solvesys, group=Solver.group_fixed):

model/base_entity.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,11 @@ def is_dirty(self, value: bool):
6363

6464
@property
6565
def _shader(self):
66-
if self.is_point():
67-
return Shaders.uniform_color_3d()
68-
return Shaders.uniform_color_line_3d()
66+
return Shaders.uniform_color_3d()
6967

7068
@property
7169
def _id_shader(self):
72-
if self.is_point():
73-
return Shaders.id_shader_3d()
74-
return Shaders.id_line_3d()
70+
return Shaders.id_shader_3d()
7571

7672
@property
7773
def point_size(self):
@@ -222,18 +218,18 @@ def draw(self, context):
222218
shader = self._shader
223219
shader.bind()
224220

221+
# Enable required GPU features for Vulkan compatibility
222+
try:
223+
gpu.state.program_point_size_set(True)
224+
except (AttributeError, RuntimeError):
225+
pass # Not available in older Blender versions or when GPU context unavailable
226+
225227
gpu.state.blend_set("ALPHA")
226228
gpu.state.point_size_set(self.point_size)
227229

228230
col = self.color(context)
229231
shader.uniform_float("color", col)
230232

231-
if not self.is_point():
232-
shader.uniform_bool("dashed", (self.is_dashed(),))
233-
shader.uniform_float("dash_width", 0.05)
234-
shader.uniform_float("dash_factor", 0.3)
235-
gpu.state.line_width_set(self.line_width)
236-
237233
batch.draw(shader)
238234
gpu.shader.unbind()
239235
self.restore_opengl_defaults()
@@ -250,14 +246,15 @@ def draw_id(self, context):
250246
shader = self._id_shader
251247
shader.bind()
252248

249+
# Enable required GPU features for Vulkan compatibility
250+
try:
251+
gpu.state.program_point_size_set(True)
252+
except AttributeError:
253+
pass # Not available in older Blender versions
254+
253255
gpu.state.point_size_set(self.point_size_select)
254256

255257
shader.uniform_float("color", (*index_to_rgb(self.slvs_index), 1.0))
256-
if not self.is_point():
257-
# viewport = [context.area.width, context.area.height]
258-
# shader.uniform_float("Viewport", viewport)
259-
shader.uniform_bool("dashed", (False,))
260-
gpu.state.line_width_set(self.line_width_select)
261258

262259
batch.draw(shader)
263260
gpu.shader.unbind()

model/circle.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .utilities import slvs_entity_pointer, tag_update
1818
from .constants import CURVE_RESOLUTION
1919
from ..utilities.constants import HALF_TURN, FULL_TURN
20-
from ..utilities.draw import coords_arc_2d
20+
from ..utilities.draw import coords_arc_2d, draw_thick_line_strip_3d, draw_thick_dashed_line_strip_3d
2121
from .utilities import (
2222
get_bezier_curve_midpoint_positions,
2323
create_bezier_curve,
@@ -76,8 +76,13 @@ def update(self):
7676
mat = self.wp.matrix_basis @ mat_local
7777
coords = [(mat @ Vector((*co, 0)))[:] for co in coords]
7878

79-
kwargs = {"pos": coords}
80-
self._batch = batch_for_shader(self._shader, "LINE_STRIP", kwargs)
79+
if self.is_dashed():
80+
thick_coords, indices = draw_thick_dashed_line_strip_3d(coords, self.line_width)
81+
else:
82+
thick_coords, indices = draw_thick_line_strip_3d(coords, self.line_width)
83+
kwargs = {"pos": thick_coords}
84+
self._batch = batch_for_shader(self._shader, "TRIS", kwargs, indices=indices)
85+
8186
self.is_dirty = False
8287

8388
def create_slvs_data(self, solvesys, group=Solver.group_fixed):

model/line_2d.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .base_entity import Entity2D
1515
from .utilities import slvs_entity_pointer, get_connection_point, round_v
1616
from ..utilities.geometry import nearest_point_line_line
17+
from ..utilities.draw import draw_thick_line_3d, draw_thick_dashed_line_3d
1718

1819

1920
logger = logging.getLogger(__name__)
@@ -52,10 +53,14 @@ def update(self):
5253
return
5354

5455
p1, p2 = self.p1.location, self.p2.location
55-
coords = (p1, p2)
5656

57+
if self.is_dashed():
58+
coords, indices = draw_thick_dashed_line_3d(p1, p2, self.line_width)
59+
else:
60+
coords, indices = draw_thick_line_3d(p1, p2, self.line_width)
5761
kwargs = {"pos": coords}
58-
self._batch = batch_for_shader(self._shader, "LINES", kwargs)
62+
self._batch = batch_for_shader(self._shader, "TRIS", kwargs, indices=indices)
63+
5964
self.is_dirty = False
6065

6166
def create_slvs_data(self, solvesys, group=Solver.group_fixed):

model/line_3d.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .base_entity import SlvsGenericEntity
1111
from .utilities import slvs_entity_pointer
1212
from ..utilities.geometry import nearest_point_line_line
13+
from ..utilities.draw import draw_thick_line_3d, draw_thick_dashed_line_3d
1314

1415
logger = logging.getLogger(__name__)
1516

@@ -45,10 +46,13 @@ def update(self):
4546
return
4647

4748
p1, p2 = self.p1.location, self.p2.location
48-
coords = (p1, p2)
4949

50+
if self.is_dashed():
51+
coords, indices = draw_thick_dashed_line_3d(p1, p2, self.line_width)
52+
else:
53+
coords, indices = draw_thick_line_3d(p1, p2, self.line_width)
5054
kwargs = {"pos": coords}
51-
self._batch = batch_for_shader(self._shader, "LINES", kwargs)
55+
self._batch = batch_for_shader(self._shader, "TRIS", kwargs, indices=indices)
5256

5357
self.is_dirty = False
5458

model/point_2d.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from mathutils import Matrix, Vector
99
from bpy.utils import register_classes_factory
1010

11-
from ..utilities.draw import draw_rect_2d
11+
from ..utilities.draw import draw_rect_2d, draw_thick_point_3d
1212
from ..solver import Solver
1313
from .base_entity import SlvsGenericEntity
1414
from .base_entity import Entity2D
@@ -27,16 +27,10 @@ def update(self):
2727
if bpy.app.background:
2828
return
2929

30-
u, v = self.co
31-
mat_local = Matrix.Translation(Vector((u, v, 0)))
30+
coords, indices = draw_thick_point_3d(self.location, self.point_size)
31+
kwargs = {"pos": coords}
32+
self._batch = batch_for_shader(self._shader, "TRIS", kwargs, indices=indices)
3233

33-
mat = self.wp.matrix_basis @ mat_local
34-
size = 0.1
35-
coords = draw_rect_2d(0, 0, size, size)
36-
coords = [(mat @ Vector(co))[:] for co in coords]
37-
indices = ((0, 1, 2), (0, 2, 3))
38-
pos = self.location
39-
self._batch = batch_for_shader(self._shader, "POINTS", {"pos": (pos[:],)})
4034
self.is_dirty = False
4135

4236
@property

model/point_3d.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from gpu_extras.batch import batch_for_shader
77
from bpy.utils import register_classes_factory
88

9-
from ..utilities.draw import draw_cube_3d
9+
from ..utilities.draw import draw_cube_3d, draw_thick_point_3d
1010
from ..solver import Solver
1111
from .base_entity import SlvsGenericEntity
1212

@@ -23,10 +23,10 @@ def update(self):
2323
if bpy.app.background:
2424
return
2525

26-
coords, indices = draw_cube_3d(*self.location, 0.05)
27-
self._batch = batch_for_shader(
28-
self._shader, "POINTS", {"pos": (self.location[:],)}
29-
)
26+
coords, indices = draw_thick_point_3d(self.location, self.point_size)
27+
kwargs = {"pos": coords}
28+
self._batch = batch_for_shader(self._shader, "TRIS", kwargs, indices=indices)
29+
3030
self.is_dirty = False
3131

3232
# TODO: maybe rename -> pivot_point, midpoint

model/workplane.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from ..declarations import Operators
1212
from .. import global_data
13-
from ..utilities.draw import draw_rect_2d
13+
from ..utilities.draw import draw_rect_2d, draw_thick_line_3d, draw_thick_dashed_line_3d
1414
from ..shaders import Shaders
1515
from ..utilities import preferences
1616
from ..solver import Solver
@@ -44,15 +44,35 @@ def update(self):
4444
if bpy.app.background:
4545
return
4646

47-
p1, nm = self.p1, self.nm
47+
# Create thick line geometry for workplane edges (Vulkan compatible)
48+
rect_coords = draw_rect_2d(0, 0, self.size, self.size)
49+
rect_coords = [Vector(co) for co in rect_coords]
4850

49-
coords = draw_rect_2d(0, 0, self.size, self.size)
50-
coords = [(Vector(co))[:] for co in coords]
51+
# Use a much smaller width for workplane edges (accounting for view distance scaling)
52+
line_width = 0.2
53+
54+
all_coords = []
55+
all_indices = []
56+
vertex_offset = 0
57+
58+
# Draw each edge as thick line
59+
edges = [(0, 1), (1, 2), (2, 3), (3, 0)]
60+
for start_idx, end_idx in edges:
61+
start_point = rect_coords[start_idx]
62+
end_point = rect_coords[end_idx]
63+
64+
line_coords, line_indices = draw_thick_line_3d(start_point, end_point, line_width)
65+
all_coords.extend(line_coords)
66+
67+
# Adjust indices for current vertex offset
68+
adjusted_indices = [(idx[0] + vertex_offset, idx[1] + vertex_offset, idx[2] + vertex_offset) for idx in line_indices]
69+
all_indices.extend(adjusted_indices)
70+
vertex_offset += len(line_coords)
5171

52-
indices = ((0, 1), (1, 2), (2, 3), (3, 0))
5372
self._batch = batch_for_shader(
54-
self._shader, "LINES", {"pos": coords}, indices=indices
73+
self._shader, "TRIS", {"pos": all_coords}, indices=all_indices
5574
)
75+
5676
self.is_dirty = False
5777

5878
# NOTE: probably better to avoid overwriting draw func..

0 commit comments

Comments
 (0)