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:
30 self.inline_data = True
32 def export_texture(self, tex_node, usage='RGB', *, invert_green=False):
33 image = tex_node.image
34 from .datafile import Resource, Statement, Token
35 tex_res = Resource(image.name+".tex2d", "texture2d")
37 if tex_node.use_mipmap:
38 tex_res.statements.append(Statement("generate_mipmap", True))
40 colorspace = image.colorspace_settings.name
41 if usage=='GRAY' and colorspace=='sRGB':
42 raise Exception("Grayscale textures with sRGB colorspace are not supported")
44 from .util import basename
45 fn = basename(image.filepath)
46 if not self.inline_data and not invert_green and fn:
47 srgb = "_srgb" if colorspace=='sRGB' else ""
48 tex_res.statements.append(Statement("external_image"+srgb, fn))
51 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
55 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
57 tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
59 pixels = tuple(image.pixels)
62 texdata = encode_pixels(pixels_to_rgba(pixels))
64 texdata = encode_pixels(pixels_to_gray(pixels))
66 texdata = encode_pixels(pixels_to_rgb_invert_green(pixels))
68 texdata = encode_pixels(pixels_to_rgb(pixels))
69 tex_res.statements.append(Statement("raw_data", texdata))
73 class SamplerExporter:
74 def export_sampler(self, tex_node):
75 from .datafile import Resource, Statement, Token
76 samp_res = Resource(self.get_sampler_name(tex_node), "sampler")
78 use_interpolation = tex_node.interpolation!='Closest'
80 if tex_node.use_mipmap:
81 samp_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
83 samp_res.statements.append(Statement("filter", Token('LINEAR')))
84 samp_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
86 if tex_node.use_mipmap:
87 samp_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
89 samp_res.statements.append(Statement("filter", Token('NEAREST')))
91 if tex_node.extension=="REPEAT":
92 samp_res.statements.append(Statement("wrap", Token('REPEAT')))
93 elif tex_node.extension=="EXTEND":
94 samp_res.statements.append(Statement("wrap", Token('CLAMP_TO_EDGE')))
95 elif tex_node.extension=="CLIP":
96 samp_res.statements.append(Statement("wrap", Token('CLAMP_TO_BORDER')))
97 samp_res.statements.append(Statement("border_color", 0.0, 0.0, 0.0, 0.0))
101 def get_sampler_name(self, tex_node):
104 use_interpolation = tex_node.interpolation!='Closest'
105 name_parts.append("linear" if use_interpolation else "nearest")
106 if tex_node.use_mipmap:
107 name_parts.append("mip")
108 if use_interpolation and tex_node.max_anisotropy>1:
109 name_parts.append("aniso{:g}x".format(tex_node.max_anisotropy))
110 if tex_node.extension!="REPEAT":
111 name_parts.append("clip" if tex_node.extension=="CLIP" else "clamp")
113 return "_".join(name_parts)+".samp"