-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
162 lines (131 loc) · 4.23 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const glmatrix = require('gl-matrix')
const vec3 = glmatrix.vec3
const mat4 = glmatrix.mat4
const quat = glmatrix.quat
function Camera(position, target) {
let driving = false
let cx = 0
let cy = 0
let xMove = t => 0.0
let zMove = t => 0.0
let currentTarget = vec3.copy(vec3.create(), target)
let view = mat4.lookAt(mat4.create(),
position,
target,
vec3.fromValues(0.0, 1.0, 0.0))
let rotationDelta = {
pitch: (t, s) => 0.0,
yaw: (t, s) => 0.0
}
const currentRotation = {
pitch: 0.0,
yaw: 0.0
}
const moveSpeed = 16.0
const rotationSpeed = 20.0
this.trackHeight = (getHeight
this.getProjection = context => {
const result = mat4.create()
const aspect = context.canvas.clientWidth/context.canvas.clientHeight
mat4.perspective(result, Math.PI/4, aspect, 0.1, 200.0)
return result
}
this.getView = () => view
this.getPosition = () => vec3.copy(vec3.create(), position)
this.getTarget = () => vec3.copy(vec3.create(), currentTarget)
this.getWorldViewProjection = (context, world) => {
const result = mat4.create()
mat4.multiply(result, this.getProjection(context), this.getWorldView(world))
return result
}
this.getWorldView = world => {
const result = mat4.create()
mat4.multiply(result, this.getView(), world)
return result
}
const mousedown = e => {
driving = true
cx = event.clientX
cy = event.clientY
}
const mouseup = e => {
driving = false
rotationDelta = {
pitch: (t, s) => 0.0,
yaw: (t, s) => 0.0
}
}
const mousemove = e => {
const degToRad = value => value*Math.PI/180
if (!driving) {
return
}
const x = e.clientX
const y = e.clientY
const dx = x-cx
const dy = y-cy
rotationDelta = {
pitch: (t, s) => degToRad(dy*t*s),
yaw: (t, s) => degToRad(dx*t*s)
}
cx = x
cy = y
}
const keydown = e => {
if (e.keyCode === 37) {
// move left
xMove = (t, s) => -1.0*t*s
} if (e.keyCode === 38) {
// move forward
zMove = (t, s) => -1.0*t*s
} if (e.keyCode === 39) {
// move right
xMove = (t, s) => 1.0*t*s
} if (e.keyCode === 40) {
// move backward
zMove = (t, s) => 1.0*t*s
}
}
const keyup = e => {
if (e.keyCode === 37) {
xMove = t => 0.0
} if (e.keyCode === 38) {
zMove = t => 0.0
} if (e.keyCode === 39) {
xMove = t => 0.0
} if (e.keyCode === 40) {
zMove = t => 0.0
}
}
this.initialize = (context, content) => {
context.canvas.onmousedown = mousedown
document.onmouseup = mouseup
document.onmousemove = mousemove
document.onkeydown = keydown
document.onkeyup = keyup
}
this.getRotation = () => {
let pitch = mat4.fromXRotation(mat4.create(), currentRotation.pitch)
let yaw = mat4.fromYRotation(mat4.create(), currentRotation.yaw)
return mat4.multiply(mat4.create(), pitch, yaw)
}
this.update = (context, time) => {
currentRotation.pitch += rotationDelta.pitch(time.delta, rotationSpeed)
currentRotation.yaw += rotationDelta.yaw(time.delta, rotationSpeed)
let rotation = this.getRotation()
let addVector = vec3.fromValues(xMove(time.delta, moveSpeed),
0.0,
zMove(time.delta, moveSpeed))
let rotatedVector = vec3.transformMat4(vec3.create(), addVector, rotation);
vec3.add(position, position, rotatedVector)
let rotatedTarget = vec3.transformMat4(vec3.create(),
vec3.copy(vec3.create(), target),
rotation)
currentTarget = vec3.add(currentTarget, position, rotatedTarget)
let rotatedUp = vec3.transformMat4(vec3.fromValues(0.0, 1.0, 0.0),
vec3.fromValues(0.0, 1.0, 0.0),
rotation)
view = mat4.lookAt(mat4.create(), position, currentTarget, rotatedUp)
}
}
module.exports = Camera