forked from potree/potree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.js
More file actions
191 lines (142 loc) · 4.19 KB
/
View.js
File metadata and controls
191 lines (142 loc) · 4.19 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
import * as THREE from "../../libs/three.js/build/three.module.js";
export class View{
constructor () {
this.position = new THREE.Vector3(0, 0, 0);
this.yaw = Math.PI / 4;
this._pitch = -Math.PI / 4;
this.radius = 1;
this.maxPitch = Math.PI / 2;
this.minPitch = -Math.PI / 2;
}
clone () {
let c = new View();
c.yaw = this.yaw;
c._pitch = this.pitch;
c.radius = this.radius;
c.maxPitch = this.maxPitch;
c.minPitch = this.minPitch;
return c;
}
get pitch () {
return this._pitch;
}
set pitch (angle) {
this._pitch = Math.max(Math.min(angle, this.maxPitch), this.minPitch);
}
get direction () {
let dir = new THREE.Vector3(0, 1, 0);
dir.applyAxisAngle(new THREE.Vector3(1, 0, 0), this.pitch);
dir.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw);
return dir;
}
set direction (dir) {
//if(dir.x === dir.y){
if(dir.x === 0 && dir.y === 0){
this.pitch = Math.PI / 2 * Math.sign(dir.z);
}else{
let yaw = Math.atan2(dir.y, dir.x) - Math.PI / 2;
let pitch = Math.atan2(dir.z, Math.sqrt(dir.x * dir.x + dir.y * dir.y));
this.yaw = yaw;
this.pitch = pitch;
}
}
lookAt(t){
let V;
if(arguments.length === 1){
V = new THREE.Vector3().subVectors(t, this.position);
}else if(arguments.length === 3){
V = new THREE.Vector3().subVectors(new THREE.Vector3(...arguments), this.position);
}
let radius = V.length();
let dir = V.normalize();
this.radius = radius;
this.direction = dir;
}
getPivot () {
return new THREE.Vector3().addVectors(this.position, this.direction.multiplyScalar(this.radius));
}
getSide () {
let side = new THREE.Vector3(1, 0, 0);
side.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw);
return side;
}
pan (x, y) {
let dir = new THREE.Vector3(0, 1, 0);
dir.applyAxisAngle(new THREE.Vector3(1, 0, 0), this.pitch);
dir.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw);
// let side = new THREE.Vector3(1, 0, 0);
// side.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw);
let side = this.getSide();
let up = side.clone().cross(dir);
let pan = side.multiplyScalar(x).add(up.multiplyScalar(y));
this.position = this.position.add(pan);
// this.target = this.target.add(pan);
}
translate (x, y, z) {
let dir = new THREE.Vector3(0, 1, 0);
dir.applyAxisAngle(new THREE.Vector3(1, 0, 0), this.pitch);
dir.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw);
let side = new THREE.Vector3(1, 0, 0);
side.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw);
let up = side.clone().cross(dir);
let t = side.multiplyScalar(x)
.add(dir.multiplyScalar(y))
.add(up.multiplyScalar(z));
this.position = this.position.add(t);
}
translateWorld (x, y, z) {
this.position.x += x;
this.position.y += y;
this.position.z += z;
}
setView(position, target, duration = 0, callback = null){
let endPosition = null;
if(position instanceof Array){
endPosition = new THREE.Vector3(...position);
}else if(position.x != null){
endPosition = position.clone();
}
let endTarget = null;
if(target instanceof Array){
endTarget = new THREE.Vector3(...target);
}else if(target.x != null){
endTarget = target.clone();
}
const startPosition = this.position.clone();
const startTarget = this.getPivot();
//const endPosition = position.clone();
//const endTarget = target.clone();
let easing = TWEEN.Easing.Quartic.Out;
if(duration === 0){
this.position.copy(endPosition);
this.lookAt(endTarget);
}else{
let value = {x: 0};
let tween = new TWEEN.Tween(value).to({x: 1}, duration);
tween.easing(easing);
//this.tweens.push(tween);
tween.onUpdate(() => {
let t = value.x;
//console.log(t);
const pos = new THREE.Vector3(
(1 - t) * startPosition.x + t * endPosition.x,
(1 - t) * startPosition.y + t * endPosition.y,
(1 - t) * startPosition.z + t * endPosition.z,
);
const target = new THREE.Vector3(
(1 - t) * startTarget.x + t * endTarget.x,
(1 - t) * startTarget.y + t * endTarget.y,
(1 - t) * startTarget.z + t * endTarget.z,
);
this.position.copy(pos);
this.lookAt(target);
});
tween.start();
tween.onComplete(() => {
if(callback){
callback();
}
});
}
}
};