1
$\begingroup$

Example code from GPU Shader Module (gpu) "Mesh with Random Vertex Colors" renders faces in wrong/weird order, what might be the cause?

enter image description here

import bpy
import gpu
import numpy as np
from random import random
from gpu_extras.batch import batch_for_shader

mesh = bpy.context.active_object.data
mesh.calc_loop_triangles()

vertices = np.empty((len(mesh.vertices), 3), 'f')
indices = np.empty((len(mesh.loop_triangles), 3), 'i')

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():
    batch.draw(shader)


bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
$\endgroup$
5
  • $\begingroup$ Can you elaborate on "renders faces in wrong/weird order" Note that the code above draws in local coordinates If your context object has any transformation it will not be overliaid. $\endgroup$
    – batFINGER
    Jan 10 '20 at 1:32
  • $\begingroup$ Added image that shows the issue $\endgroup$
    – KJS
    Jan 10 '20 at 7:54
  • $\begingroup$ For default cube on 2.82.6 pasteall.org/pic/d3c9ad2bdab676539b02555e48df4df6 What is your expected result? $\endgroup$
    – batFINGER
    Jan 10 '20 at 8:05
  • $\begingroup$ Actually I found the issue, depth testing was not enabled, enabling that fixed the issue. Maybe it was fixed in 2.82.6 $\endgroup$
    – KJS
    Jan 10 '20 at 8:28
  • 1
    $\begingroup$ pasting the added lines in case someone stumbles on the same issue bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_TRUE) $\endgroup$
    – KJS
    Jan 10 '20 at 8:29

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.