-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.html
394 lines (323 loc) · 12.9 KB
/
main.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<html>
<head>
<script src="libs/twgl.js/dist/5.x/twgl-full.js"></script>
</head>
<body>
<title>Project webgl-solar-system-model</title>
<canvas id="c"></canvas>
</body>
<script id="gouraud-vs" type="notjs">
precision mediump float;
// uniforms for transformation and lighting
uniform mat4 u_worldViewProjection;
uniform vec3 u_lightWorldPos;
uniform mat4 u_world;
uniform mat4 u_viewInverse;
uniform mat4 u_worldInverseTranspose;
uniform vec4 u_lightColor;
uniform vec4 u_ambient;
uniform sampler2D u_diffuse;
uniform vec4 u_specular;
uniform float u_shininess;
uniform int u_is_light_source;
uniform int u_ambient_enabled;
uniform int u_diffuse_enabled;
uniform int u_specular_enabled;
// vertex attributes
attribute vec4 position;
attribute vec3 normal;
attribute vec2 texcoord;
// varying variables to pass data from vertex to fragment shader
varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;
varying vec4 v_intensity; // intensity calculated in the vertex shader
void main() {
// pass texture coordinates to fragment shader
v_texCoord = texcoord;
// calculate lighting in the vertex shader
vec4 diffuseColor = texture2D(u_diffuse, texcoord);
vec3 N = normalize((u_worldInverseTranspose * vec4(normal, 0)).xyz);
vec3 L = normalize(u_lightWorldPos - (u_world * position).xyz);
vec3 V = normalize((u_viewInverse[3] - (u_world * position)).xyz);
vec3 R = reflect(-L, N);
float n = u_shininess;
float F_at = 1.0;
float LN = max(dot(L, N), 0.0); // L.N
float RV = max(dot(R, V), 0.0); // R.V
vec4 I_a = u_ambient;
vec4 I_p = u_lightColor;
vec4 K_a = diffuseColor;
vec4 K_d = diffuseColor;
vec4 K_s = u_specular;
// ambient reflection: ambientTerm := I_a * K_a
vec4 ambientTerm = (I_a * K_a) * vec4(u_ambient_enabled);
// light source: lightSourceTerm := I_p * F_at
vec4 lightSourceTerm = I_p * F_at;
// diffuse reflection: diffuseTerm := K_d * (L . N)
vec4 diffuseTerm = (K_d * LN) * vec4(u_diffuse_enabled);
// specular reflection: specularTerm := K_s * (R . V)^n
vec4 specularTerm = (LN > 0.0) ? (K_s * pow(RV, n)) * vec4(u_specular_enabled) : vec4(0.0);
vec4 I;
if (u_is_light_source == 0) {
I = ambientTerm + lightSourceTerm * (diffuseTerm + specularTerm);
} else {
// is light source => just output the diffuse color
I = diffuseColor;
}
// set alpha value
I.a = diffuseColor.a;
v_intensity = I;
// pass other variables to the fragment shader
v_position = u_worldViewProjection * position;
v_normal = N;
v_surfaceToLight = L;
v_surfaceToView = V;
gl_Position = v_position;
}
</script>
<script id="gouraud-fs" type="notjs">
precision mediump float;
// varying variables from the vertex shader
varying vec4 v_intensity;
void main() {
// retrieve values from vs
gl_FragColor = v_intensity;
}
</script>
<script id="phong-vs" type="notjs">
// uniforms for transformation and lighting
uniform mat4 u_worldViewProjection;
uniform vec3 u_lightWorldPos;
uniform mat4 u_world;
uniform mat4 u_viewInverse;
uniform mat4 u_worldInverseTranspose;
// vertex attributes
attribute vec4 position;
attribute vec3 normal;
attribute vec2 texcoord;
// varying variables to pass data from vertex to fragment shader
varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;
void main() {
// pass texture coordinates to fragment shader
v_texCoord = texcoord;
v_position = u_worldViewProjection * position;
v_normal = (u_worldInverseTranspose * vec4(normal, 0)).xyz;
v_surfaceToLight = u_lightWorldPos - (u_world * position).xyz;
v_surfaceToView = (u_viewInverse[3] - (u_world * position)).xyz;
gl_Position = v_position;
}
</script>
<script id="phong-fs" type="notjs">
precision mediump float;
varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;
uniform vec4 u_lightColor;
uniform vec4 u_ambient;
uniform sampler2D u_diffuse;
uniform vec4 u_specular;
uniform float u_shininess;
uniform int u_is_light_source;
uniform int u_ambient_enabled;
uniform int u_diffuse_enabled;
uniform int u_specular_enabled;
void main() {
// retrieve values from vs
vec4 diffuseColor = texture2D(u_diffuse, v_texCoord);
vec3 normal = normalize(v_normal);
vec3 surfaceToLight = normalize(v_surfaceToLight);
vec3 surfaceToView = normalize(v_surfaceToView);
// extract Phong model vars: I = I_aK_a + I_pF_at [k_d(L.N) + k_s (R.V)^n]
vec3 N = normal;
vec3 L = surfaceToLight;
vec3 V = surfaceToView;
vec3 R = reflect(-L, N);
float n = u_shininess;
// float dL = length(v_surfaceToLight);
// we tried to use the distance between light source and surface, but it was not good
float F_at = 1.0; // attenuation factor: F_at = 1.0 / (dL^2) or F_at = 1.0
float LN = max(dot(L, N), 0.0); // L.N
float RV = max(dot(R, V), 0.0); // R.V
vec4 I_a = u_ambient;
vec4 I_p = u_lightColor;
vec4 K_a = diffuseColor;
vec4 K_d = diffuseColor;
vec4 K_s = u_specular;
// ambient reflection: ambientTerm := I_a * K_a
vec4 ambientTerm = (I_a * K_a) * vec4(u_ambient_enabled);
// light source: lightSourceTerm := I_p * F_at
vec4 lightSourceTerm = I_p * F_at;
// diffuse reflection: diffuseTerm := K_d * (L . N)
vec4 diffuseTerm = (K_d * LN) * vec4(u_diffuse_enabled);
// specular reflection: specularTerm := K_s * (R . V)^n
vec4 specularTerm = (LN > 0.0) ? (K_s * pow(RV, n)) * vec4(u_specular_enabled): vec4(0.0);
vec4 I;
if (u_is_light_source == 0) {
I = ambientTerm + lightSourceTerm * (diffuseTerm + specularTerm);
} else {
// is light source => just output the diffuse color
I = diffuseColor;
}
// set alpha value
I.a = diffuseColor.a;
gl_FragColor = I;
}
</script>
<script type="module">
import * as celestial from './libs/celestial.js/celestial-full.js';
import * as space from './libs/space.js/space-full.js';
const m4 = twgl.m4;
const gl = document.querySelector("canvas").getContext("webgl");
const programInfos = [
twgl.createProgramInfo(gl, ["gouraud-vs", "gouraud-fs"]),
twgl.createProgramInfo(gl, ["phong-vs", "phong-fs"])
];
// texture src: https://in.pinterest.com/pin/white-texture-free-download-free-for-commercial-use-no-registration-required-add-a-subtle-white-te--430867889343458127/
const default_texture = twgl.createTexture(gl, { src: 'textures/white.jpeg' });
let timescale = 1200 / 365; // 1200 seconds per 365 days
// bodies
const sun = celestial.create(gl, timescale, "Sun");
const mercury = celestial.create(gl, timescale, "Mercury");
const venus = celestial.create(gl, timescale, "Venus");
const earth = celestial.create(gl, timescale, "Earth");
const moon = celestial.createMoon(gl, timescale, earth);
const mars = celestial.create(gl, timescale, "Mars");
const jupiter = celestial.create(gl, timescale, "Jupiter");
const io = celestial.createJupiterMoon(gl, timescale, jupiter, "io");
const europa = celestial.createJupiterMoon(gl, timescale, jupiter, "europa");
const ganymede = celestial.createJupiterMoon(gl, timescale, jupiter, "ganymede");
const callisto = celestial.createJupiterMoon(gl, timescale, jupiter, "callisto");
const saturn = celestial.create(gl, timescale, "Saturn");
const titan = celestial.createSaturnMoonTitan(gl, timescale, saturn);
const uranus = celestial.create(gl, timescale, "Uranus");
const neptune = celestial.create(gl, timescale, "Neptune");
const triton = celestial.createNeptuneMoonTriton(gl, timescale, neptune);
const pluto = celestial.create(gl, timescale, "Pluto");
const charon = celestial.createPlutoMoonCharon(gl, timescale, pluto);
const celestial_bodies = [
sun,
mercury,
venus,
earth,
moon,
mars,
jupiter,
io,
europa,
ganymede,
callisto,
saturn,
titan,
uranus,
neptune,
triton,
pluto,
charon
];
gl.clearColor(0.0, 0.0, 0.0, 1.0); // black background
const state = {
eye: [0, 30, 40],
target: [0, 0, 0],
up: [0, 1, 0],
};
const interactions = {
isDragging: false,
lastX: 0,
lastY: 0,
speedFactor: 1,
lastTS: 0,
paused: false,
enable_draw_orbits: 1,
enable_draw_moons: 1,
shading_selection: 1
}
// Event listeners for keys
window.addEventListener('keydown', (event) => space.SpatialHandlingUtils.handleKeyDown(event, state, uniforms, interactions, earth, sun));
// Event listeners for mouse
window.addEventListener('mousedown', (event) => space.SpatialHandlingUtils.handleMouseDown(event, interactions));
window.addEventListener('mousemove', (event) => space.SpatialHandlingUtils.handleMouseMove(event, interactions, state));
window.addEventListener('mouseup', (event) => space.SpatialHandlingUtils.handleMouseUp(interactions));
window.addEventListener('wheel', (event) => space.SpatialHandlingUtils.handleWheel(event, state));
const uniforms = {
u_lightWorldPos: sun.worldMatrix,
u_lightColor: [1, 0.8, 0.8, 1], // I_p (Intensity of the point light source)
u_ambient: [0.1, 0.1, 0.1, 1], // I_a (Ambient light intensity)
u_specular: [1, 1, 1, 1], // K_s (Specular reflection coefficient)
u_shininess: 30, // n
u_ambient_enabled: 1,
u_diffuse_enabled: 1,
u_specular_enabled: 1,
u_diffuse: default_texture,
u_is_light_source: 0,
};
// Configurações de canvas
const canvas = gl.canvas;
twgl.resizeCanvasToDisplaySize(canvas);
// Define explicitamente o tamanho do canvas
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Configurações WebGL
gl.viewport(0, 0, canvas.width, canvas.height);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
const fov = 30 * Math.PI / 180;
const aspect = canvas.width / canvas.height;
const zNear = 0.5;
const zFar = 750;
const projection = m4.perspective(fov, aspect, zNear, zFar);
function render(time) {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
time *= 0.001 * interactions.speedFactor; // ms => s
if (interactions.paused)
time = interactions.lastTS;
else
interactions.lastTS = time;
// view config
const view = m4.inverse(m4.lookAt(state.eye, state.target, state.up));
const viewProjection = m4.multiply(projection, view);
// shading selection
const programInfo = programInfos[interactions.shading_selection];
celestial_bodies
.filter(b => b.parent == null || interactions.enable_draw_moons) // draw only planets, if thats the case
.forEach((body, index) => {
body.updateWorldMatrix(time);
uniforms.u_world = body.worldMatrix;
uniforms.u_worldInverseTranspose = m4.transpose(m4.inverse(body.worldMatrix));
uniforms.u_worldViewProjection = m4.multiply(viewProjection, body.worldMatrix);
uniforms.u_diffuse = body.texture || default_texture;
if (body.name == "Sun") uniforms.u_is_light_source = 1;
gl.useProgram(programInfo.program);
twgl.setBuffersAndAttributes(gl, programInfo, body.buffer);
twgl.setUniforms(programInfo, uniforms);
twgl.drawBufferInfo(gl, body.buffer);
if (body.name == "Sun") uniforms.u_is_light_source = 0;
if (interactions.enable_draw_orbits == 1 && body.path != null) {
uniforms.u_is_light_source = 1; // push
for (const part of body.path) {
uniforms.u_world = part.worldMatrix;
uniforms.u_worldInverseTranspose = m4.transpose(m4.inverse(part.worldMatrix));
uniforms.u_worldViewProjection = m4.multiply(viewProjection, part.worldMatrix);
uniforms.u_diffuse = default_texture;
gl.useProgram(programInfo.program);
twgl.setBuffersAndAttributes(gl, programInfo, part.buffer);
twgl.setUniforms(programInfo, uniforms);
twgl.drawBufferInfo(gl, part.buffer);
}
uniforms.u_is_light_source = 0; // pop
}
})
requestAnimationFrame(render);
}
requestAnimationFrame(render);
</script>
</html>