Created
March 21, 2015 06:48
-
-
Save SharkyRawr/29bf91bf1fea14a77004 to your computer and use it in GitHub Desktop.
Valve GoldSrc/Source engine skybox to Cubemap composer
This file contains 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
from PIL import Image | |
import sys, os, re | |
from os import path | |
transforms = ( | |
('FT', 90), ('BK', 270), ('LF', 180), ('RT', 0), ('UP', 0), ('DN', 180) | |
) | |
def usage(): | |
print "%s <skyname> (without direction suffix UP|DN|FT|BK|LF|RT)" % (sys.argv[0],) | |
sys.exit(1) | |
if __name__ == "__main__": | |
if len(sys.argv) <= 1: | |
usage() | |
arg = sys.argv[1] | |
dir = path.dirname(arg) | |
prefix = path.basename(arg) | |
cubefilepat = re.compile(prefix + r'(UP|DN|LF|RT|FT|BK)\..*', re.IGNORECASE) | |
print "Scanning:", dir | |
files = {} | |
for f in os.listdir(dir): | |
r = cubefilepat.match(f) | |
if r is not None: | |
files[r.group(1).upper()] = r.group(0) | |
if len(files) != 6: | |
print "Too few or too many files:" | |
print files | |
sys.exit(1) | |
ft = Image.open(path.join(dir, files['FT'])) | |
w, h = ft.size | |
ft.close() | |
print "Skybox size is: %dx%d" % (w,h) | |
print "Compositing cubemap ..." | |
cm = Image.new('RGBA', (w*6, h)) | |
for direction, rot in transforms: | |
fn = path.join(dir, files[direction]) | |
side = Image.open(fn) | |
siderot = side.rotate(rot) | |
side.close() | |
box = (w * transforms.index((direction, rot)), 0) | |
tox, toy = box | |
print "%s to X:%d, Y:%d, r:%d" % (files[direction], tox, toy, rot) | |
cm.paste(siderot, box) | |
siderot.close() | |
cm.save('cubemap.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment