-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
146 lines (109 loc) · 3.15 KB
/
main.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
import {Mat4} from 'lib/math';
import Model from 'lib/model';
let id;
let model;
let gl;
let screen = document.getElementById('screen');
screen.width = screen.clientWidth;
screen.height = screen.clientHeight;
let scale = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
let rotateY = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
let translate = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
let files = document.getElementById('file');
let list = document.getElementById('emdlist');
files.addEventListener('change', async() => {
let names = await fetch('models.json');
names = await names.json();
while (list.firstChild)
list.removeChild(list.firstChild);
for (var i = 0; i < files.files.length; ++i) {
var op = document.createElement('option');
op.value = i;
op.text = names[files.files[i].name];
list.add(op);
}
list.selectedIndex = 0;
list.dispatchEvent(new Event('change'));
});
list.addEventListener('change', () => {
let reader = new FileReader();
reader.addEventListener('load', () => load(reader.result));
if (model) {
window.cancelAnimationFrame(id);
model.delete(gl);
}
reader.readAsArrayBuffer(files.files[list.selectedIndex]);
});
let slider = document.getElementById('y-axis');
slider.addEventListener('input', () => {
let rad = slider.value * 2 * Math.PI;
rotateY[0] = rotateY[10] = Math.cos(rad);
rotateY[2] = Math.sin(rad);
rotateY[8] = -rotateY[2];
});
slider.dispatchEvent(new Event('input'));
let zoom = document.getElementById('zoom');
zoom.addEventListener('input', () => {
scale[0] = scale[5] = scale[10] = 19 * zoom.value;
});
zoom.dispatchEvent(new Event('input'));
let ypos = document.getElementById('y-pos');
ypos.addEventListener('input', () => {
translate[7] = ypos.value;
});
ypos.dispatchEvent(new Event('input'));
async function loadShader(url, gl, program, type)
{
let source = await fetch(url);
let shader = gl.createShader(type);
gl.shaderSource(shader, await source.text());
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
throw new Error(
`Could not compile shader: ${gl.getShaderInfoLog(shader)}`
);
gl.attachShader(program, shader);
}
(async function() {
gl = screen.getContext('webgl') || screen.getContext('experimental-webgl');
if (!gl)
throw new Error('Could not obtain webgl context');
let program = gl.createProgram();
await Promise.all([
loadShader('vertex.glsl', gl, program, gl.VERTEX_SHADER),
loadShader('fragment.glsl', gl, program, gl.FRAGMENT_SHADER),
]);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
throw (`Program failed to link: ${gl.getProgramInfoLog(program)}`);
gl.useProgram(program);
gl.enable(gl.DEPTH_TEST);
Model.init(gl);
})();
function load(buf)
{
model = new Model(gl, buf);
let view = new Float32Array(16);
id = window.requestAnimationFrame(function draw(time) {
view.set(scale);
Mat4.mult(view, rotateY);
Mat4.mult(view, translate);
model.draw(gl, view);
id = window.requestAnimationFrame(draw);
});
}