-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathcamera.js
90 lines (79 loc) · 2.59 KB
/
camera.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
import * as THREE from 'three';
import { registerComponent } from '../core/component.js';
/**
* Camera component.
* Pairs along with camera system to handle tracking the active camera.
*/
export var Component = registerComponent('camera', {
schema: {
active: {default: true},
far: {default: 10000},
fov: {default: 80, min: 0},
near: {default: 0.005, min: 0},
spectator: {default: false},
zoom: {default: 1, min: 0}
},
/**
* Initialize three.js camera and add it to the entity.
* Add reference from scene to this entity as the camera.
*/
init: function () {
var camera;
var el = this.el;
// Create camera.
camera = this.camera = new THREE.PerspectiveCamera();
el.setObject3D('camera', camera);
},
/**
* Update three.js camera.
*/
update: function (oldData) {
var data = this.data;
var camera = this.camera;
// Update properties.
camera.aspect = data.aspect || (window.innerWidth / window.innerHeight);
camera.far = data.far;
camera.fov = data.fov;
camera.near = data.near;
camera.zoom = data.zoom;
camera.updateProjectionMatrix();
this.updateActiveCamera(oldData);
this.updateSpectatorCamera(oldData);
},
updateActiveCamera: function (oldData) {
var data = this.data;
var el = this.el;
var system = this.system;
// Active property did not change.
if (oldData && oldData.active === data.active || data.spectator) { return; }
// If `active` property changes, or first update, handle active camera with system.
if (data.active && system.activeCameraEl !== el) {
// Camera enabled. Set camera to this camera.
system.setActiveCamera(el);
} else if (!data.active && system.activeCameraEl === el) {
// Camera disabled. Set camera to another camera.
system.disableActiveCamera();
}
},
updateSpectatorCamera: function (oldData) {
var data = this.data;
var el = this.el;
var system = this.system;
// spectator property did not change.
if (oldData && oldData.spectator === data.spectator) { return; }
// If `spectator` property changes, or first update, handle spectator camera with system.
if (data.spectator && system.spectatorCameraEl !== el) {
// Camera enabled. Set camera to this camera.
system.setSpectatorCamera(el);
} else if (!data.spectator && system.spectatorCameraEl === el) {
// Camera disabled. Set camera to another camera.
system.disableSpectatorCamera();
}
},
/**
* Remove camera on remove (callback).
*/
remove: function () {
this.el.removeObject3D('camera');
}
});