Spaces:
Running
Running
File size: 2,039 Bytes
95a7e69 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | """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()
|