Improve performance and remove object arrays for mesh/geometry/materials

This commit is contained in:
Rob L 2016-07-14 13:21:22 -04:00
parent 8af99436ae
commit 2de0aa215c
2 changed files with 216 additions and 237 deletions

View File

@ -1,242 +1,222 @@
if ( ! Detector.webgl ) Detector.addGetWebGLMessage(); if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var camera, controls, scene, renderer; var camera, controls, scene, renderer;
var canvas_t=[];
var context_t=[];
var clock = new THREE.Clock(); var clock = new THREE.Clock();
var WIDTH = window.innerWidth , HEIGHT = window.innerHeight var WIDTH = window.innerWidth , HEIGHT = window.innerHeight
window.onload = function() { window.onload = function() {
loadData(function() { loadData(function() {
init(); init();
animate(); animate();
render();
}); });
} }
function loadData(_callback) { function loadData(_callback) {
// Load Data (hopefully) before the rest of the place loads. // Load Data (hopefully) before the rest of the place loads.
var xmlhttp = new XMLHttpRequest(); var xmlhttp = new XMLHttpRequest();
var url = "js/atsdata.json"; var url = "js/atsdata.json";
xmlhttp.onreadystatechange = function() { xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
jsonEmpire = JSON.parse(xmlhttp.responseText); jsonEmpire = JSON.parse(xmlhttp.responseText)['ATS_Navcomp_DB']['empires'];
_callback(); _callback();
} }
}; };
xmlhttp.open("GET", url, true); xmlhttp.open("GET", url, true);
xmlhttp.send(); xmlhttp.send();
} }
function reset_view() { function reset_view() {
camera.position.set(-9300,50,550); camera.position.set(-9300,50,550);
controls.target.x = jsonEmpire[1]['borders'][0].x; controls.target.x = scene.getObjectByName("Federation").position.x;
controls.target.y = jsonEmpire[1]['borders'][0].y; controls.target.y = scene.getObjectByName("Federation").position.y;
controls.target.z = jsonEmpire[1]['borders'][0].z; controls.target.z = scene.getObjectByName("Federation").position.z;
camera.updateProjectionMatrix();
render();
} }
function init() { function init() {
scene = new THREE.Scene(); scene = new THREE.Scene();
//scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
renderer = new THREE.WebGLRenderer(); renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight ); renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement ); document.body.appendChild( renderer.domElement );
container = document.createElement( 'div' ); container = document.createElement( 'div' );
document.body.appendChild( container ); document.body.appendChild( container );
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1e7); camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1e7);
/* controls = new THREE.TrackballControls( camera, renderer.domElement ); // I don't like how the Trackball controls work.
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = true;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.keys = [ 65, 83, 68 ];
camera.position.set(-8500,50,550);
/* camera.target = new THREE.Vector3().addVectors(/*new line for readability*/
// new THREE.Vector3(-9800,200,0), camera.getWorldDirection()); */
//camera.target = new THREE.Vector3(-9197,0,0); // END Trackball controls
controls = new THREE.OrbitControls( camera, renderer.domElement ); controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableDamping = true; controls.enableDamping = true;
controls.dampingFactor = 0.25; controls.dampingFactor = 0.25;
controls.enableZoom = true; controls.enableZoom = true;
camera.position.set(-9300,50,550);
controls.addEventListener( 'change', render ); controls.addEventListener( 'change', render );
var borders=[];
var geometry=[];
var material=[];
var planets=[];
var labels=[];
var bases=[];
var Text2D = THREE_Text.Text2D; var Text2D = THREE_Text.Text2D;
var SpriteText2D = THREE_Text.SpriteText2D; var SpriteText2D = THREE_Text.SpriteText2D;
var textAlign = THREE_Text.textAlign var textAlign = THREE_Text.textAlign
var raycaster = new THREE.Raycaster(); var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2(); var mouse = new THREE.Vector2();
var rays = []; var b_geometry, b_material, b_mesh, p_geometry, p_material, p_mesh, s_geometry, s_material, s_mesh, l_text;
jsonEmpire = jsonEmpire['ATS_Navcomp_DB']['empires'];
for (var key in jsonEmpire) { for (var key in jsonEmpire) {
area=jsonEmpire[key]; area=jsonEmpire[key];
// Border Generation. If it's not visible, don't generate it, dummy.
for (var key2 in area['borders']) { for (var key2 in area['borders']) {
var border = area['borders'][key2]; var border = area['borders'][key2];
geometry[border.name] = new THREE.SphereGeometry( border.radius, 10, 10 ); b_geometry = new THREE.SphereGeometry( border.radius, 10, 10 );
material[border.name] = new THREE.MeshBasicMaterial( { color: area.color, wireframe: true} ); b_material = new THREE.MeshBasicMaterial( { color: area.color, wireframe: true} );
borders[border.name] = new THREE.Mesh( geometry[border.name], material[border.name] ); b_mesh = new THREE.Mesh( b_geometry, b_material );b_mesh
borders[border.name].position.x = border.x; b_mesh.position.x = border.x;
borders[border.name].position.y = border.y; b_mesh.position.y = border.y;
borders[border.name].position.z = border.z; b_mesh.position.z = border.z;
borders[border.name].name = border.name; b_mesh.name = border.name;
scene.add( borders[border.name] ); scene.add( b_mesh );
if (border.radius > 10) { if (border.radius > 10) {
var text = new Text2D(border.name, { align: textAlign.center, font: '25px Arial', fillStyle: '#777' , antialias: false }); l_text = new Text2D(border.name, { align: textAlign.center, font: '25px Arial', fillStyle: '#777' , antialias: false });
text.material.alphaTest = 0.5; l_text.material.alphaTest = 0.5;
text.position.set(border.x,border.y,border.z); l_text.position.set(border.x,border.y,border.z);
text.scale.set(0.75,0.75,0.75); l_text.scale.set(0.75,0.75,0.75);
scene.add(text); scene.add(l_text);
}
} }
}
// Planet Generation // Planet Generation
for (var key in area["planets"]) { for (var key in area["planets"]) {
var planet = area.planets[key]; var planet = area.planets[key];
geometry[planet.name] = new THREE.SphereGeometry( 1, 10, 10 ); p_geometry= new THREE.SphereGeometry( 1, 10, 10 );
material[planet.name] = new THREE.MeshBasicMaterial( { color: area.color, wireframe: false} ); p_material = new THREE.MeshBasicMaterial( { color: area.color, wireframe: false} );
planets[planet.name]= new THREE.Mesh( geometry[planet.name], material[planet.name] ); p_mesh = new THREE.Mesh( p_geometry, p_material );
planets[planet.name].position.x=planet.x; p_mesh.position.x=planet.x;
planets[planet.name].position.y=planet.y; p_mesh.position.y=planet.y;
planets[planet.name].position.z=planet.z; p_mesh.position.z=planet.z;
planets[planet.name].name = planet.name; p_mesh.name = planet.name;
var text = new Text2D(planet.name, { align: textAlign.right, font: '12px Arial', fillStyle: '#FFF' , antialias: false }); scene.add( p_mesh );
text.material.alphaTest = 0.0; l_text = new Text2D(planet.name, { align: textAlign.right, font: '12px Arial', fillStyle: '#FFF' , antialias: false });
text.position.set(planet.x,planet.y,planet.z); l_text.material.alphaTest = 0.0;
text.scale.set(0.25,0.25,0.25); l_text.position.set(planet.x,planet.y,planet.z);
scene.add(text); l_text.scale.set(0.25,0.25,0.25);
scene.add( planets[planet.name] ); scene.add(l_text);
} }
// Base Generation // Base Generation
for (var key in area["stations"]) { for (var key in area["stations"]) {
var base = area.stations[key]; var base = area.stations[key];
var TILE_SIZE = 0.5; s_geometry = new THREE.CylinderGeometry( 0.1, 0.5*3, 0.5*3, 4 );
geometry[base.name] = new THREE.CylinderGeometry( 0.1, TILE_SIZE*3, TILE_SIZE*3, 4 ); s_material = new THREE.MeshBasicMaterial( { color: area.color, wireframe: false} );
material[base.name] = new THREE.MeshBasicMaterial( { color: area.color, wireframe: false} ); s_mesh = new THREE.Mesh( s_geometry, s_material );
bases[base.name] = new THREE.Mesh( geometry[base.name], material[base.name] ); s_mesh.position.x=base.x;
bases[base.name].position.x=base.x; s_mesh.position.y=base.y;
bases[base.name].position.y=base.y; s_mesh.position.z=base.z;
bases[base.name].position.z=base.z; s_mesh.name = base.name;
bases[base.name].name = base.name; scene.add( s_mesh );
scene.add( bases[base.name] ); l_text = new Text2D(base.name, { align: textAlign.left, font: '12px Arial', fillStyle: '#ABABAB' , antialias: false });
var text = new Text2D(base.name, { align: textAlign.left, font: '12px Arial', fillStyle: '#ABABAB' , antialias: false }); l_text.material.alphaTest = 0.0;
text.material.alphaTest = 0.0; l_text.position.set(base.x,base.y+3,base.z);
text.position.set(base.x,base.y+3,base.z); l_text.scale.set(0.20,0.20,0.20);
text.scale.set(0.20,0.20,0.20); scene.add(l_text);
scene.add(text);
} }
}
}
// Initial Target spot. UFP for convinence. // Set view and camera to point to initial location
reset_view(); reset_view();
} }
window.onresize = function() { window.onresize = function() {
camera.aspect = window.innerWidth / window.innerHeight; camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix(); camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight ); renderer.setSize( window.innerWidth, window.innerHeight );
render(); render();
}
function animate() {
var delta = clock.getDelta();
requestAnimationFrame( animate );
scene.updateMatrixWorld()
controls.update(delta);
renderer.render(scene, camera);
} }
function animate() {
var delta = clock.getDelta();
requestAnimationFrame( animate );
scene.updateMatrixWorld()
controls.update(delta);
render();
}
function render () { function render () {
//requestAnimationFrame( render );
requestAnimationFrame( animate ); renderer.render( scene, camera );
renderer.render( scene, camera );
} }
function listobjects(type) { function listobjects(type) {
var objects = {}; var objects = {};
for (var key in jsonEmpire) { for (var key in jsonEmpire) {
area=jsonEmpire[key]; area=jsonEmpire[key];
for (var key2 in area[type]) { for (var key2 in area[type]) {
object = area[type][key2]; object = area[type][key2];
objectname = object.name; objectname = object.name;
objects[object.name] = object; objects[object.name] = object;
} }
} }
return objects; return objects;
} }
function zoomfocus(name) { function zoomfocus(name) {
var types = ['planets','stations']; var types = ['planets','stations'];
for (var type in types){ for (var type in types) {
var objects = listobjects(types[type]); var objects = listobjects(types[type]);
for ( var key in objects ) { for ( var key in objects ) {
if (escapeHTML(key) == name) { if (escapeHTML(key) == name) {
var object = objects[key]; var object = objects[key];
controls.target.x = object.x; controls.target.x = object.x;
controls.target.y = object.y; controls.target.y = object.y;
controls.target.z = object.z; controls.target.z = object.z;
var focus = new THREE.Vector3( object.x, object.y, object.z ); var focus = new THREE.Vector3( object.x, object.y, object.z );
var vantage = new THREE.Vector3( 5, 60 , 150 ); var vantage = new THREE.Vector3( 5, 60 , 150 );
focus.add(vantage); focus.add(vantage);
camera.position.set(focus.x,focus.y,focus.z); camera.position.set(focus.x,focus.y,focus.z);
camera.updateProjectionMatrix();
render();
} }
} }
} }
} }
function drawline(name,origin,dest) { function drawline(name,origin,dest) {
var geometry = new THREE.Geometry(); var geometry = new THREE.Geometry();
var material = new THREE.LineBasicMaterial( { color: '#FFF', }); var material = new THREE.LineBasicMaterial( { color: '#FFF', });
geometry.vertices.push(origin, direction); geometry.vertices.push(origin, direction);
var line = new THREE.Line( geometry, material ); var line = new THREE.Line( geometry, material );
ray.name = "test"; ray.name = "test";
scene.add(ray); scene.add(ray);
animate(); animate();
} }

