-
Notifications
You must be signed in to change notification settings - Fork 1
/
billiards.html
401 lines (330 loc) · 13.2 KB
/
billiards.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
<html>
<head>
<title>Billiards</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="graphics.css">
<style>
html, body {
padding: 0px;
margin: 0px;
width: 100%;
height: 100%;
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
border: none;
}
</style>
<script type="text/javascript" src="common/MV.js"></script>
<script type="text/javascript" src="common/webgl-utils.js"></script>
<script type="text/javascript" src="common/initShaders.js"></script>
<script id="loading-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexUV;
uniform mat4 projectionMatrix;
varying mediump vec2 uv;
void main(void) {
gl_Position = projectionMatrix * vec4(vertexPosition, 1.0);
uv = vec2(vertexUV.s, 1.0 - vertexUV.t); // Flip UV's because OpenGL is backwards
}
</script>
<script id="loading-frag" type="x-shader/x-fragment">
precision mediump float;
varying mediump vec2 uv;
uniform sampler2D textureSampler;
void main(void) {
gl_FragColor = texture2D(textureSampler, uv);
}
</script>
<script id="text-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexUV;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying mediump vec2 uv;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
uv = vec2(vertexUV.s, 1.0 - vertexUV.t); // Flip UV's because OpenGL is backwards
}
</script>
<script id="text-frag" type="x-shader/x-fragment">
// Signed distance fields for crisp lettering, based on Chris Green's paper
// See here: <http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf>
precision mediump float;
varying mediump vec2 uv;
varying vec3 normal;
uniform sampler2D textureSampler;
uniform vec3 color;
// NOTE: Mesa+WebGL doesn't seem to support setting uniforms this early.
// IDK if that's part of the WebGL spec, or a limitation of Mesa, or
// what. Beware magic constants in the rest of this shader.
// uniform float SOFT_EDGE_DISTANCE = 0.01;
void main(void) {
vec4 baseColor = texture2D(textureSampler, uv);
// Get rid of jaggies
float softEdgeDistance = 0.01; // SOFT_EDGE_DISTANCE
float distAlphaMask = baseColor.a;
float alpha = smoothstep(0.5 - softEdgeDistance, 0.5 + softEdgeDistance, distAlphaMask);
/*
float alpha;
if (baseColor.a > 0.5) {
alpha = 1.0;
} else {
alpha = 0.0;
}
*/
gl_FragColor = vec4(color, 1.0 - alpha);
}
</script>
<script id="billiardtable-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexUV;
attribute vec3 vertexNormal;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying mediump vec2 uv;
varying vec3 normal;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
uv = vec2(vertexUV.s, 1.0 - vertexUV.t); // Flip UV's because OpenGL is backwards
normal = vertexNormal;
}
</script>
<script id="billiardtable-frag" type="x-shader/x-fragment">
precision mediump float;
varying mediump vec2 uv;
varying vec3 normal;
uniform sampler2D textureSampler;
void main(void) {
gl_FragColor = texture2D(textureSampler, uv);
}
</script>
<script id="cuestick-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexUV;
attribute vec3 vertexNormal;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying mediump vec2 uv;
varying vec3 normal;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
uv = vec2(vertexUV.s, 1.0 - vertexUV.t); // Flip UV's because OpenGL is backwards
normal = vertexNormal;
}
</script>
<script id="billiardball-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexUV;
attribute vec3 vertexNormal;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying mediump vec2 uv;
varying vec3 normal;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
uv = vec2(vertexUV.s, 1.0 - vertexUV.t); // Flip UV's because OpenGL is backwards
normal = vertexNormal;
}
</script>
<script id="billiardball-frag" type="x-shader/x-fragment">
precision mediump float;
varying mediump vec2 uv;
varying vec3 normal;
uniform sampler2D textureSampler;
void main(void) {
gl_FragColor = vec4(texture2D(textureSampler, uv).rgb, 1.0);
}
</script>
<script id="billiardball-sdf-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexUV;
attribute vec3 vertexNormal;
uniform mat4 modelViewMatrix;
uniform mediump mat4 projectionMatrix;
varying mediump vec2 uv;
varying vec3 normal;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
uv = vec2(vertexUV.s, 1.0 - vertexUV.t); // Flip UV's because OpenGL is backwards
normal = vertexNormal;
}
</script>
<script id="billiardball-sdf-frag" type="x-shader/x-fragment">
// Signed distance fields for crisp lettering, based on Chris Green's paper
// See here: <http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf>
precision mediump float;
varying mediump vec2 uv;
varying vec3 normal;
uniform mediump mat4 projectionMatrix;
uniform sampler2D nearTexture;
uniform sampler2D farTexture;
uniform sampler2D numberMask;
uniform vec3 outsideColor;
uniform vec3 insideColor;
void main(void) {
// Math for Z-depth from gl_fragCoord.z comes from Lengyel,
// "Mathematics for 3D Game Programming and Computer Graphics, Third
// Edition" page 114.
float a = projectionMatrix[2][2];
float b = projectionMatrix[3][2];
float zPos = -(((gl_FragCoord.z + a)*(-1.0/b))/1.0 + 1.0);
vec4 baseColor;
if (zPos < -0.2) { // Arbitrary, 0.2 meters
// Use our far texture for balls far away
baseColor = texture2D(farTexture, uv);
} else {
// Use our texture for balls close up
baseColor = texture2D(nearTexture, uv);
}
float distAlphaMask = baseColor.a;
vec4 numberMask = texture2D(numberMask, uv);
if (distAlphaMask <= 0.5) {
if (numberMask.a > 0.9) {
// Make the lettering black
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
} else {
gl_FragColor = vec4(insideColor.rgb, 1.0);
}
} else {
gl_FragColor = vec4(outsideColor.rgb, 1.0);
}
}
</script>
<script id="billiardball-sdf-smooth-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexUV;
attribute vec3 vertexNormal;
uniform mat4 modelViewMatrix;
uniform mediump mat4 projectionMatrix;
varying mediump vec2 uv;
varying vec3 normal;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
uv = vec2(vertexUV.s, 1.0 - vertexUV.t); // Flip UV's because OpenGL is backwards
normal = vertexNormal;
}
</script>
<script id="billiardball-sdf-smooth-frag" type="x-shader/x-fragment">
// Signed distance fields for crisp lettering, based on Chris Green's paper
// See here: <http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf>
precision mediump float;
varying mediump vec2 uv;
varying vec3 normal;
uniform mediump mat4 projectionMatrix;
uniform sampler2D nearTexture;
uniform sampler2D farTexture;
uniform sampler2D numberMask;
uniform vec3 outsideColor;
uniform vec3 insideColor;
// NOTE: Mesa+WebGL doesn't seem to support setting uniforms this early.
// IDK if that's part of the WebGL spec, or a limitation of Mesa, or
// what. Beware magic constants in the rest of this shader.
// uniform float FAR_TEXTURE_DISTANCE = 0.5; // Arbitrary, 0.5 meters
// The range of distance values for the near and far textures are
// completely different, so we have to smooth out over two different
// distances. These values are chosen through experimentation to see what
// looks good.
// uniform float SOFT_EDGE_DISTANCE_NEAR = 0.01;
// uniform float SOFT_EDGE_DISTANCE_FAR = 0.003;
void main(void) {
// Math for Z-depth from gl_fragCoord.z comes from Lengyel,
// "Mathematics for 3D Game Programming and Computer Graphics, Third
// Edition" page 114.
float a = projectionMatrix[2][2];
float b = projectionMatrix[3][2];
float zPos = -(((gl_FragCoord.z + a)*(-1.0/b))/1.0 + 1.0);
vec4 baseColor;
float softEdgeDistance;
if (zPos < -0.5 /* FAR_TEXTURE_DISTANCE */) {
// Use our far texture for balls far away
baseColor = texture2D(farTexture, uv);
softEdgeDistance = 0.003; // SOFT_EDGE_DISTANCE_FAR;
/*
// Useful for debugging near/far texture distance
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
return;
*/
} else {
// Use our texture for balls close up
baseColor = texture2D(nearTexture, uv);
softEdgeDistance = 0.01; // SOFT_EDGE_DISTANCE_NEAR;
/*
// Useful for debugging near/far texture distance
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
return;
*/
}
float distAlphaMask = baseColor.a;
// NOTE: In Green's paper, he uses the smoothstep function to smooth
// alpha values for the edges of the cutouts. We're not using cutouts,
// but we can use the same technique to smooth the edge by weighting
// each of the two colors.
float colorWeight = smoothstep(0.5 - softEdgeDistance, 0.5 + softEdgeDistance, distAlphaMask);
vec4 numberMask = texture2D(numberMask, uv);
if (numberMask.a > 0.9) {
// Make the lettering black
gl_FragColor = vec4(
colorWeight * outsideColor.rgb +
(1.0 - colorWeight) * vec3(0.0, 0.0, 0.0), 1.0);
} else {
gl_FragColor = vec4(
colorWeight * outsideColor.rgb +
(1.0 - colorWeight) * insideColor.rgb, 1.0);
}
}
</script>
<script id="cuestick-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
attribute vec2 vertexUV;
attribute vec3 vertexNormal;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying mediump vec2 uv;
varying vec3 normal;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
uv = vec2(vertexUV.s, 1.0 - vertexUV.t); // Flip UV's because OpenGL is backwards
normal = vertexNormal;
}
</script>
<script id="cuestick-frag" type="x-shader/x-fragment">
precision mediump float;
varying mediump vec2 uv;
varying vec3 normal;
uniform sampler2D textureSampler;
uniform float fadeAlpha;
void main(void) {
gl_FragColor = vec4(texture2D(textureSampler, uv).rgb, fadeAlpha);
}
</script>
<script id="debug-vert" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
void main(void) {
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
}
</script>
<script id="debug-frag" type="x-shader/x-fragment">
precision mediump float;
void main(void) {
gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
}
</script>
<!------------------------------------------------------------>
<!-- This is the file you will modify -->
<!------------------------------------------------------------>
<script type="text/javascript" src="billiards.js"></script>
</head>
<body>
<!-- Make sure the loading screen loads early, for convenience; no sense starting without it -->
<img id="loading-screen" src="common/loading_screen.jpg" style="display: none;"/>
<img id="rules-screen" src="common/rules_screen.jpg" style="display: none;"/>
<img id="controls-screen" src="common/controls_screen.jpg" style="display: none;"/>
<img id="notes-screen" src="common/notes_screen.jpg" style="display: none;"/>
<canvas id="gl-canvas"></canvas>
</body>
</html>