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 export_object_resources(self, context, obj, resources, material_atlases, progress):
28 if material_atlases is None:
31 lods = self.collect_object_lods(obj)
33 from .export_mesh import MeshExporter
34 from .export_material import MaterialAtlasExporter, MaterialExporter
35 from .mesh import create_mesh_from_object
36 from .material import Material, create_material_atlas
37 mesh_export = MeshExporter()
38 material_export = MaterialExporter()
39 material_atlas_export = MaterialAtlasExporter()
41 for i, l in enumerate(lods):
42 lod_index = l.lod_index if l.lod_for_parent else 0
43 progress.push_task_slice("LOD {}".format(lod_index), i, len(lods))
46 atlas_flags = [m.render_mode!='EXTERNAL' and m.material_atlas for m in l.data.materials if m]
48 mmk = lambda m: m.shader if m.render_mode=='CUSTOM' else ""
49 material_atlas_key = mmk(l.data.materials[0])
50 key_mismatch = any(mmk(m)!=material_atlas_key for m in l.data.materials)
51 if not all(atlas_flags) or key_mismatch:
52 raise Exception("Invalid configuration on object {}: Mixed material atlas state")
54 if material_atlas_key in material_atlases:
55 material_atlas = material_atlases[material_atlas_key]
57 material_atlas = create_material_atlas(context, l.data.materials[0])
58 material_atlases[material_atlas_key] = material_atlas
60 tech_name = "{}.tech".format(material_atlas.name)
61 if tech_name not in resources:
62 material_atlas_export.export_technique_resources(material_atlas, resources)
63 resources[tech_name] = material_atlas_export.export_technique(material_atlas, resources)
64 elif l.material_slots and l.material_slots[0].material:
65 material = l.material_slots[0].material
66 if material.render_mode!='EXTERNAL':
67 tech_name = material.name+".tech"
68 if tech_name not in resources:
69 material = Material(material)
70 material_export.export_technique_resources(material, resources)
71 resources[tech_name] = material_export.export_technique(material, resources)
72 elif "stub.tech" not in resources:
73 resources["stub.tech"] = self.export_stub_technique()
75 mesh_name = l.data.name+".mesh"
76 if mesh_name not in resources:
77 mesh = create_mesh_from_object(context, l, material_atlas, progress)
78 mesh_res = mesh_export.export_mesh(context, mesh, progress)
79 resources[mesh_name] = mesh_res
83 def export_object(self, obj, resources, progress):
85 raise ValueError("Object {} is not a mesh".format(obj.name))
87 lods = self.collect_object_lods(obj)
89 from .datafile import Resource, Statement
90 obj_res = Resource(obj.name+".object", "object")
91 statements = obj_res.statements
93 center, radius = self.compute_bounding_sphere(obj)
94 statements.append(Statement("bounding_sphere_hint", *center, radius))
98 for i, l in enumerate(lods):
101 if l.data.name!=prev_mesh:
102 mesh_res = resources[l.data.name+".mesh"]
103 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
105 prev_mesh = l.data.name
109 material = l.material_slots[0].material
111 if material.render_mode=='EXTERNAL':
112 tech_name = material.technique
113 elif material.material_atlas:
114 tech_name = "material_atlas_{}.tech".format(os.path.splitext(material.technique)[0])
116 tech_name = material.name+".tech"
118 tech_name = "stub.tech"
120 if tech_name!=prev_tech:
121 if material and material.render_mode=='EXTERNAL':
122 lod_st.append(Statement("technique", material.technique))
124 lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
125 prev_tech = tech_name
128 st = Statement("level_of_detail", i)
130 statements.append(st)
134 progress.set_progress(1.0)
138 def export_stub_technique(self):
139 from .datafile import Resource, Statement, Token
140 tech_res = Resource("stub.tech", "technique")
141 pass_st = Statement("method", "")
142 tech_res.statements.append(pass_st)
143 mat_st = Statement("material")
144 pass_st.sub.append(mat_st)
145 mat_st.sub.append(Statement("type", Token("basic")))