View File

@ -4,10 +4,10 @@
<script src="js/three.js"></script> <script src="js/three.js"></script>
<script src="js/OrbitControls.js"></script> <script src="js/OrbitControls.js"></script>
<script src="js/three-text2d.js"></script> <script src="js/three-text2d.js"></script>
<script src="js/Detector.js"></script> <script src="js/Detector.js"></script>
<script src="js/mapviewgl.js"></script> <script src="js/mapviewgl.js"></script>
<link href="https://fonts.googleapis.com/css?family=Space+Mono" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Space+Mono" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script> <script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script src="js/GUI.Utils.js"></script> <script src="js/GUI.Utils.js"></script>
<script> <script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
@ -20,109 +20,108 @@
</script> </script>
<style type="text/css"> <style type="text/css">
body { margin: 0; overflow:hidden;} body { margin: 0; overflow:hidden;}
canvas { width: 75%; height: 100% ;} canvas { width: 75%; height: 100% ;}
.reset-button {margin: 0; padding: 0; padding: 3px 3px; color: #AAA; } .reset-button {margin: 0; padding: 0; padding: 3px 3px; color: #AAA; }
.menu-button { float: right; position: absolute; right: 0.1vw; top: 0; z-index: 5; color:white; background-color:#003133; padding: 0px; font-weight: bold; font-size:2.5vh; } .menu-button { float: right; position: absolute; right: 0.1vw; top: 0; z-index: 5; color:white; background-color:#003133; padding: 0px; font-weight: bold; font-size:2.5vh; }
.reset-container { margin: 0; padding: 10px; font-weight: bold; text-align: center; font-family: 'Space Mono', monospace; color:#67989A; background-color: maroon; width: 100%; height: 2.5vh; float:bottom; } .reset-container { margin: 0; padding: 10px; font-weight: bold; text-align: center; font-family: 'Space Mono', monospace; color:#67989A; background-color: maroon; width: 100%; height: 2.5vh; float:bottom; }
.wvg-controls { position: fixed; left: 85%; height: 96%; width: 15%; margin-top: 0; display: none;} .wvg-controls { position: fixed; left: 85%; height: 96%; width: 15%; margin-top: 0; display: none;}
ul.wvg-navbar { height: 3vh; width: 100%; background-color: #003133; list-style-type: none; overflow: hidden; padding: 0; margin: 0; } ul.wvg-navbar { height: 3vh; width: 100%; background-color: #003133; list-style-type: none; overflow: hidden; padding: 0; margin: 0; }
ul.wvg-navbar li { width:auto; height: 100%; float: left; } .wvg-tablink { margin: 0; padding: 0; padding: 8px 10px; font-weight: bold; text-align: center; font-family: 'Space Mono', monospace; color:#67989A; } ul.wvg-navbar li { width:auto; height: 100%; float: left; } .wvg-tablink { margin: 0; padding: 0; padding: 8px 10px; font-weight: bold; text-align: center; font-family: 'Space Mono', monospace; color:#67989A; }
.wvg-tab-active { background-color: #0D4A4D; color: #003133; } .wvg-tab-active { background-color: #0D4A4D; color: #003133; }
.wvg-tools { width: 100%; height: 98%; background: #333; display: none; color: #AAA; padding-top: 2vh; padding-left:1vw; padding-right: 2.5vw;} .wvg-tools { width: 100%; height: 98%; background: #333; display: none; color: #AAA; padding-top: 2vh; padding-left:1vw; padding-right: 2.5vw;}
.wvg-tools select { .wvg-tools select {
width: 75%; width: 75%;
padding: 5px; padding: 5px;
font-size: 16px; font-size: 16px;
line-height: 1; line-height: 1;
border: 1px solid #67878A; border: 1px solid #67878A;
border-radius: 0; border-radius: 0;
height: auto; height: auto;
-webkit-appearance: none; -webkit-appearance: none;
background: url('assets/select-arrow.png') no-repeat right center #333; background: url('assets/select-arrow.png') no-repeat right center #333;
color: #67989A; color: #67989A;
margin: 0 auto; margin: 0 auto;
} }
.wvg-tools span { text-align: center; color: #FFF; font-family: 'Space Mono', monospace; margin: 0 auto;} .wvg-tools span { text-align: center; color: #FFF; font-family: 'Space Mono', monospace; margin: 0 auto;}
.wvg-first { display: block; } .wvg-first { display: block; }
#wrapper { height: inherit; width: 100%; } #wrapper { height: inherit; width: 100%; }
.active { display: block; } .active { display: block; }
input[type=radio], input[type=checkbox] { input[type=radio], input[type=checkbox] {
display:none; display:none;
} }
input[type=checkbox] + label:before { input[type=checkbox] + label:before {
content: ""; content: "";
display: inline-block; display: inline-block;
width: 15px; width: 15px;
height: 15px; height: 15px;
vertical-align:middle; vertical-align:middle;
margin-right: 8px; margin-right: 8px;
background-color: #aaa; background-color: #aaa;
box-shadow: inset 0px 2px 2px rgba(0, 0, 0, .3); box-shadow: inset 0px 2px 2px rgba(0, 0, 0, .3);
border-radius: 4px; border-radius: 4px;
} }
input[type=radio]:checked + label:before { input[type=radio]:checked + label:before {
content: "\2022"; /* Bullet */ content: "\2022"; /* Bullet */
color:white; color:white;
background-color: #666; background-color: #666;
font-size:1.8em; font-size:1.8em;
text-align:center; text-align:center;
line-height:14px; line-height:14px;
text-shadow:0px 0px 3px #eee; text-shadow:0px 0px 3px #eee;
} }
input[type=checkbox]:checked + label:before { input[type=checkbox]:checked + label:before {
content:"\2714"; /* Tick */ content:"\2714"; /* Tick */
color:white; color:white;
background-color: #666; background-color: #666;
text-align:center; text-align:center;
line-height:15px; line-height:15px;
text-shadow:0px 0px 3px #eee; text-shadow:0px 0px 3px #eee;
} }
.btn { .btn {
background: #67989A; background: #67989A;
-webkit-border-radius: 0; -webkit-border-radius: 0;
-moz-border-radius: 0; -moz-border-radius: 0;
border-radius: 0px; border-radius: 0px;
font-family: 'Space Mono', monospace; font-family: 'Space Mono', monospace;
color: #003133; color: #003133;
font-size: 14px; font-size: 14px;
width: 75%; width: 75%;
height: 4vh; height: 4vh;
padding: 3px; padding: 3px;
border: solid #67878A 1px; border: solid #67878A 1px;
text-decoration: none; text-decoration: none;
} }
.btn:hover { .btn:hover {
background: #0D4A4D; background: #0D4A4D;
text-decoration: none; text-decoration: none;
} }
</style> </style>
</head> </head>
<body> <body>
<div id="container"></div> <div id="container"></div>
<div class="menu-button" id="hotdog"> &#9776; </div> <div class="menu-button" id="hotdog"> &#9776; </div>
<div id="controls" class="wvg-controls active"> <div id="controls" class="wvg-controls active">
<div id="wrapper"> <div id="wrapper">
<ul class="wvg-navbar"> <ul class="wvg-navbar">
<li><a class="wvg-tablink wvg-tab-active" href='#' onclick="openTab(event,'Find');">Find</a></li> <li><a class="wvg-tablink wvg-tab-active" href='#' onclick="openTab(event,'Find');">Find</a></li>
<li><a class="wvg-tablink" href='#' onclick="openTab(event,'Info');">Info</a></li> <li><a class="wvg-tablink" href='#' onclick="openTab(event,'Info');">Info</a></li>
</ul> </ul>
<div class="wvg-tools wvg-first" id="Find"> <div class="wvg-tools wvg-first" id="Find">
<span> Find Object By Name </span> <span> Find Object By Name </span>
<span> <select id="findbyselect"> <span> <select id="findbyselect">
<option value=''></option> <option value=''></option>
</select> </span> </select> </span>
<br/ ><input type="checkbox" name="objtype" id="cbp" value="planets" onclick="populateUserFields();"><label for="cbp"> Planets</label> <input type="checkbox" name="objtype" id="cbs" value="stations" onclick="populateUserFields();"> <label for="cbs">Stations</label> <br/><input type="checkbox" name="objtype" id="cbp" value="planets" onclick="populateUserFields();"><label for="cbp"> Planets</label> <input type="checkbox" name="objtype" id="cbs" value="stations" onclick="populateUserFields();"> <label for="cbs">Stations</label>
<br><button id="submitfindbyname" class="btn">Find</button> <br /><button id="submitfindbyname" class="btn">Find</button>
</div> </div>
<div class="wvg-tools" id="Info">
<div class="wvg-tools" id="Info"> <span> This tool was designed by Frey @ ATSMUSH. It is based on MapView for ATS with data used from the Navcomp MUSHClient plugin with permission. </span>
<span> This tool was designed by Frey @ ATSMUSH. It is based on MapView for ATS with data used from the Navcomp MUSHClient plugin with permission. </span> </div>
</div> </div>
</div> <div class="reset-container"><span class="reset-button"> Reset </span></div> <div class="reset-container"><span class="reset-button"> Reset </span></div>
</div> </div>
</body> </body>
</html> </html>