[Bf-extensions-cvs] [5b76cb1d] master: Snap_Utilities_Line: update clip distance API

Germano Cavalcante noreply at git.blender.org
Thu Aug 26 18:07:36 CEST 2021


Commit: 5b76cb1d313cb2ca72c477a62b59e6718481b00b
Author: Germano Cavalcante
Date:   Thu Aug 26 10:25:55 2021 -0300
Branches: master
https://developer.blender.org/rBA5b76cb1d313cb2ca72c477a62b59e6718481b00b

Snap_Utilities_Line: update clip distance API

===================================================================

M	mesh_snap_utilities_line/__init__.py
M	mesh_snap_utilities_line/common_classes.py
M	mesh_snap_utilities_line/drawing_utilities.py
M	mesh_snap_utilities_line/snap_context_l/mesh_drawing.py
M	mesh_snap_utilities_line/snap_context_l/shaders/ID_color_frag.glsl
M	mesh_snap_utilities_line/snap_context_l/shaders/ID_color_vert.glsl

===================================================================

diff --git a/mesh_snap_utilities_line/__init__.py b/mesh_snap_utilities_line/__init__.py
index 0b1b7a15..9b93ba55 100644
--- a/mesh_snap_utilities_line/__init__.py
+++ b/mesh_snap_utilities_line/__init__.py
@@ -21,7 +21,7 @@
 bl_info = {
     "name": "Snap_Utilities_Line",
     "author": "Germano Cavalcante",
-    "version": (6, 9, 3),
+    "version": (6, 9, 4),
     "blender": (3, 0, 0),
     "location": "View3D > TOOLS > Line Tool",
     "description": "Extends Blender Snap controls",
diff --git a/mesh_snap_utilities_line/common_classes.py b/mesh_snap_utilities_line/common_classes.py
index 611b07da..f0e2a8ed 100644
--- a/mesh_snap_utilities_line/common_classes.py
+++ b/mesh_snap_utilities_line/common_classes.py
@@ -471,6 +471,7 @@ class SnapUtilities:
 
             preferences = context.preferences.addons[__package__].preferences
             self.preferences = preferences
+
             #Init DrawCache
             self.draw_cache = SnapDrawn(
                 preferences.out_color,
@@ -482,7 +483,8 @@ class SnapUtilities:
                 preferences.constrain_shift_color,
                 tuple(context.preferences.themes[0].user_interface.axis_x) + (1.0,),
                 tuple(context.preferences.themes[0].user_interface.axis_y) + (1.0,),
-                tuple(context.preferences.themes[0].user_interface.axis_z) + (1.0,))
+                tuple(context.preferences.themes[0].user_interface.axis_z) + (1.0,),
+                self.sctx.rv3d)
 
         self.snap_vert = self.snap_edge = snap_edge_and_vert
 
diff --git a/mesh_snap_utilities_line/drawing_utilities.py b/mesh_snap_utilities_line/drawing_utilities.py
index cf67db4b..b4e53a00 100644
--- a/mesh_snap_utilities_line/drawing_utilities.py
+++ b/mesh_snap_utilities_line/drawing_utilities.py
@@ -15,7 +15,7 @@
 #
 # ##### END GPL LICENSE BLOCK #####
 import gpu
-from mathutils import Vector
+from mathutils import Vector, Matrix
 
 
 class SnapDrawn():
@@ -30,6 +30,7 @@ class SnapDrawn():
         'axis_x_color',
         'axis_y_color',
         'axis_z_color',
+        'rv3d',
         '_format_pos',
         '_format_pos_and_color',
         '_program_unif_col',
@@ -39,7 +40,7 @@ class SnapDrawn():
     def __init__(self, out_color, face_color,
                  edge_color, vert_color, center_color,
                  perpendicular_color, constrain_shift_color,
-                 axis_x_color, axis_y_color, axis_z_color):
+                 axis_x_color, axis_y_color, axis_z_color, rv3d):
 
         import gpu
 
@@ -55,6 +56,8 @@ class SnapDrawn():
         self.axis_y_color = axis_y_color
         self.axis_z_color = axis_z_color
 
+        self.rv3d = rv3d
+
         self._format_pos = gpu.types.GPUVertFormat()
         self._format_pos.attr_add(id="pos", comp_type='F32', len=3, fetch_mode='FLOAT')
 
@@ -62,18 +65,38 @@ class SnapDrawn():
         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._program_unif_col = gpu.shader.from_builtin("3D_UNIFORM_COLOR")
-        self._program_smooth_col = gpu.shader.from_builtin("3D_SMOOTH_COLOR")
-
         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
+
+        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)
 
-    def _gl_state_push(self):
         gpu.state.program_point_size_set(False)
         gpu.state.blend_set('ALPHA')
+        gpu.matrix.push()
+        if ob_mat:
+            gpu.matrix.multiply_matrix(ob_mat)
+
+        if clip_planes:
+            gpu.state.clip_distances_set(4)
+            mat = ob_mat if ob_mat else Matrix.Identity(4)
+
+            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_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)
 
     def _gl_state_restore(self):
         gpu.state.blend_set('NONE')
