]> git.tdb.fi Git - libs/gl.git/commitdiff
Export texture sampling settings as sampler objects
authorMikko Rasa <tdb@tdb.fi>
Sat, 13 Feb 2021 15:34:12 +0000 (17:34 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 13 Feb 2021 18:54:23 +0000 (20:54 +0200)
blender/io_mspgl/export_material.py
blender/io_mspgl/export_texture.py
blender/io_mspgl/material.py

index 0a75c47a761e929bbd5bf1c4485406357b99eb61..5710e0cec1b998697a92ecf1be01ed37260d1532 100644 (file)
@@ -28,7 +28,9 @@ class MaterialExporter:
                return texture_export
 
        def export_technique_resources(self, material, resources):
+               from .export_texture import SamplerExporter
                texture_export = self.create_texture_exporter()
+               sampler_export = SamplerExporter()
 
                from .material import Material
                material = Material(material)
@@ -40,6 +42,10 @@ class MaterialExporter:
                                        if tex_name not in resources:
                                                resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage)
 
+                                       samp_name = sampler_export.get_sampler_name(p.texture)
+                                       if samp_name not in resources:
+                                               resources[samp_name] = sampler_export.export_sampler(p.texture)
+
                mat_name = material.name+".mat"
                if mat_name not in resources:
                        resources[mat_name] = self.export_material(material, resources=resources)
@@ -57,6 +63,12 @@ class MaterialExporter:
                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))
+               if self.use_textures:
+                       first_tex = (p.texture for p in material.properties if p.texture).__next__
+                       if first_tex and not first_tex.default_filter:
+                               from .export_texture import SamplerExporter
+                               sampler_export = SamplerExporter()
+                               st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(first_tex)]))
                mat_res.statements.append(st)
 
                return mat_res
@@ -83,21 +95,30 @@ class MaterialMapExporter:
        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 = resources.get(base_color_name)
+               if not base_color_res:
                        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
 
+               sampler_name = "nearest.samp"
+               sampler_res = resources.get(sampler_name)
+               if not sampler_res:
+                       sampler_res = Resource(sampler_name, "sampler")
+
+                       sampler_res.statements.append(Statement("filter", Token('NEAREST')))
+
+                       resources[sampler_name] = sampler_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))
+                       st.sub.append(mat_res.create_reference_statement("sampler", sampler_res))
                        mat_res.statements.append(st)
 
                        resources[mat_name] = mat_res
index 3e78a4cd6ee33af497f4cde7d6a27f281490982c..2a736b84032e0df7dc76dd008718c9a24233316c 100644 (file)
@@ -9,20 +9,8 @@ class TextureExporter:
                from .datafile import Resource, Statement, Token
                tex_res = Resource(image.name+".tex2d", "texture2d")
 
-               use_interpolation = tex_node.interpolation!='Closest'
-               if use_interpolation:
-                       if tex_node.use_mipmap:
-                               tex_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
-                               tex_res.statements.append(Statement("generate_mipmap", True))
-                       else:
-                               tex_res.statements.append(Statement("filter", Token('LINEAR')))
-                       tex_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
-               else:
-                       if tex_node.use_mipmap:
-                               tex_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
-                               tex_res.statements.append(Statement("generate_mipmap", True))
-                       else:
-                               tex_res.statements.append(Statement("filter", Token('NEAREST')))
+               if tex_node.use_mipmap:
+                       tex_res.statements.append(Statement("generate_mipmap", True))
 
                colorspace = image.colorspace_settings.name
                if usage=='GRAY' and colorspace=='sRGB':
@@ -56,3 +44,35 @@ class TextureExporter:
                        tex_res.statements.append(Statement("raw_data", texdata))
 
                return tex_res
+
+class SamplerExporter:
+       def export_sampler(self, tex_node):
+               from .datafile import Resource, Statement, Token
+               samp_res = Resource(self.get_sampler_name(tex_node), "sampler")
+
+               use_interpolation = tex_node.interpolation!='Closest'
+               if use_interpolation:
+                       if tex_node.use_mipmap:
+                               samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
+                       else:
+                               samp_res.statements.append(Statement("filter", Token('LINEAR')))
+                       samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
+               else:
+                       if tex_node.use_mipmap:
+                               samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
+                       else:
+                               samp_res.statements.append(Statement("filter", Token('NEAREST')))
+
+               return samp_res
+
+       def get_sampler_name(self, tex_node):
+               name_parts = []
+
+               use_interpolation = tex_node.interpolation!='Closest'
+               name_parts.append("linear" if use_interpolation else "nearest")
+               if tex_node.use_mipmap:
+                       name_parts.append("mip")
+               if use_interpolation and tex_node.max_anisotropy>1:
+                       name_parts.append("aniso{:g}x".format(tex_node.max_anisotropy))
+
+               return "_".join(name_parts)+".samp"
index b275caa37f54a92a2a459df8ca9d66d7d12d90d0..ce31317418042f1d92d2d69e7f12432d47740cf2 100644 (file)
@@ -87,6 +87,15 @@ class Material:
 
                self.properties = (self.base_color, self.metalness, self.roughness, self.normal, self.emission)
 
+               sampler_settings = None
+               for p in self.properties:
+                       if p.texture:
+                               settings = (p.texture.default_filter, p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
+                               if sampler_settings is None:
+                                       sampler_settings = settings
+                               elif settings!=sampler_settings:
+                                       raise Exception("Conflicting sampler settings in material textures")
+
 
 class MaterialMap:
        def __init__(self, materials):