-
Notifications
You must be signed in to change notification settings - Fork 0
/
outline_renderer.py
163 lines (139 loc) · 5.66 KB
/
outline_renderer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import bpy
import gpu
import bgl
import numpy as np
from random import random
import gpu_extras.batch as gpu_extras_batch
import bmesh
vertex_shader = """
uniform mat4 perspective_matrix;
uniform mat4 matrix_world;
uniform float outline_width;
in vec3 position;
in vec3 vertex_normal;
in vec4 vertex_color;
varying vec4 vcolor;
void main()
{
vec3 vertex = (matrix_world * vec4(position, 1)).xyz + vertex_normal * vertex_color.xyz * 0.1 * outline_width;
gl_Position = perspective_matrix * vec4(vertex, 1);
vcolor = vertex_color;
}
"""
pixel_shader = """
uniform vec4 color;
varying vec4 vcolor;
void main()
{
gl_FragColor = color;
}
"""
def get_modified_mesh(object):
depsgraph = bpy.context.evaluated_depsgraph_get()
bm = bmesh.new()
mesh_to_render = bpy.data.meshes.new(object.name + "-outline_mesh")
mesh_to_render.create_normals_split()
bm.from_object(object, depsgraph, face_normals=False)
bm.to_mesh(mesh_to_render)
bm.free()
return mesh_to_render
def draw_mesh(scene):
draw_handlers = scene.custom_shaders_draw_handlers
for draw_handler in draw_handlers:
try:
bpy.types.SpaceView3D.draw_handler_remove(draw_handler, "WINDOW")
except (AttributeError, ValueError) as e:
pass
draw_handlers.remove(draw_handler)
if bpy.context.active_object.type != "MESH" or not scene.use_outline_shaders:
return
mesh = None
if scene.custom_outline_apply_modifiers:
mesh = get_modified_mesh(bpy.context.active_object)
else:
mesh = bpy.context.active_object.data
mesh.calc_loop_triangles()
mesh.calc_normals_split()
print(mesh.vertex_colors.items())
indices = np.empty((len(mesh.loop_triangles), 3), dtype=np.int)
positions = np.empty((len(mesh.loops), 3), dtype=np.float32)
normals = np.empty((len(mesh.loops), 3), dtype=np.float32)
vertex_colors = np.ones((len(mesh.loops), 4), dtype=np.float32)
mesh.loop_triangles.foreach_get(
"loops", np.reshape(indices, len(mesh.loop_triangles) * 3))
for i in range(len(mesh.loops)):
loop = mesh.loops[i]
positions[i] = mesh.vertices[loop.vertex_index].co
normals[i] = loop.normal
try:
vertex_colors[i] = mesh.vertex_colors[bpy.context.object.data.custom_outline_vertex_offsets].data[i].color
except KeyError:
pass
shader = gpu.types.GPUShader(vertex_shader, pixel_shader)
fmt = gpu.types.GPUVertFormat()
fmt.attr_add(id="position", comp_type="F32", len=3, fetch_mode="FLOAT")
fmt.attr_add(id="vertex_normal", comp_type="F32", len=3, fetch_mode="FLOAT")
fmt.attr_add(id="vertex_color", comp_type="F32", len=4, fetch_mode="FLOAT")
vbo = gpu.types.GPUVertBuf(len=len(mesh.loops), format=fmt)
vbo.attr_fill(id="position", data=positions)
vbo.attr_fill(id="vertex_normal", data=normals)
vbo.attr_fill(id="vertex_color", data=vertex_colors)
ibo = gpu.types.GPUIndexBuf(type="TRIS", seq=indices)
batch = gpu.types.GPUBatch(type="TRIS", buf=vbo, elem=ibo)
def draw():
shader.bind()
shader.uniform_float("outline_width", scene.custom_outline_width)
shader.uniform_float("color", scene.custom_outline_color)
shader.uniform_float("perspective_matrix", bpy.context.region_data.perspective_matrix)
shader.uniform_float("matrix_world", bpy.context.active_object.matrix_world)
bgl.glEnable(bgl.GL_CULL_FACE)
bgl.glCullFace(bgl.GL_FRONT)
bgl.glEnable(bgl.GL_DEPTH_TEST)
batch.draw(shader)
bgl.glDisable(bgl.GL_CULL_FACE)
bgl.glCullFace(bgl.GL_BACK)
bgl.glDisable(bgl.GL_DEPTH_TEST)
draw_handlers.append(bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW'))
if scene.custom_outline_apply_modifiers:
bpy.data.meshes.remove(mesh)
class OBJECT_PT_outline_rendering(bpy.types.Panel):
bl_label = "Outline Rendering"
bl_idname = "OBJECT_PT_outline_rendering"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "GL"
def draw(self, context):
layout = self.layout
scene = context.scene
layout.prop(scene, "use_outline_shaders")
layout.prop(scene, "custom_outline_width")
layout.prop(scene, "custom_outline_color")
layout.prop(scene, "custom_outline_apply_modifiers")
layout.prop_search(context.object.data, "custom_outline_vertex_offsets", context.object.data, "vertex_colors")
def register():
bpy.utils.register_class(OBJECT_PT_outline_rendering)
bpy.types.Scene.custom_shaders_draw_handlers = []
bpy.types.Scene.use_outline_shaders = bpy.props.BoolProperty(name = "Use Outline Shaders", default=False)
bpy.types.Scene.custom_outline_width = bpy.props.FloatProperty(name = "Outline Width", default=1.0)
bpy.types.Scene.custom_outline_apply_modifiers = bpy.props.BoolProperty(
name = "Apply Modifiers",
description = "Disable for custom normals",
default = True
)
bpy.types.Scene.custom_outline_color = bpy.props.FloatVectorProperty(
name = "Color",
size = 4,
default = (0, 0, 0, 1),
subtype = "COLOR",
min = 0,
max = 1
)
bpy.types.Mesh.custom_outline_vertex_offsets = bpy.props.StringProperty(name = "Vertex Offsets")
bpy.app.handlers.depsgraph_update_post.append(draw_mesh)
bpy.app.handlers.frame_change_post.append(draw_mesh)
def unregister():
bpy.utils.unregister_class(OBJECT_PT_outline_rendering)
bpy.app.handlers.depsgraph_update_post.remove(draw_mesh)
bpy.app.handlers.frame_change_post.remove(draw_mesh)
if __name__ == "__main__":
register()