-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
119 lines (97 loc) · 2.59 KB
/
utils.js
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
function getBoundingBox(obj) {
return new THREE.Box3().setFromObject(obj);
}
// function getMesh(model) {
// let mesh = null;
// if (model instanceof THREE.Mesh) {
// mesh = model;
// } else if (model) {
// model.traverse(child => {
// if (child instanceof THREE.Mesh && mesh === null) {
// mesh = child;
// return mesh;
// }
// });
// }
// return mesh;
// }
function getCameraStateFromModel(model, zoomFactor, fov) {
let center;
let position;
let sceneDistance;
if (model) {
const box = getBoundingBox(model);
center = box.getCenter(new THREE.Vector3());
const size = box.getSize(new THREE.Vector3()).length();
sceneDistance =
(zoomFactor * size) /
Math.tan((fov * Math.PI) / 180);
position = new THREE.Vector3();
position.copy(center);
position.z += sceneDistance;
return {
target: center,
position: position
};
}
return null;
}
// function getCameraStateFromMesh(mesh, zoomFactor, fov) {
// let meshCenter;
// let position;
// let sceneDistance;
// if (mesh) {
// const geom = mesh.geometry;
// meshCenter = this.getGeometryCenter(geom);
// sceneDistance =
// (zoomFactor * geom.boundingSphere.radius) /
// Math.tan((fov * Math.PI) / 180);
// position = new THREE.Vector3();
// position.copy(meshCenter);
// position.z += sceneDistance;
// return {
// target: meshCenter,
// position: position
// };
// }
// return null;
// }
// function getGeometryCenter(geometry) {
// let geom;
// if (geometry instanceof THREE.BufferGeometry) {
// geom = new THREE.Geometry().fromBufferGeometry(geometry);
// } else {
// geom = geometry;
// }
// geom.computeBoundingSphere();
// return geom.boundingSphere.center;
// }
function lookToFrustrumSpace(
object,
camera,
worldPosition,
cameraPosition
) {
const cameraDirection = new THREE.Vector3();
camera.getWorldDirection(cameraDirection);
const distance = cameraPosition.distanceTo(new THREE.Vector3(0, 0, 0));
const lookPlane = new THREE.Plane(cameraDirection, distance);
const frustrumDirection = new THREE.Vector3();
lookPlane.projectPoint(worldPosition, frustrumDirection);
object.lookAt(frustrumDirection);
}
function objectToVector3(vec) {
const res = new THREE.Vector3();
res.x = vec.x;
res.y = vec.y;
res.z = vec.z;
return res;
}
function stringifyVec3(vec3) {
return AFRAME.utils.coordinates.stringify(vec3);
}
function waitOneFrame(func, minFrameMS) {
window.setTimeout(() => {
func();
}, minFrameMS);
}