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.
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""Measure the physical scale of the rice scan from the height map.
|
|
|
|
python3 measure_scan_scale.py
|
|
|
|
Everything about apparent grain size depends on how much real rice the texture
|
|
covers, and ambientCG does not publish that. A spectral peak is useless here:
|
|
the radial average is dominated by pile-scale undulation (~240 px). Counting
|
|
grain crowns and measuring their second moments gives the grain scale directly.
|
|
"""
|
|
|
|
import numpy as np
|
|
from PIL import Image
|
|
from scipy import ndimage as ndi
|
|
|
|
HEIGHT = "tex/food_0015_height_2k.png"
|
|
TEX_PX = 2048.0
|
|
|
|
# Long-grain white rice, typical figure used to convert pixels to millimetres.
|
|
GRAIN_LENGTH_MM = 6.5
|
|
|
|
|
|
def measure(path=HEIGHT):
|
|
height = np.array(Image.open(path)).astype(np.float64)
|
|
# Remove pile-scale undulation so individual grains dominate.
|
|
flattened = height - ndi.gaussian_filter(height, 40)
|
|
crowns = flattened > np.percentile(flattened, 62)
|
|
labels, count = ndi.label(ndi.binary_opening(crowns, np.ones((5, 5))))
|
|
|
|
lengths, widths = [], []
|
|
for index, window in enumerate(ndi.find_objects(labels)):
|
|
if window is None:
|
|
continue
|
|
mask = labels[window] == index + 1
|
|
ys, xs = np.nonzero(mask)
|
|
if len(ys) < 300:
|
|
continue
|
|
values = np.sort(np.linalg.eigvalsh(np.cov(np.vstack([ys, xs]))))
|
|
if values[0] <= 0 or values[1] <= 0:
|
|
continue
|
|
lengths.append(4.0 * np.sqrt(values[1])) # 2 sigma each side
|
|
widths.append(4.0 * np.sqrt(values[0]))
|
|
|
|
return (count, len(lengths), float(np.median(lengths)),
|
|
float(np.median(widths)))
|
|
|
|
|
|
def main():
|
|
count, kept, length_px, width_px = measure()
|
|
print(f"grain crowns: {kept} used of {count} components")
|
|
print(f"median crown length {length_px:.1f} px, width {width_px:.1f} px, "
|
|
f"aspect {length_px / width_px:.2f}")
|
|
mm_per_px = GRAIN_LENGTH_MM / length_px
|
|
print(f"if a grain is {GRAIN_LENGTH_MM} mm long, the map spans "
|
|
f"{TEX_PX * mm_per_px:.0f} mm of rice")
|
|
print(f"grain as a fraction of the patch: {length_px / TEX_PX:.4f}")
|
|
print(f"true-scale patch for a 40 mm face: {GRAIN_LENGTH_MM * TEX_PX / length_px:.0f} mm")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|