-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathcentaur.py
118 lines (88 loc) · 3.07 KB
/
centaur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Preprocess the Centaur model
The model is available for download from
https://sketchfab.com/models/0d3f1b4a51144b7fbc4e2ff64d858413
The Python Imaging Library and NumPy are required
pip install pillow numpy
"""
from __future__ import print_function
import json
import os
import zipfile
import numpy
from PIL import Image
from utils.gltf import dump_obj_data
SRC_FILENAME = "centaur.zip"
DST_DIRECTORY = "../assets/centaur"
OBJ_FILENAMES = [
"body.obj",
None,
"flame.obj",
"gas.obj",
]
IMG_FILENAMES = {
"body": [
"textures/body_diffuse.jpeg",
"textures/body_emissive.jpeg",
"textures/body_specularGlossiness.png",
],
"flame": [
"textures/flame_diffuse.png",
"textures/flame_emissive.jpeg",
None,
],
"gas": [
"textures/material_diffuse.jpeg",
None,
"textures/material_specularGlossiness.png",
],
}
def fix_flame_data(flame_data):
start = flame_data.find("f 1/1/1 2/2/2 3/3/3")
end = flame_data.find("f 458/458/458 459/459/459 460/460/460")
return flame_data[:start] + flame_data[end:]
def process_meshes(zip_file):
gltf = json.loads(zip_file.read("scene.gltf"))
buffer = zip_file.read("scene.bin")
for mesh_index, filename in enumerate(OBJ_FILENAMES):
if filename:
obj_data = dump_obj_data(gltf, buffer, mesh_index)
if filename == "flame.obj":
obj_data = fix_flame_data(obj_data)
filepath = os.path.join(DST_DIRECTORY, filename)
with open(filepath, "w") as f:
f.write(obj_data)
def load_image(zip_file, filename):
with zip_file.open(filename) as f:
image = Image.open(f)
image = image.transpose(Image.FLIP_TOP_BOTTOM)
return image
def save_image(image, filename, size=512):
if max(image.size) > size:
image = image.resize((size, size), Image.LANCZOS)
filepath = os.path.join(DST_DIRECTORY, filename)
image.save(filepath, rle=True)
def fix_spec_image(image):
array = numpy.amax(numpy.array(image), axis=2) / 255.0
array = numpy.uint8(numpy.power(array, 1 / 2.2) * 255)
return Image.fromarray(array)
def process_images(zip_file):
for name, paths in IMG_FILENAMES.items():
diffuse_path, emission_path, specular_path = paths
if diffuse_path:
diffuse_image = load_image(zip_file, diffuse_path)
save_image(diffuse_image, "{}_diffuse.tga".format(name))
if emission_path:
emission_image = load_image(zip_file, emission_path)
save_image(emission_image, "{}_emission.tga".format(name))
if specular_path:
specular_image = load_image(zip_file, specular_path)
specular_image = fix_spec_image(specular_image)
save_image(specular_image, "{}_specular.tga".format(name))
def main():
if not os.path.exists(DST_DIRECTORY):
os.makedirs(DST_DIRECTORY)
with zipfile.ZipFile(SRC_FILENAME) as zip_file:
process_meshes(zip_file)
process_images(zip_file)
if __name__ == "__main__":
main()