7 return 1.055*(l**(1/2.4))-0.055
12 return os.path.split(fp)[1]
16 def external_name(out_file, ext, index):
17 path, base = os.path.split(out_file.filename)
18 base = os.path.splitext(base)[0]
20 base += "_lod{}".format(index)
26 self.material_tex = False
27 self.srgb_colors = True
29 self.separate_mesh = False
30 self.separate_tech = False
31 self.external_tech = True
32 self.shared_tech = True
33 self.export_lods = True
35 def export(self, context, out_file, objs=None, progress=None):
37 obj = context.active_object
42 for c in obj.children:
44 if c.lod_index>=len(lods):
45 lods += [None]*(c.lod_index+1-len(lods))
48 from .outfile import open_output
49 out_file = open_output(out_file)
52 prev_tech = (None, None)
53 for i, l in enumerate(lods):
55 out_file.begin("level_of_detail", i)
58 same_mesh = (l.data.name==prev_mesh)
59 if i==0 or not same_mesh:
60 mesh = self.export_object_mesh(context, out_file, i, objs, progress)
61 prev_mesh = l.data.name
65 if l.material_slots and l.material_slots[0].material:
66 mat = l.material_slots[0].material.name
69 if self.external_tech and l.technique!=prev_tech[0]:
71 if i==0 or not same_tech:
72 self.export_object_technique(l, mesh, out_file, i)
73 prev_tech = (l.technique, mat)
78 def export_object_mesh(self, context, out_file, lod_index, objs, progress):
79 from .export_mesh import MeshExporter
80 mesh_export = MeshExporter()
81 for k, v in self.__dict__.items():
82 setattr(mesh_export, k, v)
84 if self.separate_mesh:
85 from .outfile import open_output
86 path, name = external_name(out_file, ".mesh", lod_index)
87 mesh_out = open_output(os.path.join(path, name))
88 mesh = mesh_export.export(context, mesh_out, objs, progress)
89 out_file.write("mesh", '"{}"'.format(name))
91 out_file.begin("mesh")
92 mesh = mesh_export.export(context, out_file, objs, progress)
97 def export_object_technique(self, obj, mesh, out_file, lod_index):
99 self.colormap = linear_to_srgb
101 self.colormap = lambda x: x
104 if obj.material_slots:
105 material = obj.material_slots[0].material
107 from .outfile import open_output
108 path, name = external_name(out_file, ".tech", lod_index)
110 if self.external_tech and obj.technique:
111 if obj.inherit_tech and material and (obj.override_material or material.texture_slots):
112 out_file.begin("technique")
113 out_file.begin("inherit", '"{}"'.format(obj.technique))
114 for slot in material.texture_slots:
115 if slot and slot.texture.type=="IMAGE":
116 name = image_name(slot.texture.image)
117 if slot.use_map_color_diffuse:
118 out_file.write("texture", '"diffuse_map"', '"{}"'.format(name))
119 elif slot.use_map_normal:
120 out_file.write("texture", '"normal_map"', '"{}"'.format(name))
121 if obj.override_material:
122 mat_name = material.name+".mat"
123 mat_out = open_output(os.path.join(path, mat_name))
124 self.export_material(material, mat_out)
125 out_file.write("material", '""', '"{}"'.format(mat_name))
129 out_file.write("technique", '"{}"'.format(obj.technique))
130 elif self.separate_tech:
131 if self.shared_tech and material:
132 name = material.name+".tech"
133 tech_out = open_output(os.path.join(path, name))
134 self.export_technique_definition(material, mesh, tech_out)
135 out_file.write("technique", '"{}"'.format(name))
137 out_file.begin("technique")
138 self.export_technique_definition(material, mesh, out_file)
141 def export_technique_definition(self, material, mesh, out_file):
142 out_file.begin("pass", '""')
146 if self.material_tex:
147 out_file.begin("material")
148 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
151 for u in mesh.uv_layers:
152 if u.name=="material_tex":
154 out_file.begin("texunit", index)
155 out_file.begin("texture2d")
156 out_file.write("min_filter", "NEAREST")
157 out_file.write("mag_filter", "NEAREST")
158 out_file.write("storage", "RGB", len(mesh.materials), 1)
160 for m in mesh.materials:
161 color = [int(cm(c)*255) for c in m.diffuse_color*mat.diffuse_intensity]
162 texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
164 out_file.write("raw_data", texdata)
168 out_file.begin("material")
169 self.export_material(material, out_file)
172 if self.textures!="NONE":
173 for slot in material.texture_slots:
178 if tex.type!="IMAGE":
182 for u in mesh.uv_layers:
183 if u.name==slot.uv_layer:
186 index = mesh.uv_layers[0].unit
188 out_file.begin("texunit", index)
189 if self.textures=="INLINE":
190 out_file.begin("texture2d")
191 out_file.write("min_filter", "LINEAR")
192 out_file.write("storage", "RGBA", tex.image.size[0], tex.image.size[1])
194 for p in tex.image.pixels:
195 texdata += "\\x%02X"%int(p*255)
197 out_file.write("raw_data", texdata)
200 out_file.write("texture", '"%s"'%image_name(tex.image))
205 def export_material(self, mat, out_file):
207 if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
208 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
209 amb = cm(mat.ambient)
210 out_file.write("ambient", amb, amb, amb, 1.0)
212 diff = mat.diffuse_color*mat.diffuse_intensity
213 out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
214 amb = diff*mat.ambient
215 out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
216 spec = mat.specular_color*mat.specular_intensity
217 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
218 out_file.write("shininess", mat.specular_hardness);