forked from maticstric/WebGL-Breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Breakout.js
700 lines (566 loc) · 18.8 KB
/
Breakout.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Vertex shader
var VSHADER_SOURCE =`
precision mediump float;
attribute vec3 a_Position;
attribute vec2 a_UV;
attribute vec4 a_Color;
attribute vec3 a_Normal;
varying vec3 v_Position;
varying vec2 v_UV;
varying vec4 v_Color;
varying vec3 v_Normal;
uniform mat4 u_NormalMatrix;
uniform mat4 u_ModelMatrix;
uniform mat4 u_ProjectionMatrix;
uniform mat4 u_ViewMatrix;
void main() {
v_UV = a_UV;
v_Color = a_Color;
v_Normal = vec3(u_NormalMatrix * vec4(a_Normal, 1.0));
v_Position = vec3(u_ModelMatrix * vec4(a_Position, 1.0));
gl_Position = u_ProjectionMatrix * u_ViewMatrix * u_ModelMatrix * vec4(a_Position, 1.0);
}`;
// Fragment shader
var FSHADER_SOURCE = `
precision mediump float;
varying vec3 v_Position;
varying vec2 v_UV;
varying vec4 v_Color;
varying vec3 v_Normal;
uniform float u_TextureWeight;
uniform int u_TextureNum;
// Copy pasted, remove any not needed
uniform sampler2D u_Sampler0;
uniform sampler2D u_Sampler1;
uniform sampler2D u_Sampler2;
uniform sampler2D u_Sampler3;
uniform sampler2D u_Sampler4;
uniform bool u_LightingOn;
uniform vec3 u_LightPosition;
uniform vec3 u_LightColor;
uniform vec3 u_CameraPosition;
void main() {
// Calculate combined texture and base color
float t = u_TextureWeight;
vec4 texColor;
vec4 vertexColor = v_Color;
vec4 baseColor;
if (u_TextureNum == 0) {
texColor = texture2D(u_Sampler0, v_UV); // use texture0
baseColor = (1.0 - t) * vertexColor + t * texColor;
} else if (u_TextureNum == 1) {
texColor = texture2D(u_Sampler1, v_UV); // use texture1
baseColor = (1.0 - t) * vertexColor + t * texColor;
} else if (u_TextureNum == 2) {
texColor = texture2D(u_Sampler2, v_UV); // use texture2
baseColor = (1.0 - t) * vertexColor + t * texColor;
} else if (u_TextureNum == 3) {
texColor = texture2D(u_Sampler3, v_UV); // use texture3
baseColor = (1.0 - t) * vertexColor + t * texColor;
} else if (u_TextureNum == 4) {
texColor = texture2D(u_Sampler4, v_UV); // use texture4
baseColor = (1.0 - t) * vertexColor + t * texColor;
} else {
baseColor = vertexColor;
}
if (u_LightingOn) {
// Lighting
vec3 normal = normalize(v_Normal);
vec3 lightDirection = normalize(u_LightPosition - v_Position);
vec3 reflect = reflect(-lightDirection, normal);
vec3 cameraDirection = normalize(u_CameraPosition - v_Position);
float nDotL = max(dot(normal, lightDirection), 0.0);
float eDotR = pow(max(dot(cameraDirection, reflect), 0.0), 100.0);
vec3 ambient = baseColor.rgb * 0.7;
vec3 diffuse = baseColor.rgb * nDotL * u_LightColor;
vec3 specular = baseColor.rgb * eDotR * u_LightColor;
gl_FragColor = vec4(ambient + diffuse + specular, baseColor.a);
} else {
gl_FragColor = baseColor;
}
}`;
const SLIDER_LENGTH = 100;
const MAX_SENSITIVITY = 0.1;
const NUM_LIVES = 3;
const SKY1_Y_SPEED = 0.05;
const SKY1_Z_SPEED = 0.06;
const SKY2_X_SPEED = 0.1;
const SKY2_Y_SPEED = 0.09;
let g_mouseSensitivity = MAX_SENSITIVITY / 2;
let g_gameStarted = false;
let g_lives = NUM_LIVES;
let g_livesText;
let g_currentLevel = 1;
let g_paddle_bounce_audio = new Audio('audio/paddle_bounce.mp3');
let g_wall_bounce_audio = new Audio('audio/wall_bounce.mp3');
let g_tile_bounce_audio_1 = new Audio('audio/tile_bounce_1.mp3');
let canvas;
let gl;
let g_dataPerVertex = 12;
let g_camera = new Camera();
let g_ball = new Ball();
let g_paddle = new Paddle();
let g_sky1 = new Sky(50, 3);
let g_sky2 = new Sky(28, 4);
let g_tiles;
let g_tilesOriginalLength;
let g_walls;
let a_Position;
let a_UV;
let a_Color;
let a_Normal;
let u_NormalMatrix;
let u_TextureWeight;
let u_TextureNum;
let u_ModelMatrix;
let u_ProjectionMatrix;
let u_ViewMatrix;
let u_LightingOn;
let u_LightColor;
let u_LightPosition;
let u_CameraPosition;
let u_Sampler0;
let u_Sampler1;
let u_Sampler2;
let u_Sampler3;
let u_Sampler4;
function main() {
// WebGL and GLSL setup
setupWebGL();
initializeShaders();
connectVariablesToGLSL();
setupBuffer();
initializeTextures();
setupHTMLElements();
setupControls();
window.addEventListener('resize', resize, false);
g_tiles = TileGrid.generateLevel1(7, 7, 0.3);
g_walls = TileGrid.generateWalls();
// Specify the color for clearing <canvas>
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Render
requestAnimationFrame(tick);
}
function tick() {
resize();
renderAllShapes();
requestAnimationFrame(tick);
}
function resize() {
let canvasRatio = canvas.height / canvas.width;
let viewHeight = window.innerHeight - parseInt($('.center').css("marginTop")) * 2 -
$("#lowerContent").outerHeight(true);
let viewWidth = window.innerWidth;
let viewRatio = viewHeight / window.innerWidth;
let newWidth;
let newHeight;
if (viewRatio < canvasRatio) {
newHeight = viewHeight;
newWidth = newHeight / canvasRatio;
} else {
newWidth = viewWidth;
newHeight = newWidth * canvasRatio;
}
canvas.style.width = newWidth + 'px';
canvas.style.height = newHeight + 'px';
}
function renderAllShapes() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
/* CAMERA */
let projectionMatrix = new Matrix4();
projectionMatrix.setPerspective(g_camera.fov, canvas.width / canvas.height, g_camera.near, g_camera.far);
gl.uniformMatrix4fv(u_ProjectionMatrix, false, projectionMatrix.elements);
let viewMatrix = new Matrix4();
viewMatrix.setLookAt(
g_camera.eye.elements[0], g_camera.eye.elements[1], g_camera.eye.elements[2],
g_camera.lookAt.elements[0], g_camera.lookAt.elements[1], g_camera.lookAt.elements[2],
g_camera.up.elements[0], g_camera.up.elements[1], g_camera.up.elements[2]
);
gl.uniformMatrix4fv(u_ViewMatrix, false, viewMatrix.elements);
/* FOR LIGHTING */
gl.uniform3f(u_LightPosition, g_ball.positionX, g_ball.positionY, g_ball.positionZ);
gl.uniform3f(u_CameraPosition, g_camera.eye.elements[0], g_camera.eye.elements[1], g_camera.eye.elements[2]);
gl.uniform3fv(u_LightColor, new Float32Array(g_ball.color.slice(0, 3)));
/* CUBES */
// Draw tiles
g_tiles.forEach((t) => {
t.model.render();
});
// Draw walls
g_walls.forEach((w) => {
w.model.render();
});
// Update sky1 rotation
g_sky1.rotate(SKY1_Y_SPEED, 0, 1, 0);
g_sky1.rotate(SKY1_Z_SPEED, 0, 0, 1);
// Draw sky box
g_sky1.model.render();
// Update sky2 rotation
g_sky2.rotate(SKY2_X_SPEED, 1, 0, 0);
g_sky2.rotate(SKY2_Y_SPEED, 0, 1, 0);
// Draw sky box
g_sky2.model.render();
// Draw g_paddle
g_paddle.model.render();
/* SPHERES */
// Update ball position
g_ball.move();
// Draw g_ball
g_ball.model.render();
}
function checkHasWon() {
if (g_tiles.length == 0) {
g_gameStarted = false;
g_currentLevel++;
// Reset speed
g_ball.velocity.normalize();
g_ball.velocity.mul(Ball.MIN_SPEED);
switch(g_currentLevel) {
case 2:
g_tiles = TileGrid.generateLevel2(10, 7, 0.3);
break;
case 3:
g_tiles = TileGrid.generateLevel3(10, 7, 0.3);
break;
case 4:
// Same as level 1, but bigger
g_tiles = TileGrid.generateLevel1(18, 7, 0.3);
break;
default:
g_tiles = TileGrid.generateLevel1(7, 7, 0.3);
g_currentLevel = 1;
g_livesText.innerHTML = "You Win! 😊";
g_lives = NUM_LIVES;
}
}
}
function livesString() {
let string = "";
for (let i = 0; i < g_lives; i ++) {
string += "💛";
}
return string;
}
function startGame() {
g_gameStarted = true;
if (g_lives == NUM_LIVES) {
g_livesText.innerHTML = livesString();
}
let randomX = Math.random() * 2 - 1; // between -1 and 1
let randomY = Math.random() * (1 - Math.sqrt(2) / 2) + Math.sqrt(2) / 2; // between 0 and sqrt(2) / 2
let velocity = new Vector3([randomX, randomY, 0]);
velocity.normalize();
if (g_ball.velocity.magnitude() === 0){
velocity.mul(Ball.MIN_SPEED);
} else {
velocity.mul(g_ball.velocity.magnitude());
}
g_ball.velocity = velocity;
}
function endGame(){
g_lives --;
g_gameStarted = false;
g_livesText.innerHTML = livesString();
if (g_lives == 0) {
g_ball.velocity = new Vector3([0, 0, 0]);
g_tiles = TileGrid.generateLevel1(7, 7, 0.3);
g_lives = NUM_LIVES;
g_livesText.innerHTML = "Game Over 😒";
}
}
function mouseMove(e) {
// Only use the x direction since paddle moves horizontally
let moveDirection = e.movementX * g_mouseSensitivity;
g_paddle.mouseMove(moveDirection); // Update the paddle position
}
function pointerLockChange(){
if (document.pointerLockElement === canvas||
document.mozPointerLockElement === canvas||
document.webkitPointerLockElement === canvas) {
// Pointer was just locked, enable the mousemove listener
document.addEventListener("mousemove", mouseMove, false);
} else {
// Pointer was just unlocked, disable the mousemove listener
document.removeEventListener("mousemove", mouseMove, false);
}
}
function setupControls() {
// Pointer lock setup to work for several browsers
canvas.requestPointerLock = canvas.requestPointerLock ||
canvas.mozRequestPointerLock ||
canvas.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock ||
document.mozExitPointerLock ||
document.webkitExitPointerLock;
// Hook pointer lock state change events
document.addEventListener('pointerlockchange', pointerLockChange, false);
document.addEventListener('mozpointerlockchange', pointerLockChange,
false);
document.addEventListener('webkitpointerlockchange', pointerLockChange,
false);
// Ask the browser to lock the pointer on canvas click
canvas.onclick = function() {
if(document.pointerLockElement !== canvas &&
document.mozPointerLockElement !== canvas &&
document.webkitPointerLockElement !== canvas) {
canvas.requestPointerLock();
}
};
// Add listener to start game
document.addEventListener('keydown', (event) => {
if (event.key === ' ') {
if (!g_gameStarted) {
startGame();
}
}
});
}
function setupHTMLElements() {
document.getElementById("sensitivity").oninput = function() {
g_mouseSensitivity = this.value / SLIDER_LENGTH * MAX_SENSITIVITY;
};
g_livesText = document.getElementById("lives");
g_livesText.innerHTML = livesString();
}
function sendImageToGLSL(n, image) {
var texture = gl.createTexture(); // Create a texture object
if (!texture) {
console.log('Failed to create the texture object');
return false;
}
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // flip the image's y axis
// Enable the tecture unit
switch (n) {
case 0:
gl.activeTexture(gl.TEXTURE0);
break;
case 1:
gl.activeTexture(gl.TEXTURE1);
break;
case 2:
gl.activeTexture(gl.TEXTURE2);
break;
case 3:
gl.activeTexture(gl.TEXTURE3);
break;
case 4:
gl.activeTexture(gl.TEXTURE4);
break;
}
// Bind the texture object to the target
gl.bindTexture(gl.TEXTURE_2D, texture);
// Set the texture parameters
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
// Set the tecture image
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// Set the texture until 0 to the sampler
switch (n) {
case 0:
gl.uniform1i(u_Sampler0, 0);
break;
case 1:
gl.uniform1i(u_Sampler1, 1);
break;
case 2:
gl.uniform1i(u_Sampler2, 2);
break;
case 3:
gl.uniform1i(u_Sampler3, 3);
break;
case 4:
gl.uniform1i(u_Sampler4, 4);
break;
}
}
function initializeTextures() {
var image0 = new Image(); // Create the Image object
if (!image0) {
console.log('Failed to create the image object');
return false;
}
var image1 = new Image();
if (!image1) {
console.log('Failed to create the image object');
return false;
}
var image2 = new Image();
if (!image2) {
console.log('Failed to create the image object');
return false;
}
var image3 = new Image();
if (!image3) {
console.log('Failed to create the image object');
return false;
}
var image4 = new Image();
if (!image4) {
console.log('Failed to create the image object');
return false;
}
// Register the event handler to be called on loading an image
image0.onload = function(){ sendImageToGLSL(0, image0); };
image1.onload = function(){ sendImageToGLSL(1, image1); };
image2.onload = function(){ sendImageToGLSL(2, image2); };
image3.onload = function(){ sendImageToGLSL(3, image3); };
image4.onload = function(){ sendImageToGLSL(4, image4); };
// Tell the browser to load the image
image0.src = 'textures/paddle.png';
image1.src = 'textures/tile.png';
image2.src = 'textures/wall.png';
image3.src = 'textures/sky1.png';
image4.src = 'textures/sky2.png';
return true;
}
function setupBuffer() {
// Create a buffer object
let vertexBuffer = gl.createBuffer();
if(!vertexBuffer) {
console.log('Failed to create the buffer object');
return -1;
}
// Bind the buffer object to target
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
let F_SIZE = Float32Array.BYTES_PER_ELEMENT;
// Assign the buffer object to a_Position variable and enable
gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, F_SIZE * g_dataPerVertex, 0);
gl.enableVertexAttribArray(a_Position);
// Assign the buffer object to a_UV variable and enable
gl.vertexAttribPointer(a_UV, 2, gl.FLOAT, false, F_SIZE * g_dataPerVertex, 3 * F_SIZE);
gl.enableVertexAttribArray(a_UV);
// Assign the buffer object to a_Color variable and enable
gl.vertexAttribPointer(a_Color, 4, gl.FLOAT, false, F_SIZE * g_dataPerVertex, 5 * F_SIZE);
gl.enableVertexAttribArray(a_Color);
// Assign the buffer object to a_Normal variable and enable
gl.vertexAttribPointer(a_Normal, 3, gl.FLOAT, false, F_SIZE * g_dataPerVertex, 9 * F_SIZE);
gl.enableVertexAttribArray(a_Normal);
}
function connectVariablesToGLSL() {
// Get the storage location of a_Position
a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < 0) {
console.log('Failed to get the storage location of a_Position');
//return;
}
// Get the storage location of a_UV
a_UV = gl.getAttribLocation(gl.program, 'a_UV');
if (a_UV < 0) {
console.log('Failed to get the storage location of a_UV');
//return;
}
// Get the storage location of a_Color
a_Color = gl.getAttribLocation(gl.program, 'a_Color');
if (a_Color < 0) {
console.log('Failed to get the storage location of a_Color');
//return;
}
// Get the storage location of a_Normal
a_Normal = gl.getAttribLocation(gl.program, 'a_Normal');
if (a_Normal < 0) {
console.log('Failed to get the storage location of a_Normal');
//return;
}
// Get the storage location of u_LightingOn
u_LightingOn = gl.getUniformLocation(gl.program, 'u_LightingOn');
if (!u_LightingOn) {
console.log('Failed to get the storage location of u_LightingOn');
}
// Get the storage location of u_TextureWeight
u_TextureWeight = gl.getUniformLocation(gl.program, 'u_TextureWeight');
if (!u_TextureWeight) {
console.log('Failed to get the storage location of u_TextureWeight');
//return;
}
// Get the storage location of u_TextureNum
u_TextureNum = gl.getUniformLocation(gl.program, 'u_TextureNum');
if (!u_TextureNum) {
console.log('Failed to get the storage location of u_TextureNum');
//return;
}
// Get the storage location of u_NormalMatrix
u_NormalMatrix = gl.getUniformLocation(gl.program, 'u_NormalMatrix');
if (!u_NormalMatrix) {
console.log('Failed to get the storage location of u_NormalMatrix');
//return;
}
// Get the storage location of u_ModelMatrix
u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
if (!u_ModelMatrix) {
console.log('Failed to get the storage location of u_ModelMatrix');
//return;
}
// Get the storage location of u_ProjectionMatrix
u_ProjectionMatrix = gl.getUniformLocation(gl.program, 'u_ProjectionMatrix');
if (!u_ProjectionMatrix) {
console.log('Failed to get the storage location of u_ProjectionMatrix');
//return;
}
// Get the storage location of u_ViewMatrix
u_ViewMatrix = gl.getUniformLocation(gl.program, 'u_ViewMatrix');
if (!u_ViewMatrix) {
console.log('Failed to get the storage location of u_ViewMatrix');
//return;
}
// Get the storage location of u_LightColor
u_LightColor = gl.getUniformLocation(gl.program, 'u_LightColor');
if (!u_LightColor) {
console.log('Failed to get the storage location of u_LightColor');
//return;
}
// Get the storage location of u_Sampler0
u_Sampler0 = gl.getUniformLocation(gl.program, 'u_Sampler0');
if(!u_Sampler0) {
console.log('Failed to get the storage location of u_Sampler0');
}
// Get the storage location of u_Sampler1
u_Sampler1 = gl.getUniformLocation(gl.program, 'u_Sampler1');
if(!u_Sampler1) {
console.log('Failed to get the storage location of u_Sampler1');
}
// Get the storage location of u_Sampler2
u_Sampler2 = gl.getUniformLocation(gl.program, 'u_Sampler2');
if(!u_Sampler2) {
console.log('Failed to get the storage location of u_Sampler2');
}
// Get the storage location of u_Sampler3
u_Sampler3 = gl.getUniformLocation(gl.program, 'u_Sampler3');
if(!u_Sampler3) {
console.log('Failed to get the storage location of u_Sampler3');
}
// Get the storage location of u_Sampler4
u_Sampler4 = gl.getUniformLocation(gl.program, 'u_Sampler4');
if(!u_Sampler4) {
console.log('Failed to get the storage location of u_Sampler4');
}
// Get the storage location of u_LightPosition
u_LightPosition = gl.getUniformLocation(gl.program, 'u_LightPosition');
if (!u_LightPosition) {
console.log('Failed to get the storage location of u_LightPosition');
}
// Get the storage location of u_CameraPosition
u_CameraPosition = gl.getUniformLocation(gl.program, 'u_CameraPosition');
if (!u_CameraPosition) {
console.log('Failed to get the storage location of u_CameraPosition');
}
}
function initializeShaders() {
// Initialize shaders
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}
}
function setupWebGL() {
// Retrieve <canvas> element
canvas = document.getElementById('webgl', false);
// Get the rendering context for WebGL
gl = getWebGLContext(canvas, false);
gl.enable(gl.BLEND);
gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
gl.enable(gl.DEPTH_TEST);
}