forked from GreggJuanEduardoPH/vue-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgl-camera-helper.js
63 lines (61 loc) · 1.88 KB
/
vgl-camera-helper.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
import { CameraHelper, Object3D } from 'three';
import VglObject3d from '../core/vgl-object3d';
import { name } from '../types';
import { validateName } from '../validators';
/**
* This helps with visualizing what a camera contains in its frustum,
* corresponding [THREE.CameraHelper](https://threejs.org/docs/index.html#api/helpers/CameraHelper).
* It visualizes the frustum of a camera using a LineSegments.
*
* Properties of [VglObject3d](../core/vgl-object3d) are also available as mixin.
*/
export default {
mixins: [VglObject3d],
props: {
/** Name of the camera to visualize. */
camera: { type: name, required: true, validator: validateName },
},
data: () => ({
/** If camera specified by the name exists or not. Do not set this data manually. */
exist: false,
}),
computed: {
/**
* The THREE.CameraHelper instance. If any cameras specified by the name, it returns a
* THREE.Object3D instance.
*/
inst() {
if (!this.exist) return new Object3D();
const camera = this.vglNamespace.cameras.get(this.camera);
camera.updateProjectionMatrix();
return new CameraHelper(camera);
},
},
methods: {
/** Update the helper geometry for given camera object. */
update(camera) {
if (!camera) {
this.exist = false;
return;
}
this.exist = true;
camera.updateProjectionMatrix();
this.inst.camera = camera;
this.inst.update();
this.vglObject3d.emit();
},
},
beforeDestroy() {
this.vglNamespace.cameras.unlisten(this.camera, this.update);
},
watch: {
camera: {
handler(newName, oldName) {
if (oldName !== undefined) this.vglNamespace.cameras.unlisten(oldName, this.update);
this.vglNamespace.cameras.listen(newName, this.update);
this.exist = !!this.vglNamespace.cameras.get(newName);
},
immediate: true,
},
},
};