Segizu's picture
Encuadre del visor y latencias medidas en el Space
09e7903 verified
Raw History Blame Contribute Delete
3.76 kB
// Visor 3D (three.js fijado por versión vía importmap en index.html).
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const COLORES_CAM = [0xff3b30, 0x34c759, 0x0a84ff, 0xffcc00];
let renderer, scene, camera, controls, grupo;
export function iniciarVisor(lienzo) {
renderer = new THREE.WebGLRenderer({ canvas: lienzo, antialias: true, preserveDrawingBuffer: true });
renderer.setPixelRatio(Math.min(2, self.devicePixelRatio || 1));
scene = new THREE.Scene(); scene.background = new THREE.Color(0x10141a);
camera = new THREE.PerspectiveCamera(55, 1, 0.001, 1000);
controls = new OrbitControls(camera, lienzo); controls.enableDamping = true;
const ajustar = () => { const w = lienzo.clientWidth || 800, h = lienzo.clientHeight || 450; renderer.setSize(w, h, false); camera.aspect = w / h; camera.updateProjectionMatrix(); };
new ResizeObserver(ajustar).observe(lienzo); ajustar();
renderer.setAnimationLoop(() => { controls.update(); renderer.render(scene, camera); });
}
// pos/col en marco OpenCV de la cámara 1 → three.js (y arriba, z hacia atrás): (x, −y, −z)
export function mostrarNube(nb, P, { tamPunto = 1.5, colorPorVista = false } = {}) {
if (grupo) { scene.remove(grupo); grupo.traverse((o) => { o.geometry?.dispose(); o.material?.dispose(); }); }
grupo = new THREE.Group();
const n = nb.total, p = new Float32Array(3 * n), c = new Float32Array(3 * n);
for (let i = 0; i < n; i++) {
p[3 * i] = nb.pos[3 * i]; p[3 * i + 1] = -nb.pos[3 * i + 1]; p[3 * i + 2] = -nb.pos[3 * i + 2];
if (colorPorVista) { const k = new THREE.Color(COLORES_CAM[nb.vista[i] % 4]); c[3 * i] = k.r; c[3 * i + 1] = k.g; c[3 * i + 2] = k.b; }
else { c[3 * i] = nb.col[3 * i] / 255; c[3 * i + 1] = nb.col[3 * i + 1] / 255; c[3 * i + 2] = nb.col[3 * i + 2] / 255; }
}
const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.BufferAttribute(p, 3)); g.setAttribute('color', new THREE.BufferAttribute(c, 3));
g.computeBoundingSphere();
const radio = g.boundingSphere.radius || 1;
grupo.add(new THREE.Points(g, new THREE.PointsMaterial({ size: tamPunto, sizeAttenuation: false, vertexColors: true })));
// pirámides de cámara (tamaño relativo a la escena)
const s = radio * 0.08;
P.forEach((cam, i) => {
const K = cam.K, w = 2 * K[2], h = 2 * K[5], Rt = [cam.R[0], cam.R[3], cam.R[6], cam.R[1], cam.R[4], cam.R[7], cam.R[2], cam.R[5], cam.R[8]];
const aMundo = (v) => { const x = Rt[0] * v[0] + Rt[1] * v[1] + Rt[2] * v[2] + cam.C[0], y = Rt[3] * v[0] + Rt[4] * v[1] + Rt[5] * v[2] + cam.C[1], z = Rt[6] * v[0] + Rt[7] * v[1] + Rt[8] * v[2] + cam.C[2]; return new THREE.Vector3(x, -y, -z); };
const esq = [[0, 0], [w, 0], [w, h], [0, h]].map(([u, v]) => aMundo([((u - K[2]) / K[0]) * s, ((v - K[5]) / K[4]) * s, s]));
const o = aMundo([0, 0, 0]), pts = [];
for (let k = 0; k < 4; k++) pts.push(o, esq[k], esq[k], esq[(k + 1) % 4]);
const lg = new THREE.BufferGeometry().setFromPoints(pts);
grupo.add(new THREE.LineSegments(lg, new THREE.LineBasicMaterial({ color: COLORES_CAM[i % 4] })));
});
scene.add(grupo);
// encuadre: mirar al centro de la nube desde detrás de la cámara 1, algo elevado
const caja = new THREE.Box3().setFromBufferAttribute(g.getAttribute('position'));
P.forEach((cam) => caja.expandByPoint(new THREE.Vector3(cam.C[0], -cam.C[1], -cam.C[2])));
const centro = caja.getCenter(new THREE.Vector3()), tam = caja.getSize(new THREE.Vector3()).length();
controls.target.copy(centro); camera.position.set(centro.x + tam * 0.18, centro.y + tam * 0.25, centro.z + tam * 0.62);
camera.near = radio / 1000; camera.far = radio * 100; camera.updateProjectionMatrix(); controls.update();
}