]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_texture.py
Remake the desertpillars demo using modern techniques
[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 export_texture(self, tex_node, usage='RGB', *, invert_green=False):
30                 image = tex_node.image
31                 from .datafile import Resource, Statement, Token
32                 tex_res = Resource(image.name+".tex2d", "texture2d")
33
34                 if tex_node.use_mipmap:
35                         tex_res.statements.append(Statement("generate_mipmap", True))
36
37                 colorspace = image.colorspace_settings.name
38                 if usage=='GRAY' and colorspace=='sRGB':
39                         raise Exception("Unsupported configuration on texture {}: Grayscale with sRGB".format(image.name))
40
41                 from .util import basename
42                 fn = basename(image.filepath)
43                 if not invert_green and fn:
44                         srgb = "_srgb" if colorspace=='sRGB' else ""
45                         tex_res.statements.append(Statement("external_image"+srgb, fn))
46                 else:
47                         if usage=='RGBA':
48                                 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
49                         elif usage=='GRAY':
50                                 fmt = 'LUMINANCE8'
51                         else:
52                                 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
53
54                         tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
55
56                         pixels = tuple(image.pixels)
57                         texdata = ""
58                         if usage=='RGBA':
59                                 texdata = encode_pixels(pixels_to_rgba(pixels))
60                         elif usage=='GRAY':
61                                 texdata = encode_pixels(pixels_to_gray(pixels))
62                         elif invert_green:
63                                 texdata = encode_pixels(pixels_to_rgb_invert_green(pixels))
64                         else:
65                                 texdata = encode_pixels(pixels_to_rgb(pixels))
66                         tex_res.statements.append(Statement("raw_data", texdata))
67
68                 return tex_res
69
70 class SamplerExporter:
71         def export_sampler(self, tex_node):
72                 from .datafile import Resource, Statement, Token
73                 samp_res = Resource(self.get_sampler_name(tex_node), "sampler")
74
75                 use_interpolation = tex_node.interpolation!='Closest'
76                 if use_interpolation:
77                         if tex_node.use_mipmap:
78                                 samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
79                         else:
80                                 samp_res.statements.append(Statement("filter", Token('LINEAR')))
81                         samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
82                 else:
83                         if tex_node.use_mipmap:
84                                 samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
85                         else:
86                                 samp_res.statements.append(Statement("filter", Token('NEAREST')))
87
88                 if tex_node.extension=="REPEAT":
89                         samp_res.statements.append(Statement("wrap", Token('REPEAT')))
90                 elif tex_node.extension=="EXTEND":
91                         samp_res.statements.append(Statement("wrap", Token('CLAMP_TO_EDGE')))
92                 elif tex_node.extension=="CLIP":
93                         samp_res.statements.append(Statement("wrap", Token('CLAMP_TO_BORDER')))
94                         samp_res.statements.append(Statement("border_color", 0.0, 0.0, 0.0, 0.0))
95
96                 return samp_res
97
98         def get_sampler_name(self, tex_node):
99                 name_parts = []
100
101                 use_interpolation = tex_node.interpolation!='Closest'
102                 name_parts.append("linear" if use_interpolation else "nearest")
103                 if tex_node.use_mipmap:
104                         name_parts.append("mip")
105                 if use_interpolation and tex_node.max_anisotropy>1:
106                         name_parts.append("aniso{:g}x".format(tex_node.max_anisotropy))
107                 if tex_node.extension!="REPEAT":
108                         name_parts.append("clip" if tex_node.extension=="CLIP" else "clamp")
109
110                 return "_".join(name_parts)+".samp"