Notch - Minecraft tunneling render
HN - http://news.ycombinator.com/item?id=4862830
--
I've renamed variables to make sense.
HTML
<!DOCTYPE html>
<html>
<head><title>ChamberedTest</title></head>
<script type="text/javascript" src="js/chambered.js"></script>
<style type="text/css">
canvas, img {
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: optimize-contrast;
-ms-interpolation-mode: nearest-neighbor;
width:848px;
height:480px;
}
</style>
<body>
<canvas id="game" width="424" height="240"></canvas>
<script type="text/javascript">init();</script>
</body>
</html>
JavaScript
var ctx;
var pixels;
var w = 212 * 2;
var h = 120 * 2;
var map = new Array(64 * 64 * 64);
var texmap = new Array(16 * 16 * 3 * 16);
var textures = {
'grass-top': 1,
'stone': 4,
'brick': 5,
'wood': 7,
'leafy': 8,
'water': 9
};
function init() {
// There are 16 different textures.
for ( var texture = 1; texture < 16; texture++) {
// Pick a random brightness
var br = 255;// - ((Math.random() * 96) | 0); // (The "|0" ensures integer)
// The map for the texture - 3 16x16 squares.
// Square 1: y = [0,15] - Top of cube
// Square 2: y = [16,31] - Sides of cube (only 2 are ever visible)
// Square 3: y = [32,47] - Bottom of cube
// 'y' basically denotes the row of the map.
for ( var y = 0; y < 16 * 3; y++) {
// Hence, 'x' denotes the column per row.
for ( var x = 0; x < 16; x++) {
var color = 0x966C4A; //dirt brown
if (texture == textures['stone'])
color = 0x7F7F7F; // stone gray
if (texture != textures['stone'] || ((Math.random() * 3) | 0) == 0) {
br = 255 - ((Math.random() * 96) | 0);
}
// If we are in the top (16 + 5 or 6) rows of the grass topped block
if ((texture == textures['grass-top'] && y < (((x * x * 3 + x * 81) >> 2) & 3) + 18)) {
color = 0x6AAA40; // grass green
} else if ((texture == textures['grass-top'] && y < (((x * x * 3 + x * 81) >> 2) & 3) + 19)) {
br = br * 2 / 3; // reduce brightness by a third
}
if (texture == textures['wood']) {
color = 0x675231; // woody brown
// If we are in square 1 or 3
if (x > 0 && x < 15
&& ((y > 0 && y < 15) || (y > 32 && y < 47)))...