7 return 1.055*(l**(1/2.4))-0.055
9 def get_colormap(srgb):
18 return os.path.split(fp)[1]
23 class MaterialExporter:
27 def export_technique_resources(self, material, resources):
28 from .export_texture import TextureExporter
29 texture_export = TextureExporter()
31 mat_name = material.name+".mat"
32 if mat_name not in resources:
33 resources[mat_name] = self.export_material(material)
35 if self.textures=='INLINE':
36 for s in material.texture_slots:
37 if s and s.texture.type=='IMAGE' and s.texture.image:
38 tex_name = s.texture.name+".tex2d"
39 if tex_name not in resources:
40 resources[tex_name] = texture_export.export_texture(s.texture)
42 def export_technique(self, material, *, resources):
43 from .datafile import Resource, Statement
44 tech_res = Resource(material.name+".tech")
46 mat_res = resources[material.name+".mat"]
48 if self.textures!='NONE':
49 image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE' and s.texture.image]
50 for s in image_texture_slots:
51 if s.use_map_color_diffuse:
52 textures["diffuse_map"] = s.texture
53 elif s.use_map_normal:
54 textures["normal_map"] = s.texture
56 if material.technique:
57 if not obj.inherit_tech:
60 st = Statement("inherit", material.technique)
61 for s, t in textures.items():
62 st.sub.append(Statement("texture", s, image_name(t.image)))
63 if material.override_material:
64 st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
65 tech_res.statements.append(st)
67 st = Statement("pass", "")
68 st.sub.append(tech_res.create_embed_statement("material", mat_res))
70 if "diffuse_map" in textures:
71 diffuse_tex = textures["diffuse_map"]
72 ss = Statement("texunit", 0)
73 if self.textures=='INLINE':
74 ss.sub.append(tech_res.create_embed_statement("texture2d", resources[diffuse_tex.name+".tex2d"]))
76 ss.sub.append(Statement("texture", image_name(diffuse_tex.image)))
79 tech_res.statements.append(st)
83 def export_material(self, material):
84 from .datafile import Resource, Statement
85 mat_res = Resource(material.name+".mat")
86 statements = mat_res.statements
88 cm = get_colormap(material.srgb_colors)
89 if any(s.use_map_color_diffuse for s in material.texture_slots if s):
90 statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
91 amb = cm(material.ambient)
92 statements.append(Statement("ambient", amb, amb, amb, 1.0))
94 diff = material.diffuse_color*material.diffuse_intensity
95 statements.append(Statement("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0))
96 amb = diff*material.ambient
97 statements.append(Statement("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0))
98 spec = material.specular_color*material.specular_intensity
99 statements.append(Statement("specular", cm(spec.r), cm(spec.g), cm(spec.g), 1.0))
100 statements.append(Statement("shininess", material.specular_hardness))