public
Last active

  • Download Gist
gistfile1.txt
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
 
<!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">
var ctx;
var pixels;
 
var w = 212;
var h = 120;
 
var map = new Array(64 * 64 * 64);
var texmap = new Array(16 * 16 * 3 * 16);
 
function init() {
ctx = document.getElementById('game').getContext('2d');
 
for ( var x = 0; x < 64; x++) {
for ( var y = 0; y < 64; y++) {
for ( var z = 0; z < 64; z++) {
var i = z << 12 | y << 6 | x;
var yd = (y - 32.5) * 0.4;
var zd = (z - 32.5) * 0.4;
map[i] = (Math.random() * 16) | 0;
if (Math.random() > Math.sqrt(Math.sqrt(yd * yd + zd * zd)) - 0.8)
map[i] = 0;
}
}
}
 
pixels = ctx.createImageData(w, h);
 
for ( var i = 0; i < w * h; i++) {
pixels.data[i * 4 + 3] = 255;
}
 
setInterval(clock, 1000 / 100);
};
 
function clock() {
renderMinecraft();
ctx.putImageData(pixels, 0, 0);
};
 
var f = 0;
function renderMinecraft() {
var ox = 32.5 + Date.now() % 10000 / 10000 * 64;
var oy = 32.5;
var oz = 32.5;
 
f++;
for ( var x = 0; x < w; x++) {
var worldxd = (x - w / 2) / h;
for ( var y = 0; y < h; y++) {
var worldyd = (y - h / 2) / h;
var worldzd = 1;
 
var ___zd = worldzd;
var rotyd = worldyd;
 
var rotxd = ___zd;
var rotzd = worldxd;
 
var col = 0;
var br = 255;
var ddist = 0;
 
var closest = 32;
for ( var d = 0; d < 3; d++) {
var dimLength;
 
if (d == 0) dimLength = rotxd;
if (d == 1) dimLength = rotyd;
if (d == 2) dimLength = rotzd;
 
var ll = 1 / Math.abs(dimLength);
var xd = (rotxd) * ll;
var yd = (rotyd) * ll;
var zd = (rotzd) * ll;
 
var initial;
if (d == 0) initial = ox - Math.floor(ox);
if (d == 1) initial = oy - Math.floor(oy);
if (d == 2) initial = oz - Math.floor(oz);
if (dimLength > 0) initial = 1 - initial;
 
var dist = ll * initial;
 
var xp = ox + xd * initial;
var yp = oy + yd * initial;
var zp = oz + zd * initial;
 
if (dimLength < 0) {
if (d == 0) xp--;
if (d == 1) yp--;
if (d == 2) zp--;
}
 
while (dist < closest) {
var tex = map[(zp & 63) << 12 | (yp & 63) << 6 | (xp & 63)];
 
if (tex > 0) {
col = 255+ (255<<8) + (255<<16);
ddist = 255 - ((dist / 32 * 255) | 0);
br = 255 * (255 - ((d + 2) % 3) * 50) / 255;
closest = dist;
}
xp += xd;
yp += yd;
zp += zd;
dist += ll;
}
}
 
var r = ((col >> 16) & 0xff) * br * ddist / (255 * 255);
var g = ((col >> 8) & 0xff) * br * ddist / (255 * 255);
var b = ((col) & 0xff) * br * ddist / (255 * 255);
 
pixels.data[(x + y * w) * 4 + 0] = r;
pixels.data[(x + y * w) * 4 + 1] = g;
pixels.data[(x + y * w) * 4 + 2] = b;
}
}
}
 
init();
</script>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.