]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_material.py
Refactor writing files in the Blender exporter
[libs/gl.git] / blender / io_mspgl / export_material.py
index 3e5210c1ba79df2043e967096d5bc3426042db4a..8f9bdd40ba31a72c7c7c8d61add3a8a11c67f02d 100644 (file)
 import os
 
-def linear_to_srgb(l):
-       if l<0.0031308:
-               return 12.92*l
-       else:
-               return 1.055*(l**(1/2.4))-0.055
+def create_technique_resource(material, resources, single_file):
+       from .datafile import Resource, Statement
+       tech_res = Resource(material.name+".tech", "technique")
 
-def get_colormap(srgb):
-       if srgb:
-               return linear_to_srgb
-       else:
-               return lambda x: x
+       mat_res = resources[material.name+".mat"]
 
-def image_name(i):
-       fp = i.filepath
-       if fp:
-               return os.path.split(fp)[1]
+       st = Statement("pass", "")
+       if single_file:
+               st.sub.append(tech_res.create_embed_statement("material", mat_res))
        else:
-               return i.name
+               st.sub.append(tech_res.create_reference_statement("material", mat_res))
+
+       if material.render_mode=='CUSTOM':
+               st.sub.append(Statement("shader", material.shader))
+
+       tech_res.statements.append(st)
 
+       return tech_res
 
 class MaterialExporter:
        def __init__(self):
-               self.textures = 'REF'
+               self.single_file = True
+               self.use_textures = True
 
-       def export_technique_resources(self, material, resources):
+       def create_texture_exporter(self):
                from .export_texture import TextureExporter
                texture_export = TextureExporter()
+               texture_export.inline_data = self.single_file
+               return texture_export
 
-               mat_name = material.name+".mat"
-               if mat_name not in resources:
-                       resources[mat_name] = self.export_material(material)
+       def export_technique_resources(self, material, resources):
+               texture_export = self.create_texture_exporter()
 
-               if self.textures=='INLINE':
-                       for s in material.texture_slots:
-                               if s and s.texture.type=='IMAGE':
-                                       tex_name = s.texture.name+".tex2d"
+               from .material import Material
+               material = Material(material)
+
+               if self.use_textures:
+                       for p in material.properties:
+                               if p.texture:
+                                       tex_name = p.texture.image.name+".tex2d"
                                        if tex_name not in resources:
-                                               resources[tex_name] = texture_export.export_texture(s.texture)
+                                               resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage)
+
+               mat_name = material.name+".mat"
+               if mat_name not in resources:
+                       resources[mat_name] = self.export_material(material, resources=resources)
 
        def export_technique(self, material, *, resources):
+               return create_technique_resource(material, resources, self.single_file)
+
+       def export_material(self, material, *, resources):
                from .datafile import Resource, Statement
-               tech_res = Resource(material.name+".tech")
-
-               mat_res = resources[material.name+".mat"]
-               image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE']
-
-               if material.technique:
-                       if not obj.inherit_tech:
-                               return []
-
-                       st = Statement("inherit", material.technique)
-                       for slot in image_texture_slots:
-                               name = image_name(slot.texture.image)
-                               if slot.use_map_color_diffuse:
-                                       st.sub.append(Statement("texture", "diffuse_map", name))
-                               elif slot.use_map_normal:
-                                       st.sub.append(Statement("texture", "normal_map", name))
-                       if material.override_material:
-                               st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
-                       tech_res.statements.append(st)
-               else:
-                       st = Statement("pass", "")
-                       st.sub.append(tech_res.create_embed_statement("material", mat_res))
-
-                       if self.textures!='NONE':
-                               diffuse_tex = None
-                               for slot in image_texture_slots:
-                                       if slot.use_map_color_diffuse:
-                                               diffuse_tex = slot.texture
-                                               break
-
-                               if diffuse_tex:
-                                       ss = Statement("texunit", 0)
-                                       if self.textures=='INLINE':
-                                               tex_res = resources[slot.texture.name+".tex2d"]
-                                               ss.sub.append(tech_res.create_embed_statement("texture2d", tex_res))
-                                       elif tex.image:
-                                               ss.sub.append(Statement("texture", image_name(tex.image)))
-                                       st.sub.append(ss)
-
-                       tech_res.statements.append(st)
-
-               return tech_res
-
-       def export_material(self, material):
-               from .datafile import Resource, Statement
-               mat_res = Resource(material.name+".mat")
-               statements = mat_res.statements
-
-               cm = get_colormap(material.srgb_colors)
-               if any(s.use_map_color_diffuse for s in material.texture_slots if s):
-                       statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
-                       amb = cm(material.ambient)
-                       statements.append(Statement("ambient", amb, amb, amb, 1.0))
-               else:
-                       diff = material.diffuse_color*material.diffuse_intensity
-                       statements.append(Statement("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0))
-                       amb = diff*material.ambient
-                       statements.append(Statement("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0))
-               spec = material.specular_color*material.specular_intensity
-               statements.append(Statement("specular", cm(spec.r), cm(spec.g), cm(spec.g), 1.0))
-               statements.append(Statement("shininess", material.specular_hardness))
+               mat_res = Resource(material.name+".mat", "material")
+
+               st = Statement("pbr")
+               st.sub.append(self.create_property_statement(mat_res, material.base_color, "base_color", resources))
+               st.sub.append(self.create_property_statement(mat_res, material.metalness, "metalness", resources))
+               st.sub.append(self.create_property_statement(mat_res, material.roughness, "roughness", resources))
+               st.sub.append(self.create_property_statement(mat_res, material.normal, "normal", resources, tex_only=True))
+               st.sub.append(self.create_property_statement(mat_res, material.emission, "emission", resources))
+               mat_res.statements.append(st)
 
                return mat_res
+
+       def create_property_statement(self, mat_res, prop, keyword, resources, *, tex_only=False):
+               from .datafile import Statement
+               if self.use_textures and prop.texture:
+                       tex_res = resources[prop.texture.image.name+".tex2d"]
+                       fn = os.path.basename(prop.texture.image.filepath)
+                       if self.single_file:
+                               raise Exception("Can't export textures to a single file")
+                       elif prop.texture.default_filter and fn:
+                               return Statement(keyword+"_map", fn)
+                       else:
+                               return mat_res.create_reference_statement(keyword+"_map", tex_res)
+               elif type(prop.value)==tuple:
+                       return Statement(keyword, *prop.value)
+               else:
+                       return Statement(keyword, prop.value)
+
+
+class MaterialMapExporter:
+       def __init__(self):
+               self.single_file = True
+
+       def export_technique_resources(self, material_map, resources):
+               from .datafile import Resource, Statement, Token
+               base_color_name = material_map.name+"_base_color.tex2d"
+               if base_color_name not in resources:
+                       base_color_res = Resource(base_color_name, "texture2d")
+
+                       base_color_res.statements.append(Statement("min_filter", Token('NEAREST')))
+                       base_color_res.statements.append(Statement("mag_filter", Token('NEAREST')))
+                       base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_map.size))
+                       base_color_res.statements.append(Statement("raw_data", material_map.base_color_data))
+
+                       resources[base_color_name] = base_color_res
+
+               mat_name = material_map.name+".mat"
+               if mat_name not in resources:
+                       mat_res = Resource(mat_name, "material")
+                       st = Statement("pbr")
+                       st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
+                       mat_res.statements.append(st)
+
+                       resources[mat_name] = mat_res
+
+       def export_technique(self, material_map, *, resources):
+               return create_technique_resource(material_map, resources, self.single_file)