Spaces:
Running

pydraw / pydraw3d /interior.py
linoyts's picture
linoyts HF Staff
night wave 3 merge (master decb488): body emotions, shoulders, pouch, fetch, horseback, shelters, circus balls, tethers, 3D window views, pixel atmosphere
097fa87 verified
Raw History Blame Contribute Delete
16.8 kB
"""pydraw3d.interior: furnished rooms (pure code). A room used to be a bare floor + one painted wall + a white
window. Now: furniture models stand along the back wall (never in front of / behind a subject, never a prompted
concept), a patterned rug lies under the subjects, framed paintings hang on the wall, the window has curtains and
a view (sky, clouds, hills, a tree line), and some walls get wallpaper."""
import math
import numpy as np
from .world import vnoise, fbm2, hash2, smooth, lin
# per room kind: furniture (concept, real height m, distance of its centre from the wall m), in order of preference
ROOM_FURNITURE = {
"room": [("bookshelf", 1.8, .2), ("floor lamp", 1.6, .3), ("potted plant", .95, .35)],
"bedroom": [("floor lamp", 1.5, .3), ("potted plant", .9, .35), ("bookshelf", 1.6, .2)],
"office": [("bookshelf", 1.9, .2), ("potted plant", 1.0, .35)],
"library": [("bookshelf", 2.0, .2), ("bookshelf", 2.0, .2), ("floor lamp", 1.6, .3)],
"classroom": [("bookshelf", 1.6, .2), ("potted plant", .9, .35)],
"kitchen": [("potted plant", .7, .35)],
"bathroom": [("potted plant", .8, .35)],
"restaurant": [("potted plant", 1.2, .35), ("floor lamp", 1.6, .3)],
"cafe": [("potted plant", 1.1, .35), ("bookshelf", 1.7, .2)],
# night wave: the other rooms 3D now furnishes (family models where there is no hand model)
"throne room": [("pillar", 2.6, .25), ("banner", 2.0, .1), ("pillar", 2.6, .25), ("torch", 1.7, .15)],
"supermarket": [("shelf", 1.8, .2), ("shelf", 1.8, .2)],
"garage": [("shelf", 1.8, .2), ("toolbox", .6, .3)],
}
WINVIEW = [__import__("os").environ.get("PYDRAW3D_EXP", "").find("winview=0") < 0]
SKIP_WORDS = {"bookshelf": {"bookshelf", "bookcase", "shelf"},
"floor lamp": {"lamp", "floor lamp", "light", "lantern", "candle"},
"potted plant": {"potted plant", "plant", "flower", "flowers", "vase", "tree", "cactus", "houseplant"}}
class Interior:
def __init__(self, scene, objs, wall_z, upm, cam, seed, L, trace, Obj, MODELS):
self.m, self.cam, self.seed = upm, cam, seed
self.wall_z = wall_z
self.win_x = .9 * upm * (1 if -L[0] > 0 else -1)
rng = np.random.RandomState(seed + 31)
self.rng = rng
names = {e["concept"] for e in scene.get("entities", [])}
words = set()
for n in names:
words |= {n, n.split()[-1]}
subj = [o for o in objs]
# subjects' horizontal screen ranges (u = x/z)
self.usub = []
for o in subj:
a_ = math.sqrt(max(((o.radius / o.scale - .15) / .5) ** 2 - 1, 0) / 2)
hw = .55 * o.scale * max(a_, .6)
zc = max(o.base[2], .3)
self.usub.append(((o.base[0] - hw) / zc, (o.base[0] + hw) / zc))
floor = [o for o in subj if abs(o.base[1]) < .1 * o.scale + 1e-6] or subj
if floor:
xs = [o.base[0] for o in floor]
zs = [o.base[2] for o in floor]
ext = max([abs(o.base[0] - np.mean(xs)) + .5 * o.scale for o in floor] + [.6 * upm])
rx = min(ext * 1.1 + .15 * upm, 1.5 * upm)
self.rug = (float(np.mean(xs)), float(np.mean(zs)) + .1 * upm, rx, min(ext * .6 + .3 * upm, .9 * upm))
else:
self.rug = None
self.rug_style = rng.randint(3)
self.wallpaper = rng.rand() < .4
kind = scene.get("place") if scene.get("place") in ROOM_FURNITURE else "room"
self.kind = kind
# night wave: the window shows the scene's time and weather (rain on a rainy day, snow, a night sky)
self.wx_time = scene.get("time") or "day"
self.wx = scene.get("weather") or ("snow" if scene.get("place") == "snow" or scene.get("season") == "winter" else None)
self.objs = []
placed = []
for name, h, dwall in ROOM_FURNITURE[kind]:
if words & SKIP_WORDS.get(name, {name}):
continue
mdl = MODELS.get(name)
if mdl is None:
try:
from pydraw.open_vocab import family_model
mdl = family_model(name)
except Exception:
mdl = None
if mdl is None:
continue
fn, asp = mdl
z = wall_z - dwall * upm
sc = h * upm
half = .5 * sc * asp / z
ok = None
for tries in range(24):
side = 1 if (tries + len(placed)) % 2 == 0 else -1
# night wave rule: later tries may stand at the frame edges (half cut off, framing the room), so a big
# central subject no longer leaves a library without bookshelves
uc = side * (rng.uniform(.18, .5) if tries < 12 else rng.uniform(.45, .85))
lo, hi = uc - half, uc + half
if any(not (hi < a - .03 or lo > b + .03) for a, b in self.usub + placed):
continue
if name == "bookshelf" and abs(uc * z - self.win_x) < .55 * upm + .5 * sc * asp:
continue # rule: never in front of the window
ok = uc
break
if ok is None:
continue
try:
parts = fn(None, None)
except Exception:
continue
ob = Obj(parts, (ok * z, 0.0, z), sc, 0.0, "_room_" + name, asp)
ob.col, ob.target, ob.cname, ob.dressing = None, None, None, True
self.objs.append(ob)
placed.append((ok - half, ok + half))
# paintings: 1-2 frames on the back wall, away from the window, above furniture height
self.pictures = []
for k in range(2):
for _ in range(10):
px = rng.uniform(-1.6, 1.6) * upm
pw, ph = rng.uniform(.45, .75) * upm, rng.uniform(.35, .55) * upm
if abs(px - self.win_x) < .55 * upm + pw * .5 + .15 * upm:
continue
if any(abs(px - q[0]) < (pw + q[2]) * .5 + .15 * upm for q in self.pictures):
continue
self.pictures.append((px, rng.uniform(1.35, 1.65) * upm, pw, ph, rng.randint(3), rng.rand()))
break
trace.append("interior: %s furniture %s, %d paintings, rug=%s" % (kind, [o.name[6:] for o in self.objs], len(self.pictures), self.rug is not None))
def _weather_view(self, sky, wx, wy, hill, trees):
"""night wave rule (knob winview=0): the window's view follows the scene - night (dark blue, stars, dark land),
sunset (warm sky), snow (white ground, frosted trees, falling flakes), rain (grey sky, slanted streaks, drops
on the glass), fog (washed out)"""
land = hill | trees
t, w = self.wx_time, self.wx
if w == "snow":
sky[hill] = lin((232, 236, 244)) * (.92 + .1 * vnoise(wx[hill] * 9, wy[hill] * 9, self.seed))[:, None]
sky[trees] = sky[trees] * .6 + lin((220, 228, 238)) * .4
sky[~land] = sky[~land] * .45 + lin((205, 210, 220)) * .55
if w in ("rain", "storm"):
sky[~land] = sky[~land] * .25 + lin((150, 156, 165)) * .75
sky[land] = sky[land] * .7
if w == "fog":
sky = sky * .35 + lin((215, 218, 222)) * .65
if t == "sunset":
g = np.clip(wy, 0, 1)[:, None]
sky[~land] = (lin((250, 170, 110)) * (1 - g) + lin((120, 100, 160)) * g)[~land]
sky[land] = sky[land] * .55
elif t == "night":
sky[~land] = (lin((20, 28, 60)) * (1 - wy[:, None]) + lin((6, 10, 28)) * wy[:, None])[~land]
st = (~land) & (vnoise(wx * 90, wy * 90, self.seed + 77) > .93)
sky[st] = lin((230, 230, 245))
sky[land] = sky[land] * (.18 if w != "snow" else .35)
if w in ("rain", "storm"):
# slanted streaks + a few bright drops on the glass
st = (np.sin((wx * 40 + wy * 14) * 2 * math.pi / 3) > .96) & (vnoise(wx * 30, wy * 6, self.seed + 81) > .45)
sky[st] = sky[st] * .5 + lin((225, 230, 238)) * .5
dr = vnoise(wx * 55, wy * 55, self.seed + 83) > .9
sky[dr] = lin((240, 244, 250))
elif w == "snow":
fl = vnoise(wx * 70, wy * 70, self.seed + 85) > .9
sky[fl] = lin((250, 250, 252))
return sky
def surfaces(self, q, wallm, a_, win, frame):
"""room surfaces: rug on the floor under the subjects, wallpaper, paintings, curtains, window view"""
m = self.m
floorm = ~wallm
if self.rug is not None:
cx, cz, rx, rz = self.rug
lx, lz = (q[:, 0] - cx) / rx, (q[:, 2] - cz) / rz
if self.rug_style == 2:
r = np.sqrt(lx ** 2 + lz ** 2)
inside = floorm & (r < 1)
else:
r = np.maximum(np.abs(lx), np.abs(lz))
inside = floorm & (r < 1)
if inside.any():
li, lj, rr = lx[inside], lz[inside], r[inside]
wool = (.85 + .25 * vnoise(q[inside, 0] / m * 60, q[inside, 2] / m * 60, self.seed))[:, None]
if self.rug_style == 0:
# Persian: red field, navy border with cream lines, central medallion
c = np.tile(lin((150, 32, 36)), (len(li), 1))
c[rr > .8] = lin((30, 40, 90))
c[(np.abs(rr - .78) < .015) | (np.abs(rr - .93) < .012)] = lin((230, 215, 175))
md = np.abs(li) + np.abs(lj)
c[md < .42] = lin((30, 40, 90))
c[md < .26] = lin((230, 215, 175))
c[md < .12] = lin((150, 32, 36))
orn = (rr < .75) & (md > .5) & (np.sin(li * 18) * np.sin(lj * 18) > .55)
c[orn] = lin((210, 150, 60))
elif self.rug_style == 1:
# modern stripes
pal = np.array([lin(x) for x in ((230, 225, 210), (70, 90, 110), (200, 140, 90), (120, 150, 140))])
c = pal[(np.floor((li + 1) * 4.5).astype(int)) % 4]
c[rr > .94] = lin((230, 225, 210))
else:
# round braided jute
c = lin((180, 150, 100)) * (.9 + .1 * (np.sin(rr * 60) > 0))[:, None] # (night: softer braid, read as rings)
a_[inside] = c * wool
wm = wallm & ~win & ~frame
if self.kind == "throne room" and wm.any():
# night wave rule: a throne room has dressed stone walls (offset blocks, pale mortar, a little soot) and
# a red carpet runs from the camera to the subjects (gold edges)
xx, yy = q[wm, 0] / m, q[wm, 1] / m
row = np.floor(yy / .32)
u = (xx + (row % 2) * .3) / .6
mort = (np.abs(yy / .32 - np.round(yy / .32)) < .05) | (np.abs(u - np.round(u)) < .03)
blk = (.78 + .22 * vnoise(np.floor(u) * 3.1, row * 2.7, self.seed + 9))[:, None]
a_[wm] = np.where(mort[:, None], lin((150, 145, 135)), lin((122, 118, 112)) * blk)
elif self.kind in ("garage", "supermarket") and wm.any():
# a garage: bare grey concrete with stains; a supermarket: white wall tiles
xx, yy = q[wm, 0] / m, q[wm, 1] / m
if self.kind == "garage":
a_[wm] = lin((150, 150, 146)) * (.82 + .22 * fbm2(xx * 1.5, yy * 1.5, 4, self.seed + 3))[:, None]
else:
tile = (np.abs(xx / .3 - np.round(xx / .3)) < .03) | (np.abs(yy / .3 - np.round(yy / .3)) < .03)
a_[wm] = np.where(tile[:, None], lin((200, 200, 200)), lin((238, 240, 240)))
elif self.kind == "library" and wm.any():
# a library's walls are dark wood panelling
xx, yy = q[wm, 0] / m, q[wm, 1] / m
seam = np.abs((xx / .45) - np.round(xx / .45)) < .02
grain = (.85 + .2 * vnoise(xx * 30, yy * 2, self.seed + 4))[:, None]
a_[wm] = np.where(seam[:, None], lin((60, 38, 24)), lin((112, 70, 42)) * grain)
if self.kind == "throne room" and self.rug is not None:
cx, cz, rx, rz = self.rug
run = floorm & (np.abs(q[:, 0] - cx) < .45 * m) & (q[:, 2] < cz + rz)
if run.any():
edge = np.abs(np.abs(q[run, 0] - cx) - .4 * m) < .03 * m
a_[run] = np.where(edge[:, None], lin((210, 170, 60)), lin((150, 22, 30)) * (.9 + .15 * vnoise(q[run, 0] / m * 40, q[run, 2] / m * 40, self.seed))[:, None])
if self.wallpaper and wm.any() and self.kind not in ("throne room", "library"):
# rule: some rooms have wallpaper: soft vertical stripes with a faint damask
xx = q[wm, 0] / m
st = (np.sin(xx * 2 * math.pi / .18) > .3) * .06
dm = .04 * (np.sin(xx * 25) * np.sin(q[wm, 1] / m * 25) > .4)
a_[wm] = a_[wm] * (1 - st - dm)[:, None] + lin((240, 235, 220)) * (st + dm)[:, None]
# paintings (not in a garage / supermarket)
for px, py, pw, ph, style, hh in (self.pictures if self.kind not in ("garage", "supermarket") else ()):
lx, ly = (q[:, 0] - px) / (pw * .5), (q[:, 1] - py) / (ph * .5)
inside = wallm & (np.abs(lx) < 1) & (np.abs(ly) < 1)
if not inside.any():
continue
li, lj = lx[inside], ly[inside]
fr = (np.abs(li) > .9) | (np.abs(lj) > .87)
if style == 0:
# small landscape: sky gradient, sun, two hill layers
c = lin((150, 190, 225)) * (1 - (lj[:, None] + 1) * 0) * (.8 + .3 * ((lj + 1) / 2))[:, None]
hill1 = lj < -.1 + .25 * np.sin(li * 2.5 + hh * 6)
hill2 = lj < -.45 + .15 * np.sin(li * 4 + hh * 3)
c[hill1] = lin((90, 130, 80))
c[hill2] = lin((60, 100, 55))
sun = ((li - .45) ** 2 + (lj - .45) ** 2) < .02
c[sun] = lin((250, 230, 160))
elif style == 1:
# colour-field abstract: two soft blocks
pal = [lin(x) for x in ((200, 80, 60), (240, 190, 90), (60, 90, 140), (230, 220, 200), (40, 110, 100))]
i1 = int(hh * 5) % 5
c = np.where((lj > .05)[:, None], pal[i1], pal[(i1 + 2) % 5]) * (.9 + .15 * vnoise(li * 6, lj * 6, self.seed))[:, None]
else:
# botanical print: pale paper + a dark leafy silhouette
c = np.tile(lin((235, 228, 210)), (len(li), 1))
leaf = (np.abs(li) < .06 + .5 * np.clip(1 - np.abs(lj + .1), 0, 1) * (np.abs(np.sin(lj * 9)) ** 2)) & (lj > -.8) & (lj < .8)
c[leaf] = lin((60, 95, 60))
c[fr] = lin((60, 40, 28)) if hh < .6 else lin((190, 150, 70))
a_[inside] = c
# window: a view instead of a white rectangle, plus curtains on both sides
if win.any() and self.kind == "garage":
# a garage's opening is a roll-up door: horizontal metal slats
wy = q[win, 1] / m
a_[win] = lin((185, 188, 190)) * (.8 + .2 * (np.sin(wy * 2 * math.pi / .12) > 0))[:, None]
return a_
if win.any():
wx = (q[win, 0] - self.win_x) / (.55 * m)
wy = (q[win, 1] / m - .9) / 1.1 # 0 bottom .. 1 top of the window
sky = lin((120, 170, 225)) * (1 - wy[:, None]) * 0 + (lin((175, 205, 235)) * (1 - wy[:, None]) + lin((95, 150, 220)) * wy[:, None])
cl = smooth(.5, .7, fbm2(wx * 2 + 3, wy * 3, 4, self.seed + 5)) * smooth(.3, .7, wy)
sky = sky * (1 - cl[:, None]) + lin((245, 245, 245)) * cl[:, None]
hill = wy < .28 + .08 * np.sin(wx * 3 + self.seed) + .04 * fbm2(wx * 4, wx * 0, 3, self.seed)
sky[hill] = lin((110, 150, 95)) * (.85 + .2 * vnoise(wx[hill] * 9, wy[hill] * 9, self.seed))[:, None]
trees = wy < .18 + .07 * np.abs(np.sin(wx * 13 + 2 * fbm2(wx * 5, wx * 0, 2, self.seed)))
sky[trees] = lin((60, 100, 60))
if WINVIEW[0]:
sky = self._weather_view(sky, wx, wy, hill, trees)
a_[win] = sky * 2.2
cur = wallm & (q[:, 1] > .75 * m) & (q[:, 1] < 2.2 * m) & (np.abs(np.abs(q[:, 0] - self.win_x) - .7 * m) < .17 * m)
if cur.any():
fold = .75 + .3 * np.sin((q[cur, 0] - self.win_x) / m * 60)
ccol = [lin((150, 60, 60)), lin((60, 90, 130)), lin((200, 180, 130))][self.seed % 3]
a_[cur] = ccol * fold[:, None]
rod = wallm & (np.abs(q[:, 1] - 2.22 * m) < .02 * m) & (np.abs(q[:, 0] - self.win_x) < .9 * m)
a_[rod] = lin((70, 55, 40))
return a_
def make_interior(scene, objs, wall_z, upm, cam, seed, L, trace, Obj, MODELS):
return Interior(scene, objs, wall_z, upm, cam, seed, L, trace, Obj, MODELS)