Welcome to mirror list, hosted at ThFree Co, Russian Federation.

drawing_utilities.py « mesh_snap_utilities_line - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2141cbc6ed09ed63114c411adc8ed4a6ca61a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# SPDX-License-Identifier: GPL-2.0-or-later
import gpu
from mathutils import Vector, Matrix


class SnapDrawn():
    __slots__ = (\
        'out_color',
        'face_color',
        'edge_color',
        'vert_color',
        'center_color',
        'perpendicular_color',
        'constrain_shift_color',
        'axis_x_color',
        'axis_y_color',
        'axis_z_color',
        'rv3d',
        '_format_pos',
        '_format_pos_and_color',
        '_program_unif_col',
        '_program_smooth_col',
        '_batch_point',)

    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, rv3d):

        import gpu

        self.out_color = out_color
        self.face_color = face_color
        self.edge_color = edge_color
        self.vert_color = vert_color
        self.center_color = center_color
        self.perpendicular_color = perpendicular_color
        self.constrain_shift_color = constrain_shift_color

        self.axis_x_color = axis_x_color
        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')

        self._format_pos_and_color = gpu.types.GPUVertFormat()
        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._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)

        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 (
            GPUVertBuf,
            GPUBatch,
        )

        vbo = GPUVertBuf(self._format_pos, len = len(coords))
        vbo.attr_fill(0, data = coords)
        batch_lines = GPUBatch(type = "LINE_STRIP", buf = vbo)
        return batch_lines

    def batch_lines_smooth_color_create(self, coords, colors):
        from gpu.types import (
            GPUVertBuf,
            GPUBatch,
        )

        vbo = GPUVertBuf(self._format_pos_and_color, len = len(coords))
        vbo.attr_fill(0, data = coords)
        vbo.attr_fill(1, data = colors)
        batch_lines = GPUBatch(type = "LINES", buf = vbo)
        return batch_lines

    def batch_triangles_create(self, coords):
        from gpu.types import (
            GPUVertBuf,
            GPUBatch,
        )

        vbo = GPUVertBuf(self._format_pos, len = len(coords))
        vbo.attr_fill(0, data = coords)
        batch_tris = GPUBatch(type = "TRIS", buf = vbo)
        return batch_tris

    def batch_point_get(self):
        if self._batch_point is None:
            from gpu.types import (
                GPUVertBuf,
                GPUBatch,
            )
            vbo = GPUVertBuf(self._format_pos, len = 1)
            vbo.attr_fill(0, ((0.0, 0.0, 0.0),))
            self._batch_point = GPUBatch(type = "POINTS", buf = vbo)
        return self._batch_point

    def draw(self, type, location, list_verts_co, vector_constrain, prevloc):
        import gpu

        self._gl_state_push()
        self._program_unif_col.bind()

        if list_verts_co:
            # draw 3d line OpenGL in the 3D View
            winmat = gpu.matrix.get_projection_matrix()
            winmat[3][2] -= 0.0001
            gpu.matrix.push_projection()
            gpu.matrix.load_projection_matrix(winmat)
            gpu.state.line_width_set(3.0)

            batch = self.batch_line_strip_create([v.to_tuple() for v in list_verts_co] + [location.to_tuple()])

            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

        gpu.state.depth_test_set('NONE')

        point_batch = self.batch_point_get()
        if vector_constrain:
            if prevloc:
                gpu.state.point_size_set(5.0)
                gpu.matrix.translate(prevloc)

                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)

            if vector_constrain[2] == 'X':
                Color4f = self.axis_x_color
            elif vector_constrain[2] == 'Y':
                Color4f = self.axis_y_color
            elif vector_constrain[2] == 'Z':
                Color4f = self.axis_z_color
            else:
                Color4f = self.constrain_shift_color
        else:
            if type == 'OUT':
                Color4f = self.out_color
            elif type == 'FACE':
                Color4f = self.face_color
            elif type == 'EDGE':
                Color4f = self.edge_color
            elif type == 'VERT':
                Color4f = self.vert_color
            elif type == 'CENTER':
                Color4f = self.center_color
            elif type == 'PERPENDICULAR':
                Color4f = self.perpendicular_color
            else: # type == None
                Color4f = self.out_color

        gpu.state.point_size_set(10.0)

        gpu.matrix.translate(location)
        self._program_unif_col.bind()
        self._program_unif_col.uniform_float("color", Color4f)
        point_batch.draw(self._program_unif_col)

        # restore opengl defaults
        gpu.state.point_size_set(1.0)
        gpu.state.line_width_set(1.0)
        gpu.state.depth_test_set('LESS_EQUAL')

        self._gl_state_restore()

    def draw_elem(self, snap_obj, bm, elem):
        #TODO: Cache coords (because antialiasing)
        import gpu
        from bmesh.types import(
            BMVert,
            BMEdge,
            BMFace,
        )

        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)

                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", 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')

        self._gl_state_restore()