Created
March 2, 2019 15:26
-
-
Save furby-tm/4596d37d5be1575618e33896eeb52e74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gpu | |
import random | |
from gpu_extras.batch import batch_for_shader | |
from gpu_extras.presets import draw_circle_2d | |
vertex_shader = ''' | |
uniform mat4 ModelViewProjectionMatrix; | |
/* Keep in sync with intern/opencolorio/gpu_shader_display_transform_vertex.glsl */ | |
in vec2 texCoord; | |
in vec2 pos; | |
out vec2 texCoord_interp; | |
void main() | |
{ | |
gl_Position = ModelViewProjectionMatrix * vec4(pos.xy, 0.0f, 1.0f); | |
gl_Position.z = 1.0; | |
texCoord_interp = texCoord; | |
} | |
''' | |
fragment_shader = ''' | |
in vec2 texCoord_interp; | |
out vec4 fragColor; | |
uniform sampler2D image; | |
uniform bool ColorMode; | |
void main() | |
{ | |
if (ColorMode) { | |
fragColor = texture(image, texCoord_interp); | |
} else { | |
fragColor = texture(image, texCoord_interp).rrrr; | |
} | |
} | |
''' | |
from bpy.types import SpaceView3D, SpaceImageEditor | |
def tag_redraw_all_3dviews(): | |
for window in bpy.context.window_manager.windows: | |
for area in window.screen.areas: | |
if area.type == 'VIEW_3D': | |
for region in area.regions: | |
if region.type == 'WINDOW': | |
region.tag_redraw() | |
def callback_enable(*args, overlay='POST_VIEW'): | |
handle_pixel = SpaceView3D.draw_handler_add(draw_callback_px, args, 'WINDOW', overlay) | |
tag_redraw_all_3dviews() | |
def restore_opengl_defaults(): | |
bgl.glLineWidth(1) | |
bgl.glDisable(bgl.GL_BLEND) | |
def draw_callback_px(n_id, data): | |
context = bpy.context.space_data | |
drawing_func = data.get('custom_function') | |
args = data.get('args', (None,)) | |
drawing_func(args) | |
restore_opengl_defaults() | |
def init_texture(width, height, texname, texture, clr): | |
bgl.glEnable(bgl.GL_TEXTURE_2D) | |
bgl.glActiveTexture(bgl.GL_TEXTURE0) | |
bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) | |
bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) | |
bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) | |
bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) | |
bgl.glTexImage2D( | |
bgl.GL_TEXTURE_2D, | |
0, bgl.GL_RGB, width, height, | |
0, bgl.GL_RGB, bgl.GL_FLOAT, texture | |
) | |
def simple_screen(args): | |
""" shader draw function for the texture """ | |
# border_color = (0.390805, 0.754022, 1.000000, 1.00) | |
texture, texname, width, height, batch, shader, cMode = args | |
def draw_texture(x=0, y=0, w=width, h=height, texname=texname, c=cMode): | |
# function to draw a texture | |
bgl.glDisable(bgl.GL_DEPTH_TEST) | |
act_tex = bgl.Buffer(bgl.GL_INT, 1) | |
bgl.glBindTexture(bgl.GL_TEXTURE_2D, texname) | |
shader.bind() | |
shader.uniform_int("image", act_tex) | |
shader.uniform_bool("ColorMode", c) | |
batch.draw(shader) | |
# restoring settings | |
bgl.glBindTexture(bgl.GL_TEXTURE_2D, act_tex[0]) | |
bgl.glDisable(bgl.GL_TEXTURE_2D) | |
draw_texture(x=x, y=y, w=width, h=height, texname=texname, c=cMode) | |
def generate_batch_shader(args): | |
x, y, w, h = args | |
positions = ((x, y), (x + w, y), (x + w, y - h), (x, y - h)) | |
indices = ((0, 1), (1, 1), (1, 0), (0, 0)) | |
shader = gpu.types.GPUShader(vertex_shader, fragment_shader) | |
batch = batch_for_shader(shader, 'TRI_FAN', {"pos": positions, "texCoord": indices}) | |
return batch, shader | |
def adjust_position_and_dimensions(x, y, width, height): | |
""" | |
this could also return scale for a blf notation in the vacinity of the texture | |
""" | |
scale = 0.5 | |
multiplier = 1.0 | |
x, y = [x * multiplier, y * multiplier] | |
width, height = [width * scale, height * scale] | |
return x, y, width, height | |
total_size = width * height * 3 | |
texture = bgl.Buffer(bgl.GL_FLOAT, len(rect), rect) | |
x = 0 | |
y = 0 | |
name = bgl.Buffer(bgl.GL_INT, 1) | |
bgl.glGenTextures(1, name) | |
#self.texture[n_id] = name[0] | |
init_texture(width, height, name[0], texture, 6407) | |
x, y, width, height = adjust_position_and_dimensions(x, y, width, height) | |
batch, shader = generate_batch_shader((x, y, width, height)) | |
is_multi_channel = 1 == 1 | |
cMode = (is_multi_channel,) | |
draw_data = { | |
'tree_name': "ARNOLD_WORLD_NODETREE", | |
'mode': 'custom_function', | |
'custom_function': simple_screen, | |
'loc': (0, 0), | |
'args': (texture, name[0], width, height, batch, shader, cMode) | |
} | |
callback_enable(0, draw_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment