mapserver/static/js/test.js

193 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-05-05 18:33:09 +03:00
var camera, scene, renderer;
2020-02-14 17:52:58 +03:00
var geometry = new THREE.BufferGeometry().fromGeometry(
new THREE.BoxGeometry(1,1,1)
);
2019-05-05 18:33:09 +03:00
init();
animate();
2019-05-05 18:44:30 +03:00
function getNodePos(x,y,z){ return x + (y * 16) + (z * 256); }
2019-05-05 18:33:09 +03:00
2020-02-14 11:30:07 +03:00
var colormapping, controls;
2020-02-14 10:28:36 +03:00
var materialCache = {};
function getMaterial(nodeName){
var material = materialCache[nodeName];
if (!material) {
var colorObj = colormapping[nodeName];
if (!colorObj){
return;
}
var color = new THREE.Color( colorObj.r/256, colorObj.g/256, colorObj.b/256 );
material = new THREE.MeshBasicMaterial( { color: color } );
2020-02-14 22:20:49 +03:00
if (nodeName == "default:water_source"){
material.transparent = true;
material.opacity = 0.8;
material.clipIntersection = true;
}
2020-02-14 10:28:36 +03:00
materialCache[nodeName] = material;
}
return material;
}
2020-02-14 11:30:07 +03:00
function isNodeHidden(mapblock,x,y,z){
2020-02-14 21:53:32 +03:00
if (x<=1 || x>=14 || y<=1 || y>=14 || z<=1 || z>=14){
2020-02-14 11:30:07 +03:00
// not sure, may be visible
return false;
}
function isTransparent(contentId){
var nodeName = mapblock.blockmapping[contentId];
2020-02-14 22:20:49 +03:00
return nodeName == "air" || nodeName == "default:water_source";
2020-02-14 11:30:07 +03:00
}
2020-02-14 16:08:03 +03:00
if (isTransparent(mapblock.contentid[getNodePos(x-1,y,z)]))
2020-02-14 11:30:07 +03:00
return false;
2020-02-14 16:08:03 +03:00
if (isTransparent(mapblock.contentid[getNodePos(x,y-1,z)]))
2020-02-14 11:30:07 +03:00
return false;
2020-02-14 16:08:03 +03:00
if (isTransparent(mapblock.contentid[getNodePos(x,y,z-1)]))
2020-02-14 11:30:07 +03:00
return false;
2020-02-14 16:08:03 +03:00
if (isTransparent(mapblock.contentid[getNodePos(x+1,y,z)]))
2020-02-14 11:30:07 +03:00
return false;
2020-02-14 16:08:03 +03:00
if (isTransparent(mapblock.contentid[getNodePos(x,y+1,z)]))
2020-02-14 11:30:07 +03:00
return false;
2020-02-14 16:08:03 +03:00
if (isTransparent(mapblock.contentid[getNodePos(x,y,z+1)]))
2020-02-14 11:30:07 +03:00
return false;
2020-02-14 11:45:45 +03:00
return true;
2020-02-14 11:30:07 +03:00
}
2020-02-14 10:28:36 +03:00
function drawMapblock(posx,posy,posz){
return m.request("api/viewblock/"+posx+"/"+posy+"/"+posz)
2019-05-05 18:33:09 +03:00
.then(function(mapblock){
2019-05-05 18:44:30 +03:00
if (!mapblock)
return;
2019-05-05 18:33:09 +03:00
2020-02-14 17:52:58 +03:00
if (mapblock.blockmapping.length == 1 && mapblock.blockmapping[0] == "air"){
return;
}
var nodenameGeometriesMap = {}; // nodeName => [geo, geo, ...]
2020-02-14 21:53:32 +03:00
for (var x=0; x<16; x++){
for (var y=0; y<16; y++){
for (var z=0; z<16; z++){
2020-02-14 11:30:07 +03:00
if (isNodeHidden(mapblock, x,y,z)){
//skip hidden node
continue;
}
2020-02-14 21:53:32 +03:00
var i = getNodePos(x,y,z);
2019-12-13 13:03:34 +03:00
var contentId = mapblock.contentid[i];
2020-02-14 11:30:07 +03:00
var nodeName = mapblock.blockmapping[contentId];
2019-05-05 18:33:09 +03:00
2020-02-14 17:52:58 +03:00
var geo = geometry.clone();
var matrix = new THREE.Matrix4()
.makeTranslation(
x + (posx*16),
y + (posy*16),
z + (posz*16)
);
geo.applyMatrix4(matrix);
var list = nodenameGeometriesMap[nodeName];
if (!list){
list = [];
nodenameGeometriesMap[nodeName] = list;
2020-02-14 10:28:36 +03:00
}
2020-02-14 17:52:58 +03:00
list.push(geo);
2019-05-05 18:33:09 +03:00
}
}
}
2020-02-14 17:52:58 +03:00
Object.keys(nodenameGeometriesMap).forEach(function(nodeName){
var material = getMaterial(nodeName);
if (material){
var list = THREE.BufferGeometryUtils.mergeBufferGeometries(nodenameGeometriesMap[nodeName]);
var mesh = new THREE.Mesh(list, material);
scene.add( mesh );
}
});
2019-05-05 18:44:30 +03:00
});
}
function init() {
2020-02-14 16:08:03 +03:00
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 2, 2000 );
camera.position.z = -50;
2019-05-05 18:44:30 +03:00
camera.position.y = 10;
scene = new THREE.Scene();
2020-02-14 21:53:32 +03:00
var min = -7, max = 7;
2020-02-14 15:08:12 +03:00
var x = min, y = -1, z = min;
2019-12-13 13:03:34 +03:00
2020-02-14 15:08:12 +03:00
function increment(){
x++;
2020-02-14 17:52:58 +03:00
if (x > max){
2020-02-14 15:08:12 +03:00
z++;
2020-02-14 17:52:58 +03:00
x = min;
2020-02-14 15:08:12 +03:00
}
2020-02-14 17:52:58 +03:00
if (z > max){
2020-02-14 15:08:12 +03:00
y++;
2020-02-14 17:52:58 +03:00
z = min;
2019-05-05 18:44:30 +03:00
}
2020-02-14 15:08:12 +03:00
}
2019-05-05 18:44:30 +03:00
2020-02-14 15:08:12 +03:00
var drawLoop = function(){
if (y >= 3){
return;
}
2020-02-14 16:08:03 +03:00
drawMapblock(x,y,z)
.then(function(){
render();
increment();
2020-02-14 21:53:32 +03:00
setTimeout(drawLoop, 50);
2020-02-14 16:08:03 +03:00
});
2020-02-14 15:08:12 +03:00
};
m.request("api/colormapping")
.then(function(_colormapping){
colormapping = _colormapping;
drawLoop();
2019-05-05 18:33:09 +03:00
});
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
controls = new THREE.TrackballControls( camera, renderer.domElement );
2020-02-14 21:53:32 +03:00
controls.rotateSpeed = 2.0;
2019-05-05 18:33:09 +03:00
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.keys = [ 65, 83, 68 ];
controls.addEventListener( 'change', render );
render();
}
function render(){
renderer.render( scene, camera );
}
function animate() {
requestAnimationFrame( animate );
controls.update();
}