]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_texture.py
Include only tangent in mesh data and calculate binormal on the fly
[libs/gl.git] / blender / io_mspgl / export_texture.py
1 import os
2 import base64
3 import codecs
4
5 def encode_pixels(pixels):
6         from .datafile import Token
7         return Token(codecs.decode(b"="+base64.b64encode(bytes(pixels))+b"=", "ascii"))
8
9 def pixels_to_rgba(pixels):
10         return (int(p*255) for p in pixels)
11
12 def pixels_to_rgb(pixels):
13         for i in range(0, len(pixels), 4):
14                 yield int(pixels[i]*255)
15                 yield int(pixels[i+1]*255)
16                 yield int(pixels[i+2]*255)
17
18 def pixels_to_rgb_invert_green(pixels):
19         for i in range(0, len(pixels), 4):
20                 yield int(pixels[i]*255)
21                 yield 255-int(pixels[i+1]*255)
22                 yield int(pixels[i+2]*255)
23
24 def pixels_to_gray(pixels):
25         for i in range(0, len(pixels), 4):
26                 yield int((pixels[i]+pixels[i+1]+pixels[i+2])*255/3)
27
28 class TextureExporter:
29         def __init__(self):
30                 self.inline_data = True
31
32         def export_texture(self, tex_node, usage='RGB', *, invert_green=False):
33                 image = tex_node.image
34                 from .datafile import Resource, Statement, Token
35                 tex_res = Resource(image.name+".tex2d", "texture2d")
36
37                 if tex_node.use_mipmap:
38                         tex_res.statements.append(Statement("generate_mipmap", True))
39
40                 colorspace = image.colorspace_settings.name
41                 if usage=='GRAY' and colorspace=='sRGB':
42                                 raise Exception("Grayscale textures with sRGB colorspace are not supported")
43
44                 from .util import basename
45                 fn = basename(image.filepath)
46                 if not self.inline_data and not invert_green and fn:
47                         srgb = "_srgb" if colorspace=='sRGB' else ""
48                         tex_res.statements.append(Statement("external_image"+srgb, fn))
49                 else:
50                         if usage=='RGBA':
51                                 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
52                         elif usage=='GRAY':
53                                 fmt = 'LUMINANCE8'
54                         else:
55                                 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
56
57                         tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
58
59                         pixels = tuple(image.pixels)
60                         texdata = ""
61                         if usage=='RGBA':
62                                 texdata = encode_pixels(pixels_to_rgba(pixels))
63                         elif usage=='GRAY':
64                                 texdata = encode_pixels(pixels_to_gray(pixels))
65                         elif invert_green:
66                                 texdata = encode_pixels(pixels_to_rgb_invert_green(pixels))
67                         else:
68                                 texdata = encode_pixels(pixels_to_rgb(pixels))
69                         tex_res.statements.append(Statement("raw_data", texdata))
70
71                 return tex_res
72
73 class SamplerExporter:
74         def export_sampler(self, tex_node):
75                 from .datafile import Resource, Statement, Token
76                 samp_res = Resource(self.get_sampler_name(tex_node), "sampler")
77
78                 use_interpolation = tex_node.interpolation!='Closest'
79                 if use_interpolation:
80                         if tex_node.use_mipmap:
81                                 samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
82                         else:
83                                 samp_res.statements.append(Statement("filter", Token('LINEAR')))
84                         samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
85                 else:
86                         if tex_node.use_mipmap:
87                                 samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
88                         else:
89                                 samp_res.statements.append(Statement("filter", Token('NEAREST')))
90
91                 if tex_node.extension=="REPEAT":
92                         samp_res.statements.append(Statement("wrap", Token('REPEAT')))
93                 elif tex_node.extension=="EXTEND":
94                         samp_res.statements.append(Statement("wrap", Token('CLAMP_TO_EDGE')))
95                 elif tex_node.extension=="CLIP":
96                         samp_res.statements.append(Statement("wrap", Token('CLAMP_TO_BORDER')))
97                         samp_res.statements.append(Statement("border_color", 0.0, 0.0, 0.0, 0.0))
98
99                 return samp_res
100
101         def get_sampler_name(self, tex_node):
102                 name_parts = []
103
104                 use_interpolation = tex_node.interpolation!='Closest'
105                 name_parts.append("linear" if use_interpolation else "nearest")
106                 if tex_node.use_mipmap:
107                         name_parts.append("mip")
108                 if use_interpolation and tex_node.max_anisotropy>1:
109                         name_parts.append("aniso{:g}x".format(tex_node.max_anisotropy))
110                 if tex_node.extension!="REPEAT":
111                         name_parts.append("clip" if tex_node.extension=="CLIP" else "clamp")
112
113                 return "_".join(name_parts)+".samp"