Spaces:
Running
Running
Add a material picker for assets with a Material variant set
Browse filesThe 50 fasteners show Aluminum / Brass / Nylon / Stainless steel buttons under the 3D view. Picking one recolours the model and updates the mass and a new Physics material row (density, friction, restitution); the choice is kept in the link (&m=brass). build_catalog.py reads the variant set and each materials/*.usda file.
Files changed (3)
- app.js+63-8
- data/catalog.json
- index.html+2-2
64 unmodified lines65666768686970717243 unmodified lines116117118118119120121122123116 unmodified lines240241242241243244245246210 unmodified lines45745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649722 unmodified lines5205215225235245255265275285295305315325335345355367 unmodified lines5445455464995475485495505515525535045545555565574 unmodified lines56256356456556651656756856957018 unmodified lines5895905915415925935945955965975985 unmodified lines6046056065556076086096102 unmodified lines61361461561661761861916 unmodified lines6366376386396406416426 unmodified lines64965065165265365465564 unmodified linesconst COLLAPSE = "M9 4v5H4M15 4v5h5M9 20v-5H4M15 20v-5h5";
let DATA = null;// material: the chosen option of the open asset's "Material" variant set (null = its default)const state = { q: "", group: null, category: null, facets: {}, sort: "name", asset: null, open: new Set() };const state = { q: "", group: null, category: null, facets: {}, sort: "name", asset: null, material: null, open: new Set() };
// ---- formatting ---------------------------------------------------------------function maxDim(a) { return a.extents ? Math.max(...a.extents) : 0; }43 unmodified lines// ---- filtering ----------------------------------------------------------------function matchesQuery(a, words) { if (!words.length) return true; const hay = a._hay || (a._hay = [a.id, a.name, a.class, a.category, a.group, a.summary || "", ...a.joints.map((j) => `${j.name} ${j.type}`)] const hay = a._hay || (a._hay = [a.id, a.name, a.class, a.category, a.group, a.summary || "", ...a.joints.map((j) => `${j.name} ${j.type}`), ...(a.materials ? a.materials.options.map((o) => o.id) : [])] .join(" ").toLowerCase().replace(/_/g, " ")); return words.every((w) => hay.includes(w));}116 unmodified linesasync function getViewer() { if (viewer || viewerFailed) return viewer; try { const { createViewer } = await import(signed("./viewer.js?v=29")); const { createViewer } = await import(signed("./viewer.js?v=30")); viewer = createViewer(); } catch (e) { console.error("3D viewer failed to start", e);210 unmodified lines });}
// ---- material switch ------------------------------------------------------------// Assets with a "Material" variant set (the fasteners) list each choice with its preview// colour, mass and physics values (tools/build_catalog.py). Picking one recolours the 3D// view and updates the mass and physics rows; the thumbnail stays the default material.const matName = (id) => cap(id.replace(/_/g, " "));function currentMaterial(a) { const opts = a.materials.options; return opts.find((o) => o.id === state.material) || opts.find((o) => o.id === a.materials.default) || opts[0];}const lookOf = (a, o) => ({ name: a.materials.look, color: o.color, metallic: o.metallic, roughness: o.roughness });function swatchCss(c) { const srgb = (v) => Math.round(255 * Math.min(1, Math.max(0, v <= 0.0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - 0.055))); return c ? `rgb(${c.map(srgb).join(", ")})` : "transparent";}function physicsText(o) { const parts = []; if (o.density != null) parts.push(`${o.density.toLocaleString("en-US")} kg/m³`); if (o.static_friction != null) parts.push(`friction ${o.static_friction} static, ${o.dynamic_friction} dynamic`); if (o.restitution != null) parts.push(`restitution ${o.restitution}`); return parts.join(" · ") || "—";}function materialPicker(a, onPick) { const cur = currentMaterial(a); const buttons = a.materials.options.map((o) => el("button", { class: "opt mat-opt", type: "button", "aria-pressed": o === cur ? "true" : "false", "data-material": o.id, onclick: () => onPick(o), }, el("span", { class: "swatch", style: `background: ${swatchCss(o.color)}` }), matName(o.id))); const section = el("section", {}, el("h3", { class: "section-title" }, "Material"), el("div", { class: "mat-opts", role: "group", "aria-label": "Material" }, buttons), el("p", { class: "mat-note" }, "A USD variant set (“Material”): the look, mass and friction switch together.")); section._select = (o) => buttons.forEach((b) => b.setAttribute("aria-pressed", b.dataset.material === o.id ? "true" : "false")); return section;}
async function renderDetail() { const a = state.asset && DATA.assets.find((x) => x.id === state.asset); const pane = $("detail");22 unmodified lines const scene = a.kind === "scene"; const folder = scene ? `${HF}/datasets/${a.scene_dataset}/tree/main/scenes/${a.scene}` : `${HF}/datasets/${DATA.dataset}/tree/main/${a.id}`; const files = el("a", { href: folder, target: "_blank", rel: "noopener" }, `${scene ? a.scene_dataset : DATA.dataset} ↗`); const mat = !scene && a.materials ? currentMaterial(a) : null; const massEl = el("span", {}, fmtMass(mat ? mat.mass : a.mass)); const physEl = el("span", {}, mat ? physicsText(mat) : ""); const picker = mat ? materialPicker(a, (o) => { state.material = o.id === a.materials.default ? null : o.id; picker._select(o); massEl.textContent = fmtMass(o.mass); physEl.textContent = physicsText(o); if (viewer && loadedId === a.id) viewer.setLook(lookOf(a, o)); syncUrl(); }) : null; const facts = scene ? [ ["Type", "Indoor scene"], ["Dimensions", fmtDims(a)],7 unmodified lines ["Class", a.qcode ? el("a", { href: `https://www.wikidata.org/wiki/${a.qcode}`, target: "_blank", rel: "noopener" }, `${a.class || a.qcode} (${a.qcode})`) : a.class], ["Category", `${a.group} › ${cap(a.category)}`], ["Dimensions", fmtDims(a)], ["Mass", fmtMass(a.mass)], ["Mass", massEl], ["Rigid bodies", a.rigid_bodies], ["Moving parts", partsLabel(a)], ["Placement", a.mounted ? "Mounted: base fixed to the world where you place it" : "Free-standing"], ["Physics", "PhysX (NVIDIA Isaac Sim)"], mat && ["Physics material", physEl], ["Format", mat ? `OpenUSD, “Material” variant set with ${a.materials.options.length} materials` ["Format", `OpenUSD${a.glass ? ", glass MDL shaders included" : ""}`], : `OpenUSD${a.glass ? ", glass MDL shaders included" : ""}`], ["License", "CC BY 4.0 · credit “projectsim by Kaedim”"], ["Version", a.version], ["Files", files],4 unmodified lines el("button", { class: "icon-button", type: "button", "aria-label": "Close details", onclick: () => openAsset(null) }, icon(CROSS))), el("p", { class: "detail-sub" }, scene ? a.summary : `${cap(a.class || a.category)} · ${partsLabel(a)}`)), box, picker, a.joints.length ? el("section", {}, el("h3", { class: "section-title" }, "Moving parts"), jointList, jointActions) : null, el("dl", { class: "facts" }, facts.map(([k, v]) => [el("dt", {}, k), el("dd", {}, v ?? "—")])), el("dl", { class: "facts" }, facts.filter(Boolean).map(([k, v]) => [el("dt", {}, k), el("dd", {}, v ?? "—")])), el("section", { class: "more" }, el("p", {}, el("strong", {}, "Need many variations, or your own objects?")), el("p", {}, scene ? "projectsim generates sim-ready rooms and the objects in them at scale, with physics set up."18 unmodified lines } else { loadedId = a.id; rig = scene ? await v.load(signed(`previews/scenes/${a.scene}.glb`), null, { interior: !a.splat, enclosed: !a.splat }) : await v.load(signed(`previews/${a.id}.glb`), signed(`previews/${a.id}.json`)); : await v.load(signed(`previews/${a.id}.glb`), signed(`previews/${a.id}.json`), mat ? { look: lookOf(a, currentMaterial(a)) } : {}); if (state.asset !== a.id) return; } if (mat) v.setLook(lookOf(a, currentMaterial(a))); // also after a click made while it loaded if (rig.length) { bindSliders(rows, rig); jointActions.append(5 unmodified lines// ---- state + url --------------------------------------------------------------// Only our own keys are rewritten: a private Space's page URL also carries a signed// access token (__sign), and every later request for files of this Space needs it.const OWN_KEYS = ["q", "g", "c", "sort", "a", ...FACETS.map((f) => f.key)];const OWN_KEYS = ["q", "g", "c", "sort", "a", "m", ...FACETS.map((f) => f.key)];function ownParams() { const p = new URLSearchParams(); if (state.q) p.set("q", state.q);2 unmodified lines for (const [k, s] of Object.entries(state.facets)) if (s.size) p.set(k, [...s].join(",")); if (state.sort !== "name") p.set("sort", state.sort); if (state.asset) p.set("a", state.asset); if (state.asset && state.material) p.set("m", state.material); return p;}function syncUrl() {16 unmodified lines state.category = p.get("c"); state.sort = p.get("sort") || "name"; state.asset = p.get("a"); state.material = p.get("m"); for (const f of FACETS) if (p.get(f.key)) state.facets[f.key] = new Set(p.get(f.key).split(",")); if (state.group) state.open.add(state.group);}6 unmodified lines refresh();}function openAsset(id) { if (id !== state.asset) state.material = null; state.asset = id; renderDetail(); document.querySelectorAll(".card").forEach((c) => c.setAttribute("aria-current", c.dataset.id === id ? "true" : "false"));7 unmodified lines8910111112131468 unmodified lines838485868687887 unmodified lines <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&family=Fira+Code:wght@400;500&display=swap"> <link rel="stylesheet" href="style.css?v=30"> <link rel="stylesheet" href="style.css?v=31"> <script type="importmap"> { "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",68 unmodified lines </aside> </div>
<script src="app.js?v=30"></script> <script src="app.js?v=31"></script></body></html>app.js+63-8
data/catalog.json
index.html+2-2