This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
/
frustum.ts
134 lines (120 loc) · 5 KB
/
frustum.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
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
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Matrix, Plane } from '.';
/**
* Represents a camera frustum
*/
export class Frustum {
/**
* Gets the planes representing the frustum
* @param transform matrix to be applied to the returned planes
* @returns a new array of 6 Frustum planes computed by the given transformation matrix.
*/
public static GetPlanes(transform: Matrix): Plane[] {
const frustumPlanes = [];
for (let index = 0; index < 6; index++) {
frustumPlanes.push(new Plane(0.0, 0.0, 0.0, 0.0));
}
Frustum.GetPlanesToRef(transform, frustumPlanes);
return frustumPlanes;
}
/**
* Gets the near frustum plane transformed by the transform matrix
* @param transform transformation matrix to be applied to the resulting frustum plane
* @param frustumPlane the resuling frustum plane
*/
public static GetNearPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
const m = transform.m;
frustumPlane.normal.x = m[3] + m[2];
frustumPlane.normal.y = m[7] + m[6];
frustumPlane.normal.z = m[11] + m[10];
frustumPlane.d = m[15] + m[14];
frustumPlane.normalize();
}
/**
* Gets the far frustum plane transformed by the transform matrix
* @param transform transformation matrix to be applied to the resulting frustum plane
* @param frustumPlane the resuling frustum plane
*/
public static GetFarPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
const m = transform.m;
frustumPlane.normal.x = m[3] - m[2];
frustumPlane.normal.y = m[7] - m[6];
frustumPlane.normal.z = m[11] - m[10];
frustumPlane.d = m[15] - m[14];
frustumPlane.normalize();
}
/**
* Gets the left frustum plane transformed by the transform matrix
* @param transform transformation matrix to be applied to the resulting frustum plane
* @param frustumPlane the resuling frustum plane
*/
public static GetLeftPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
const m = transform.m;
frustumPlane.normal.x = m[3] + m[0];
frustumPlane.normal.y = m[7] + m[4];
frustumPlane.normal.z = m[11] + m[8];
frustumPlane.d = m[15] + m[12];
frustumPlane.normalize();
}
/**
* Gets the right frustum plane transformed by the transform matrix
* @param transform transformation matrix to be applied to the resulting frustum plane
* @param frustumPlane the resuling frustum plane
*/
public static GetRightPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
const m = transform.m;
frustumPlane.normal.x = m[3] - m[0];
frustumPlane.normal.y = m[7] - m[4];
frustumPlane.normal.z = m[11] - m[8];
frustumPlane.d = m[15] - m[12];
frustumPlane.normalize();
}
/**
* Gets the top frustum plane transformed by the transform matrix
* @param transform transformation matrix to be applied to the resulting frustum plane
* @param frustumPlane the resuling frustum plane
*/
public static GetTopPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
const m = transform.m;
frustumPlane.normal.x = m[3] - m[1];
frustumPlane.normal.y = m[7] - m[5];
frustumPlane.normal.z = m[11] - m[9];
frustumPlane.d = m[15] - m[13];
frustumPlane.normalize();
}
/**
* Gets the bottom frustum plane transformed by the transform matrix
* @param transform transformation matrix to be applied to the resulting frustum plane
* @param frustumPlane the resuling frustum plane
*/
public static GetBottomPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
const m = transform.m;
frustumPlane.normal.x = m[3] + m[1];
frustumPlane.normal.y = m[7] + m[5];
frustumPlane.normal.z = m[11] + m[9];
frustumPlane.d = m[15] + m[13];
frustumPlane.normalize();
}
/**
* Sets the given array "frustumPlanes" with the 6 Frustum planes computed by the given transformation matrix.
* @param transform transformation matrix to be applied to the resulting frustum planes
* @param frustumPlanes the resuling frustum planes
*/
public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
// Near
Frustum.GetNearPlaneToRef(transform, frustumPlanes[0]);
// Far
Frustum.GetFarPlaneToRef(transform, frustumPlanes[1]);
// Left
Frustum.GetLeftPlaneToRef(transform, frustumPlanes[2]);
// Right
Frustum.GetRightPlaneToRef(transform, frustumPlanes[3]);
// Top
Frustum.GetTopPlaneToRef(transform, frustumPlanes[4]);
// Bottom
Frustum.GetBottomPlaneToRef(transform, frustumPlanes[5]);
}
}