Source

halfblock / index.html

Full commit
  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
<!DOCTYPE html>
<html>

<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>Halfblock</title>
<script type="text/javascript" src="gl-matrix.js"></script>
<script type="text/javascript" src="perlin.js"></script>
<script type="text/javascript" src="mapgen.js"></script>
<script type="text/javascript" src="main.js"></script>

<!-- Shaders for wireframe selection box -->
<script id="wireframe-vs" type="x-shader/x-vertex">
  attribute vec3 aPos;

  uniform mat4 uMVMatrix;
  uniform mat4 uPMatrix;

  void main(void) {
    gl_Position = uPMatrix * uMVMatrix * vec4(aPos, 1.0);
  }
</script>
<script id="wireframe-fs" type="x-shader/x-fragment">
  precision mediump float;
  void main(void) {
    gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  }
</script>

<!-- Shaders for particles -->
<script id="particle-vs" type="x-shader/x-vertex">
  attribute vec3 aInitialPos;
  attribute vec3 aVelocity;
  attribute float aBirthday;
  //attribute float aDeathday;
  attribute vec2 aTexCoord;

  uniform float uClock;
  uniform float uGravity;
  uniform mat4 uMVMatrix;
  uniform mat4 uPMatrix;

  varying vec2 vTexCoord;

  void main(void) {
    vTexCoord = aTexCoord;
    if (aBirthday > uClock /*|| uClock > aDeathday*/) {
      gl_Position = vec4(-9999.0, -9999.0, -9999.0, 1.0);
    } else {
      float age = uClock - aBirthday;
      vec3 pos = aInitialPos + age * aVelocity;
      pos.y -= 0.5 * uGravity * age * age;  // reduced gravity
      if (uClock == aBirthday) pos.y += 3.0;
      gl_Position = uPMatrix * uMVMatrix * vec4(pos, 1.0);
      gl_PointSize = 70.0 / gl_Position.z;
    }
  }
</script>
<script id="particle-fs" type="x-shader/x-fragment">
  precision mediump float;
  uniform sampler2D uSampler;
  varying vec2 vTexCoord;
  void main(void) {
    vec2 coord = (vTexCoord + 4.0 * gl_PointCoord) / 256.0;
    coord.t = 1.0 - coord.t;
    gl_FragColor = texture2D(uSampler, coord);
    if (gl_FragColor.a <= 0.5)
      discard;
  }
</script>

<!-- Primary geometry shader -->
<script id="main-vs" type="x-shader/x-vertex">
  attribute vec3 aPos;
  attribute vec2 aTexCoord;
  attribute vec4 aLighting;
  attribute vec3 aColor;
  
  uniform mat4 uMVMatrix;
  uniform mat4 uPMatrix;
  uniform float uFogDistance;
  uniform float uSunlight;

  const float lightScale = 7.8;
  const vec3 minLight = vec3(0.2, 0.2, 0.2);
  
  varying vec2 vTexCoord;
  varying vec3 vLighting;
  varying float vFog;
  
  void main(void) {
    gl_Position = uPMatrix * uMVMatrix * vec4(aPos, 1.0);
    vTexCoord = aTexCoord / 16.0;
    vTexCoord.t = 1.0 - vTexCoord.t;
    vLighting = minLight +
      aColor * max(aLighting.xyz,
                   uSunlight * vec3(aLighting.w, 
                                    aLighting.w, 
                                    aLighting.w)) / lightScale;
    float z = abs(gl_Position.z);  // Use length() if not enough fog at corners
    vFog = clamp(1.0 - exp(-z*z / (uFogDistance * uFogDistance)),
                 0.0, 1.0);
  }
</script>
<script id="main-fs" type="x-shader/x-fragment">
  precision mediump float;

  varying vec2 vTexCoord;
  varying vec3 vLighting;
  varying float vFog;
  
  uniform sampler2D uSampler;
  uniform float uSunlight;

  const vec4 kFogColor = vec4(0.5, 0.8, 0.98, 1.0);

  void main(void) {
    vec4 textureColor = texture2D(uSampler, vTexCoord);
    if (textureColor.a <= 0.5) {
      discard;
    } else {
      textureColor = vec4(textureColor.rgb * vLighting, textureColor.a);
      textureColor = vFog * uSunlight * kFogColor + (1.0 - vFog) * textureColor;
      gl_FragColor = textureColor;
    }
  }
</script>

<script id="twodee-vs" type="x-shader/x-vertex">
  attribute vec2 aPosition;
  uniform vec2 uResolution;
  void main() {
    vec2 reposition = (2.0 * aPosition / uResolution - 1.0) * vec2(1,-1);
    gl_Position = vec4(reposition, 0, 1);
  }
</script>
<script id="twodee-fs" type="x-shader/x-fragment">
  precision mediump float;
  uniform vec4 uColor;
  void main() {
    gl_FragColor = uColor;
  }
</script>

<!-- Blur shaders
     http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/ -->
