5 def encode_pixels(pixels):
6 from .datafile import Token
7 return Token(codecs.decode(b"="+base64.b64encode(bytes(pixels))+b"=", "ascii"))
9 def pixels_to_rgba(pixels):
10 return (int(p*255) for p in pixels)
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)
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)
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)
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")
34 if tex_node.use_mipmap:
35 tex_res.statements.append(Statement("generate_mipmap", True))
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))
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))
48 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
52 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
54 tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
56 pixels = tuple(image.pixels)
59 texdata = encode_pixels(pixels_to_rgba(pixels))
61 texdata = encode_pixels(pixels_to_gray(pixels))
63 texdata = encode_pixels(pixels_to_rgb_invert_green(pixels))
65 texdata = encode_pixels(pixels_to_rgb(pixels))
66 tex_res.statements.append(Statement("raw_data", texdata))
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")
75 use_interpolation = tex_node.interpolation!='Closest'
77 if tex_node.use_mipmap:
78 samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
80 samp_res.statements.append(Statement("filter", Token('LINEAR')))
81 samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
83 if tex_node.use_mipmap:
84 samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
86 samp_res.statements.append(Statement("filter", Token('NEAREST')))
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))
98 def get_sampler_name(self, tex_node):
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")
110 return "_".join(name_parts)+".samp"