]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_texture.py
Make texture channel handling in the Blender exporter more flexible
[libs/gl.git] / blender / io_mspgl / export_texture.py
index 3f11b06be7e5cb0cbbef65a86e8c51baffb7589d..eb6fa223cf1f8d0f31d903daa5d9ac9229effd7f 100644 (file)
 import os
+import base64
+import codecs
 
-class TextureExporter:
-       def __init__(self):
-               self.inline_data = True
+def pixels_to_rgba(pixels):
+       return (int(p*255) for p in pixels)
+
+def pixels_to_rgb(pixels):
+       for i in range(0, len(pixels), 4):
+               yield int(pixels[i]*255)
+               yield int(pixels[i+1]*255)
+               yield int(pixels[i+2]*255)
+
+def pixels_to_rgb_invert(pixels, mask):
+       for i in range(0, len(pixels), 4):
+               r = int(pixels[i]*255)
+               yield 255-r if mask&1 else r
+               g = int(pixels[i+1]*255)
+               yield 255-g if mask&2 else g
+               b = int(pixels[i+2]*255)
+               yield 255-b if mask&4 else b
+
+def pixels_to_gray(pixels):
+       for i in range(0, len(pixels), 4):
+               yield int((pixels[i]+pixels[i+1]+pixels[i+2])*255/3)
 
-       def export_texture(self, tex_node, usage='RGB'):
+class TextureExporter:
+       def export_texture(self, tex_node, channels=['R', 'G', 'B']):
                image = tex_node.image
+               from .datafile import RawData, Resource, Statement, Token
+               tex_res = Resource(image.name+".tex", "texture")
+
+               tex_res.statements.append(Statement("type", Token("\\2d")))
+
+               if tex_node.use_mipmap:
+                       tex_res.statements.append(Statement("generate_mipmap", True))
+
+               colorspace = image.colorspace_settings.name
+               if len(channels)==1 and colorspace=='sRGB':
+                       raise Exception("Unsupported configuration on texture {}: Grayscale with sRGB".format(image.name))
+
+               invert_mask = sum(1<<i for i, c in enumerate(channels) if c[0]=='~')
+
+               from .util import basename
+               fn = basename(image.filepath)
+               if not invert_mask and fn:
+                       if not tex_node.use_mipmap:
+                               tex_res.statements.append(Statement("mipmap_levels", 1))
+                       srgb = "_srgb" if colorspace=='sRGB' else ""
+                       tex_res.statements.append(Statement("external_image"+srgb, fn))
+               else:
+                       if len(channels)==4:
+                               fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
+                       elif len(channels)==1:
+                               fmt = 'LUMINANCE8'
+                       else:
+                               fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
+
+                       tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
+
+                       pixels = tuple(image.pixels)
+                       texdata = ""
+                       if len(channels)==4:
+                               texdata = pixels_to_rgba(pixels)
+                       elif len(channels)==1:
+                               texdata = pixels_to_gray(pixels)
+                       elif invert_mask:
+                               texdata = pixels_to_rgb_invert(pixels, invert_mask)
+                       else:
+                               texdata = pixels_to_rgb(pixels)
+
+                       data = RawData(image.name+".mdr", bytes(texdata))
+                       tex_res.statements.append(tex_res.create_reference_statement("external_data", data))
+
+               return tex_res
+
+class SamplerExporter:
+       def export_sampler(self, tex_node):
                from .datafile import Resource, Statement, Token
-               tex_res = Resource(image.name+".tex2d", "texture2d")
+               samp_res = Resource(self.get_sampler_name(tex_node), "sampler")
 
                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))
+                               samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
                        else:
-                               tex_res.statements.append(Statement("filter", Token('LINEAR')))
-                       tex_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
+                               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:
-                               tex_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
-                               tex_res.statements.append(Statement("generate_mipmap", True))
+                               samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
                        else:
-                               tex_res.statements.append(Statement("filter", Token('NEAREST')))
+                               samp_res.statements.append(Statement("filter", Token('NEAREST')))
 
-               colorspace = image.colorspace_settings.name
-               if usage=='RGBA':
-                       fmt = 'SRGB_ALPHA' if colorspace=='sRGB' else 'RGBA'
-               elif usage=='GRAY':
-                       if colorspace=='sRGB':
-                               raise Exception("Grayscale textures with sRGB colorspace are not supported")
-                       fmt = 'LUMINANCE'
-               else:
-                       fmt = 'SRGB' if colorspace=='sRGB' else 'RGB'
+               if tex_node.extension=="REPEAT":
+                       samp_res.statements.append(Statement("wrap", Token('REPEAT')))
+               elif tex_node.extension=="EXTEND":
+                       samp_res.statements.append(Statement("wrap", Token('CLAMP_TO_EDGE')))
+               elif tex_node.extension=="CLIP":
+                       samp_res.statements.append(Statement("wrap", Token('CLAMP_TO_BORDER')))
+                       samp_res.statements.append(Statement("border_color", 0.0, 0.0, 0.0, 0.0))
 
-               tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
+               return samp_res
 
-               fn = os.path.basename(image.filepath)
-               if not self.inline_data and fn:
-                       tex_res.statements.append(Statement("external_image", fn))
-               else:
-                       texdata = ""
-                       if usage=='RGBA':
-                               for p in image.pixels:
-                                       texdata += "\\x{:02X}".format(int(p*255))
-                       elif usage=='GRAY':
-                               for i in range(0, len(image.pixels), 4):
-                                       texdata += "\\x{:02X}".format(image.pixels[i])
-                       else:
-                               for i in range(0, len(image.pixels), 4):
-                                       for j in range(3):
-                                               texdata += "\\x{:02X}".format(int(image.pixels[i+j]*255))
-                       tex_res.statements.append(Statement("raw_data", texdata))
+       def get_sampler_name(self, tex_node):
+               name_parts = []
 
-               return tex_res
+               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))
+               if tex_node.extension!="REPEAT":
+                       name_parts.append("clip" if tex_node.extension=="CLIP" else "clamp")
+
+               return "_".join(name_parts)+".samp"