Skip to content

Commit 81df8af

Browse files
committed
added thumbhash support, to load tiny versions of textures, which can be quicker. you can call texture_importer.generate_thumbhashes() to hash all the textures in the project to a thumbhashes.json file. if you then set texture_importer.use_thumbhash = True, it will load those instead of the full res textures.
1 parent e097c00 commit 81df8af

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pyperclip = "*"
2323
screeninfo = "*"
2424

2525
[tool.poetry.extras]
26-
extras = ["numpy", "imageio", "psd-tools3", "psutil"]
26+
extras = ["numpy", "imageio", "psd-tools3", "psutil", "tripy", "thumbhash"]
2727

2828

2929

ursina/texture_importer.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,37 @@
1010
file_types = ('.tif', '.jpg', '.jpeg', '.png', '.gif')
1111
textureless = False
1212

13+
use_thumbhash = False
14+
thumbhashes = None
15+
thumbhash_path = application.textures_compressed_folder / 'thumbhashes.json'
1316

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'):
1519
if textureless and '*' not in name:
1620
return Texture(application.internal_textures_folder/'white_cube.png')
1721

1822
if use_cache and name in imported_textures:
1923
return copy(imported_textures[name])
2024

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+
2139
if folder is not None:
2240
if not isinstance(folder, Path):
2341
raise TypeError(f'folder must be a Path, not a {type(folder)}')
2442
_folders = (folder,)
25-
43+
2644
else:
2745
_folders = (application.textures_compressed_folder, application.asset_folder, application.internal_textures_folder)
2846

@@ -115,6 +133,28 @@ def compress_textures(name=''):
115133
print(' compressing to png:', application.textures_compressed_folder / (f.stem + '.png'))
116134

117135

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+
118158

119159
if __name__ == '__main__':
120160
from ursina import *

0 commit comments

Comments
 (0)