-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Camera.ts
97 lines (77 loc) · 2.57 KB
/
Camera.ts
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
import Object3D from "./Object3D";
import Mat4 from "../math/Mat4";
import Frustum from "./Frustum";
import Plane from "./Plane";
import Vec3 from "~/lib/math/Vec3";
import AABB3D from "~/lib/math/AABB3D";
const jitterOffsets: [number, number][] = [
[-7 / 8, 1 / 8],
[-5 / 8, -5 / 8],
[-1 / 8, -3 / 8],
[3 / 8, -7 / 8],
[5 / 8, -1 / 8],
[7 / 8, 7 / 8],
[1 / 8, 3 / 8],
[-3 / 8, 5 / 8]
];
export default abstract class Camera extends Object3D {
public projectionMatrix: Mat4;
public projectionMatrixInverse: Mat4;
public jitteredProjectionMatrix: Mat4;
public jitteredProjectionMatrixInverse: Mat4;
public matrixWorldInverse: Mat4;
public frustumPlanes: Plane[] = null;
public zoomFactor: number = 1;
protected constructor() {
super();
this.matrixWorldInverse = Mat4.identity();
this.matrixOverwrite = false;
this.frustumPlanes = null;
}
public updateMatrixWorldInverse(): void {
this.matrixWorldInverse = Mat4.inverse(this.matrixWorld);
}
public updateProjectionMatrixInverse(): void {
this.projectionMatrixInverse = Mat4.inverse(this.projectionMatrix);
}
public abstract updateProjectionMatrix(): void;
public updateFrustum(): void {
this.frustumPlanes = Frustum.getPlanes(Mat4.multiply(this.projectionMatrix, this.matrixWorldInverse));
}
public frustumContainsPoint(point: Vec3): boolean {
const planes = this.frustumPlanes;
for (let i = 0; i < 6; i++) {
if (planes[i].distanceToPoint(point) < 0) {
return false;
}
}
return true;
}
public zoom(factor: number): void {
this.zoomFactor = factor;
this.updateProjectionMatrix();
}
public updateJitteredProjectionMatrix(frameIndex: number, width: number, height: number, factor: number = 1): void {
const offsetX = jitterOffsets[frameIndex % jitterOffsets.length][0];
const offsetY = jitterOffsets[frameIndex % jitterOffsets.length][1];
this.jitteredProjectionMatrix = Mat4.copy(this.projectionMatrix);
this.jitteredProjectionMatrix.values[8] = offsetX / width * factor;
this.jitteredProjectionMatrix.values[9] = offsetY / height * factor;
this.jitteredProjectionMatrixInverse = Mat4.inverse(this.jitteredProjectionMatrix);
}
public isFrustumIntersectsBoundingBox(boundingBox: AABB3D): boolean {
const planes = this.frustumPlanes;
for (let i = 0; i < 6; ++i) {
const plane = planes[i];
const point = new Vec3(
plane.x > 0 ? boundingBox.max.x : boundingBox.min.x,
plane.y > 0 ? boundingBox.max.y : boundingBox.min.y,
plane.z > 0 ? boundingBox.max.z : boundingBox.min.z
);
if (plane.distanceToPoint(point) < 0) {
return false;
}
}
return true;
}
}