]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_texture.py
Refactor resolution of declarations for MemberAccess
[libs/gl.git] / blender / io_mspgl / export_texture.py
1 import os
2
3 class TextureExporter:
4         def __init__(self):
5                 self.inline_data = True
6
7         def export_texture(self, tex_node, usage='RGB'):
8                 image = tex_node.image
9                 from .datafile import Resource, Statement, Token
10                 tex_res = Resource(image.name+".tex2d", "texture2d")
11
12                 if tex_node.use_mipmap:
13                         tex_res.statements.append(Statement("generate_mipmap", True))
14
15                 colorspace = image.colorspace_settings.name
16                 if usage=='GRAY' and colorspace=='sRGB':
17                                 raise Exception("Grayscale textures with sRGB colorspace are not supported")
18
19                 from .util import basename
20                 fn = basename(image.filepath)
21                 if not self.inline_data and fn:
22                         srgb = "_srgb" if colorspace=='sRGB' else ""
23                         tex_res.statements.append(Statement("external_image"+srgb, fn))
24                 else:
25                         if usage=='RGBA':
26                                 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
27                         elif usage=='GRAY':
28                                 fmt = 'LUMINANCE8'
29                         else:
30                                 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
31
32                         tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
33
34                         texdata = ""
35                         if usage=='RGBA':
36                                 for p in image.pixels:
37                                         texdata += "\\x{:02X}".format(int(p*255))
38                         elif usage=='GRAY':
39                                 for i in range(0, len(image.pixels), 4):
40                                         texdata += "\\x{:02X}".format(image.pixels[i])
41                         else:
42                                 for i in range(0, len(image.pixels), 4):
43                                         for j in range(3):
44                                                 texdata += "\\x{:02X}".format(int(image.pixels[i+j]*255))
45                         tex_res.statements.append(Statement("raw_data", texdata))
46
47                 return tex_res
48
49 class SamplerExporter:
50         def export_sampler(self, tex_node):
51                 from .datafile import Resource, Statement, Token
52                 samp_res = Resource(self.get_sampler_name(tex_node), "sampler")
53
54                 use_interpolation = tex_node.interpolation!='Closest'
55                 if use_interpolation:
56                         if tex_node.use_mipmap:
57                                 samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
58                         else:
59                                 samp_res.statements.append(Statement("filter", Token('LINEAR')))
60                         samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
61                 else:
62                         if tex_node.use_mipmap:
63                                 samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
64                         else:
65                                 samp_res.statements.append(Statement("filter", Token('NEAREST')))
66
67                 return samp_res
68
69         def get_sampler_name(self, tex_node):
70                 name_parts = []
71
72                 use_interpolation = tex_node.interpolation!='Closest'
73                 name_parts.append("linear" if use_interpolation else "nearest")
74                 if tex_node.use_mipmap:
75                         name_parts.append("mip")
76                 if use_interpolation and tex_node.max_anisotropy>1:
77                         name_parts.append("aniso{:g}x".format(tex_node.max_anisotropy))
78
79                 return "_".join(name_parts)+".samp"