-
Notifications
You must be signed in to change notification settings - Fork 1
/
orbit.js
361 lines (289 loc) · 10.7 KB
/
orbit.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"use strict";
function get_projection(angle, a, zMin, zMax) {
let ang = Math.tan((angle*.5)*Math.PI/180);//angle*.5
return mat4.fromValues(
0.5/ang, 0 , 0, 0,
0, 0.5*a/ang, 0, 0,
0, 0, -(zMax+zMin)/(zMax-zMin), -1,
0, 0, (-2*zMax*zMin)/(zMax-zMin), 0
);
}
function createShader(gl, type, source) {
let shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) return shader;
let err = new Error(gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
throw err;
}
function solveKepler(e, M, N = 20) {
let E = M;
for(let i = 0; i<N; i++) E = M + e*Math.sin(E);
return E;
}
function drawScene(gl, ...drawers) {
gl.clear(gl.COLOR_BUFFER_BIT);
for (let drawer of drawers) drawer();
}
function conicDrawer(gl) {
const vertexShaderSource = `#version 300 es
const float tau = 6.283185307179586;
uniform int N;
uniform float a;
uniform float e;
uniform float theta;
uniform mat4 PMatrix;
uniform mat4 VMatrix;
uniform mat4 MMatrix;
out float intensity;
void main() {
intensity = float(gl_VertexID)/float(N);
float p = a*(1.0-e*e);
float phi = theta + tau*intensity;
gl_Position = PMatrix*VMatrix*MMatrix*vec4( p*cos(phi), p*sin(phi), 0.0, 1.0 + e*cos(phi) );
}`;
const fragmentShaderSource = `#version 300 es
// fragment shaders don't have a default precision so we need
// to pick one. mediump is a good default. It means "medium precision"
precision mediump float;
// we need to declare an output for the fragment shader
out vec4 outColor;
in float intensity;
void main() {
outColor = vec4(intensity, intensity, intensity, 1);
}`;
let vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource),
fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource),
program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
let err = new Error(gl.getProgramInfoLog(program));
gl.deleteProgram(program);
throw err;
}
let uniformLocation = {}
for (let name of ["N", "a", "e", "theta", "PMatrix", "VMatrix", "MMatrix",]) {
uniformLocation[name] = gl.getUniformLocation(program, name);
}
const N = 256;
return function(matrices, eccentricity, semiMajorAxis, theta = 0) {
gl.useProgram(program);
gl.uniform1i(uniformLocation["N"], N);
gl.uniform1f(uniformLocation["a"], semiMajorAxis);
gl.uniform1f(uniformLocation["e"], eccentricity);
gl.uniform1f(uniformLocation["theta"], theta);
gl.uniformMatrix4fv(uniformLocation["PMatrix"], false, matrices.proj);
gl.uniformMatrix4fv(uniformLocation["VMatrix"], false, gl.trackball.matrix);
gl.uniformMatrix4fv(uniformLocation["MMatrix"], false, matrices.model);
gl.drawArrays(gl.LINE_STRIP, 0, N);
}
}
async function loadBuffers(gltf) {
let promises = [];
for (let buffer of gltf.buffers) {
if (buffer.reader === undefined) {
buffer.reader = new FileReader();
fetch(buffer.uri, {cache: "no-store"})
.then(response => response.blob())
.then(blob => buffer.reader.readAsArrayBuffer(blob));
promises.push(
new Promise(
(resolve, reject) =>
buffer.reader.onload = resolve
)
);
}
}
return Promise.all(promises);
}
function getAccessorData(gltf, accessor) {
let bufferView = gltf.bufferViews[accessor.bufferView],
buffer = gltf.buffers[bufferView.buffer],
result = buffer.reader.result,
start = bufferView.byteOffset ? bufferView.byteOffset : 0,
end = start + bufferView.byteLength,
slicedBuffer = result.slice(start, end),
data = new (
function (componentType) {
switch (componentType) {
case 5120:
return Int8Array;
case 5121:
return Uint8Array;
case 5122:
return Int16Array;
case 5123:
return Uint16Array;
case 5125:
return Uint32Array;
case 5126:
return Float32Array;
default:
throw new Error("unknown component type " + componentType);
}
}(accessor.componentType)
)(slicedBuffer);
return data;
}
async function asteroidDrawer(gl, matrices) {
let drawers = [];
const vertexShaderSource = `#version 300 es
// an attribute is an input (in) to a vertex shader.
// It will receive data from a buffer
in vec4 a_position;
in vec3 a_normal;
// A matrix to transform the positions by
uniform mat4 PMatrix;
uniform mat4 VMatrix;
uniform mat4 MMatrix;
// varying to pass the normal to the fragment shader
out vec3 v_normal;
// all shaders have a main function
void main() {
// Multiply the position by the matrix.
gl_Position = PMatrix * VMatrix * MMatrix * a_position;
// Pass the normal to the fragment shader
v_normal = a_normal;
}`;
const fragmentShaderSource = `#version 300 es
precision mediump float;
// Passed in and varied from the vertex shader.
in vec3 v_normal;
uniform vec3 u_reverseLightDirection;
uniform vec4 u_color;
// we need to declare an output for the fragment shader
out vec4 outColor;
void main() {
// because v_normal is a varying it's interpolated
// so it will not be a uint vector. Normalizing it
// will make it a unit vector again
vec3 normal = normalize(v_normal);
// compute the light by taking the dot product
// of the normal to the light's reverse direction
float light = dot(normal, u_reverseLightDirection);
outColor = u_color;
// Lets multiply just the color portion (not the alpha)
// by the light
outColor.rgb *= light;
}`;
let vertexShader = gl.createShader(gl.VERTEX_SHADER),
fragmentShader = gl.createShader(gl.FRAGMENT_SHADER),
program = gl.createProgram();
gl.shaderSource(vertexShader, vertexShaderSource);
gl.compileShader(vertexShader);
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS))
throw new Error(gl.getShaderInfoLog(vertexShader));
gl.shaderSource(fragmentShader, fragmentShaderSource);
gl.compileShader(fragmentShader);
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS))
throw new Error(gl.getShaderInfoLog(fragmentShader));
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
throw new Error(gl.getProgramInfoLog(program));
let positionAttributeLocation = gl.getAttribLocation(program, "a_position"),
normalAttributeLocation = gl.getAttribLocation(program, "a_normal"),
pMatrixLocation = gl.getUniformLocation(program, "PMatrix"),
vMatrixLocation = gl.getUniformLocation(program, "VMatrix"),
mMatrixLocation = gl.getUniformLocation(program, "MMatrix"),
colorLocation = gl.getUniformLocation(program, "u_color"),
reverseLightDirectionLocation = gl.getUniformLocation(program, "u_reverseLightDirection");
console.log("fetching asteroid.gltf");
let gltf = await fetch("asteroid.gltf", {cache: "no-store"}).then(r => r.json());
console.log("fetching binary data");
await loadBuffers(gltf);
for (let node of gltf.nodes) {
let mesh = gltf.meshes[node.mesh];
for (let primitive of mesh.primitives) {
let attributes = primitive.attributes,
vertices = getAccessorData(gltf, gltf.accessors[attributes.POSITION]),
normals = getAccessorData(gltf, gltf.accessors[attributes.NORMAL]);
if (primitive.indices !== undefined) {
let accessor = gltf.accessors[primitive.indices],
indices = getAccessorData(gltf, accessor),
indexType = accessor.componentType;
gl.bindBuffer(gl.ARRAY_BUFFER, null);
primitive.indexObject = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, primitive.indexObject);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
//gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
//gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, g.box.indexObject);
primitive.numIndices = indices.length;
primitive.indexType =
indexType == 5121 ? gl.UNSIGNED_BYTE :
indexType == 5123 ? gl.UNSIGNED_SHORT :
indexType == 5125 ? gl.UNSIGNED_INT :
null;
}
let positionBuffer = gl.createBuffer(),
vao = gl.createVertexArray();
gl.bindVertexArray(vao);
gl.enableVertexAttribArray(positionAttributeLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
gl.vertexAttribPointer(positionAttributeLocation, 3, gl.FLOAT, false, 0, 0);
let normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW);
gl.enableVertexAttribArray(normalAttributeLocation);
gl.vertexAttribPointer(normalAttributeLocation, 3, gl.FLOAT, false, 0, 0);
drawers.push(
function() {
gl.useProgram(program);
gl.bindVertexArray(vao);
gl.uniformMatrix4fv(pMatrixLocation, false, matrices.proj);
gl.uniformMatrix4fv(vMatrixLocation, false, gl.trackball.matrix);
gl.uniformMatrix4fv(mMatrixLocation, false, matrices.model);
gl.uniform4fv(colorLocation, [1.0, 1.0, 1.0, 1.0]);
gl.uniform3fv(reverseLightDirectionLocation, [1.0, 0.0, 0.0]);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, primitive.indexObject);
gl.drawElements(
gl.TRIANGLES, primitive.numIndices,
primitive.indexType, 0
);
}
);
}
}
return drawers;
}
async function main() {
let canvas = document.getElementById("asteroid"),
gl = canvas.getContext("webgl2"),
conic = conicDrawer(gl, 0.2);
gl.trackball = new Trackball(canvas),
canvas.width = 800;
canvas.height = 640;
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(0, 0, 0, 1);
gl.enable(gl.DEPTH_TEST);
let matrices = {
proj: get_projection(40, gl.canvas.width/gl.canvas.height, 1, 100),
model: mat4.create(),
view: gl.trackball.matrix,
},
asteroids = await asteroidDrawer(gl, matrices),
orbitalElements = {
eccentricity: 0.8,
semiMajorAxis: 6,
};
(function animate() {
let e = orbitalElements.eccentricity,
a = orbitalElements.semiMajorAxis,
w = 0.3,
t = Date.now()/1000,
M = (w*t) % (2*Math.PI) - Math.PI,
E = solveKepler(e, M),
theta = 2*Math.atan(Math.sqrt((1+e)/(1-e))*Math.tan(E/2));
drawScene(
gl,
() => conic(matrices, e, a, theta),
...asteroids
);
window.requestAnimationFrame(animate);
})();
}