update decorator to match shader + changes to text rendering

This commit is contained in:
Lucas Nascimento
2024-09-24 20:53:13 -03:00
committed by Dion Moult
parent 53a2b87c97
commit d915dc9df0
@@ -3,6 +3,7 @@ from mathutils import Vector
import bpy
import gpu
import blf
from bpy_extras import view3d_utils
from bpy.types import SpaceView3D
from gpu_extras.batch import batch_for_shader
from bonsai.bim.module.structural.shader import ShaderInfo
@@ -12,6 +13,7 @@ class LoadsDecorator:
handlers = []
linear_load_shader = None
text_info = []
shader_info = []
@classmethod
def install(cls, context):
@@ -27,6 +29,7 @@ class LoadsDecorator:
#)
cls.handlers.append(SpaceView3D.draw_handler_add(handler, (), "WINDOW", "POST_VIEW"))
cls.linear_load_shader = ShaderInfo("DistributedLoad")
cls.update(context)
cls.is_installed = True
@classmethod
@@ -41,12 +44,13 @@ class LoadsDecorator:
@classmethod
def update(cls, context):
cls.linear_load_shader.update()
cls.text_info += cls.linear_load_shader.text_info
cls.text_info = cls.linear_load_shader.text_info
cls.shader_info = cls.linear_load_shader.info
def __call__(self):
# set open gl configurations
original_blend = gpu.state.blend_get()
gpu.state.blend_set('ALPHA')
gpu.state.blend_set('ADDITIVE')
original_depth_mask = gpu.state.depth_mask_get()
#gpu.state.depth_mask_set(True)
original_depth_test = gpu.state.depth_test_get()
@@ -62,17 +66,20 @@ class LoadsDecorator:
def draw_batch(self,shader_type: str):
""" param: shader_type: type of shader in ["DistributedLoad", "PointLoad"]"""
if shader_type == "DistributedLoad" and not self.linear_load_shader.is_empty:
shader_info = self.linear_load_shader
shader = shader_info.shader
args = shader_info.args
indices = shader_info.indices
batch = batch_for_shader(shader, 'TRIS', args, indices=indices)
matrix = bpy.context.region_data.perspective_matrix
shader.uniform_float("viewProjectionMatrix", matrix)
shader.uniform_float("spacing", 0.2) # make it customizable
shader.uniform_float("maxload", 200) # make it customizable
batch.draw(shader)
if not self.linear_load_shader.is_empty:
for info in self.shader_info:
shader = info["shader"]
args = info["args"]
indices = info["indices"]
batch = batch_for_shader(shader, 'TRIS', args, indices=indices)
matrix = bpy.context.region_data.perspective_matrix
shader.bind()
shader.uniform_float("viewProjectionMatrix", matrix)
for key, value in info["uniforms"]:
shader.uniform_float(key, value)
batch.draw(shader)
def draw_load_values(self, context):
for info in self.text_info:
@@ -91,11 +98,6 @@ def location_3d_to_region_2d(coord, normal, context):
perspective = rv3d.view_perspective
view_matrix = rv3d.view_matrix
point_view_space = view_matrix @ coord
x,y,z = view_matrix.to_3x3()
angle = abs(0.5*pi-normal.angle(z))
#if small view angle betwen the load and viewport
if angle<0.05:
return None
if perspective == 'ORTHO' or -20 < point_view_space.z < 0:
prj = rv3d.perspective_matrix @ Vector((coord[0], coord[1], coord[2], 1.0))
@@ -104,5 +106,10 @@ def location_3d_to_region_2d(coord, normal, context):
co = Vector((width_half + width_half * (prj.x / prj.w),
height_half + height_half * (prj.y / prj.w),
))
view_vector = view3d_utils.region_2d_to_vector_3d(context.region, rv3d, co)
angle = abs(0.5*pi-normal.angle(view_vector))
#if small view angle betwen the load and viewport
if angle<0.05:
return None
return co
return None