7 return 1.055*(l**(1/2.4))-0.055
9 def get_colormap(srgb):
16 class MaterialExporter:
18 self.single_file = True
19 self.use_textures = True
21 def create_texture_exporter(self):
22 from .export_texture import TextureExporter
23 texture_export = TextureExporter()
24 texture_export.inline_data = self.single_file
27 def export_technique_resources(self, material, resources):
28 texture_export = self.create_texture_exporter()
30 mat_name = material.name+".mat"
31 if mat_name not in resources:
32 resources[mat_name] = self.export_material(material)
35 for s in material.texture_slots:
36 if s and s.texture.type=='IMAGE' and s.texture.image:
37 tex_name = s.texture.name+".tex2d"
38 if tex_name not in resources:
39 resources[tex_name] = texture_export.export_texture(s.texture)
41 def export_technique(self, material, *, resources):
42 from .datafile import Resource, Statement
43 tech_res = Resource(material.name+".tech")
45 mat_res = resources[material.name+".mat"]
48 image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE' and s.texture.image]
49 for s in image_texture_slots:
50 if s.use_map_color_diffuse:
51 textures["diffuse_map"] = s.texture
52 elif s.use_map_normal:
53 textures["normal_map"] = s.texture
55 if material.technique:
56 if not obj.inherit_tech:
60 raise Exception("Can't export inherited technique to a single file")
62 st = Statement("inherit", material.technique)
63 for s, t in textures.items():
65 st.sub.append(Statement("texture", s, image_name(t.image)))
67 st.sub.append(tech_res.create_reference_statement("texture", s, resources[t.name+".tex2d"]))
68 if material.override_material:
69 st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
70 tech_res.statements.append(st)
72 st = Statement("pass", "")
74 st.sub.append(tech_res.create_embed_statement("material", mat_res))
76 st.sub.append(tech_res.create_reference_statement("material", mat_res))
78 if "diffuse_map" in textures:
79 diffuse_tex = textures["diffuse_map"]
80 tex_res = resources[diffuse_tex.name+".tex2d"]
81 ss = Statement("texunit", 0)
83 ss.sub.append(tech_res.create_embed_statement("texture2d", tex_res))
84 elif not diffuse_tex.default_filter:
85 ss.sub.append(tech_res.create_reference_statement("texture", tex_res))
87 ss.sub.append(Statement("texture", image_name(diffuse_tex.image)))
90 tech_res.statements.append(st)
94 def export_material(self, material):
95 from .datafile import Resource, Statement
96 mat_res = Resource(material.name+".mat")
97 statements = mat_res.statements
99 cm = get_colormap(material.srgb_colors)
100 if any(s.use_map_color_diffuse for s in material.texture_slots if s):
101 statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
102 amb = cm(material.ambient)
103 statements.append(Statement("ambient", amb, amb, amb, 1.0))
105 diff = material.diffuse_color*material.diffuse_intensity
106 statements.append(Statement("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0))
107 amb = diff*material.ambient
108 statements.append(Statement("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0))
109 spec = material.specular_color*material.specular_intensity
110 statements.append(Statement("specular", cm(spec.r), cm(spec.g), cm(spec.g), 1.0))
111 statements.append(Statement("shininess", material.specular_hardness))