How to show mesh using bgl with proper occulsion/depth? #1772
-
I was trying to create a simple falloff visualizer using bgl. It shows 3 planes in 3d viewport with vertex color created from falloff. This is how it looks now: Code: import bpy
from bpy.props import *
from ... draw_handler import drawHandler
from ... base_types import AnimationNode
from ... tree_info import getNodesByType
from ... utils.blender_ui import redrawAll
import gpu
from bgl import *
from gpu_extras.batch import batch_for_shader
import numpy as np
from math import pi
from mathutils import Euler
from ... data_structures import Matrix4x4List
from ... nodes . mesh . c_utils import replicateMesh
from ... algorithms.mesh_generation.grid import getGridMesh_Size
dataByIdentifier = {}
class DrawData:
def __init__(self, data, drawFunction):
self.data = data
self.drawFunction = drawFunction
class BF_VisualizeFalloff(bpy.types.Node, AnimationNode):
bl_idname = "an_bf_VisualizeFalloff"
bl_label = "Visualize Falloff"
def drawPropertyChanged(self, context):
self.execute(self.getCurrentData())
self.redrawViewport(context)
def redrawViewport(self, context):
redrawAll()
enabled: BoolProperty(name = "Enabled", default = True, update = redrawViewport)
resolution: IntProperty(name = "Resolution", default = 10, min = 2, update = drawPropertyChanged)
scale: FloatProperty(name = "Scale", default = 5, update = drawPropertyChanged)
def create(self):
self.newInput("Falloff", "Falloff", "falloff")
def draw(self, layout):
layout.prop(self, "enabled")
layout.prop(self, "resolution")
layout.prop(self, "scale")
def execute(self, falloff):
self.freeDrawingData()
dataByIdentifier[self.identifier] = DrawData(falloff, self.drawPlanes)
def drawPlanes(self, data):
vertices, polygons = self.createPlanes(self.resolution, self.scale)
falloffStrengths = self.getFalloffStrengths(data, vertices)
_vertices = vertices.asNumpyArray().reshape(-1,3)
_polygons = np.array(polygons, 'i')
colors = [(i,i,i,1) for i in falloffStrengths]
shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR')
batch = batch_for_shader(shader, 'TRIS', {"pos": _vertices, "color": colors}, indices=_polygons)
shader.bind()
batch.draw(shader)
def createPlanes(self, resolution, scale):
resolution = max(resolution, 2)
mesh = getGridMesh_Size(scale, scale, resolution, resolution)
mesh.triangulateMesh(method = "FAN")
matrices = Matrix4x4List.fromValues([Euler((0,pi/2,0)).to_matrix().to_4x4(),
Euler((pi/2,0,0)).to_matrix().to_4x4(),
Euler((0,0,0)).to_matrix().to_4x4()])
meshes = replicateMesh(mesh, matrices)
return meshes.vertices, meshes.polygons
def getFalloffStrengths(self, falloff, vectors):
evaluator = falloff.getEvaluator("LOCATION")
return evaluator.evaluateList(vectors)
def delete(self):
self.freeDrawingData()
def freeDrawingData(self):
if self.identifier in dataByIdentifier:
del dataByIdentifier[self.identifier]
def getCurrentData(self):
if self.identifier in dataByIdentifier:
return dataByIdentifier[self.identifier].data
@drawHandler("SpaceView3D", "WINDOW", "POST_VIEW")
def draw():
for node in getNodesByType("an_bf_VisualizeFalloff"):
if node.enabled and node.identifier in dataByIdentifier:
drawData = dataByIdentifier[node.identifier]
drawData.drawFunction(drawData.data) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@harisreedhar Hi. I'm not sure but you need to set alpha to zero for the transparency in
|
Beta Was this translation helpful? Give feedback.
-
@harisreedhar What you are looking for is called Depth Testing in OpenGL. In its most basic form, you just need to enable it before drawing then disable it after drawing like this:
I haven't tested this, but it should work. There are other states you can play with, mentioned in the aforementioned article . |
Beta Was this translation helpful? Give feedback.
@harisreedhar What you are looking for is called Depth Testing in OpenGL. In its most basic form, you just need to enable it before drawing then disable it after drawing like this:
I haven't tested this, but it should work. There are other states you can play with, mentioned in the aforementioned article .