Created
July 6, 2022 02:46
-
-
Save larryhou/e0a6ca9fd49a3fa56df92975a3c92239 to your computer and use it in GitHub Desktop.
ASTC texture generating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import math | |
import os | |
import re | |
import sys | |
from PIL import Image | |
def main(): | |
arguments = argparse.ArgumentParser() | |
arguments.add_argument('--file', '-f', nargs='+', required=True, help='texture file') | |
arguments.add_argument('--block', '-b', default=4, type=int, help='astc block size') | |
arguments.add_argument('--quality', '-q', choices=('fastest', 'fast', 'medium', 'thorough', 'exhaustive'), default='medium', help='texture quality tier') | |
arguments.add_argument('--minimum', '-m', default=64, type=int, help='minimum texture size') | |
options = arguments.parse_args(sys.argv[1:]) | |
mininum = 2**round(math.log2(options.minimum)) | |
for filename in options.file: | |
im = Image.open(filename) | |
size = im.size[0] | |
if im.size[1] < size: size = im.size[1] | |
dx = (im.size[0] - size)//2 | |
dy = (im.size[1] - size)//2 | |
im = im.crop((dx,dy,dx+size,dy+size)) | |
pot = 2**math.floor(math.log2(size)) | |
dir = re.sub(r'\.[^.]+$', '', filename) | |
os.makedirs(dir, exist_ok=True) | |
while pot >= mininum: | |
mip = im.resize(size=(pot, pot), resample=Image.Resampling.BICUBIC) | |
tga = os.path.join(dir, '{}.tga'.format(pot)) | |
mip.save(tga) | |
tex = os.path.join(dir, '{}.astc'.format(pot)) | |
command = 'astcenc -cs {0} {1} {2}x{2} -{3}'.format(tga, tex, options.block, options.quality) | |
print('+', command) | |
assert os.system(command) == 0 | |
pot >>= 1 | |
if __name__ == '__main__': main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment