-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathcamera.ts
More file actions
151 lines (129 loc) · 5.38 KB
/
camera.ts
File metadata and controls
151 lines (129 loc) · 5.38 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
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
import { mat4 } from 'wgpu-matrix'
import { renderUniformsValues, renderUniformsViews } from './common'
export class Camera {
isDragging: boolean
prevX: number
prevY: number
prevHoverX: number
prevHoverY: number
currentHoverX: number
currentHoverY: number
currentXtheta: number
currentYtheta: number
maxYTheta: number
minYTheta: number
sensitivity: number
currentDistance: number
maxDistance: number
minDistance: number
target: number[]
fov: number
zoomRate: number
canvas: HTMLCanvasElement
constructor (canvas: HTMLCanvasElement) {
this.canvas = canvas;
this.canvas.addEventListener("mousedown", (event: MouseEvent) => {
this.isDragging = true;
this.prevX = event.clientX;
this.prevY = event.clientY;
});
this.canvas.addEventListener("wheel", (event: WheelEvent) => {
event.preventDefault();
var scrollDelta = event.deltaY;
this.currentDistance += ((scrollDelta > 0) ? 1 : -1) * this.zoomRate;
if (this.currentDistance < this.minDistance) this.currentDistance = this.minDistance;
if (this.currentDistance > this.maxDistance) this.currentDistance = this.maxDistance;
this.recalculateView()
})
this.canvas.addEventListener("mousemove", (event: MouseEvent) => {
this.currentHoverX = event.clientX;
this.currentHoverY = event.clientY;
if (this.isDragging) {
const deltaX = this.prevX - event.clientX;
// const deltaY = this.prevY - event.clientY;
this.currentXtheta += this.sensitivity * deltaX;
// this.currentYtheta += this.sensitivity * deltaY;
if (this.currentYtheta > this.maxYTheta) this.currentYtheta = this.maxYTheta
if (this.currentYtheta < this.minYTheta) this.currentYtheta = this.minYTheta
this.prevX = event.clientX;
this.prevY = event.clientY;
this.recalculateView()
}
});
this.canvas.addEventListener("mouseup", () => {
if (this.isDragging) this.isDragging = false;
});
}
reset(initDistance: number, target: number[], fov: number, zoomRate: number) {
this.isDragging = false
this.prevX = 0
this.prevY = 0
this.currentXtheta = -Math.PI / 2 * 1
// this.currentYtheta = -Math.PI / 12
this.currentYtheta = 0
this.maxYTheta = 0
this.minYTheta = -0.99 * Math.PI / 2.
this.sensitivity = 0.005
this.currentDistance = initDistance
this.maxDistance = 2. * this.currentDistance
this.minDistance = 0.7 * this.currentDistance
this.target = target
this.fov = fov
this.zoomRate = zoomRate
const aspect = this.canvas.clientWidth / this.canvas.clientHeight
const projection = mat4.perspective(fov, aspect, 0.1, 300)
renderUniformsViews.projection_matrix.set(projection)
renderUniformsViews.inv_projection_matrix.set(mat4.inverse(projection))
this.recalculateView()
}
recalculateView() {
var mat = mat4.identity();
mat4.translate(mat, this.target, mat)
mat4.rotateY(mat, this.currentXtheta, mat)
mat4.rotateX(mat, this.currentYtheta, mat)
mat4.translate(mat, [0, 0, this.currentDistance], mat)
var position = mat4.multiply(mat, [0, 0, 0, 1])
const view = mat4.lookAt(
[position[0], position[1], position[2]], // position
this.target, // target
[0, 1, 0], // up
)
renderUniformsViews.view_matrix.set(view)
renderUniformsViews.inv_view_matrix.set(mat4.inverse(view))
}
calcMouseVelocity() {
if (this.isDragging) {
return [0, 0]
}
let [mousePlaneX, mousePlaneY] = this.calcPlaneCoord(this.currentHoverX, this.currentHoverY)
let [prevMousePlaneX, prevMousePlaneY] = this.calcPlaneCoord(this.prevHoverX, this.prevHoverY)
// これはスケールも重要なので正規化してはいけない
let mouseVelocityX = mousePlaneX - prevMousePlaneX
let mouseVelocityY = mousePlaneY - prevMousePlaneY
let mouseViewVelocity = [mouseVelocityX, mouseVelocityY, 0, 0]
let velX = (this.currentHoverX - this.prevHoverX) / this.canvas.width * (this.canvas.width / this.canvas.height);
let velY = -(this.currentHoverY - this.prevHoverY) / this.canvas.height;
// ワールド座標に直すのはコンピュートシェーダーで
return mouseViewVelocity
}
calcPlaneCoord(x: number, y: number) {
let normalizedX = x / this.canvas.width
let normalizedY = y / this.canvas.height
let ndcX = 2.0 * normalizedX - 1.0
let ndcY = (1.0 - normalizedY) * 2.0 - 1.0
let viewSpaceMouseRay = [
ndcX * Math.tan(this.fov / 2.0) * (this.canvas.width / this.canvas.height),
ndcY * Math.tan(this.fov / 2.0),
-1.0
]
return [viewSpaceMouseRay[0] * this.currentDistance, viewSpaceMouseRay[1] * this.currentDistance]
}
setNewPrevMouseCoord() {
this.prevHoverX = this.currentHoverX;
this.prevHoverY = this.currentHoverY;
}
stepAngle() {
this.currentXtheta += 0.012
this.recalculateView()
}
}