From 90298c24a2cab088093006ef92a685d41c5a6ac2 Mon Sep 17 00:00:00 2001 From: Shashank Shekhar Date: Mon, 9 May 2022 08:07:28 +0200 Subject: EEVEE & Viewport: Add a built-in shader called 3D_IMAGE, and expose to the python API Adds an example python script to the documentation for the 3D_IMAGE shader. The **use-case** is to draw textures with 3D vertex positions, in XR views as well as non-XR views (in a simpler manner). **Testing**: I've tested that this compiles and works on my Macbook (with the example python script included in this change). I don't have access to a Windows or Linux machine right now, but this change doesn't look platform-specific and no new glsl shaders have been added or edited by this change. I'll try to get access to a Windows machine, but if someone does have one, I'd be really grateful if they could try this change. Thanks! **Problem addressed**: The existing 2D_IMAGE shader (exposed in the python API) gets near-clipped when drawn in the XR view, regardless of the near-clip settings. Additionally, the 2D_IMAGE shader only accepts 2D positions for the image vertices, which means drawing textures in 3D requires providing 2D coordinates and then pushing a transform-rotate-scale matrix to the GPU, even for non-XR (i.e. WINDOW) views. The 3D_IMAGE shader is simpler: it accepts 3D vertex positions, and doesn't require any additional work by the scripter. **Workaround**: The current workaround is to use custom shaders in the python script. **Non-intrusive change**: No new glsl shaders were added. This change just bundles two existing shaders: the vertex shader used by the 3D_IMAGE_MODULATE_ALPHA shader, and the fragment shader used by the 2D_IMAGE shader. Reviewed By: #eevee_viewport, jbakker Differential Revision: https://developer.blender.org/D14832 --- doc/python_api/examples/gpu.6.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'doc/python_api') diff --git a/doc/python_api/examples/gpu.6.py b/doc/python_api/examples/gpu.6.py index be164a03028..5576b2d0bfe 100644 --- a/doc/python_api/examples/gpu.6.py +++ b/doc/python_api/examples/gpu.6.py @@ -29,3 +29,36 @@ def draw(): 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.bind() + shader.uniform_sampler("image", texture) + batch.draw(shader) + + +bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW') -- cgit v1.2.3