forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraGeometry.js
76 lines (67 loc) · 2.31 KB
/
CameraGeometry.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
import * as THREE from 'three';
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localMatrix = new THREE.Matrix4();
class CameraGeometry extends THREE.BufferGeometry {
constructor() {
super();
const boxGeometry = new THREE.BoxBufferGeometry(1, 1, 1);
const positions = new Float32Array(boxGeometry.attributes.position.array.length * 8);
const indices = new Uint16Array(boxGeometry.index.array.length * 8);
const _pushBoxGeometry = m => {
const g = boxGeometry.clone();
g.applyMatrix4(m);
positions.set(g.attributes.position.array, positionIndex);
for (let i = 0; i < g.index.array.length; i++) {
indices[indexIndex + i] = g.index.array[i] + positionIndex/3;
}
positionIndex += g.attributes.position.array.length;
indexIndex += g.index.array.length;
};
const topLeft = new THREE.Vector3(-1, 0.5, -2);
const topRight = new THREE.Vector3(1, 0.5, -2);
const bottomLeft = new THREE.Vector3(-1, -0.5, -2);
const bottomRight = new THREE.Vector3(1, -0.5, -2);
const back = new THREE.Vector3(0, 0, 0);
const _setMatrixBetweenPoints = (m, p1, p2) => {
const quaternion = localQuaternion.setFromRotationMatrix(
localMatrix.lookAt(
p1,
p2,
localVector.set(0, 1, 0)
)
);
const position = localVector.copy(p1)
.add(p2)
.divideScalar(2)
// .add(new THREE.Vector3(0, 2, 0));
const sc = 0.01;
const scale = localVector2.set(sc, sc, p1.distanceTo(p2));
m.compose(position, quaternion, scale);
return m;
};
let positionIndex = 0;
let indexIndex = 0;
[
[topLeft, back],
[topRight, back],
[bottomLeft, back],
[bottomRight, back],
[topLeft, topRight],
[topRight, bottomRight],
[bottomRight, bottomLeft],
[bottomLeft, topLeft],
].forEach(e => {
const [p1, p2] = e;
_pushBoxGeometry(
_setMatrixBetweenPoints(localMatrix, p1, p2)
);
});
this.setAttribute('position', new THREE.BufferAttribute(positions, 3));
this.setIndex(new THREE.BufferAttribute(indices, 1));
}
}
export {
CameraGeometry,
};