3 class MaterialExporter:
5 self.single_file = True
6 self.use_textures = True
8 def create_texture_exporter(self):
9 from .export_texture import TextureExporter
10 texture_export = TextureExporter()
11 texture_export.inline_data = self.single_file
14 def export_technique_resources(self, material, resources):
15 texture_export = self.create_texture_exporter()
17 mat_name = material.name+".mat"
18 if mat_name not in resources:
19 resources[mat_name] = self.export_material(material)
22 for s in material.texture_slots:
23 if s and s.texture.type=='IMAGE' and s.texture.image:
24 tex_name = s.texture.name+".tex2d"
25 if tex_name not in resources:
26 resources[tex_name] = texture_export.export_texture(s.texture)
28 def export_technique(self, material, *, resources):
29 from .datafile import Resource, Statement
30 tech_res = Resource(material.name+".tech")
32 mat_res = resources[material.name+".mat"]
35 image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE' and s.texture.image]
36 for s in image_texture_slots:
37 if s.use_map_color_diffuse:
38 textures["diffuse_map"] = s.texture
39 elif s.use_map_normal:
40 textures["normal_map"] = s.texture
42 if material.technique:
43 if not material.inherit_tech:
47 raise Exception("Can't export inherited technique to a single file")
49 st = Statement("inherit", material.technique)
50 for s, t in textures.items():
52 st.sub.append(Statement("texture", s, image_name(t.image)))
54 st.sub.append(tech_res.create_reference_statement("texture", s, resources[t.name+".tex2d"]))
55 if material.override_material:
56 st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
57 tech_res.statements.append(st)
59 st = Statement("pass", "")
61 st.sub.append(tech_res.create_embed_statement("material", mat_res))
63 st.sub.append(tech_res.create_reference_statement("material", mat_res))
65 if "diffuse_map" in textures:
66 diffuse_tex = textures["diffuse_map"]
67 tex_res = resources[diffuse_tex.name+".tex2d"]
68 ss = Statement("texunit", 0)
70 ss.sub.append(tech_res.create_embed_statement("texture2d", tex_res))
71 elif not diffuse_tex.default_filter:
72 ss.sub.append(tech_res.create_reference_statement("texture", tex_res))
74 ss.sub.append(Statement("texture", image_name(diffuse_tex.image)))
77 tech_res.statements.append(st)
81 def export_material(self, material):
82 from .datafile import Resource, Statement
83 mat_res = Resource(material.name+".mat")
84 statements = mat_res.statements
86 from .util import get_colormap
87 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))
105 class MaterialMapExporter:
107 self.single_file = True
109 def export_technique_resources(self, material_map, resources):
110 from .datafile import Resource, Statement, Token
111 diffuse_name = material_map.name+"_diffuse.tex2d"
112 if diffuse_name not in resources:
113 diffuse_res = Resource(diffuse_name)
115 fmt = 'SRGB_ALPHA' if material_map.srgb_colors else 'RGBA'
117 diffuse_res.statements.append(Statement("min_filter", Token('NEAREST')))
118 diffuse_res.statements.append(Statement("mag_filter", Token('NEAREST')))
119 diffuse_res.statements.append(Statement("storage", Token(fmt), *material_map.size))
120 diffuse_res.statements.append(Statement("raw_data", material_map.diffuse_data))
122 resources[diffuse_name] = diffuse_res
124 if "basic_white.mat" not in resources:
125 mat_res = Resource("basic_white.mat")
126 mat_res.statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
128 resources["basic_white.mat"] = mat_res
130 def export_technique(self, material_map, *, resources=None):
131 from .datafile import Resource, Statement
132 tech_res = Resource(material_map.name+".tech")
134 mat_res = resources["basic_white.mat"]
135 diffuse_res = resources[material_map.name+"_diffuse.tex2d"]
137 if material_map.technique:
139 raise Exception("Can't export inherited technique to a single file")
141 st = Statement("inherit", material_map.technique)
142 st.sub.append(tech_res.create_reference_statement("texture", "diffuse_map", diffuse_res))
143 st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
144 tech_res.statements.append(st)
146 st = Statement("pass", "")
148 st.sub.append(tech_res.create_embed_statement("material", mat_res))
150 st.sub.append(tech_res.create_reference_statement("material", mat_res))
151 ss = Statement("texunit", 0)
153 ss.sub.append(tech_res.create_embed_statement("texture2d", diffuse_res))
155 ss.sub.append(tech_res.create_reference_statement("texture", diffuse_res))
157 tech_res.statements.append(st)