"""Kitchen, food and sports models: bottle, wine glass, cutlery, bowl, banana, sandwich, broccoli, carrot, hot dog, pizza, donut, cake, frisbee, skis, snowboard, sports ball, baseball bat/glove, skateboard, surfboard, tennis racket. Same contract as models.py (base y=0, height 1, width = aspect, viewer on the -z side).""" import math import numpy as np from .models import model, Part, V, darker from . import sdf as S SILVER = (150, 158, 172) # ---------------- helpers ---------------- def lighter(c, k): return tuple(min(255, v + (255 - v) * k) for v in c) def poly2(x, y, pts): """exact signed distance to a closed 2D polygon (iq's sdPolygon), vectorised over points""" P = np.asarray(pts, float) d = (x - P[0, 0]) ** 2 + (y - P[0, 1]) ** 2 s = np.ones_like(x) n = len(P) for i in range(n): j = i - 1 ex, ey = P[j, 0] - P[i, 0], P[j, 1] - P[i, 1] wx, wy = x - P[i, 0], y - P[i, 1] h = np.clip((wx * ex + wy * ey) / (ex * ex + ey * ey + 1e-12), 0, 1) bx, by = wx - ex * h, wy - ey * h d = np.minimum(d, bx * bx + by * by) c1, c2, c3 = y >= P[i, 1], y < P[j, 1], ex * wy > ey * wx flip = (c1 & c2 & c3) | (~c1 & ~c2 & ~c3) s = np.where(flip, -s, s) return s * np.sqrt(d) def line2(x, y, pts): """unsigned distance to an open 2D polyline""" P = np.asarray(pts, float) d = np.full_like(x, 1e9) for i in range(len(P) - 1): ax, ay = P[i] ex, ey = P[i + 1, 0] - ax, P[i + 1, 1] - ay wx, wy = x - ax, y - ay h = np.clip((wx * ex + wy * ey) / (ex * ex + ey * ey + 1e-12), 0, 1) d = np.minimum(d, (wx - ex * h) ** 2 + (wy - ey * h) ** 2) return np.sqrt(d) def radial(p, c=(0, 0)): """(r, y) of a point around the vertical axis through (c[0], ?, c[1])""" return np.sqrt((p[:, 0] - c[0]) ** 2 + (p[:, 2] - c[1]) ** 2), p[:, 1] def revolve(p, pts, c=(0, 0)): r, y = radial(p, c) return poly2(r, y, pts) def extrude(d2, w, h): """extrude a 2D sdf along w with half-thickness h""" q0, q1 = d2, np.abs(w) - h return np.sqrt(np.maximum(q0, 0) ** 2 + np.maximum(q1, 0) ** 2) + np.minimum(np.maximum(q0, q1), 0) def rot_x(p, a): c, s = np.cos(a), np.sin(a) out = p.copy() out[:, 1] = c * p[:, 1] - s * p[:, 2] out[:, 2] = s * p[:, 1] + c * p[:, 2] return out def rot_z(p, a): c, s = np.cos(a), np.sin(a) out = p.copy() out[:, 0] = c * p[:, 0] - s * p[:, 1] out[:, 1] = s * p[:, 0] + c * p[:, 1] return out def frame(p, center, ang=0.0, tilt=0.0): """object frame for flat things standing in the view plane: translate to centre, lean the top back by `tilt` (about x), then rotate in the picture plane by `ang` so u runs along the object's length""" q = p - V(*center) q = rot_x(q, -tilt) return rot_z(q, -ang) def face_camera(parts, inst, aspect, keep=0.0): """rule: flat things (cutlery, boards) read only face-on; cancel the renderer's 3/4 turn (keep a little)""" left = getattr(inst, "facing", None) == "left" y0 = (math.pi if left else 0.0) + ((-.55 if left else .55) if aspect > 1.05 else 0.0) a = y0 - (keep if not left else -keep) for pt in parts: pt.fn = (lambda f: (lambda p: f(S.rot_y(p, a))))(pt.fn) return parts def ring(n, r, y, phase=0.0): return [(r * math.cos(phase + 2 * math.pi * i / n), y, r * math.sin(phase + 2 * math.pi * i / n)) for i in range(n)] def named(col, inst): """the prompt's colour only when the prompt actually names one (the renderer passes the concept default otherwise)""" if inst is None: return col return col if getattr(inst, "color", None) and getattr(inst, "color_explicit", True) else None def union(ds): return np.minimum.reduce(ds) if len(ds) > 1 else ds[0] # ---------------- table things ---------------- @model("bottle", aspect=.3) def m_bottle(col, inst=None): col = named(col, inst) glass = col or (35, 105, 60) prof = [(0, 0), (.125, 0), (.14, .02), (.14, .55), (.13, .62), (.1, .7), (.06, .77), (.048, .82), (.048, .93), (.056, .935), (.056, .975), (.045, .985), (0, .985)] return [Part(lambda p: revolve(p, prof), glass, spec=1.0, rough=.05), Part(lambda p: S.cylinder(p, V(0, .33, 0), .1435, .13), (238, 228, 200), spec=.2, rough=.7), # label Part(lambda p: S.cylinder(p, V(0, .44, 0), .1445, .022), (170, 40, 45), spec=.4, rough=.4), Part(lambda p: S.cylinder(p, V(0, .91, 0), .053, .075, .01), (150, 30, 40), spec=.7, rough=.25)] # foil @model("wine glass", aspect=.45) def m_wine_glass(col, inst=None): col = named(col, inst) tint = col or (196, 218, 230) cup = [(.03, .46)] + [(.215 * math.sin(t), .66 - .2 * math.cos(t)) for t in np.linspace(.15, math.pi / 2, 24)] + \ [(.215 - .045 * ((y - .66) / .32) ** 2, y) for y in np.linspace(.7, .98, 12)] def glass(p): r, y = radial(p) d = np.maximum(line2(r, y, cup) - .008, .69 - y) d = np.minimum(d, np.maximum(line2(r, y, cup) - .008, y - .5)) d = np.minimum(d, S.cylinder(p, V(0, .25, 0), .016, .22)) # stem d = S.smin(d, S.cylinder(p, V(0, .012, 0), .17, .012, .008), .03) # foot return d wine = [(0, .45), (.035, .45)] + [(.225 * math.sin(t), .66 - .21 * math.cos(t)) for t in np.linspace(.15, math.pi / 2, 24)] + [(.224, .7), (0, .7)] return [Part(glass, tint, spec=1.0, rough=.04), Part(lambda p: revolve(p, wine), (125, 18, 40), spec=.9, rough=.08)] def _cutlery(inst, aspect, head2, handle2, thick, metal, handle_col=None, extra=()): L, ang, tilt, W = 1.62, math.atan2(.78, 1.38), .45, 1.35 def fr(p): return frame(p, (0, .5, 0), ang, tilt) def metal_fn(p): q = fr(p) d = head2(q[:, 0], q[:, 1] / W) if handle_col is None: d = np.minimum(d, handle2(q[:, 0], q[:, 1] / W)) return extrude(d + .006, q[:, 2], thick - .006) - .006 parts = [Part(metal_fn, metal, spec=1.0, rough=.12)] if handle_col is not None: parts.append(Part(lambda p: (lambda q: extrude(handle2(q[:, 0], q[:, 1] / W) + .02, q[:, 2], thick * 1.8 - .02) - .02)(fr(p)), handle_col, spec=.6, rough=.25)) for fn, c in extra: parts.append(Part((lambda f: lambda p: f(fr(p)))(fn), c, spec=1.0, rough=.12)) return face_camera(parts, inst, aspect, keep=.1) @model("fork", aspect=1.6) def m_fork(col, inst=None): col = named(col, inst) metal = col or SILVER def head(x, y): neck = poly2(x, y, [(.12, -.035), (.36, -.09), (.47, -.1), (.5, 0), (.47, .1), (.36, .09), (.12, .035)]) d = neck for v in (-.075, -.025, .025, .075): d = np.minimum(d, S.rbox(np.stack([x, y, 0 * x], -1), V(.62, v, 0), (.17, .016, 1), .015)) return d def handle(x, y): return poly2(x, y, [(-.8, -.045), (-.75, -.065), (-.6, -.07), (.15, -.035), (.15, .035), (-.6, .07), (-.75, .065), (-.8, .045)]) return _cutlery(inst, 1.6, head, handle, .02, metal) @model("knife", aspect=1.7) def m_knife(col, inst=None): col = named(col, inst) metal = SILVER hcol = col if col and tuple(col) != SILVER else (35, 32, 30) def blade(x, y): # rule: a chef's knife: straight spine, belly curving up to a sharp tip spine = [(-.14, .085), (.5, .075), (.7, .05), (.9, -.005)] belly = [(.9, -.005)] + [(.9 - .45 * t, -.005 - .11 * math.sin(t * math.pi / 2) ** .7) for t in np.linspace(.1, 1, 8)] + [(-.14, -.115)] edge = spine + belly[1:] return poly2(x, y, edge) def handle(x, y): return poly2(x, y, [(-.82, -.06), (-.78, -.075), (-.18, -.07), (-.14, -.085), (-.14, .085), (-.18, .07), (-.78, .075), (-.82, .06)]) rivets = [((lambda q, xx=xx: S.cylinder(q[:, [0, 2, 1]], V(xx, 0, 0), .022, .045)), SILVER) for xx in (-.66, -.5, -.34)] return _cutlery(inst, 1.7, blade, handle, .018, metal, handle_col=hcol, extra=rivets) @model("spoon", aspect=1.5) def m_spoon(col, inst=None): col = named(col, inst) metal = col or SILVER def handle(x, y): return poly2(x, y, [(-.78, -.045), (-.72, -.07), (-.58, -.072), (.22, -.028), (.22, .028), (-.58, .072), (-.72, .07), (-.78, .045)]) L, ang, tilt = 1.62, math.atan2(.78, 1.38), .45 def body(p): q = frame(p, (0, .5, 0), ang, tilt) h = extrude(handle(q[:, 0], q[:, 1] / 1.35) + .006, q[:, 2], .014) - .006 c = V(.5, 0, 0) outer = S.ellipsoid(q, c, V(.29, .2, .085)) inner = S.ellipsoid(q, c + V(0, 0, -.045), V(.27, .18, .085)) bowl = np.maximum(np.maximum(outer, -inner), -q[:, 2] - .01) return S.smin(h, bowl * .9, .03) return face_camera([Part(body, metal, spec=1.0, rough=.12)], inst, 1.5, keep=.1) @model("bowl", aspect=1.9) def m_bowl(col, inst=None): col = named(col, inst) cer = col or (236, 234, 228) band = (60, 100, 175) if col is None or sum(col) > 600 else (245, 243, 238) curve = [(0, .1), (.38, .1)] + [(.38 + .57 * math.sin(t), .1 + .86 * (1 - math.cos(t))) for t in np.linspace(0, math.pi / 2, 33)[1:]] def body(p): r, y = radial(p) d = line2(r, y, curve) - .045 d = np.minimum(d, extrude(np.abs(r - .4) - .05, y - .05, .05)) # foot ring return d def bandf2(p): r, y = radial(p) # rule: the band only on the OUTER face (outside the profile curve) outside = r - np.interp(y, [c[1] for c in curve[2:]], [c[0] for c in curve[2:]]) return np.maximum(np.maximum(line2(r, y, curve) - .048, np.abs(y - .8) - .045), -outside - .02) return [Part(body, cer, spec=.8, rough=.15), Part(bandf2, band, spec=.8, rough=.15)] # ---------------- food ---------------- def _arc(p, cy, R, phi, rfun, zs=1.0): """tube along a circular arc in the xy plane (centre (0, cy), radius R, angles -phi..phi measured from the bottom); radius rfun(t) with t in -1..1. Returns (distance, t).""" qx, qy, qz = p[:, 0], p[:, 1] - cy, p[:, 2] / zs th = np.clip(np.arctan2(qx, -qy), -phi, phi) cx, cyy = R * np.sin(th), -R * np.cos(th) t = th / phi d = np.sqrt((qx - cx) ** 2 + (qy - cyy) ** 2 + qz ** 2) - rfun(t) return d * min(1.0, zs) * .85, t @model("banana", aspect=2.1) def m_banana(col, inst=None): col = named(col, inst) yel = col or (245, 210, 50) R, phi = .95, math.radians(64) cy = .3 + R rf = lambda t: .05 + .25 * np.sqrt(np.clip(1 - t * t, 0, 1)) ** 1.3 def body(p): d, t = _arc(p, cy, R, phi, rf, zs=.9) # rule: banana skin has soft ridges (a pentagonal section): shave five faces lightly return d tipL = (-R * math.sin(phi), cy - R * math.cos(phi)) tipR = (R * math.sin(phi), cy - R * math.cos(phi)) a = phi + .25 stem_end = (tipR[0] + .17 * math.cos(a), tipR[1] + .17 * math.sin(a)) return [Part(body, yel, spec=.45, rough=.35, tex="speckle"), Part(lambda p: S.capsule(p, (tipR[0] - .05, tipR[1] - .03, 0), (stem_end[0], stem_end[1], 0), .055), (120, 130, 50), k=.04, rough=.6), Part(lambda p: S.sphere(p, V(stem_end[0], stem_end[1], 0), .06), (70, 55, 35), rough=.8), Part(lambda p: S.sphere(p, V(tipL[0] - .02, tipL[1] - .01, 0), .055), (60, 45, 30), k=.03, rough=.8)] @model("carrot", aspect=3.0) def m_carrot(col, inst=None): col = named(col, inst) org = col or (240, 125, 30) a, b = np.array([-.47, .34, 0]), np.array([1.48, .2, 0]) def body(p): ba = b - a h = np.clip(((p - a) @ ba) / (ba @ ba), 0, 1) rad = .33 * (1 - h) ** .75 + .02 d = S.length(p - a - h[:, None] * ba) - rad # rule: carrots have shallow horizontal growth rings ring_ = .006 * np.sin(p[:, 0] * 30) ** 8 d = d + ring_ - .01 * (S.noise3(p * 9) - .5) d = S.smin(d, S.sphere(p, V(-.45, .34, 0), .3), .06) return d * .8 fronds = [(-1.3, 1.0, -.1), (-.95, 1.08, 0), (-1.38, .72, .05), (-.55, 1.02, -.12), (-1.15, .95, .14), (-.75, .9, .12)] def tops(p): d = 1e9 s0 = np.array([-.67, .42, 0]) for e in fronds: e = np.array(e) d = np.minimum(d, S.capsule(p, s0, e, .022)) # rule: carrot greens are feathery fronds: leaflets strung along each stem for t, rr in ((.35, .045), (.47, .055), (.59, .06), (.71, .065), (.83, .065), (.95, .055)): c = s0 + (e - s0) * t side = np.cross(e - s0, [0, 0, 1.0]); side = side / np.linalg.norm(side) * rr * .9 for sg in (-1, 1): d = np.minimum(d, S.capsule(p, c, c + sg * side + (e - s0) * .05, .022)) return d * .75 return face_camera([Part(body, org, spec=.35, rough=.5), Part(tops, (70, 150, 50), k=.03, spec=.1, rough=.85, tex="leaves")], inst, 3.0, keep=.3) @model("hot dog", aspect=3.0) def m_hotdog(col, inst=None): col = named(col, inst) bun = (210, 140, 70) def bunf(p): d = 1e9 for z in (-.24, .24): d = np.minimum(d, S.ellipsoid(p, V(0, .3, z), V(1.12, .28, .2))) d = S.smin(d, S.ellipsoid(p, V(0, .17, 0), V(1.1, .16, .36)), .08) return d sau = col or (195, 75, 45) def must(p): x = p[:, 0] zc = .12 * np.sin(x * 9) # rule: mustard zig-zag rides the top of the sausage d = np.sqrt((p[:, 2] - zc) ** 2 * .6 + (p[:, 1] - .68) ** 2) - .05 return np.maximum(d, np.abs(x) - 1.0) * .75 return [Part(bunf, bun, spec=.35, rough=.5, tex="pores"), Part(lambda p: S.capsule(p, (-1.32, .47, 0), (1.32, .47, 0), .21), sau, spec=.7, rough=.25), Part(must, (245, 200, 30), spec=.5, rough=.3)] @model("sandwich", aspect=1.5) def m_sandwich(col, inst=None): col = named(col, inst) crumb, crust_c = (244, 222, 175), (185, 120, 55) # rule: a sandwich reads as one when cut diagonally: triangle halves showing the layered cut face (not a burger) tri = [(-.62, -.46), (.62, -.46), (0, .44)] # rule: each half stands on its cut edge, triangle face to the viewer, so the silhouette is the iconic wedge halves = [(V(-.15, 0, -.1), 0.0), (V(.42, 0, .35), -1.25)] def loc(p, h): q = S.rot_y(p - h[0], h[1]) return np.stack([q[:, 0], q[:, 2] + .47, q[:, 1] - .46], -1) def slab(q, y0, y1, grow=0.0, r=.04): d2 = poly2(q[:, 0], q[:, 2], tri) - grow return extrude(d2 + r, q[:, 1] - (y0 + y1) / 2, (y1 - y0) / 2 - r) - r def bread(q): return np.minimum(slab(q, 0, .3, 0, .06), slab(q, .64, .95, 0, .07)) def over(fn): return lambda p: union([fn(loc(p, h)) for h in halves]) def crust(q): return np.maximum(np.maximum(bread(q) - .014, -.43 - q[:, 2]), -poly2(q[:, 0], q[:, 2], tri) - .07) wav = lambda q, f, a: a * np.sin(q[:, 0] * f + 1) * np.cos(q[:, 2] * f * 1.3) lett = lambda q: (slab(q, .28, .4, .07, .03) + wav(q, 12, .03)) * .8 tom = lambda q: slab(q, .38, .47, .03, .03) chs = lambda q: slab(q, .46, .52, .05, .015) ham = lambda q: (slab(q, .51, .64, .02, .04) + wav(q, 9, .015)) * .85 return face_camera([Part(over(bread), crumb, spec=.1, rough=.85, tex="pores"), Part(over(crust), crust_c, spec=.2, rough=.7, tex="pores"), Part(over(lett), (105, 180, 55), spec=.35, rough=.45), Part(over(tom), (215, 55, 45), spec=.6, rough=.3), Part(over(chs), (250, 200, 50), spec=.4, rough=.4), Part(over(ham), (232, 145, 145), spec=.3, rough=.5)], inst, 1.5, keep=.25) @model("broccoli", aspect=1.0) def m_broccoli(col, inst=None): col = named(col, inst) green = col or (45, 110, 40) stalk_c = (150, 180, 95) if col is None else lighter(green, .45) florets = [((0, .78, 0), .22), ((-.26, .66, 0), .18), ((.26, .66, 0), .18), ((-.12, .74, -.18), .17), ((.14, .72, -.17), .17), ((0, .66, .2), .17), ((-.18, .6, -.18), .14), ((.2, .58, -.12), .14), ((0, .6, -.24), .15)] def stalk(p): d = S.cone(p, V(0, 0, 0), .2, 1.6) * .8 d = np.maximum(d, p[:, 1] - .5) d = S.smin(d, S.cylinder(p, V(0, .2, 0), .14, .2, .03), .05) for (c, r) in florets: d = S.smin(d, S.capsule(p, (0, .3, 0), (c[0] * .8, c[1] - .1, c[2] * .8), .07), .05) return d def crown(p): d = 1e9 for c, r in florets: d = S.smin(d, S.sphere(p, V(*c), r), .015) # rule: broccoli heads are a cluster of tiny bud bumps return (d - .03 * S.noise3(p * 18) - .015 * S.noise3(p * 45, 3)) * .72 return [Part(stalk, stalk_c, spec=.3, rough=.5), Part(crown, green, k=.03, spec=.15, rough=.75, tex="speckle")] def _tilted(p, c, tilt): """frame for a horizontal disc centred at c whose far (+z) side is raised by `tilt` radians toward the camera""" return rot_x(p - V(*c), tilt) @model("pizza", aspect=3.5) def m_pizza(col, inst=None): col = named(col, inst) R, tilt = 1.62, math.radians(13) c = (0, R * math.sin(tilt) + .1, 0) fr = lambda p: _tilted(p, c, tilt) cheese_c = (250, 205, 95) if col is None else col def crust(p): q = fr(p) d = S.cylinder(q, V(0, 0, 0), R - .05, .05, .03) return np.minimum(d, S.torus(q, V(0, .02, 0), R - .1, .11)) def sauce(p): q = fr(p) return S.cylinder(q, V(0, .055, 0), R - .17, .02, .01) def cheese(p): q = fr(p) r = np.sqrt(q[:, 0] ** 2 + q[:, 2] ** 2) # rule: melted cheese is blotchy so sauce peeks through near the crust h = .02 + .03 * S.noise3(q * 3.5) - .04 * np.clip((r - 1.2) / .3, 0, 1) return (np.maximum(q[:, 1] - .06 - h, np.maximum(r - (R - .2), -q[:, 1])) * .8) pep = [(0, 0)] + [(.62 * math.cos(a), .62 * math.sin(a)) for a in np.linspace(0, 2 * math.pi, 6)[:-1] + .3] + \ [(1.13 * math.cos(a), 1.13 * math.sin(a)) for a in np.linspace(0, 2 * math.pi, 9)[:-1]] def pepperoni(p): q = fr(p) d = 1e9 for x, z in pep: d = np.minimum(d, S.cylinder(q, V(x, .11, z), .2, .025, .015)) return d leaves = [(.35, .3), (-.4, -.25), (-.75, .55), (.85, -.5), (.2, -.9), (-.2, .95)] def basil(p): q = fr(p) return union([S.ellipsoid(S.rot_y(q - V(x, .12, z), x * 3), V(0, 0, 0), V(.12, .02, .07)) for x, z in leaves]) parts = [Part(crust, (215, 150, 75), spec=.3, rough=.6, tex="pores"), Part(sauce, (200, 50, 30), spec=.6, rough=.3), Part(cheese, cheese_c, spec=.5, rough=.35), Part(pepperoni, (175, 45, 35), spec=.6, rough=.3), Part(basil, (50, 125, 45), spec=.3, rough=.5)] return face_camera(parts, inst, 3.5) @model("donut", "doughnut", aspect=1.7) def m_donut(col, inst=None): col = named(col, inst) icing = col or (242, 140, 180) tilt = math.radians(24) Rm, r0 = .52, .3 c = (0, (Rm + r0) * math.sin(tilt) + r0 * math.cos(tilt), 0) fr = lambda p: _tilted(p, c, tilt) dough = lambda p: S.torus(fr(p), V(0, 0, 0), Rm, r0) def frost(p): q = fr(p) ang = np.arctan2(q[:, 2], q[:, 0]) rr = np.sqrt(q[:, 0] ** 2 + q[:, 2] ** 2) - Rm a2 = np.arctan2(q[:, 1], rr) # rule: icing covers the top and drips over the edge in soft waves lim = .05 + .12 * np.sin(ang * 7) + .08 * np.sin(ang * 13 + 1) d = S.torus(q, V(0, 0, 0), Rm, r0 + .035) return np.maximum(d, (lim - q[:, 1]) * .6) if False else np.maximum(d, -(a2 - (-.5 + lim * 2)) * .3) rng = np.random.RandomState(4) cols = [(250, 250, 245), (90, 170, 230), (250, 220, 60), (120, 200, 110)] spr = {k: [] for k in range(4)} for i in range(44): th, ph = rng.uniform(0, 2 * math.pi), rng.uniform(.25, 1.25) cc = np.array([(Rm + (r0 + .035) * math.cos(ph)) * math.cos(th), (r0 + .035) * math.sin(ph), (Rm + (r0 + .035) * math.cos(ph)) * math.sin(th)]) dirv = np.array([math.cos(rng.uniform(0, 6.3)), 0, math.sin(rng.uniform(0, 6.3))]) * .045 spr[i % 4].append((cc - dirv, cc + dirv)) parts = [Part(dough, (215, 155, 85), spec=.3, rough=.55, tex="pores"), Part(frost, icing, spec=.7, rough=.2)] for k in range(4): segs = spr[k] parts.append(Part((lambda sg: lambda p: (lambda q: union([S.capsule(q, a, b, .018) for a, b in sg]))(fr(p)))(segs), cols[k], spec=.6, rough=.25)) return face_camera(parts, inst, 1.7) @model("cake", aspect=1.3) def m_cake(col, inst=None): col = named(col, inst) frost = col or (250, 245, 240) drip_c = (235, 120, 160) if col is None or sum(col) > 600 else lighter(col, .55) H, Rc = .58, .6 def body(p): d = S.cylinder(p, V(0, .05 + H / 2, 0), Rc, H / 2, .05) return d def drip(p): r, y = radial(p) ang = np.arctan2(p[:, 2], p[:, 0]) L = .06 + .16 * np.clip(np.sin(ang * 9), 0, 1) ** 3 + .06 * np.clip(np.sin(ang * 9 + 2.5), 0, 1) ** 4 top = S.cylinder(p, V(0, .05 + H / 2 + .01, 0), Rc + .02, H / 2, .06) return np.maximum(top, (.05 + H - L - y) * .5) beads = ring(16, Rc - .03, .05 + H + .03) def border(p): return union([S.sphere(p, V(*b), .055) for b in beads] + [S.torus(p, V(0, .05, 0), Rc + .01, .05)]) plate = lambda p: S.cylinder(p, V(0, .025, 0), Rc + .14, .025, .015) berries = ring(5, .32, .05 + H + .07, .4) def straw(p): return union([S.ellipsoid(p, V(*b), V(.07, .085, .07)) for b in berries]) cand = [(-.18, .05 + H, -.05), (.0, .05 + H, .08), (.18, .05 + H, -.05)] candles = lambda p: union([S.cylinder(p, V(x, y + .12, z), .025, .12, .01) for x, y, z in cand]) flames = lambda p: union([S.ellipsoid(p, V(x, y + .3, z), V(.03, .055, .03)) for x, y, z in cand]) return [Part(plate, (235, 235, 240), spec=.8, rough=.15), Part(body, frost, spec=.35, rough=.45), Part(drip, drip_c, spec=.7, rough=.2), Part(border, lighter(drip_c, .0) if False else frost, spec=.35, rough=.45), Part(straw, (215, 35, 50), spec=.8, rough=.2, tex="speckle"), Part(candles, (120, 180, 240), spec=.5, rough=.3), Part(flames, (255, 200, 80), spec=1.0, rough=.05, emit=.9)] @model("frisbee", aspect=1.6) def m_frisbee(col, inst=None): col = named(col, inst) plast = col or (240, 120, 40) tilt, R = math.radians(20), .72 c = (0, .6, 0) # rule: a frisbee floats (in flight) with its shadow on the ground below prof = [(0, .14)] + [(R * .97 * t, .14 - .09 * t * t) for t in np.linspace(.1, 1, 14)] + \ [(R + .02 * math.sin(a), .05 - .04 * (1 - math.cos(a)) - .04 * math.sin(a) * 0) for a in np.linspace(.3, math.pi / 2, 4)] + [(R, -.03), (R - .07, -.04)] def disc(p): q = _tilted(p, c, tilt) r, y = radial(q) return line2(r, y, prof) - .022 def rings_(p): q = _tilted(p, c, tilt) r, y = radial(q) d = line2(r, y, prof) - .03 return np.maximum(d, np.minimum(np.abs(r - .5) - .02, np.abs(r - .62) - .02)) return face_camera([Part(disc, plast, spec=.8, rough=.18), Part(rings_, darker(plast, .8), spec=.8, rough=.18)], inst, 1.6) # ---------------- sports ---------------- _PHI = (1 + 5 ** .5) / 2 _ICO = np.array([v for a, b in ((1, _PHI), (-1, _PHI), (1, -_PHI), (-1, -_PHI)) for v in ((0, a, b), (a, b, 0), (b, 0, a))], float) _ICO /= np.linalg.norm(_ICO, axis=1, keepdims=True) _DOD = np.array([v for v in [(x, y, z) for x in (-1, 1) for y in (-1, 1) for z in (-1, 1)] + [(0, s * _PHI, t / _PHI) for s in (-1, 1) for t in (-1, 1)] + [(s * _PHI, t / _PHI, 0) for s in (-1, 1) for t in (-1, 1)] + [(s / _PHI, 0, t * _PHI) for s in (-1, 1) for t in (-1, 1)]], float) _DOD /= np.linalg.norm(_DOD, axis=1, keepdims=True) @model("ball", "sports ball", "soccer ball", aspect=1.0) def m_ball(col, inst=None): col = named(col, inst) word = (getattr(inst, "word", "") or "").lower() kind = "basket" if "basket" in word else "tennis" if "tennis" in word else "baseball" if "baseball" in word else \ "beach" if "beach" in word else "soccer" k = getattr(inst, "k", 0) or 0 R, C = .5, V(0, .5, 0) a1, a2 = .45 + k * 1.1, .55 + k * .7 def unit(p): q = rot_x(S.rot_y(p - C, a1), a2) return q / np.maximum(S.length(q), 1e-6)[:, None] shell = lambda p: S.sphere(p, C, R) decal = lambda p, region: np.maximum(S.sphere(p, C, R + .004), region) if kind == "soccer": body = col or (246, 246, 244) ink = (28, 26, 30) if sum(body) > 150 else (240, 240, 240) def scores(p): n = unit(p) sp = (n @ _ICO.T) / 2.3274 sh = (n @ _DOD.T) / 2.2673 return sp.max(-1), sh.max(-1) def patch(p): sp, sh = scores(p) # rule: a soccer ball = truncated icosahedron: black pentagons where the pentagon face plane is nearest return decal(p, (sh - sp) * 6.0) def seam(p): n = unit(p) allf = np.concatenate([(n @ _ICO.T) / 2.3274, (n @ _DOD.T) / 2.2673], -1) s2 = np.sort(allf, -1) return decal(p, (s2[:, -1] - s2[:, -2]) * 9.0 - .012) return [Part(shell, body, spec=.6, rough=.3), Part(patch, ink, spec=.6, rough=.3), Part(seam, darker(body, .55), spec=.3, rough=.5)] if kind == "basket": body = col or (222, 110, 40) def seams(p): n = unit(p) d = np.minimum(np.minimum(np.abs(n[:, 0]), np.abs(n[:, 1])), np.abs(np.sqrt(n[:, 1] ** 2 + n[:, 2] ** 2) - .72)) return decal(p, d * R - .012) return [Part(shell, body, spec=.3, rough=.6, tex="pores"), Part(seams, (30, 25, 25), spec=.3, rough=.6)] if kind in ("tennis", "baseball"): body = col or ((205, 230, 70) if kind == "tennis" else (244, 240, 230)) line_c = (250, 250, 245) if kind == "tennis" else (205, 35, 35) def seam(p): n = unit(p) d = np.abs(np.abs(n[:, 0]) - .45 - .35 * n[:, 1] ** 2) return decal(p, d * R * .8 - .014) return [Part(shell, body, spec=.15 if kind == "tennis" else .5, rough=.85 if kind == "tennis" else .35, tex="fur" if kind == "tennis" else None), Part(seam, line_c, spec=.3, rough=.6)] # beach ball: coloured gores gcols = [(225, 45, 45), (40, 110, 220), (250, 210, 40), (40, 170, 80), (250, 250, 248), (240, 120, 30)] parts = [Part(shell, col or (250, 250, 248), spec=.8, rough=.15)] # rule: a named colour owns the ball; white only at the caps for g in range(3): def gore(p, g=g): n = unit(p) a = (np.arctan2(n[:, 2], n[:, 0]) / (2 * math.pi) * 6 - g * 2) % 6 reg = np.maximum(np.abs(a - .5) - .5, 0) * .5 - (np.abs(a - .5) < .5) * .05 + .0 return decal(p, np.maximum(reg, np.abs(n[:, 1]) - .88)) parts.append(Part(gore, darker(col, .85) if col else gcols[g], spec=.8, rough=.15)) return parts def _baseball(c, r, k=0.0): """small baseball: white sphere + red stitched seam decal""" C = V(*c) def unit(p): q = rot_x(S.rot_y(p - C, .7 + k), .5) return q / np.maximum(S.length(q), 1e-6)[:, None] def seam(p): n = unit(p) d = np.abs(np.abs(n[:, 0]) - .45 - .35 * n[:, 1] ** 2) return np.maximum(S.sphere(p, C, r + .002), d * r * .8 - r * .05) return [Part(lambda p: S.sphere(p, C, r), (244, 240, 230), spec=.4, rough=.4), Part(seam, (205, 35, 35), spec=.3, rough=.6)] @model("baseball bat", aspect=.55) def m_bat(col, inst=None): col = named(col, inst) wood = col or (205, 160, 105) base, top = np.array([-.1, .03, .02]), np.array([.16, .99, .02]) ax = top - base L = float(np.linalg.norm(ax)) ax = ax / L prof = [(0, 0), (.05, 0), (.05, .018), (.026, .04), (.024, .3), (.034, .5), (.055, .72), (.064, .86), (.064, .97), (.05, 1.0), (0, 1.0)] def bat(p): q = p - base t = q @ ax r = S.length(q - t[:, None] * ax[None, :]) return poly2(r, t / L, [(a, b) for a, b in prof]) * .95 def grip(p): q = p - base t = q @ ax r = S.length(q - t[:, None] * ax[None, :]) return np.maximum(poly2(r, t / L, prof) - .004, np.abs(t / L - .17) - .13) parts = [Part(bat, wood, spec=.6, rough=.3, tex="wood"), Part(grip, (35, 35, 40), spec=.3, rough=.6)] parts += _baseball((.12, .09, -.12), .09) return face_camera(parts, inst, .55) @model("baseball glove", aspect=.95) def m_glove(col, inst=None): col = named(col, inst) leather = col or (165, 95, 45) dark = darker(leather, .6) tips = [(-.27, .86), (-.11, .95), (.05, .94), (.2, .86)] def mitt(p): d = S.ellipsoid(p, V(-.02, .45, 0), V(.34, .36, .15)) for x, y in tips: d = S.smin(d, S.capsule(p, (x * .6, .5, .02), (x, y - .07, .02), .085), .06) d = S.smin(d, S.capsule(p, (.22, .3, .0), (.36, .58, -.03), .09), .07) # thumb # rule: the pocket is a dent facing the viewer d = np.maximum(d, -S.ellipsoid(p, V(-.02, .5, -.22), V(.24, .26, .13))) return d def web(p): return S.rbox(rot_z(p - V(.27, .68, -.02), -.35), V(0, 0, 0), (.09, .12, .03), .02) cuff = lambda p: S.rbox(p, V(-.02, .1, .01), (.25, .1, .13), .07) def laces(p): d = 1e9 for x, y in tips: d = np.minimum(d, S.torus(rot_x(p - V(x, y - .03, .02), math.pi / 2)[:, [0, 1, 2]], V(0, 0, 0), .07, .014)) return np.maximum(d, -(p[:, 1] - .55)) parts = [Part(mitt, leather, spec=.45, rough=.35), Part(web, dark, k=.03, spec=.4, rough=.4), Part(cuff, dark, k=.04, spec=.4, rough=.4), Part(laces, (110, 60, 30), spec=.4, rough=.5)] parts += _baseball((-.03, .47, -.12), .14, 1.0) return face_camera(parts, inst, .95) def _board_frame(p, tilt): return rot_x(p, -tilt) @model("surfboard", aspect=.28) def m_surfboard(col, inst=None): col = named(col, inst) paint = col or (245, 200, 60) def w(y): return np.where(y > .4, .135 * np.clip(1 - ((y - .4) / .6) ** 2, 0, 1) ** .55, .135 * np.clip(1 - ((y - .4) / .42) ** 2, 0, 1) ** .35) ys = np.linspace(0, 1, 30) ys[0], ys[-1] = .01, .995 outline = [(float(w(np.array([y]))[0]), y) for y in ys] outline = [(-x, y) for x, y in outline[::-1]] + outline outline = [(x, y) for x, y in outline] tilt = .14 def board(p): q = _board_frame(p, tilt) d2 = poly2(q[:, 0], q[:, 1], outline) return extrude(d2 + .02, q[:, 2], .042 - .02) - .02 def stripe(p): q = _board_frame(p, tilt) d2 = poly2(q[:, 0], q[:, 1], outline) b = extrude(d2 + .02, q[:, 2], .042 - .02) - .022 band = np.minimum(np.abs(q[:, 1] - .6 + .15 * q[:, 0]) - .035, np.abs(q[:, 1] - .68 + .15 * q[:, 0]) - .015) return np.maximum(b, band) def stringer(p): q = _board_frame(p, tilt) d2 = poly2(q[:, 0], q[:, 1], outline) b = extrude(d2 + .02, q[:, 2], .042 - .02) - .022 return np.maximum(b, np.abs(q[:, 0]) - .005) stripe_c = (220, 60, 50) if sum(paint) > 300 else (250, 250, 245) return face_camera([Part(board, paint, spec=.9, rough=.12), Part(stripe, stripe_c, spec=.9, rough=.12), Part(stringer, (120, 90, 60), spec=.6, rough=.3)], inst, .28) @model("snowboard", aspect=.3) def m_snowboard(col, inst=None): col = named(col, inst) paint = col or (60, 170, 200) tilt = .12 def d2(q): x, y = q[:, 0], q[:, 1] # rule: a snowboard is a long rounded slab, both ends round, slightly waisted (sidecut) hw = .14 - .018 * np.cos((y - .5) * math.pi * 1.6) box = S.rbox(np.stack([x / hw * .14, y, 0 * x], -1), V(0, .5, 0), (.14, .49, 1), .13) * .9 return box def board(p): q = _board_frame(p, tilt) return extrude(d2(q) + .01, q[:, 2], .016) - .01 def graphic(p): q = _board_frame(p, tilt) b = extrude(d2(q) + .01, q[:, 2], .016) - .012 band = np.abs(q[:, 1] - .5 - .9 * q[:, 0]) - .06 return np.maximum(b, band) def bind(p, yc, a): q = rot_z(_board_frame(p, tilt) - V(0, yc, 0), a) plate = S.rbox(q, V(0, 0, -.03), (.1, .045, .018), .012) high = S.rbox(q, V(0, -.045, -.06), (.085, .012, .05), .01) return np.minimum(plate, high) def straps(p, yc, a): q = rot_z(_board_frame(p, tilt) - V(0, yc, 0), a) return union([S.rbox(q, V(0, s, -.055), (.105, .014, .03), .01) for s in (.0, .035)]) gc = (250, 250, 245) if sum(paint) < 600 else (40, 60, 120) return face_camera([Part(board, paint, spec=.9, rough=.12), Part(graphic, gc, spec=.9, rough=.12), Part(lambda p: np.minimum(bind(p, .32, .25), bind(p, .68, -.25)), (30, 30, 35), spec=.4, rough=.4), Part(lambda p: np.minimum(straps(p, .32, .25), straps(p, .68, -.25)), (220, 50, 50), spec=.4, rough=.4)], inst, .3) @model("skis", "ski", aspect=.42) def m_skis(col, inst=None): col = named(col, inst) paint = col or (210, 45, 45) def ski_q(p, x0, fan): q = rot_z(_board_frame(p, .1) - V(x0, 0, 0), fan) # rule: ski tips curl up (toward the viewer) at the top bend = np.maximum(q[:, 1] - .84, 0) q[:, 2] = q[:, 2] + 4.0 * bend ** 2 return q def ski_d(q, e=0.0): x, y = q[:, 0], q[:, 1] hw = .052 + .01 * np.abs(y - .5) * 2 d = S.rbox(np.stack([x / hw * .052, y, q[:, 2]], -1), V(0, .5, 0), (.052, .495, .013 + e), .045) * .9 return d skis = [(-.1, .05), (.1, -.05)] def body(p): return union([ski_d(ski_q(p, x0, f)) * .85 for x0, f in skis]) def stripe(p): out = [] for x0, f in skis: q = ski_q(p, x0, f) # rule: skis carry bold cross graphics (chevrons + a tip band), never long stripes (they read as poles) chev = np.abs(q[:, 1] - .72 - 1.2 * np.abs(q[:, 0])) - .03 chev = np.minimum(chev, np.abs(q[:, 1] - .2 - 1.2 * np.abs(q[:, 0])) - .03) chev = np.minimum(chev, np.abs(q[:, 1] - .9) - .025) out.append(np.maximum(ski_d(q, .002), chev) * .85) return union(out) def binding(p): d = 1e9 for x0, f in skis: q = ski_q(p, x0, f) d = np.minimum(d, S.rbox(q, V(0, .53, -.03), (.052, .07, .03), .014)) d = np.minimum(d, S.rbox(q, V(0, .35, -.035), (.052, .05, .035), .014)) return d * .85 def poles(p): d = 1e9 for a, b in (((-.19, .02, -.1), (.13, .9, -.08)), ((.19, .02, -.1), (-.13, .9, -.08))): d = np.minimum(d, S.capsule(p, a, b, .011)) return d def grips(p): d = 1e9 for a, b in (((-.19, .02, -.1), (.13, .9, -.08)), ((.19, .02, -.1), (-.13, .9, -.08))): a, b = np.array(a), np.array(b) d = np.minimum(d, S.capsule(p, a + (b - a) * .9, b + (b - a) * .02, .022)) c = a + (b - a) * .09 d = np.minimum(d, S.torus(rot_x(p - c, 0), V(0, 0, 0), .04, .008)) return d return face_camera([Part(body, paint, spec=.9, rough=.12), Part(stripe, (250, 250, 245), spec=.9, rough=.12), Part(binding, (35, 35, 40), spec=.5, rough=.3)], inst, .42, keep=-.75) @model("skateboard", aspect=2.6) def m_skateboard(col, inst=None): col = named(col, inst) paint = col or (225, 90, 40) def deck_q(p): q = p.copy() # rule: nose and tail kick up kick = np.maximum(np.abs(q[:, 0]) - .8, 0) q[:, 1] = q[:, 1] - .9 * kick ** 2 / (1 + kick) return q def deck(p): q = deck_q(p) d2 = S.rbox(np.stack([q[:, 0], q[:, 2], 0 * q[:, 0]], -1), V(0, 0, 0), (1.25, .36, 1), .35) return extrude(d2 + .02, q[:, 1] - .3, .032) * .8 def top(p): q = deck_q(p) d2 = S.rbox(np.stack([q[:, 0], q[:, 2], 0 * q[:, 0]], -1), V(0, 0, 0), (1.25, .36, 1), .35) return np.maximum(extrude(d2 + .028, q[:, 1] - .3, .034) * .8, -(q[:, 1] - .326)) trucks = lambda p: union([np.minimum(S.rbox(p, V(x, .22, 0), (.08, .06, .14), .03), S.capsule(p, (x, .13, -.33), (x, .13, .33), .04)) for x in (-.7, .7)]) wheels = lambda p: union([S.cylinder(p[:, [0, 2, 1]], V(x, z, .13), .13, .055, .03) for x in (-.7, .7) for z in (-.32, .32)]) return [Part(deck, paint, spec=.7, rough=.25), Part(top, (40, 40, 44) if col is None else lighter(paint, .15), spec=.2 if col is None else .6, rough=.8 if col is None else .3), Part(trucks, (175, 180, 190), spec=1, rough=.15), Part(wheels, (245, 240, 225), spec=.5, rough=.3)] @model("tennis racket", aspect=.42) def m_racket(col, inst=None): col = named(col, inst) paint = col or (40, 120, 200) tilt = .2 C, A, B = (0, .68), .19, .27 def ell(q): x, y = (q[:, 0] - C[0]) / A, (q[:, 1] - C[1]) / B return (np.sqrt(x * x + y * y) - 1) * min(A, B) def frame_(p): q = _board_frame(p, tilt) d = extrude(np.abs(ell(q)) - .02, q[:, 2], .02) for s in (-1, 1): d = S.smin(d, S.capsule(q, (0, .38, 0), (s * .12, .47, 0), .02), .03) return d * .9 def strings(p): q = _board_frame(p, tilt) g = .045 gx = np.abs((q[:, 0] + g / 2) % g - g / 2) gy = np.abs((q[:, 1] - C[1] + g / 2) % g - g / 2) line = np.minimum(gx, gy) - .0045 return np.maximum(np.maximum(line, np.abs(q[:, 2]) - .004), ell(q) + .01) * .9 handle = lambda p: (lambda q: S.rbox(q, V(0, .21, 0), (.026, .19, .022), .012))(_board_frame(p, tilt)) grip = lambda p: (lambda q: S.rbox(q, V(0, .14, 0), (.029, .13, .025), .014))(_board_frame(p, tilt)) def tball(p): return S.sphere(p, V(.16, .065, -.12), .065) def tseam(p): n = (p - V(.16, .065, -.12)) n = rot_x(n, .6) / np.maximum(S.length(n), 1e-6)[:, None] d = np.abs(np.abs(n[:, 0]) - .45 - .35 * n[:, 1] ** 2) return np.maximum(S.sphere(p, V(.16, .065, -.12), .067), d * .05 - .005) return face_camera([Part(frame_, paint, spec=.9, rough=.15), Part(strings, (240, 240, 235), spec=.3, rough=.5), Part(handle, paint, spec=.9, rough=.15), Part(grip, (30, 30, 35), spec=.3, rough=.6), Part(tball, (205, 230, 70), spec=.15, rough=.85, tex="fur"), Part(tseam, (250, 250, 245), spec=.2, rough=.7)], inst, .42, keep=.25) @model("plate", "saucer", "dish", "dinner plate", aspect=2.6) def m_plate(col, inst=None): """night wave (critic R8): a plate lies FLAT (the generator family stood a disc on its edge like a coin): a wide shallow lathe profile, flat well, rising rim, a thin foot ring; glazed""" col = named(col, inst) cer = col or (242, 241, 236) rim = (70, 110, 170) if col is None else tuple(min(255, int(c * .8)) for c in cer) curve = [(0, .1), (.8, .1)] + [(.8 + .48 * math.sin(t), .1 + .2 * (1 - math.cos(t))) for t in np.linspace(0, math.pi / 2, 25)[1:]] def body(p): r, y = radial(p) d = line2(r, y, curve) - .06 return np.minimum(d, extrude(np.abs(r - .55) - .05, y - .03, .03)) def band(p): r, y = radial(p) return np.maximum(line2(r, y, curve) - .063, np.abs(r - 1.18) - .05) return [Part(body, cer, spec=.85, rough=.12), Part(band, rim, spec=.85, rough=.12)]