2018-11-12 17:53:45 +01:00
|
|
|
"""
|
2020-06-03 00:20:15 +02:00
|
|
|
Rendering the 3D View into a Texture
|
|
|
|
------------------------------------
|
2018-11-12 17:53:45 +01:00
|
|
|
|
2020-06-03 00:20:15 +02:00
|
|
|
The scene has to have a camera for this example to work.
|
|
|
|
You could also make this independent of a specific camera,
|
|
|
|
but Blender does not expose good functions to create view and projection matrices yet.
|
2018-11-12 17:53:45 +01:00
|
|
|
"""
|
|
|
|
import bpy
|
2020-06-03 00:20:15 +02:00
|
|
|
import gpu
|
|
|
|
from gpu_extras.presets import draw_texture_2d
|
2018-11-12 17:53:45 +01:00
|
|
|
|
|
|
|
WIDTH = 512
|
2020-06-03 00:20:15 +02:00
|
|
|
HEIGHT = 256
|
2018-11-12 17:53:45 +01:00
|
|
|
|
|
|
|
offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT)
|
|
|
|
|
|
|
|
|
2020-06-03 00:20:15 +02:00
|
|
|
def draw():
|
|
|
|
context = bpy.context
|
|
|
|
scene = context.scene
|
|
|
|
|
|
|
|
view_matrix = scene.camera.matrix_world.inverted()
|
|
|
|
|
|
|
|
projection_matrix = scene.camera.calc_matrix_camera(
|
|
|
|
context.evaluated_depsgraph_get(), x=WIDTH, y=HEIGHT)
|
2018-11-12 17:53:45 +01:00
|
|
|
|
2020-06-03 00:20:15 +02:00
|
|
|
offscreen.draw_view3d(
|
|
|
|
scene,
|
|
|
|
context.view_layer,
|
|
|
|
context.space_data,
|
|
|
|
context.region,
|
|
|
|
view_matrix,
|
2021-08-05 02:25:50 +02:00
|
|
|
projection_matrix,
|
2021-10-15 23:55:21 +02:00
|
|
|
do_color_management=True)
|
2018-11-19 14:32:30 +01:00
|
|
|
|
2021-04-30 15:50:50 +02:00
|
|
|
gpu.state.depth_mask_set(False)
|
|
|
|
draw_texture_2d(offscreen.texture_color, (10, 10), WIDTH, HEIGHT)
|
2018-11-12 17:53:45 +01:00
|
|
|
|
|
|
|
|
2020-06-03 00:20:15 +02:00
|
|
|
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')
|