<script id="blur-vs" type="x-shader/x-vertex">
  attribute vec2 aPos;
  varying vec2 vTexCoord;
  void main(void) {
    gl_Position = vec4(sign(aPos), 0.0, 1.0);
    vTexCoord = sign(aPos) * 0.5 + 0.5;
  }
</script>
<script id="blur-box-fs" type="x-shader/x-fragment">
  precision mediump float;
  uniform sampler2D uSrc;
  uniform vec2 uResolution;
  varying vec2 vTexCoord;
  void main() {
    gl_FragColor = 
      4.0/9.0 * texture2D(uSrc, vTexCoord + vec2(-0.5, -0.5) / uResolution) +
      2.0/9.0 * texture2D(uSrc, vTexCoord + vec2(-0.5, +0.5) / uResolution) +
      2.0/9.0 * texture2D(uSrc, vTexCoord + vec2(+0.5, -0.5) / uResolution) +
      1.0/9.0 * texture2D(uSrc, vTexCoord + vec2(+1.0, +1.0) / uResolution);
  }
</script>

<!-- Skybox shaders -->
<script id="skybox-vs" type="x-shader/x-vertex">
  attribute vec3 aPos;
  void main(void) {
    gl_Position = vec4(aPos, 1.0);
  }
</script>
<script id="panorama-fs" type="x-shader/x-fragment">
  precision mediump float;
  uniform vec2 uViewport;
  uniform mat4 uInvProj;
  uniform mat3 uInvViewRot;
  uniform samplerCube uSampler;
  const int kOversampling = 3;
  // The following is about how many screen pixels hit the same skybox texture 
  // pixel along one dimension at the FOV and res we use
  // (screen width / FOV / pixels at equator of skybox)
  const float kPanoPixelsPerScreenPixel = 854. / (110. / 360.) / (256. * 4.);
  void main(void) {
    vec4 color = vec4(0);
    for (int i = 0; i < kOversampling; ++i) {
      for (int j = 0; j < kOversampling; ++j) {
        vec2 pixel = gl_FragCoord.xy + kPanoPixelsPerScreenPixel * vec2(i, j);
        vec2 frag_coord = 2.0 * pixel / uViewport - 1.0;
        vec4 device_normal = vec4(frag_coord, 0.0, 1.0);
        vec3 eye_normal = normalize((uInvProj * device_normal).xyz);
        vec3 world_normal = normalize(uInvViewRot * eye_normal);
        color += textureCube(uSampler, world_normal);
      }
    }
    gl_FragColor = color / float(kOversampling * kOversampling);
  }
</script>
<script id="sky-fs" type="x-shader/x-fragment">
  precision mediump float;
  uniform vec2 uViewport;
  uniform mat4 uInvProj;
  uniform mat3 uInvViewRot;
  uniform float uTimeOfDay;
  const vec4 skyColor = vec4(0.5, 0.8, 0.98, 1.0);
  const float sunset = 0.2;
  void main(void) {
    vec2 frag_coord = 2.0 * gl_FragCoord.xy / uViewport - 1.0;
    vec4 device_normal = vec4(frag_coord, 0.0, 1.0);
    vec3 eye_normal = normalize((uInvProj * device_normal).xyz);
    vec3 world_normal = normalize(uInvViewRot*eye_normal);
    float sunlight = 0.5 - cos(uTimeOfDay) / 2.0;
    gl_FragColor = skyColor;
    float d = abs(dot(world_normal, vec3(0.0, 1.0, 0.0))) / sunset;
    if (d < 1.0) {
      float sunsetty = 1.0 - abs(sin(uTimeOfDay));
      vec4 redness = mix(vec4(1,0,0,1), skyColor, sunsetty);
      gl_FragColor = sunlight * mix(redness, skyColor, d);
    } else {
      gl_FragColor = sunlight * skyColor;
    }
  }
</script>

<style>
body {
  height: 100%;
  width: 100%;
  margin: auto;
  margin-top: 20px;
  background-color: black;
}

#cancan {
  position: relative;
  margin: auto auto;
}


#cancan, div.page {
  width: 854px;
  height: 480px;
}

#cancan:-webkit-full-screen,
:-webkit-full-screen #canvas,
:-webkit-full-screen #hud,
:-webkit-full-screen #options,
:-webkit-full-screen #stats {
  width: 100%;
  height: 100%;
}

div.page { 
  display: none;
  font-size: 18px;
  position: absolute;
  top: 0;
  left: 0;
  color: white;
  text-align: center;
}

* {
  font-family: Helvetica, sans-serif;
}

.msg {
  text-align: center;
  font-size: 32px;
  height: 40px;
  left: 0;
  margin-top: 15%;
  margin-bottom: 5%;
}

button {
  font-size: 18px;
  width: 220px;
  height: 40px;
  color: black;
  display: block;
  margin: 20px auto;
}

