From: Mikko Rasa Date: Sat, 16 Oct 2021 10:11:49 +0000 (+0300) Subject: Recognize transparent materials in Blender and export them accordingly X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9b371e56adc1d6be89f2d30881c3759a0d5f8eca;p=libs%2Fgl.git Recognize transparent materials in Blender and export them accordingly Objects with transparency are exported into a separate, Z-sorted sub-scene. --- diff --git a/blender/io_mspgl/export_material.py b/blender/io_mspgl/export_material.py index 7e8f6066..62319b8a 100644 --- a/blender/io_mspgl/export_material.py +++ b/blender/io_mspgl/export_material.py @@ -1,7 +1,7 @@ import os def create_technique_resource(material, resources): - from .datafile import Resource, Statement + from .datafile import Resource, Statement, Token tech_res = Resource(material.name+".tech", "technique") mat_res = resources[material.name+".mat"] @@ -25,10 +25,14 @@ def create_technique_resource(material, resources): tech_res.statements.append(st) else: - st = Statement("method", "") + base_method = "blended" if material.blended else "" + st = Statement("method", base_method) if mat_res: st.sub.append(tech_res.create_embed_statement("material", mat_res)) + if material.blended: + ss.sub.append(Statement("blend", Token("SRC_ALPHA"), Token("ONE_MINUS_SRC_ALPHA"))) + if material.render_mode!='CUSTOM': if material.receive_shadows: st.sub.append(Statement("receive_shadows", True)) diff --git a/blender/io_mspgl/export_scene.py b/blender/io_mspgl/export_scene.py index 1a1bbab0..162a1882 100644 --- a/blender/io_mspgl/export_scene.py +++ b/blender/io_mspgl/export_scene.py @@ -37,19 +37,28 @@ class SceneExporter: from .datafile import Resource, Statement, Token scene_res = Resource(scene.name+".scene", "scene") - if scene.background_set: + if scene.background_set or (scene.instances and scene.blended_instances): scene_res.statements.append(Statement("type", Token("ordered"))) if scene.background_set: scene_res.statements.append(scene_res.create_reference_statement("scene", resources[scene.background_set.name+".scene"])) - st = Statement("scene") - st.sub.append(Statement("type", Token("simple"))) - self.add_instances(scene_res, st.sub, scene.instances, resources) - scene_res.statements.append(st) + if scene.instances: + st = Statement("scene") + st.sub.append(Statement("type", Token("simple"))) + self.add_instances(scene_res, st.sub, scene.instances, resources) + scene_res.statements.append(st) + + if scene.blended_instances: + st = Statement("scene") + st.sub.append(Statement("type", Token("zsorted"))) + self.add_instances(scene_res, st.sub, scene.blended_instances, resources) + scene_res.statements.append(st) else: - scene_res.statements.append(Statement("type", Token("simple"))) + scene_type = "zsorted" if scene.blended_instances else "simple" + scene_res.statements.append(Statement("type", Token(scene_type))) self.add_instances(scene_res, scene_res.statements, scene.instances, resources) + self.add_instances(scene_res, scene_res.statements, scene.blended_instances, resources) return scene_res @@ -118,11 +127,32 @@ class SceneExporter: ss.sub.append(Statement("depth", 1.0)) seq_res.statements.append(ss) - ss = Statement("step", "", "content") - ss.sub.append(Statement("depth_test", Token("LEQUAL"))) - ss.sub.append(seq_res.create_reference_statement("lighting", resources[scene.name+".lightn"])) - ss.sub.append(seq_res.create_reference_statement("scene", resources[scene.name+".scene"])) - seq_res.statements.append(ss) + scene_res = resources[scene.name+".scene"] + lighting_res = resources[scene.name+".lightn"] + + any_opaque = False + any_blended = False + s = scene + while s: + if s.instances: + any_opaque = True + if s.blended_instances: + any_blended = True + 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)) + ss.sub.append(seq_res.create_reference_statement("scene", scene_res)) + seq_res.statements.append(ss) + + 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)) + ss.sub.append(seq_res.create_reference_statement("scene", scene_res)) + seq_res.statements.append(ss) if scene.use_ao: ss = Statement("ambient_occlusion") diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index 73f33d5c..88e251b8 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -124,6 +124,7 @@ class Material: self.uniforms = material.uniforms[:] self.receive_shadows = material.receive_shadows self.cast_shadows = (material.shadow_method!='NONE') + self.blended = (material.blend_method=='BLEND') self.image_based_lighting = material.image_based_lighting if self.render_mode=='EXTERNAL' and not self.technique: diff --git a/blender/io_mspgl/scene.py b/blender/io_mspgl/scene.py index 28c30ad7..8f4f285b 100644 --- a/blender/io_mspgl/scene.py +++ b/blender/io_mspgl/scene.py @@ -23,6 +23,7 @@ class Scene: self.camera = scene.camera self.prototypes = [] self.instances = [] + self.blended_instances = [] self.lights = [] self.ambient_light = mathutils.Color((0.0, 0.0, 0.0)) self.exposure = scene.view_settings.exposure @@ -55,8 +56,11 @@ class Scene: if o.type=='MESH': clones = [c for c in objects if is_same_object(o, c)] self.prototypes.append(o) + instance_list = self.instances + if o.material_slots and o.material_slots[0].material and o.material_slots[0].material.blend_method=='BLEND': + instance_list = self.blended_instances for c in clones: - self.instances.append(Instance(c, o)) + instance_list.append(Instance(c, o)) processed.add(c.name) elif o.type=='LIGHT': self.lights.append(o)