]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_texture.py
Export texture sampling settings as sampler objects
[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                 fn = os.path.basename(image.filepath)
20                 if not self.inline_data and fn:
21                         srgb = "_srgb" if colorspace=='sRGB' else ""
22                         tex_res.statements.append(Statement("external_image"+srgb, fn))
23                 else:
24                         if usage=='RGBA':
25                                 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
26                         elif usage=='GRAY':
27                                 fmt = 'LUMINANCE8'
28                         else:
29                                 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
30
31                         tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
32
33                         texdata = ""
34                         if usage=='RGBA':
35                                 for p in image.pixels:
36                                         texdata += "\\x{:02X}".format(int(p*255))
37                         elif usage=='GRAY':
38                                 for i in range(0, len(image.pixels), 4):
39                                         texdata += "\\x{:02X}".format(image.pixels[i])
40                         else:
41                                 for i in range(0, len(image.pixels), 4):
42                                         for j in range(3):
43                                                 texdata += "\\x{:02X}".format(int(image.pixels[i+j]*255))
44                         tex_res.statements.append(Statement("raw_data", texdata))
45
46                 return tex_res
47
48 class SamplerExporter:
49         def export_sampler(self, tex_node):
50                 from .datafile import Resource, Statement, Token
51                 samp_res = Resource(self.get_sampler_name(tex_node), "sampler")
52
53                 use_interpolation = tex_node.interpolation!='Closest'
54                 if use_interpolation:
55                         if tex_node.use_mipmap:
56                                 samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
57                         else:
58                                 samp_res.statements.append(Statement("filter", Token('LINEAR')))
59                         samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
60                 else:
61                         if tex_node.use_mipmap:
62                                 samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
63                         else:
64                                 samp_res.statements.append(Statement("filter", Token('NEAREST')))
65
66                 return samp_res
67
68         def get_sampler_name(self, tex_node):
69                 name_parts = []
70
71                 use_interpolation = tex_node.interpolation!='Closest'
72                 name_parts.append("linear" if use_interpolation else "nearest")
73                 if tex_node.use_mipmap:
74                         name_parts.append("mip")
75                 if use_interpolation and tex_node.max_anisotropy>1:
76                         name_parts.append("aniso{:g}x".format(tex_node.max_anisotropy))
77
78                 return "_".join(name_parts)+".samp"