the default cursor has a constant size, whatever the depth is.
I want to do the same on an extension of the cursor gizmo, to show better the orientation, when running a modal.
so far I did this (I don’t really master the shader colors part…)
code:
import bpy
import bgl
import gpu
from gpu_extras.batch import batch_for_shader
vertex_shader = '''
uniform mat4 ModelViewProjectionMatrix;
in vec3 pos;
in vec4 color;
out vec4 finalColor;
void main()
{
gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
gl_Position.z -= 0.001;
finalColor = color;
}
'''
fragment_shader = """
in vec4 finalColor;
in vec4 fragCoord;
out vec4 fragColor;
out float fragDepth;
void main()
{
vec2 coord = gl_PointCoord - vec2(0.5, 0.5);
fragColor = finalColor;
fragDepth = 0;
}
"""
def draw_callback_3d(self,context):
curs = bpy.context.scene.cursor
loc = curs.location
coords = [loc, (loc[0]+1, loc[1], loc[2]),
loc, (loc[0], loc[1]+1, loc[2]),
loc, (loc[0], loc[1], loc[2]+1)]
colors = ((0.8, 0, 0, 0.9), (0.8, 0, 0, 0.9),
(0, 0.8, 0, 0.9), (0, 0.8, 0, 0.9),
(0, 0, 0.8, 0.9), (0, 0, 0.8, 0.9), )
shader = gpu.types.GPUShader(vertex_shader, fragment_shader) #simpler way?
batch = batch_for_shader(shader, 'LINES', {"pos": coords, "color": colors})
# bgl.glEnable(bgl.GL_MULTISAMPLE) #what for? not in the API
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
bgl.glDisable(bgl.GL_DEPTH_TEST) #on top
bgl.glLineWidth(3)
shader.bind()
batch.draw(shader)
class OT_draw_operator(bpy.types.Operator):
bl_idname = "object.draw_op"
bl_label = "Draw operator"
def invoke(self, context, event):
args = (self, context)
self.draw_handle_3d = bpy.types.SpaceView3D.draw_handler_add(draw_callback_3d, args, 'WINDOW', 'POST_VIEW')
context.area.tag_redraw()
context.window_manager.modal_handler_add(self)
return {"RUNNING_MODAL"}
def modal(self, context, event):
if event.type in {"ESC"} and event.value == "PRESS":
bpy.types.SpaceView3D.draw_handler_remove(self.draw_handle_3d, 'WINDOW')
context.area.tag_redraw()
return {'FINISHED'}
return {"PASS_THROUGH"}
def register():
bpy.utils.register_class(OT_draw_operator)
if __name__ == "__main__":
register()