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 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))
26 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
30 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
32 tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
36 for p in image.pixels:
37 texdata += "\\x{:02X}".format(int(p*255))
39 for i in range(0, len(image.pixels), 4):
40 texdata += "\\x{:02X}".format(image.pixels[i])
42 for i in range(0, len(image.pixels), 4):
44 texdata += "\\x{:02X}".format(int(image.pixels[i+j]*255))
45 tex_res.statements.append(Statement("raw_data", texdata))
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")
54 use_interpolation = tex_node.interpolation!='Closest'
56 if tex_node.use_mipmap:
57 samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
59 samp_res.statements.append(Statement("filter", Token('LINEAR')))
60 samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
62 if tex_node.use_mipmap:
63 samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
65 samp_res.statements.append(Statement("filter", Token('NEAREST')))
69 def get_sampler_name(self, tex_node):
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))
79 return "_".join(name_parts)+".samp"