5 def compute_bounding_sphere(self, obj):
6 p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
7 p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
9 radius = (p1-p2).length/2
10 for v in obj.data.vertices:
13 center += d*(1-radius/d.length)/2
14 radius = (radius+d.length)/2
18 def collect_object_lods(self, obj):
20 lods += sorted([c for c in obj.children if c.lod_for_parent], key=(lambda l: l.lod_index))
21 for i, l in enumerate(lods):
22 if i>0 and l.lod_index!=i:
23 raise Exception("Invalid configuration on object {}: Inconsistent LOD indices".format(obj.name))
27 def create_mesh_exporter(self):
28 from .export_mesh import MeshExporter
29 mesh_export = MeshExporter()
32 def create_material_exporter(self):
33 from .export_material import MaterialExporter
34 material_export = MaterialExporter()
35 return material_export
37 def create_material_atlas_exporter(self):
38 from .export_material import MaterialAtlasExporter
39 material_atlas_export = MaterialAtlasExporter()
40 return material_atlas_export
42 def export_object_resources(self, context, obj, resources, material_atlases, progress):
43 if material_atlases is None:
46 lods = self.collect_object_lods(obj)
48 from .mesh import create_mesh_from_object
49 from .material import create_material_atlas
50 mesh_export = self.create_mesh_exporter()
51 material_export = self.create_material_exporter()
52 material_atlas_export = self.create_material_atlas_exporter()
54 for i, l in enumerate(lods):
55 lod_index = l.lod_index if l.lod_for_parent else 0
56 progress.push_task_slice("LOD {}".format(lod_index), i, len(lods))
59 atlas_flags = [m.render_mode!='EXTERNAL' and m.material_atlas for m in l.data.materials if m]
61 mmk = lambda m: m.shader if m.render_mode=='CUSTOM' else ""
62 material_atlas_key = mmk(l.data.materials[0])
63 key_mismatch = any(mmk(m)!=material_atlas_key for m in l.data.materials)
64 if not all(atlas_flags) or key_mismatch:
65 raise Exception("Invalid configuration on object {}: Mixed material atlas state")
67 if material_atlas_key in material_atlases:
68 material_atlas = material_atlases[material_atlas_key]
70 material_atlas = create_material_atlas(context, l.data.materials[0])
71 material_atlases[material_atlas_key] = material_atlas
73 tech_name = "{}.tech".format(material_atlas.name)
74 if tech_name not in resources:
75 material_atlas_export.export_technique_resources(material_atlas, resources)
76 resources[tech_name] = material_atlas_export.export_technique(material_atlas, resources)
77 elif l.material_slots and l.material_slots[0].material:
78 material = l.material_slots[0].material
79 if material.render_mode!='EXTERNAL':
80 tech_name = material.name+".tech"
81 if tech_name not in resources:
82 material_export.export_technique_resources(material, resources)
83 resources[tech_name] = material_export.export_technique(material, resources)
84 elif "stub.tech" not in resources:
85 resources["stub.tech"] = self.export_stub_technique()
87 mesh_name = l.data.name+".mesh"
88 if mesh_name not in resources:
89 mesh = create_mesh_from_object(context, l, material_atlas, progress)
90 mesh_res = mesh_export.export_mesh(context, mesh, progress)
91 resources[mesh_name] = mesh_res
95 def export_object(self, obj, resources, progress):
97 raise ValueError("Object {} is not a mesh".format(obj.name))
99 lods = self.collect_object_lods(obj)
101 from .datafile import Resource, Statement
102 obj_res = Resource(obj.name+".object", "object")
103 statements = obj_res.statements
105 center, radius = self.compute_bounding_sphere(obj)
106 statements.append(Statement("bounding_sphere_hint", *center, radius))
110 for i, l in enumerate(lods):
113 if l.data.name!=prev_mesh:
114 mesh_res = resources[l.data.name+".mesh"]
115 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
117 prev_mesh = l.data.name
121 material = l.material_slots[0].material
123 if material.render_mode=='EXTERNAL':
124 tech_name = material.technique
125 elif material.material_atlas:
126 tech_name = "material_atlas_{}.tech".format(os.path.splitext(material.technique)[0])
128 tech_name = material.name+".tech"
130 tech_name = "stub.tech"
132 if tech_name!=prev_tech:
133 if material and material.render_mode=='EXTERNAL':
134 lod_st.append(Statement("technique", material.technique))
136 lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
137 prev_tech = tech_name
140 st = Statement("level_of_detail", i)
142 statements.append(st)
146 progress.set_progress(1.0)
150 def export_stub_technique(self):
151 from .datafile import Resource, Statement, Token
152 tech_res = Resource("stub.tech", "technique")
153 pass_st = Statement("method", "")
154 tech_res.statements.append(pass_st)
155 mat_st = Statement("material")
156 pass_st.sub.append(mat_st)
157 mat_st.sub.append(Statement("type", Token("basic")))