2016-07-07 14:38:02 +00:00
|
|
|
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
|
2016-07-07 17:11:49 +00:00
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
var camera, controls, scene, renderer;
|
|
|
|
var clock = new THREE.Clock();
|
2016-07-22 14:23:16 +00:00
|
|
|
var raycaster = new THREE.Raycaster();
|
|
|
|
var mouse = new THREE.Vector2(), INTERSECTED;
|
2016-07-07 14:38:02 +00:00
|
|
|
var WIDTH = window.innerWidth , HEIGHT = window.innerHeight
|
2016-07-07 17:11:49 +00:00
|
|
|
|
|
|
|
window.onload = function() {
|
2016-07-14 17:21:22 +00:00
|
|
|
loadData(function() {
|
2016-07-07 14:38:02 +00:00
|
|
|
init();
|
|
|
|
animate();
|
2016-07-22 14:23:16 +00:00
|
|
|
populateUserFields();
|
2016-07-07 17:11:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-07 17:38:01 +00:00
|
|
|
|
2016-07-07 17:11:49 +00:00
|
|
|
function loadData(_callback) {
|
2016-07-14 17:21:22 +00:00
|
|
|
// Load Data (hopefully) before the rest of the place loads.
|
2016-07-07 17:11:49 +00:00
|
|
|
var xmlhttp = new XMLHttpRequest();
|
2016-07-11 19:55:02 +00:00
|
|
|
var url = "js/atsdata.json";
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-07 17:11:49 +00:00
|
|
|
xmlhttp.onreadystatechange = function() {
|
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
2016-07-14 17:21:22 +00:00
|
|
|
jsonEmpire = JSON.parse(xmlhttp.responseText)['ATS_Navcomp_DB']['empires'];
|
2016-07-07 17:11:49 +00:00
|
|
|
_callback();
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-07 17:11:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
xmlhttp.open("GET", url, true);
|
|
|
|
xmlhttp.send();
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-07 17:11:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 17:21:22 +00:00
|
|
|
function reset_view() {
|
2016-07-07 17:38:01 +00:00
|
|
|
camera.position.set(-9300,50,550);
|
2016-07-14 17:21:22 +00:00
|
|
|
controls.target.x = scene.getObjectByName("Federation").position.x;
|
|
|
|
controls.target.y = scene.getObjectByName("Federation").position.y;
|
|
|
|
controls.target.z = scene.getObjectByName("Federation").position.z;
|
|
|
|
camera.updateProjectionMatrix();
|
|
|
|
render();
|
2016-07-07 17:38:01 +00:00
|
|
|
}
|
2016-07-07 14:38:02 +00:00
|
|
|
|
|
|
|
function init() {
|
2016-07-14 17:21:22 +00:00
|
|
|
scene = new THREE.Scene();
|
2016-07-07 14:38:02 +00:00
|
|
|
renderer = new THREE.WebGLRenderer();
|
|
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
document.body.appendChild( renderer.domElement );
|
|
|
|
container = document.createElement( 'div' );
|
|
|
|
document.body.appendChild( container );
|
|
|
|
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1e7);
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
controls = new THREE.OrbitControls( camera, renderer.domElement );
|
|
|
|
controls.enableDamping = true;
|
|
|
|
controls.dampingFactor = 0.25;
|
|
|
|
controls.enableZoom = true;
|
|
|
|
controls.addEventListener( 'change', render );
|
2016-07-22 14:23:16 +00:00
|
|
|
document.addEventListener( 'mousedown', onCanvasClick, false );
|
2016-07-14 17:21:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
var Text2D = THREE_Text.Text2D;
|
|
|
|
var SpriteText2D = THREE_Text.SpriteText2D;
|
2016-07-14 17:21:22 +00:00
|
|
|
var textAlign = THREE_Text.textAlign
|
|
|
|
var b_geometry, b_material, b_mesh, p_geometry, p_material, p_mesh, s_geometry, s_material, s_mesh, l_text;
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
|
|
|
|
for (var key in jsonEmpire) {
|
2016-07-14 17:21:22 +00:00
|
|
|
area=jsonEmpire[key];
|
|
|
|
|
2016-07-11 19:55:02 +00:00
|
|
|
for (var key2 in area['borders']) {
|
2016-07-14 17:21:22 +00:00
|
|
|
var border = area['borders'][key2];
|
|
|
|
|
|
|
|
b_geometry = new THREE.SphereGeometry( border.radius, 10, 10 );
|
|
|
|
b_material = new THREE.MeshBasicMaterial( { color: area.color, wireframe: true} );
|
|
|
|
b_mesh = new THREE.Mesh( b_geometry, b_material );b_mesh
|
|
|
|
b_mesh.position.x = border.x;
|
|
|
|
b_mesh.position.y = border.y;
|
|
|
|
b_mesh.position.z = border.z;
|
2016-07-22 14:23:16 +00:00
|
|
|
b_mesh.name = escapeHTML(border.name);
|
2016-07-14 17:21:22 +00:00
|
|
|
scene.add( b_mesh );
|
|
|
|
if (border.radius > 10) {
|
|
|
|
l_text = new Text2D(border.name, { align: textAlign.center, font: '25px Arial', fillStyle: '#777' , antialias: false });
|
|
|
|
l_text.material.alphaTest = 0.5;
|
|
|
|
l_text.position.set(border.x,border.y,border.z);
|
|
|
|
l_text.scale.set(0.75,0.75,0.75);
|
|
|
|
scene.add(l_text);
|
|
|
|
}
|
2016-07-13 15:49:18 +00:00
|
|
|
}
|
2016-07-14 17:21:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
// Planet Generation
|
|
|
|
for (var key in area["planets"]) {
|
|
|
|
var planet = area.planets[key];
|
2016-07-14 17:21:22 +00:00
|
|
|
p_geometry= new THREE.SphereGeometry( 1, 10, 10 );
|
|
|
|
p_material = new THREE.MeshBasicMaterial( { color: area.color, wireframe: false} );
|
|
|
|
p_mesh = new THREE.Mesh( p_geometry, p_material );
|
|
|
|
p_mesh.position.x=planet.x;
|
|
|
|
p_mesh.position.y=planet.y;
|
|
|
|
p_mesh.position.z=planet.z;
|
2016-07-22 14:23:16 +00:00
|
|
|
p_mesh.name = escapeHTML(planet.name);
|
2016-07-14 17:21:22 +00:00
|
|
|
scene.add( p_mesh );
|
2016-07-22 14:23:16 +00:00
|
|
|
l_text = new Text2D(escapeHTML(planet.name), { align: textAlign.right, font: '12px Arial', fillStyle: '#FFF' , antialias: false });
|
2016-07-14 17:21:22 +00:00
|
|
|
l_text.material.alphaTest = 0.0;
|
|
|
|
l_text.position.set(planet.x,planet.y,planet.z);
|
|
|
|
l_text.scale.set(0.25,0.25,0.25);
|
|
|
|
scene.add(l_text);
|
|
|
|
}
|
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
// Base Generation
|
2016-07-11 19:55:02 +00:00
|
|
|
for (var key in area["stations"]) {
|
2016-07-14 17:21:22 +00:00
|
|
|
var base = area.stations[key];
|
|
|
|
s_geometry = new THREE.CylinderGeometry( 0.1, 0.5*3, 0.5*3, 4 );
|
|
|
|
s_material = new THREE.MeshBasicMaterial( { color: area.color, wireframe: false} );
|
|
|
|
s_mesh = new THREE.Mesh( s_geometry, s_material );
|
|
|
|
s_mesh.position.x=base.x;
|
|
|
|
s_mesh.position.y=base.y;
|
|
|
|
s_mesh.position.z=base.z;
|
2016-07-22 14:23:16 +00:00
|
|
|
s_mesh.name = escapeHTML(base.name);
|
2016-07-14 17:21:22 +00:00
|
|
|
scene.add( s_mesh );
|
2016-07-22 14:23:16 +00:00
|
|
|
l_text = new Text2D(escapeHTML(base.name), { align: textAlign.left, font: '12px Arial', fillStyle: '#ABABAB' , antialias: false });
|
2016-07-14 17:21:22 +00:00
|
|
|
l_text.material.alphaTest = 0.0;
|
|
|
|
l_text.position.set(base.x,base.y+3,base.z);
|
|
|
|
l_text.scale.set(0.20,0.20,0.20);
|
|
|
|
scene.add(l_text);
|
2016-07-07 14:38:02 +00:00
|
|
|
}
|
2016-07-14 17:21:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
// Set view and camera to point to initial location
|
|
|
|
reset_view();
|
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 17:11:49 +00:00
|
|
|
window.onresize = function() {
|
2016-07-07 14:38:02 +00:00
|
|
|
|
|
|
|
camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
|
camera.updateProjectionMatrix();
|
|
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
|
|
|
|
|
|
|
|
render();
|
|
|
|
|
|
|
|
}
|
2016-07-22 14:23:16 +00:00
|
|
|
function onCanvasClick( event ) {
|
|
|
|
|
|
|
|
//event.preventDefault();
|
|
|
|
|
|
|
|
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
|
|
|
|
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
|
|
|
|
|
|
|
|
raycaster.setFromCamera( mouse, camera );
|
|
|
|
var intersects = raycaster.intersectObjects( scene.children );
|
|
|
|
|
|
|
|
if ( intersects.length > 0 ) {
|
|
|
|
if ( INTERSECTED != intersects[ 0 ].object ) {
|
|
|
|
|
|
|
|
INTERSECTED = intersects[ 0 ].object;
|
|
|
|
// console.log( INTERSECTED.name );
|
2016-07-22 15:49:31 +00:00
|
|
|
if (lastInputBox) {
|
|
|
|
document.getElementById(lastInputBox).value = INTERSECTED.name;
|
|
|
|
}
|
2016-07-22 14:23:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
INTERSECTED = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:21:22 +00:00
|
|
|
function animate() {
|
|
|
|
var delta = clock.getDelta();
|
|
|
|
requestAnimationFrame( animate );
|
|
|
|
scene.updateMatrixWorld()
|
|
|
|
controls.update(delta);
|
|
|
|
render();
|
2016-07-22 14:23:16 +00:00
|
|
|
}
|
2016-07-07 14:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
function render () {
|
2016-07-14 17:21:22 +00:00
|
|
|
//requestAnimationFrame( render );
|
|
|
|
renderer.render( scene, camera );
|
2016-07-22 14:23:16 +00:00
|
|
|
// find intersections
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-12 19:52:58 +00:00
|
|
|
function listobjects(type) {
|
|
|
|
var objects = {};
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-12 19:52:58 +00:00
|
|
|
for (var key in jsonEmpire) {
|
|
|
|
area=jsonEmpire[key];
|
|
|
|
for (var key2 in area[type]) {
|
|
|
|
object = area[type][key2];
|
|
|
|
objectname = object.name;
|
|
|
|
objects[object.name] = object;
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-12 19:52:58 +00:00
|
|
|
}
|
2016-07-14 17:21:22 +00:00
|
|
|
}
|
2016-07-12 19:52:58 +00:00
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
function zoomfocus(name) {
|
2016-07-14 17:21:22 +00:00
|
|
|
|
2016-07-12 19:52:58 +00:00
|
|
|
var types = ['planets','stations'];
|
2016-07-14 17:21:22 +00:00
|
|
|
for (var type in types) {
|
|
|
|
var objects = listobjects(types[type]);
|
|
|
|
for ( var key in objects ) {
|
2016-07-22 14:23:16 +00:00
|
|
|
if (key == name) {
|
2016-07-14 17:21:22 +00:00
|
|
|
var object = objects[key];
|
|
|
|
controls.target.x = object.x;
|
|
|
|
controls.target.y = object.y;
|
|
|
|
controls.target.z = object.z;
|
|
|
|
var focus = new THREE.Vector3( object.x, object.y, object.z );
|
|
|
|
var vantage = new THREE.Vector3( 5, 60 , 150 );
|
|
|
|
focus.add(vantage);
|
|
|
|
camera.position.set(focus.x,focus.y,focus.z);
|
|
|
|
camera.updateProjectionMatrix();
|
|
|
|
render();
|
|
|
|
|
2016-07-07 14:38:02 +00:00
|
|
|
|
2016-07-12 19:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-07-14 17:21:22 +00:00
|
|
|
|
|
|
|
}
|
2016-07-12 19:52:58 +00:00
|
|
|
|
|
|
|
}
|
2016-07-14 12:27:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
function drawline(name,origin,dest) {
|
|
|
|
var geometry = new THREE.Geometry();
|
2016-07-14 17:21:22 +00:00
|
|
|
var material = new THREE.LineBasicMaterial( { color: '#FFF', });
|
|
|
|
geometry.vertices.push(origin, direction);
|
|
|
|
var line = new THREE.Line( geometry, material );
|
2016-07-14 12:27:46 +00:00
|
|
|
ray.name = "test";
|
|
|
|
scene.add(ray);
|
2016-07-14 17:21:22 +00:00
|
|
|
animate();
|
2016-07-14 12:27:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeEntity(object) {
|
|
|
|
var selectedObject = scene.getObjectByName(object);
|
|
|
|
scene.remove( selectedObject );
|
|
|
|
animate();
|
|
|
|
}
|
2016-07-22 14:23:16 +00:00
|
|
|
|
|
|
|
// Calculates SU/s with given warp factor
|
|
|
|
function calcSUpS(warpfactor) {
|
|
|
|
// 14.0*29.979246*1298.737508 = 257494817.55 SU/s
|
|
|
|
// Velocity = WF^3.333333*lightspeed*cochranes
|
|
|
|
// 3087467836.3256578445 = 1 Parsec
|
|
|
|
var cochranes = 1298.737508; // Average cochranes
|
|
|
|
var lightspeed = 29.979246; // Lightspeed constant
|
|
|
|
var exponent = 3.333333;
|
|
|
|
|
|
|
|
var sus = Math.pow(warpfactor,exponent) * lightspeed * cochranes ;
|
|
|
|
return sus;
|
|
|
|
}
|
|
|
|
|
|
|
|
function su2pc ( su ) {
|
|
|
|
return su / 3087467836.3256578445;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates ETA for given distance and velocity.
|
|
|
|
// Velocity should be supplied as an array of speed and unit
|
|
|
|
function calcETA(velocity,distance) {
|
|
|
|
var speed = velocity.speed;
|
|
|
|
var unit = velocity.unit;
|
|
|
|
var seconds;
|
|
|
|
switch (unit) {
|
|
|
|
case 'SU/s':
|
|
|
|
seconds = new Decimal( distance / su2pc(speed) );
|
|
|
|
break;
|
|
|
|
case 'PC/s':
|
|
|
|
seconds = new Decimal( distance / speed );
|
|
|
|
break;
|
|
|
|
case 'WF':
|
|
|
|
seconds = distance / su2pc(calcSUpS(speed));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw "Invalid unit of speed."
|
|
|
|
}
|
|
|
|
|
|
|
|
return seconds;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the distance between two named points ( Stations or Bases )
|
|
|
|
function calcDist(pointa, pointb) {
|
|
|
|
var obj_A = scene.getObjectByName(pointa);
|
|
|
|
var obj_B = scene.getObjectByName(pointb);
|
|
|
|
|
|
|
|
var distance = obj_A.position.distanceTo(obj_B.position);
|
|
|
|
return distance;
|
|
|
|
}
|