]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Use low-level handles when computing descriptor set hashes
[libs/gl.git] / blender / io_mspgl / export_scene.py
1 import math
2 import os
3 import itertools
4 import mathutils
5
6 class SceneExporter:
7         def export_to_file(self, context, out_fn, *, selected_only=False, visible_only=True, collection=True, skip_existing=True):
8                 from .util import Progress
9                 progress = Progress(context)
10
11                 from .scene import create_scene_from_current
12                 scene = create_scene_from_current(context, selected_only=selected_only, visible_only=visible_only)
13
14                 resources = {}
15                 self.export_scene_resources(context, scene, resources, progress)
16                 scene_res = self.export_scene(scene, resources)
17                 progress.set_progress(1.0)
18
19                 path, base = os.path.split(out_fn)
20                 base, ext = os.path.splitext(base)
21
22                 if collection:
23                         existing = None
24                         if skip_existing:
25                                 existing = lambda r: not os.path.exists(os.path.join(path, r.name))
26                         scene_res.write_collection(out_fn, filter=existing)
27                 else:
28                         scene_res.write_to_file(out_fn)
29                         for r in scene_res.collect_references():
30                                 r.write_to_file(os.path.join(path, r.name))
31
32         def export_scene_resources(self, context, scene, resources, progress):
33                 from .export import DataExporter
34                 data_exporter = DataExporter()
35
36                 data_exporter.export_resources(context, scene.prototypes, resources, None, progress)
37
38         def export_scene(self, scene, resources):
39                 from .datafile import Resource, Statement, Token
40                 scene_res = Resource(scene.name+".scene", "scene")
41
42                 if scene.background_set or (scene.instances and scene.blended_instances):
43                         scene_res.statements.append(Statement("type", Token("ordered")))
44                         if scene.background_set:
45                                 scene_res.statements.append(scene_res.create_reference_statement("scene", resources[scene.background_set.name+".scene"]))
46
47                         if scene.instances:
48                                 st = Statement("scene")
49                                 st.sub.append(Statement("type", Token("simple")))
50                                 self.add_instances(scene_res, st.sub, scene.instances, resources)
51                                 scene_res.statements.append(st)
52
53                         if scene.blended_instances:
54                                 st = Statement("scene")
55                                 st.sub.append(Statement("type", Token("zsorted")))
56                                 self.add_instances(scene_res, st.sub, scene.blended_instances, resources)
57                                 scene_res.statements.append(st)
58                 else:
59                         scene_type = "zsorted" if scene.blended_instances else "simple"
60                         scene_res.statements.append(Statement("type", Token(scene_type)))
61
62                         self.add_instances(scene_res, scene_res.statements, scene.instances, resources)
63                         self.add_instances(scene_res, scene_res.statements, scene.blended_instances, resources)
64
65                 return scene_res
66
67         def add_instances(self, scene_res, statements, instances, resources):
68                 from .datafile import Statement
69
70                 for i in instances:
71                         obj_res = resources[i.prototype.name+".object"]
72                         st = scene_res.create_reference_statement("object", obj_res, i.name)
73
74                         ss = Statement("transform")
75
76                         loc = i.matrix_world.to_translation()
77                         ss.sub.append(Statement("position", *tuple(loc)))
78
79                         quat = i.matrix_world.to_quaternion()
80                         if i.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
81                                 angles = [a*180/math.pi for a in quat.to_euler()]
82                                 ss.sub.append(Statement("euler", *angles));
83                         else:
84                                 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
85
86                         scale = i.matrix_world.to_scale()
87                         ss.sub.append(Statement("scale", *tuple(scale)))
88
89                         st.sub.append(ss)
90                         statements.append(st)
91
92         def export_sequence_resources(self, scene, resources):
93                 from .datafile import Resource, Statement, Token
94
95                 lights = []
96                 s = scene
97                 while s:
98                         lights += s.lights
99                         s = s.background_set
100
101                 from .util import make_unique
102                 lights = make_unique(lights)
103
104                 from .export_light import LightExporter
105                 light_exporter = LightExporter()
106                 for l in lights:
107                         light_name = l.name+".light"
108                         if light_name not in resources:
109                                 resources[light_name] = light_exporter.export_light(l)
110
111                 lighting_name = scene.name+".lightn"
112                 if lighting_name not in resources:
113                         lighting_res = Resource(lighting_name, "lighting")
114                         lighting_res.statements.append(Statement("ambient", *tuple(scene.ambient_light)))
115                         for l in lights:
116                                 lighting_res.statements.append(lighting_res.create_reference_statement("light", resources[l.name+".light"]))
117
118                         resources[lighting_name] = lighting_res
119
120         def export_sequence(self, scene, resources):
121                 from .datafile import Resource, Statement, Token
122                 seq_res = Resource(scene.name+".seq", "sequence")
123
124                 if scene.use_hdr:
125                         seq_res.statements.append(Statement("hdr", True))
126
127                 self.add_clear(seq_res.statements, (0.0, 0.0, 0.0, 0.0), 1.0)
128
129                 scene_res = resources[scene.name+".scene"]
130                 seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res))
131
132                 lighting_res = resources[scene.name+".lightn"]
133
134                 any_opaque = False
135                 any_blended = False
136                 use_ibl = False
137                 use_shadow = False
138                 shadowed_lights = []
139                 shadow_casters = []
140                 s = scene
141                 while s:
142                         if s.instances:
143                                 any_opaque = True
144                         if s.blended_instances:
145                                 any_blended = True
146                         if s.use_ibl:
147                                 use_ibl = True
148                         if s.use_shadow:
149                                 use_shadow = True
150                         shadowed_lights += [l.data for l in s.lights if l.data.use_shadow]
151                         for i in itertools.chain(s.instances, s.blended_instances):
152                                 p = i.prototype
153                                 if p.material_slots and p.material_slots[0].material and p.material_slots[0].material.shadow_method!='NONE':
154                                         shadow_casters.append(i)
155                         s = s.background_set
156
157                 shadowed_lights.sort(key=lambda l:l.shadow_map_size, reverse=True)
158
159                 main_tags = []
160                 if any_opaque:
161                         main_tags.append("")
162                 if any_blended:
163                         main_tags.append("blended")
164
165                 content = "content"
166                 if use_ibl and scene.use_sky:
167                         self.add_auxiliary_sequence(seq_res, "environment", "sky", ((0.0, 0.0, 0.0, 0.0), 1.0), main_tags, lighting_res)
168
169                         st = Statement("effect", "environment")
170                         st.sub.append(Statement("type", Token("environment_map")))
171                         st.sub.append(Statement("size", 32))
172                         st.sub.append(Statement("roughness_levels", 2))
173                         st.sub.append(Statement("fixed_position", 0.0, 0.0, 0.0))
174                         st.sub.append(Statement("content", content))
175                         st.sub.append(Statement("environment", "environment_sequence"))
176
177                         seq_res.statements.append(st)
178                         content = "environment"
179
180                 if scene.use_sky:
181                         st = Statement("effect", "sky")
182                         st.sub.append(Statement("type", Token("sky")))
183                         st.sub.append(seq_res.create_reference_statement("sun", resources[scene.sun_light.name+".light"]))
184                         st.sub.append(Statement("content", content))
185
186                         seq_res.statements.append(st)
187                         content = "sky"
188
189                 if use_shadow:
190                         self.add_auxiliary_sequence(seq_res, "shadow", "content", (None, 1.0), ["shadow"], None)
191                         self.add_auxiliary_sequence(seq_res, "thsm", "content", (None, 1.0), ["shadow_thsm"], None)
192
193                         st = Statement("effect", "shadow_map")
194                         st.sub.append(Statement("type", Token("shadow_map")))
195                         st.sub.append(Statement("size", *self.compute_shadowmap_size(shadowed_lights)))
196                         target, radius = self.compute_bounding_sphere(shadow_casters)
197                         st.sub.append(Statement("target", *target))
198                         st.sub.append(Statement("radius", radius))
199                         st.sub.append(Statement("content", content))
200                         st.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
201                         for l in shadowed_lights:
202                                 ss = seq_res.create_reference_statement("light", resources[l.name+".light"])
203                                 ss.sub.append(Statement("size", int(l.shadow_map_size)))
204                                 shadow_caster = "thsm_sequence" if l.type=='POINT' else "shadow_sequence"
205                                 ss.sub.append(Statement("shadow_caster", shadow_caster))
206                                 st.sub.append(ss)
207
208                         seq_res.statements.append(st)
209                         content = "shadow_map"
210
211                 self.add_content_steps(seq_res, content, lighting_res, main_tags)
212
213                 if scene.use_ao:
214                         ss = Statement("postprocessor")
215                         ss.sub.append(Statement("type", Token("ambient_occlusion")))
216                         ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
217                         ss.sub.append(Statement("samples", scene.ao_samples))
218                         seq_res.statements.append(ss)
219
220                 if scene.use_hdr:
221                         ss = Statement("postprocessor")
222                         ss.sub.append(Statement("type", Token("bloom")))
223                         seq_res.statements.append(ss)
224
225                         ss = Statement("postprocessor")
226                         ss.sub.append(Statement("type", Token("colorcurve")))
227                         ss.sub.append(Statement("exposure_adjust", scene.exposure))
228                         ss.sub.append(Statement("srgb"))
229                         seq_res.statements.append(ss)
230                 else:
231                         # Add a colorcurve with linear response to convert into sRGB color space
232                         ss = Statement("postprocessor")
233                         ss.sub.append(Statement("type", Token("colorcurve")))
234                         ss.sub.append(Statement("brightness_response", 1.0))
235                         ss.sub.append(Statement("srgb"))
236                         seq_res.statements.append(ss)
237
238                 return seq_res
239
240         def add_clear(self, statements, color, depth):
241                 from .datafile import Statement
242
243                 st = Statement("clear")
244                 if color is not None:
245                         st.sub.append(Statement("color", *color))
246                 if depth is not None:
247                         st.sub.append(Statement("depth", depth))
248                 statements.append(st)
249
250         def add_content_steps(self, seq_res, renderable, lighting, tags):
251                 from .datafile import Statement, Token
252
253                 for t in tags:
254                         st = Statement("step", t, renderable)
255                         st.sub.append(Statement("depth_test", Token("LEQUAL")))
256                         if lighting:
257                                 st.sub.append(seq_res.create_reference_statement("lighting", lighting))
258                         seq_res.statements.append(st)
259
260         def add_auxiliary_sequence(self, seq_res, aux_name, content, clear_values, step_tags, lighting):
261                 seq_name = os.path.splitext(seq_res.name)[0]
262
263                 from .datafile import Resource, Statement
264                 aux_seq_res = Resource("{}_{}.seq".format(seq_name, aux_name), "sequence")
265                 self.add_clear(aux_seq_res.statements, *clear_values)
266                 aux_seq_res.statements.append(Statement("renderable", "content"))
267                 self.add_content_steps(aux_seq_res, "content", lighting, step_tags)
268
269                 st = seq_res.create_reference_statement("sequence", aux_name+"_sequence", aux_seq_res)
270                 st.sub.append(Statement("renderable", "content", content))
271                 seq_res.statements.append(st)
272
273         def compute_shadowmap_size(self, lights):
274                 total_area = 0
275                 for l in lights:
276                         s = int(l.shadow_map_size)
277                         total_area += s*s
278
279                 size = 1
280                 while size*size<total_area:
281                         size *= 2
282                 if size*size>total_area*2:
283                         return (size, size//2)
284                 else:
285                         return (size, size)
286
287         def compute_bounding_sphere(self, instances):
288                 points = []
289                 for i in instances:
290                         points += [i.matrix_world@mathutils.Vector(c) for c in i.prototype.bound_box]
291
292                 from .util import compute_bounding_sphere
293                 return compute_bounding_sphere(points)