5 self.inline_data = True
7 def export_texture(self, tex_node, usage='RGB'):
9 from .datafile import Resource, Statement, Token
10 tex_res = Resource(image.name+".tex2d", "texture2d")
12 if tex_node.use_mipmap:
13 tex_res.statements.append(Statement("generate_mipmap", True))
15 colorspace = image.colorspace_settings.name
16 if usage=='GRAY' and colorspace=='sRGB':
17 raise Exception("Grayscale textures with sRGB colorspace are not supported")
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))
25 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
29 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
31 tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
35 for p in image.pixels:
36 texdata += "\\x{:02X}".format(int(p*255))
38 for i in range(0, len(image.pixels), 4):
39 texdata += "\\x{:02X}".format(image.pixels[i])
41 for i in range(0, len(image.pixels), 4):
43 texdata += "\\x{:02X}".format(int(image.pixels[i+j]*255))
44 tex_res.statements.append(Statement("raw_data", texdata))
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")
53 use_interpolation = tex_node.interpolation!='Closest'
55 if tex_node.use_mipmap:
56 samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
58 samp_res.statements.append(Statement("filter", Token('LINEAR')))
59 samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
61 if tex_node.use_mipmap:
62 samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
64 samp_res.statements.append(Statement("filter", Token('NEAREST')))
68 def get_sampler_name(self, tex_node):
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))
78 return "_".join(name_parts)+".samp"