X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=blender%2Fio_mspgl%2Fexport_scene.py;h=ac3bf0be184e31999e285eeb54c3a29c16c1dcfd;hb=26b3d7bb741bf27468bfad7224a3d06a72579a68;hp=8666e98beee0ef9489557d28da89fdcf788ee036;hpb=f44366c8785eb7a88755cd71250a8b85289eeed2;p=libs%2Fgl.git diff --git a/blender/io_mspgl/export_scene.py b/blender/io_mspgl/export_scene.py index 8666e98b..ac3bf0be 100644 --- a/blender/io_mspgl/export_scene.py +++ b/blender/io_mspgl/export_scene.py @@ -1,22 +1,24 @@ import math import os +import itertools +import mathutils class SceneExporter: - def export_to_file(self, context, out_fn, *, selected_only=False, visible_only=True, collection=True, skip_existing=True): - from .util import Progress - progress = Progress(context) - + def export_to_file(self, ctx, out_fn, *, selected_only=False, visible_only=True, collection=True, skip_existing=True): from .scene import create_scene_from_current - scene = create_scene_from_current(context, selected_only=selected_only, visible_only=visible_only) + task = ctx.task("Preparing scene", 0.1) + scene = create_scene_from_current(task, selected_only=selected_only, visible_only=visible_only) resources = {} - self.export_scene_resources(context, scene, resources, progress) + task = ctx.task("Exporting resources", 0.9) + self.export_scene_resources(task, scene, resources) + task = ctx.task(scene, 1.0) scene_res = self.export_scene(scene, resources) - progress.set_progress(1.0) path, base = os.path.split(out_fn) base, ext = os.path.splitext(base) + task = ctx.task("Writing files", 1.0) if collection: existing = None if skip_existing: @@ -27,11 +29,11 @@ class SceneExporter: for r in scene_res.collect_references(): r.write_to_file(os.path.join(path, r.name)) - def export_scene_resources(self, context, scene, resources, progress): + def export_scene_resources(self, ctx, scene, resources): from .export import DataExporter data_exporter = DataExporter() - data_exporter.export_resources(context, scene.prototypes, resources, None, progress) + data_exporter.export_resources(ctx, scene.prototypes, resources, None) def export_scene(self, scene, resources): from .datafile import Resource, Statement, Token @@ -66,7 +68,7 @@ class SceneExporter: from .datafile import Statement for i in instances: - obj_res = resources[i.prototype+".object"] + obj_res = resources[i.prototype.name+".object"] st = scene_res.create_reference_statement("object", obj_res, i.name) ss = Statement("transform") @@ -122,10 +124,7 @@ class SceneExporter: if scene.use_hdr: seq_res.statements.append(Statement("hdr", True)) - ss = Statement("clear") - ss.sub.append(Statement("color", 0.0, 0.0, 0.0, 0.0)) - ss.sub.append(Statement("depth", 1.0)) - seq_res.statements.append(ss) + self.add_clear(seq_res.statements, (0.0, 0.0, 0.0, 0.0), 1.0) scene_res = resources[scene.name+".scene"] seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res)) @@ -134,25 +133,83 @@ class SceneExporter: any_opaque = False any_blended = False + use_ibl = False + use_shadow = False + shadowed_lights = [] + shadow_casters = [] s = scene while s: if s.instances: any_opaque = True if s.blended_instances: any_blended = True + if s.use_ibl: + use_ibl = True + if s.use_shadow: + use_shadow = True + shadowed_lights += [l.data for l in s.lights if l.data.use_shadow] + for i in itertools.chain(s.instances, s.blended_instances): + p = i.prototype + if p.material_slots and p.material_slots[0].material and p.material_slots[0].material.shadow_method!='NONE': + shadow_casters.append(i) s = s.background_set - if any_opaque: - ss = Statement("step", "", "content") - ss.sub.append(Statement("depth_test", Token("LEQUAL"))) - ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res)) - seq_res.statements.append(ss) + shadowed_lights.sort(key=lambda l:l.shadow_map_size, reverse=True) + main_tags = [] + if any_opaque: + main_tags.append("") if any_blended: - ss = Statement("step", "blended", "content") - ss.sub.append(Statement("depth_test", Token("LEQUAL"))) - ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res)) - seq_res.statements.append(ss) + main_tags.append("blended") + + content = "content" + if use_ibl and scene.use_sky: + self.add_auxiliary_sequence(seq_res, "environment", "sky", ((0.0, 0.0, 0.0, 0.0), 1.0), main_tags, lighting_res) + + st = Statement("effect", "environment") + st.sub.append(Statement("type", Token("environment_map"))) + st.sub.append(Statement("size", 32)) + st.sub.append(Statement("roughness_levels", 2)) + st.sub.append(Statement("fixed_position", 0.0, 0.0, 0.0)) + st.sub.append(Statement("content", content)) + st.sub.append(Statement("environment", "environment_sequence")) + + seq_res.statements.append(st) + content = "environment" + + if scene.use_sky: + st = Statement("effect", "sky") + st.sub.append(Statement("type", Token("sky"))) + st.sub.append(seq_res.create_reference_statement("sun", resources[scene.sun_light.name+".light"])) + st.sub.append(Statement("content", content)) + + seq_res.statements.append(st) + content = "sky" + + if use_shadow: + self.add_auxiliary_sequence(seq_res, "shadow", "content", (None, 1.0), ["shadow"], None) + self.add_auxiliary_sequence(seq_res, "thsm", "content", (None, 1.0), ["shadow_thsm"], None) + + st = Statement("effect", "shadow_map") + st.sub.append(Statement("type", Token("shadow_map"))) + st.sub.append(Statement("enable_for_method", "blended")) + st.sub.append(Statement("size", *self.compute_shadowmap_size(shadowed_lights))) + target, radius = self.compute_bounding_sphere(shadow_casters) + st.sub.append(Statement("target", *target)) + st.sub.append(Statement("radius", radius)) + st.sub.append(Statement("content", content)) + st.sub.append(seq_res.create_reference_statement("lighting", lighting_res)) + for l in shadowed_lights: + ss = seq_res.create_reference_statement("light", resources[l.name+".light"]) + ss.sub.append(Statement("size", int(l.shadow_map_size))) + shadow_caster = "thsm_sequence" if l.type=='POINT' else "shadow_sequence" + ss.sub.append(Statement("shadow_caster", shadow_caster)) + st.sub.append(ss) + + seq_res.statements.append(st) + content = "shadow_map" + + self.add_content_steps(seq_res, content, lighting_res, main_tags) if scene.use_ao: ss = Statement("postprocessor") @@ -180,3 +237,58 @@ class SceneExporter: seq_res.statements.append(ss) return seq_res + + def add_clear(self, statements, color, depth): + from .datafile import Statement + + st = Statement("clear") + if color is not None: + st.sub.append(Statement("color", *color)) + if depth is not None: + st.sub.append(Statement("depth", depth)) + statements.append(st) + + def add_content_steps(self, seq_res, renderable, lighting, tags): + from .datafile import Statement, Token + + for t in tags: + st = Statement("step", t, renderable) + st.sub.append(Statement("depth_test", Token("LEQUAL"))) + if lighting: + st.sub.append(seq_res.create_reference_statement("lighting", lighting)) + seq_res.statements.append(st) + + def add_auxiliary_sequence(self, seq_res, aux_name, content, clear_values, step_tags, lighting): + seq_name = os.path.splitext(seq_res.name)[0] + + from .datafile import Resource, Statement + aux_seq_res = Resource("{}_{}.seq".format(seq_name, aux_name), "sequence") + self.add_clear(aux_seq_res.statements, *clear_values) + aux_seq_res.statements.append(Statement("renderable", "content")) + self.add_content_steps(aux_seq_res, "content", lighting, step_tags) + + st = seq_res.create_reference_statement("sequence", aux_name+"_sequence", aux_seq_res) + st.sub.append(Statement("renderable", "content", content)) + seq_res.statements.append(st) + + def compute_shadowmap_size(self, lights): + total_area = 0 + for l in lights: + s = int(l.shadow_map_size) + total_area += s*s + + size = 1 + while size*sizetotal_area*2: + return (size, size//2) + else: + return (size, size) + + def compute_bounding_sphere(self, instances): + points = [] + for i in instances: + points += [i.matrix_world@mathutils.Vector(c) for c in i.prototype.bound_box] + + from .util import compute_bounding_sphere + return compute_bounding_sphere(points)