Two halves, one pipeline. make_rice_texture.py models short-grain rice as randomly oriented 3D ellipsoids (surface height solved as the quadratic root, so any orientation works -- including grains standing on end), scattered by Poisson disk on a torus and unioned into a height field. Because the scatter wraps by index, the baked height/normal/AO/albedo set tiles exactly; that is proven by translating the whole scatter a quarter patch and requiring a rolled copy back (0.0 mm error), not by sampling a seam line, which turned out to be a coin flip. make_rice_cube.py displaces a filleted cube with that height map: voxel remesh for uniform vertex density, then triplanar sampling of the height field in Geometry Nodes on object-space position. Exports STL via Blender and a hand-rolled core-1.0 3MF, because this Blender build has no 3MF support and the nix store is read-only. Kept here: the procedural map set, the render of each body that was built (40 mm / 4 mm fillet, and 160 mm / 8 mm), and the A/B sheets that record why -- fusing is voxel resolution, relief ratio 0.6 beats the map's own 0.9 on a small body, and the fillet only reads once the body is large relative to the relief. Not kept: mesh binaries (560 MB, 384 MB of it one STL), the scan extract, and the baseline runs whose renders survive inside the sheets. rebuild.sh regenerates all of it, in dependency order, including the sheets.
71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
"""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")
|