| 1 | // Vista «Ciudad 3D» — el proyecto abierto renderizado con el MOTOR REAL de |
| 2 | // VibeCodeViewer (Sendery/VibeCodeViewer, Apache-2.0), vendorizado en js/vcc. |
| 3 | // Aquí solo hacemos de glue: escaneamos la carpeta abierta al árbol empaquetado |
| 4 | // que espera buildModel, montamos el render (cámara + OrbitControls + bloom) y |
| 5 | // conectamos el raycast → abrir fichero en Monaco. La geometría (ciudad anidada |
| 6 | // distrito→edificio→planta→…, cristal fresnel, fachadas con el árbol impreso, |
| 7 | // suelo de circuito, leyenda) es la del builder original, sin tocar. |
| 8 | import * as THREE from 'three'; |
| 9 | import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 10 | import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; |
| 11 | import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'; |
| 12 | import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js'; |
| 13 | import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; |
| 14 | import * as code from './tools/code.js'; |
| 15 | import { buildModel } from './vcc/model.js'; |
| 16 | import { buildCity } from './vcc/builder.js'; |
| 17 | import { THEME } from './vcc/theme.js'; |
| 18 | |
| 19 | const IGNORE = new Set(['node_modules', '.git', 'dist', 'build', 'target', '__pycache__', '.next', 'venv', '.venv', '.DS_Store']); |
| 20 | const MAX_FILES = 8000; // tope de seguridad para no colgar el navegador en monorepos |
| 21 | |
| 22 | let disposer = null; |
| 23 | |
| 24 | // Escanea el handle del proyecto al formato empaquetado de VibeCodeViewer: |
| 25 | // dir = [nombre, [subdirs], [ficheros]] · fichero = [nombre, bytes] |
| 26 | async function scanTree(dir, budget) { |
| 27 | const dirs = [], files = []; |
| 28 | for await (const e of dir.values()) { |
| 29 | if (IGNORE.has(e.name)) continue; |
| 30 | (e.kind === 'directory' ? dirs : files).push(e); |
| 31 | } |
| 32 | dirs.sort((a, b) => a.name.localeCompare(b.name)); |
| 33 | files.sort((a, b) => a.name.localeCompare(b.name)); |
| 34 | const outFiles = []; |
| 35 | for (const f of files) { |
| 36 | if (budget.n >= MAX_FILES) break; |
| 37 | let size = 0; try { size = (await f.getFile()).size; } catch { /* ilegible */ } |
| 38 | outFiles.push([f.name, size]); budget.n++; |
| 39 | } |
| 40 | const outDirs = []; |
| 41 | for (const d of dirs) { if (budget.n >= MAX_FILES) break; outDirs.push(await scanTree(d, budget)); } |
| 42 | return [dir.name, outDirs, outFiles]; |
| 43 | } |
| 44 | |
| 45 | export async function renderCity(container, onOpenFile) { |
| 46 | disposeCity(); |
| 47 | const root = code.handle(); |
| 48 | if (!root) { container.innerHTML = '<div class="view-loading">Abre un proyecto primero.</div>'; return; } |
| 49 | container.innerHTML = '<div class="view-loading">Construyendo la metrópolis del proyecto…</div>'; |
| 50 | |
| 51 | const budget = { n: 0 }; |
| 52 | const tree = await scanTree(root, budget); |
| 53 | const model = buildModel(tree, null); |
| 54 | if (!model.files.length) { container.innerHTML = '<div class="view-loading">Proyecto vacío.</div>'; return; } |
| 55 | |
| 56 | container.innerHTML = |
| 57 | '<canvas id="city-canvas"></canvas>' + |
| 58 | '<div class="view-head">Ciudad 3D · ' + model.files.length + (budget.n >= MAX_FILES ? '+' : '') + |
| 59 | ' ficheros en ' + model.districts.length + ' distritos' + |
| 60 | '<span class="view-hint">arrastra para orbitar · rueda para zoom · clic en un edificio → abrir</span></div>'; |
| 61 | const canvas = container.querySelector('#city-canvas'); |
| 62 | const W = container.clientWidth || 800, H = container.clientHeight || 600; |
| 63 | |
| 64 | const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, powerPreference: 'high-performance' }); |
| 65 | renderer.setPixelRatio(Math.min(devicePixelRatio, 2)); |
| 66 | renderer.setSize(W, H, false); |
| 67 | renderer.toneMapping = THREE.ACESFilmicToneMapping; |
| 68 | renderer.toneMappingExposure = 1.0; |
| 69 | renderer.setClearColor(THEME.background, 1); |
| 70 | |
| 71 | const scene = new THREE.Scene(); |
| 72 | scene.background = new THREE.Color(THEME.background); |
| 73 | const extent = Math.max(model.city.box.sx, model.city.box.sz); |
| 74 | scene.fog = new THREE.FogExp2(THEME.fog, 0.62 / extent); |
| 75 | |
| 76 | const camera = new THREE.PerspectiveCamera(55, W / H, 0.1, extent * 14); |
| 77 | camera.position.set(extent * 0.7, extent * 0.6, extent * 0.7); |
| 78 | |
| 79 | const controls = new OrbitControls(camera, canvas); |
| 80 | controls.enableDamping = true; |
| 81 | controls.dampingFactor = 0.08; |
| 82 | controls.maxPolarAngle = Math.PI * 0.495; // no bajar del suelo |
| 83 | controls.target.set(0, extent * 0.04, 0); |
| 84 | controls.update(); |
| 85 | |
| 86 | // pipeline con bloom (la receta del builder: HDR + bloom solo en lo resaltado) |
| 87 | const composer = new EffectComposer(renderer); |
| 88 | composer.setSize(W, H); |
| 89 | composer.addPass(new RenderPass(scene, camera)); |
| 90 | const bloom = new UnrealBloomPass(new THREE.Vector2(W, H), THEME.bloom.strength, 0.25, THEME.bloom.threshold); |
| 91 | composer.addPass(bloom); |
| 92 | composer.addPass(new OutputPass()); |
| 93 | |
| 94 | const city = buildCity(scene, model, THEME); |
| 95 | |
| 96 | // ── interacción: hover resalta edificios, clic abre el fichero ──────────── |
| 97 | const ray = new THREE.Raycaster(), ptr = new THREE.Vector2(); |
| 98 | let hovered = null, lastHover = 0, downXY = null; |
| 99 | const setPtr = e => { const r = canvas.getBoundingClientRect(); ptr.set(((e.clientX - r.left) / r.width) * 2 - 1, -((e.clientY - r.top) / r.height) * 2 + 1); }; |
| 100 | const onMove = e => { |
| 101 | setPtr(e); |
| 102 | const now = performance.now(); if (now - lastHover < 50) return; lastHover = now; |
| 103 | ray.setFromCamera(ptr, camera); |
| 104 | const b = ray.intersectObjects(city.shells)[0]?.object.userData.building ?? null; |
| 105 | if (b !== hovered) { |
| 106 | if (hovered) city.paintNode(hovered, 0); |
| 107 | hovered = b; |
| 108 | if (b) city.paintNode(b, 0.16); |
| 109 | canvas.style.cursor = b ? 'pointer' : 'grab'; |
| 110 | } |
| 111 | }; |
| 112 | const onDown = e => { downXY = [e.clientX, e.clientY]; }; |
| 113 | const onUp = e => { |
| 114 | if (!downXY) return; |
| 115 | const moved = Math.hypot(e.clientX - downXY[0], e.clientY - downXY[1]); downXY = null; |
| 116 | if (moved > 5) return; // fue un arrastre de cámara |
| 117 | setPtr(e); ray.setFromCamera(ptr, camera); |
| 118 | const fileHit = ray.intersectObject(city.filesMesh)[0]; |
| 119 | const shellHit = ray.intersectObjects(city.shells)[0]; |
| 120 | if (fileHit && (!shellHit || fileHit.distance < shellHit.distance + 2)) { |
| 121 | const f = model.files[fileHit.instanceId]; |
| 122 | if (f) onOpenFile(f.rel); |
| 123 | } else if (shellHit) { |
| 124 | // clic en un edificio (carpeta): centra la cámara en él |
| 125 | const b = shellHit.object.userData.building; |
| 126 | controls.target.set(b.box.cx, b.box.cy, b.box.cz); |
| 127 | } |
| 128 | }; |
| 129 | canvas.addEventListener('pointermove', onMove); |
| 130 | canvas.addEventListener('pointerdown', onDown); |
| 131 | canvas.addEventListener('pointerup', onUp); |
| 132 | |
| 133 | const onResize = () => { |
| 134 | const w = container.clientWidth || W, h = container.clientHeight || H; |
| 135 | renderer.setSize(w, h, false); composer.setSize(w, h); bloom.resolution.set(w, h); |
| 136 | camera.aspect = w / h; camera.updateProjectionMatrix(); |
| 137 | }; |
| 138 | window.addEventListener('resize', onResize); |
| 139 | |
| 140 | let raf = null; |
| 141 | const loop = () => { raf = requestAnimationFrame(loop); controls.update(); composer.render(); }; |
| 142 | loop(); |
| 143 | |
| 144 | disposer = () => { |
| 145 | if (raf) cancelAnimationFrame(raf); |
| 146 | window.removeEventListener('resize', onResize); |
| 147 | canvas.removeEventListener('pointermove', onMove); |
| 148 | canvas.removeEventListener('pointerdown', onDown); |
| 149 | canvas.removeEventListener('pointerup', onUp); |
| 150 | controls.dispose(); |
| 151 | city.dispose(); |
| 152 | composer.dispose?.(); |
| 153 | renderer.dispose(); |
| 154 | }; |
| 155 | } |
| 156 | |
| 157 | export function disposeCity() { if (disposer) { disposer(); disposer = null; } } |