From d14b33347983d1d0cd608b29b5ee9b7a37d98c5b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 13 Feb 2021 17:34:12 +0200 Subject: [PATCH] Export texture sampling settings as sampler objects --- blender/io_mspgl/export_material.py | 27 ++++++++++++++-- blender/io_mspgl/export_texture.py | 48 ++++++++++++++++++++--------- blender/io_mspgl/material.py | 9 ++++++ 3 files changed, 67 insertions(+), 17 deletions(-) diff --git a/blender/io_mspgl/export_material.py b/blender/io_mspgl/export_material.py index 0a75c47a..5710e0ce 100644 --- a/blender/io_mspgl/export_material.py +++ b/blender/io_mspgl/export_material.py @@ -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 diff --git a/blender/io_mspgl/export_texture.py b/blender/io_mspgl/export_texture.py index 3e78a4cd..2a736b84 100644 --- a/blender/io_mspgl/export_texture.py +++ b/blender/io_mspgl/export_texture.py @@ -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" diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index b275caa3..ce313174 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -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): -- 2.45.2