"""Contact sheet + fake-lit preview for the procedural rice maps. python3 preview_maps.py [tex_proc] The lit panel shades the baked normal map with a directional light and the AO map, which is the fastest way to judge whether the bake is right without a full render. """ import os import sys import numpy as np from PIL import Image, ImageDraw, ImageFont FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" PANEL = 440 def load(directory): def grey(name): return np.array(Image.open(os.path.join(directory, name))).astype(np.float32) height = grey("rice_height_16bit.png") / 65535.0 normal = grey("rice_normal_opengl.png").reshape(*height.shape, 3) / 255.0 * 2 - 1 ao = grey("rice_ao_8bit.png") / 255.0 albedo = np.array(Image.open(os.path.join(directory, "rice_albedo.png")) ).astype(np.float32) / 255.0 return height, normal, ao, albedo def shade(normal, ao, albedo, azimuth=-40.0, elevation=35.0): az, el = np.deg2rad(azimuth), np.deg2rad(elevation) light = np.array([np.cos(el) * np.cos(az), np.cos(el) * np.sin(az), np.sin(el)]) lambert = np.clip((normal * light).sum(-1), 0.0, 1.0) return np.clip(albedo * ((0.10 + 0.90 * lambert) * (0.45 + 0.55 * ao))[..., None], 0.0, 1.0) def to_tile(array): if array.ndim == 2: array = np.repeat(array[..., None], 3, axis=2) return Image.fromarray((np.clip(array, 0, 1) * 255).astype(np.uint8)).resize( (PANEL, PANEL), Image.LANCZOS) def main(directory="tex_proc"): height, normal, ao, albedo = load(directory) lit = shade(normal, ao, albedo) # A 2x2 repeat proves the tiling, not just the single tile. tiled = np.tile(lit, (2, 2, 1)) panels = [("height", height), ("normal", normal * 0.5 + 0.5), ("ao", ao), ("albedo", albedo), ("lit", lit), ("2x2 lit", tiled)] width = PANEL * len(panels) + 10 * (len(panels) + 1) sheet = Image.new("RGB", (width, PANEL + 34), (28, 29, 31)) draw = ImageDraw.Draw(sheet) font = ImageFont.truetype(FONT, 17) for index, (label, data) in enumerate(panels): x = 10 + index * (PANEL + 10) sheet.paste(to_tile(data), (x, 30)) draw.text((x + 2, 6), label, font=font, fill=(225, 225, 225)) sheet.save(os.path.join(directory, "maps_sheet.png")) Image.fromarray((lit * 255).astype(np.uint8)).save( os.path.join(directory, "maps_lit.png")) print(f"wrote {directory}/maps_sheet.png") if __name__ == "__main__": main(sys.argv[1] if len(sys.argv) > 1 else "tex_proc")