-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathRenderCamera.ts
More file actions
503 lines (384 loc) · 15.1 KB
/
RenderCamera.ts
File metadata and controls
503 lines (384 loc) · 15.1 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
import * as THREE from "three";
import * as Geo from "../geo/Geo";
import { RenderMode } from "./RenderMode";
import { ViewportSize } from "./interfaces/ViewportSize";
import { Camera } from "../geo/Camera";
import { Spatial } from "../geo/Spatial";
import { Transform } from "../geo/Transform";
import { ViewportCoords } from "../geo/ViewportCoords";
import { State } from "../state/State";
import { AnimationFrame } from "../state/interfaces/AnimationFrame";
import { EulerRotation } from "../state/interfaces/EulerRotation";
import { isSpherical } from "../geo/Geo";
import { MathUtils, Vector3 } from "three";
export class RenderCamera {
private _spatial: Spatial;
private _viewportCoords: ViewportCoords;
private _alpha: number;
private _stateTransitionAlpha: number;
private _stateTransitionFov: number;
private _renderMode: RenderMode;
private _zoom: number;
private _frameId: number;
private _size: ViewportSize;
private _camera: Camera;
private _perspective: THREE.PerspectiveCamera;
private _rotation: EulerRotation;
private _changed: boolean;
private _changedForFrame: number;
private _currentImageId: string;
private _previousImageId: string;
private _currentSpherical: boolean;
private _previousSpherical: boolean;
private _state: State;
private _currentBearings: {x: number[][], y: number[][]};
private _previousBearings: {x: number[][], y: number[][]};
private _currentFov: number;
private _previousFov: number;
private _currentCameraUp: Vector3;
private _previousCameraUp: Vector3;
private _currentTransformUp: Vector3;
private _previousTransformUp: Vector3;
private _currentTransformForward: Vector3;
private _previousTransformForward: Vector3;
private _initialFov: number;
constructor(
elementWidth: number,
elementHeight: number,
renderMode: RenderMode) {
this._spatial = new Spatial();
this._viewportCoords = new ViewportCoords();
this._size = { width: elementWidth, height: elementHeight };
this._initialFov = 60;
this._alpha = -1;
this._stateTransitionAlpha = -1;
this._stateTransitionFov = -1;
this._renderMode = renderMode;
this._zoom = 0;
this._frameId = -1;
this._changed = false;
this._changedForFrame = -1;
this._currentImageId = null;
this._previousImageId = null;
this._currentSpherical = false;
this._previousSpherical = false;
this._state = null;
this._currentBearings = {x: [], y: []};
this._previousBearings = {x: [], y: []};
this._currentFov = this._initialFov;
this._previousFov = this._initialFov;
this._camera = new Camera();
this._currentCameraUp = new Vector3();
this._previousCameraUp = new Vector3();
this._currentTransformUp = new Vector3();
this._previousTransformUp = new Vector3();
this._currentTransformForward = new Vector3();
this._previousTransformForward = new Vector3();
this._perspective = new THREE.PerspectiveCamera(
this._initialFov,
this._computeAspect(elementWidth, elementHeight),
1e-1,
1e4);
this._perspective.position.copy(this._camera.position);
this._perspective.up.copy(this._camera.up);
this._perspective.lookAt(this._camera.lookat);
this._perspective.updateMatrixWorld(true);
this._perspective.matrixAutoUpdate = false;
this._rotation = { phi: 0, theta: 0 };
}
public get alpha(): number {
return this._alpha;
}
public get camera(): Camera {
return this._camera;
}
public get changed(): boolean {
return this._frameId === this._changedForFrame;
}
public get frameId(): number {
return this._frameId;
}
public get perspective(): THREE.PerspectiveCamera {
return this._perspective;
}
public get renderMode(): RenderMode {
return this._renderMode;
}
public get rotation(): EulerRotation {
return this._rotation;
}
public get zoom(): number {
return this._zoom;
}
public get size(): ViewportSize {
return this._size;
}
public getTilt(): number {
return 90 - this._spatial.radToDeg(this._rotation.theta);
}
public fovToZoom(fov: number): number {
fov = Math.min(90, Math.max(0, fov));
const currentFov = this._computeCurrentFov(0);
const actualFov = this._alpha === 1 ?
currentFov :
this._interpolateFov(
currentFov,
this._computePreviousFov(0),
this._alpha);
const y0 = Math.tan(actualFov / 2 * Math.PI / 180);
const y1 = Math.tan(fov / 2 * Math.PI / 180);
const zoom = Math.log(y0 / y1) / Math.log(2);
return zoom;
}
public setFrame(frame: AnimationFrame): void {
const state = frame.state;
if (state.state !== this._state) {
this._state = state.state;
if (this._state !== State.Custom) {
this.setRenderMode(this._renderMode);
this.setSize(this._size);
}
if (this._state === State.Earth) {
this._stateTransitionFov = this._zoomedFovToFov(this._perspective.fov, this._zoom);
}
this._changed = true;
}
const currentImageId = state.currentImage.id;
const previousImageId = !!state.previousImage ? state.previousImage.id : null;
if (currentImageId !== this._currentImageId || this._changed) {
this._currentImageId = currentImageId;
this._currentSpherical = isSpherical(state.currentTransform.cameraType);
this._currentBearings = this._computeBearings(state.currentTransform);
this._currentCameraUp.copy(state.currentCamera.up);
this._currentTransformUp.copy(state.currentTransform.upVector());
this._currentTransformForward.copy(
new Vector3().fromArray(
state.currentTransform.unprojectSfM([0, 0], 10))
.sub(
new Vector3().fromArray(
state.currentTransform.
unprojectSfM([0, 0], 0))));
this._changed = true;
}
if (previousImageId !== this._previousImageId) {
this._previousImageId = previousImageId;
this._previousSpherical =
isSpherical(state.previousTransform.cameraType);
this._previousBearings = this._computeBearings(state.previousTransform);
this._previousCameraUp.copy(state.previousCamera.up);
this._previousTransformUp.copy(state.previousTransform.upVector());
this._previousTransformForward.copy(
new Vector3().fromArray(
state.previousTransform.unprojectSfM([0, 0], 10))
.sub(
new Vector3().fromArray(
state.previousTransform.
unprojectSfM([0, 0], 0))));
this._changed = true;
}
const zoom = state.zoom;
if (zoom !== this._zoom) {
this._changed = true;
}
const camera = state.camera;
if (this._changed) {
this._currentFov = this._computeCurrentFov(zoom);
this._previousFov = this._computePreviousFov(zoom);
}
const alpha = state.alpha;
const sta = state.stateTransitionAlpha;
if (this._changed ||
alpha !== this._alpha ||
sta !== this._stateTransitionAlpha) {
this._alpha = alpha;
this._stateTransitionAlpha = sta;
switch (this._state) {
case State.Earth: {
const startFov = this._stateTransitionFov;
const endFov = this._focalToFov(state.camera.focal);
const fov = MathUtils.lerp(startFov, endFov, sta);
this._perspective.fov = this._fovToZoomedFov(fov, zoom);
break;
}
case State.Custom:
break;
default:
this._perspective.fov =
this._interpolateFov(
this._currentFov,
this._previousFov,
this._alpha);
this._changed = true;
break;
}
this._zoom = zoom;
if (this._state !== State.Custom) {
this._perspective.updateProjectionMatrix();
}
}
if (this._camera.diff(camera) > 1e-9) {
this._camera.copy(camera);
this._rotation = this._computeRotation(camera);
this._perspective.up.copy(camera.up);
this._perspective.position.copy(camera.position);
// Workaround for shaking camera
this._perspective.matrixAutoUpdate = true;
this._perspective.lookAt(camera.lookat);
this._perspective.matrixAutoUpdate = false;
this._perspective.updateMatrix();
this._perspective.updateMatrixWorld(false);
this._changed = true;
}
this._setFrameId(frame.id);
}
public setProjectionMatrix(matrix: number[]): void {
this._perspective.fov = this._focalToFov(matrix[5] / 2);
this._perspective.projectionMatrix.fromArray(matrix);
this._perspective.projectionMatrixInverse
.copy(this._perspective.projectionMatrix)
.invert();
this._changed = true;
}
public setRenderMode(renderMode: RenderMode): void {
this._renderMode = renderMode;
if (this._state === State.Custom) {
return;
}
this._perspective.fov = this._computeFov();
this._perspective.updateProjectionMatrix();
this._changed = true;
}
public setSize(size: ViewportSize): void {
this._size = size;
if (this._state === State.Custom) {
return;
}
this._perspective.aspect = this._computeAspect(size.width, size.height);
this._perspective.fov = this._computeFov();
this._perspective.updateProjectionMatrix();
this._changed = true;
}
private _computeAspect(elementWidth: number, elementHeight: number): number {
return elementWidth === 0 ? 0 : elementWidth / elementHeight;
}
private _computeCurrentFov(zoom: number): number {
if (this._perspective.aspect === 0) {
return 0;
}
if (!this._currentImageId) {
return this._initialFov;
}
return this._currentSpherical ?
this._fovToZoomedFov(90, zoom) :
this._computeVerticalBearingFov(
this._currentBearings,
this._renderMode,
zoom,
this.perspective.aspect);
}
private _computeFov(): number {
this._currentFov = this._computeCurrentFov(this._zoom);
this._previousFov = this._computePreviousFov(this._zoom);
return this._interpolateFov(this._currentFov, this._previousFov, this._alpha);
}
private _computePreviousFov(zoom: number): number {
if (this._perspective.aspect === 0) {
return 0;
}
if (!this._currentImageId) {
return this._initialFov;
}
return !this._previousImageId ?
this._currentFov :
this._previousSpherical ?
this._fovToZoomedFov(90, zoom) :
this._computeVerticalBearingFov(
this._previousBearings,
this._renderMode,
zoom,
this.perspective.aspect);
}
private _computeBearings(transform: Transform): {x: number[][], y: number[][]} {
const yVertices = [[0, 0]];
const yDirections = [[1, 0]];
const yPointsPerLine = 25;
const y = Geo.computeBearings(
transform,
yVertices,
yDirections,
yPointsPerLine,
this._viewportCoords);
const xVertices = [[0, 0]];
const xDirections = [[0, 1]];
const xPointsPerLine = 25;
const x = Geo.computeBearings(
transform,
xVertices,
xDirections,
xPointsPerLine,
this._viewportCoords);
return {x: x, y: y};
}
private _computeRotation(camera: Camera): EulerRotation {
let direction: THREE.Vector3 = camera.lookat.clone().sub(camera.position);
let up: THREE.Vector3 = camera.up.clone();
let phi = this._spatial.azimuthal(direction.toArray(), up.toArray());
let theta = Math.PI / 2 - this._spatial.angleToPlane(direction.toArray(), [0, 0, 1]);
return { phi: phi, theta: theta };
}
private _computeVerticalBearingFov(
bearings: {x: number[][], y: number[][]},
renderMode: RenderMode,
zoom: number,
aspect: number): number {
const {_spatial} = this;
const yProjections = bearings.y
.map(b => _spatial.projectToPlane(b, [1, 0, 0]))
.map(p => [p[1], -p[2]]);
const xProjections = bearings.x
.map(b => _spatial.projectToPlane(b, [0, 1, 0]))
.map(p => [p[0], -p[2]]);
const yFovs = yProjections.map(p => 2 * _spatial.radToDeg(Math.abs(Math.atan2(p[0], p[1]))));
const xFovs = xProjections.map(p => 2 * _spatial.radToDeg(Math.abs(Math.atan2(p[0] / aspect, p[1]))));
const fovs = [...yFovs, ...xFovs];
const vFovMin = fovs.length > 0 ? 0.995 * Math.min(...fovs) : 125;
const fovMin = this._fovToZoomedFov(vFovMin, zoom);
const fovMax = this._fovToZoomedFov(125, zoom);
const vFovFill = Math.min(fovMin, fovMax);
if (renderMode === RenderMode.Fill) {
return vFovFill;
}
const vFovLetterbox = Math.max(...fovs);
const hFovLetterbox = aspect * vFovLetterbox;
const fovCoeff = 1.25;
const hFovFill = aspect * vFovFill;
const hFovMax = fovCoeff * hFovFill;
let vFov = vFovLetterbox;
if (hFovLetterbox > hFovMax) {
vFov *= hFovMax / hFovLetterbox;
}
const vFovMax = fovCoeff * vFovFill;
vFov = Math.min(vFov, vFovMax, fovMax);
vFov = Math.max(vFov, vFovFill);
return vFov;
}
private _fovToZoomedFov(fov: number, zoom: number): number {
return fov / Math.pow(2, zoom);
}
private _zoomedFovToFov(zoomedFov: number, zoom: number): number {
return Math.pow(2, zoom) * zoomedFov;
}
private _focalToFov(focal: number): number {
return 2 * Math.atan2(1, 2 * focal) * 180 / Math.PI;
}
private _interpolateFov(v1: number, v2: number, alpha: number): number {
return alpha * v1 + (1 - alpha) * v2;
}
private _setFrameId(frameId: number): void {
this._frameId = frameId;
if (this._changed) {
this._changed = false;
this._changedForFrame = frameId;
}
}
}