"""3D side of pydraw/attach.py: attached things find their anchor on the host's own SDF (night round 1, scene lane). flag the pole base stands on the highest surface of the host near the layout's anchor (the keep, the lantern room's roof, the roof ridge); "hoist" = the pole's top at the mast top (the pole overlaps the mast) front a mounted sign board sits flat on the host's front face (first surface hit along +z in host space) hang a cord part from the thing's top up to the limb / canopy; a limb host is rebuilt as a horizontal bough and the pair is moved to a sensible depth (rays through the camera are kept, so the picture's 2D layout holds) Called from interact.place (inst.attach set by the 2D layout). Knob: PYDRAW_EXP attach=0 (no inst.attach is set).""" import math import numpy as np from . import sdf as S from .models import Part, V CAM = 1.0 # plot3d's camera height (the layout's pinhole is back-projected with cam = 1.0) def _union_d(parts, q): d = None for pt in parts: e = pt.fn(q) d = e if d is None else np.minimum(d, e) return d def _local(obj, P): return S.rot_y((np.asarray(P, float) - obj.base)[None, :] / obj.scale, -obj.yaw)[0] def _world(obj, a): q = np.asarray(a, float)[None, :] * obj.scale return S.rot_y(q, obj.yaw)[0] + obj.base def top_near(parts, lx, aspect, thin=False): """highest surface point of `parts` (model space) in columns around x = lx; thin = look for a mast (fine grid)""" xs = lx + np.linspace(-.05, .05, 5) * max(1.0, aspect) if not thin else lx + np.linspace(-.04, .04, 9) * max(1.0, aspect) zs = np.linspace(-.7, .7, 15) * max(1.0, aspect) if not thin else np.linspace(-.12, .12, 13) ys = np.linspace(2.2, 0.0, 221) X, Z, Y = np.meshgrid(xs, zs, ys, indexing="ij") q = np.stack([X.ravel(), Y.ravel(), Z.ravel()], -1).astype(np.float32) d = _union_d(parts, q).reshape(X.shape) hit = d < .006 best = None for i in range(len(xs)): for j in range(len(zs)): k = np.nonzero(hit[i, j])[0] if not len(k): continue y = ys[k[0]] score = y - .3 * abs(xs[i] - lx) if best is None or score > best[0]: best = (score, (float(xs[i]), float(y), float(zs[j]))) return None if best is None else best[1] def front_at(parts, lx, ly, aspect): """first surface hit along +z (toward the back) at model (lx, ly): the host's front face""" zs = np.linspace(-1.6 * max(1.0, aspect), 1.6 * max(1.0, aspect), 321) q = np.stack([np.full_like(zs, lx), np.full_like(zs, ly), zs], -1).astype(np.float32) d = _union_d(parts, q) k = np.nonzero(d < .006)[0] return float(zs[k[0]]) if len(k) else None def pole_x(it): """the flag model's pole x in its own model space""" from pydraw.registry import CONCEPTS c = CONCEPTS.get(it.concept, {}) if (c.get("sim") or {}).get("kind") == "flag": return -float(c.get("aspect", .5)) / 2 + .02 if getattr(it, "pole_free", False) or getattr(it, "attach_kind", None) != "flag": return 0.0 return -.42 def place(it, obj, host, trace): mode, anchor = it.attach from pydraw.registry import CONCEPTS hasp = float(CONCEPTS.get(host.name, {}).get("aspect", 1.0)) if host.name in CONCEPTS else 1.0 if mode in ("top", "hoist"): lx = anchor[0] * hasp p = top_near(host.parts, lx, hasp, thin=(mode == "hoist")) if p is None: return w = _world(host, p) off = S.rot_y(np.array([[pole_x(it) * obj.scale, 0.0, 0.0]]), obj.yaw)[0] base = w - off if mode == "hoist": base[1] -= .97 * obj.scale # rule: the flag is hoisted to the masthead, its pole along the mast else: base[1] -= .01 * obj.scale obj.base = base obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) trace.append("3d: %s flies from the %s (%s at y=%.2f)" % (it.concept.split(":")[0], host.name, mode, w[1])) elif mode == "front" and len(anchor) > 2: # a mounted object (clock, window, door...): its centre on the host's front face, its back touching it ly = anchor[1] zf = front_at(host.parts, 0.0, ly, hasp) if zf is None: return obj.yaw = host.yaw oasp = float(CONCEPTS.get(it.concept, {}).get("aspect", 1.0)) zs = np.linspace(1.2 * max(1.0, oasp), -1.2 * max(1.0, oasp), 241) q = np.stack([np.zeros_like(zs), np.full_like(zs, .5), zs], -1).astype(np.float32) k_ = np.nonzero(_union_d(obj.parts, q) < .006)[0] zb = float(zs[k_[0]]) if len(k_) else .1 # the object's back face (model units) c = _world(host, (0.0, ly, zf - .01)) off = S.rot_y(np.array([[0.0, 0.0, zb * obj.scale]]), obj.yaw)[0] obj.base = c - off - np.array([0, .5 * obj.scale, 0]) obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) trace.append("3d: %s mounted on the %s's front (z=%.2f)" % (it.concept, host.name, zf)) elif mode == "inside": # rule: a miniature in a bottle / jar stands just behind the front glass (inside the SDF it would be hidden by # the container's own surface): moved along its camera ray to the container's front face zf = front_at(host.parts, 0.0, .3, hasp) if zf is None: return wf = _world(host, (0.0, .3, zf)) Zo = obj.base[2] Zc = max(Zo * .5, wf[2] - .02 * host.scale) s_ = Zc / max(1e-6, Zo) O = np.array([0.0, CAM, 0.0]) obj.base = O + (obj.base - O) * s_ obj.scale *= s_ obj.radius *= s_ obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) trace.append("3d: %s inside the %s, behind its front glass" % (it.concept, host.name)) elif mode == "climb": # rule: a climber is on the trunk's front, not inside it: moved along its ray to the trunk's front face zf = front_at(host.parts, 0.0, anchor[1], hasp) if zf is not None: wf = _world(host, (0.0, anchor[1], zf)) Zo = obj.base[2] Zc = max(Zo * .5, wf[2] - .12 * obj.scale) s_ = Zc / max(1e-6, Zo) O = np.array([0.0, CAM, 0.0]) obj.base = O + (obj.base - O) * s_ obj.scale *= s_ obj.radius *= s_ obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) trace.append("3d: %s climbs the front of the %s" % (it.concept, host.name)) elif mode == "intree" and getattr(it, "hollow", None) is not None: # rule (language shift 3): an animal in a tree hole sits IN the trunk's front face: half sunk into the bark, # a dark hollow ring round it (the hole), no branch Zo = obj.base[2] Zc = max(Zo * .45, host.base[2] - .075 * host.scale) s = Zc / max(1e-6, Zo) O = np.array([0.0, CAM, 0.0]) obj.base = O + (obj.base - O) * s obj.base[0] = host.base[0] obj.scale *= s obj.radius *= s obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) oasp = float(CONCEPTS.get(it.concept, {}).get("aspect", 1.0)) obj.parts = list(obj.parts) + [ Part(lambda p, ra=(.62 * max(1.0, oasp), .68, .55): S.ellipsoid(p, np.array([0.0, .5, .5], np.float32), np.array(ra, np.float32)), (26, 18, 14), rough=1.0, spec=0.0), Part(lambda p: S.torus(np.stack([p[:, 0], p[:, 2] - .02, p[:, 1] - .5], -1) * np.array([1, 1, 1.1], np.float32), np.array([0.0, 0.0, 0.0], np.float32), .66 * max(1.0, oasp), .09), (92, 66, 44), rough=.9, tex="wood")] for a_ in ("pbox", "box"): if hasattr(obj, a_): delattr(obj, a_) trace.append("3d: %s looks out of a hollow in the %s" % (it.concept, host.name)) elif mode == "intree": # rule: an animal in a tree sits in front of the leaves on a branch that runs back into the crown Zo = obj.base[2] Zc = max(Zo * .45, Zo - .5 * host.scale * hasp) s = Zc / max(1e-6, Zo) O = np.array([0.0, CAM, 0.0]) obj.base = O + (obj.base - O) * s obj.scale *= s obj.radius *= s obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) oasp = float(CONCEPTS.get(it.concept, {}).get("aspect", 1.0)) trunk = np.array([host.base[0], obj.base[1] - .02 * obj.scale, host.base[2]]) lt = _local(obj, trunk) sx = 1.0 if anchor[0] > 0 else -1.0 obj.parts = list(obj.parts) + [Part(lambda p, a=(sx * .45 * oasp, -.03, 0.0), c=tuple(float(v) for v in lt): S.capsule(p, a, c, .07), (96, 70, 48), rough=.9, tex="wood")] obj.radius = max(obj.radius, float(np.linalg.norm(trunk - obj.center)) + .1 * obj.scale) for a_ in ("pbox", "box"): if hasattr(obj, a_): delattr(obj, a_) trace.append("3d: %s on a branch in the %s" % (it.concept, host.name)) elif mode == "front": ly = anchor[1] zf = front_at(host.parts, 0.0, ly, hasp) if zf is None: return obj.yaw = host.yaw # the sign board (model y .41-.99, centre .7, back face at z = +.04) lies on the front face k = obj.scale / host.scale c = _world(host, (0.0, ly, zf - .045 * k)) obj.base = c - np.array([0, .7 * obj.scale, 0]) obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) trace.append("3d: %s mounted on the %s's front (z=%.2f)" % (it.concept, host.name, zf)) elif mode == "hang": W, H, y0 = it.view f = float(W) if getattr(it.bound, "perch", False): # rule: a limb + what hangs from it sit at a natural depth (as if over the near ground), in the air Zt = f * CAM / max(4.0, H * .9 - y0) Zo = obj.base[2] s = Zt / max(1e-6, Zo) O = np.array([0.0, CAM, 0.0]) obj.base = O + (obj.base - O) * s obj.scale *= s obj.radius *= s obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) limb3d(it.bound, host, Zt) Zc = Zt else: # rule: under a tree the hanging thing comes forward out of the crown (half the crown's depth) Zo = obj.base[2] Zc = max(Zo * .45, Zo - .55 * host.scale * hasp) s = Zc / Zo O = np.array([0.0, CAM, 0.0]) obj.base = O + (obj.base - O) * s obj.scale *= s obj.radius *= s obj.center = obj.base + np.array([0, .5 * obj.scale, 0]) if not getattr(it, "cord", None): trace.append("3d: %s hangs from the %s" % (it.concept, host.name)) return cx, cy = it.cord[0], it.cord[1] top = np.array([(cx - W / 2) * Zc / f, CAM - (cy - y0) * Zc / f, Zc]) lt = _local(obj, top) r = max(.012, .02 / max(.2, obj.scale / max(1e-6, host.scale))) obj.parts = list(obj.parts) + [Part(lambda p, a=(0.0, .96, 0.0), c=tuple(float(v) for v in lt): S.capsule(p, a, c, min(r, .03)), (55, 48, 42), rough=.6, spec=.3)] # the bounding sphere must reach the cord's top obj.radius = max(obj.radius, float(np.linalg.norm(top - obj.center)) + .05 * obj.scale) for a_ in ("pbox", "box"): if hasattr(obj, a_): delattr(obj, a_) trace.append("3d: %s hangs on a cord from the %s" % (it.concept, host.name)) def crack_parts(obj, trace=None): """rule (surface state "cracked"): every part's texture gets a crack network - dark fissures along Worley cell borders (two scales: wide main cracks + a finer second set), with a lighter lip beside each crack""" from . import texture as _texture for pt in obj.parts: def tex(q, base, seed, _t=pt.tex): c = _texture(_t, q, base, seed) qq = q + .06 * S.noise3(q * 6, seed + 7)[:, None] f1, f2, _ = S.worley(qq * 4.5, seed + 11) g1, g2, _ = S.worley(qq * 11, seed + 12) # (a full Voronoi network reads as tiles or a turtle shell: cracks are broken runs of it) mask = np.clip((S.fbm(q * 2.2, 3, seed + 3) - .47) * 12, 0, 1) main = (1 - np.clip((f2 - f1) / .04, 0, 1)) * mask fine = (1 - np.clip((g2 - g1) / .025, 0, 1)) * np.clip((S.fbm(q * 3, 2, seed + 13) - .58) * 12, 0, 1) crack = np.maximum(main, .8 * fine) lip = np.clip((f2 - f1 - .045) / .03, 0, 1) * (1 - np.clip((f2 - f1 - .075) / .03, 0, 1)) * mask return c * (1 - .8 * crack[:, None]) + c * .25 * lip[:, None] pt.tex = tex if trace is not None: trace.append("3d: %s drawn cracked (Worley crack network on every part)" % obj.name) def limb3d(b, host, Z=None): """rebuild a perch host (2D: a limb reaching in from the frame edge, pydraw/perch.py) as a 3D bough at depth Z""" W, H, y0 = getattr(b, "view", (None, None, None)) if W is None: return f = float(W) Z = Z or float(host.base[2]) host.base = np.array([(b.x - W / 2) * Z / f, CAM - (b.y - y0) * Z / f, Z]) host.scale = b.h * Z / f host.yaw = 0.0 host.center = host.base + np.array([0, .5 * host.scale, 0]) Lw = b.w / max(1e-6, b.h) # the limb's box in host model units (height 1) side = 1 if b.perch_x1 > b.perch_x0 else -1 # the free end col = (105, 80, 58) x0, x1 = -side * Lw / 2, side * Lw / 2 host.parts = [Part(lambda p, a=(x0, .55, 0), c=(x1, .38, 0): S.capsule(p, a, c, .34), col, rough=.9, tex="wood"), Part(lambda p, a=(x0 + (x1 - x0) * .7, .45, 0), c=(x0 + (x1 - x0) * .7 + side * 1.1, 1.4, 0): S.capsule(p, a, c, .16), col, rough=.9, tex="wood")] host.radius = host.scale * (.3 + .5 * math.sqrt(1 + 2 * Lw ** 2)) for a_ in ("pbox", "box"): if hasattr(host, a_): delattr(host, a_)