# tools/blender/build_materials.py # FBC: NIGHT SHIFT - one tileable concrete PBR set, generated in Blender. # # Run inside Blender 5.x: # exec(open("/home/dandovlatov/Documents/cntrl/tools/blender/build_materials.py").read()) # # Produces seamless 2K albedo / normal / roughness maps in # assets/textures/concrete/, a source material library .blend, and a preview # render. build_scene.py imports apply_concrete() so the diorama uses the exact # same maps as the in-game triplanar material. import math import os import bpy import numpy as np REPO = "/home/dandovlatov/Documents/cntrl" TEX_DIR = os.path.join(REPO, "assets", "textures", "concrete") BLEND_DIR = os.path.join(REPO, "assets", "blender") CONCEPT_DIR = os.path.join(REPO, "docs", "concept") SIZE = 2048 SEED = 0xC0FFEE for d in (TEX_DIR, BLEND_DIR, CONCEPT_DIR): os.makedirs(d, exist_ok=True) ALBEDO = os.path.join(TEX_DIR, "concrete_albedo.png") NORMAL = os.path.join(TEX_DIR, "concrete_normal.png") ROUGH = os.path.join(TEX_DIR, "concrete_roughness.png") # ------------------------------------------------------------ periodic noise _rng = np.random.default_rng(SEED) def value_noise(grid, size=SIZE): """Seamless value noise on a grid x grid lattice, sampled size x size.""" g = _rng.random((grid, grid)).astype(np.float32) x = np.arange(size, dtype=np.float32) * (grid / size) xi = x.astype(np.int32) xf = x - xi sx = xf * xf * (3.0 - 2.0 * xf) x0 = xi % grid x1 = (xi + 1) % grid row = g[:, x0] * (1.0 - sx)[None, :] + g[:, x1] * sx[None, :] y = x yi = x.astype(np.int32) yf = y - yi sy = yf * yf * (3.0 - 2.0 * yf) y0 = yi % grid y1 = (yi + 1) % grid return row[y0, :] * (1.0 - sy)[:, None] + row[y1, :] * sy[:, None] def fbm(grids, amps, size=SIZE): out = np.zeros((size, size), dtype=np.float32) total = 0.0 for grid, amp in zip(grids, amps): out += value_noise(grid, size) * amp total += amp return out / total def aggregate(size=SIZE, cells=44): """Periodic Voronoi distance: exposed aggregate / pinhole pits.""" pts = _rng.random((cells, cells, 2)).astype(np.float32) yy, xx = np.mgrid[0:size, 0:size].astype(np.float32) fx = xx * (cells / size) fy = yy * (cells / size) cx = np.floor(fx).astype(np.int32) % cells cy = np.floor(fy).astype(np.int32) % cells dmin = np.full((size, size), 1e9, dtype=np.float32) for dy in (-1, 0, 1): for dx in (-1, 0, 1): nx = (cx + dx) % cells ny = (cy + dy) % cells px = (cx + dx) + pts[ny, nx, 0] py = (cy + dy) + pts[ny, nx, 1] d = (fx - px) ** 2 + (fy - py) ** 2 np.minimum(dmin, d, out=dmin) return np.sqrt(dmin) def smoothstep(a, b, x): t = np.clip((x - a) / (b - a), 0.0, 1.0) return t * t * (3.0 - 2.0 * t) def save_png(name, rgb): h, w, _ = rgb.shape img = bpy.data.images.new(name, width=w, height=h, alpha=True, float_buffer=False) img.colorspace_settings.name = "Non-Color" # keep the exact byte values flat = np.zeros((h, w, 4), dtype=np.float32) flat[:, :, :3] = np.clip(rgb, 0.0, 1.0) flat[:, :, 3] = 1.0 img.pixels.foreach_set(flat.reshape(-1)) img.filepath_raw = name img.file_format = "PNG" img.save() bpy.data.images.remove(img) print("[concrete] saved", name) def generate(): grain = fbm((6, 12, 24, 48, 96, 192), (0.30, 0.24, 0.18, 0.13, 0.09, 0.06)) stain = fbm((2, 3, 6, 12), (0.45, 0.30, 0.17, 0.08)) fine = fbm((64, 128, 256), (0.5, 0.32, 0.18)) # Warp the Voronoi distance with noise so pores are irregular, not circles. dist = aggregate(cells=64) + (grain - 0.5) * 0.13 + (fine - 0.5) * 0.04 pits = 1.0 - smoothstep(0.012, 0.075, dist) grit = smoothstep(0.68, 0.88, fine) # Albedo: sealed #2b2b2f concrete with mottling, dark pores and pale grit. base = np.array([43.0, 43.0, 47.0], dtype=np.float32) / 255.0 variation = ((grain - 0.5) * 0.62 + (stain - 0.5) * 0.26 - pits * 0.13 + grit * 0.10) color = base[None, None, :] * (1.0 + variation[:, :, None] * 1.5) color[:, :, 0] += (stain - 0.5) * 0.014 # faint warm/cool patching color[:, :, 2] += (grain - 0.5) * 0.012 save_png(ALBEDO, color) # Height -> normal. Fine grain + aggregate pits, fully periodic. height = (grain * 0.45 + fine * 0.35 - pits * 0.24 + grit * 0.08).astype(np.float32) dx = np.roll(height, -1, axis=1) - np.roll(height, 1, axis=1) dy = np.roll(height, -1, axis=0) - np.roll(height, 1, axis=0) nx, ny, nz = -dx * 3.2, dy * 3.2, np.ones_like(height) inv = 1.0 / np.sqrt(nx * nx + ny * ny + nz * nz) normal = np.stack((nx * inv * 0.5 + 0.5, ny * inv * 0.5 + 0.5, nz * inv * 0.5 + 0.5), axis=-1) save_png(NORMAL, normal) # Roughness: uneven sealer — glossier where burnished, matte in the pores. rough = 0.30 + grain * 0.30 + pits * 0.18 - grit * 0.08 rough = np.clip(rough, 0.16, 0.88) save_png(ROUGH, np.stack((rough, rough, rough), axis=-1)) print("[concrete] generated 3 x %dpx seamless maps" % SIZE) # ------------------------------------------------------- Blender material use def load_maps(): maps = {} for key, path in (("albedo", ALBEDO), ("normal", NORMAL), ("rough", ROUGH)): if not os.path.exists(path): generate() break img = bpy.data.images.load(path, check_existing=True) img.colorspace_settings.name = ( "sRGB" if key == "albedo" else "Non-Color") maps[key] = img return maps def apply_concrete(materials): """Wire the shared maps into build_scene.py's concrete + polished floor.""" maps = load_maps() for key, mat in (("concrete", materials.get("concrete")), ("floor", materials.get("floor"))): if mat is None or not mat.use_nodes: continue nt = mat.node_tree b = next(n for n in nt.nodes if n.type == "BSDF_PRINCIPLED") for name in ("Color Image", "Normal Image", "Rough Image", "Normal Map", "Rough Scale"): old = nt.nodes.get(name) if old is not None: nt.nodes.remove(old) tex = nt.nodes.new("ShaderNodeTexImage") tex.name = "Color Image" tex.image = maps["albedo"] tex.location = (-900, 220) nt.links.new(tex.outputs["Color"], b.inputs["Base Color"]) ntex = nt.nodes.new("ShaderNodeTexImage") ntex.name = "Normal Image" ntex.image = maps["normal"] ntex.location = (-900, -60) nmap = nt.nodes.new("ShaderNodeNormalMap") nmap.name = "Normal Map" nmap.inputs["Strength"].default_value = 0.5 if key == "floor" else 0.85 nmap.location = (-520, -60) nt.links.new(ntex.outputs["Color"], nmap.inputs["Color"]) nt.links.new(nmap.outputs["Normal"], b.inputs["Normal"]) rtex = nt.nodes.new("ShaderNodeTexImage") rtex.name = "Rough Image" rtex.image = maps["rough"] rtex.location = (-900, -360) if key == "floor": scale = nt.nodes.new("ShaderNodeMath") scale.name = "Rough Scale" scale.operation = "MULTIPLY" scale.inputs[1].default_value = 0.55 scale.location = (-520, -360) nt.links.new(rtex.outputs["Color"], scale.inputs[0]) nt.links.new(scale.outputs[0], b.inputs["Roughness"]) else: nt.links.new(rtex.outputs["Color"], b.inputs["Roughness"]) print("[concrete] maps applied to diorama concrete + floor") # ------------------------------------------------------------- material demo def build_library(): bpy.ops.object.select_all(action="SELECT") bpy.ops.object.delete(use_global=False) maps = load_maps() def made(name, rough_mult, normal_strength): mat = bpy.data.materials.new(name) mat.use_nodes = True b = next(n for n in mat.node_tree.nodes if n.type == "BSDF_PRINCIPLED") nt = mat.node_tree for label, img, socket in (("albedo", maps["albedo"], "Base Color"), ("normal", maps["normal"], None), ("rough", maps["rough"], "Roughness")): node = nt.nodes.new("ShaderNodeTexImage") node.image = img if socket: nt.links.new(node.outputs["Color"], b.inputs[socket]) else: nmap = nt.nodes.new("ShaderNodeNormalMap") nmap.inputs["Strength"].default_value = normal_strength nt.links.new(node.outputs["Color"], nmap.inputs["Color"]) nt.links.new(nmap.outputs["Normal"], b.inputs["Normal"]) b.inputs["Roughness"].default_value = rough_mult return mat wall = made("Concrete_Wall", 1.0, 0.9) floor = made("Concrete_Polished", 0.55, 0.5) bpy.ops.mesh.primitive_cube_add(size=2.0, location=(-1.35, 0, 1.0)) cube = bpy.context.object cube.data.materials.append(wall) bpy.ops.mesh.primitive_plane_add(size=9.0, location=(0, 0, 0)) plane = bpy.context.object plane.data.materials.append(floor) light = bpy.data.lights.new("Key", "AREA") light.energy = 1800 light.size = 6 lo = bpy.data.objects.new("Key", light) bpy.context.scene.collection.objects.link(lo) lo.location = (-3, -4, 7) import mathutils lo.rotation_euler = (mathutils.Vector((0, 0, 0)) - lo.location).to_track_quat( "-Z", "Y").to_euler() cam = bpy.data.objects.new("MaterialCam", bpy.data.cameras.new("MaterialCam")) bpy.context.scene.collection.objects.link(cam) cam.location = (2.6, -8.5, 4.2) cam.rotation_euler = (mathutils.Vector((0, 0, 1.1)) - cam.location).to_track_quat( "-Z", "Y").to_euler() bpy.context.scene.camera = cam world = bpy.data.worlds.new("ConcreteWorld") bpy.context.scene.world = world world.use_nodes = True bg = next(n for n in world.node_tree.nodes if n.type == "BACKGROUND") bg.inputs["Color"].default_value = (0.02, 0.02, 0.025, 1) bg.inputs["Strength"].default_value = 0.6 scene = bpy.context.scene engines = [i.identifier for i in bpy.types.RenderSettings.bl_rna.properties["engine"].enum_items] scene.render.engine = ("BLENDER_EEVEE_NEXT" if "BLENDER_EEVEE_NEXT" in engines else engines[0]) scene.render.resolution_x = 900 scene.render.resolution_y = 560 scene.render.image_settings.file_format = "PNG" scene.render.filepath = os.path.join(CONCEPT_DIR, "concrete_material.png") try: scene.eevee.taa_render_samples = 64 except Exception: pass bpy.ops.render.render(write_still=True) print("[concrete] preview ->", scene.render.filepath) bpy.ops.wm.save_as_mainfile( filepath=os.path.join(BLEND_DIR, "fbc_concrete.blend")) print("[concrete] material library saved") if __name__ == "__main__": generate() build_library()