8 return 1.055*(l**(1/2.4))-0.055
13 return os.path.split(fp)[1]
17 def external_name(out_file, ext, index):
18 path, base = os.path.split(out_file.filename)
19 base = os.path.splitext(base)[0]
21 base += "_lod{}".format(index)
27 self.material_tex = False
28 self.srgb_colors = True
30 self.separate_mesh = False
31 self.separate_tech = False
32 self.external_tech = True
33 self.shared_tech = True
34 self.export_lods = True
36 def export(self, context, out_file, objs=None, progress=None):
38 obj = context.active_object
43 for c in obj.children:
45 if c.lod_index>=len(lods):
46 lods += [None]*(c.lod_index+1-len(lods))
49 from .outfile import open_output
50 out_file = open_output(out_file)
52 p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
53 p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
55 radius = (p1-p2).length/2
56 for v in obj.data.vertices:
59 center += d*(1-radius/d.length)/2
60 radius = (radius+d.length)/2
62 out_file.write("bounding_sphere_hint", center[0], center[1], center[2], radius)
65 prev_tech = (None, None)
66 for i, l in enumerate(lods):
68 out_file.begin("level_of_detail", i)
71 same_mesh = (l.data.name==prev_mesh)
72 if i==0 or not same_mesh:
73 mesh = self.export_object_mesh(context, out_file, i, objs, progress)
74 prev_mesh = l.data.name
78 if l.material_slots and l.material_slots[0].material:
79 mat = l.material_slots[0].material.name
82 if self.external_tech and l.technique!=prev_tech[0]:
84 if i==0 or not same_tech:
85 self.export_object_technique(l, mesh, out_file, i)
86 prev_tech = (l.technique, mat)
91 def export_object_mesh(self, context, out_file, lod_index, objs, progress):
92 from .export_mesh import MeshExporter
93 mesh_export = MeshExporter()
94 for k, v in self.__dict__.items():
95 setattr(mesh_export, k, v)
97 if self.separate_mesh:
98 from .outfile import open_output
99 path, name = external_name(out_file, ".mesh", lod_index)
100 mesh_out = open_output(os.path.join(path, name))
101 mesh = mesh_export.export(context, mesh_out, objs, progress)
102 out_file.write("mesh", '"{}"'.format(name))
104 out_file.begin("mesh")
105 mesh = mesh_export.export(context, out_file, objs, progress)
110 def export_object_technique(self, obj, mesh, out_file, lod_index):
112 self.colormap = linear_to_srgb
114 self.colormap = lambda x: x
117 if obj.material_slots:
118 material = obj.material_slots[0].material
120 from .outfile import open_output
121 path, name = external_name(out_file, ".tech", lod_index)
123 if self.external_tech and obj.technique:
124 if obj.inherit_tech and material and (obj.override_material or material.texture_slots):
125 out_file.begin("technique")
126 out_file.begin("inherit", '"{}"'.format(obj.technique))
127 for slot in material.texture_slots:
128 if slot and slot.texture.type=="IMAGE":
129 name = image_name(slot.texture.image)
130 if slot.use_map_color_diffuse:
131 out_file.write("texture", '"diffuse_map"', '"{}"'.format(name))
132 elif slot.use_map_normal:
133 out_file.write("texture", '"normal_map"', '"{}"'.format(name))
134 if obj.override_material:
135 mat_name = material.name+".mat"
136 mat_out = open_output(os.path.join(path, mat_name))
137 self.export_material(material, mat_out)
138 out_file.write("material", '""', '"{}"'.format(mat_name))
142 out_file.write("technique", '"{}"'.format(obj.technique))
143 elif self.separate_tech:
144 if self.shared_tech and material:
145 name = material.name+".tech"
146 tech_out = open_output(os.path.join(path, name))
147 self.export_technique_definition(material, mesh, tech_out)
148 out_file.write("technique", '"{}"'.format(name))
150 out_file.begin("technique")
151 self.export_technique_definition(material, mesh, out_file)
154 def export_technique_definition(self, material, mesh, out_file):
155 out_file.begin("pass", '""')
159 if self.material_tex:
160 out_file.begin("material")
161 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
164 for u in mesh.uv_layers:
165 if u.name=="material_tex":
167 out_file.begin("texunit", index)
168 out_file.begin("texture2d")
169 out_file.write("min_filter", "NEAREST")
170 out_file.write("mag_filter", "NEAREST")
171 out_file.write("storage", "RGB", len(mesh.materials), 1)
173 for m in mesh.materials:
174 color = [int(cm(c)*255) for c in m.diffuse_color*mat.diffuse_intensity]
175 texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
177 out_file.write("raw_data", texdata)
181 out_file.begin("material")
182 self.export_material(material, out_file)
185 if self.textures!="NONE":
186 for slot in material.texture_slots:
191 if tex.type!="IMAGE":
195 for u in mesh.uv_layers:
196 if u.name==slot.uv_layer:
199 index = mesh.uv_layers[0].unit
201 out_file.begin("texunit", index)
202 if self.textures=="INLINE":
203 out_file.begin("texture2d")
204 out_file.write("min_filter", "LINEAR")
205 out_file.write("storage", "RGBA", tex.image.size[0], tex.image.size[1])
207 for p in tex.image.pixels:
208 texdata += "\\x%02X"%int(p*255)
210 out_file.write("raw_data", texdata)
213 out_file.write("texture", '"%s"'%image_name(tex.image))
218 def export_material(self, mat, out_file):
220 if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
221 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
222 amb = cm(mat.ambient)
223 out_file.write("ambient", amb, amb, amb, 1.0)
225 diff = mat.diffuse_color*mat.diffuse_intensity
226 out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
227 amb = diff*mat.ambient
228 out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
229 spec = mat.specular_color*mat.specular_intensity
230 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
231 out_file.write("shininess", mat.specular_hardness);