+        gpu.matrix.pop()
+        if self.rv3d.use_clip_planes:
+            gpu.state.clip_distances_set(0)
 
     def batch_line_strip_create(self, coords):
         from gpu.types import (
@@ -124,7 +147,6 @@ class SnapDrawn():
         import gpu
 
         self._gl_state_push()
-        gpu.matrix.push()
         self._program_unif_col.bind()
 
         if list_verts_co:
@@ -139,6 +161,7 @@ class SnapDrawn():
 
             self._program_unif_col.bind()
             self._program_unif_col.uniform_float("color", (1.0, 0.8, 0.0, 0.5))
+
             batch.draw(self._program_unif_col)
             gpu.matrix.pop_projection()
             del batch
@@ -153,6 +176,7 @@ class SnapDrawn():
 
                 self._program_unif_col.bind()
                 self._program_unif_col.uniform_float("color", (1.0, 1.0, 1.0, 0.5))
+
                 point_batch.draw(self._program_unif_col)
                 gpu.matrix.translate(-prevloc)
 
@@ -192,7 +216,6 @@ class SnapDrawn():
         gpu.state.line_width_set(1.0)
         gpu.state.depth_test_set('LESS_EQUAL')
 
-        gpu.matrix.pop()
         self._gl_state_restore()
 
     def draw_elem(self, snap_obj, bm, elem):
@@ -204,53 +227,48 @@ class SnapDrawn():
             BMFace,
         )
 
-        with gpu.matrix.push_pop():
-            self._gl_state_push()
-            gpu.state.depth_test_set('NONE')
-
-            gpu.matrix.multiply_matrix(snap_obj.mat)
-
-            if isinstance(elem, BMVert):
-                if elem.link_edges:
-                    import numpy as np
-
-                    color = self.vert_color
-                    edges = np.empty((len(elem.link_edges), 2), [("pos", "f4", 3), ("color", "f4", 4)])
-                    edges["pos"][:, 0] = elem.co
-                    edges["pos"][:, 1] = [e.other_vert(elem).co for e in elem.link_edges]
-                    edges["color"][:, 0] = color
-                    edges["color"][:, 1] = (color[0], color[1], color[2], 0.0)
-                    edges.shape = -1
-
-                    self._program_smooth_col.bind()
-                    gpu.state.line_width_set(3.0)
-                    batch = self.batch_lines_smooth_color_create(edges["pos"], edges["color"])
-                    batch.draw(self._program_smooth_col)
-                    gpu.state.line_width_set(1.0)
-            else:
+        self._gl_state_push(snap_obj.mat)
+        gpu.state.depth_test_set('NONE')
+
+        if isinstance(elem, BMVert):
+            if elem.link_edges:
+                import numpy as np
+
+                color = self.vert_color
+                edges = np.empty((len(elem.link_edges), 2), [("pos", "f4", 3), ("color", "f4", 4)])
+                edges["pos"][:, 0] = elem.co
+                edges["pos"][:, 1] = [e.other_vert(elem).co for e in elem.link_edges]
+                edges["color"][:, 0] = color
+                edges["color"][:, 1] = (color[0], color[1], color[2], 0.0)
+                edges.shape = -1
+
+                self._program_smooth_col.bind()
+                gpu.state.line_width_set(3.0)
+                batch = self.batch_lines_smooth_color_create(edges["pos"], edges["color"])
+                batch.draw(self._program_smooth_col)
+                gpu.state.line_width_set(1.0)
+        else:
+            if isinstance(elem, BMEdge):
                 self._program_unif_col.bind()
+                self._program_unif_col.uniform_float("color", self.edge_color)
 
-                if isinstance(elem, BMEdge):
+                gpu.state.line_width_set(3.0)
+                batch = self.batch_line_strip_create([v.co for v in elem.verts])
+                batch.draw(self._program_unif_col)
+                gpu.state.line_width_set(1.0)
+
+            elif isinstance(elem, BMFace):
+                if len(snap_obj.data) == 2:
+                    face_color = self.face_color[0], self.face_color[1], self.face_color[2], self.face_color[3] * 0.2
                     self._program_unif_col.bind()
-                    self._program_unif_col.uniform_float("color", self.edge_color)
+                    self._program_unif_col.uniform_float("color", face_color)
 
-                    gpu.state.line_width_set(3.0)
-                    batch = self.batch_line_strip_create([v.co for v in elem.verts])
+                    tris = snap_obj.data[1].get_loop_tri_co_by_bmface(bm, elem)
+                    tris.shape = (-1, 3)
+                    batch = self.batch_triangles_create(tris)
                     batch.draw(self._program_unif_col)
-                    gpu.state.line_width_set(1.0)
-
-                elif isinstance(elem, BMFace):
-                    if len(snap_obj.data) == 2:
-                        face_color = self.face_color[0], self.face_color[1], self.face_color[2], self.face_color[3] * 0.2
-                        self._program_unif_col.bind()
-                        self._program_unif_col.uniform_float("color", face_color)
-
-                        tris = snap_obj.data[1].get_loop_tri_co_by_bmface(bm, elem)
-                        tris.shape = (-1, 3)
-                        batch = self.batch_triangles_create(tris)
-                        batch.draw(self._program_unif_col)
 
-            # restore opengl defaults
-            gpu.state.depth_test_set('LESS_EQUAL')
+        # restore opengl defaults
+        gpu.state.depth_test_set('LESS_EQUAL')
 
         self._gl_state_restore()
diff --git a/mesh_snap_utilities_line/snap_context_l/mesh_drawing.py b/mesh_snap_utilities_line/snap_context_l/mesh_drawing.py
index bd324afd..679ee932 100644
--- a/mesh_snap_utilities_line/snap_context_l/mesh_drawing.py
+++ b/mesh_snap_utilities_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list