]> git.tdb.fi Git - libs/gl.git/commitdiff
Use filtering options from Blender texture
authorMikko Rasa <tdb@tdb.fi>
Thu, 16 May 2019 21:46:21 +0000 (00:46 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 17 May 2019 22:10:28 +0000 (01:10 +0300)
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
blender/io_mspgl/export_texture.py
blender/io_mspgl/properties.py
blender/io_mspgl/util.py

index 86fce780c00b22e151aaa04ee79f0941d2ddf3a8..3f0117d40a210909ce5075ec9b952e68619d661a 100644 (file)
@@ -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)
index bb3eb28d243ae79a86503dc2bc1e9258adb3d184..fea02a7d7e3d885cc606be138bdb01dda93f58f8 100644 (file)
@@ -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
index 3d16ff36214f2456104d7dc7a4218dcdc87d787b..91f3c56bb2f6725116e54f220037f2916cf9633b 100644 (file)
@@ -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")
index 3b5e8cd56f3349d6ab1f094b8a25cde41015f558..7e932df382791007e3151beff6d4cc41bf5301ad 100644 (file)
@@ -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