788 lines
28 KiB
JavaScript
788 lines
28 KiB
JavaScript
import * as THREE from "three";
|
|
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
|
|
|
|
const FLOOR_HEIGHT = 3.6;
|
|
const WALL_HEIGHT = 2.6;
|
|
const WALL_THICK = 0.16;
|
|
|
|
// sRGB equivalents of the Aurora light canvas and blue action palette.
|
|
const SCENE_COLORS = {
|
|
background: 0xeff0f2,
|
|
gridMajor: 0xd6d9df,
|
|
gridMinor: 0xe3e5e9,
|
|
accent: 0x235cc7,
|
|
};
|
|
|
|
const ZONE_COLORS = {
|
|
seating: 0xe8dfd1,
|
|
counter: 0xd4a373,
|
|
bar: 0xc89f81,
|
|
kitchen: 0xb7b7a4,
|
|
restroom: 0xa8dadc,
|
|
terrace: 0xccd5ae,
|
|
entrance: 0xf4e285,
|
|
other: 0xdedbd2,
|
|
};
|
|
|
|
const scenes = new Map();
|
|
let nextId = 1;
|
|
|
|
function floorIndex(floors, level) {
|
|
const sorted = [...floors].sort((a, b) => a.level - b.level);
|
|
const idx = sorted.findIndex((f) => f.level === level);
|
|
return idx >= 0 ? idx : 0;
|
|
}
|
|
|
|
function createScene(canvas, layoutJson, onZoneSelect, cleanup) {
|
|
const layout = JSON.parse(layoutJson);
|
|
if (!layout || !Array.isArray(layout.floors)) throw new Error("Invalid floor layout");
|
|
const coarsePointer = window.matchMedia("(pointer: coarse)");
|
|
const originalTouchAction = canvas.style.touchAction;
|
|
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: false });
|
|
cleanup.push(() => renderer.dispose());
|
|
renderer.shadowMap.enabled = true;
|
|
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
|
|
renderer.setClearColor(SCENE_COLORS.background, 1);
|
|
|
|
const scene = new THREE.Scene();
|
|
cleanup.push(() => disposeSceneObjects(scene));
|
|
scene.fog = new THREE.FogExp2(SCENE_COLORS.background, 0.015);
|
|
|
|
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 200);
|
|
const controls = new OrbitControls(camera, canvas);
|
|
cleanup.push(() => { controls.dispose(); canvas.style.touchAction = originalTouchAction; });
|
|
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
|
|
controls.enableDamping = !reducedMotion.matches;
|
|
controls.dampingFactor = 0.08;
|
|
controls.maxPolarAngle = Math.PI * 0.48;
|
|
|
|
const hemi = new THREE.HemisphereLight(0xffffff, 0xaab0ba, 1.05);
|
|
scene.add(hemi);
|
|
|
|
const dir = new THREE.DirectionalLight(0xffffff, 1.25);
|
|
dir.position.set(16, 28, 18);
|
|
dir.castShadow = true;
|
|
dir.shadow.mapSize.set(2048, 2048);
|
|
dir.shadow.camera.near = 1;
|
|
dir.shadow.camera.far = 90;
|
|
const d = 24;
|
|
dir.shadow.camera.left = -d;
|
|
dir.shadow.camera.right = d;
|
|
dir.shadow.camera.top = d;
|
|
dir.shadow.camera.bottom = -d;
|
|
scene.add(dir);
|
|
|
|
const groundGrid = new THREE.GridHelper(60, 60, SCENE_COLORS.gridMajor, SCENE_COLORS.gridMinor);
|
|
groundGrid.position.y = -0.08;
|
|
scene.add(groundGrid);
|
|
|
|
const floorGroups = new Map();
|
|
const pinMeshes = [];
|
|
const floors = layout.floors || [];
|
|
|
|
floors.forEach((floor) => {
|
|
const idx = floorIndex(floors, floor.level);
|
|
const yBase = idx * FLOOR_HEIGHT;
|
|
const group = buildFloorGroup(floor, yBase, pinMeshes);
|
|
scene.add(group);
|
|
floorGroups.set(floor.level, { group, yBase, floor });
|
|
});
|
|
|
|
const maxW = Math.max(10, ...floors.map((f) => f.width_m || 10));
|
|
const maxD = Math.max(10, ...floors.map((f) => f.depth_m || 10));
|
|
const span = Math.max(maxW, maxD);
|
|
camera.position.set(span * 1.15, span * 1.05, span * 1.25);
|
|
controls.target.set(0, (floors.length - 1) * FLOOR_HEIGHT * 0.35, 0);
|
|
controls.update();
|
|
|
|
let hasSize = false;
|
|
let visible = true;
|
|
let contextLost = false;
|
|
let disposed = false;
|
|
let raf = 0;
|
|
let st;
|
|
const requestFrame = () => {
|
|
if (!disposed && !contextLost && !document.hidden && visible && hasSize && !raf) {
|
|
raf = requestAnimationFrame(animate);
|
|
}
|
|
};
|
|
let pixelRatio = 0;
|
|
const resize = () => {
|
|
const rect = canvas.getBoundingClientRect();
|
|
const w = rect.width;
|
|
const h = rect.height;
|
|
hasSize = w > 0 && h > 0;
|
|
if (!hasSize) return;
|
|
const nextPixelRatio = Math.min(window.devicePixelRatio || 1, coarsePointer.matches ? 1.5 : 2);
|
|
if (nextPixelRatio !== pixelRatio) {
|
|
renderer.setPixelRatio(nextPixelRatio);
|
|
pixelRatio = nextPixelRatio;
|
|
}
|
|
renderer.setSize(w, h, false);
|
|
camera.aspect = w / h;
|
|
// Leave room around the model on portrait and near-square phone viewports.
|
|
camera.fov = (2 * Math.atan(Math.tan((42 * Math.PI) / 360) / Math.min(1, camera.aspect / 1.15)) * 180) / Math.PI;
|
|
camera.updateProjectionMatrix();
|
|
requestFrame();
|
|
};
|
|
resize();
|
|
const ro = new ResizeObserver(resize);
|
|
cleanup.push(() => ro.disconnect());
|
|
ro.observe(canvas);
|
|
window.addEventListener("resize", resize);
|
|
cleanup.push(() => window.removeEventListener("resize", resize));
|
|
|
|
const raycaster = new THREE.Raycaster();
|
|
const pointer = new THREE.Vector2();
|
|
const activePointers = new Set();
|
|
let downPos = null;
|
|
|
|
const syncInteraction = () => {
|
|
downPos = null;
|
|
controls.enabled = !coarsePointer.matches || canvas.dataset.interactive === "true";
|
|
// OrbitControls sets this to "none" in its constructor, so apply the page-scroll mode afterward.
|
|
canvas.style.touchAction = controls.enabled ? "none" : "pan-y";
|
|
};
|
|
syncInteraction();
|
|
const interactionObserver = new MutationObserver(syncInteraction);
|
|
cleanup.push(() => interactionObserver.disconnect());
|
|
interactionObserver.observe(canvas, { attributes: true, attributeFilter: ["data-interactive"] });
|
|
const onPointerModeChange = () => {
|
|
syncInteraction();
|
|
resize();
|
|
};
|
|
coarsePointer.addEventListener("change", onPointerModeChange);
|
|
cleanup.push(() => coarsePointer.removeEventListener("change", onPointerModeChange));
|
|
|
|
const onActivePointerDown = (e) => {
|
|
activePointers.add(e.pointerId);
|
|
if (activePointers.size > 1) downPos = null;
|
|
};
|
|
const onPointerDown = (e) => {
|
|
activePointers.add(e.pointerId);
|
|
// Pins remain tappable in page-scroll mode; only orbit gestures require opt-in.
|
|
if (activePointers.size !== 1 || e.isPrimary === false || e.button !== 0) {
|
|
downPos = null;
|
|
return;
|
|
}
|
|
downPos = { pointerId: e.pointerId, x: e.clientX, y: e.clientY, at: performance.now() };
|
|
};
|
|
const onPointerMove = (e) => {
|
|
if (downPos?.pointerId === e.pointerId && Math.hypot(e.clientX - downPos.x, e.clientY - downPos.y) > 6) {
|
|
downPos = null;
|
|
}
|
|
};
|
|
const onPointerUp = (e) => {
|
|
const wasOnlyPointer = activePointers.size === 1 && activePointers.has(e.pointerId);
|
|
activePointers.delete(e.pointerId);
|
|
const tap = downPos;
|
|
downPos = null;
|
|
if (!wasOnlyPointer || tap?.pointerId !== e.pointerId || performance.now() - tap.at > 800) return;
|
|
if (Math.hypot(e.clientX - tap.x, e.clientY - tap.y) > 6) return;
|
|
const rect = canvas.getBoundingClientRect();
|
|
if (rect.width <= 0 || rect.height <= 0) return;
|
|
if (e.clientX < rect.left || e.clientX > rect.right || e.clientY < rect.top || e.clientY > rect.bottom) return;
|
|
pointer.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
|
|
pointer.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
|
|
raycaster.setFromCamera(pointer, camera);
|
|
const selectablePins = pinMeshes.filter((pin) => {
|
|
if (st.activeLevel !== 0 && pin.userData.floorLevel !== st.activeLevel) return false;
|
|
for (let parent = pin; parent; parent = parent.parent) {
|
|
if (!parent.visible) return false;
|
|
}
|
|
return true;
|
|
});
|
|
const hits = raycaster.intersectObjects(selectablePins, true);
|
|
let obj = hits[0]?.object;
|
|
// The decorative pin is small; use a 44px minimum tap target around its visible center.
|
|
if (!obj) {
|
|
let distance = 22;
|
|
const projected = new THREE.Vector3();
|
|
for (const pin of selectablePins) {
|
|
pin.getWorldPosition(projected);
|
|
projected.project(camera);
|
|
if (projected.z < -1 || projected.z > 1) continue;
|
|
const x = rect.left + (projected.x + 1) * rect.width / 2;
|
|
const y = rect.top + (1 - projected.y) * rect.height / 2;
|
|
const candidateDistance = Math.hypot(e.clientX - x, e.clientY - y);
|
|
if (candidateDistance < distance) {
|
|
distance = candidateDistance;
|
|
obj = pin;
|
|
}
|
|
}
|
|
}
|
|
if (obj) {
|
|
while (obj && !obj.userData?.photoIds) {
|
|
obj = obj.parent;
|
|
}
|
|
if (obj && obj.userData?.photoIds && typeof onZoneSelect === "function") {
|
|
highlightZone(st.id, obj.userData.zoneLabel, JSON.stringify(obj.userData.photoIds));
|
|
onZoneSelect(obj.userData.zoneLabel || "구역", JSON.stringify(obj.userData.photoIds));
|
|
}
|
|
}
|
|
};
|
|
const onPointerCancel = (e) => {
|
|
activePointers.delete(e.pointerId);
|
|
downPos = null;
|
|
};
|
|
canvas.addEventListener("pointerdown", onPointerDown);
|
|
window.addEventListener("pointerdown", onActivePointerDown, true);
|
|
window.addEventListener("pointermove", onPointerMove, true);
|
|
canvas.addEventListener("lostpointercapture", onPointerCancel);
|
|
// Capture release/cancel even when a scrolling or dragged pointer leaves the canvas.
|
|
window.addEventListener("pointerup", onPointerUp, true);
|
|
window.addEventListener("pointercancel", onPointerCancel, true);
|
|
cleanup.push(() => {
|
|
canvas.removeEventListener("pointerdown", onPointerDown);
|
|
window.removeEventListener("pointerdown", onActivePointerDown, true);
|
|
window.removeEventListener("pointermove", onPointerMove, true);
|
|
canvas.removeEventListener("lostpointercapture", onPointerCancel);
|
|
window.removeEventListener("pointerup", onPointerUp, true);
|
|
window.removeEventListener("pointercancel", onPointerCancel, true);
|
|
});
|
|
|
|
function animate() {
|
|
raf = 0;
|
|
if (disposed || contextLost || document.hidden || !visible || !hasSize) return;
|
|
const t = performance.now() / 1000;
|
|
pinMeshes.forEach((pin, i) => {
|
|
pin.position.y = pin.userData.baseY + (reducedMotion.matches ? 0 : Math.sin(t * 2.6 + i) * 0.08);
|
|
pin.rotation.y = reducedMotion.matches ? 0 : t * 1.1 + i;
|
|
});
|
|
controls.update();
|
|
renderer.render(scene, camera);
|
|
if (!reducedMotion.matches) requestFrame();
|
|
}
|
|
const stopFrame = () => {
|
|
cancelAnimationFrame(raf);
|
|
raf = 0;
|
|
};
|
|
const onVisibilityChange = () => document.hidden ? stopFrame() : requestFrame();
|
|
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
cleanup.push(() => { disposed = true; stopFrame(); document.removeEventListener("visibilitychange", onVisibilityChange); });
|
|
const intersectionObserver = new IntersectionObserver(([entry]) => {
|
|
visible = entry.isIntersecting;
|
|
visible ? requestFrame() : stopFrame();
|
|
});
|
|
intersectionObserver.observe(canvas);
|
|
cleanup.push(() => intersectionObserver.disconnect());
|
|
const onMotionChange = () => {
|
|
controls.enableDamping = !reducedMotion.matches;
|
|
requestFrame();
|
|
};
|
|
reducedMotion.addEventListener("change", onMotionChange);
|
|
controls.addEventListener("change", requestFrame);
|
|
cleanup.push(() => { reducedMotion.removeEventListener("change", onMotionChange); controls.removeEventListener("change", requestFrame); });
|
|
const onContextLost = (event) => {
|
|
event.preventDefault();
|
|
contextLost = true;
|
|
stopFrame();
|
|
reportSceneState(canvas, "공간 화면이 중단됐습니다. 잠시 후 다시 시도해 주세요.");
|
|
};
|
|
const onContextRestored = () => {
|
|
contextLost = false;
|
|
resize();
|
|
requestFrame();
|
|
reportSceneState(canvas, null);
|
|
};
|
|
canvas.addEventListener("webglcontextlost", onContextLost);
|
|
canvas.addEventListener("webglcontextrestored", onContextRestored);
|
|
cleanup.push(() => { canvas.removeEventListener("webglcontextlost", onContextLost); canvas.removeEventListener("webglcontextrestored", onContextRestored); });
|
|
|
|
const id = nextId++;
|
|
st = {
|
|
id,
|
|
renderer,
|
|
scene,
|
|
camera,
|
|
controls,
|
|
floorGroups,
|
|
pinMeshes,
|
|
ro,
|
|
interactionObserver,
|
|
coarsePointer,
|
|
onPointerModeChange,
|
|
resize,
|
|
requestFrame,
|
|
reducedMotion,
|
|
onMotionChange,
|
|
intersectionObserver,
|
|
onVisibilityChange,
|
|
onContextLost,
|
|
onContextRestored,
|
|
stopAnimation: () => {
|
|
disposed = true;
|
|
stopFrame();
|
|
},
|
|
canvas,
|
|
originalTouchAction,
|
|
onPointerDown,
|
|
onActivePointerDown,
|
|
onPointerMove,
|
|
onPointerUp,
|
|
onPointerCancel,
|
|
span,
|
|
activeLevel: 0,
|
|
viewMode: "3d",
|
|
};
|
|
scenes.set(id, st);
|
|
resetScene(id);
|
|
reportSceneState(canvas, null);
|
|
requestFrame();
|
|
return id;
|
|
}
|
|
|
|
function reportSceneState(canvas, message) {
|
|
canvas.dataset.sceneError = message || "";
|
|
canvas.dispatchEvent(new Event(message ? "scene-error" : "scene-ready", { bubbles: true }));
|
|
}
|
|
|
|
export function mountScene(canvas, layoutJson, onZoneSelect) {
|
|
const cleanup = [];
|
|
try {
|
|
return createScene(canvas, layoutJson, onZoneSelect, cleanup);
|
|
} catch (error) {
|
|
cleanup.reverse().forEach((dispose) => { try { dispose(); } catch { /* Finish the remaining cleanup. */ } });
|
|
console.warn("Space viewer could not start", error?.message);
|
|
reportSceneState(canvas, "공간 화면을 열 수 없습니다. 아래 정보와 사진을 확인하거나 다시 시도해 주세요.");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
export function setFloor(handle, level) {
|
|
const st = scenes.get(handle);
|
|
if (!st) return;
|
|
if (level !== 0 && !st.floorGroups.has(level)) return;
|
|
const previousY = floorTarget(st);
|
|
st.activeLevel = level;
|
|
const offsetY = floorTarget(st) - previousY;
|
|
st.camera.position.y += offsetY;
|
|
st.controls.target.y += offsetY;
|
|
st.floorGroups.forEach(({ group, yBase }, fLevel) => {
|
|
if (level === 0) {
|
|
group.visible = true;
|
|
setGroupOpacity(group, 1.0);
|
|
} else if (fLevel === level) {
|
|
group.visible = true;
|
|
setGroupOpacity(group, 1.0);
|
|
} else if (fLevel < level) {
|
|
group.visible = st.viewMode !== "plan";
|
|
setGroupOpacity(group, 0.18);
|
|
} else {
|
|
group.visible = false;
|
|
}
|
|
});
|
|
st.controls.update();
|
|
st.requestFrame();
|
|
}
|
|
|
|
function floorTarget(st) {
|
|
return st.floorGroups.get(st.activeLevel)?.yBase ?? Math.max(0, st.floorGroups.size - 1) * FLOOR_HEIGHT * 0.35;
|
|
}
|
|
|
|
export function setViewMode(handle, mode) {
|
|
const st = scenes.get(handle);
|
|
if (!st) return;
|
|
st.viewMode = mode === "plan" ? "plan" : "3d";
|
|
const plan = st.viewMode === "plan";
|
|
st.controls.enableRotate = !plan;
|
|
st.controls.minPolarAngle = 0;
|
|
st.controls.maxPolarAngle = plan ? 0 : Math.PI * 0.48;
|
|
st.controls.screenSpacePanning = true;
|
|
st.controls.mouseButtons.LEFT = plan ? THREE.MOUSE.PAN : THREE.MOUSE.ROTATE;
|
|
st.controls.touches.ONE = plan ? THREE.TOUCH.PAN : THREE.TOUCH.ROTATE;
|
|
st.controls.touches.TWO = THREE.TOUCH.DOLLY_PAN;
|
|
setFloor(handle, st.activeLevel);
|
|
resetScene(handle);
|
|
}
|
|
|
|
export function resetScene(handle) {
|
|
const st = scenes.get(handle);
|
|
if (!st) return;
|
|
const yTarget = floorTarget(st);
|
|
const s = st.span;
|
|
// Flush accumulated damping so reset and view changes do not inherit a prior gesture.
|
|
const damping = st.controls.enableDamping;
|
|
st.controls.enableDamping = false;
|
|
st.controls.update();
|
|
st.controls.enableDamping = damping;
|
|
st.controls.minDistance = Math.max(2, s * 0.18);
|
|
st.controls.maxDistance = s * 5;
|
|
if (st.viewMode === "plan") {
|
|
st.camera.position.set(0, yTarget + s * 1.65, 0.01);
|
|
st.controls.target.set(0, yTarget, 0);
|
|
} else {
|
|
st.camera.position.set(s * 1.15, yTarget + s * 1.0, s * 1.25);
|
|
st.controls.target.set(0, yTarget + 0.4, 0);
|
|
}
|
|
st.controls.update();
|
|
st.requestFrame();
|
|
}
|
|
|
|
export function zoomScene(handle, delta) {
|
|
const st = scenes.get(handle);
|
|
if (!st || !Number.isFinite(delta)) return;
|
|
const offset = st.camera.position.clone().sub(st.controls.target);
|
|
const distance = THREE.MathUtils.clamp(offset.length() * Math.exp(-delta * 0.18), st.controls.minDistance, st.controls.maxDistance);
|
|
st.camera.position.copy(st.controls.target).add(offset.setLength(distance));
|
|
st.controls.update();
|
|
st.requestFrame();
|
|
}
|
|
|
|
export function highlightZone(handle, label, photoIdsJson) {
|
|
const st = scenes.get(handle);
|
|
if (!st) return;
|
|
let photoIds = [];
|
|
try { photoIds = JSON.parse(photoIdsJson); } catch { /* An empty selection simply clears emphasis. */ }
|
|
st.scene.traverse((obj) => {
|
|
if (!obj.userData.zoneSurface) return;
|
|
const selected = Boolean(label) && obj.userData.zoneLabel === label &&
|
|
(!photoIds.length || obj.userData.photoIds.some((id) => photoIds.includes(id)));
|
|
obj.material.emissive.setHex(selected ? SCENE_COLORS.accent : 0x000000);
|
|
obj.material.emissiveIntensity = selected ? 0.23 : 0;
|
|
});
|
|
st.pinMeshes.forEach((pin) => {
|
|
const selected = Boolean(label) && pin.userData.zoneLabel === label &&
|
|
(!photoIds.length || pin.userData.photoIds.some((id) => photoIds.includes(id)));
|
|
pin.scale.setScalar(selected ? 1.4 : 1);
|
|
});
|
|
st.requestFrame();
|
|
}
|
|
|
|
export function disposeScene(handle) {
|
|
const st = scenes.get(handle);
|
|
if (!st) return;
|
|
st.stopAnimation();
|
|
st.ro.disconnect();
|
|
st.intersectionObserver.disconnect();
|
|
st.interactionObserver.disconnect();
|
|
st.coarsePointer.removeEventListener("change", st.onPointerModeChange);
|
|
st.reducedMotion.removeEventListener("change", st.onMotionChange);
|
|
document.removeEventListener("visibilitychange", st.onVisibilityChange);
|
|
window.removeEventListener("resize", st.resize);
|
|
st.canvas.removeEventListener("pointerdown", st.onPointerDown);
|
|
window.removeEventListener("pointerdown", st.onActivePointerDown, true);
|
|
window.removeEventListener("pointermove", st.onPointerMove, true);
|
|
st.canvas.removeEventListener("lostpointercapture", st.onPointerCancel);
|
|
window.removeEventListener("pointerup", st.onPointerUp, true);
|
|
window.removeEventListener("pointercancel", st.onPointerCancel, true);
|
|
st.canvas.removeEventListener("webglcontextlost", st.onContextLost);
|
|
st.canvas.removeEventListener("webglcontextrestored", st.onContextRestored);
|
|
st.controls.removeEventListener("change", st.requestFrame);
|
|
st.controls.dispose();
|
|
st.canvas.style.touchAction = st.originalTouchAction;
|
|
disposeSceneObjects(st.scene);
|
|
st.renderer.dispose();
|
|
scenes.delete(handle);
|
|
}
|
|
|
|
function disposeSceneObjects(scene) {
|
|
const geometries = new Set();
|
|
const materials = new Set();
|
|
scene.traverse((obj) => {
|
|
if (obj.geometry) geometries.add(obj.geometry);
|
|
if (obj.material) (Array.isArray(obj.material) ? obj.material : [obj.material]).forEach((m) => materials.add(m));
|
|
if (obj.shadow) obj.shadow.dispose();
|
|
});
|
|
geometries.forEach((geometry) => geometry.dispose());
|
|
materials.forEach((material) => material.dispose());
|
|
}
|
|
|
|
function setGroupOpacity(group, opacity) {
|
|
group.traverse((obj) => {
|
|
if (obj.isMesh && obj.material) {
|
|
const mats = Array.isArray(obj.material) ? obj.material : [obj.material];
|
|
mats.forEach((m) => {
|
|
if (m.userData.baseOpacity === undefined) {
|
|
m.userData.baseOpacity = m.opacity ?? 1.0;
|
|
m.userData.wasTransparent = m.transparent ?? false;
|
|
}
|
|
if (opacity < 0.99) {
|
|
m.transparent = true;
|
|
m.opacity = m.userData.baseOpacity * opacity;
|
|
} else {
|
|
m.transparent = m.userData.wasTransparent;
|
|
m.opacity = m.userData.baseOpacity;
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
// Convert floor local (x east 0..W, y north 0..D) into Three.js world (x east -W/2..W/2, z south -D/2..D/2)
|
|
// Note: local y=0 is SOUTH (front/entrance), which maps to z = +D/2 in Three.js so south faces the viewer.
|
|
function toWorldXZ(floor, lx, ly) {
|
|
return [lx - floor.width_m / 2, floor.depth_m / 2 - ly];
|
|
}
|
|
|
|
function buildFloorGroup(floor, yBase, pinMeshes) {
|
|
const group = new THREE.Group();
|
|
group.position.y = yBase;
|
|
const W = floor.width_m || 10;
|
|
const D = floor.depth_m || 10;
|
|
|
|
// Floor slab
|
|
const slabGeo = new THREE.BoxGeometry(W, 0.14, D);
|
|
const slabMat = new THREE.MeshStandardMaterial({ color: 0xf3ede2, roughness: 0.85 });
|
|
const slab = new THREE.Mesh(slabGeo, slabMat);
|
|
slab.position.y = -0.07;
|
|
slab.receiveShadow = true;
|
|
group.add(slab);
|
|
|
|
// Subtle perimeter walls (low cutaway height on South/East so interior is easy to see)
|
|
buildWalls(group, floor);
|
|
|
|
// Zones + furniture + pins
|
|
(floor.zones || []).forEach((zone) => {
|
|
const zg = buildZone(floor, zone, pinMeshes);
|
|
group.add(zg);
|
|
});
|
|
|
|
// Verticals (stairs / elevator)
|
|
(floor.verticals || []).forEach((v) => {
|
|
group.add(buildVertical(floor, v));
|
|
});
|
|
|
|
return group;
|
|
}
|
|
|
|
function buildWalls(group, floor) {
|
|
const W = floor.width_m;
|
|
const D = floor.depth_m;
|
|
const wallMat = new THREE.MeshStandardMaterial({ color: 0xd8d2c5, roughness: 0.75 });
|
|
const glassMat = new THREE.MeshStandardMaterial({
|
|
color: 0x8ecae6,
|
|
transparent: true,
|
|
opacity: 0.32,
|
|
roughness: 0.2,
|
|
});
|
|
const doorMat = new THREE.MeshStandardMaterial({ color: 0xe76f51, roughness: 0.6 });
|
|
|
|
// Back walls (North & West) full height; front walls (South & East) cutaway 0.9m so camera sees inside
|
|
const north = new THREE.Mesh(new THREE.BoxGeometry(W, WALL_HEIGHT, WALL_THICK), wallMat.clone());
|
|
north.position.set(0, WALL_HEIGHT / 2, -D / 2);
|
|
north.castShadow = true;
|
|
group.add(north);
|
|
|
|
const west = new THREE.Mesh(new THREE.BoxGeometry(WALL_THICK, WALL_HEIGHT, D), wallMat.clone());
|
|
west.position.set(-W / 2, WALL_HEIGHT / 2, 0);
|
|
west.castShadow = true;
|
|
group.add(west);
|
|
|
|
const lowH = 0.65;
|
|
const south = new THREE.Mesh(new THREE.BoxGeometry(W, lowH, WALL_THICK), wallMat.clone());
|
|
south.position.set(0, lowH / 2, D / 2);
|
|
group.add(south);
|
|
|
|
const east = new THREE.Mesh(new THREE.BoxGeometry(WALL_THICK, lowH, D), wallMat.clone());
|
|
east.position.set(W / 2, lowH / 2, 0);
|
|
group.add(east);
|
|
|
|
// Render openings (windows as glass panels, entrances as welcome mats + arch)
|
|
(floor.openings || []).forEach((op) => {
|
|
const len = Math.max(0.9, op.length_m || 1.5);
|
|
const off = op.offset_m || 1.0;
|
|
const isWindow = op.kind === "window";
|
|
const h = isWindow ? 1.5 : 2.1;
|
|
const yCenter = isWindow ? 1.3 : h / 2;
|
|
const mat = isWindow ? glassMat.clone() : doorMat.clone();
|
|
|
|
let mesh;
|
|
if (op.wall === "south" || op.wall === "north") {
|
|
mesh = new THREE.Mesh(new THREE.BoxGeometry(len, h, WALL_THICK * 1.3), mat);
|
|
const x = -W / 2 + off + len / 2;
|
|
const z = op.wall === "south" ? D / 2 : -D / 2;
|
|
mesh.position.set(x, yCenter, z);
|
|
} else {
|
|
mesh = new THREE.Mesh(new THREE.BoxGeometry(WALL_THICK * 1.3, h, len), mat);
|
|
const z = D / 2 - (off + len / 2);
|
|
const x = op.wall === "east" ? W / 2 : -W / 2;
|
|
mesh.position.set(x, yCenter, z);
|
|
}
|
|
group.add(mesh);
|
|
});
|
|
}
|
|
|
|
function buildZone(floor, zone, pinMeshes) {
|
|
const g = new THREE.Group();
|
|
const [wx, wz] = toWorldXZ(floor, zone.x + zone.w / 2, zone.y + zone.d / 2);
|
|
g.position.set(wx, 0.02, wz);
|
|
|
|
const color = ZONE_COLORS[zone.kind] || ZONE_COLORS.other;
|
|
const pad = new THREE.Mesh(
|
|
new THREE.BoxGeometry(Math.max(0.8, zone.w - 0.12), 0.04, Math.max(0.8, zone.d - 0.12)),
|
|
new THREE.MeshStandardMaterial({ color, roughness: 0.9 })
|
|
);
|
|
pad.receiveShadow = true;
|
|
pad.userData = { zoneSurface: true, zoneLabel: zone.label || zone.kind, photoIds: zone.evidence_photos || [] };
|
|
g.add(pad);
|
|
|
|
// Place furniture groups inside the zone
|
|
(zone.furniture || []).forEach((fg) => {
|
|
populateFurniture(g, zone.w, zone.d, fg);
|
|
});
|
|
|
|
// If this zone has evidence photos, place a glowing interactive pin above it
|
|
if (zone.evidence_photos && zone.evidence_photos.length > 0) {
|
|
const pin = buildPin(zone.label || zone.kind, zone.evidence_photos);
|
|
pin.position.set(0, 1.65, 0);
|
|
pin.userData.baseY = 1.65;
|
|
pin.userData.floorLevel = floor.level;
|
|
g.add(pin);
|
|
pinMeshes.push(pin);
|
|
}
|
|
return g;
|
|
}
|
|
|
|
function populateFurniture(zoneGroup, zw, zd, fg) {
|
|
const count = Math.min(24, Math.max(1, fg.count || 1));
|
|
const cols = Math.max(1, Math.ceil(Math.sqrt(count * (zw / Math.max(1, zd)))));
|
|
const rows = Math.max(1, Math.ceil(count / cols));
|
|
const cellW = (zw * 0.82) / cols;
|
|
const cellD = (zd * 0.82) / rows;
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
const r = Math.floor(i / cols);
|
|
const c = i % cols;
|
|
const x = -zw * 0.41 + (c + 0.5) * cellW;
|
|
const z = -zd * 0.41 + (r + 0.5) * cellD;
|
|
const item = buildFurnitureMesh(fg.kind, cellW, cellD);
|
|
item.position.set(x, 0.02, z);
|
|
zoneGroup.add(item);
|
|
}
|
|
}
|
|
|
|
function buildFurnitureMesh(kind, cellW, cellD) {
|
|
const g = new THREE.Group();
|
|
const wood = new THREE.MeshStandardMaterial({ color: 0x8b5e3c, roughness: 0.7 });
|
|
const darkWood = new THREE.MeshStandardMaterial({ color: 0x5c3d28, roughness: 0.75 });
|
|
const chairMat = new THREE.MeshStandardMaterial({ color: 0x3d405b, roughness: 0.8 });
|
|
const sofaMat = new THREE.MeshStandardMaterial({ color: 0x6b705c, roughness: 0.85 });
|
|
const metal = new THREE.MeshStandardMaterial({ color: 0xadb5bd, metalness: 0.5, roughness: 0.4 });
|
|
const leaf = new THREE.MeshStandardMaterial({ color: 0x52b788, roughness: 0.8 });
|
|
|
|
if (kind === "counter_bar") {
|
|
const w = Math.min(2.6, Math.max(1.2, cellW * 0.9));
|
|
const d = Math.min(1.0, Math.max(0.65, cellD * 0.6));
|
|
const body = new THREE.Mesh(new THREE.BoxGeometry(w, 0.95, d), darkWood);
|
|
body.position.y = 0.475;
|
|
body.castShadow = true;
|
|
g.add(body);
|
|
const machine = new THREE.Mesh(new THREE.BoxGeometry(0.45, 0.35, 0.35), metal);
|
|
machine.position.set(-w * 0.2, 1.12, 0);
|
|
g.add(machine);
|
|
return g;
|
|
}
|
|
|
|
if (kind === "bar_seat") {
|
|
const stool = new THREE.Mesh(new THREE.CylinderGeometry(0.2, 0.2, 0.68, 12), chairMat);
|
|
stool.position.y = 0.34;
|
|
stool.castShadow = true;
|
|
g.add(stool);
|
|
return g;
|
|
}
|
|
|
|
if (kind === "sofa") {
|
|
const seat = new THREE.Mesh(new THREE.BoxGeometry(1.3, 0.42, 0.65), sofaMat);
|
|
seat.position.y = 0.21;
|
|
seat.castShadow = true;
|
|
const back = new THREE.Mesh(new THREE.BoxGeometry(1.3, 0.45, 0.16), sofaMat);
|
|
back.position.set(0, 0.55, -0.24);
|
|
g.add(seat, back);
|
|
return g;
|
|
}
|
|
|
|
if (kind === "plant") {
|
|
const pot = new THREE.Mesh(new THREE.CylinderGeometry(0.22, 0.16, 0.36, 12), darkWood);
|
|
pot.position.y = 0.18;
|
|
const crown = new THREE.Mesh(new THREE.SphereGeometry(0.36, 12, 10), leaf);
|
|
crown.position.y = 0.62;
|
|
crown.castShadow = true;
|
|
g.add(pot, crown);
|
|
return g;
|
|
}
|
|
|
|
// Tables (table2, table4, table6, communal_table)
|
|
const isLarge = kind === "table6" || kind === "communal_table";
|
|
const isFour = kind === "table4";
|
|
const tw = isLarge ? 1.5 : isFour ? 0.95 : 0.68;
|
|
const td = isLarge ? 0.8 : 0.68;
|
|
const top = new THREE.Mesh(new THREE.BoxGeometry(tw, 0.06, td), wood);
|
|
top.position.y = 0.72;
|
|
top.castShadow = true;
|
|
const leg = new THREE.Mesh(new THREE.CylinderGeometry(0.06, 0.08, 0.7, 10), metal);
|
|
leg.position.y = 0.35;
|
|
g.add(top, leg);
|
|
|
|
const chairOffsets = isLarge
|
|
? [
|
|
[-0.45, -0.52],
|
|
[0, -0.52],
|
|
[0.45, -0.52],
|
|
[-0.45, 0.52],
|
|
[0, 0.52],
|
|
[0.45, 0.52],
|
|
]
|
|
: isFour
|
|
? [
|
|
[-0.24, -0.48],
|
|
[0.24, -0.48],
|
|
[-0.24, 0.48],
|
|
[0.24, 0.48],
|
|
]
|
|
: [
|
|
[0, -0.46],
|
|
[0, 0.46],
|
|
];
|
|
|
|
chairOffsets.forEach(([cx, cz]) => {
|
|
const ch = new THREE.Mesh(new THREE.BoxGeometry(0.32, 0.44, 0.32), chairMat);
|
|
ch.position.set(cx, 0.22, cz);
|
|
ch.castShadow = true;
|
|
g.add(ch);
|
|
});
|
|
return g;
|
|
}
|
|
|
|
function buildVertical(floor, v) {
|
|
const g = new THREE.Group();
|
|
const [wx, wz] = toWorldXZ(floor, v.x + v.w / 2, v.y + v.d / 2);
|
|
g.position.set(wx, 0, wz);
|
|
const mat = new THREE.MeshStandardMaterial({ color: 0xadb5bd, roughness: 0.6 });
|
|
|
|
if (v.kind === "elevator") {
|
|
const box = new THREE.Mesh(new THREE.BoxGeometry(v.w, WALL_HEIGHT, v.d), mat);
|
|
box.position.y = WALL_HEIGHT / 2;
|
|
g.add(box);
|
|
} else {
|
|
const steps = 8;
|
|
for (let i = 0; i < steps; i++) {
|
|
const sh = ((i + 1) / steps) * (WALL_HEIGHT * 0.85);
|
|
const step = new THREE.Mesh(new THREE.BoxGeometry(v.w, sh, v.d / steps), mat);
|
|
step.position.set(0, sh / 2, -v.d / 2 + ((i + 0.5) * v.d) / steps);
|
|
step.castShadow = true;
|
|
g.add(step);
|
|
}
|
|
}
|
|
return g;
|
|
}
|
|
|
|
function buildPin(zoneLabel, photoIds) {
|
|
const g = new THREE.Group();
|
|
g.userData = { zoneLabel, photoIds };
|
|
const headMat = new THREE.MeshStandardMaterial({
|
|
color: SCENE_COLORS.accent,
|
|
emissive: SCENE_COLORS.accent,
|
|
emissiveIntensity: 0.18,
|
|
roughness: 0.3,
|
|
});
|
|
const head = new THREE.Mesh(new THREE.OctahedronGeometry(0.24, 0), headMat);
|
|
head.position.y = 0.2;
|
|
const stem = new THREE.Mesh(
|
|
new THREE.CylinderGeometry(0.03, 0.03, 0.35, 8),
|
|
new THREE.MeshBasicMaterial({ color: SCENE_COLORS.accent })
|
|
);
|
|
g.add(head, stem);
|
|
return g;
|
|
}
|