From e891021affc89a329d65b526744ebd38589cae32 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 17 May 2019 00:46:21 +0300 Subject: [PATCH] Use filtering options from Blender texture A new property has been added on textures to reference the original image only and use default options at load time. --- blender/io_mspgl/export_material.py | 27 ++++++++++++-------- blender/io_mspgl/export_texture.py | 39 ++++++++++++++++++++++++----- blender/io_mspgl/properties.py | 21 ++++++++++++++++ blender/io_mspgl/util.py | 10 ++++++++ 4 files changed, 80 insertions(+), 17 deletions(-) diff --git a/blender/io_mspgl/export_material.py b/blender/io_mspgl/export_material.py index 86fce780..3f0117d4 100644 --- a/blender/io_mspgl/export_material.py +++ b/blender/io_mspgl/export_material.py @@ -12,27 +12,26 @@ def get_colormap(srgb): else: return lambda x: x -def image_name(i): - fp = i.filepath - if fp: - return os.path.split(fp)[1] - else: - return i.name - class MaterialExporter: def __init__(self): self.textures = 'REF' - def export_technique_resources(self, material, resources): + def create_texture_exporter(self): from .export_texture import TextureExporter texture_export = TextureExporter() + if self.textures=='INLINE': + texture_export.pixels = 'INLINE' + return texture_export + + def export_technique_resources(self, material, resources): + texture_export = self.create_texture_exporter() mat_name = material.name+".mat" if mat_name not in resources: resources[mat_name] = self.export_material(material) - if self.textures=='INLINE': + if self.textures!='NONE': for s in material.texture_slots: if s and s.texture.type=='IMAGE' and s.texture.image: tex_name = s.texture.name+".tex2d" @@ -59,7 +58,10 @@ class MaterialExporter: st = Statement("inherit", material.technique) for s, t in textures.items(): - st.sub.append(Statement("texture", s, image_name(t.image))) + if t.default_filter: + st.sub.append(Statement("texture", s, image_name(t.image))) + else: + st.sub.append(tech_res.create_reference_statement("texture", s, resources[t.name+".tex2d"])) if material.override_material: st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res)) tech_res.statements.append(st) @@ -69,9 +71,12 @@ class MaterialExporter: if "diffuse_map" in textures: diffuse_tex = textures["diffuse_map"] + tex_res = resources[diffuse_tex.name+".tex2d"] ss = Statement("texunit", 0) if self.textures=='INLINE': - ss.sub.append(tech_res.create_embed_statement("texture2d", resources[diffuse_tex.name+".tex2d"])) + ss.sub.append(tech_res.create_embed_statement("texture2d", tex_res)) + elif not diffuse_tex.default_filter: + ss.sub.append(tech_res.create_reference_statement("texture2d", tex_res)) else: ss.sub.append(Statement("texture", image_name(diffuse_tex.image))) st.sub.append(ss) diff --git a/blender/io_mspgl/export_texture.py b/blender/io_mspgl/export_texture.py index bb3eb28d..fea02a7d 100644 --- a/blender/io_mspgl/export_texture.py +++ b/blender/io_mspgl/export_texture.py @@ -1,13 +1,40 @@ class TextureExporter: + def __init__(self): + self.pixels = 'REF' + def export_texture(self, texture): from .datafile import Resource, Statement, Token tex_res = Resource(texture.name+".tex2d") - tex_res.statements.append(Statement("min_filter", Token("LINEAR"))) - tex_res.statements.append(Statement("storage", Token("RGBA"), texture.image.size[0], texture.image.size[1])) - texdata = "" - for p in texture.image.pixels: - texdata += "\\x%02X"%int(p*255) - tex_res.statements.append(Statement("raw_data", texdata)) + if texture.use_interpolation: + if texture.use_mipmap: + tex_res.statements.append(Statement("min_filter", Token('LINEAR_MIPMAP_LINEAR'))) + tex_res.statements.append(Statement("generate_mipmap", True)) + else: + tex_res.statements.append(Statement("min_filter", Token('LINEAR'))) + tex_res.statements.append(Statement("max_anisotropy", texture.filter_eccentricity)) + else: + if texture.use_mipmap: + tex_res.statements.append(Statement("min_filter", Token('NEAREST_MIPMAP_NEAREST'))) + tex_res.statements.append(Statement("generate_mipmap", True)) + else: + tex_res.statements.append(Statement("min_filter", Token('NEAREST'))) + + if self.pixels=='REF': + from .util import image_name + tex_res.statements.append(Statement("image_data", image_name(texture.image))) + else: + texdata = "" + if texture.use_alpha: + fmt = 'RGBA' + for p in texture.image.pixels: + texdata += "\\x{:02X}".format(int(p*255)) + else: + fmt = 'RGB' + for i in range(0, len(texture.image.pixels), 4): + for j in range(3): + texdata += "\\x{:02X}".format(int(texture.image.pixels[i+j]*255)) + tex_res.statements.append(Statement("storage", Token(fmt), texture.image.size[0], texture.image.size[1])) + tex_res.statements.append(Statement("raw_data", texdata)) return tex_res diff --git a/blender/io_mspgl/properties.py b/blender/io_mspgl/properties.py index 3d16ff36..91f3c56b 100644 --- a/blender/io_mspgl/properties.py +++ b/blender/io_mspgl/properties.py @@ -73,6 +73,25 @@ class MspGLMaterialProperties(bpy.types.Panel): if mat.array_atlas: self.layout.prop(mat, "array_layer") +class MspGLTextureProperties(bpy.types.Panel): + bl_idname = "TEXTURE_PT_mspgl_properties" + bl_label = "MspGL properties" + bl_space_type = "PROPERTIES" + bl_region_type = "WINDOW" + bl_context = "texture" + + @classmethod + def poll(cls, context): + mat = context.active_object.active_material + return mat is not None and mat.active_texture is not None + + def draw(self, context): + tex = context.active_object.active_material.active_texture + if not tex: + return + + self.layout.prop(tex, "default_filter") + def register_properties(): bpy.types.Mesh.winding_test = bpy.props.BoolProperty(name="Winding test", description="Perform winding test to skip back faces") bpy.types.Mesh.smoothing = bpy.props.EnumProperty(name="Smoothing", description="Smoothing method to use", default="MSPGL", @@ -99,3 +118,5 @@ def register_properties(): bpy.types.Material.srgb_colors = bpy.props.BoolProperty(name="sRGB colors", description="Export material colors as sRGB instead of linear", default=True) bpy.types.Material.array_atlas = bpy.props.BoolProperty(name="Texture array atlas", description="The material is stored in a texture array") bpy.types.Material.array_layer = bpy.props.IntProperty("Texture array layer", description="Layer of the texture array atlas to use") + + bpy.types.Texture.default_filter = bpy.props.BoolProperty(name="Default filter", description="Let the loading program determine filtering options") diff --git a/blender/io_mspgl/util.py b/blender/io_mspgl/util.py index 3b5e8cd5..7e932df3 100644 --- a/blender/io_mspgl/util.py +++ b/blender/io_mspgl/util.py @@ -1,3 +1,5 @@ +import os + class Progress: def __init__(self, context): self.task = "" @@ -48,3 +50,11 @@ class Progress: if self.window_manager: self.window_manager.progress_update(value) self.last = value + + +def image_name(img): + fp = img.filepath + if fp: + return os.path.split(fp)[1] + else: + return img.name -- 2.45.2