-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcanvas3d.html
335 lines (280 loc) · 8.55 KB
/
canvas3d.html
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!-- vim: sw=2 ts=2 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Canvas Demo</title>
<style> document, body { margin: 0px; padding: 0px; overflow: hidden; } </style>
</head>
<body>
<canvas id="glcanvas"></canvas>
<script>"use strict";
let input = {
zoom: 10,
scroll: 0,
pitch: Math.PI*0.70,
yaw: -Math.PI*0.12,
eye: new DOMPoint(0, 0, 0, 1),
cam_pivot: new DOMPoint(0, 0, 0, 1),
dampedEvent: { button: 0, movementX: 0, movementY: 0 },
lmb_down: false,
rmb_down: false,
mouse_x: 0,
mouse_y: 0,
}
/* mouse controls */
{
const opts = { passive: false };
window.addEventListener('wheel', e => {
e.preventDefault();
if (input.mouse_down) return;
input.scroll += e.deltaY;
}, opts);
window.addEventListener('mousedown', ev => {
ev.preventDefault();
input.dampedEvent.button = ev.button ? 2 : 0;
if (ev.button == 0) input.lmb_down = true;
if (ev.button == 2) input.rmb_down = true;
}, opts);
window.addEventListener('mousemove', ev => {
ev.preventDefault();
if (input.lmb_down || input.rmb_down) {
input.dampedEvent.movementX += ev.movementX;
input.dampedEvent.movementY += ev.movementY;
}
input.mouse_x = ev.offsetX*window.devicePixelRatio;
input.mouse_y = ev.offsetY*window.devicePixelRatio;
}, opts);
window.addEventListener("contextmenu", ev => {
ev.preventDefault();
}, opts);
window.addEventListener('mouseup', ev => {
ev.preventDefault();
if (ev.button == 0) input.lmb_down = false;
if (ev.button == 2) input.rmb_down = false;
}, opts);
}
/* touch controls */
{
const opts = { passive: false };
let touch_x = 0;
let touch_y = 0;
window.addEventListener("touchstart", ev => {
ev.preventDefault();
touch_x = ev.changedTouches[0].clientX;
touch_y = ev.changedTouches[0].clientY;
input.lmb_down = true;
}, opts);
window.addEventListener("touchmove", ev => {
ev.preventDefault();
input.dampedEvent.button = 0;
input.dampedEvent.movementX += ev.changedTouches[0].clientX - touch_x;
input.dampedEvent.movementY += ev.changedTouches[0].clientY - touch_y;
touch_x = ev.changedTouches[0].clientX;
touch_y = ev.changedTouches[0].clientY;
}, opts);
window.addEventListener("touchend", ev => {
ev.preventDefault();
input.lmb_down = false;
}, opts);
}
const canvas = document.getElementById("glcanvas");
const ctx = canvas.getContext('2d');
(window.onresize = () => {
canvas.width = window.innerWidth*window.devicePixelRatio,
canvas.height = window.innerHeight*window.devicePixelRatio
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
})();
let last_timestamp;
requestAnimationFrame(function render(timestamp) {
requestAnimationFrame(render);
timestamp *= 0.001;
last_timestamp ??= timestamp;
const deltaTime = timestamp - last_timestamp;
last_timestamp = timestamp;
{
const ev = input.dampedEvent;
/* based on the assumption that if you're zoomed in more,
* you're doing finer-detailed work and want more precise movements. */
const zoom_fudge = Math.sqrt(input.zoom/10.0)*2.0;
if (ev.button == 0) {
input.pitch -= ev.movementX * 0.0005 * zoom_fudge;
input.yaw -= ev.movementY * 0.0005 * zoom_fudge;
input.yaw = Math.max(-Math.PI*0.5 + 0.01, Math.min(Math.PI*0.5 - 0.01, input.yaw));
}
if (ev.button == 2) {
const unit = new DOMPoint(0, -ev.movementX*0.00075*zoom_fudge, ev.movementY*0.00075*zoom_fudge, 1)
.matrixTransform(new DOMMatrix().rotateSelf(0, input.yaw / Math.PI * 180, input.pitch / Math.PI * 180));
input.cam_pivot.x += unit.x;
input.cam_pivot.y += unit.y;
input.cam_pivot.z += unit.z;
}
ev.movementX *= Math.pow(1 - 0.17, 60*deltaTime);
ev.movementY *= Math.pow(1 - 0.17, 60*deltaTime);
{
const t = Math.cbrt(Math.abs(input.scroll)) * Math.sign(input.scroll);
input.zoom += 0.005*t*input.zoom;
input.scroll *= Math.pow(1 - 0.5, 60*deltaTime);
input.zoom = Math.min(200, input.zoom);
}
}
let mvp;
{
const FIELD_OF_VIEW = 50 / 180 * Math.PI;
const ar = window.innerWidth / window.innerHeight;
const projection = mat4_perspective(new DOMMatrix(), FIELD_OF_VIEW, ar, 0.01, 100.0);
const view = new DOMMatrix().rotateSelf(0, input.yaw / Math.PI * 180, input.pitch / Math.PI * 180);
{
const eye = input.eye = new DOMPoint(input.zoom, 0, 0, 1).matrixTransform(view);
eye.x += input.cam_pivot.x;
eye.y += input.cam_pivot.y;
eye.z += input.cam_pivot.z;
mat4_target_to(view, eye, input.cam_pivot, new DOMPoint(0, 0, 1));
view.invertSelf();
}
mvp = new DOMMatrix(projection).multiplySelf(view);
}
ctx.globalAlpha = 1.0;
ctx.fillStyle = "rgb(30 40 50)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalAlpha = 1.0;
const transform = (p) => {
p.w = 1;
p = p.matrixTransform(mvp);
const w = p.w;
p.x = (0 + (p.x/w*0.5 + 0.5))*canvas.width;
p.y = (1 - (p.y/w*0.5 + 0.5))*canvas.height;
return p;
}
{
ctx.beginPath();
ctx.globalAlpha = 0.5;
ctx.lineWidth = 0.15 * 40;
ctx.strokeStyle = 'teal';
function *spiral(direction) {
let p = new DOMPoint();
const lines = 81;
const loops = 2;
const height = 6;
const radius = 0.5;
let last = null;
for (let i = 0; i < lines; i++) {
const t = i / (lines - 1);
const r = t * Math.PI * 2 * loops - direction * Math.PI*0.5;
p.x = Math.cos(r) * radius;
p.y = Math.sin(r) * radius;
p.z = height * (0.5 - t);
p = transform(p);
if (last === null) {
last = new DOMPoint();
} else {
yield [last, p];
}
last.x = p.x;
last.y = p.y;
last.z = p.z;
last.w = p.w;
}
}
const lhs_spiral = spiral( 1);
const rhs_spiral = spiral(-1);
while (true) {
const lhs = lhs_spiral.next();
const rhs = rhs_spiral.next();
if (lhs.done || rhs.done) break;
ctx.beginPath();
ctx.moveTo(lhs.value[0].x, lhs.value[0].y);
ctx.lineTo(lhs.value[1].x, lhs.value[1].y);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(rhs.value[0].x, rhs.value[0].y);
ctx.lineTo(rhs.value[1].x, rhs.value[1].y);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(lhs.value[0].x, lhs.value[0].y);
ctx.lineTo(rhs.value[0].x, rhs.value[0].y);
ctx.stroke();
}
// ctx.globalAlpha = 1.0;
// ctx.fillStyle = "teal";
// const point = [0, 0, 0, 1];
// transform(point);
// ctx.fillRect(point[0] - 10, point[1] - 10, 20, 20);
}
ctx.globalAlpha = 1.0;
})
function mat4_perspective(out, fovy, aspect, near, far) {
let f = 1.0 / Math.tan(fovy / 2),
nf;
out.m11 = f / aspect;
out.m12 = 0;
out.m13 = 0;
out.m14 = 0;
out.m21 = 0;
out.m22 = f;
out.m23 = 0;
out.m24 = 0;
out.m31 = 0;
out.m32 = 0;
out.m33 = 0;
out.m34 = -1;
out.m41 = 0;
out.m42 = 0;
out.m43 = 0;
out.m44 = 0;
if (far != null && far !== Infinity) {
nf = 1 / (near - far);
out.m33 = (far + near) * nf;
out.m43 = 2 * far * near * nf;
} else {
out.m33 = -1;
out.m43 = -2 * near;
}
return out;
}
function mat4_target_to(out, eye, target, up) {
let z0 = eye.x - target.x,
z1 = eye.y - target.y,
z2 = eye.z - target.z;
let len = z0 * z0 + z1 * z1 + z2 * z2;
if (len > 0) {
len = 1 / Math.sqrt(len);
z0 *= len;
z1 *= len;
z2 *= len;
}
let x0 = up.y * z2 - up.z * z1,
x1 = up.z * z0 - up.x * z2,
x2 = up.x * z1 - up.y * z0;
len = x0 * x0 + x1 * x1 + x2 * x2;
if (len > 0) {
len = 1 / Math.sqrt(len);
x0 *= len;
x1 *= len;
x2 *= len;
}
out.m11 = x0;
out.m12 = x1;
out.m13 = x2;
out.m14 = 0;
out.m21 = z1 * x2 - z2 * x1;
out.m22 = z2 * x0 - z0 * x2;
out.m23 = z0 * x1 - z1 * x0;
out.m24 = 0;
out.m31 = z0;
out.m32 = z1;
out.m33 = z2;
out.m34 = 0;
out.m41 = eye.x;
out.m42 = eye.y;
out.m43 = eye.z;
out.m44 = 1;
return out;
}
function lerp(v0, v1, t) { return (1 - t) * v0 + t * v1; }
function inv_lerp(min, max, p) { return (p - min) / (max - min); }
</script>
</body>
</html>