gpu.10.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """
  2. Rendering the 3D View into a Texture
  3. ------------------------------------
  4. The scene has to have a camera for this example to work.
  5. You could also make this independent of a specific camera,
  6. but Blender does not expose good functions to create view and projection matrices yet.
  7. """
  8. import bpy
  9. import bgl
  10. import gpu
  11. from gpu_extras.presets import draw_texture_2d
  12. WIDTH = 512
  13. HEIGHT = 256
  14. offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT)
  15. def draw():
  16. context = bpy.context
  17. scene = context.scene
  18. view_matrix = scene.camera.matrix_world.inverted()
  19. projection_matrix = scene.camera.calc_matrix_camera(
  20. context.evaluated_depsgraph_get(), x=WIDTH, y=HEIGHT)
  21. offscreen.draw_view3d(
  22. scene,
  23. context.view_layer,
  24. context.space_data,
  25. context.region,
  26. view_matrix,
  27. projection_matrix)
  28. bgl.glDisable(bgl.GL_DEPTH_TEST)
  29. draw_texture_2d(offscreen.color_texture, (10, 10), WIDTH, HEIGHT)
  30. bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')