"""Conditions on any 3D model (parity with pydraw/condition_px.py): the parser's attrs["cond"] (pydraw/language.py) re-dress a model's parts after it is built. Model frame: base on y=0, about one unit tall, centred on x=0. surface rusty / mossy / moldy / dirty / burnt / weathered / withered / frosty / wet / torn / broken: the part's texture is wrapped (the old texture first, then the condition mixed in by 3D noise), plus spec / rough light shiny -> high spec, low rough (and a touch of metal on grey things); glowing -> emissive shape twisted (domain warp: sideways wave growing with height + lean), stout / thin (wider / narrower in x and z); the SDF is scaled down by the warp's stretch so marching stays safe Knob: PYDRAW_LANG_OFF=cond3d (or PYDRAW_LANG=0). """ import numpy as np from . import sdf as S def _lin(c): return (np.asarray(c, float) / 255.0) ** 2.2 def _mixer(kind, seed): """texture post-function (q, albedo) -> albedo for a surface condition""" rust, moss, mold, dirt, char = _lin((150, 72, 30)), _lin((68, 112, 44)), _lin((150, 160, 120)), _lin((92, 76, 58)), _lin((26, 20, 18)) rime, dry = _lin((236, 244, 255)), _lin((150, 112, 62)) def f(q, a): if kind == "rusty": n = S.fbm(q * 7, 3, seed) t = np.clip((n - .45) * 3.2, 0, .85)[:, None] return a * (1 - t) + rust * t * (.7 + .5 * S.noise3(q * 40, seed + 1)[:, None]) if kind == "mossy": n = S.fbm(q * 6, 3, seed) + np.clip(.35 - q[:, 1], 0, .35) t = np.clip((n - .55) * 3, 0, .9)[:, None] return a * (1 - t) + moss * t * (.75 + .5 * S.noise3(q * 50, seed + 2)[:, None]) if kind == "moldy": t = (S.noise3(q * 22, seed) > .72)[:, None] * .75 return a * (1 - t) + mold * t if kind == "dirty": n = S.fbm(q * 6, 3, seed) + (1 - np.clip(q[:, 1], 0, 1)) * .35 t = np.clip((n - .6) * 2.5, 0, .7)[:, None] return a * (1 - t) + dirt * t if kind == "burnt": n = S.fbm(q * 5, 3, seed) t = np.clip(.7 + (n - .3) * 1.2, .55, .95)[:, None] # mostly charred, a few lighter scorched patches return a * (1 - t) + char * t if kind == "weathered": L = (a * np.array([.3, .59, .11])).sum(1, keepdims=True) g = (a * .4 + L * .6 * np.array([1.02, 1.0, .94])) * .72 + np.array([.06, .05, .035]) return g * (.7 + .45 * S.noise3(q * np.array([60, 5, 60]), seed)[:, None]) if kind == "withered": L = (a * np.array([.3, .59, .11])).sum(1, keepdims=True) return a * .35 + dry * (L / max(1e-3, float((dry * np.array([.3, .59, .11])).sum()))) * .65 if kind == "frosty": up = np.clip((S.noise3(q * 30, seed) - .3) * 2, 0, 1)[:, None] * .6 return a * .75 * (1 - up) + rime * (up + .25 * (1 - up)) if kind == "wet": L = (a * np.array([.3, .59, .11])).sum(1, keepdims=True) return np.clip((L + (a - L) * 1.25) * .7, 0, 1) if kind == "torn": t = (S.fbm(q * 9, 3, seed) > .6)[:, None] * .85 # dark rips (3D keeps the surface; the pixel pass cuts holes) return a * (1 - t) if kind == "broken": n = np.abs(S.noise3(q * 14, seed) - .5) t = (n < .03)[:, None] * .8 return a * (1 - t) return a return f SURFACE = {"rusty": (.15, .9), "mossy": (.05, .95), "moldy": (.05, .9), "dirty": (.08, .9), "burnt": (.05, .95), "weathered": (.06, .9), "withered": (.05, .9), "frosty": (.4, .4), "wet": (.7, .12), "torn": (None, None), "broken": (None, None)} def _wrap_tex(pt, kind, seed): old = pt.tex mix = _mixer(kind, seed) def tex(q, base, s, _old=old): from . import texture return mix(q, texture(_old, q, base, s)) pt.tex = tex sp, ro = SURFACE[kind] if sp is not None: pt.spec, pt.rough = sp, ro def _warp(pt, kind, seed): f0 = pt.fn if kind == "twisted": amp, lean = .09, .06 * (1 if seed % 2 else -1) def fn(p, _f=f0): q = p.copy() y = np.clip(q[:, 1], 0, None) q[:, 0] = q[:, 0] - (amp * np.sin(y * 6.9 + seed % 7) * y ** .7 + lean * y) return _f(q.astype(np.float32)) * .7 else: k = 1.3 if kind == "stout" else .75 def fn(p, _f=f0): q = p.copy() q[:, 0] = q[:, 0] / k q[:, 2] = q[:, 2] / k return _f(q.astype(np.float32)) * min(k, 1.0) pt.fn = fn old = pt.tex if callable(old): if kind == "twisted": def tex(q, base, s, _t=old): q2 = q.copy() y = np.clip(q2[:, 1], 0, None) q2[:, 0] = q2[:, 0] - (amp * np.sin(y * 6.9 + seed % 7) * y ** .7 + lean * y) return _t(q2, base, s) else: def tex(q, base, s, _t=old): q2 = q.copy() q2[:, 0] /= k q2[:, 2] /= k return _t(q2, base, s) pt.tex = tex def _branches(seed, bark_col): """a bare crown: limbs forking from the trunk top, thinner each fork (rule: same as the pixel draw_tree skeleton)""" from .models import Part rs = np.random.RandomState(seed) segs = [] def limb(a, d, ln, r, depth): b = a + d * ln segs.append((a, b, r)) if depth: for _ in range(2): n = d + rs.uniform(-.6, .6, 3) * np.array([1, .4, 1]) n[1] = abs(n[1]) + .25 limb(b, n / np.linalg.norm(n), ln * .7, r * .65, depth - 1) top = np.array([0, .46, 0.]) for ang in np.linspace(0, 2 * np.pi, 4, endpoint=False) + rs.uniform(0, 1): d = np.array([np.cos(ang) * .9, 1, np.sin(ang) * .9]) limb(top, d / np.linalg.norm(d), .24, .022, 3) segs = [(a.astype(np.float32), b.astype(np.float32), r) for a, b, r in segs] def fn(p): d = None for a, b, r in segs: x = S.capsule(p, tuple(a), tuple(b), r) d = x if d is None else np.minimum(d, x) return d return Part(fn, bark_col, spec=.05, rough=.9, tex="bark") def apply(parts, e, trace, seed=0): from pydraw.language import on at = e.get("attrs") or {} conds = list(at.get("cond") or []) if not on("cond3d") or not parts: return parts leafy = [j for j, pt in enumerate(parts) if pt.tex == "leaves"] if leafy and on("tree") and ("leaves" in at.get("absent", []) or "bare" in conds or "withered" in conds) \ and "branches" not in at.get("absent", []): # rule: a tree without leaves / a dead tree = its trunk + a bare branch skeleton in place of the crown bark = next((pt.color for pt in parts if pt.tex != "leaves"), (110, 75, 45)) parts = [pt for j, pt in enumerate(parts) if j not in leafy] + [_branches(seed + 3, bark)] conds = [c for c in conds if c not in ("bare", "withered")] trace.append("3d: %s without leaves -> bare branches" % e.get("concept")) if leafy and "twisted" in conds: for pt in parts: # rule: a gnarled tree also has a thicker, knotted trunk (every non-leaf part) if pt.tex != "leaves": f0 = pt.fn pt.fn = lambda p, _f=f0: _f(p) - .025 - .012 * S.noise3(p * 18) if not conds: return parts done = [] for c in conds: for j, pt in enumerate(parts): if c in SURFACE: _wrap_tex(pt, c, seed + j) elif c == "shiny": pt.spec, pt.rough = max(pt.spec, .85), min(pt.rough, .12) elif c == "glowing": pt.emit = max(getattr(pt, "emit", 0.0), .55) elif c in ("twisted", "stout", "thin"): _warp(pt, c, seed) else: break else: done.append(c) if done: trace.append("3d: conditions %s on %s" % ("+".join(done), e.get("concept"))) return parts