cntrl/tools/blender/process_concrete_maps.py

60 lines
2.2 KiB
Python

import os
import numpy as np
from PIL import Image, ImageFilter
REPO = "/home/dandovlatov/Documents/cntrl"
SRC = "/tmp/opencode"
DST = os.path.join(REPO, "assets", "textures", "concrete")
def load(name):
return np.asarray(Image.open(os.path.join(SRC, name + ".png")).convert("RGB"),
np.float32) / 255.0
def save(name, rgb):
Image.fromarray((np.clip(rgb, 0.0, 1.0) * 255.0 + 0.5).astype(np.uint8)).save(
os.path.join(DST, name + ".png"))
im = np.asarray(Image.open(os.path.join(DST, name + ".png")), np.float32) / 255.0
print("[concrete] %-22s mean %.3f seamX %.4f" %
(name + ".png", im.mean(), np.abs(im[:, 0] - im[:, -1]).mean()), flush=True)
def upscale(rgb):
h, w = rgb.shape[:2]
out = np.zeros((h * 2, w * 2, 3), np.float32)
for c in range(3):
out[:, :, c] = np.asarray(
Image.fromarray((rgb[:, :, c] * 255).astype(np.uint8)).resize(
(w * 2, h * 2), Image.Resampling.LANCZOS), np.float32) / 255.0
return out
def blur(a, radius):
return np.asarray(
Image.fromarray((np.clip(a, 0.0, 1.0) * 255.0).astype(np.uint8)).filter(
ImageFilter.GaussianBlur(radius)), np.float32) / 255.0
albedo = upscale(load("albedo")) * np.array([0.66, 0.66, 0.71], np.float32)
albedo = blur(albedo, 1.6)
rough = upscale(load("roughness"))
r = rough[:, :, 0]
rough[:, :, 0] = rough[:, :, 1] = rough[:, :, 2] = (
(r - r.min()) / max(r.max() - r.min(), 1e-5) * 0.27 + 0.45)
voronoi = upscale(load("height_voronoi"))[:, :, 0]
crack = blur(np.clip((0.92 - voronoi) / 0.5, 0.0, 1.0), 1.0)
albedo *= (1.0 - 0.22 * crack)[:, :, None]
height = (0.37 * (blur(rough[:, :, 0], 1.0) - rough[:, :, 0].mean())
+ 0.89 * (blur(voronoi, 1.0) - voronoi.mean()))
gx = (np.roll(height, -1, 1) - np.roll(height, 1, 1)) * 0.5
gy = (np.roll(height, -1, 0) - np.roll(height, 1, 0)) * 0.5
nx, ny, nz = -gx * 8.0, gy * 8.0, np.ones_like(height)
inv = 1.0 / np.sqrt(nx * nx + ny * ny + nz * nz)
normal = np.stack((nx * inv, ny * inv, nz * inv), 2) * 0.5 + 0.5
save("concrete_albedo", albedo)
save("concrete_roughness", rough)
save("concrete_normal", normal)
print("[concrete] 3 x 2048px seamless maps written")