@@ -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+
153175def 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