#failinit {
  position: absolute;
  left: 50%;
  top: 55%;
  width: 70%;
  margin-left: -35%;
  margin-top: -24px;
  height: 48px;
  font-size: 20px;
  color: red;
  text-align: center;
  display: none;
}

#newgame {
  margin-top: 30%;
}

.logo {
  position: absolute;
  left: 50%;
  top: 60px;
  width: 442px;
  height: 93px;
  margin-left: -221px;
  transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -webkit-transition: opacity 1s ease-in-out;
}

#throbber {
  position: absolute;
  left: 48%;
  width: 300px;
  left-margin: -150px;
  text-align: center;
  top: 100px;
  font-size: 30px;
  -webkit-transform: rotate(-18deg);
  -moz-transform: rotate(-18deg);
  color: yellow;
  text-shadow: 3px 3px 2px #222;
  animation:         pulse 0.27s ease-in-out 0s infinite alternate;
  -moz-animation:    pulse 0.27s ease-in-out 0s infinite alternate;
  -webkit-animation: pulse 0.27s ease-in-out 0s infinite alternate;
}

@keyframes pulse {
  0% {   transform: scale(1,1)       rotate(-19deg); }
  100% { transform: scale(1.07,1.07) rotate(-19deg); }
} 
@-webkit-keyframes pulse {
  0% {   -webkit-transform: scale(1,1)       rotate(-19deg); }
  100% { -webkit-transform: scale(1.07,1.07) rotate(-19deg); }
} 
@-moz-keyframes pulse {
  0% {   -moz-transform: scale(1,1)       rotate(-19deg); }
  100% { -moz-transform: scale(1.07,1.07) rotate(-19deg); }
} 

.toolbox {
  position: absolute;
  width: 48px;
  height: 48px;
  border: solid 2px rgb(128, 128, 128);
  background: rgba(128, 128, 128, 0.5);
}
#toolbox {
  left: 16px;
  bottom: 3.33%;
}

.tool {
  background: transparent;
}

#held {
  position: absolute;
  z-index: 2;
  pointer-events: none;
}

#terrain {
  display: none;
}

#chat {
  position: absolute;
  width 93%;
  height: 83%;
  top: 0px;
  left: 16px;
  color: white;
  overflow-y: hidden;
}

#clog {
  height: 400px;
}


#options div {
  margin-top: 1em;
  margin-left: 1em;
  text-align: left;
}

#stats {
  margin-top: 1em;
  margin-left: 1em;
  text-align: left;
}

a, a:visited {
  color: cyan;
  border: solid 1px cyan;
  text-decoration: none;
  text-transformation: uppercase;
  padding: 0 8px;
  background-color: rgba(0, 255, 255, .2);
  border-radius: 10px;
}

#byline {
  float: right;
  margin: 0 20px;
  font-size: 14px;
}

</style>
</head>


<body>
  <div id="cancan">
    <canvas id="canvas" width="854" height="480"></canvas>
    <div id="hud" class="page">
      <div id="chat"><div id="clog">&nbsp;</div></div>
    </div>
    <div id="stats" class="page"></div>
    <div id="options" class="page">
      <div style='font-size:24px'>Options</div>
      <div>
        <input id="anisotropic" type="checkbox">
        <label for="anisotropic">Anisotropic filtering</label>
      </div>
      <div>
        Chunk generation distance:<br>
        <input type="radio" name="rdist" value="0">Off<br>
        <input type="radio" name="rdist" value="2">Very Near Indeed<br>
        <input type="radio" name="rdist" value="16" checked>Near<br>
        <input type="radio" name="rdist" value="32">Medium<br>
        <input type="radio" name="rdist" value="64">Far</br>
        <input type="radio" name="rdist" value="64">Very Far Indeed</br>
      </div>
      <div><button id="resumegame2">Done</button></div>
    </div>
    <div id="title" class="page">
      <div class="logo"><img src="logo.png"></div>
      <div id="throbber"></div>

      <div id="failinit"></div>
      <button id="newgame">New Game</button>
      <button id="newhalfgame">New &half; Game</button>
      <button id="loadgame">Load Game</button>
      <div id="byline">by Grumdrig 
        <a href="https://bitbucket.org/grumdrig/halfblock">source code</a>
      </div>
    </div>
    <div id="pause" class="page">
      <div class="msg">Paused</div>
      <button id="resumegame">Return to Game</button>
      <button id="savegame">Save Game &amp; Quit</button>
      <button id="quitgame">Quit Game</button>
    </div>
    <div id="loading" class="page">
      <div class="msg">Loading...</div>
    </div>
    <div id="dead" class="page">
      <div class="msg">You died.</div>
      <button id="respawn">Respawn</button>
      <button id="quitgame2">Quit Game</button>
    </div>
    <div id="inventory" class="page">
      <canvas id="held" class="tool" width="48" height="48"></canvas>
    </div>
  </div>
  <img src="terrain.png" id="terrain">
</body>

</html>