Spaces:
Running
Running
Download tools/make_textures.py from VectorW/xlerobot-sim: direct link, hf CLI and curl.
- Browser
- Download file 2.04 kB
-
https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/tools/make_textures.py
- Command line
-
hf download hf://spaces/VectorW/xlerobot-sim/tools/make_textures.py
-
curl -L -o make_textures.py https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/tools/make_textures.py
2.04 kB
| """Photo textures for the Lit renderer, from Poly Haven (CC0): downloaded, resized, re-encoded into | |
| assets/textures/, with their mean colour (linear RGB), which src/render/materials.js keeps for the light | |
| baker (tools/bake_light.mjs). Toon and Pixel keep the drawn textures. | |
| usage: python tools/make_textures.py | |
| """ | |
| import io | |
| import json | |
| import os | |
| import urllib.request | |
| import numpy as np | |
| from PIL import Image | |
| ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") | |
| OUT = os.path.join(ROOT, "assets", "textures") | |
| # name in materials.js -> (Poly Haven asset, size in pixels) | |
| TEXTURES = { | |
| "wood_floor": ("wood_floor", 1024), | |
| "oak_veneer": ("oak_veneer_01", 1024), | |
| "wool": ("poly_wool_herringbone", 512), | |
| } | |
| AGENT = "xlerobot-sim texture fetch" | |
| def fetch(url): | |
| request = urllib.request.Request(url, headers={"User-Agent": AGENT}) | |
| with urllib.request.urlopen(request) as response: | |
| return response.read() | |
| def main(): | |
| os.makedirs(OUT, exist_ok=True) | |
| sources = {} | |
| for name, (asset, size) in TEXTURES.items(): | |
| files = json.loads(fetch(f"https://api.polyhaven.com/files/{asset}")) | |
| url = files["Diffuse"]["1k"]["jpg"]["url"] | |
| image = Image.open(io.BytesIO(fetch(url))).convert("RGB") | |
| if image.size[0] != size: | |
| image = image.resize((size, size), Image.LANCZOS) | |
| path = os.path.join(OUT, f"{name}.jpg") | |
| image.save(path, quality=88, optimize=True, progressive=True) | |
| srgb = np.asarray(image, dtype=np.float64) / 255 | |
| linear = np.where(srgb <= 0.04045, srgb / 12.92, ((srgb + 0.055) / 1.055) ** 2.4) | |
| mean = [round(float(v), 3) for v in linear.reshape(-1, 3).mean(axis=0)] | |
| sources[name] = {"source": f"https://polyhaven.com/a/{asset}", "license": "CC0", "mean": mean} | |
| print(f"{name}: {os.path.getsize(path) // 1024} KB, mean {mean}") | |
| with open(os.path.join(OUT, "sources.json"), "w", encoding="utf-8", newline="\n") as f: | |
| json.dump(sources, f, indent=2) | |
| f.write("\n") | |
| main() | |