Spaces:
Running
Running
Download tools/make_decor.py from VectorW/xlerobot-sim: direct link, hf CLI and curl.
- Browser
- Download file 32.1 kB
-
https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/tools/make_decor.py
- Command line
-
hf download hf://spaces/VectorW/xlerobot-sim/tools/make_decor.py
-
curl -L -o make_decor.py https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/tools/make_decor.py
32.1 kB
| """Write visual models of MuJoCo-GS-Web's splat scenes, so they read without the 3D Gaussian splat. | |
| usage: python tools/make_decor.py | |
| For each splat environment this writes assets/environments/<id>/decor.xml: static bodies named decor_* with | |
| visual-only geoms (contype 0, conaffinity 0, group 2) that stand in for the splat in the page's Toon and Lit | |
| renderers and in MuJoCo's own viewer. The page includes the file when the environment's registry entry names | |
| it ("decor"), and hides these bodies while it shows the splat. The physics is unchanged: MuJoCo-GS-Web's | |
| collision boxes stay in scene.xml. | |
| The layout is measured from the splat (decoded below) and the collision boxes: walls, counters, shelves and | |
| furniture are placed by hand from those measurements, and the colours of doors, tiles and ceramics, and where | |
| the ceramics and utensils stand, are sampled from the splat's points at the same places. | |
| """ | |
| import gzip | |
| import struct | |
| from pathlib import Path | |
| import numpy as np | |
| ENVS = Path(__file__).resolve().parents[1] / "assets" / "environments" | |
| SH_C0 = 0.28209479177387814 | |
| # ------------------------------------------------------------------------------------------------ | |
| # Splat sampling | |
| def decode_spz(path: Path): | |
| """Points of an .spz (version 2) in MuJoCo coordinates, with sRGB colours, opacities and sizes. | |
| Spark loads SPZ positions unchanged; the page turns the splat +90 degrees about x into MuJoCo's z-up | |
| world, so (x, y, z) -> (x, -z, y).""" | |
| raw = gzip.decompress(path.read_bytes()) | |
| magic, version, n, _sh, frac, _flags, _ = struct.unpack("<IIIBBBB", raw[:16]) | |
| if magic != 0x5053474E or version != 2: | |
| raise ValueError(f"{path}: not an SPZ version 2 file") | |
| off = 16 | |
| p = np.frombuffer(raw, np.uint8, n * 9, off).reshape(n, 3, 3).astype(np.int32) | |
| off += n * 9 | |
| v = p[..., 0] | (p[..., 1] << 8) | (p[..., 2] << 16) | |
| v = np.where(v & 0x800000, v - (1 << 24), v) | |
| pos = v.astype(np.float64) / (1 << frac) | |
| alpha = np.frombuffer(raw, np.uint8, n, off) / 255 | |
| off += n | |
| col = np.frombuffer(raw, np.uint8, n * 3, off).reshape(n, 3) / 255 | |
| off += n * 3 | |
| rgb = np.clip(0.5 + SH_C0 * (col - 0.5) / 0.15, 0, 1) | |
| scale = np.exp(np.frombuffer(raw, np.uint8, n * 3, off).reshape(n, 3) / 16 - 10) | |
| keep = (alpha > 0.35) & (scale.max(1) < 0.25) # opaque, not the large background blobs | |
| world = np.stack([pos[:, 0], -pos[:, 2], pos[:, 1]], 1) | |
| return world[keep], rgb[keep] | |
| class Splat: | |
| def __init__(self, path: Path): | |
| self.p, self.c = decode_spz(path) | |
| mx, mn = self.c.max(1), self.c.min(1) | |
| self.sat = (mx - mn) / np.maximum(mx, 1e-3) | |
| self.val = mx | |
| def select(self, lo, hi): | |
| lo, hi = np.asarray(lo), np.asarray(hi) | |
| return np.all((self.p >= lo) & (self.p <= hi), axis=1) | |
| def colour(self, lo, hi, default, vivid=0.6): | |
| """Median colour of the points in a box, from its most saturated `vivid` share (glazes over shading).""" | |
| m = self.select(lo, hi) | |
| if m.sum() < 8: | |
| return default | |
| c, s = self.c[m], self.sat[m] | |
| top = c[s >= np.quantile(s, 1 - vivid)] | |
| return tuple(float(x) for x in np.median(top, 0)) | |
| # ------------------------------------------------------------------------------------------------ | |
| # MJCF writing | |
| def f(*v: float) -> str: | |
| return " ".join(f"{x:.4g}" for x in v) | |
| def rgba(c, a: float = 1.0) -> str: | |
| return f(*c[:3], a) | |
| VIS = 'contype="0" conaffinity="0" group="2"' | |
| class Body: | |
| def __init__(self, name: str): | |
| self.name = f"decor_{name}" | |
| self.geoms: list[str] = [] | |
| def add(self, kind: str, material: str, colour=None, **attrs) -> None: | |
| a = " ".join(f'{k}="{v}"' for k, v in attrs.items()) | |
| c = f' rgba="{rgba(colour)}"' if colour is not None else "" | |
| self.geoms.append(f'<geom type="{kind}" {a} material="{material}"{c} {VIS}/>') | |
| def box(self, lo, hi, material, colour=None, **extra): | |
| lo, hi = np.asarray(lo, float), np.asarray(hi, float) | |
| self.add("box", material, colour, pos=f(*(lo + hi) / 2), size=f(*np.abs(hi - lo) / 2), **extra) | |
| def capsule(self, a, b, r, material, colour=None): | |
| self.add("capsule", material, colour, fromto=f(*a, *b), size=f(r)) | |
| def cylinder(self, a, b, r, material, colour=None): | |
| self.add("cylinder", material, colour, fromto=f(*a, *b), size=f(r)) | |
| def ellipsoid(self, centre, radii, material, colour=None, **extra): | |
| self.add("ellipsoid", material, colour, pos=f(*centre), size=f(*radii), **extra) | |
| def sphere(self, centre, r, material, colour=None): | |
| self.add("sphere", material, colour, pos=f(*centre), size=f(r)) | |
| def xml(self) -> str: | |
| inner = "\n".join(f" {g}" for g in self.geoms) | |
| return f' <body name="{self.name}">\n{inner}\n </body>' | |
| def write(env: str, title: str, materials: dict, textures: list[str], bodies: list[Body]) -> None: | |
| mats = "\n".join(f' <material name="{k}" {v}/>' for k, v in materials.items()) | |
| tex = "\n".join(f" {t}" for t in textures) | |
| body_xml = "\n".join(b.xml() for b in bodies if b.geoms) | |
| count = sum(len(b.geoms) for b in bodies) | |
| xml = f"""<!-- {title} | |
| Written by tools/make_decor.py: visual-only geoms that stand in for the splat (see that file). --> | |
| <mujoco> | |
| <asset> | |
| {tex} | |
| {mats} | |
| </asset> | |
| <worldbody> | |
| {body_xml} | |
| </worldbody> | |
| </mujoco> | |
| """ | |
| out = ENVS / env / "decor.xml" | |
| out.write_text(xml, encoding="utf-8") | |
| print(f"wrote {out} ({count} geoms, {len(xml) / 1000:.1f} kB)") | |
| def mix(a, b, t): | |
| return tuple(float(x) * (1 - t) + float(y) * t for x, y in zip(a, b)) | |
| # ------------------------------------------------------------------------------------------------ | |
| # Tabletop: MuJoCo-GS-Web's kitchen. Walls and counters follow the collision boxes in scene.xml (inner wall | |
| # faces at y = -0.86 and x = +-1.7, counter tops at 0.82); the rest is measured from the splat. | |
| BACK, LEFT, RIGHT, FRONT = -0.86, -1.7, 1.7, 1.05 | |
| WALL_T, WALL_H = 0.1, 2.45 | |
| TOP = 0.82 # counter top | |
| SHELVES = (1.42, 1.73, 1.99) # board centres | |
| SHELF_T, SHELF_D = 0.035, 0.22 | |
| RAIL_Z = 1.37 | |
| WINDOW = (0.45, 0.95, 1.07, 2.13) # right wall: y0, y1, z0, z1 | |
| class Wall: | |
| """A wall's frame: `along` runs left to right as seen from the room, `out` points into the room.""" | |
| def __init__(self, name, origin, along, out, span): | |
| self.name, self.o, self.a, self.n, self.span = name, np.array(origin, float), np.array(along, float), np.array(out, float), span | |
| def at(self, s, d, z): | |
| return self.o + self.a * s + self.n * d + np.array([0, 0, z]) | |
| def slab(self, p, q, half_along, half_out): | |
| """Corners of a box around the segment p-q, `half_along` wide along the wall and `half_out` deep.""" | |
| pad = np.abs(self.a) * half_along + np.abs(self.n) * half_out | |
| return np.minimum(p, q) - pad, np.maximum(p, q) + pad | |
| WALLS = { | |
| # back wall seen from the room: left is +x | |
| "back": Wall("back", (0, BACK, 0), (-1, 0, 0), (0, 1, 0), (-RIGHT, -LEFT)), | |
| # left wall (x = -1.7): left is -y (the back corner) | |
| "left": Wall("left", (LEFT, 0, 0), (0, 1, 0), (1, 0, 0), (BACK, FRONT)), | |
| # right wall (x = +1.7): left is +y (the window) | |
| "right": Wall("right", (RIGHT, 0, 0), (0, -1, 0), (-1, 0, 0), (-FRONT, -BACK)), | |
| } | |
| def pot(body: Body, base, n, w, h, colour, kind): | |
| """A ceramic piece standing at `base` (bottom centre), front facing `n`, `w` wide and `h` tall.""" | |
| n = np.asarray(n, float) | |
| side = np.array([-n[1], n[0], 0.0]) | |
| base = np.asarray(base, float) | |
| dark = mix(colour, (0.1, 0.1, 0.1), 0.35) | |
| if kind == "bowl": | |
| body.ellipsoid(base + [0, 0, h * 0.5], (w / 2, w / 2, h * 0.5), "decor_ceramic", colour) | |
| body.ellipsoid(base + [0, 0, h * 0.95], (w * 0.36, w * 0.36, h * 0.12), "decor_ceramic", mix(colour, (1, 1, 1), 0.25)) | |
| elif kind == "jug": | |
| r = w * 0.38 | |
| body.cylinder(base + [0, 0, 0.004], base + [0, 0, h * 0.9], r, "decor_ceramic", colour) | |
| body.ellipsoid(base + [0, 0, h * 0.9], (r * 0.95, r * 0.95, h * 0.1), "decor_ceramic", colour) | |
| body.capsule(base + side * (r + 0.012) + [0, 0, h * 0.3], base + side * (r + 0.012) + [0, 0, h * 0.75], 0.011, "decor_ceramic", colour) | |
| body.ellipsoid(base - side * r * 0.95 + [0, 0, h * 0.86], (0.018, 0.018, 0.012), "decor_ceramic", colour) | |
| elif kind == "teapot": | |
| body.ellipsoid(base + [0, 0, h * 0.45], (w * 0.42, w * 0.42, h * 0.45), "decor_ceramic", colour) | |
| body.capsule(base - side * w * 0.3 + [0, 0, h * 0.35], base - side * w * 0.55 + [0, 0, h * 0.62], 0.012, "decor_ceramic", colour) | |
| body.capsule(base + side * w * 0.38 + [0, 0, h * 0.25], base + side * w * 0.48 + [0, 0, h * 0.6], 0.012, "decor_ceramic", colour) | |
| body.sphere(base + [0, 0, h * 0.95], 0.016, "decor_ceramic", dark) | |
| else: # lidded pot | |
| body.ellipsoid(base + [0, 0, h * 0.42], (w / 2, w / 2, h * 0.42), "decor_ceramic", colour) | |
| body.ellipsoid(base + [0, 0, h * 0.82], (w * 0.36, w * 0.36, h * 0.12), "decor_ceramic", colour) | |
| body.sphere(base + [0, 0, h * 0.95], 0.014, "decor_ceramic", dark) | |
| for sgn in (-1, 1): | |
| body.ellipsoid(base + side * sgn * w * 0.5 + [0, 0, h * 0.55], (0.012, 0.012, 0.008), "decor_ceramic", colour) | |
| def glaze(colours): | |
| """The main colour of a piece: the commonest colour bin among its points, saturated bins weighted up | |
| (shading greys a glaze), averaged.""" | |
| q = np.round(colours * 4).astype(int) | |
| keys, inverse, counts = np.unique(q, axis=0, return_inverse=True, return_counts=True) | |
| inverse = inverse.reshape(-1) | |
| sat = (keys.max(1) - keys.min(1)) / np.maximum(keys.max(1), 1) | |
| best = int(np.argmax(counts * (0.6 + sat))) | |
| return tuple(float(x) for x in colours[inverse == best].mean(0)) | |
| def shelf_ceramics(splat: Splat, body: Body, wall: Wall, s0, s1, top, room): | |
| """Find the pieces standing on a shelf (board centre `top`, `room` to the next) from the splat's points | |
| above it, and model each one.""" | |
| rel = splat.p - wall.o | |
| s = rel @ wall.a | |
| d = rel @ wall.n | |
| z = splat.p[:, 2] | |
| c = splat.c | |
| # Not the shelf boards (their fuzzy edges reach into the gap), the wall, or dark floaters. | |
| wood = (c[:, 0] - c[:, 2] > 0.22) & (c[:, 1] < 0.78 * c[:, 0]) & (c[:, 1] > 0.38 * c[:, 0]) | |
| board = top + SHELF_T / 2 | |
| m = (s >= s0) & (s <= s1) & (d > 0.07) & (d < SHELF_D + 0.03) & (z > board + 0.012) & (z < top + min(0.2, room - 0.07)) | |
| m &= ~wood & (splat.val > 0.15) | |
| if m.sum() < 20: | |
| return 0 | |
| bins = np.arange(s0, s1 + 0.02, 0.02) | |
| count, _ = np.histogram(s[m], bins) | |
| count = np.convolve(count, [1, 1, 1], "same") | |
| occupied = count >= max(6, 0.3 * np.median(count[count > 0])) | |
| runs, start = [], None | |
| for i, o in enumerate(list(occupied) + [False]): | |
| if o and start is None: | |
| start = i | |
| if not o and start is not None: | |
| runs.append((bins[start], bins[i])) | |
| start = None | |
| placed = 0 | |
| for a, b in runs: | |
| n = max(1, int(round((b - a) / 0.19))) | |
| for k in range(n): | |
| pa, pb = a + (b - a) * k / n, a + (b - a) * (k + 1) / n | |
| w = float(np.clip(pb - pa - 0.02, 0.09, 0.22)) | |
| if pb - pa < 0.07: | |
| continue | |
| mm = m & (s >= pa) & (s <= pb) | |
| if mm.sum() < 8: | |
| continue | |
| h = float(np.clip(np.quantile(z[mm], 0.95) - board, 0.07, room - 0.08)) | |
| colour = glaze(c[mm]) | |
| centre = (pa + pb) / 2 | |
| depth = float(np.clip(np.median(d[mm]), 0.08, SHELF_D - 0.07)) | |
| ratio = h / w | |
| seed = int(abs(centre) * 1000 + top * 100) % 7 | |
| kind = "jug" if ratio > 0.95 else "bowl" if ratio < 0.55 else ("teapot" if seed in (0, 3) else "pot") | |
| pot(body, wall.at(centre, depth, top + SHELF_T / 2), wall.n, w, h, colour, kind) | |
| placed += 1 | |
| return placed | |
| def utensils(splat: Splat, body: Body, wall: Wall, s0, s1): | |
| """A rail under the lowest shelf, and the utensils hanging from it where the splat has them.""" | |
| rel = splat.p - wall.o | |
| s, d, z = rel @ wall.a, rel @ wall.n, splat.p[:, 2] | |
| body.capsule(wall.at(s0, 0.035, RAIL_Z), wall.at(s1, 0.035, RAIL_Z), 0.006, "decor_steel") | |
| for s_end in (s0, s1): | |
| body.capsule(wall.at(s_end, 0.0, RAIL_Z), wall.at(s_end, 0.035, RAIL_Z), 0.005, "decor_steel") | |
| # Hanging things are darker or more colourful than the white tiles behind them. | |
| m = (s > s0 + 0.02) & (s < s1 - 0.02) & (d > 0.01) & (d < 0.12) & (z > 1.0) & (z < RAIL_Z - 0.03) & ((splat.val < 0.66) | (splat.sat > 0.25)) | |
| bins = np.arange(s0, s1 + 0.015, 0.015) | |
| count, _ = np.histogram(s[m], bins) | |
| peaks = [i for i in range(1, len(count) - 1) if count[i] >= 4 and count[i] >= count[i - 1] and count[i] >= count[i + 1]] | |
| xs = [] | |
| for i in peaks: | |
| c = (bins[i] + bins[i + 1]) / 2 | |
| if not xs or c - xs[-1] > 0.05: | |
| xs.append(c) | |
| for k, c in enumerate(xs): | |
| mm = m & (abs(s - c) < 0.02) | |
| low = float(np.clip(np.quantile(z[mm], 0.05), 1.0, RAIL_Z - 0.15)) if mm.sum() > 4 else 1.1 | |
| wood = mm.sum() > 4 and np.median(splat.sat[mm]) > 0.3 | |
| top = wall.at(c, 0.035, RAIL_Z - 0.012) | |
| bottom = wall.at(c, 0.03, low) | |
| kind = "knife" if wood else ("ladle", "spoon", "skimmer", "spatula")[k % 4] | |
| if kind == "knife": | |
| mid = wall.at(c, 0.03, low + (RAIL_Z - low) * 0.55) | |
| body.box(*wall.slab(top, mid, 0.008, 0.008), "decor_wood") | |
| body.box(*wall.slab(mid, bottom, 0.012, 0.003), "decor_steel") | |
| else: | |
| head = {"ladle": (0.04, 0.035), "spoon": (0.025, 0.04), "skimmer": (0.045, 0.045), "spatula": (0.035, 0.05)}[kind] | |
| body.capsule(top, bottom + [0, 0, head[1]], 0.005, "decor_steel") | |
| centre = bottom + [0, 0, head[1] * 0.6] | |
| flat = 0.006 if kind == "spatula" else head[0] | |
| radii = np.abs(wall.a) * head[0] + np.abs(wall.n) * flat + np.array([0, 0, head[1] * 0.55]) | |
| body.ellipsoid(centre, radii, "decor_steel") | |
| return len(xs) | |
| def cabinet_fronts(splat: Splat, body: Body, layout, face, out): | |
| """Doors and drawers on a counter front. `layout` is [(s0, s1, z0, z1, kind, default colour)]; `face` | |
| maps (s, z) to a point on the front, `out` is the front's outward normal.""" | |
| out = np.asarray(out, float) | |
| for s0, s1, z0, z1, kind, default in layout: | |
| lo = np.minimum(face(s0 + 0.04, z0 + 0.04), face(s1 - 0.04, z1 - 0.04)) - np.abs(out) * 0.08 | |
| hi = np.maximum(face(s0 + 0.04, z0 + 0.04), face(s1 - 0.04, z1 - 0.04)) + np.abs(out) * 0.08 | |
| colour = splat.colour(lo, hi, default, vivid=0.7) | |
| a, b = face(s0 + 0.006, z0 + 0.006), face(s1 - 0.006, z1 - 0.006) | |
| b = b + out * 0.018 | |
| body.box(np.minimum(a, b), np.maximum(a, b), "decor_cabinet", colour) | |
| # a recessed panel reads as a door in the outline pass | |
| ia, ib = face(s0 + 0.05, z0 + 0.05) + out * 0.018, face(s1 - 0.05, z1 - 0.05) + out * 0.024 | |
| body.box(np.minimum(ia, ib), np.maximum(ia, ib), "decor_cabinet", mix(colour, (1, 1, 1), 0.08)) | |
| # handle: horizontal on drawers, vertical on doors (near the edge that opens) | |
| mid_s, mid_z = (s0 + s1) / 2, (z0 + z1) / 2 | |
| if kind == "drawer": | |
| p, q = face(mid_s - 0.07, z1 - 0.05) + out * 0.04, face(mid_s + 0.07, z1 - 0.05) + out * 0.04 | |
| else: | |
| edge = s1 - 0.045 if kind == "door_r" else s0 + 0.045 | |
| p, q = face(edge, mid_z + 0.08) + out * 0.04, face(edge, mid_z - 0.06) + out * 0.04 | |
| body.capsule(p, q, 0.007, "decor_brass") | |
| for e in (p, q): | |
| body.capsule(e, e - out * 0.02, 0.005, "decor_brass") | |
| def tabletop(): | |
| splat = Splat(ENVS / "tabletop" / "scene.spz") | |
| sp = splat.p | |
| room, counters, shelves, things = Body("room"), Body("counters"), Body("shelves"), Body("things") | |
| ceramics, rails = Body("ceramics"), Body("utensils") | |
| # Room shell, floor and the diorama base under it. | |
| room.box((LEFT - WALL_T, BACK - WALL_T, -0.1), (RIGHT + WALL_T, FRONT, 0.0), "decor_base") | |
| room.box((LEFT, BACK, 0.0), (RIGHT, FRONT, 0.003), "decor_floor") | |
| room.box((LEFT - WALL_T, BACK - WALL_T, 0), (RIGHT + WALL_T, BACK, WALL_H), "decor_plaster") | |
| room.box((LEFT - WALL_T, BACK, 0), (LEFT, FRONT, WALL_H), "decor_plaster") | |
| y0, y1, z0, z1 = WINDOW | |
| for lo, hi in [((RIGHT, BACK, 0), (RIGHT + WALL_T, y0, WALL_H)), ((RIGHT, y1, 0), (RIGHT + WALL_T, FRONT, WALL_H)), | |
| ((RIGHT, y0, 0), (RIGHT + WALL_T, y1, z0)), ((RIGHT, y0, z1), (RIGHT + WALL_T, y1, WALL_H))]: | |
| room.box(lo, hi, "decor_plaster") | |
| # Window: frame, mullions, glass, sill. | |
| fr = 0.035 | |
| for lo, hi in [((RIGHT - 0.03, y0, z0), (RIGHT + 0.04, y0 + fr, z1)), ((RIGHT - 0.03, y1 - fr, z0), (RIGHT + 0.04, y1, z1)), | |
| ((RIGHT - 0.03, y0, z1 - fr), (RIGHT + 0.04, y1, z1)), ((RIGHT - 0.03, y0, z0), (RIGHT + 0.04, y1, z0 + fr)), | |
| ((RIGHT - 0.025, (y0 + y1) / 2 - 0.012, z0), (RIGHT + 0.03, (y0 + y1) / 2 + 0.012, z1))]: | |
| room.box(lo, hi, "decor_frame") | |
| for z in np.linspace(z0, z1, 4)[1:-1]: | |
| room.box((RIGHT - 0.025, y0, z - 0.012), (RIGHT + 0.03, y1, z + 0.012), "decor_frame") | |
| room.box((RIGHT + 0.01, y0, z0), (RIGHT + 0.02, y1, z1), "decor_glass") | |
| room.box((RIGHT - 0.09, y0 - 0.04, z0 - 0.03), (RIGHT + 0.0, y1 + 0.04, z0 + 0.005), "decor_frame") | |
| # White tiles between the counters and the lowest shelf; under the window too. | |
| tile_top = SHELVES[0] - SHELF_T / 2 | |
| room.box((LEFT, BACK, TOP), (RIGHT, BACK + 0.006, tile_top), "decor_tiles") | |
| room.box((LEFT, BACK, TOP), (LEFT + 0.006, 0.95, tile_top), "decor_tiles") | |
| room.box((RIGHT - 0.006, BACK, TOP), (RIGHT, y0, tile_top), "decor_tiles") | |
| room.box((RIGHT - 0.006, y0, TOP), (RIGHT, y1, z0 - 0.03), "decor_tiles") | |
| # Counters: carcasses on the collision boxes, a kick plinth, worktops with a small overhang. | |
| for lo, hi, top_lo, top_hi, kick_lo, kick_hi in [ | |
| ((-1.0, BACK, 0.06), (1.0, -0.30, TOP - 0.05), (-0.97, BACK, TOP - 0.05), (0.97, -0.27, TOP + 0.005), (-1.0, BACK, 0.0), (1.0, -0.35, 0.06)), | |
| ((1.0, BACK, 0.06), (RIGHT, 0.90, TOP - 0.05), (0.97, BACK, TOP - 0.05), (RIGHT, 0.93, TOP + 0.005), (1.05, BACK, 0.0), (RIGHT, 0.85, 0.06)), | |
| ((LEFT, BACK, 0.06), (-1.0, 0.90, TOP - 0.05), (LEFT, BACK, TOP - 0.05), (-0.97, 0.93, TOP + 0.005), (LEFT, BACK, 0.0), (-1.05, 0.85, 0.06)), | |
| ]: | |
| counters.box(lo, hi, "decor_carcass") | |
| counters.box(top_lo, top_hi, "decor_worktop") | |
| counters.box(kick_lo, kick_hi, "decor_plinth") | |
| pink, teal, yellow, cream = (0.86, 0.55, 0.62), (0.45, 0.76, 0.76), (0.86, 0.72, 0.42), (0.88, 0.82, 0.62) | |
| lo_z, top_z = 0.065, TOP - 0.055 | |
| # Back run, seen from the room (left = +x): measured door edges along x. | |
| back_face = lambda s, z: np.array([-s, -0.30, z]) # s = -x | |
| cabinet_fronts(splat, counters, [ | |
| (-0.98, -0.62, lo_z, top_z, "door_r", yellow), | |
| (-0.60, -0.10, 0.60, top_z, "drawer", pink), (-0.60, -0.10, lo_z, 0.58, "door_r", pink), | |
| (-0.08, 0.34, lo_z, top_z, "door_r", teal), (0.34, 0.77, lo_z, top_z, "door_l", teal), | |
| (0.79, 0.98, lo_z, top_z, "door_l", pink), | |
| ], back_face, (0, 1, 0)) | |
| # Right run, front at x = 1.0 facing -x; seen from the room, left is +y. | |
| right_face = lambda s, z: np.array([1.0, -s, z]) # s = -y | |
| cabinet_fronts(splat, counters, [ | |
| (-0.88, -0.62, 0.52, top_z, "drawer", pink), (-0.60, 0.28, 0.52, top_z, "drawer", yellow), | |
| (-0.88, -0.31, lo_z, 0.50, "door_l", pink), (-0.29, 0.28, lo_z, 0.50, "door_r", pink), | |
| ], right_face, (-1, 0, 0)) | |
| # Left run, front at x = -1.0 facing +x; seen from the room, left is -y. | |
| left_face = lambda s, z: np.array([-1.0, s, z]) # s = y | |
| cabinet_fronts(splat, counters, [ | |
| (-0.28, 0.16, 0.63, top_z, "drawer", yellow), (-0.28, 0.16, lo_z, 0.61, "door_r", pink), | |
| (0.18, 0.88, 0.63, top_z, "drawer", teal), (0.18, 0.88, 0.38, 0.61, "drawer", yellow), (0.18, 0.88, lo_z, 0.36, "drawer", teal), | |
| ], left_face, (1, 0, 0)) | |
| # Shelves along the three walls, with brackets, and the ceramics on them. | |
| placed = 0 | |
| for zc in SHELVES: | |
| shelves.box((LEFT, BACK, zc - SHELF_T / 2), (RIGHT, BACK + SHELF_D, zc + SHELF_T / 2), "decor_wood") | |
| shelves.box((LEFT, BACK + SHELF_D, zc - SHELF_T / 2), (LEFT + SHELF_D, 0.88, zc + SHELF_T / 2), "decor_wood") | |
| shelves.box((RIGHT - SHELF_D, BACK + SHELF_D, zc - SHELF_T / 2), (RIGHT, 0.40, zc + SHELF_T / 2), "decor_wood") | |
| for wall, s0, s1 in [(WALLS["back"], -1.45, 1.45), (WALLS["left"], -0.5, 0.75), (WALLS["right"], -0.3, 0.6)]: | |
| for s in np.linspace(s0, s1, 3): | |
| shelves.box(wall.at(s, 0, zc - SHELF_T / 2) - [0.008, 0.008, 0.012], wall.at(s, SHELF_D * 0.7, zc - SHELF_T / 2) + [0.008, 0.008, 0], "decor_bracket") | |
| shelves.box(wall.at(s, 0, zc - SHELF_T / 2 - 0.11) - [0.008, 0.008, 0], wall.at(s, 0.012, zc - SHELF_T / 2) + [0.008, 0.008, 0], "decor_bracket") | |
| room_h = (SHELVES[SHELVES.index(zc) + 1] - zc) if zc != SHELVES[-1] else 0.3 | |
| top = zc + SHELF_T / 2 | |
| placed += shelf_ceramics(splat, ceramics, WALLS["back"], -RIGHT + 0.02, -LEFT - 0.02, zc, room_h) | |
| placed += shelf_ceramics(splat, ceramics, WALLS["left"], BACK + SHELF_D + 0.01, 0.86, zc, room_h) | |
| placed += shelf_ceramics(splat, ceramics, WALLS["right"], -0.39, -(BACK + SHELF_D + 0.01), zc, room_h) | |
| # Utensil rails under the lowest shelf (back wall: two runs; left wall: one), measured from the splat. | |
| hung = utensils(splat, rails, WALLS["back"], -1.10, -0.12) | |
| hung += utensils(splat, rails, WALLS["back"], 0.20, 0.78) | |
| hung += utensils(splat, rails, WALLS["left"], -0.40, 0.86) | |
| # Louvred cabinets on the counter in the back corners, louvres facing the open side (+y). | |
| for x0, x1, y0_, y1_ in [(-1.62, -1.18, BACK, -0.42), (1.24, 1.66, BACK, -0.38)]: | |
| c = splat.colour((x0, y0_, TOP + 0.05), (x1, y1_, TOP + 0.55), teal, vivid=0.5) | |
| things.box((x0, y0_, TOP + 0.005), (x1, y1_, SHELVES[0] - SHELF_T / 2 - 0.005), "decor_cabinet", c) | |
| for z in np.arange(TOP + 0.08, SHELVES[0] - 0.1, 0.045): | |
| things.box((x0 + 0.04, y1_, z), (x1 - 0.04, y1_ + 0.012, z + 0.022), "decor_cabinet", mix(c, (0.15, 0.25, 0.25), 0.15)) | |
| things.box((x0 + 0.02, y1_, TOP + 0.03), (x1 - 0.02, y1_ + 0.006, TOP + 0.05), "decor_cabinet", c) | |
| # Mini fridge on the back counter. | |
| c = splat.colour((-0.2, -0.80, TOP + 0.05), (0.06, -0.66, TOP + 0.5), teal, vivid=0.5) | |
| things.box((-0.21, BACK, TOP + 0.03), (0.07, -0.68, 1.37), "decor_cabinet", c) | |
| things.box((-0.19, -0.68, TOP + 0.05), (0.05, -0.672, 1.35), "decor_cabinet", mix(c, (1, 1, 1), 0.1)) | |
| things.capsule((0.03, -0.66, 1.18), (0.03, -0.66, 1.3), 0.007, "decor_steel") | |
| for x in (-0.19, 0.05): | |
| things.box((x - 0.012, -0.84, TOP + 0.005), (x + 0.012, -0.70, TOP + 0.03), "decor_plinth") | |
| # Microwave on the right counter, door facing the room. | |
| things.box((1.20, 0.10, TOP + 0.01), (1.68, 0.60, 1.12), "decor_appliance") | |
| things.box((1.195, 0.13, TOP + 0.04), (1.205, 0.47, 1.09), "decor_dark") | |
| things.box((1.195, 0.49, TOP + 0.04), (1.205, 0.58, 1.09), "decor_panel") | |
| for z in np.linspace(TOP + 0.08, 1.04, 5): | |
| things.sphere((1.19, 0.535, z), 0.009, "decor_steel") | |
| # A round board hanging on the right wall, and a plant on the window sill. | |
| things.cylinder((RIGHT - 0.004, -0.06, 1.2), (RIGHT - 0.02, -0.06, 1.2), 0.075, "decor_wood") | |
| things.cylinder((RIGHT - 0.019, -0.06, 1.2), (RIGHT - 0.022, -0.06, 1.2), 0.035, "decor_ceramic", (0.85, 0.35, 0.3)) | |
| things.cylinder((RIGHT - 0.05, 0.75, z0 + 0.005), (RIGHT - 0.05, 0.75, z0 + 0.09), 0.045, "decor_ceramic", (0.82, 0.5, 0.35)) | |
| for k in range(6): | |
| a = k * np.pi / 3 | |
| axis = np.array([-np.sin(a), np.cos(a), 0.0]) # tilt outward; a quaternion, as MJCF euler units vary | |
| quat = f(np.cos(0.2), *(np.sin(0.2) * axis)) | |
| things.ellipsoid((RIGHT - 0.05 + 0.03 * np.cos(a), 0.75 + 0.03 * np.sin(a), z0 + 0.15), (0.025, 0.025, 0.07), "decor_leaf", (0.45, 0.62, 0.3), quat=quat) | |
| # On the back counter: a tray with bottles, bowls and a pan (left of the fridge as seen from the room). | |
| things.cylinder((-0.5, -0.68, TOP + 0.005), (-0.5, -0.68, TOP + 0.02), 0.18, "decor_wood") | |
| for x, y, h, r, default in [(-0.27, -0.76, 0.22, 0.03, (0.45, 0.42, 0.15)), (-0.33, -0.72, 0.17, 0.028, (0.72, 0.55, 0.2)), (-0.74, -0.76, 0.3, 0.035, (0.25, 0.45, 0.2))]: | |
| col = splat.colour((x - r, y - 0.05, TOP + 0.03), (x + r, y + 0.05, TOP + h * 0.7), default, vivid=0.5) | |
| things.cylinder((x, y, TOP + 0.02), (x, y, TOP + 0.02 + h * 0.7), r, "decor_glass_bottle", col) | |
| things.capsule((x, y, TOP + 0.02 + h * 0.7), (x, y, TOP + 0.02 + h), r * 0.35, "decor_glass_bottle", col) | |
| for x, y, col in [(-0.43, -0.6, (0.2, 0.35, 0.55)), (-0.62, -0.58, (0.35, 0.65, 0.3))]: | |
| things.ellipsoid((x, y, TOP + 0.05), (0.05, 0.05, 0.028), "decor_ceramic", col) | |
| things.cylinder((-0.55, -0.72, TOP + 0.02), (-0.55, -0.72, TOP + 0.05), 0.09, "decor_dark") | |
| # Jars along the left wall on the left counter, coloured from the splat. | |
| for y, h, r, default in [(-0.16, 0.14, 0.04, (0.95, 0.55, 0.1)), (0.0, 0.12, 0.05, (0.7, 0.5, 0.35)), (0.1, 0.11, 0.04, (0.5, 0.65, 0.2)), | |
| (0.31, 0.09, 0.045, (0.55, 0.8, 0.85)), (0.47, 0.1, 0.05, (0.85, 0.82, 0.75)), (0.74, 0.12, 0.04, (0.85, 0.75, 0.2))]: | |
| col = splat.colour((-1.66, y - r, TOP + 0.02), (-1.48, y + r, TOP + h), default, vivid=0.4) | |
| things.cylinder((-1.6, y, TOP + 0.005), (-1.6, y, TOP + h), r, "decor_ceramic", col) | |
| things.cylinder((-1.6, y, TOP + h), (-1.6, y, TOP + h + 0.015), r * 0.8, "decor_ceramic", mix(col, (0.2, 0.2, 0.2), 0.3)) | |
| # Tile colours for the floor (two teals, cream, terracotta), sampled with k-means from the floor. | |
| floor = (sp[:, 2] < 0.03) & (np.abs(sp[:, 0]) < 0.9) & (sp[:, 1] > -0.25) & (sp[:, 1] < 0.95) | |
| pts = splat.c[floor] | |
| rng = np.random.default_rng(0) | |
| cent = pts[rng.choice(len(pts), 4, replace=False)] | |
| for _ in range(40): | |
| lab = np.argmin(((pts[:, None] - cent[None]) ** 2).sum(2), 1) | |
| cent = np.array([pts[lab == k].mean(0) for k in range(4)]) | |
| cent = cent[np.argsort(cent.sum(1))] | |
| print("floor tiles:", [tuple(round(float(x), 2) for x in c) for c in cent]) | |
| # The splat's wall between the shelves is in their shade; the paint itself is lighter. | |
| plaster = mix(splat.colour((-1.4, BACK - 0.05, 1.5), (1.4, BACK + 0.05, 1.6), (0.71, 0.69, 0.62), vivid=1.0), (1, 1, 1), 0.35) | |
| worktop = splat.colour((-0.9, -0.6, TOP - 0.03), (0.9, -0.35, TOP + 0.01), (0.55, 0.55, 0.52), vivid=1.0) | |
| materials = { | |
| "decor_base": f'rgba="{rgba((0.36, 0.33, 0.3))}"', | |
| "decor_floor": 'texture="decor_floor" texrepeat="2.27 2.27" texuniform="true" rgba="1 1 1 1"', | |
| "decor_plaster": f'rgba="{rgba(plaster)}"', | |
| "decor_tiles": f'rgba="{rgba((0.86, 0.87, 0.85))}"', | |
| "decor_frame": 'rgba="0.93 0.93 0.91 1"', | |
| "decor_glass": 'rgba="0.82 0.9 0.95 1" emission="0.6"', | |
| "decor_carcass": f'rgba="{rgba((0.82, 0.78, 0.7))}"', | |
| "decor_worktop": f'rgba="{rgba(worktop)}"', | |
| "decor_plinth": 'rgba="0.28 0.27 0.26 1"', | |
| "decor_cabinet": 'rgba="0.86 0.55 0.62 1"', | |
| "decor_brass": 'rgba="0.62 0.48 0.27 1" specular="0.6" shininess="0.6"', | |
| "decor_wood": 'rgba="0.74 0.45 0.24 1"', | |
| "decor_bracket": 'rgba="0.18 0.18 0.18 1"', | |
| "decor_ceramic": 'rgba="0.45 0.75 0.73 1" specular="0.4" shininess="0.5"', | |
| "decor_steel": 'rgba="0.72 0.73 0.74 1" specular="0.8" shininess="0.8"', | |
| "decor_appliance": 'rgba="0.74 0.74 0.75 1" specular="0.5"', | |
| "decor_dark": 'rgba="0.12 0.12 0.13 1" specular="0.6"', | |
| "decor_panel": 'rgba="0.25 0.25 0.27 1"', | |
| "decor_leaf": 'rgba="0.45 0.62 0.3 1"', | |
| "decor_glass_bottle": 'rgba="0.4 0.45 0.2 1" specular="0.7"', | |
| } | |
| textures = [f'<texture name="decor_floor" type="2d" builtin="checker" width="256" height="256" rgb1="{f(*cent[1])}" rgb2="{f(*cent[3])}"/>'] | |
| write("tabletop", "A visual model of MuJoCo-GS-Web's tabletop kitchen.", materials, textures, [room, counters, shelves, ceramics, rails, things]) | |
| print(f"tabletop: {placed} ceramics on the shelves, {hung} utensils on the rails") | |
| # ------------------------------------------------------------------------------------------------ | |
| # UFC: MuJoCo-GS-Web's boxing ring. Collision walls at x = +-3 and y = +-2.35 keep the robot on the mat. | |
| RING_X, RING_Y = 3.2, 2.45 # mat half extents (measured 3.15-3.17 and 2.37-2.44) | |
| POST_X, POST_Y = 3.04, 2.36 # corner posts (measured 2.97-3.06, 2.08-2.34), outside the collision walls | |
| ROPES = (0.36, 0.68, 1.02, 1.34) | |
| ROPE_X, ROPE_Y = 2.97, 2.31 | |
| PLATFORM = 1.0 # the ring stands this high above the arena floor | |
| def ufc(): | |
| splat = Splat(ENVS / "UFC" / "scene.spz") | |
| ring, ropes = Body("ring"), Body("ropes") | |
| mat = splat.colour((-2.5, -1.8, -0.05), (2.5, 1.8, 0.05), (0.94, 0.93, 0.93), vivid=1.0) | |
| ring.box((-12, -12, -PLATFORM - 0.05), (12, 12, -PLATFORM), "decor_arena_floor") | |
| ring.box((-RING_X, -RING_Y, -PLATFORM), (RING_X, RING_Y, -0.03), "decor_skirt") | |
| ring.box((-RING_X - 0.03, -RING_Y - 0.03, -0.06), (RING_X + 0.03, RING_Y + 0.03, -0.0), "decor_apron") | |
| ring.box((-RING_X + 0.06, -RING_Y + 0.06, -0.01), (RING_X - 0.06, RING_Y - 0.06, 0.003), "decor_mat", mat) | |
| # Steps up to the ring in two corners, as rings have. | |
| for sx, sy in [(1, -1), (-1, 1)]: | |
| for k in range(4): | |
| h = -PLATFORM + (k + 1) * PLATFORM / 4 | |
| x = sx * (RING_X + 0.25 + (3 - k) * 0.28) | |
| ring.box((min(x, x + sx * 0.28) - 0.0, sy * POST_Y - 0.35, -PLATFORM), (max(x, x + sx * 0.28), sy * POST_Y + 0.35, h), "decor_steps") | |
| corners = {(1, 1): (0.2, 0.32, 0.72)} # blue corner; the splat's others are red | |
| for sx in (-1, 1): | |
| for sy in (-1, 1): | |
| colour = corners.get((sx, sy), (0.8, 0.12, 0.12)) | |
| x, y = sx * POST_X, sy * POST_Y | |
| ring.cylinder((x, y, -0.03), (x, y, 1.55), 0.055, "decor_post") | |
| ring.box((x - sx * 0.02 - 0.08, y - sy * 0.02 - 0.08, 0.22), (x - sx * 0.02 + 0.08, y - sy * 0.02 + 0.08, 1.5), "decor_pad", colour) | |
| ring.sphere((x, y, 1.57), 0.06, "decor_post") | |
| # Ropes: each side's half next to the blue corner is blue, the rest red; ties at the middle of each side. | |
| blue, red = (0.2, 0.3, 0.7), (0.78, 0.12, 0.12) | |
| for z in ROPES: | |
| for sy in (-1, 1): | |
| for sx in (-1, 1): | |
| col = blue if (sx, sy) == (1, 1) else red | |
| ropes.capsule((0, sy * ROPE_Y, z), (sx * (POST_X - 0.08), sy * ROPE_Y, z), 0.022, "decor_rope", col) | |
| for sx in (-1, 1): | |
| for sy in (-1, 1): | |
| col = blue if (sx, sy) == (1, 1) else red | |
| ropes.capsule((sx * ROPE_X, 0, z), (sx * ROPE_X, sy * (POST_Y - 0.08), z), 0.022, "decor_rope", col) | |
| for p in [(0, ROPE_Y), (0, -ROPE_Y), (ROPE_X, 0), (-ROPE_X, 0)]: | |
| ropes.capsule((p[0], p[1], ROPES[0] - 0.02), (p[0], p[1], ROPES[-1] + 0.02), 0.012, "decor_tie") | |
| materials = { | |
| "decor_arena_floor": 'rgba="0.2 0.17 0.17 1"', | |
| "decor_skirt": 'rgba="0.08 0.08 0.09 1"', | |
| "decor_apron": 'rgba="0.55 0.5 0.5 1"', | |
| "decor_mat": f'rgba="{rgba(mat)}"', | |
| "decor_steps": 'rgba="0.25 0.23 0.23 1"', | |
| "decor_post": 'rgba="0.2 0.2 0.22 1" specular="0.6"', | |
| "decor_pad": 'rgba="0.8 0.12 0.12 1"', | |
| "decor_rope": 'rgba="0.78 0.12 0.12 1"', | |
| "decor_tie": 'rgba="0.15 0.15 0.18 1"', | |
| } | |
| write("UFC", "A visual model of MuJoCo-GS-Web's boxing ring.", materials, [], [ring, ropes]) | |
| if __name__ == "__main__": | |
| tabletop() | |
| ufc() | |