forked from jamespeterthornton/HackDartmouth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
322 lines (267 loc) · 8.64 KB
/
main.js
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
function calculatePoint(lat, lon) {
lon -= 9.1;
var phi = (lat)*Math.PI/180;
var theta = (lon-180)*Math.PI/180;
var x = - Math.cos(phi) * Math.cos(theta);
var y = Math.sin(phi);
var z = Math.cos(phi) * Math.sin(theta);
answer = [x, y, z];
//console.log(answer);
return answer;
}
function calculatePointM(lat, lon, offset) {
lon -= 9.1;
var phi = (lat)*Math.PI/180;
var theta = (lon-180)*Math.PI/180;
var x = -offset * Math.cos(phi) * Math.cos(theta);
var y = offset * Math.sin(phi);
var z = offset * Math.cos(phi) * Math.sin(theta);
answer = [x, y, z];
return answer;
}
function calculateSpline(slat, slon, elat, elon, offset) {
var lat = (slat + elat)/2;
var lon = (elon + elon)/2;
lon -= 9.1;
var phi = (lat)*Math.PI/180;
var theta = (lon-180)*Math.PI/180;
var x = - offset * Math.cos(phi) * Math.cos(theta);
var y = offset * Math.sin(phi);
var z = offset * Math.cos(phi) * Math.sin(theta);
answer = [x, y, z];
return answer;
}
//reference points for countries
function initCountry() {
//reference points for countries
var countriesLL = [
["Canada", 65, -95 ],
["Mexico", 19, -102.4],
["China", 35, 103],
["Japan", 35, 136],
["UK", 54, -2],
["Germany", 51.6, 10],
["Brazil", -10.7, -53],
["Netherlands", 52.2, 4.5],
["HK", 22.3, 114.2],
["Korea", 37.6, 127],
["Saudi", 24, 45],
["France", 47, 2],
["India", 21, 78],
["US", 38.9, -90.01]
];
var countries = {};
for (var i = 0; i < countriesLL.length; i++) {
var key = countriesLL[i][0];
var lat = countriesLL[i][1];
var lon = countriesLL[i][2];
//console.log(key);
//calculatePoint(countriesLL[i][0], countries.countriesLL[i][1]);
var data = calculatePoint(lat, lon);
countries[key] = data;
};
//console.log(1);
//console.log(countries["Canada"]);
//console.log(calculatePoint(65, -95))
//console.log(Object.keys(countries));
return countries;
}
function calculateSpline1(origin, dest) {
var p1 = new THREE.Vector3().fromArray(origin);
var p2 = new THREE.Vector3().fromArray(dest);
console.log(p1);
console.log(p2);
var p3 = new THREE.Vector3()
p3.addVectors(p1, p2)
p3.normalize()
p3.multiplyScalar(2)
console.log("p3");
console.log(p3)
return p3.toArray();
}
function addCurve(arr1, arr2, arr3, object, geometry1, material1) {
var curve = new THREE.QuadraticBezierCurve3(
new THREE.Vector3( arr1[0], arr1[1], arr1[2]),
new THREE.Vector3( arr2[0], arr2[1], arr2[2]),
new THREE.Vector3( arr3[0], arr3[1], arr3[2])
);
var geometry = new THREE.Geometry();
geometry.vertices = curve.getPoints( 500 );
var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
material.linewidth = 2;
material.opacity = 1;
material.transparent = true;
console.log(material);
// Create the final Object3d to add to the scene
var curveObject = new THREE.Line( geometry, material );
object.add(curveObject);
var city = new THREE.Mesh( geometry1, material1 );
city.translateX(arr3[0]);
city.translateY(arr3[1]);
city.translateZ(arr3[2]);
object.add(city);
}
window.onload = function() {
//init
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//lights
var light = new THREE.AmbientLight( 0x404040 ); // soft white light
scene.add( light );
var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
directionalLight.position.set( 0, 0.5, 1 );
scene.add( directionalLight );
var geometry = new THREE.SphereGeometry( 1, 50, 50 );
var material = new THREE.MeshPhongMaterial();
material.map = THREE.ImageUtils.loadTexture('images/map.png')
material.map.minFilter = THREE.NearestFilter;
var sphere1 = new THREE.Mesh( geometry, material );
var material1 = new THREE.MeshPhongMaterial({color: 0xFF0000});
var geometry1 = new THREE.SphereGeometry( 0.02, 32, 32 )
var sphere2 = new THREE.Mesh( geometry1, material1 );
scene.add( sphere1 );
//sphere.translateX()
//0, 0
// var translate1 = calculatePoint(0, 0);
// sphere2.translateX(translate1[0]);
// sphere2.translateY(translate1[1]);
// sphere2.translateZ(translate1[2]);
//sphere1.add( sphere2 );
//180, 180
// var sphere3 = new THREE.Mesh( geometry1, material1 );
// var translate2 = calculatePoint(20, 0);
// sphere3.translateX(translate2[0]);
// sphere3.translateY(translate2[1]);
// sphere3.translateZ(translate2[2]);
// sphere1.add( sphere3 );
//90, 0
// var sphere4 = new THREE.Mesh( geometry1, material1 );
// var translate3 = calculatePoint(-22.9, -43.1);
// sphere4.translateX(translate3[0]);
// sphere4.translateY(translate3[1]);
// sphere4.translateZ(translate3[2]);
// sphere1.add( sphere4 );
// var sphere5 = new THREE.Mesh( geometry1, material1 );
// var translate4 = calculatePoint(40.71, -74.01);
// sphere5.translateX(translate4[0]);
// sphere5.translateY(translate4[1]);
// sphere5.translateZ(translate4[2]);
// sphere1.add( sphere5 );
//var mid = calculateSpline(-22.9, -43.1, 40.71, -74.01, 1.5);
//reference countries
var countries = initCountry();
//US export then import
var USEcondata = [
[
["Canada", 300.2], ["Mexico", 226.2], ["China", 122.1], ["Japan", 65.2],
["UK", 47.4], ["Germany",47], ["Brazil",44.2], ["Netherlands", 42.7],
["HK", 42.3], ["Korea", 41.5]
],
[
["China", 460], ["Canada", 336.6], ["Mexico", 283.1], ["Japan", 143.1],
["Germany", 116.9], ["Korea",65], ["UK",53.6], ["Saudi", 53.1],
["France", 46.4], ["India", 43.3]
]
]
//new points
//addCurve(translate3, mid, translate4, sphere1);
//add data points to world
var origin = countries["US"];
var us = new THREE.Mesh( geometry1, material1 );
us.translateX(origin[0]);
us.translateY(origin[1]);
us.translateZ(origin[2]);
sphere1.add(us);
//console.log(origin);
//origin = new THREE.Vector3().fromArray(origin)
//exports
var geometry2 = new THREE.SphereGeometry( 0.015, 32, 32 )
for (var i = 0; i < USEcondata[0].length; i++) {
var key = USEcondata[0][i][0];
//var lat = referenceAr[key
console.log(key);
console.log(countries[key]);
var dest = countries[key];
//add the line
var mid = calculateSpline1(origin, dest);
console.log("mid");
console.log(mid);
addCurve(origin, mid, dest, sphere1, geometry2, material1);
//add destination
}
var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
scene.add( camera )
camera.position.z = 3;
var lrAngle = 0;
var udAngle = 0;
var zoom = 1;
var temp;
var mutlMatrix;
document.addEventListener('keydown', function(event) {
//rotates world up; moves camera down
if (event.keyCode == 40) {
event.preventDefault();
//this check prevents axis from flipping
if (udAngle >= -123) {
udAngle -= 2;
sphere1.rotation.x += 0.5;
}
}
//rotates the world down; moves camera up
else if (event.keyCode == 38) {
event.preventDefault();
//this check prevents axis from flipping
if (udAngle <= 54) {
udAngle += 2;
sphere1.rotation.x -= 0.5;
}
}
//rotates the world right; moves camera left
else if (event.keyCode == 37) {
event.preventDefault();
//this check makes realistic turns
if (lrAngle >= -360) {
lrAngle -= 5;
console.log(1);
sphere1.rotation.y += 0.5;
}
}
//rotates the world left; moves camera right
else if (event.keyCode == 39) {
event.preventDefault();
//this check makes realistics turns
if (lrAngle <= 360) {
lrAngle += 10;
sphere1.rotation.y -= 0.5;
}
}
//zoom in
else if (event.keyCode == 90) {
event.preventDefault();
//prevent from zooming too much and not seeing anything
if (zoom >= 0.1) {
zoom *= 0.9;
camera.position.z -= 0.1;
console.log(1);
}
}
//zoom out
else if (event.keyCode == 88) {
event.preventDefault();
//prevents from zooming out too much and not seeing anything
if (zoom <= 10) {
zoom *= 1.1;
camera.position.z += 0.1;
//zoomT.value = zoom;
}
}
})
var render = function () {
requestAnimationFrame(render);
renderer.render(scene, camera);
};
render();
}