gpu.3.py 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. Triangle with Custom Shader
  3. ---------------------------
  4. """
  5. import bpy
  6. import gpu
  7. from gpu_extras.batch import batch_for_shader
  8. vertex_shader = '''
  9. uniform mat4 viewProjectionMatrix;
  10. in vec3 position;
  11. out vec3 pos;
  12. void main()
  13. {
  14. pos = position;
  15. gl_Position = viewProjectionMatrix * vec4(position, 1.0f);
  16. }
  17. '''
  18. fragment_shader = '''
  19. uniform float brightness;
  20. in vec3 pos;
  21. void main()
  22. {
  23. gl_FragColor = vec4(pos * brightness, 1.0);
  24. }
  25. '''
  26. coords = [(1, 1, 1), (2, 0, 0), (-2, -1, 3)]
  27. shader = gpu.types.GPUShader(vertex_shader, fragment_shader)
  28. batch = batch_for_shader(shader, 'TRIS', {"position": coords})
  29. def draw():
  30. shader.bind()
  31. matrix = bpy.context.region_data.perspective_matrix
  32. shader.uniform_float("viewProjectionMatrix", matrix)
  33. shader.uniform_float("brightness", 0.5)
  34. batch.draw(shader)
  35. bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')