| 1 |
import * as THREE from "three"; |
| 2 |
import {OrbitControls} from "three/addons/controls/OrbitControls.js"; |
| 3 |
import {LineMaterial} from "three/addons/lines/LineMaterial.js"; |
| 4 |
import {LineSegments2} from "three/addons/lines/LineSegments2.js"; |
| 5 |
import {LineSegmentsGeometry} from "three/addons/lines/LineSegmentsGeometry.js"; |
| 6 |
|
| 7 |
import {median, percentile, projectionCenter, quatRotate} from "./math"; |
| 8 |
import { |
| 9 |
findPoint3DIndex, |
| 10 |
INVALID_POINT3D_ID, |
| 11 |
point2DCount, |
| 12 |
point3DAt, |
| 13 |
point3DCount, |
| 14 |
point3DTrackLength, |
| 15 |
} from "./types"; |
| 16 |
import type {Camera, ImageRecord, Point3D, Reconstruction, Vec3} from "./types"; |
| 17 |
|
| 18 |
export const CAMERA_FRUSTUM_COLORS = { |
| 19 |
frame: [0.8, 0.1, 0, 1], |
| 20 |
plane: [1, 0.1, 0, 0.6], |
| 21 |
selectedFrame: [0.8, 0, 0.8, 1], |
| 22 |
selectedPlane: [1, 0, 1, 0.6], |
| 23 |
sameFrame: [0.6, 0, 0.6, 179 / 255], |
| 24 |
sameFramePlane: [0.8, 0, 0.8, 77 / 255], |
| 25 |
} as const; |
| 26 |
|
| 27 |
export const COORDINATE_COLORS = { |
| 28 |
grid: [51 / 255, 51 / 255, 51 / 255, 153 / 255], |
| 29 |
x: [230 / 255, 0, 0, 128 / 255], |
| 30 |
y: [0, 230 / 255, 0, 128 / 255], |
| 31 |
z: [0, 0, 230 / 255, 128 / 255], |
| 32 |
} as const; |
| 33 |
|
| 34 |
const COLORS = { |
| 35 |
selectedPoint: new THREE.Color(0, 1, 0), |
| 36 |
selectedCameraPlane: new THREE.Color(1, 0, 1), |
| 37 |
pointConnection: new THREE.Color(0, 1, 0), |
| 38 |
imageConnection: new THREE.Color(0.8, 0, 0.8), |
| 39 |
}; |
| 40 |
|
| 41 |
type Selection = {type: "point"; value: Point3D} | {type: "image"; value: ImageRecord} | null; |
| 42 |
type InternalSelection = {type: "point"; index: number} | {type: "image"; value: ImageRecord} | null; |
| 43 |
|
| 44 |
export interface ViewerSettings { |
| 45 |
pointSize: number; |
| 46 |
cameraSize: number; |
| 47 |
minTrackLength: number; |
| 48 |
maxError: number; |
| 49 |
showConnections: boolean; |
| 50 |
projection: "perspective" | "orthographic"; |
| 51 |
} |
| 52 |
|
| 53 |
function toThreeQuaternion(rotation: [number, number, number, number]): THREE.Quaternion { |
| 54 |
return new THREE.Quaternion(rotation[1], rotation[2], rotation[3], rotation[0]).invert(); |
| 55 |
} |
| 56 |
|
| 57 |
function pushRgba(target: number[], color: readonly [number, number, number, number], count = 1): void { |
| 58 |
for (let i = 0; i < count; ++i) target.push(...color); |
| 59 |
} |
| 60 |
|
| 61 |
function vertexRgbaMaterial(side: THREE.Side = THREE.FrontSide, depthWrite = true): THREE.ShaderMaterial { |
| 62 |
return new THREE.ShaderMaterial({ |
| 63 |
vertexShader: "attribute vec4 color; varying vec4 vColor; void main(){vColor=color; gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.0);}", |
| 64 |
fragmentShader: "varying vec4 vColor; void main(){gl_FragColor=vColor;}", |
| 65 |
transparent: true, |
| 66 |
depthWrite, |
| 67 |
side, |
| 68 |
toneMapped: false, |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
function cameraRgbaMaterial(cameraSize: number, side: THREE.Side = THREE.FrontSide, depthWrite = true): THREE.ShaderMaterial { |
| 73 |
return new THREE.ShaderMaterial({ |
| 74 |
uniforms: {cameraSize: {value: cameraSize}}, |
| 75 |
vertexShader: "attribute vec3 cameraCenter; attribute vec4 color; varying vec4 vColor; uniform float cameraSize; void main(){vColor=color; vec3 p=cameraCenter+(position-cameraCenter)*cameraSize; gl_Position=projectionMatrix*modelViewMatrix*vec4(p,1.0);}", |
| 76 |
fragmentShader: "varying vec4 vColor; void main(){gl_FragColor=vColor;}", |
| 77 |
transparent: true, |
| 78 |
depthWrite, |
| 79 |
side, |
| 80 |
toneMapped: false, |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
function cameraPickingMaterial(cameraSize: number): THREE.ShaderMaterial { |
| 85 |
return new THREE.ShaderMaterial({ |
| 86 |
uniforms: {cameraSize: {value: cameraSize}}, |
| 87 |
vertexShader: "attribute vec3 cameraCenter; attribute vec3 color; varying vec3 vColor; uniform float cameraSize; void main(){vColor=color; vec3 p=cameraCenter+(position-cameraCenter)*cameraSize; gl_Position=projectionMatrix*modelViewMatrix*vec4(p,1.0);}", |
| 88 |
fragmentShader: "varying vec3 vColor; void main(){gl_FragColor=vec4(vColor,1.0);}", |
| 89 |
side: THREE.DoubleSide, |
| 90 |
toneMapped: false, |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
function pushVector(target: number[], vector: THREE.Vector3, count: number): void { |
| 95 |
for (let i = 0; i < count; ++i) target.push(vector.x, vector.y, vector.z); |
| 96 |
} |
| 97 |
|
| 98 |
function coordinateLines(colors: ReadonlyArray<readonly [number, number, number, number]>): THREE.LineSegments { |
| 99 |
const geometry = new THREE.BufferGeometry(); |
| 100 |
geometry.setAttribute("position", new THREE.Float32BufferAttribute(new Float32Array(colors.length * 2 * 3), 3)); |
| 101 |
const vertexColors: number[] = []; |
| 102 |
for (const color of colors) pushRgba(vertexColors, color, 2); |
| 103 |
geometry.setAttribute("color", new THREE.Float32BufferAttribute(vertexColors, 4)); |
| 104 |
return new THREE.LineSegments(geometry, vertexRgbaMaterial()); |
| 105 |
} |
| 106 |
|
| 107 |
function thickCoordinateAxes(): LineSegments2 { |
| 108 |
const geometry = new LineSegmentsGeometry(); |
| 109 |
geometry.setPositions(new Float32Array(18)); |
| 110 |
geometry.setColors([ |
| 111 |
...COORDINATE_COLORS.x.slice(0, 3), ...COORDINATE_COLORS.x.slice(0, 3), |
| 112 |
...COORDINATE_COLORS.y.slice(0, 3), ...COORDINATE_COLORS.y.slice(0, 3), |
| 113 |
...COORDINATE_COLORS.z.slice(0, 3), ...COORDINATE_COLORS.z.slice(0, 3), |
| 114 |
]); |
| 115 |
const material = new LineMaterial({transparent: true, opacity: 128 / 255}); |
| 116 |
material.linewidth = 2; |
| 117 |
material.vertexColors = true; |
| 118 |
const axes = new LineSegments2(geometry, material); |
| 119 |
axes.frustumCulled = false; |
| 120 |
return axes; |
| 121 |
} |
| 122 |
|
| 123 |
export function scaleFromNativeWheel(value: number, deltaY: number, deltaMode: number, minimum: number, maximum: number): number { |
| 124 |
const unitScale = deltaMode === 1 ? 40 : deltaMode === 2 ? 100 : 1; |
| 125 |
const nativeDelta = -deltaY * unitScale; |
| 126 |
const factor = Math.max(0.01, 1 + nativeDelta / 100 * 0.1); |
| 127 |
return THREE.MathUtils.clamp(value * factor, minimum, maximum); |
| 128 |
} |
| 129 |
|
| 130 |
export function photometricPointColor(rgb: readonly [number, number, number]): THREE.Color { |
| 131 |
return new THREE.Color().setRGB(rgb[0] / 255, rgb[1] / 255, rgb[2] / 255, THREE.SRGBColorSpace); |
| 132 |
} |
| 133 |
|
| 134 |
function disposeObject(object: THREE.Object3D): void { |
| 135 |
object.traverse((child) => { |
| 136 |
const renderable = child as THREE.Mesh; |
| 137 |
renderable.geometry?.dispose(); |
| 138 |
const material = renderable.material; |
| 139 |
if (Array.isArray(material)) material.forEach((item) => item.dispose()); |
| 140 |
else material?.dispose(); |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
export class ReconstructionViewer { |
| 145 |
readonly settings: ViewerSettings = { |
| 146 |
pointSize: 2, |
| 147 |
cameraSize: 0.025, |
| 148 |
minTrackLength: 3, |
| 149 |
maxError: 2, |
| 150 |
showConnections: false, |
| 151 |
projection: "perspective", |
| 152 |
}; |
| 153 |
|
| 154 |
onSelection: (selection: Selection) => void = () => undefined; |
| 155 |
onError: (error: Error) => void = () => undefined; |
| 156 |
onContextChange: (contextLost: boolean) => void = () => undefined; |
| 157 |
onSettingsChange: (settings: Readonly<ViewerSettings>) => void = () => undefined; |
| 158 |
|
| 159 |
private readonly renderer: THREE.WebGLRenderer; |
| 160 |
private readonly scene = new THREE.Scene(); |
| 161 |
private readonly pickingScene = new THREE.Scene(); |
| 162 |
private readonly perspectiveCamera = new THREE.PerspectiveCamera(25, 1, 0.0001, 1e6); |
| 163 |
private readonly orthographicCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.0001, 1e6); |
| 164 |
private activeCamera: THREE.PerspectiveCamera | THREE.OrthographicCamera = this.perspectiveCamera; |
| 165 |
private controls: OrbitControls; |
| 166 |
private readonly renderTarget = new THREE.WebGLRenderTarget(1, 1, {depthBuffer: true, stencilBuffer: false}); |
| 167 |
private readonly content = new THREE.Group(); |
| 168 |
private readonly pickingContent = new THREE.Group(); |
| 169 |
private readonly pointContent = new THREE.Group(); |
| 170 |
private readonly cameraContent = new THREE.Group(); |
| 171 |
private readonly connectionContent = new THREE.Group(); |
| 172 |
private readonly pointPickingContent = new THREE.Group(); |
| 173 |
private readonly cameraPickingContent = new THREE.Group(); |
| 174 |
private readonly coordinateGrid = coordinateLines([COORDINATE_COLORS.grid, COORDINATE_COLORS.grid, COORDINATE_COLORS.grid]); |
| 175 |
private readonly coordinateAxes = thickCoordinateAxes(); |
| 176 |
private readonly resizeObserver: ResizeObserver; |
| 177 |
private reconstruction: Reconstruction | null = null; |
| 178 |
private pointsObject: THREE.Points | null = null; |
| 179 |
private pickPointsObject: THREE.Points | null = null; |
| 180 |
private pointDrawList = new Uint32Array(); |
| 181 |
private imageDrawList: ImageRecord[] = []; |
| 182 |
private center: Vec3 = [0, 0, 0]; |
| 183 |
private scale = 1; |
| 184 |
private viewCenter = new THREE.Vector3(); |
| 185 |
private viewRadius = 1; |
| 186 |
private coordinateOrigin = new THREE.Vector3(); |
| 187 |
private selection: InternalSelection = null; |
| 188 |
private animationFrame = 0; |
| 189 |
private contextLost = false; |
| 190 |
private disposed = false; |
| 191 |
|
| 192 |
constructor(private readonly canvas: HTMLCanvasElement) { |
| 193 |
this.renderer = new THREE.WebGLRenderer({canvas, antialias: true, preserveDrawingBuffer: false}); |
| 194 |
this.renderer.setClearColor(0xffffff, 1); |
| 195 |
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); |
| 196 |
this.coordinateGrid.visible = false; |
| 197 |
this.coordinateAxes.visible = false; |
| 198 |
this.coordinateGrid.renderOrder = -2; |
| 199 |
this.coordinateAxes.renderOrder = -1; |
| 200 |
this.content.add(this.pointContent, this.cameraContent, this.connectionContent); |
| 201 |
this.pickingContent.add(this.pointPickingContent, this.cameraPickingContent); |
| 202 |
this.scene.add(this.coordinateGrid, this.coordinateAxes, this.content); |
| 203 |
this.pickingScene.add(this.pickingContent); |
| 204 |
this.perspectiveCamera.position.set(1.8, -1.8, 1.8); |
| 205 |
this.perspectiveCamera.up.set(0, -1, 0); |
| 206 |
this.orthographicCamera.up.copy(this.perspectiveCamera.up); |
| 207 |
this.controls = this.createControls(this.activeCamera); |
| 208 |
this.resizeObserver = new ResizeObserver(() => this.resize()); |
| 209 |
this.resizeObserver.observe(canvas.parentElement ?? canvas); |
| 210 |
canvas.addEventListener("dblclick", (event) => this.pick(event)); |
| 211 |
canvas.addEventListener("contextmenu", (event) => event.preventDefault()); |
| 212 |
canvas.addEventListener("wheel", (event) => this.handleModifiedWheel(event), {passive: false, capture: true}); |
| 213 |
canvas.addEventListener("webglcontextlost", this.handleContextLost); |
| 214 |
canvas.addEventListener("webglcontextrestored", this.handleContextRestored); |
| 215 |
this.resize(); |
| 216 |
this.startAnimation(); |
| 217 |
} |
| 218 |
|
| 219 |
private createControls(camera: THREE.Camera): OrbitControls { |
| 220 |
this.controls?.dispose(); |
| 221 |
const controls = new OrbitControls(camera, this.canvas); |
| 222 |
controls.enableDamping = true; |
| 223 |
controls.dampingFactor = 0.08; |
| 224 |
controls.screenSpacePanning = true; |
| 225 |
controls.mouseButtons.LEFT = THREE.MOUSE.ROTATE; |
| 226 |
controls.mouseButtons.RIGHT = THREE.MOUSE.PAN; |
| 227 |
return controls; |
| 228 |
} |
| 229 |
|
| 230 |
private startAnimation(): void { |
| 231 |
if (this.animationFrame === 0 && !this.contextLost && !this.disposed) this.animationFrame = requestAnimationFrame(this.animate); |
| 232 |
} |
| 233 |
|
| 234 |
private animate = (): void => { |
| 235 |
this.animationFrame = 0; |
| 236 |
if (this.contextLost || this.disposed) return; |
| 237 |
this.controls.update(); |
| 238 |
this.updateCoordinateOverlays(); |
| 239 |
try { |
| 240 |
this.renderer.render(this.scene, this.activeCamera); |
| 241 |
this.startAnimation(); |
| 242 |
} catch (error) { |
| 243 |
this.onError(error instanceof Error ? error : new Error(String(error))); |
| 244 |
} |
| 245 |
}; |
| 246 |
|
| 247 |
private handleContextLost = (event: Event): void => { |
| 248 |
event.preventDefault(); |
| 249 |
this.contextLost = true; |
| 250 |
cancelAnimationFrame(this.animationFrame); |
| 251 |
this.animationFrame = 0; |
| 252 |
if (!this.disposed) this.onContextChange(true); |
| 253 |
}; |
| 254 |
|
| 255 |
private handleContextRestored = (): void => { |
| 256 |
if (this.disposed) return; |
| 257 |
this.contextLost = false; |
| 258 |
this.renderer.resetState(); |
| 259 |
this.resize(); |
| 260 |
this.onContextChange(false); |
| 261 |
this.startAnimation(); |
| 262 |
}; |
| 263 |
|
| 264 |
dispose(): void { |
| 265 |
if (this.disposed) return; |
| 266 |
this.disposed = true; |
| 267 |
cancelAnimationFrame(this.animationFrame); |
| 268 |
this.animationFrame = 0; |
| 269 |
this.canvas.removeEventListener("webglcontextlost", this.handleContextLost); |
| 270 |
this.canvas.removeEventListener("webglcontextrestored", this.handleContextRestored); |
| 271 |
this.resizeObserver.disconnect(); |
| 272 |
this.controls.dispose(); |
| 273 |
this.clearGroups(); |
| 274 |
disposeObject(this.coordinateGrid); |
| 275 |
disposeObject(this.coordinateAxes); |
| 276 |
this.renderTarget.dispose(); |
| 277 |
this.renderer.dispose(); |
| 278 |
} |
| 279 |
|
| 280 |
setReconstruction(reconstruction: Reconstruction): void { |
| 281 |
this.reconstruction = reconstruction; |
| 282 |
this.selection = null; |
| 283 |
const centers = [...reconstruction.images.values()].map((image) => projectionCenter(image.camFromWorld)); |
| 284 |
const xs = centers.map((center) => center[0]); |
| 285 |
const ys = centers.map((center) => center[1]); |
| 286 |
const zs = centers.map((center) => center[2]); |
| 287 |
this.center = [median(xs), median(ys), median(zs)]; |
| 288 |
const extent = Math.max( |
| 289 |
percentile(xs, 0.95) - percentile(xs, 0.05), |
| 290 |
percentile(ys, 0.95) - percentile(ys, 0.05), |
| 291 |
percentile(zs, 0.95) - percentile(zs, 0.05), |
| 292 |
1e-6, |
| 293 |
); |
| 294 |
this.scale = 1 / extent; |
| 295 |
this.coordinateOrigin.copy(this.normalized([0, 0, 0])); |
| 296 |
this.coordinateGrid.visible = true; |
| 297 |
this.coordinateAxes.visible = true; |
| 298 |
this.rebuild(); |
| 299 |
this.computeViewBounds(); |
| 300 |
this.resize(); |
| 301 |
this.resetView(); |
| 302 |
} |
| 303 |
|
| 304 |
clearReconstruction(): void { |
| 305 |
this.reconstruction = null; |
| 306 |
this.selection = null; |
| 307 |
this.clearGroups(); |
| 308 |
this.pointDrawList = new Uint32Array(); |
| 309 |
this.imageDrawList = []; |
| 310 |
this.coordinateGrid.visible = false; |
| 311 |
this.coordinateAxes.visible = false; |
| 312 |
this.onSelection(null); |
| 313 |
} |
| 314 |
|
| 315 |
get visiblePointCount(): number { |
| 316 |
return this.pointDrawList.length; |
| 317 |
} |
| 318 |
|
| 319 |
updateSettings(settings: Partial<ViewerSettings>): void { |
| 320 |
const projectionChanged = settings.projection !== undefined && settings.projection !== this.settings.projection; |
| 321 |
const pointSizeChanged = settings.pointSize !== undefined && settings.pointSize !== this.settings.pointSize; |
| 322 |
const cameraSizeChanged = settings.cameraSize !== undefined && settings.cameraSize !== this.settings.cameraSize; |
| 323 |
const filtersChanged = |
| 324 |
(settings.minTrackLength !== undefined && settings.minTrackLength !== this.settings.minTrackLength) || |
| 325 |
(settings.maxError !== undefined && settings.maxError !== this.settings.maxError); |
| 326 |
const connectionsChanged = settings.showConnections !== undefined && settings.showConnections !== this.settings.showConnections; |
| 327 |
Object.assign(this.settings, settings); |
| 328 |
this.onSettingsChange(this.settings); |
| 329 |
if (projectionChanged) this.switchProjection(); |
| 330 |
if (filtersChanged) { |
| 331 |
this.rebuild(); |
| 332 |
return; |
| 333 |
} |
| 334 |
if (pointSizeChanged) { |
| 335 |
const material = this.pointsObject?.material as THREE.PointsMaterial | undefined; |
| 336 |
if (material) material.size = this.settings.pointSize; |
| 337 |
const pickMaterial = this.pickPointsObject?.material as THREE.ShaderMaterial | undefined; |
| 338 |
if (pickMaterial) pickMaterial.uniforms.pointSize!.value = Math.max(8, this.settings.pointSize * 2); |
| 339 |
} |
| 340 |
if (cameraSizeChanged) this.updateCameraSize(); |
| 341 |
if (connectionsChanged) this.rebuildConnections(); |
| 342 |
} |
| 343 |
|
| 344 |
resetView(): void { |
| 345 |
const distance = Math.max( |
| 346 |
0.5, |
| 347 |
this.viewRadius / Math.tan(THREE.MathUtils.degToRad(this.perspectiveCamera.fov) / 2) * 1.25, |
| 348 |
); |
| 349 |
const direction = new THREE.Vector3(1, -1, 1).normalize(); |
| 350 |
this.activeCamera.position.copy(this.viewCenter).addScaledVector(direction, distance); |
| 351 |
this.controls.target.copy(this.viewCenter); |
| 352 |
this.activeCamera.lookAt(this.viewCenter); |
| 353 |
this.controls.update(); |
| 354 |
this.resize(); |
| 355 |
} |
| 356 |
|
| 357 |
clearSelection(): void { |
| 358 |
this.selection = null; |
| 359 |
this.rebuild(); |
| 360 |
this.onSelection(null); |
| 361 |
} |
| 362 |
|
| 363 |
private normalized(point: Vec3): THREE.Vector3 { |
| 364 |
return new THREE.Vector3( |
| 365 |
(point[0] - this.center[0]) * this.scale, |
| 366 |
(point[1] - this.center[1]) * this.scale, |
| 367 |
(point[2] - this.center[2]) * this.scale, |
| 368 |
); |
| 369 |
} |
| 370 |
|
| 371 |
private normalizedPoint(index: number): THREE.Vector3 { |
| 372 |
const xyz = this.reconstruction!.points3D.xyz; |
| 373 |
const offset = index * 3; |
| 374 |
return this.normalized([xyz[offset]!, xyz[offset + 1]!, xyz[offset + 2]!]); |
| 375 |
} |
| 376 |
|
| 377 |
private worldUnitsPerPixel(): number { |
| 378 |
const height = Math.max(1, this.canvas.clientHeight); |
| 379 |
if (this.activeCamera instanceof THREE.OrthographicCamera) { |
| 380 |
return (this.activeCamera.top - this.activeCamera.bottom) / (this.activeCamera.zoom * height); |
| 381 |
} |
| 382 |
const distance = this.activeCamera.position.distanceTo(this.controls.target); |
| 383 |
return 2 * Math.tan(THREE.MathUtils.degToRad(this.activeCamera.fov) / 2) * distance / height; |
| 384 |
} |
| 385 |
|
| 386 |
private updateCoordinateOverlays(): void { |
| 387 |
if (!this.reconstruction) return; |
| 388 |
const unitsPerPixel = this.worldUnitsPerPixel(); |
| 389 |
const gridExtent = 20 * unitsPerPixel; |
| 390 |
const axesExtent = 50 * unitsPerPixel; |
| 391 |
const target = this.controls.target; |
| 392 |
const gridPositions = this.coordinateGrid.geometry.getAttribute("position") as THREE.BufferAttribute; |
| 393 |
const axesStarts = this.coordinateAxes.geometry.getAttribute("instanceStart") as THREE.InterleavedBufferAttribute; |
| 394 |
const axesEnds = this.coordinateAxes.geometry.getAttribute("instanceEnd") as THREE.InterleavedBufferAttribute; |
| 395 |
for (let axis = 0; axis < 3; ++axis) { |
| 396 |
const negative = target.clone(); |
| 397 |
const positive = target.clone(); |
| 398 |
negative.setComponent(axis, negative.getComponent(axis) - gridExtent); |
| 399 |
positive.setComponent(axis, positive.getComponent(axis) + gridExtent); |
| 400 |
gridPositions.setXYZ(axis * 2, negative.x, negative.y, negative.z); |
| 401 |
gridPositions.setXYZ(axis * 2 + 1, positive.x, positive.y, positive.z); |
| 402 |
|
| 403 |
const endpoint = this.coordinateOrigin.clone(); |
| 404 |
endpoint.setComponent(axis, endpoint.getComponent(axis) + axesExtent); |
| 405 |
axesStarts.setXYZ(axis, this.coordinateOrigin.x, this.coordinateOrigin.y, this.coordinateOrigin.z); |
| 406 |
axesEnds.setXYZ(axis, endpoint.x, endpoint.y, endpoint.z); |
| 407 |
} |
| 408 |
gridPositions.needsUpdate = true; |
| 409 |
axesStarts.data.needsUpdate = true; |
| 410 |
} |
| 411 |
|
| 412 |
private clearGroup(group: THREE.Group): void { |
| 413 |
for (const child of [...group.children]) { |
| 414 |
group.remove(child); |
| 415 |
disposeObject(child); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
private clearGroups(): void { |
| 420 |
for (const group of [this.pointContent, this.cameraContent, this.connectionContent, this.pointPickingContent, this.cameraPickingContent]) { |
| 421 |
this.clearGroup(group); |
| 422 |
} |
| 423 |
this.pointsObject = null; |
| 424 |
this.pickPointsObject = null; |
| 425 |
} |
| 426 |
|
| 427 |
private rebuild(): void { |
| 428 |
if (!this.reconstruction) return; |
| 429 |
this.clearGroups(); |
| 430 |
this.buildPoints(); |
| 431 |
this.buildCameras(); |
| 432 |
this.buildConnections(); |
| 433 |
} |
| 434 |
|
| 435 |
private rebuildConnections(): void { |
| 436 |
if (!this.reconstruction) return; |
| 437 |
this.clearGroup(this.connectionContent); |
| 438 |
this.buildConnections(); |
| 439 |
} |
| 440 |
|
| 441 |
private updateCameraSize(): void { |
| 442 |
for (const group of [this.cameraContent, this.cameraPickingContent]) { |
| 443 |
group.traverse((object) => { |
| 444 |
const material = (object as THREE.Mesh).material; |
| 445 |
const materials = Array.isArray(material) ? material : [material]; |
| 446 |
for (const item of materials) { |
| 447 |
if (item instanceof THREE.ShaderMaterial && item.uniforms.cameraSize) { |
| 448 |
item.uniforms.cameraSize.value = this.settings.cameraSize; |
| 449 |
} |
| 450 |
} |
| 451 |
}); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
private computeViewBounds(): void { |
| 456 |
if (!this.reconstruction) return; |
| 457 |
const axes: [number[], number[], number[]] = [[], [], []]; |
| 458 |
const append = (point: THREE.Vector3): void => { |
| 459 |
axes[0].push(point.x); |
| 460 |
axes[1].push(point.y); |
| 461 |
axes[2].push(point.z); |
| 462 |
}; |
| 463 |
const stride = Math.max(1, Math.floor(this.pointDrawList.length / 100000)); |
| 464 |
for (let i = 0; i < this.pointDrawList.length; i += stride) { |
| 465 |
const pointIndex = this.pointDrawList[i]!; |
| 466 |
const offset = pointIndex * 3; |
| 467 |
append(this.normalized([ |
| 468 |
this.reconstruction.points3D.xyz[offset]!, |
| 469 |
this.reconstruction.points3D.xyz[offset + 1]!, |
| 470 |
this.reconstruction.points3D.xyz[offset + 2]!, |
| 471 |
])); |
| 472 |
} |
| 473 |
for (const image of this.reconstruction.images.values()) append(this.normalized(projectionCenter(image.camFromWorld))); |
| 474 |
if (axes[0].length === 0) { |
| 475 |
this.viewCenter.set(0, 0, 0); |
| 476 |
this.viewRadius = 1; |
| 477 |
return; |
| 478 |
} |
| 479 |
const minimum = new THREE.Vector3(...axes.map((values) => percentile(values, 0.01)) as Vec3); |
| 480 |
const maximum = new THREE.Vector3(...axes.map((values) => percentile(values, 0.99)) as Vec3); |
| 481 |
this.viewCenter.copy(minimum).add(maximum).multiplyScalar(0.5); |
| 482 |
this.viewRadius = Math.max(0.05, minimum.distanceTo(maximum) * 0.5); |
| 483 |
} |
| 484 |
|
| 485 |
private buildPoints(): void { |
| 486 |
const reconstruction = this.reconstruction!; |
| 487 |
const selectedImage = this.selection?.type === "image" ? this.selection.value : null; |
| 488 |
const observed = new Set<bigint>(); |
| 489 |
if (selectedImage) { |
| 490 |
for (const point3DId of selectedImage.points2D.point3DIds) { |
| 491 |
if (point3DId !== INVALID_POINT3D_ID) observed.add(point3DId); |
| 492 |
} |
| 493 |
} |
| 494 |
const points = reconstruction.points3D; |
| 495 |
let visibleCount = 0; |
| 496 |
for (let pointIndex = 0; pointIndex < point3DCount(points); ++pointIndex) { |
| 497 |
if (points.errors[pointIndex]! <= this.settings.maxError && point3DTrackLength(points, pointIndex) >= this.settings.minTrackLength) { |
| 498 |
++visibleCount; |
| 499 |
} |
| 500 |
} |
| 501 |
this.pointDrawList = new Uint32Array(visibleCount); |
| 502 |
const positions = new Float32Array(visibleCount * 3); |
| 503 |
const colors = new Float32Array(visibleCount * 3); |
| 504 |
const pickColors = new Float32Array(visibleCount * 3); |
| 505 |
const color = new THREE.Color(); |
| 506 |
let drawIndex = 0; |
| 507 |
for (let pointIndex = 0; pointIndex < point3DCount(points); ++pointIndex) { |
| 508 |
if (points.errors[pointIndex]! > this.settings.maxError || point3DTrackLength(points, pointIndex) < this.settings.minTrackLength) continue; |
| 509 |
this.pointDrawList[drawIndex] = pointIndex; |
| 510 |
const sourceOffset = pointIndex * 3; |
| 511 |
const targetOffset = drawIndex * 3; |
| 512 |
positions[targetOffset] = (points.xyz[sourceOffset]! - this.center[0]) * this.scale; |
| 513 |
positions[targetOffset + 1] = (points.xyz[sourceOffset + 1]! - this.center[1]) * this.scale; |
| 514 |
positions[targetOffset + 2] = (points.xyz[sourceOffset + 2]! - this.center[2]) * this.scale; |
| 515 |
color.setRGB( |
| 516 |
points.colors[sourceOffset]! / 255, |
| 517 |
points.colors[sourceOffset + 1]! / 255, |
| 518 |
points.colors[sourceOffset + 2]! / 255, |
| 519 |
THREE.SRGBColorSpace, |
| 520 |
); |
| 521 |
if (this.selection?.type === "point" && this.selection.index === pointIndex) color.copy(COLORS.selectedPoint); |
| 522 |
else if (observed.has(points.ids[pointIndex]!)) color.copy(COLORS.selectedCameraPlane); |
| 523 |
colors[targetOffset] = color.r; |
| 524 |
colors[targetOffset + 1] = color.g; |
| 525 |
colors[targetOffset + 2] = color.b; |
| 526 |
const pickIndex = drawIndex + 1; |
| 527 |
pickColors[targetOffset] = (pickIndex & 255) / 255; |
| 528 |
pickColors[targetOffset + 1] = ((pickIndex >> 8) & 255) / 255; |
| 529 |
pickColors[targetOffset + 2] = ((pickIndex >> 16) & 255) / 255; |
| 530 |
++drawIndex; |
| 531 |
} |
| 532 |
const geometry = new THREE.BufferGeometry(); |
| 533 |
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3)); |
| 534 |
geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3)); |
| 535 |
this.pointsObject = new THREE.Points(geometry, new THREE.PointsMaterial({size: this.settings.pointSize, sizeAttenuation: false, vertexColors: true})); |
| 536 |
this.pointContent.add(this.pointsObject); |
| 537 |
|
| 538 |
const pickGeometry = new THREE.BufferGeometry(); |
| 539 |
pickGeometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3)); |
| 540 |
pickGeometry.setAttribute("pickColor", new THREE.Float32BufferAttribute(pickColors, 3)); |
| 541 |
const pickMaterial = new THREE.ShaderMaterial({ |
| 542 |
uniforms: {pointSize: {value: Math.max(8, this.settings.pointSize * 2)}}, |
| 543 |
vertexShader: "attribute vec3 pickColor; varying vec3 vColor; uniform float pointSize; void main(){vColor=pickColor; gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.0); gl_PointSize=pointSize;}", |
| 544 |
fragmentShader: "varying vec3 vColor; void main(){if(length(gl_PointCoord-vec2(0.5))>0.5) discard; gl_FragColor=vec4(vColor,1.0);}", |
| 545 |
toneMapped: false, |
| 546 |
}); |
| 547 |
this.pickPointsObject = new THREE.Points(pickGeometry, pickMaterial); |
| 548 |
this.pointPickingContent.add(this.pickPointsObject); |
| 549 |
} |
| 550 |
|
| 551 |
private cameraGeometry(image: ImageRecord, camera: Camera): {center: THREE.Vector3; corners: THREE.Vector3[]} { |
| 552 |
const center = this.normalized(projectionCenter(image.camFromWorld)); |
| 553 |
const quaternion = toThreeQuaternion(image.camFromWorld.rotation); |
| 554 |
const aspectWidth = camera.width / Math.max(camera.width, camera.height); |
| 555 |
const aspectHeight = camera.height / Math.max(camera.width, camera.height); |
| 556 |
const focal = (camera.params[0] ?? Math.max(camera.width, camera.height)) / Math.max(camera.width, camera.height); |
| 557 |
const halfWidth = aspectWidth * 0.5; |
| 558 |
const halfHeight = aspectHeight * 0.5; |
| 559 |
const depth = Math.max(focal, 0.25); |
| 560 |
const corners = [ |
| 561 |
new THREE.Vector3(-halfWidth, -halfHeight, depth), |
| 562 |
new THREE.Vector3(halfWidth, -halfHeight, depth), |
| 563 |
new THREE.Vector3(halfWidth, halfHeight, depth), |
| 564 |
new THREE.Vector3(-halfWidth, halfHeight, depth), |
| 565 |
].map((corner) => corner.applyQuaternion(quaternion).add(center)); |
| 566 |
return {center, corners}; |
| 567 |
} |
| 568 |
|
| 569 |
private sphericalCameraGeometry(image: ImageRecord): {center: THREE.Vector3; lines: THREE.Vector3[]; triangles: THREE.Vector3[]} { |
| 570 |
const center = this.normalized(projectionCenter(image.camFromWorld)); |
| 571 |
const quaternion = toThreeQuaternion(image.camFromWorld.rotation); |
| 572 |
const radius = 0.55; |
| 573 |
const worldPoint = (point: THREE.Vector3): THREE.Vector3 => point.applyQuaternion(quaternion).add(center); |
| 574 |
const lines: THREE.Vector3[] = []; |
| 575 |
const segments = 24; |
| 576 |
for (let plane = 0; plane < 3; ++plane) { |
| 577 |
for (let i = 0; i < segments; ++i) { |
| 578 |
const angles = [i / segments * Math.PI * 2, (i + 1) / segments * Math.PI * 2]; |
| 579 |
for (const angle of angles) { |
| 580 |
const a = Math.cos(angle) * radius; |
| 581 |
const b = Math.sin(angle) * radius; |
| 582 |
const local = plane === 0 ? new THREE.Vector3(a, b, 0) : plane === 1 ? new THREE.Vector3(a, 0, b) : new THREE.Vector3(0, a, b); |
| 583 |
lines.push(worldPoint(local)); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
const vertices = [ |
| 588 |
new THREE.Vector3(radius, 0, 0), new THREE.Vector3(-radius, 0, 0), |
| 589 |
new THREE.Vector3(0, radius, 0), new THREE.Vector3(0, -radius, 0), |
| 590 |
new THREE.Vector3(0, 0, radius), new THREE.Vector3(0, 0, -radius), |
| 591 |
]; |
| 592 |
const faces = [ |
| 593 |
0, 2, 4, 2, 1, 4, 1, 3, 4, 3, 0, 4, |
| 594 |
2, 0, 5, 1, 2, 5, 3, 1, 5, 0, 3, 5, |
| 595 |
]; |
| 596 |
return {center, lines, triangles: faces.map((index) => worldPoint(vertices[index]!.clone()))}; |
| 597 |
} |
| 598 |
|
| 599 |
private buildCameras(): void { |
| 600 |
const reconstruction = this.reconstruction!; |
| 601 |
this.imageDrawList = [...reconstruction.images.values()]; |
| 602 |
const linePositions: number[] = []; |
| 603 |
const lineColors: number[] = []; |
| 604 |
const lineCenters: number[] = []; |
| 605 |
const planePositions: number[] = []; |
| 606 |
const planeColors: number[] = []; |
| 607 |
const planeCenters: number[] = []; |
| 608 |
const pickPositions: number[] = []; |
| 609 |
const pickColors: number[] = []; |
| 610 |
const pickCenters: number[] = []; |
| 611 |
const selectedFrame = this.selection?.type === "image" ? this.selection.value.frameId : -1; |
| 612 |
|
| 613 |
for (let imageIndex = 0; imageIndex < this.imageDrawList.length; ++imageIndex) { |
| 614 |
const image = this.imageDrawList[imageIndex]!; |
| 615 |
const camera = reconstruction.cameras.get(image.cameraId)!; |
| 616 |
const selected = this.selection?.type === "image" && this.selection.value.id === image.id; |
| 617 |
const sameFrame = !selected && image.frameId === selectedFrame; |
| 618 |
const lineColor = selected ? CAMERA_FRUSTUM_COLORS.selectedFrame : sameFrame ? CAMERA_FRUSTUM_COLORS.sameFrame : CAMERA_FRUSTUM_COLORS.frame; |
| 619 |
const planeColor = selected ? CAMERA_FRUSTUM_COLORS.selectedPlane : sameFrame ? CAMERA_FRUSTUM_COLORS.sameFramePlane : CAMERA_FRUSTUM_COLORS.plane; |
| 620 |
const index = this.pointDrawList.length + imageIndex + 1; |
| 621 |
if (camera.modelId === 17) { |
| 622 |
const sphere = this.sphericalCameraGeometry(image); |
| 623 |
for (const vertex of sphere.lines) linePositions.push(...vertex.toArray()); |
| 624 |
pushRgba(lineColors, lineColor, sphere.lines.length); |
| 625 |
pushVector(lineCenters, sphere.center, sphere.lines.length); |
| 626 |
for (const vertex of sphere.triangles) { |
| 627 |
planePositions.push(...vertex.toArray()); |
| 628 |
pickPositions.push(...vertex.toArray()); |
| 629 |
} |
| 630 |
pushRgba(planeColors, planeColor, sphere.triangles.length); |
| 631 |
pushVector(planeCenters, sphere.center, sphere.triangles.length); |
| 632 |
pushVector(pickCenters, sphere.center, sphere.triangles.length); |
| 633 |
for (let i = 0; i < sphere.triangles.length; ++i) pickColors.push((index & 255) / 255, ((index >> 8) & 255) / 255, ((index >> 16) & 255) / 255); |
| 634 |
continue; |
| 635 |
} |
| 636 |
const {center, corners} = this.cameraGeometry(image, camera); |
| 637 |
for (let i = 0; i < 4; ++i) { |
| 638 |
linePositions.push(...center.toArray(), ...corners[i]!.toArray()); |
| 639 |
linePositions.push(...corners[i]!.toArray(), ...corners[(i + 1) % 4]!.toArray()); |
| 640 |
pushRgba(lineColors, lineColor, 4); |
| 641 |
pushVector(lineCenters, center, 4); |
| 642 |
} |
| 643 |
const triangles = [corners[0]!, corners[1]!, corners[2]!, corners[0]!, corners[2]!, corners[3]!]; |
| 644 |
for (const vertex of triangles) planePositions.push(...vertex.toArray()); |
| 645 |
pushRgba(planeColors, planeColor, 6); |
| 646 |
pushVector(planeCenters, center, 6); |
| 647 |
for (const vertex of triangles) pickPositions.push(...vertex.toArray()); |
| 648 |
pushVector(pickCenters, center, 6); |
| 649 |
for (let i = 0; i < 6; ++i) pickColors.push((index & 255) / 255, ((index >> 8) & 255) / 255, ((index >> 16) & 255) / 255); |
| 650 |
} |
| 651 |
const lineGeometry = new THREE.BufferGeometry(); |
| 652 |
lineGeometry.setAttribute("position", new THREE.Float32BufferAttribute(linePositions, 3)); |
| 653 |
lineGeometry.setAttribute("color", new THREE.Float32BufferAttribute(lineColors, 4)); |
| 654 |
lineGeometry.setAttribute("cameraCenter", new THREE.Float32BufferAttribute(lineCenters, 3)); |
| 655 |
this.cameraContent.add(new THREE.LineSegments(lineGeometry, cameraRgbaMaterial(this.settings.cameraSize))); |
| 656 |
|
| 657 |
const planeGeometry = new THREE.BufferGeometry(); |
| 658 |
planeGeometry.setAttribute("position", new THREE.Float32BufferAttribute(planePositions, 3)); |
| 659 |
planeGeometry.setAttribute("color", new THREE.Float32BufferAttribute(planeColors, 4)); |
| 660 |
planeGeometry.setAttribute("cameraCenter", new THREE.Float32BufferAttribute(planeCenters, 3)); |
| 661 |
this.cameraContent.add(new THREE.Mesh(planeGeometry, cameraRgbaMaterial(this.settings.cameraSize, THREE.DoubleSide, false))); |
| 662 |
|
| 663 |
const pickGeometry = new THREE.BufferGeometry(); |
| 664 |
pickGeometry.setAttribute("position", new THREE.Float32BufferAttribute(pickPositions, 3)); |
| 665 |
pickGeometry.setAttribute("color", new THREE.Float32BufferAttribute(pickColors, 3)); |
| 666 |
pickGeometry.setAttribute("cameraCenter", new THREE.Float32BufferAttribute(pickCenters, 3)); |
| 667 |
this.cameraPickingContent.add(new THREE.Mesh(pickGeometry, cameraPickingMaterial(this.settings.cameraSize))); |
| 668 |
} |
| 669 |
|
| 670 |
private buildConnections(): void { |
| 671 |
if (!this.reconstruction || (!this.selection && !this.settings.showConnections)) return; |
| 672 |
const points = this.reconstruction.points3D; |
| 673 |
const positions: number[] = []; |
| 674 |
let color = COLORS.pointConnection; |
| 675 |
if (!this.selection) { |
| 676 |
color = COLORS.imageConnection; |
| 677 |
const pairs = new Set<string>(); |
| 678 |
for (const image of this.reconstruction.images.values()) { |
| 679 |
const from = this.normalized(projectionCenter(image.camFromWorld)); |
| 680 |
for (let observationIndex = 0; observationIndex < point2DCount(image); ++observationIndex) { |
| 681 |
const point3DId = image.points2D.point3DIds[observationIndex]!; |
| 682 |
if (point3DId === INVALID_POINT3D_ID) continue; |
| 683 |
const pointIndex = findPoint3DIndex(points, point3DId); |
| 684 |
if (pointIndex < 0) continue; |
| 685 |
for (let trackIndex = points.trackOffsets[pointIndex]!; trackIndex < points.trackOffsets[pointIndex + 1]!; ++trackIndex) { |
| 686 |
const trackImageId = points.trackImageIds[trackIndex]!; |
| 687 |
if (trackImageId === image.id) continue; |
| 688 |
const a = Math.min(image.id, trackImageId); |
| 689 |
const b = Math.max(image.id, trackImageId); |
| 690 |
const key = `${a}:${b}`; |
| 691 |
if (pairs.has(key)) continue; |
| 692 |
const connected = this.reconstruction.images.get(trackImageId); |
| 693 |
if (!connected) continue; |
| 694 |
pairs.add(key); |
| 695 |
positions.push(...from.toArray(), ...this.normalized(projectionCenter(connected.camFromWorld)).toArray()); |
| 696 |
} |
| 697 |
} |
| 698 |
} |
| 699 |
} else if (this.selection.type === "point") { |
| 700 |
const pointPosition = this.normalizedPoint(this.selection.index); |
| 701 |
for (let trackIndex = points.trackOffsets[this.selection.index]!; trackIndex < points.trackOffsets[this.selection.index + 1]!; ++trackIndex) { |
| 702 |
const image = this.reconstruction.images.get(points.trackImageIds[trackIndex]!); |
| 703 |
if (image) positions.push(...pointPosition.toArray(), ...this.normalized(projectionCenter(image.camFromWorld)).toArray()); |
| 704 |
} |
| 705 |
} else { |
| 706 |
color = COLORS.imageConnection; |
| 707 |
const selectedCenter = this.normalized(projectionCenter(this.selection.value.camFromWorld)); |
| 708 |
const connected = new Set<number>(); |
| 709 |
for (const point3DId of this.selection.value.points2D.point3DIds) { |
| 710 |
if (point3DId === INVALID_POINT3D_ID) continue; |
| 711 |
const pointIndex = findPoint3DIndex(points, point3DId); |
| 712 |
if (pointIndex < 0) continue; |
| 713 |
for (let trackIndex = points.trackOffsets[pointIndex]!; trackIndex < points.trackOffsets[pointIndex + 1]!; ++trackIndex) { |
| 714 |
connected.add(points.trackImageIds[trackIndex]!); |
| 715 |
} |
| 716 |
} |
| 717 |
connected.delete(this.selection.value.id); |
| 718 |
for (const imageId of connected) { |
| 719 |
const image = this.reconstruction.images.get(imageId); |
| 720 |
if (image) positions.push(...selectedCenter.toArray(), ...this.normalized(projectionCenter(image.camFromWorld)).toArray()); |
| 721 |
} |
| 722 |
} |
| 723 |
if (positions.length === 0) return; |
| 724 |
const geometry = new THREE.BufferGeometry(); |
| 725 |
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3)); |
| 726 |
this.connectionContent.add(new THREE.LineSegments(geometry, new THREE.LineBasicMaterial({color, transparent: true, opacity: 0.8}))); |
| 727 |
} |
| 728 |
|
| 729 |
private pick(event: MouseEvent): void { |
| 730 |
if (!this.reconstruction || this.pointDrawList.length + this.imageDrawList.length === 0) return; |
| 731 |
const bounds = this.canvas.getBoundingClientRect(); |
| 732 |
const x = Math.floor(event.clientX - bounds.left); |
| 733 |
const y = Math.floor(event.clientY - bounds.top); |
| 734 |
const width = Math.max(1, Math.floor(bounds.width)); |
| 735 |
const height = Math.max(1, Math.floor(bounds.height)); |
| 736 |
this.activeCamera.setViewOffset(width, height, x, y, 1, 1); |
| 737 |
this.renderer.setRenderTarget(this.renderTarget); |
| 738 |
this.renderer.setClearColor(0x000000, 1); |
| 739 |
this.renderer.clear(); |
| 740 |
this.renderer.render(this.pickingScene, this.activeCamera); |
| 741 |
const pixel = new Uint8Array(4); |
| 742 |
this.renderer.readRenderTargetPixels(this.renderTarget, 0, 0, 1, 1, pixel); |
| 743 |
this.renderer.setRenderTarget(null); |
| 744 |
this.renderer.setClearColor(0xffffff, 1); |
| 745 |
this.activeCamera.clearViewOffset(); |
| 746 |
const index = pixel[0]! + (pixel[1]! << 8) + (pixel[2]! << 16); |
| 747 |
if (index > 0 && index <= this.pointDrawList.length) { |
| 748 |
this.selection = {type: "point", index: this.pointDrawList[index - 1]!}; |
| 749 |
} else { |
| 750 |
const image = this.imageDrawList[index - this.pointDrawList.length - 1]; |
| 751 |
this.selection = image ? {type: "image", value: image} : null; |
| 752 |
} |
| 753 |
this.rebuild(); |
| 754 |
if (this.selection?.type === "point") { |
| 755 |
this.onSelection({type: "point", value: point3DAt(this.reconstruction.points3D, this.selection.index)}); |
| 756 |
} else { |
| 757 |
this.onSelection(this.selection); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
private switchProjection(): void { |
| 762 |
const previous = this.activeCamera; |
| 763 |
const target = this.controls.target.clone(); |
| 764 |
this.activeCamera = this.settings.projection === "perspective" ? this.perspectiveCamera : this.orthographicCamera; |
| 765 |
this.activeCamera.position.copy(previous.position); |
| 766 |
this.activeCamera.quaternion.copy(previous.quaternion); |
| 767 |
this.activeCamera.up.copy(previous.up); |
| 768 |
this.controls = this.createControls(this.activeCamera); |
| 769 |
this.controls.target.copy(target); |
| 770 |
this.controls.update(); |
| 771 |
this.resize(); |
| 772 |
} |
| 773 |
|
| 774 |
private resize(): void { |
| 775 |
const parent = this.canvas.parentElement; |
| 776 |
if (!parent) return; |
| 777 |
const width = Math.max(1, parent.clientWidth); |
| 778 |
const height = Math.max(1, parent.clientHeight); |
| 779 |
this.renderer.setSize(width, height, false); |
| 780 |
this.perspectiveCamera.aspect = width / height; |
| 781 |
this.perspectiveCamera.updateProjectionMatrix(); |
| 782 |
const distance = Math.max(0.2, this.activeCamera.position.distanceTo(this.controls.target)); |
| 783 |
const extent = Math.tan(THREE.MathUtils.degToRad(this.perspectiveCamera.fov) / 2) * distance; |
| 784 |
this.orthographicCamera.left = -extent * width / height; |
| 785 |
this.orthographicCamera.right = extent * width / height; |
| 786 |
this.orthographicCamera.top = extent; |
| 787 |
this.orthographicCamera.bottom = -extent; |
| 788 |
this.orthographicCamera.updateProjectionMatrix(); |
| 789 |
} |
| 790 |
|
| 791 |
private handleModifiedWheel(event: WheelEvent): void { |
| 792 |
if (!event.ctrlKey && !event.metaKey && !event.altKey) return; |
| 793 |
event.preventDefault(); |
| 794 |
event.stopImmediatePropagation(); |
| 795 |
if (event.ctrlKey || event.metaKey) this.updateSettings({pointSize: scaleFromNativeWheel(this.settings.pointSize, event.deltaY, event.deltaMode, 0.5, 100)}); |
| 796 |
else this.updateSettings({cameraSize: scaleFromNativeWheel(this.settings.cameraSize, event.deltaY, event.deltaMode, 1e-6, 1e3)}); |
| 797 |
} |
| 798 |
} |