forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfly-camera.js
More file actions
100 lines (84 loc) · 2.7 KB
/
fly-camera.js
File metadata and controls
100 lines (84 loc) · 2.7 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
var FlyCamera = pc.createScript('flyCamera');
FlyCamera.attributes.add('speed', {
type: 'number',
default: 10
});
FlyCamera.attributes.add('fastSpeed', {
type: 'number',
default: 20
});
FlyCamera.attributes.add('mode', {
type: 'number',
default: 0,
enum: [{
"Lock": 0
}, {
"Drag": 1
}]
});
FlyCamera.prototype.initialize = function () {
// Camera euler angle rotation around x and y axes
var eulers = this.entity.getLocalEulerAngles();
this.ex = eulers.x;
this.ey = eulers.y;
this.moved = false;
this.lmbDown = false;
// Disabling the context menu stops the browser displaying a menu when
// you right-click the page
this.app.mouse.disableContextMenu();
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
};
FlyCamera.prototype.update = function (dt) {
// Update the camera's orientation
this.entity.setLocalEulerAngles(this.ex, this.ey, 0);
var app = this.app;
var speed = this.speed;
if (app.keyboard.isPressed(pc.KEY_SHIFT)) {
speed = this.fastSpeed;
}
// Update the camera's position
if (app.keyboard.isPressed(pc.KEY_UP) || app.keyboard.isPressed(pc.KEY_W)) {
this.entity.translateLocal(0, 0, -speed * dt);
} else if (app.keyboard.isPressed(pc.KEY_DOWN) || app.keyboard.isPressed(pc.KEY_S)) {
this.entity.translateLocal(0, 0, speed * dt);
}
if (app.keyboard.isPressed(pc.KEY_LEFT) || app.keyboard.isPressed(pc.KEY_A)) {
this.entity.translateLocal(-speed * dt, 0, 0);
} else if (app.keyboard.isPressed(pc.KEY_RIGHT) || app.keyboard.isPressed(pc.KEY_D)) {
this.entity.translateLocal(speed * dt, 0, 0);
}
};
FlyCamera.prototype.onMouseMove = function (event) {
if (!this.mode) {
if (!pc.Mouse.isPointerLocked())
return;
} else {
if (!this.lmbDown)
return;
}
// Update the current Euler angles, clamp the pitch.
if (!this.moved) {
// first move event can be very large
this.moved = true;
return;
}
this.ex -= event.dy / 5;
this.ex = pc.math.clamp(this.ex, -90, 90);
this.ey -= event.dx / 5;
};
FlyCamera.prototype.onMouseDown = function (event) {
if (event.button === 0) {
this.lmbDown = true;
// When the mouse button is clicked try and capture the pointer
if (!this.mode && !pc.Mouse.isPointerLocked()) {
this.app.mouse.enablePointerLock();
}
}
};
FlyCamera.prototype.onMouseUp = function (event) {
if (event.button === 0) {
this.lmbDown = false;
}
};