diff options
Diffstat (limited to 'doc/python_api/examples/gpu.4.py')
-rw-r--r-- | doc/python_api/examples/gpu.4.py | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/doc/python_api/examples/gpu.4.py b/doc/python_api/examples/gpu.4.py index 0c86b52bcf5..e05290a9442 100644 --- a/doc/python_api/examples/gpu.4.py +++ b/doc/python_api/examples/gpu.4.py @@ -1,30 +1,39 @@ """ -Wireframe Cube using Index Buffer ---------------------------------- +Mesh with Random Vertex Colors +------------------------------ """ import bpy import gpu +import bgl +import numpy as np +from random import random 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)) +mesh = bpy.context.active_object.data +mesh.calc_loop_triangles() -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)) +vertices = np.empty((len(mesh.vertices), 3), 'f') +indices = np.empty((len(mesh.loop_triangles), 3), 'i') -shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') -batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices) +mesh.vertices.foreach_get( + "co", np.reshape(vertices, len(mesh.vertices) * 3)) +mesh.loop_triangles.foreach_get( + "vertices", np.reshape(indices, len(mesh.loop_triangles) * 3)) + +vertex_colors = [(random(), random(), random(), 1) for _ in range(len(mesh.vertices))] + +shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR') +batch = batch_for_shader( + shader, 'TRIS', + {"pos": vertices, "color": vertex_colors}, + indices=indices, +) def draw(): - shader.bind() - shader.uniform_float("color", (1, 0, 0, 1)) + bgl.glEnable(bgl.GL_DEPTH_TEST) batch.draw(shader) + bgl.glDisable(bgl.GL_DEPTH_TEST) bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW') |