-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
410 lines (328 loc) · 14.8 KB
/
index.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!doctype html>
<html>
<head>
<title>Escena montacargas</title>
<link rel="icon" type="image/png" href="favicon.ico" />
<style>
body {
background-color: grey;
}
canvas {
background-color: white;
}
textarea {
background-color: black;
}
.wrapper {
display: flex;
flex-flow: row;
}
.wrapper>* {
flex: 1 100%;
}
@media all and (min-width: 600px) {
.aside {
flex: 1 auto;
padding: 5px;
}
}
.aside-1 {
width: 20px;
height: 400px;
overflow: hidden;
background-color: #202020;
color: #E8E8E8;
border-radius: 10px;
font-size: 15px;
}
</style>
<script src="js/dat.gui/dat.gui.min.js"></script>
<script src="js/glmatrix/gl-matrix.js"></script>
</head>
<body>
<div class="wrapper">
<aside class="aside aside-1">
<p align="center">
<b>Comandos</b>
<ul>
<li>
<b>w s</b> - avanzar y retroceder
</li>
<li>
<b>a d</b> - girar a la izquierda y derecha
</li>
<li>
<b>q e</b> - elevar o bajar la pala del montacargas
</li>
<li>
<b>g</b> - tomar un objeto de la impresora o dejarlo en un casillero libre de la estantería
</li>
<li>
<b>mouse</b> - arrastrar para desplazar la cámara
</li>
<li>
<b>1</b> - cámara en el centro de la escena
</li>
<li>
<b>2</b> - cámara centrada en la estantería
</li>
<li>
<b>3</b> - cámara centrada en la impresora
</li>
<li>
<b>4</b> - cámara de conductor
</li>
<li>
<b>5</b> - cámara trasera del montacargas
</li>
<li>
<b>6</b> - cámara lateral del montacargas
</li>
</ul>
</p>
</aside>
<aside class="aside aside-2">
<canvas id="my-canvas" width="1000" height="800">
Your browser does not support the HTML5 canvas element.
</canvas>
</aside>
</div>
<script type="text/javascript" src="js/glmatrix/gl-matrix.js"></script>
<script id="shader-vs" type="x-shader/x-vertex">
precision highp float;
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;
uniform mat4 normalMatrix;
// uniform vec3 uColor
// uniform highp float uShininess;
varying vec3 vNormal;
varying vec3 vPosWorld;
varying highp vec2 vTextureCoord;
void main(void) {
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(aVertexPosition, 1.0);
vPosWorld=(modelMatrix*vec4(aVertexPosition,1.0)).xyz; //la posicion en coordenadas de mundo
vNormal=(normalMatrix*vec4(aVertexNormal,1.0)).xyz; //la normal en coordenadas de mundo
vTextureCoord = aTextureCoord;
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
precision highp float;
varying vec3 vNormal;
varying vec3 vPosWorld;
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform vec3 uCameraPos;
uniform vec3 uColor;
uniform highp float uShininess;
uniform bool hasTexture;
uniform bool isCubeMap;
uniform samplerCube uCubeSampler;
struct SpotLightInfo {
vec4 position;
float intensityA;
float intensityD;
float intensityS;
vec3 direction;
float exponent; // exponente de atenuación
float cutoff;
};
uniform SpotLightInfo Spot1;
uniform SpotLightInfo Spot2;
uniform SpotLightInfo Spot3;
uniform SpotLightInfo Spot4;
uniform SpotLightInfo Spot5;
uniform SpotLightInfo Spot6;
uniform vec3 uPosPoint1;
uniform vec3 uPosPoint2;
uniform vec3 uPosPoint3;
uniform vec3 uPosPoint4;
vec3 calculateSpotlight( SpotLightInfo thisSpot, vec4 textureVec){
vec3 spotDir = normalize(thisSpot.direction);
vec3 s = normalize( vec3(thisSpot.position) - vPosWorld ); // surface to light
vec3 Ka = vec3(textureVec.s, textureVec.t, textureVec.p);
vec3 Kd = vec3(textureVec.s, textureVec.t, textureVec.p);
vec3 Ks = vec3(1,1,1);
float angle = acos( dot(s, -spotDir) );
float cutoff = radians( clamp( thisSpot.cutoff, 0.0, 90.0 ) );
vec3 ambient = thisSpot.intensityA * Ka;
if (angle < cutoff) {
float spotFactor = pow( dot(s, -spotDir), thisSpot.exponent );
vec3 v = normalize(uCameraPos - vPosWorld); // surface to view
vec3 n = normalize(vNormal);
vec3 h = normalize( v + s );
vec3 SpecularLight;
vec3 DiffuseLight = spotFactor * thisSpot.intensityD * Kd * max(dot(s, n), 0.0 );
if (uShininess == 0.0){
SpecularLight = vec3(0.0, 0.0, 0.0);
} else {
SpecularLight = spotFactor * thisSpot.intensityS * Ks * pow(dot(n, h), uShininess);
}
return max(ambient, DiffuseLight + SpecularLight);
} else {
return ambient;
}
}
vec3 calculatePointLight(vec3 lightPos, vec4 textureVec) {
vec3 Kd = vec3(1,0,0);
vec3 Ks = vec3(1,0,0);
vec3 n = normalize(vNormal);
vec3 s = normalize(lightPos - vPosWorld); // vector from vertex to light
vec3 v = normalize(uCameraPos - vPosWorld ); // vector from vertex to camera
vec3 h = normalize(v + s);
highp float factorAtenuacion = 1.0/(1.0 + 12.0 * length(vPosWorld - lightPos)); // decaimiento lineal
highp float intensityD = 1.0;
highp float intensityS = 0.8;
vec3 SpecularLight;
vec3 DiffuseLight = Kd * max(dot (n, s), 0.0 ) * intensityD * factorAtenuacion;
if (uShininess == 0.0) {
SpecularLight = vec3(0.0,0.0,0.0);
} else {
SpecularLight = Ks * pow(dot(n, h), uShininess) * intensityS * factorAtenuacion;
}
return DiffuseLight; // fix brillo especular -> introduce zonas color cyan
}
vec4 getCubeMapTexture() {
vec3 worldNormal = normalize(vNormal);
vec3 eyeToSurfaceDir = normalize(vPosWorld - uCameraPos);
vec3 direction = reflect(eyeToSurfaceDir, worldNormal);
return textureCube(uCubeSampler, direction);
}
void main(void) {
vec4 textureVec;
if (isCubeMap) {
textureVec = getCubeMapTexture();
} else if (hasTexture) {
textureVec = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
} else {
textureVec = vec4(uColor, 1.0);
}
vec3 totalSpotLight = calculateSpotlight(Spot1, textureVec) + calculateSpotlight(Spot2, textureVec) + calculateSpotlight(Spot3, textureVec) + calculateSpotlight(Spot4, textureVec) + calculateSpotlight(Spot5, textureVec) + calculateSpotlight(Spot6, textureVec);
vec3 totalPointLight = calculatePointLight(uPosPoint1, textureVec) + calculatePointLight(uPosPoint2, textureVec) + calculatePointLight(uPosPoint3, textureVec) + calculatePointLight(uPosPoint4, textureVec);
gl_FragColor = vec4( totalSpotLight + totalPointLight, 1.0);
}
</script>
<script type="module">
import { Escena } from './js/objects/Escena.js'
import { mouseWheelListener, mouseMoveListener, mouseUpListener, mouseDownListener, keyUpListener, keyDownListener } from './js/handle_comandos.js'
import { initMenu } from './js/gui.js'
import { Camara } from './js/camara.js'
var mat4 = glMatrix.mat4;
var vec3 = glMatrix.vec3;
var vec4 = glMatrix.vec4;
var canvas = null,
fragmentShader = null,
vertexShader = null;
var escena = null;
var camara = null;
var camaraInfo = null;
var modelMatrix = mat4.create();
var viewMatrix = mat4.create();
var projMatrix = mat4.create();
var normalMatrix = mat4.create();
function initWebGL() {
canvas = document.getElementById("my-canvas");
canvas.height = window.innerHeight - 50;
canvas.width = Math.min(window.innerWidth, canvas.width);
try {
window.gl = canvas.getContext("webgl");
} catch (e) {
alert("Error: Your browser does not appear to support WebGL.");
}
if (window.gl) {
setupWebGL();
initShaders();
escena = new Escena();
camara = new Camara(escena.getCarro(), escena.getPosImpresora(), escena.getPosEstanteria());
setupVertexShaderMatrix();
initMenu(escena);
setupListeners();
drawScene();
tick();
} else {
alert("Error: Your browser does not appear to support WebGL.");
}
}
function setupListeners() {
document.addEventListener('keydown', function (event) { keyDownListener(event, escena, camara) });
document.addEventListener('keyup', function (event) { keyUpListener(event, escena) });
document.addEventListener('mousedown', mouseDownListener);
document.addEventListener('mousemove', function (event) { mouseMoveListener(event, camara, canvas.width, canvas.height) });
document.addEventListener('mouseup', mouseUpListener);
document.addEventListener('wheel', function (event) { mouseWheelListener(event, camara) });
}
function setupWebGL() {
window.gl.enable(window.gl.DEPTH_TEST);
//set the clear color
window.gl.clearColor(0.1, 0.1, 0.2, 1.0);
window.gl.clear(window.gl.COLOR_BUFFER_BIT | window.gl.DEPTH_BUFFER_BIT);
window.gl.viewport(0, 0, canvas.width, canvas.height);
// Matrix de Proyeccion Perspectiva
mat4.perspective(projMatrix, 45, canvas.width / canvas.height, 0.1, 100.0);
// Matriz de vista
mat4.identity(viewMatrix);
mat4.translate(viewMatrix, viewMatrix, [0.0, 0.0, -5.0]);
}
function initShaders() {
//get shader source
var fs_source = document.getElementById('shader-fs').innerHTML,
vs_source = document.getElementById('shader-vs').innerHTML;
//compile shaders
vertexShader = makeShader(vs_source, window.gl.VERTEX_SHADER);
fragmentShader = makeShader(fs_source, window.gl.FRAGMENT_SHADER);
//create program
window.glProgram = window.gl.createProgram();
//attach and link shaders to the program
window.gl.attachShader(window.glProgram, vertexShader);
window.gl.attachShader(window.glProgram, fragmentShader);
window.gl.linkProgram(window.glProgram);
if (!window.gl.getProgramParameter(window.glProgram, window.gl.LINK_STATUS)) {
alert("Unable to initialize the shader program.");
}
//use program
window.gl.useProgram(window.glProgram);
}
function makeShader(src, type) {
//compile the vertex shader
var shader = window.gl.createShader(type);
window.gl.shaderSource(shader, src);
window.gl.compileShader(shader);
if (!window.gl.getShaderParameter(shader, window.gl.COMPILE_STATUS)) {
console.log("Error compiling shader: " + window.gl.getShaderInfoLog(shader));
}
return shader;
}
function setupVertexShaderMatrix() {
var modelMatrixUniform = window.gl.getUniformLocation(window.glProgram, "modelMatrix");
var viewMatrixUniform = window.gl.getUniformLocation(window.glProgram, "viewMatrix");
var projMatrixUniform = window.gl.getUniformLocation(window.glProgram, "projMatrix");
var normalMatrixUniform = window.gl.getUniformLocation(window.glProgram, "normalMatrix");
var samplerUniform = window.gl.getUniformLocation(window.glProgram, "uSampler");
var cubeMapSample = window.gl.getUniformLocation(window.glProgram, "uCubeSampler");
window.gl.uniform1i(samplerUniform, 0);
window.gl.uniform1i(cubeMapSample, 1);
window.gl.uniformMatrix4fv(modelMatrixUniform, false, modelMatrix); // REVISAR: no hay que hacer nada con la matriz modelMatrix? es solo la identidad
window.gl.uniformMatrix4fv(viewMatrixUniform, false, viewMatrix);
window.gl.uniformMatrix4fv(projMatrixUniform, false, projMatrix);
window.gl.uniformMatrix4fv(normalMatrixUniform, false, normalMatrix);
}
function drawScene() {
setupVertexShaderMatrix();
camaraInfo = camara.getCameraMatrix();
gl.uniformMatrix4fv(gl.getUniformLocation(glProgram, "viewMatrix"), false, camaraInfo.viewMatrix);
gl.uniform3fv(gl.getUniformLocation(glProgram, "uCameraPos"), camaraInfo.posCamara);
escena.dibujar(mat4.create());
}
function tick() {
requestAnimationFrame(tick);
window.gl.clear(window.gl.COLOR_BUFFER_BIT | window.gl.DEPTH_BUFFER_BIT);
window.gl.clearColor(0, 0, 0, 1);
drawScene();
}
window.onload = initWebGL;
</script>
</body>
</html>