bgl to gpu: profiles and openings decorations #2897

Still need to fix shaders.py and decoration.py
This commit is contained in:
Andrej730
2023-03-31 16:04:29 +05:00
parent a03fbab4d5
commit 3a3d96be0a
5 changed files with 62 additions and 82 deletions
@@ -358,7 +358,7 @@ class BaseDecorator:
bgl.glEnable(bgl.GL_LINE_SMOOTH)
bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
bgl.glEnable(bgl.GL_BLEND)
gpu.state.blend_set("ALPHA")
bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
self.shader.bind()
@@ -2065,6 +2065,7 @@ class SectionDecorator(LevelDecorator):
# TODO: add dashed line to shader
# mind the vertices limit
# ref: https://docs.blender.org/api/main/gpu.html#custom-shader-for-dotted-3d-line
display_data = DecoratorData.get_section_markers_display_data(obj)
self.draw_lines(
context,
@@ -273,7 +273,7 @@ class CustomGizmo:
gpu.matrix.multiply_matrix(self.matrix_world)
batch.draw()
bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set("NONE")
class OffsetHandle:
@@ -20,6 +20,7 @@ import bgl
from mathutils import Matrix
from gpu.types import GPUShader
from gpu_extras.batch import batch_for_shader
import gpu
class BaseShader:
@@ -120,12 +121,11 @@ class BaseShader:
self.prog.bind()
def glenable(self):
bgl.glEnable(bgl.GL_BLEND)
gpu.state.blend_set("ALPHA")
bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
bgl.glBlendEquation(bgl.GL_FUNC_ADD)
# bgl.glEnable(bgl.GL_DEPTH_TEST)
# bgl.glDepthFunc(bgl.GL_LEQUAL)
# gpu.state.depth_test_set('LESS_EQUAL')
# bgl.glDepthMask(True)
def uniform_region(self, ctx):
@@ -17,18 +17,16 @@
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import gpu
import bgl
import bmesh
import blenderbim.tool as tool
from math import pi, degrees, sin, cos, radians
from math import sin, cos, radians
from bpy.types import SpaceView3D
from mathutils import Vector, Matrix
from gpu.types import GPUShader, GPUBatch, GPUIndexBuf, GPUVertBuf, GPUVertFormat
from gpu_extras.batch import batch_for_shader
white = (1, 1, 1, 1)
lightgrey = (0.7, 0.7, 0.7, 1)
white_t = (1, 1, 1, 0.5)
green = (0.545, 0.863, 0, 1)
red = (1, 0.2, 0.322, 1)
blue = (0.157, 0.565, 1, 1)
@@ -71,13 +69,11 @@ class ProfileDecorator:
pass
cls.installed = None
def create_batch(self, shader_type, content_pos, color, indices=None, bind=True):
batch = batch_for_shader(self.shader, shader_type, {"pos": content_pos}, indices=indices)
# TODO: what's bind is for?
if bind:
self.shader.bind()
self.shader.uniform_float("color", color)
batch.draw(self.shader)
def draw_batch(self, shader_type, content_pos, color, indices=None):
shader = self.line_shader if shader_type == "LINES" else self.shader
batch = batch_for_shader(shader, shader_type, {"pos": content_pos}, indices=indices)
shader.uniform_float("color", color)
batch.draw(shader)
def draw_faces(self, bm, vertices_coords):
"""mutates original bm (triangulates it)
@@ -87,7 +83,7 @@ class ProfileDecorator:
bmesh.ops.triangulate(traingulated_bm, faces=traingulated_bm.faces)
face_indices = [[v.index for v in f.verts] for f in traingulated_bm.faces]
self.create_batch("TRIS", vertices_coords, faces_color, face_indices)
self.draw_batch("TRIS", vertices_coords, faces_color, face_indices)
def __call__(self, context, get_custom_bmesh=None, draw_faces=False, exit_edit_mode_callback=None):
obj = context.active_object
@@ -103,20 +99,8 @@ class ProfileDecorator:
else:
bm = bmesh.from_edit_mesh(obj.data)
def gl_init(use_bgl=False):
# TODO: remove as deprecated?
if use_bgl:
bgl.glLineWidth(2)
bgl.glPointSize(6)
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
else:
gpu.state.line_width_set(2)
gpu.state.point_size_set(6)
gpu.state.blend_set("ALPHA")
bgl.glEnable(bgl.GL_LINE_SMOOTH)
gl_init(True)
gpu.state.point_size_set(6)
gpu.state.blend_set("ALPHA")
### Actually drawing
all_vertices = []
@@ -202,22 +186,31 @@ class ProfileDecorator:
unselected_edges.append(edge_indices)
### Actually drawing
# 3D_POLYLINE_UNIFORM_COLOR is good for smoothed lines since `bgl.enable(GL_LINE_SMOOTH)` is deprecated
self.line_shader = gpu.shader.from_builtin("3D_POLYLINE_UNIFORM_COLOR")
self.line_shader.bind()
# POLYLINE_UNIFORM_COLOR specific uniforms
self.line_shader.uniform_float("viewportSize", (context.region.width, context.region.height))
self.line_shader.uniform_float("lineWidth", 2.0)
# general shader
self.shader = gpu.shader.from_builtin("3D_UNIFORM_COLOR")
self.shader.bind()
# Draw faces
if draw_faces:
self.draw_faces(bm, all_vertices)
self.create_batch("LINES", all_vertices, lightgrey, unselected_edges)
self.create_batch("LINES", all_vertices, green, selected_edges)
self.create_batch("LINES", all_vertices, grey, arc_edges)
self.create_batch("LINES", all_vertices, preview_edges_color, preview_edges)
self.create_batch("LINES", all_vertices, blue, roof_angle_edges)
self.draw_batch("LINES", all_vertices, white_t, unselected_edges)
self.draw_batch("LINES", all_vertices, green, selected_edges)
self.draw_batch("LINES", all_vertices, grey, arc_edges)
self.draw_batch("LINES", all_vertices, preview_edges_color, preview_edges)
self.draw_batch("LINES", all_vertices, blue, roof_angle_edges)
self.create_batch("POINTS", unselected_vertices, lightgrey)
self.create_batch("POINTS", error_vertices, red)
self.create_batch("POINTS", special_vertices, blue)
self.create_batch("POINTS", selected_vertices, green)
self.draw_batch("POINTS", unselected_vertices, white_t)
self.draw_batch("POINTS", error_vertices, red)
self.draw_batch("POINTS", special_vertices, blue)
self.draw_batch("POINTS", selected_vertices, green)
# Draw arcs
arc_centroids = []
@@ -242,9 +235,9 @@ class ProfileDecorator:
arc_centroids.append(tuple(centroid))
arc_segments.append(tool.Cad.create_arc_segments(pts=points, num_verts=17, make_edges=True))
self.create_batch("POINTS", arc_centroids, grey, bind=False)
self.draw_batch("POINTS", arc_centroids, grey)
for verts, edges in arc_segments:
self.create_batch("LINES", verts, blue, edges, bind=False)
self.draw_batch("LINES", verts, blue, edges)
# Draw circles
circle_centroids = []
@@ -263,9 +256,9 @@ class ProfileDecorator:
segments = [[list(matrix @ Vector(v)) for v in segments[0]], segments[1]]
circle_segments.append(segments)
self.create_batch("POINTS", circle_centroids, grey, bind=False)
self.draw_batch("POINTS", circle_centroids, grey)
for verts, edges in circle_segments:
self.create_batch("LINES", verts, blue, edges, bind=False)
self.draw_batch("LINES", verts, blue, edges)
def create_matrix(self, p, x, y, z):
return Matrix([x, y, z, p]).to_4x4().transposed()
@@ -18,7 +18,6 @@
import bpy
import gpu
import bgl
import bmesh
import logging
import numpy as np
@@ -36,7 +35,6 @@ from bpy.types import Operator
from bpy.types import SpaceView3D
from bpy.props import FloatProperty
from bpy_extras.object_utils import AddObjectHelper, object_data_add
from gpu.types import GPUShader, GPUBatch, GPUIndexBuf, GPUVertBuf, GPUVertFormat
from gpu_extras.batch import batch_for_shader
@@ -758,7 +756,7 @@ class EditOpenings(Operator, tool.Ifc.Operator):
results.add(obj)
return results
# TODO: merge with ProfileDecorator?
class DecorationsHandler:
installed = None
@@ -777,11 +775,15 @@ class DecorationsHandler:
pass
cls.installed = None
def draw_batch(self, shader_type, content_pos, color, indices=None):
shader = self.line_shader if shader_type == "LINES" else self.shader
batch = batch_for_shader(shader, shader_type, {"pos": content_pos}, indices=indices)
shader.uniform_float("color", color)
batch.draw(shader)
def __call__(self, context):
bgl.glLineWidth(2)
bgl.glPointSize(6)
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
gpu.state.point_size_set(6)
gpu.state.blend_set("ALPHA")
for opening in context.scene.BIMModelProperties.openings:
obj = opening.obj
@@ -790,7 +792,7 @@ class DecorationsHandler:
continue
white = (1, 1, 1, 1)
white_t = (1, 1, 1, 0.1)
white_t = (1, 1, 1, 0.5)
green = (0.545, 0.863, 0, 1)
red = (1, 0.2, 0.322, 1)
red_t = (1, 0.2, 0.322, 0.1)
@@ -798,6 +800,13 @@ class DecorationsHandler:
blue_t = (0.157, 0.565, 1, 0.1)
grey = (0.2, 0.2, 0.2, 1)
self.line_shader = gpu.shader.from_builtin("3D_POLYLINE_UNIFORM_COLOR")
self.line_shader.bind() # required to be able to change uniforms of the shader
# POLYLINE_UNIFORM_COLOR specific uniforms
self.line_shader.uniform_float("viewportSize", (context.region.width, context.region.height))
self.line_shader.uniform_float("lineWidth", 2.0)
# general shader
self.shader = gpu.shader.from_builtin("3D_UNIFORM_COLOR")
verts = []
@@ -829,41 +838,21 @@ class DecorationsHandler:
else:
unselected_edges.append(edge_indices)
batch = batch_for_shader(self.shader, "LINES", {"pos": verts}, indices=unselected_edges)
self.shader.bind()
self.shader.uniform_float("color", white)
batch.draw(self.shader)
batch = batch_for_shader(self.shader, "LINES", {"pos": verts}, indices=selected_edges)
self.shader.uniform_float("color", green)
batch.draw(self.shader)
batch = batch_for_shader(self.shader, "POINTS", {"pos": unselected_vertices})
self.shader.uniform_float("color", white)
batch.draw(self.shader)
batch = batch_for_shader(self.shader, "POINTS", {"pos": selected_vertices})
self.shader.uniform_float("color", green)
batch.draw(self.shader)
self.draw_batch("LINES", verts, white_t, unselected_edges)
self.draw_batch("LINES", verts, green, selected_edges)
self.draw_batch("POINTS", unselected_vertices, white)
self.draw_batch("POINTS", selected_vertices, green)
else:
bm = bmesh.new()
bm.from_mesh(obj.data)
verts = [tuple(obj.matrix_world @ v.co) for v in bm.verts]
edges = [tuple([v.index for v in e.verts]) for e in bm.edges]
batch = batch_for_shader(self.shader, "LINES", {"pos": verts}, indices=edges)
self.shader.bind()
self.shader.uniform_float("color", green if obj in context.selected_objects else blue)
batch.draw(self.shader)
self.draw_batch("LINES", verts, green if obj in context.selected_objects else blue, edges)
obj.data.calc_loop_triangles()
tris = [tuple(t.vertices) for t in obj.data.loop_triangles]
batch = batch_for_shader(self.shader, "TRIS", {"pos": verts}, indices=tris)
self.shader.bind()
self.shader.uniform_float("color", blue_t)
batch.draw(self.shader)
self.draw_batch("TRIS", verts, blue_t, tris)
if "HalfSpaceSolid" in obj.name:
# Arrow shape
@@ -876,10 +865,7 @@ class DecorationsHandler:
tuple(obj.matrix_world @ Vector((0, -0.05, 0.45))),
]
edges = [(0, 1), (1, 2), (1, 3), (1, 4), (1, 5)]
batch = batch_for_shader(self.shader, "LINES", {"pos": verts}, indices=edges)
self.shader.bind()
self.shader.uniform_float("color", green if obj in context.selected_objects else blue)
batch.draw(self.shader)
self.draw_batch("LINES", verts, green if obj in context.selected_objects else blue, edges)
if obj.mode != "EDIT":
bm.free()