Skip to content

Commit

Permalink
Fix regression with Clip Planes in Snap Utilities Line
Browse files Browse the repository at this point in the history
A while ago shaders started to use uniform buffers to set clip planes.

The python codes also need to be updated.
  • Loading branch information
Germano Cavalcante committed Apr 13, 2022
1 parent 50718ab commit 209ee28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions mesh_snap_utilities_line/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
bl_info = {
"name": "Snap_Utilities_Line",
"author": "Germano Cavalcante",
"version": (6, 9, 5),
"blender": (3, 0, 0),
"version": (6, 9, 6),
"blender": (3, 2, 0),
"location": "View3D > TOOLS > Line Tool",
"description": "Extends Blender Snap controls",
"doc_url" : "{BLENDER_MANUAL_URL}/addons/mesh/snap_utilities_line.html",
Expand Down
40 changes: 32 additions & 8 deletions mesh_snap_utilities_line/drawing_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SnapDrawn():
'_format_pos_and_color',
'_program_unif_col',
'_program_smooth_col',
'_UBO',
'_batch_point',)

def __init__(self, out_color, face_color,
Expand Down Expand Up @@ -50,11 +51,12 @@ def __init__(self, out_color, face_color,
self._format_pos_and_color.attr_add(id="pos", comp_type='F32', len=3, fetch_mode='FLOAT')
self._format_pos_and_color.attr_add(id="color", comp_type='F32', len=4, fetch_mode='FLOAT')

self._UBO = None

self._batch_point = None

def _gl_state_push(self, ob_mat=None):
clip_planes = gpu.types.Buffer('FLOAT', (6, 4), self.rv3d.clip_planes) if self.rv3d.use_clip_planes else None

clip_planes = self.rv3d.clip_planes if self.rv3d.use_clip_planes else None
config = 'CLIPPED' if clip_planes else 'DEFAULT'
self._program_unif_col = gpu.shader.from_builtin("3D_UNIFORM_COLOR", config=config)
self._program_smooth_col = gpu.shader.from_builtin("3D_SMOOTH_COLOR", config=config)
Expand All @@ -67,20 +69,42 @@ def _gl_state_push(self, ob_mat=None):

if clip_planes:
gpu.state.clip_distances_set(4)
mat = ob_mat if ob_mat else Matrix.Identity(4)
if self._UBO is None:
import ctypes
class _GPUClipPlanes(ctypes.Structure):
_pack_ = 16
_fields_ = [
("ModelMatrix", (ctypes.c_float * 4) * 4),
("world", (ctypes.c_float * 4) * 6),
]

mat = ob_mat.transposed() if ob_mat else Matrix.Identity(4)

UBO_data = _GPUClipPlanes()
UBO_data.ModelMatrix[0] = mat[0][:]
UBO_data.ModelMatrix[1] = mat[1][:]
UBO_data.ModelMatrix[2] = mat[2][:]
UBO_data.ModelMatrix[3] = mat[3][:]

UBO_data.world[0] = clip_planes[0][:]
UBO_data.world[1] = clip_planes[1][:]
UBO_data.world[2] = clip_planes[2][:]
UBO_data.world[3] = clip_planes[3][:]

self._UBO = gpu.types.GPUUniformBuf(UBO_data)

self._program_unif_col.bind()
self._program_unif_col.uniform_float("ModelMatrix", mat)
self._program_unif_col.uniform_vector_float(self._program_unif_col.uniform_from_name("WorldClipPlanes"), clip_planes, 4, 4)
self._program_unif_col.uniform_block("clipPlanes", self._UBO)

self._program_smooth_col.bind()
self._program_smooth_col.uniform_float("ModelMatrix", mat)
self._program_smooth_col.uniform_vector_float(self._program_smooth_col.uniform_from_name("WorldClipPlanes"), clip_planes, 4, 4)
self._program_smooth_col.uniform_block("clipPlanes", self._UBO)

def _gl_state_restore(self):
gpu.state.blend_set('NONE')
gpu.matrix.pop()
if self.rv3d.use_clip_planes:
if self._UBO:
# del self._UBO
self._UBO = None
gpu.state.clip_distances_set(0)

def batch_line_strip_create(self, coords):
Expand Down

0 comments on commit 209ee28

Please sign in to comment.