Skip to content

Commit 0ad5bda

Browse files
authored
Add subtexture manifests and proper scaling support while packing textures
2 parents e41889a + aa92a5d commit 0ad5bda

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

deppth2/entries.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ def _unpack(self, path):
381381

382382
self._export_subtextures(fullpath)
383383

384+
def _export_subatlas_manifest(self, subatlas, subtexture_path):
385+
subatlas_manifest_path = os.path.splitext(subtexture_path)[0] + ".json"
386+
with open(subatlas_manifest_path, 'w') as f:
387+
json.dump(subatlas, f)
388+
384389
@requires('PIL.Image')
385390
def _export_subtextures(self, target, include_mip=False):
386391
# First, get the image out of the entry data
@@ -402,13 +407,13 @@ def _export_subtextures(self, target, include_mip=False):
402407
os.makedirs(os.path.join(target, subatlasdir), exist_ok=True)
403408
subtexture_path = get_unique_export_path(os.path.join(target, subatlasdir, f'{subatlasfile}.png'))
404409
subtexture.save(subtexture_path)
410+
self._export_subatlas_manifest(subatlas, subtexture_path)
405411

406412
def _get_original_image(self, image, original_size, top_left, scale_ratio):
407413
canvas_width = round(original_size['x']/scale_ratio['x'])
408414
canvas_height = round(original_size['y']/scale_ratio['y'])
409415
canvas = PIL.Image.new("RGBA", (canvas_width, canvas_height))
410416
canvas.paste(image, (top_left['x'], top_left['y']))
411-
canvas.resize((original_size['x'], original_size['y']))
412417
return canvas
413418

414419
def _extraction_path(self, target):

deppth2/texpacking.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def build_atlases_hades(source_dir, target_dir, deppth2_pack=True, include_hulls
7070
files = find_files(source_dir)
7171
hulls = {}
7272
namemap = {}
73+
scaleRatioMap = {}
74+
manifestHulls = {}
7375

7476
temp_dir = tempfile.mkdtemp()
7577
temp_name_mapping = {}
@@ -85,6 +87,9 @@ def build_atlases_hades(source_dir, target_dir, deppth2_pack=True, include_hulls
8587
hulls[rel_path] = []
8688
namemap[rel_path] = str(file_path)
8789

90+
scaleRatioMap[rel_path] = get_scale_ratio(file_path)
91+
manifestHulls[rel_path] = get_hull_from_manifest(file_path)
92+
8893
# Create temporary images with unique names for PyTexturePacker to not override when creating the json file
8994
for rel_path, original_path in namemap.items():
9095
temp_filename = f"img_{len(temp_files)}.png"
@@ -117,7 +122,7 @@ def build_atlases_hades(source_dir, target_dir, deppth2_pack=True, include_hulls
117122
with open(f'{basename}{index}.json', 'w') as f:
118123
json.dump(original_data, f, indent=2)
119124

120-
atlases.append(transform_atlas(target_dir, basename, f'{basename}{index}.json', namemap, hulls, source_dir, manifest_paths))
125+
atlases.append(transform_atlas(target_dir, basename, f'{basename}{index}.json', namemap, hulls, source_dir, manifest_paths, scaleRatioMap=scaleRatioMap, manifestHulls=manifestHulls))
121126
os.remove(f'{basename}{index}.json')
122127
index += 1
123128

@@ -149,7 +154,24 @@ def build_atlases_hades(source_dir, target_dir, deppth2_pack=True, include_hulls
149154

150155
shutil.rmtree(temp_dir)
151156

152-
@requires('scipy.spatial')
157+
def get_scale_ratio(path):
158+
default_scale = {"x":1.0, "y":1.0}
159+
image_manifest_path = os.path.splitext(path)[0] + ".json"
160+
if os.path.exists(image_manifest_path):
161+
with open(image_manifest_path, 'r') as f:
162+
image_manifest = json.load(f)
163+
return image_manifest.get("scaleRatio", default_scale)
164+
return default_scale
165+
166+
def get_hull_from_manifest(path):
167+
default_hull = []
168+
image_manifest_path = os.path.splitext(path)[0] + ".json"
169+
if os.path.exists(image_manifest_path):
170+
with open(image_manifest_path, 'r') as f:
171+
image_manifest = json.load(f)
172+
return image_manifest.get("hull", default_hull)
173+
return default_hull
174+
153175
def get_hull_points(path):
154176
try:
155177
import scipy.spatial
@@ -188,7 +210,7 @@ def find_files(source_dir):
188210
file_list.append(path)
189211
return file_list
190212

191-
def transform_atlas(target_dir, basename, filename, namemap, hulls={}, source_dir='', manifest_paths=[]):
213+
def transform_atlas(target_dir, basename, filename, namemap, hulls={}, source_dir='', manifest_paths=[], scaleRatioMap = {}, manifestHulls = []):
192214
with open(filename) as f:
193215
ptp_atlas = json.load(f)
194216
frames = ptp_atlas['frames']
@@ -205,18 +227,22 @@ def transform_atlas(target_dir, basename, filename, namemap, hulls={}, source_di
205227
subatlas['name'] = os.path.join(basename, os.path.splitext(os.path.relpath(namemap[texture_name], source_dir))[0])
206228
manifest_paths.append(subatlas['name'])
207229
subatlas['topLeft'] = {'x': frame['spriteSourceSize']['x'], 'y': frame['spriteSourceSize']['y']}
208-
subatlas['originalSize'] = {'x': frame['sourceSize']['w'], 'y': frame['sourceSize']['h']}
230+
subatlas['scaleRatio'] = scaleRatioMap.get(texture_name, {'x': 1.0, 'y': 1.0 })
231+
subatlas['originalSize'] = {
232+
'x': round(frame['sourceSize']['w'] * subatlas['scaleRatio']['x']),
233+
'y': round(frame['sourceSize']['h'] * subatlas['scaleRatio']['y'])}
209234
subatlas['rect'] = {
210235
'x': frame['frame']['x'],
211236
'y': frame['frame']['y'],
212237
'width': frame['frame']['w'],
213238
'height': frame['frame']['h']
214239
}
215-
subatlas['scaleRatio'] = {'x': 1.0, 'y': 1.0}
216240
subatlas['isMulti'] = False
217241
subatlas['isMip'] = False
218242
subatlas['isAlpha8'] = False
219243
subatlas['hull'] = transform_hull(hulls.get(texture_name, []), subatlas['topLeft'], (subatlas['rect']['width'], subatlas['rect']['height']))
244+
if len(subatlas['hull']) == 0:
245+
subatlas['hull'] = manifestHulls.get(texture_name, [])
220246
atlas.subAtlases.append(subatlas)
221247

222248
atlas.export_file(f'{os.path.splitext(filename)[0]}.atlas.json')

0 commit comments

Comments
 (0)