0
$\begingroup$

Is it possible to draw multiple types of Opengl draw methods inside the same shader batch?

Based on the the GPU module documentation, please see the code below.

For instance I have this line.

batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices)

Then if I add this line just below it

batch = batch_for_shader(shader, 'POINTS', {"pos": coords})

It only draws the points, the lines are gone, which makes sense I guess. However I would like to know what is the fastest/proper way to be able to draw multiple types of geometry with the GPU module.

import bpy
import gpu
from gpu_extras.batch import batch_for_shader

coords = (
    (-1, -1, -1), (+1, -1, -1),
    (-1, +1, -1), (+1, +1, -1),
    (-1, -1, +1), (+1, -1, +1),
    (-1, +1, +1), (+1, +1, +1))

indices = (
    (0, 1), (0, 2), (1, 3), (2, 3),
    (4, 5), (4, 6), (5, 7), (6, 7),
    (0, 4), (1, 5), (2, 6), (3, 7))

shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices)


def draw():
    shader.bind()
    shader.uniform_float("color", (1, 0, 0, 1))
    batch.draw(shader)


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.