Created
May 6, 2019 07:13
-
-
Save codewings/5e83f873353a317047ec3e3d95f785a2 to your computer and use it in GitHub Desktop.
Simple gizmo drawer for blender 2.80
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import gpu | |
from random import random | |
from mathutils import Vector | |
from gpu_extras.batch import batch_for_shader | |
gizmos_line_vert = ''' | |
uniform mat4 u_ViewProjectionMatrix; | |
in vec3 position; | |
in vec4 color; | |
out vec4 vertex_color; | |
void main() | |
{ | |
vertex_color = color; | |
gl_Position = u_ViewProjectionMatrix * vec4(position, 1.0f); | |
} | |
''' | |
gizmos_line_frag = ''' | |
in vec4 vertex_color; | |
void main() | |
{ | |
gl_FragColor = vertex_color; | |
} | |
''' | |
# | |
class QuickGizmosStub: | |
__GizmosHandlers__ = {}; | |
__GizmosInitialized__ = False; | |
# data for line gizmos | |
__LineGizmosCoords__ = []; | |
__LineGizmosColors__ = []; | |
__LineGizmosShader__ = None; | |
__LineGizmosDrawer__ = None; | |
__LineGizmosDirty__ = True; | |
@staticmethod | |
def __create_drawer__(stub, key, fn, vs, ps): | |
shader = gpu.types.GPUShader(vs, ps); | |
stub.__GizmosHandlers__[key] = bpy.types.SpaceView3D.draw_handler_add(fn,(), 'WINDOW', 'POST_PIXEL'); | |
return shader; | |
@staticmethod | |
def get_stub(): | |
stub = None; | |
if 'QUICKGIZMOS' in bpy.app.driver_namespace: | |
stub = bpy.app.driver_namespace['QUICKGIZMOS']; | |
if stub == None: | |
stub = QuickGizmosStub(); | |
# initialize for line gizmo | |
stub.__LineGizmosShader__ = QuickGizmosStub.__create_drawer__(stub, 'GIZMO_LINE_DRAWER', QuickGizmos.__draw_lines__, gizmos_line_vert, gizmos_line_frag); | |
# TODO | |
bpy.app.driver_namespace['QUICKGIZMOS'] = stub; | |
return stub; | |
# | |
class QuickGizmos: | |
@staticmethod | |
def __draw_lines__(): | |
stub = QuickGizmosStub.get_stub(); | |
if len(stub.__LineGizmosCoords__) > 0 and len(stub.__LineGizmosColors__) > 0: | |
stub.__LineGizmosShader__.bind(); | |
matrix = bpy.context.region_data.perspective_matrix; | |
stub.__LineGizmosShader__.uniform_float("u_ViewProjectionMatrix", matrix); | |
# update batch if dirty | |
if stub.__LineGizmosDirty__ == True: | |
stub.__LineGizmosDirty__ = False; | |
stub.__LineGizmosDrawer__ = batch_for_shader(stub.__LineGizmosShader__, 'LINES', | |
{"position": stub.__LineGizmosCoords__, "color": stub.__LineGizmosColors__}); | |
stub.__LineGizmosDrawer__.draw(stub.__LineGizmosShader__); | |
@classmethod | |
def draw_line(cls, v1, v2, color): | |
stub = QuickGizmosStub.get_stub(); | |
stub.__LineGizmosCoords__.append(v1); | |
stub.__LineGizmosCoords__.append(v2); | |
stub.__LineGizmosColors__.append(color); | |
stub.__LineGizmosColors__.append(color); | |
stub.__LineGizmosDirty__ = True; | |
@classmethod | |
def clear_lines(cls): | |
stub = QuickGizmosStub.get_stub(); | |
stub.__LineGizmosCoords__ = []; | |
stub.__LineGizmosColors__ = []; | |
stub.__LineGizmosDirty__ = True; | |
# | |
def draw_gizmo_line(v1, v2, clr=[1,0,0,1]): | |
QuickGizmos.draw_line(v1, v2, clr); | |
# | |
def clear_gizmo_lines(): | |
QuickGizmos.clear_lines(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment