-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pacman-3D.html
275 lines (165 loc) · 7.5 KB
/
Pacman-3D.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
<!DOCTYPE html>
<html style="width:95%; height:90%">
<head>
<title>Pacman 3D</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec2 vTextureCoord;
varying vec4 fColor;
// For the Phong Illumination Model
attribute vec3 vNormal;
uniform vec4 viewerPosition;
// Array of lights
#define MAX_LIGHTS 5
uniform int numLights;
uniform struct Light {
vec4 position;
mat4 lightSourceMatrix;
vec3 intensities;
vec3 ambientIntensities;
} allLights[MAX_LIGHTS];
// The material properties
uniform vec3 k_ambient;
uniform vec3 k_diffuse;
uniform vec3 k_specular;
uniform float shininess;
// Spotlight properties
uniform int difficulty;
uniform float threshold;
void main(void) {
// To allow seeing the points drawn
gl_PointSize = 5.0;
// Converting the (x,y,z) vertices to Homogeneous Coord.
// And multiplying by the Projection and the Model-View matrix
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
// Phong Illumination Model
// Pos is vertex position after applying the global transformation
vec3 pos = (uMVMatrix * vec4(aVertexPosition, 1.0)).xyz;
fColor = vec4(0.0, 0.0, 0.0, 0.0);
for( int i = 0; i < MAX_LIGHTS; i++ )
{
if( i == numLights )
break;
// Ambient component is constant for each light source
vec4 ambient = vec4( k_ambient * allLights[i].ambientIntensities, 1.0 );
fColor += ambient;
// Vector from vertex position to light source
vec3 L, X;
// Check for directional light
if(allLights[i].position.w == 0.0)
L = normalize( (allLights[i].lightSourceMatrix * allLights[i].position).xyz );
else {
X = (allLights[i].lightSourceMatrix * allLights[i].position).xyz - pos;
L = normalize( (allLights[i].lightSourceMatrix * allLights[i].position).xyz - pos );
}
// Draw spotlight in hard difficulty (threshold = 3.0)
if (difficulty == 1 && sqrt(X[0] * X[0] + X[1] * X[1] + X[2] * X[2]) > threshold)
continue;
// Vector from the vertex position to the eye
vec3 E;
// The viewer is at the origin or at an indefinite distance
// On the ZZ axis
if(viewerPosition.w == 1.0)
// At the origin
E = -normalize( pos );
else
// On the ZZ axis
E = vec3(0,0,1);
// Halfway vector
vec3 H = normalize( L + E );
// Transform vertex normal into eye coordinates
vec4 N = normalize( uMVMatrix * vec4(vNormal, 0.0));
// Compute terms in the illumination equation
// Diffuse component
float dotProductLN = L[0] * N[0] + L[1] * N[1] + L[2] * N[2];
float cosNL = max( dotProductLN, 0.0 );
vec4 diffuse = vec4( k_diffuse * cosNL * allLights[i].intensities, 1.0 );
// Specular component
float dotProductNH = N[0] * H[0] + N[1] * H[1] + N[2] * H[2];
float cosNH = pow( max( dotProductNH, 0.0 ), shininess );
vec4 specular = vec4( k_specular * cosNH * allLights[i].intensities, 1.0 );
if( dotProductLN < 0.0 ) {
specular = vec4(0.0, 0.0, 0.0, 1.0);
}
// Adding the components
fColor += diffuse + specular;
}
// Converting the RGB color value to RGBA
vTextureCoord = aTextureCoord;
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
varying vec4 fColor;
uniform sampler2D uSampler;
void main(void) {
// Using the passed vertex color * texture
gl_FragColor = fColor * texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
}
</script>
<!-- Some useful functions for browser compatibility -->
<script type="text/javascript" src="webgl-utils.js"></script>
<!-- Handling vectors and matrices -->
<script type="text/javascript" src="maths.js"></script>
<!-- Processing triangle mesh models -->
<script type="text/javascript" src="models.js"></script>
<!-- Creating the light sources -->
<script type="text/javascript" src="lightSources.js"></script>
<!-- WebGL code -->
<script type="text/javascript" src="initShaders.js"></script>
<script type="text/javascript" src="Pacman-3D.js"></script>
</head>
<body onload="runWebGL();" style="background: black; width:100%; height:100%">
<!-- Pacman logo -->
<div align="center" style="height:10%;">
<img src="assets/logo.gif" style="height:100%;"/>
</div>
<!-- Intro page -->
<div align="center" id="welcome-screen" style="display:block;color:white; width:80%; height:80%; border-style: solid; border-width:5%; border-radius: 30%; margin:0 auto;">
<br/>
<span style="font-size:100%;">
<u>INSTRUCTIONS</u>:
<br/>
<br/>1. USE THE ARROWS TO MOVE PACMAN
<br/>
<br/>2. USE MOUSE DRAG OR W,A,S,D KEYS TO CHANGE CAMERA POSITION
<br/>
<br/>3. USE MOUSE WHEEL TO ZOOM IN OR OUT
<br/>
<br/>4. IF YOU WANT TO USE A CUSTOM FIELD, UPLOAD A FILE BELOW WITH A
<br/> MATRIX M x N WITH f FOR FOOD, s FOR SUPER-FOOD, w FOR WALL AND p FOR PORTALS, SEPARATED BY COMMAS.
<br/>
<br/>
<br/>PLAY THIS GAME IN FULLSCREEN MODE FOR A BETTER EXPERIENCE
</span>
<br/>
<br/>
<br/>
<input type="file" id="file" name="file">
<br/>
<h3 id="field-error" style="color:red;"></h3>
<br/>
GAME DIFFICULTY:
<select id="difficulty">
<option selected="selected" value="0">Easy</option>
<option value="1">Hard</option>
</select>
<br/>
<br/>
<span style="font-size:25px;color:yellow;">PRESS SPACE TO START!</span>
</div>
<!-- Game page -->
<div id="game" align="center" style="display:none; width:100%; height:90%;">
<canvas id="my-canvas" width="900" height="700" style="border:none; width:auto; height:80%;"></canvas>
<h3 id="score" style="color:yellow;width:auto;height:5%;font-size:100%;"></h3>
<h1 id="super-mode" style="color:red;width:auto;height:5%;font-size:100%;"></h1>
<h1 id="result" style="color:yellow;width:auto;height:5%;font-size:100%;"></h2>
<button id="restart" type="button" onclick="restartGame()" style="display:none;height:5%;font-size:100%;">Restart Game</button>
</div>
</body>
</html>