This simplifies python code. When we call a method like shader.uniform_float("color", (1,1,1,1)), we expect the shader's uniform to be updated regardless of whether the shader is bound or not. And `batch.draw()` already calls `GPU_shader_bind` inside. Differential Revision: https://developer.blender.org/D15929
64 lines
1.4 KiB
Python
64 lines
1.4 KiB
Python
"""
|
|
2D Image
|
|
--------
|
|
|
|
To use this example you have to provide an image that should be displayed.
|
|
"""
|
|
import bpy
|
|
import gpu
|
|
from gpu_extras.batch import batch_for_shader
|
|
|
|
IMAGE_NAME = "Untitled"
|
|
image = bpy.data.images[IMAGE_NAME]
|
|
texture = gpu.texture.from_image(image)
|
|
|
|
shader = gpu.shader.from_builtin('2D_IMAGE')
|
|
batch = batch_for_shader(
|
|
shader, 'TRI_FAN',
|
|
{
|
|
"pos": ((100, 100), (200, 100), (200, 200), (100, 200)),
|
|
"texCoord": ((0, 0), (1, 0), (1, 1), (0, 1)),
|
|
},
|
|
)
|
|
|
|
|
|
def draw():
|
|
shader.bind()
|
|
shader.uniform_sampler("image", texture)
|
|
batch.draw(shader)
|
|
|
|
|
|
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')
|
|
|
|
"""
|
|
3D Image
|
|
--------
|
|
|
|
Similar to the 2D Image shader, but works with 3D positions for the image vertices.
|
|
To use this example you have to provide an image that should be displayed.
|
|
"""
|
|
import bpy
|
|
import gpu
|
|
from gpu_extras.batch import batch_for_shader
|
|
|
|
IMAGE_NAME = "Untitled"
|
|
image = bpy.data.images[IMAGE_NAME]
|
|
texture = gpu.texture.from_image(image)
|
|
|
|
shader = gpu.shader.from_builtin('3D_IMAGE')
|
|
batch = batch_for_shader(
|
|
shader, 'TRIS',
|
|
{
|
|
"pos": ((0, 0, 0), (0, 1, 1), (1, 1, 1), (1, 1, 1), (1, 0, 0), (0, 0, 0)),
|
|
"texCoord": ((0, 0), (0, 1), (1, 1), (1, 1), (1, 0), (0, 0)),
|
|
},
|
|
)
|
|
|
|
|
|
def draw():
|
|
shader.uniform_sampler("image", texture)
|
|
batch.draw(shader)
|
|
|
|
|
|
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
|