6
$\begingroup$

I've been messing around with the new GPU python module in Blender 2.8. But I cannot find a way to make my lines and triangles transparent. The alpha channel is completely ignored. I even tried it with a vert frag shader and even that doesn't work out.

import bpy
import gpu
import bgl
from gpu_extras.batch import batch_for_shader

vertex_shader = '''
    uniform mat4 viewProjectionMatrix;

    in vec3 position;
    out vec3 pos;

    void main()
    {
        pos = position;
        gl_Position = viewProjectionMatrix * vec4(position, 1.0f);
    }
'''

fragment_shader = '''
    uniform float brightness;

    in vec3 pos;

    void main()
    {
        gl_FragColor = vec4(vec3(1, 0, 0) * brightness, 1.0);
    }
'''

vertices = [(1, 0, 1), (-1, 0, 1), (-1, -0, -1), (1, 0, -1)]
indices = [(3, 0, 1), (1, 2, 3)]

shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
batch = batch_for_shader(shader, 'TRIS', {"position": vertices}, indices=indices)

verticesOutline = [(1, 0, 1), (-1, 0, 1), (-1, -0, -1), (1, 0, -1)]
indicesOutline = [(3, 0), (0, 1), (1, 2), (2, 3)]

shaderOutline = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
batchOutline = batch_for_shader(shaderOutline, 'LINES', {"pos": verticesOutline}, indices=indicesOutline)


def draw():
    shader.bind()
    matrix = bpy.context.region_data.perspective_matrix
    shader.uniform_float("viewProjectionMatrix", matrix)
    shader.uniform_float("brightness", 0.2)
    batch.draw(shader)

bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

def drawOutlines():
    shaderOutline.bind()
    shaderOutline.uniform_float("color", (0, 1, 0, 0.4))
    batchOutline.draw(shaderOutline)

bpy.types.SpaceView3D.draw_handler_add(drawOutlines, (), 'WINDOW', 'POST_VIEW')
$\endgroup$

1 Answer 1

6
$\begingroup$

So I found the answer to this one with some help from the folks on Blender.chat (thanks Okuma_10!).

In your draw method before you draw put in: -

glEnable(GL_BLEND)

and after you're done drawing put in: -

glDisable(GL_BLEND)

also make sure you import all of BGL with: -

from bgl import *

Apparently there is no performance cost for doing so as the program naturally iterates through the entire library anyway. After that the alpha values should work.

$\endgroup$
1
  • $\begingroup$ Thanks, man! I already labeled this as impossible and started to think about alternative methods, like rendering a bunch of small dots to get kind of fake transparency or just drawing a wireframe as a last resort and there you come with the proper solution :D $\endgroup$ Commented Dec 19, 2019 at 7:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .