|
10 | 10 | file_types = ('.tif', '.jpg', '.jpeg', '.png', '.gif')
|
11 | 11 | textureless = False
|
12 | 12 |
|
| 13 | +use_thumbhash = False |
| 14 | +thumbhashes = None |
| 15 | +thumbhash_path = application.textures_compressed_folder / 'thumbhashes.json' |
13 | 16 |
|
14 |
| -def load_texture(name, folder=None, use_cache=True, filtering='default'): |
| 17 | + |
| 18 | +def load_texture(name, folder:Path=None, use_cache=True, filtering='default'): |
15 | 19 | if textureless and '*' not in name:
|
16 | 20 | return Texture(application.internal_textures_folder/'white_cube.png')
|
17 | 21 |
|
18 | 22 | if use_cache and name in imported_textures:
|
19 | 23 | return copy(imported_textures[name])
|
20 | 24 |
|
| 25 | + if use_thumbhash: |
| 26 | + global thumbhashes |
| 27 | + import thumbhash |
| 28 | + import json |
| 29 | + if thumbhashes is None: |
| 30 | + with thumbhash_path.open('r') as file: |
| 31 | + thumbhashes = json.load(file) |
| 32 | + |
| 33 | + if name in thumbhashes: |
| 34 | + image = thumbhash.thumbhash_to_image(thumbhashes[name]) |
| 35 | + tex = Texture(image, filtering='bilinear') |
| 36 | + imported_textures[name] = tex |
| 37 | + return tex |
| 38 | + |
21 | 39 | if folder is not None:
|
22 | 40 | if not isinstance(folder, Path):
|
23 | 41 | raise TypeError(f'folder must be a Path, not a {type(folder)}')
|
24 | 42 | _folders = (folder,)
|
25 |
| - |
| 43 | + |
26 | 44 | else:
|
27 | 45 | _folders = (application.textures_compressed_folder, application.asset_folder, application.internal_textures_folder)
|
28 | 46 |
|
@@ -115,6 +133,28 @@ def compress_textures(name=''):
|
115 | 133 | print(' compressing to png:', application.textures_compressed_folder / (f.stem + '.png'))
|
116 | 134 |
|
117 | 135 |
|
| 136 | +def generate_thumbhashes(): |
| 137 | + import json |
| 138 | + thumbhashes = dict() |
| 139 | + # for suffix in ('jpg', 'jpeg', 'png', 'tif'): |
| 140 | + for image_path in application.asset_folder.glob('**/*.*'): |
| 141 | + if image_path.suffix not in ('.jpg', '.jpeg', '.png', '.tif'): |
| 142 | + continue |
| 143 | + if image_path.stem in thumbhashes: |
| 144 | + continue |
| 145 | + |
| 146 | + print('making thumbhash for:', image_path.name) |
| 147 | + with image_path.open('rb') as image_file: |
| 148 | + thumbhashes[image_path.stem] = image_to_thumbhash(image_file) |
| 149 | + # image = thumbhash.thumbhash_to_image(thumbhashes[name], base_size=128) |
| 150 | + # tex = Texture(image) |
| 151 | + |
| 152 | + if not application.textures_compressed_folder.exists(): |
| 153 | + application.textures_compressed_folder.mkdir() |
| 154 | + |
| 155 | + with thumbhash_path.open('w') as file: |
| 156 | + json.dump(thumbhashes, file, indent=0) |
| 157 | + |
118 | 158 |
|
119 | 159 | if __name__ == '__main__':
|
120 | 160 | from ursina import *
|
|
0 commit comments