-
Notifications
You must be signed in to change notification settings - Fork 18
/
BoundingBoxClipper.ts
107 lines (90 loc) · 2.16 KB
/
BoundingBoxClipper.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
/*!
* Copyright 2021 Cognite AS
*/
import * as THREE from 'three';
export class BoundingBoxClipper {
private readonly _box: THREE.Box3;
private readonly _clippingPlanes: THREE.Plane[] = [
new THREE.Plane(),
new THREE.Plane(),
new THREE.Plane(),
new THREE.Plane(),
new THREE.Plane(),
new THREE.Plane()
];
constructor(box?: THREE.Box3) {
this._box = box || new THREE.Box3();
this.updatePlanes();
}
set minX(x: number) {
this._box.min.x = x;
this.updatePlanes();
}
get minX(): number {
return this._box.min.x;
}
set minY(y: number) {
this._box.min.y = y;
this.updatePlanes();
}
get minY(): number {
return this._box.min.y;
}
set minZ(z: number) {
this._box.min.z = z;
this.updatePlanes();
}
get minZ(): number {
return this._box.min.z;
}
set maxX(x: number) {
this._box.max.x = x;
this.updatePlanes();
}
get maxX(): number {
return this._box.max.x;
}
set maxY(y: number) {
this._box.max.y = y;
this.updatePlanes();
}
get maxY(): number {
return this._box.max.y;
}
set maxZ(z: number) {
this._box.max.z = z;
this.updatePlanes();
}
get maxZ(): number {
return this._box.max.z;
}
private updatePlanes() {
this._clippingPlanes[0].setFromNormalAndCoplanarPoint(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(this.minX, 0, 0)
);
this._clippingPlanes[1].setFromNormalAndCoplanarPoint(
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(this.maxX, 0, 0)
);
this._clippingPlanes[2].setFromNormalAndCoplanarPoint(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(0, this.minY, 0)
);
this._clippingPlanes[3].setFromNormalAndCoplanarPoint(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(0, this.maxY, 0)
);
this._clippingPlanes[4].setFromNormalAndCoplanarPoint(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(0, 0, this.minZ)
);
this._clippingPlanes[5].setFromNormalAndCoplanarPoint(
new THREE.Vector3(0, 0, -1),
new THREE.Vector3(0, 0, this.maxZ)
);
}
get clippingPlanes(): THREE.Plane[] {
return this._clippingPlanes;
}
}