Spaces:
Running on Zero
Running on Zero
Download make_examples.py from trass3r/diffuse-to-pbr: direct link, hf CLI and curl.
- Browser
- Download file 6.69 kB
-
https://huggingface.co/spaces/trass3r/diffuse-to-pbr/resolve/main/make_examples.py
- Command line
-
hf download hf://spaces/trass3r/diffuse-to-pbr/make_examples.py
-
curl -L -o make_examples.py https://huggingface.co/spaces/trass3r/diffuse-to-pbr/resolve/main/make_examples.py
6.69 kB
| """Generate example diffuse game textures (with baked-in lighting) for the demo. | |
| These mimic the look of old game assets: a base albedo multiplied by a baked | |
| directional light, ambient-occlusion darkening around details, soft highlight | |
| blobs and weathering. They live in examples/ and are wired into gr.Examples. | |
| """ | |
| import os | |
| import numpy as np | |
| from PIL import Image, ImageFilter | |
| rng = np.random.default_rng(42) | |
| SIZE = 512 | |
| OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "examples") | |
| os.makedirs(OUT, exist_ok=True) | |
| def blur(arr, radius): | |
| """Gaussian blur a float array (0..1) via PIL.""" | |
| mono = arr.ndim == 2 | |
| if mono: | |
| im = Image.fromarray((np.clip(arr, 0, 1) * 255.0).astype(np.uint8)) | |
| else: | |
| im = Image.fromarray((np.clip(arr, 0, 1) * 255.0).astype(np.uint8), "RGB") | |
| out = np.asarray(im.filter(ImageFilter.GaussianBlur(radius))).astype(np.float32) / 255.0 | |
| return out[..., 0] if mono else out | |
| def save(name, rgb): | |
| Image.fromarray((np.clip(rgb, 0, 1) * 255.0).astype(np.uint8), "RGB").save( | |
| os.path.join(OUT, name) | |
| ) | |
| def light_map(h, w, top=1.25, bottom=0.55, highlight=0.35, ao=None): | |
| """Baked lighting: directional gradient + soft highlight blob, optional AO mask.""" | |
| yy, xx = np.mgrid[0:h, 0:w] | |
| t = yy / max(h - 1, 1) | |
| grad = bottom + (top - bottom) * (1.0 - t) ** 1.4 # bright top, dark bottom | |
| dx, dy = xx / w - 0.55, yy / h - 0.28 | |
| blob = np.exp(-(dx * dx + dy * dy) / (2 * 0.38**2)) | |
| light = grad * (1.0 + highlight * blob) | |
| if ao is not None: | |
| light = light * ao | |
| return light[..., None] | |
| def scifi_panel(): | |
| h = w = SIZE | |
| yy, xx = np.mgrid[0:h, 0:w] | |
| # brushed gunmetal base | |
| base = np.full((h, w, 3), (0.62, 0.64, 0.70), dtype=np.float32) | |
| base += rng.normal(0, 0.02, (h, w, 3)).astype(np.float32) | |
| brush = rng.normal(0, 1, (h, w)).astype(np.float32) | |
| brush = (blur((brush + 3) / 6, 1.2) - 0.5) * 2 * 0.03 # centered ± grain | |
| base += brush[..., None] | |
| # panel grid grooves | |
| cell = 64 | |
| groove = ((xx % cell) < 5) | ((yy % cell) < 5) | |
| # AO mask: dark around grooves and rivets, feathered | |
| ao = np.ones((h, w), np.float32) | |
| ao[groove] = 0.45 | |
| rivet_pos = [(cx, cy) for cx in range(cell // 2, w, cell) for cy in range(cell // 2, h, cell)] | |
| rivets = np.zeros((h, w), bool) | |
| for cx, cy in rivet_pos: | |
| rivets |= np.hypot(xx - cx, yy - cy) <= 7 | |
| ao[rivets] = 0.35 | |
| ao = blur(ao, 3) | |
| # render rivets: dark ring + specular dot | |
| for cx, cy in rivet_pos: | |
| d = np.hypot(xx - cx, yy - cy) | |
| ring = 0.25 * np.exp(-((d - 5.0) ** 2) / 2.0) | |
| dot = 0.30 * np.exp(-(d**2) / (2 * 1.6**2)) | |
| base = base * (1.0 - ring[..., None]) + dot[..., None] | |
| # scratches | |
| for _ in range(36): | |
| cx, cy = rng.uniform(0, w), rng.uniform(0, h) | |
| ang = rng.uniform(0, np.pi) | |
| ln = rng.uniform(10, 42) | |
| tx = np.clip((np.arange(ln) * np.cos(ang) + cx).astype(int), 0, w - 1) | |
| ty = np.clip((np.arange(ln) * np.sin(ang) + cy).astype(int), 0, h - 1) | |
| v = rng.choice([-0.14, 0.16]) | |
| base[ty, tx] += v | |
| light = light_map(h, w, top=1.32, bottom=0.48, highlight=0.42, ao=ao) | |
| save("scifi_panel.png", base * light) | |
| def stone_wall(): | |
| h = w = SIZE | |
| yy, xx = np.mgrid[0:h, 0:w] | |
| n = 7 | |
| cxs = (np.arange(n) + 0.5) * (w / n) + rng.uniform(-0.18, 0.18, n) * (w / n) | |
| cys = (np.arange(n) + 0.5) * (h / n) + rng.uniform(-0.18, 0.18, n) * (h / n) | |
| idx_x = np.zeros((h, w), int) | |
| best = np.full((h, w), np.inf) | |
| for cx in cxs: | |
| for cy in cys: | |
| d = np.hypot(xx - cx, yy - cy) | |
| m = d < best | |
| best[m] = d[m] | |
| idx_x[m] = int(cx) | |
| cells = np.zeros((h, w, 3), np.float32) | |
| cell_radius = (w / n) * 0.62 | |
| rn = best / cell_radius | |
| mortar = rn > 0.82 | |
| for cx in cxs: | |
| for cy in cys: | |
| m = idx_x == int(cx) | |
| tone = rng.uniform(0.52, 0.76, 3).astype(np.float32) * np.array([1.0, 0.93, 0.84], np.float32) | |
| cells[m] = tone | |
| # stone grain | |
| grain = rng.normal(0, 1, (h, w, 3)).astype(np.float32) | |
| grain = (blur((grain + 2) / 4, 1.4) - 0.5) * 0.35 | |
| cells = np.clip(cells + grain, 0, 1) | |
| cells[mortar] = (0.30, 0.27, 0.24) | |
| # AO in crevices | |
| ao = np.ones((h, w), np.float32) | |
| ao[mortar] = 0.45 | |
| ao = blur(ao, 4) | |
| light = light_map(h, w, top=1.45, bottom=0.42, highlight=0.22, ao=ao) | |
| save("stone_wall.png", cells * light) | |
| def painted_metal(): | |
| h = w = SIZE | |
| yy, xx = np.mgrid[0:h, 0:w] | |
| base = np.full((h, w, 3), (0.35, 0.42, 0.38), np.float32) # faded teal paint | |
| # blotchy paint variation | |
| blobs = rng.normal(0, 1, (h, w)).astype(np.float32) | |
| blobs = (blur((blobs + 2) / 4, 5) - 0.5) * 0.9 | |
| base = base * (1.0 + blobs[..., None]) | |
| # rust clusters (mostly bottom half) | |
| rust = np.zeros((h, w), np.float32) | |
| for _ in range(26): | |
| cx, cy = rng.uniform(0, w), rng.uniform(h * 0.35, h) | |
| r = rng.uniform(8, 30) | |
| rust = np.maximum(rust, np.exp(-(np.hypot(xx - cx, yy - cy) ** 2) / (2 * r**2))) | |
| rust_color = np.array([0.62, 0.26, 0.10], np.float32) | |
| base = base * (1.0 - 0.75 * rust[..., None]) + rust[..., None] * rust_color | |
| # chipped paint → bright metal showing through | |
| chips = np.zeros((h, w), np.float32) | |
| for _ in range(40): | |
| cx, cy = rng.uniform(0, w), rng.uniform(0, h) | |
| r = rng.uniform(1.5, 5) | |
| chips = np.maximum(chips, np.exp(-(np.hypot(xx - cx, yy - cy) ** 2) / (2 * r**2))) | |
| metal = np.array([0.72, 0.73, 0.76], np.float32) | |
| base = base * (1.0 - chips[..., None]) + chips[..., None] * metal | |
| # scratches | |
| for _ in range(24): | |
| cx, cy = rng.uniform(0, w), rng.uniform(0, h) | |
| ang = rng.uniform(0, np.pi) | |
| ln = rng.uniform(8, 30) | |
| tx = np.clip((np.arange(ln) * np.cos(ang) + cx).astype(int), 0, w - 1) | |
| ty = np.clip((np.arange(ln) * np.sin(ang) + cy).astype(int), 0, h - 1) | |
| base[ty, tx] = np.array([0.78, 0.79, 0.82], np.float32) | |
| # grime streaks | |
| grime = np.zeros((h, w), np.float32) | |
| for _ in range(8): | |
| gx = rng.uniform(0, w) | |
| gw = rng.uniform(3, 12) | |
| grime += np.exp(-((xx - gx) ** 2) / (2 * gw**2)) * (yy / h) ** 1.5 | |
| grime = np.clip(grime, 0, 1) * 0.28 | |
| base = base * (1.0 - grime[..., None]) | |
| # slight AO under rust | |
| ao = 1.0 - 0.22 * rust | |
| ao = blur(ao, 3) | |
| light = light_map(h, w, top=1.18, bottom=0.68, highlight=0.5, ao=ao) | |
| save("painted_metal.png", base * light) | |
| if __name__ == "__main__": | |
| scifi_panel() | |
| stone_wall() | |
| painted_metal() | |
| print("Wrote examples to", OUT) | |