-
Notifications
You must be signed in to change notification settings - Fork 0
/
illu.py
212 lines (171 loc) · 8.17 KB
/
illu.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import bpy
import bgl
import gpu
import time
from . utils import *
from . shaders import *
from . geometry import *
def render(all = False, node = None):
T = time.time()
rendered = []
failed = []
#Get 2d_shade nodes
if node:
nodes = [node,]
else :
nodes = []
scene_materials = []
if all:
objs = bpy.context.scene.objects
else:
objs = bpy.context.selected_objects
for obj in objs:
for slot in obj.material_slots:
material = slot.material
if material:
scene_materials.append(material)
for material in bpy.data.materials:
if material in scene_materials:
if material.node_tree is not None:
for node_tree in traverse_node_tree(material.node_tree):
for node in node_tree.nodes:
if node.bl_idname == 'ILLU_2DShade':
if node.objects is not None:
nodes.append (node)
#Build scene geometry
geo_objects = []
already_done = {}
shadow_objects = []
#Geo
for node in nodes:
geometry = Geometry(node.objects, node)
geo_objects.append(geometry)
already_done[node.objects] = geometry
#Shadow
for obj in bpy.context.scene.objects:
if obj.illu.cast_shadow and obj.type == 'MESH' and obj.hide_render is False:
if obj in already_done.keys():
geometry = already_done[obj]
else :
geometry = Geometry(obj)
shadow_objects.append(geometry)
#Render nodes
for geo_object in geo_objects:
print (geo_object.object.name)
result = render_node(geo_object, shadow_objects)
if result:
rendered.append(geo_object.object.name)
else:
failed.append(geo_object.object.name)
#print ((time.time()- T)*1000)
return rendered, failed
def render_node(geo, shadow_objects):
dim_x, dim_y = get_resolution()
ratio = dim_x / dim_y
if ratio > 1:
dim_x = geo.texture_size
dim_y = int(dim_x / ratio)
else:
dim_y = geo.texture_size
dim_x = int(dim_y * ratio)
#Create buffers
base_buffer = gpu.types.GPUOffScreen(dim_x, dim_y)
baseB_buffer = gpu.types.GPUOffScreen(dim_x, dim_y)
depth_buffer = gpu.types.GPUOffScreen(dim_x, dim_y)
sdf_buffer = gpu.types.GPUOffScreen(dim_x, dim_y)
erosion_buffer = gpu.types.GPUOffScreen(dim_x, dim_y)
shadow_buffer = gpu.types.GPUOffScreen(dim_x, dim_y)
line_buffer = gpu.types.GPUOffScreen(dim_x, dim_y)
noise_buffer = gpu.types.GPUOffScreen(dim_x, dim_y)
#Creation du modele
depth_precision = geo.distance * geo.smoothness /10
#Shadow Buffer
if shadow_objects:
vertices_shadow, indices_shadow = build_shadow(shadow_objects, geo)
bgl_shadow(shadow_buffer, dim_x, dim_y, geo.vertices, geo.indices, geo.colorsA, vertices_shadow, indices_shadow, geo.light, geo.shadow_size, geo.soft_shadow)
#Base render
bgl_base_render(base_buffer, dim_x, dim_y, geo.vertices, geo.indices, geo.colorsA)
bgl_base_render(baseB_buffer, dim_x, dim_y, geo.vertices, geo.indices, geo.colorsB)
bgl_depth_render(depth_buffer, dim_x, dim_y, geo.vertices, geo.indices, geo.colorsA)
# #Distance field buffer (transparence)
copy_buffer(base_buffer, sdf_buffer, dim_x, dim_y)
if geo.self_shading:
bgl_filter_expand(sdf_buffer, dim_x, dim_y, 3, channel = (1,1,1,1))
bgl_filter_distance_field(sdf_buffer, dim_x, dim_y, geo.scale)
bgl_filter_sss(sdf_buffer, depth_buffer, dim_x, dim_y, samples = 20, radius = 20, depth_precision = depth_precision)
merge_buffers(base_buffer, sdf_buffer, "merge_SDF_post", dim_x, dim_y)
#Decal (shading)
if geo.self_shading:
bgl_filter_decal(base_buffer, depth_buffer, dim_x, dim_y, geo.light, geo.scale, depth_precision, geo.angle)
merge_buffers(base_buffer, baseB_buffer, "merge_decal", dim_x, dim_y)
bgl_filter_sss(base_buffer, depth_buffer, dim_x, dim_y, samples = int(60*geo.scale), radius = 20*geo.scale, depth_precision = depth_precision, channel = (1,0,0,0))
#Ajouter le trait
bgl_filter_line(base_buffer, depth_buffer, dim_x, dim_y, geo.line_detection, geo.line_light, False, depth_precision)
bgl_filter_sss(base_buffer, depth_buffer, dim_x, dim_y, samples = 10, radius = geo.line_scale/2, depth_precision = depth_precision, channel = (0,0,1,0))
bgl_filter_custom(base_buffer, dim_x, dim_y, "line_filter", geo.line_scale*2)
#Antialiasing trait
bgl_filter_sss(base_buffer, depth_buffer, dim_x, dim_y, samples = 5, radius = geo.scale, channel = (0,0,1,0))
#Merge Shadow
if shadow_objects:
if geo.self_shading:
merge_buffers(base_buffer, shadow_buffer, "merge_shadow", dim_x, dim_y, variable = geo.shadows)
else:
merge_buffers(base_buffer, shadow_buffer, "merge_shadow_simple", dim_x, dim_y, variable = geo.shadows)
#Border
border= geo.noise_diffusion*20
copy_buffer(base_buffer, erosion_buffer, dim_x, dim_y)
if geo.self_shading:
bgl_filter_expand(erosion_buffer, dim_x, dim_y, 3, channel = (0,0,1,1))
bgl_filter_expand(erosion_buffer, dim_x, dim_y, -border)
bgl_filter_sss(erosion_buffer, depth_buffer, dim_x, dim_y, samples = 30, radius = max(geo.noise_diffusion*100, 7), channel = (0,0,0,1))
if geo.self_shading:
merge_buffers(base_buffer, erosion_buffer, "merge_noise", dim_x, dim_y)
else:
merge_buffers(base_buffer, erosion_buffer, "merge_noise_simple", dim_x, dim_y)
#Antialiasing
bgl_filter_expand(base_buffer, dim_x, dim_y, 2)
bgl_filter_sss(base_buffer, depth_buffer, dim_x, dim_y, samples = 10, radius = 1, depth_precision = depth_precision, channel = (1,1,0,0))
#Bake
if geo.bake_to_uvs:
bake_buffer = gpu.types.GPUOffScreen(geo.texture_size, geo.texture_size)
bgl_filter_expand(base_buffer, dim_x, dim_y, 20, channel = (1,1,1,1))
bake_to_texture(base_buffer, bake_buffer, dim_x, dim_y, geo.vertices, geo.uvs, geo.uv_indices, geo.loop_indices)
bgl_filter_expand(bake_buffer, geo.texture_size, geo.texture_size, 10)
#Lecture du buffer
with bake_buffer.bind():
buffer = bgl.Buffer(bgl.GL_FLOAT, geo.texture_size * geo.texture_size * 4)
bgl.glReadBuffer(bgl.GL_BACK)
bgl.glReadPixels(0, 0, geo.texture_size, geo.texture_size, bgl.GL_RGBA, bgl.GL_FLOAT, buffer)
bake_buffer.free()
dim_x = geo.texture_size
dim_y = geo.texture_size
else:
bgl_filter_scale(base_buffer, dim_x, dim_y, upscale_factor())
with base_buffer.bind():
buffer = bgl.Buffer(bgl.GL_FLOAT, dim_x * dim_y * 4)
bgl.glReadBuffer(bgl.GL_BACK)
bgl.glReadPixels(0, 0, dim_x, dim_y, bgl.GL_RGBA, bgl.GL_FLOAT, buffer)
#Suppression des buffers
shadow_buffer.free()
sdf_buffer.free()
erosion_buffer.free()
base_buffer.free()
baseB_buffer.free()
depth_buffer.free()
line_buffer.free()
noise_buffer.free()
#Enregistrement des images
buffer_to_image( geo.image_name, buffer, dim_x, dim_y)
return True