File size: 2,183 Bytes
7ed0c3d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import * as THREE from 'three';
import {predictedCameraView} from './camera-follow.js';
import {linspaceIndices} from './point-sampling.js';
export class CameraLayer {
constructor(description, reference) {
this.group = new THREE.Group();
this.cameras = [];
const samples = linspaceIndices(description.frames.length, 72);
const aspect = description.image_size[0] / description.image_size[1];
const corners = [[-aspect, -1, 2.5], [aspect, -1, 2.5], [aspect, 1, 2.5], [-aspect, 1, 2.5]];
const vertices = [];
corners.forEach((corner, index) => vertices.push(0, 0, 0, ...corner, ...corner, ...corners[(index + 1) % corners.length]));
this.geometry = new THREE.BufferGeometry();
this.geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
for (const frame of samples) {
const pose = predictedCameraView(description.frames[frame].camera_to_world, reference);
if (!pose) continue;
const color = new THREE.Color().setHSL(.76 - .3 * frame / Math.max(1, description.frames.length - 1), .65, .58);
const material = new THREE.LineBasicMaterial({color, transparent: true, opacity: .65, depthWrite: false});
const frustum = new THREE.LineSegments(this.geometry, material);
frustum.position.copy(pose.position);
frustum.quaternion.setFromRotationMatrix(new THREE.Matrix4().lookAt(pose.position, pose.position.clone().sub(pose.forward), pose.up));
frustum.frustumCulled = false;
this.cameras.push({frame, frustum});
this.group.add(frustum);
}
}
update(camera, frame, following, enabled) {
this.group.visible = enabled;
if (!enabled) return;
const direction = camera.getWorldDirection(new THREE.Vector3());
const offset = new THREE.Vector3();
for (const record of this.cameras) {
const depth = offset.copy(record.frustum.position).sub(camera.position).dot(direction);
record.frustum.visible = depth > camera.near * 10 && (!following || record.frame !== frame);
record.frustum.scale.setScalar(Math.min(.025, Math.max(0, depth) * .008));
record.frustum.material.opacity = record.frame === frame ? .85 : .4;
}
}
}
|