forked from quantsini/JS-Minecraft-Map-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
406 lines (371 loc) · 12.1 KB
/
index.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
402
403
404
405
<!DOCTYPE html>
<html lang="en">
<head>
<title>derp</title>
<link rel="stylesheet" href="reset.css" />
<style type="text/css">
body {
font-family: Monospace;
}
#fileContainer {
min-height: 100px;
border: 3px dashed #ccc;
margin: 10px;
padding: 10px;
font-size: 2em;
text-align: center;
line-height: 100px;
}
#progress {
padding: 4px;
margin: 2px;
background-color #cccccc;
border: 1px solid black;
}
#container {
background-color: #8EE3FA;
}
</style>
<script type="text/javascript" src="js/jquery-1.6.1.js"></script>
<script type="text/javascript" src="js/Detector.js"></script>
<script type="text/javascript" src="js/Three.js"></script>
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="js/BlockType.js"></script>
<script type="text/javascript" src="js/BlockHandler.js"></script>
<script type="text/javascript" src="js/DataReader.js"></script>
<script type="text/javascript" src="js/util.js"></script>
<script type="text/javascript" src="js/deflate.js"></script>
<script type="text/javascript" src="js/inflate.js"></script>
<script type="text/javascript" src="js/RegionFile.js"></script>
<script type="text/javascript" src="js/nbt.js"></script>
<script type="text/javascript" src="js/World.js"></script>
<script type="text/javascript" src="js/Stats.js"></script>
<script type="text/javascript">
var SIZE_Y = 500;
var SIZE_X = window.innerWidth;
var camera, scene, renderer;
var world = null;
var stats = null;
var container;
$( function() {
if ( ! Detector.webgl ) {
Detector.addGetWebGLMessage();
}
container = $("#container");
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.append( stats.domElement );
init();
animate();
updateWorld();
function init() {
materials = []
scene = new THREE.Scene();
var grass_dirt = loadTexture( 'textures/minecraft/grass_dirt.png' ),
grass = loadTexture( 'textures/minecraft/grass.png' ),
dirt = loadTexture( 'textures/minecraft/dirt.png' ),
smoothstone = loadTexture( 'textures/minecraft/smoothstone.png' );
sand = loadTexture( 'textures/minecraft/sand.png' );
woodside = loadTexture( 'textures/minecraft/woodside.png' );
woodtopbottom = loadTexture( 'textures/minecraft/woodtopbottom.png' );
coal = loadTexture( 'textures/minecraft/coal.png' );
leaves = loadTexture( 'textures/minecraft/leaves.png' );
cactusSide = loadTexture( 'textures/minecraft/cactusside.png' );
cactusTop = loadTexture( 'textures/minecraft/cactustop.png' );
gravel = loadTexture( 'textures/minecraft/gravel.png' );
sandstoneTop = loadTexture( 'textures/minecraft/sandstoneTop.png' );
sandstoneSide = loadTexture( 'textures/minecraft/sandstoneSide.png' );
sandstoneBottom = loadTexture( 'textures/minecraft/sandstoneBottom.png' );
water = loadTexture( 'textures/minecraft/water.png' );
lava = loadTexture( 'textures/minecraft/lava.png' );
cobblestone = loadTexture('textures/minecraft/cobblestone.png' );
woodPlank = loadTexture('textures/minecraft/woodPlank.png' );
var textures = loadTexture('textures/minecraft/textures.png');
grassMaterial = [
grass_dirt, // right
grass_dirt, // left
grass, // top
dirt, // bottom
grass_dirt, // back
grass_dirt // front
];
lavaMaterial = [
lava, // right
lava, // left
lava, // top
lava, // bottom
lava, // back
lava // front
];
waterMaterial = [
water, // right
water, // left
water, // top
water, // bottom
water, // back
water // front
];
sandstoneMaterial = [
sandstoneSide, // right
sandstoneSide, // left
sandstoneTop, // top
sandstoneBottom, // bottom
sandstoneSide, // back
sandstoneSide // front
];
cactusMaterial = [
cactusSide, // right
cactusSide, // left
cactusTop, // top
cactusTop, // bottom
cactusSide, // back
cactusSide // front
];
gravelMaterial = [
gravel, // right
gravel, // left
gravel, // top
gravel, // bottom
gravel, // back
gravel // front
];
woodMaterial = [
woodside, // right
woodside, // left
woodtopbottom, // top
woodtopbottom, // bottom
woodside, // back
woodside // front
];
coalMaterial = [
coal, // right
coal, // left
coal, // top
coal, // bottom
coal, // back
coal // front
];
leafMaterial = [
leaves, // right
leaves, // left
leaves, // top
leaves, // bottom
leaves, // back
leaves // front
];
dirtMaterial = [
dirt, // right
dirt, // left
dirt, // top
dirt, // bottom
dirt, // back
dirt // front
];
smoothStoneMaterial = [
smoothstone, // right
smoothstone, // left
smoothstone, // top
smoothstone, // bottom
smoothstone, // back
smoothstone // front
];
sandMaterial = [
sand, // right
sand, // left
sand, // top
sand, // bottom
sand, // back
sand // front
];
cobblestoneMaterial = [
cobblestone, // right
cobblestone, // left
cobblestone, // top
cobblestone, // bottom
cobblestone, // back
cobblestone // front
];
woodPlankMaterial = [
woodPlank, // right
woodPlank, // left
woodPlank, // top
woodPlank, // bottom
woodPlank, // back
woodPlank // front
];
world = new World(scene);
smoothStoneBlockType = new BlockType(1, smoothStoneMaterial);
world.addBlockTexture(smoothStoneBlockType);
dirtBlockType = new BlockType(3, dirtMaterial);
world.addBlockTexture(dirtBlockType);
grassBlockType = new BlockType(2, grassMaterial);
world.addBlockTexture(grassBlockType);
sandBlockType = new BlockType(12, sandMaterial);
world.addBlockTexture(sandBlockType);
woodBlockType = new BlockType(17, woodMaterial);
world.addBlockTexture(woodBlockType);
coalBlockType = new BlockType(16, coalMaterial);
world.addBlockTexture(coalBlockType);
leafBlockType = new BlockType(18, leafMaterial);
world.addBlockTexture(leafBlockType);
cactusBlockType = new BlockType(81, cactusMaterial);
world.addBlockTexture(cactusBlockType);
gravelBlockType = new BlockType(13, gravelMaterial);
world.addBlockTexture(gravelBlockType);
sandstoneBlockType = new BlockType(24, sandstoneMaterial);
world.addBlockTexture(sandstoneBlockType);
waterBlockType = new BlockType(9, waterMaterial);
world.addBlockTexture(waterBlockType);
lavaBlockType = new BlockType(11, lavaMaterial);
world.addBlockTexture(lavaBlockType);
cobblestoneBlockType = new BlockType(4, cobblestoneMaterial);
world.addBlockTexture(cobblestoneBlockType);
woodPlankBlockType = new BlockType(5, woodPlankMaterial);
world.addBlockTexture(woodPlankBlockType);
console.log("setting up renderer");
renderer = new THREE.WebGLRenderer();
renderer.setSize( SIZE_X, SIZE_Y );
$(renderer.domElement).appendTo(container);
console.log("renderer setup!");
camera = new THREE.QuakeCamera({
fov: 60,
aspect: SIZE_X / SIZE_Y,
near: 1,
far: 20000,
movementSpeed: 500,
lookSpeed: 0.075,
noFly: false,
lookVertical: true,
autoForward: false,
} );
camera.position.x = (0 * 16) * BLOCK_SIZE;
camera.position.y = 64 * BLOCK_SIZE;
camera.position.z = (0 * 16) * BLOCK_SIZE;
}
function updateWorld() {
requestAnimationFrame(updateWorld);
_updateWorld();
}
var haltUpdateWorld = false;
function _updateWorld() {
var x, y, z, chunkX, chunkZ, regionX, regionZ, phi, theta;
x = Math.floor(camera.position.x/BLOCK_SIZE);
y = Math.floor(camera.position.y/BLOCK_SIZE);
z = Math.floor(camera.position.z/BLOCK_SIZE);
phi = Math.floor(camera.phi * 360/(Math.PI));
theta = Math.floor(camera.theta * 360/(Math.PI)) % 360;
chunkX = Math.floor(x/16);
chunkZ = Math.floor(z/16);
regionX = Math.floor(chunkX/32);
regionZ = Math.floor(chunkZ/32);
var coords = "(" + x + ", " + y + ", " + z + ")";
var chunkCoords = "(" + chunkX + ", " + chunkZ + ")";
var regionCoords = "(" + regionX + ", " + regionZ + ")";
var rotation = "(" + phi + "," + theta + ")";
world.updateWorld({
x: x,
y: y,
z: z
});
printout("Player: " + coords + " " + rotation + " Chunk: " + chunkCoords + " Region: " + regionCoords);
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
renderer.render( scene, camera );
}
function loadTexture( path ) {
var image = new Image();
image.onload = function () {
texture.needsUpdate = true;
};
image.src = path;
var texture = new THREE.Texture( image, new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.NearestFilter, THREE.LinearMipMapLinearFilter );
return new THREE.MeshLambertMaterial({
map: texture
} );
}
function printout(s) {
$("#progress").text(s);
}
//this is fucking broken, if you remove this, don't expect drop to work
$("#fileContainer").bind('dragenter', function(event) {
$(this).css("background","black");
});
//same here, don't remove this or don't expect drop to work
$("#fileContainer").bind('dragover', function(event) {
event.stopPropagation();
event.preventDefault();
});
$("#fileContainer").bind('dragleave', function(event) {
$(this).css("background","white");
});
$("#fileContainer").bind('drop', function(event) {
event.stopPropagation();
event.preventDefault();
$("#fileContainer").css("color","white").text("Loading Region File(s)");
var regionFiles = event.originalEvent.dataTransfer.files;
if (regionFiles.length == 0) {
alert("error loading file");
return;
}
var k = 0;
$.each(regionFiles, function(index,region) {
var re = new RegExp("\\br.(-{0,1}\\d+).(-{0,1}\\d+).mcr+\\b")
var match = re.exec(region.fileName);
var regionLoc = {
x: parseInt(match[1]),
z: parseInt(match[2])
};
var fr = new FileReader();
resetDragArea = function() {
setTimeout( function() {
$("#fileContainer").css("background-color","white").css("color","black").text("Drag region file(s) here");
}, 5000);
};
fr.onerror = function(event) {
console.log(event);
$("#fileContainer").css("background","red").css("color","black").text("Error loading region file!");
resetDragArea();
}
fr.onload= function(event) {
/* TODO: use web workers here */
var rLoc = regionLoc;
var data = event.target.result;
var regionFile = new RegionFile(data, rLoc);
world.addRegion(regionFile);
k++;
if (k == regionFiles.length) {
ready = true;
$("#fileContainer").css("background","green").css("color","black").text("Region File Loaded(s)");
resetDragArea();
}
};
fr.readAsBinaryString(region);
fr = null;
});
});
});
</script>
</head>
<body>
<div id="fileContainer">
Drag region file(s) here.
</div>
<div id="progress">
...
</div>
<div id="container">
</div>
<div>
Example Map: Download
<a href="map/region/r.0.0.mcr">this file</a> then drag it in the space above.
</div>
<div id="debug">
</div>
</body>
</html>