Spaces:
Running
Running
3D viewer: HDRI lighting, clear glass, liquids visible from every angle (part 2)
Browse files4 unmodified lines567891041 unmodified lines52535455565719 unmodified lines7778798081828384858687165 unmodified lines2532542552562572584 unmodified linesimport { OrbitControls } from "three/addons/controls/OrbitControls.js";import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js";// Neutral grey studio (both themes): dark-to-light grey sky, grey floor with a faint grid// fading into the horizon, one shadow-casting key light plus soft fill.41 unmodified linesreturn tex;}export function createViewer() {const root = document.createElement("div");root.className = "viewer3d";19 unmodified linesscene.fog = new THREE.Fog(STUDIO.horizon, 10, 50);const pmrem = new THREE.PMREMGenerator(renderer);scene.environment = studioEnvironment(pmrem);scene.add(new THREE.HemisphereLight(0xffffff, 0x6a6d72, 0.5));const fill = new THREE.DirectionalLight(0xffffff, 0.45); // opposite the key, no shadowconst rim = new THREE.DirectionalLight(0xffffff, 0.4); // from behind, lights the backscene.add(fill, fill.target, rim, rim.target);const key = new THREE.DirectionalLight(0xffffff, 1.25);key.castShadow = true;key.shadow.mapSize.set(2048, 2048);key.shadow.radius = 5;165 unmodified linesscene.add(model);model.updateMatrixWorld(true);model.traverse((o) => { if (o.isMesh) o.castShadow = !opts.interior; });const byName = {};gltf.scene.traverse((n) => { if (n.name && !(clean(n.name) in byName)) byName[clean(n.name)] = n; });4 unmodified lines56789101141 unmodified lines535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012119 unmodified lines141142143144145146147148149150151152153154155156157158159160161162163164165 unmodified lines3303313323333343353364 unmodified linesimport { OrbitControls } from "three/addons/controls/OrbitControls.js";import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js";import { RGBELoader } from "three/addons/loaders/RGBELoader.js";// Neutral grey studio (both themes): dark-to-light grey sky, grey floor with a faint grid// fading into the horizon, one shadow-casting key light plus soft fill.41 unmodified linesreturn tex;}// Lighting comes from an HDRI (Poly Haven's small_hangar_02, CC0, 1K): it lights the model and// shows in its reflections, but the background stays the grey studio gradient. Until it loads// (or if it fails) the studio environment above and the fill lights are used instead.const HDRI = "env/small_hangar_02_1k.hdr";const HDRI_LIGHTS = { exposure: 1.05, key: 0.9, hemi: 0, fill: 0, rim: 0, floor: 0x7b8898 };function spaceUrl(path) {const url = new URL(path, import.meta.url);const sign = new URLSearchParams(location.search).get("__sign"); // private Space tokenif (sign) url.searchParams.set("__sign", sign);return url.href;}// See-through parts. The previews carry glass and liquids as 40%-opaque surfaces, which look// milky and are drawn back to front by their centre: a bottle and the liquid inside it (nearly// the same centre) swap order as the camera moves, and whenever the bottle draws first its// depth hides the liquid (same bug as kaedim_app's Simple3DViewer #1107). So:// - each transparent mesh that sits inside another one (liquid, dip tube) gets a fixed slot,// contents before the shell around them;// - on assets, the outer shells become clear glass (transmission: see-through with reflections,// tinted by their colour) that doesn't hide what's inside. Scenes keep plain transparency,// which is cheaper to draw.function prepareTransparent(root, { glass = true } = {}) {const items = [];root.updateMatrixWorld(true);root.traverse((o) => {if (!o.isMesh) return;const mats = Array.isArray(o.material) ? o.material : [o.material];if (!mats.some((m) => m && m.transparent)) return;const box = new THREE.Box3().setFromObject(o);const d = box.getSize(new THREE.Vector3());items.push({ o, box, vol: d.x * d.y * d.z, tol: d.length() * 0.02 });});const inside = (a, b) => a.vol < b.vol&& a.box.min.x >= b.box.min.x - b.tol && a.box.min.y >= b.box.min.y - b.tol && a.box.min.z >= b.box.min.z - b.tol&& a.box.max.x <= b.box.max.x + b.tol && a.box.max.y <= b.box.max.y + b.tol && a.box.max.z <= b.box.max.z + b.tol;for (const a of items) a.depth = items.filter((b) => b !== a && inside(a, b)).length;const deepest = Math.max(0, ...items.map((a) => a.depth));for (const a of items) {a.o.renderOrder = 1 + deepest - a.depth; // innermost draws firstif (glass && a.depth > 0) {// contents draw over the glass, so tint them by the glass they're seen through// (white syrup in an amber bottle looks amber); clear glass leaves them as they areconst shell = items.filter((b) => b.depth === 0 && inside(a, b)).sort((x, y) => x.vol - y.vol)[0];const sm = shell && (Array.isArray(shell.o.material) ? shell.o.material[0] : shell.o.material);const tint = sm && sm.color ? sm.color.clone() : null;if (tint && Math.min(tint.r, tint.g, tint.b) < 0.8 * Math.max(tint.r, tint.g, tint.b)) {tint.multiplyScalar(1 / Math.max(tint.r, tint.g, tint.b, 1e-3));const tinted = (m) => { if (!m || !m.color) return m; const c = m.clone(); c.color.multiply(tint); return c; };a.o.material = Array.isArray(a.o.material) ? a.o.material.map(tinted) : tinted(a.o.material);}}if (glass && a.depth === 0) {const toGlass = (m) => !m || !m.transparent ? m : new THREE.MeshPhysicalMaterial({name: m.name, color: m.color, map: m.map, normalMap: m.normalMap, roughness: m.roughness,roughnessMap: m.roughnessMap, metalness: 0, transmission: 1, thickness: 0, ior: 1.5,side: m.side, depthWrite: false,});a.o.material = Array.isArray(a.o.material) ? a.o.material.map(toGlass) : toGlass(a.o.material);}}}export function createViewer() {const root = document.createElement("div");root.className = "viewer3d";19 unmodified linesscene.fog = new THREE.Fog(STUDIO.horizon, 10, 50);const pmrem = new THREE.PMREMGenerator(renderer);scene.environment = studioEnvironment(pmrem);const hemi = new THREE.HemisphereLight(0xffffff, 0x6a6d72, 0.5);scene.add(hemi);const fill = new THREE.DirectionalLight(0xffffff, 0.45); // opposite the key, no shadowconst rim = new THREE.DirectionalLight(0xffffff, 0.4); // from behind, lights the backscene.add(fill, fill.target, rim, rim.target);const key = new THREE.DirectionalLight(0xffffff, 1.25);new RGBELoader().loadAsync(spaceUrl(HDRI)).then((hdr) => {hdr.mapping = THREE.EquirectangularReflectionMapping;const old = scene.environment;scene.environment = pmrem.fromEquirectangular(hdr).texture;hdr.dispose();if (old) old.dispose();// the HDRI lights every side, so the extra fill lights go; the key stays for the shadowhemi.intensity = HDRI_LIGHTS.hemi; fill.intensity = HDRI_LIGHTS.fill; rim.intensity = HDRI_LIGHTS.rim;key.intensity = HDRI_LIGHTS.key;renderer.toneMappingExposure = HDRI_LIGHTS.exposure;floor.material.color.set(HDRI_LIGHTS.floor); // bluer than the studio floor: cancels the HDRI's warm light so the floor stays neutral grey}).catch((e) => console.warn("HDRI lighting didn't load; using the studio lights", e));key.castShadow = true;key.shadow.mapSize.set(2048, 2048);key.shadow.radius = 5;165 unmodified linesscene.add(model);model.updateMatrixWorld(true);model.traverse((o) => { if (o.isMesh) o.castShadow = !opts.interior; });prepareTransparent(model, { glass: !opts.interior });const byName = {};gltf.scene.traverse((n) => { if (n.name && !(clean(n.name) in byName)) byName[clean(n.name)] = n; });
viewer.js+79-1