-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStarBurst.js
557 lines (422 loc) · 14.4 KB
/
StarBurst.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*global THREE: false*/
/*global console: false*/
/*global $: false*/
/*global requestAnimationFrame: false*/
/**
Copyright 2012 Samuel Miller
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
*NOTES
*http://mrdoob.github.com/three.js/examples/misc_camera_orbit.html
*/
/*===INIT===*/
//var camera, scene, renderer, geometry, material, mesh;
var mouse = {x:0,y:0};
/*===Functions===*/
/**
* The basic setup function for creating a new StarBurst object
*
*/
var StarBurst = function(JSONArr){
"use strict";
this.camera = null;
this.scene = null;
this.renderer = null;
this.geometry = null;
this.material = null;
this.mesh = null;
this.mouse = {x: 0 ,y: 0};
this.cameraMovement = {x: 0, y: 0, z: (Math.PI/2)};
this.cameraPosDirection = {x: true, y: true, z: true};
this.planeOn = true;
this.planeWidth = 500;
this.planeHeight = 500;
this.numOfGrids = 10;
this.planeColor = 0x555555;
this.linesToStars = true;
this.lineColor = 0x555555;
this.lineWidth = 0.01;
this.mouseDown = false;
if(JSONArr === null || JSONArr === undefined){
this.JSONArr = null;
}else{
this.JSONArr = JSONArr;
}
this.starFile = "star.png";
this.offsetX = 0;
this.offsetY = 0;
this.offsetSpeedX = 0;
this.offsetSpeedY = 0;
this.mouse.x = 0;
this.mouse.y = 0;
this.distance = 400;
this.focusPoint = new THREE.Vector3(0,0,0);
};
/**
* A function that when given the type of a star wil give back the color information for that star
* @param type A String with a letter for one of the starTypes
* @retrun
*/
StarBurst.prototype.getStarTypeColorData = function(type){
//TODO: Implment
"use strict";
};
/**
* Will process the keypress one of several different ways
* @param e - An event from the keypress
* @return Nothing
*/
StarBurst.prototype.keyPressed = function(e){
"use strict";
var charCode = e.charCode;
//Check to see if 's' was pressed to stop the rotation
if(charCode === 115 || charCode === 83){
this.offsetX = 0;
this.offsetY = 0;
}
//Checks '+' or '=' and zooms in
if(charCode === 45 || charCode === 95){
this.distance += 2;
}
//Checks '-' or '_' and zooms out
if(charCode === 61 || charCode === 43){
this.distance -= 2;
}
//For debugging
//console.log(e);
};
/**
* Will calculate the drag and check to see if the user has clicked on anything
* @param e - An event from the mouseDown
* @return Nothing
*/
StarBurst.prototype.onMouseDown = function(e){
"use strict";
e.preventDefault();
this.mouseDown = true;
this.mouse.x = e.clientX;
this.mouse.y = e.clientY;
var tempX = (e.clientX / window.innerWidth ) * 2 - 1;
var tempY = -(e.clientY / window.innerHeight ) * 2 + 1;
//Was to help make the picking very exact. Probably no longer needed
//tempX -= 0.015;
//tempY += 0.03;
var projector = new THREE.Projector();
var vector = new THREE.Vector3( tempX, tempY, -1 );
projector.unprojectVector( vector, this.camera );
var ray = new THREE.Ray( this.camera.position, vector.subSelf( this.camera.position ).normalize() );
//Get the intersected objects
var intersects = ray.intersectObjects( this.scene.children );
console.log(this.scene.position);
if ( intersects.length > 0 ) {
//Getting the first selected object
var INTERSECTED = intersects[ 0 ].object;
//Confirm that if you ARE selecting the plane select something else
if(intersects[0].object.name === "Plane" && intersects.length > 1){
INTERSECTED = intersects[ 1 ].object;
}
// This is redundant but it might be used for other objects
if(INTERSECTED.name !== "Plane"){
//TODO: Cleaner Parameters
//INTERSECTED.name
//console.log("you clicked particle named '" + this.JSONArr[parseInt(INTERSECTED.name, 10)].name + "' with id: " + INTERSECTED.id);
//INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
//INTERSECTED.material.color.setHex( 0xff0000 );
//TODO: Put this into a function
this.updateStarInfo(this.JSONArr[parseInt(INTERSECTED.name, 10)]);
//TODO: Redo the camera position
//TODO: Also do a tweet
//TODO: Make a function for this.
this.focusPoint = INTERSECTED.position;
//this.cameraMovement = {x: 1000, y: 1000, z: (Math.PI/2)};
//this.camera.position.x = Math.sin(this.cameraMovement.x) * this.distance;
//this.camera.position.y = Math.sin(this.cameraMovement.y) * this.distance;
//this.camera.position.z = Math.cos(this.cameraMovement.x) * this.distance;
console.log(INTERSECTED);
}
}
};
/**
*
* @param e -
* @return Nothing
*/
StarBurst.prototype.onMouseMove = function(e){
"use strict";
//
if(this.mouseDown){
//TODO: Figure out how to get the x and y of
var currMouseX = e.clientX;
var currMouseY = e.clientY;
//TODO: Figure out the center
var centerX = currMouseX - (window.innerWidth/2);
var centerY = currMouseY - (window.innerHeight/2);
//`rotationSpeed` should be lowered for greater sensitivy.
var rotationSpeed = 1000;
//TODO: Figure out how to keep it in the same spot
var offsetX = (currMouseX - this.mouse.x)/ rotationSpeed;
var offsetY = (currMouseY - this.mouse.y)/ rotationSpeed;
this.cameraMovement.x += offsetX/20;
this.cameraMovement.y += offsetY/20;
this.cameraMovement.z += offsetX/20;
//
this.offsetX = offsetX;
this.offsetY = offsetY;
}
};
/**
* This will be mainly to update the x and y mouse coordiates and not much else
* @param e - Event
* @return Nothing
*/
StarBurst.prototype.onMouseUp = function(e){
"use strict";
e.preventDefault();
//DEV: Just to check that the mouse is working
this.mouseDown = false;
//TODO:
//Update this.mouse
this.mouse.x = e.clientX;
this.mouse.y = e.clientY;
};
/**
* Will load a JSON file of star data
* @param file - A file to load
* @return a JSON Array
*/
StarBurst.prototype.loadJSON = function(file){
"use strict";
$.getJSON(file,function(data){
});
};
//TODO: Have it take a parameter object
/**
* The basic initilization function for StarBrst
* @return Nothing
*/
StarBurst.prototype.init = function(){
"use strict";
var container = document.createElement( 'div' );
document.body.appendChild( container );
var starInfoDiv = document.createElement( 'div' );
starInfoDiv.style.position = "absolute";
starInfoDiv.style.top = "10px";
starInfoDiv.className = "info";
starInfoDiv.innerHTML = "<strong>Controls</strong><p>Click and drag to rotate</p><p>Click a star to see it's name</p><p>Press 's' to stop the rotation</p><p> '-' and '+' to zoom in/out";
container.appendChild(starInfoDiv);
var starNameDiv = document.createElement( 'div' );
starNameDiv.style.position = "absolute";
starNameDiv.className = "starName";
starNameDiv.innerHTML = "<strong>Star Name</strong><p class='star'>None</p>";
container.appendChild(starNameDiv);
//Start the Three.Scene
this.scene = new THREE.Scene();
//TODO: Look at the documentation for the camera more
//TODO: Have more options for this
this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
//TODO: This should be a position
this.camera.position.z = 400;
this.scene.add(this.camera);
//TODO: Have option for using a plane at all
this.geometry = new THREE.PlaneGeometry(this.planeWidth, this.planeHeight, this.numOfGrids, this.numOfGrids);
//TODO: Have more options
var planeMaterial = new THREE.MeshBasicMaterial( { color: this.planeColor, wireframe: true } );
var planeMesh = new THREE.Mesh(this.geometry,planeMaterial);
planeMesh.doubleSided = true;
planeMesh.name = 'Plane';
//Add the plane to the scene
this.scene.add(planeMesh);
//
var light = new THREE.DirectionalLight( 0xffffff, 2 );
light.position.set( 1, 1, 1 ).normalize();
this.scene.add( light );
//Maybe use the example file
//TODO: Load from JSON file
light = new THREE.DirectionalLight( 0xffffff );
light.position.set( -1, -1, -1 ).normalize();
this.scene.add( light );
//TODO: Maybe there is a better name
var starMaterial = null;
//Currently I am not using any images but for the highlighting I might need to
//TODO: Rename star2.png
//TODO: Have the location be an option
var sprite = THREE.ImageUtils.loadTexture(this.starFile);
var JSONArr = this.JSONArr;
var tempGeo = new THREE.Geometry();
//TODO: Separete into function
for(var i = 0;i < JSONArr.length;i++){
var starData = JSONArr[i];
//TODO: rename 'vertex' to 'starVertex'
var starVertex = new THREE.Vector3();
this.geometry = new THREE.Geometry();
starVertex.x = starData.x;
starVertex.y = starData.y;
starVertex.z = starData.z;
if(this.linesToStars){
var lineGeometry = new THREE.Geometry();
var newVertexY = starVertex.y;
if(newVertexY > 0){
newVertexY -= 3;
}else{
newVertexY += 3;
}
//Due to the nature of the diffference between how we standardly think of...
//3D cooridinates and how WebGL uses it this is needed
//TODO: Add the correct points
lineGeometry.vertices.push(new THREE.Vector3(starVertex.x, newVertexY, starVertex.z));
lineGeometry.vertices.push(new THREE.Vector3(starVertex.x, 0, starVertex.z));
//TODO: Make some into variables
var lineMaterial = new THREE.LineBasicMaterial({color:0x555555, linewidth: 0.01});
var line = new THREE.Line(lineGeometry,lineMaterial);
this.scene.add(line);
}
//The location the star will appear at
this.geometry.vertices.push(starVertex);
tempGeo.vertices.push(starVertex);
//TODO:
var size = 10;
//DEV: Figure out what the options are for ParticleBasciMaterial
//TODO: There should be a lot of Options here
starMaterial = new THREE.ParticleBasicMaterial({ size: size,
map: sprite,
blending: THREE.AdditiveBlending,
depthTest: false
//transparent: true,
//vertexColors: true
});
//TODO: Put into a sort of ENUM or Object
var classM = 0xFF4719;
var classK = 0xFFCC00;
var classG = 0xE6E600;
var classF = 0xFFFFCC;
var classA = 0xBDCEFF;
var classB = 0xA6BBFF;
var classO = 0x9DB4FF;
//Get the starType
var starType = starData.type;
//A default color
var starColor = classM;
//TODO: I don't like this way of doing things...
if(starType === "M"){
starColor = classM;
}else if(starType === "K"){
starColor = classK;
}else if(starType === "G"){
starColor = classG;
}else if(starType === "F"){
starColor = classF;
}else if(starType === "A"){
starColor = classA;
}else if(starType === "B"){
starColor = classB;
}else if(starType === "O"){
starColor = classO;
}
//DEV: I am not sure how I feel about a particle system for everthing
//DEV: It could slow things down a lot and hard to be selected
//TODO: Is there a better name for this?
//var particleStarSystem = new THREE.ParticleSystem(this.geometry,starMaterial);
var sphereGeo = new THREE.SphereGeometry( 3, 8, 8 );
var sphere = new THREE.Mesh( sphereGeo, new THREE.MeshBasicMaterial( { color: starColor } ) );
sphere.name = i;
sphere.position.x = starData.x;
sphere.position.y = starData.y;
sphere.position.z = starData.z;
//Add to sceen
//this.scene.add(particleStarSystem);
this.scene.add(sphere);
}
//var particleStarSystem = new THREE.ParticleSystem(tempGeo,starMaterial);
//Add to sceen
//this.scene.add(particleStarSystem);
//TODO: This needs to be an option Use WebGL or Canvas with options for each
this.renderer = new THREE.WebGLRenderer({alpha:0});
//TODO: These need to be options
this.renderer.setSize(window.innerWidth, window.innerHeight);
//TODO: option of speficiying a element to attach to...?
//Attach to the body of the document
container.appendChild(this.renderer.domElement);
console.log(this.scene);
console.log(this.camera);
console.log(THREE.RenderPass);
/*var renderModel = new THREE.RenderPass(this.scene, this.camera);
/*var renderModel = new THREE.RenderPass( this.scene, this.camera );
/*var effectBloom = new THREE.BloomPass( 1.3 );
var effectScreen = new THREE.ShaderPass( THREE.ShaderExtras[ "screen" ] );
effectFXAA = new THREE.ShaderPass( THREE.ShaderExtras[ "fxaa" ] );
var width = window.innerWidth || 2;
var height = window.innerHeight || 2;
effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height );
effectScreen.renderToScreen = true;
composer = new THREE.EffectComposer( renderer );
composer.addPass( renderModel );
composer.addPass( effectFXAA );
composer.addPass( effectBloom );
composer.addPass( effectScreen );*/
//TODO: Add as option
//Attach listener(s)
document.addEventListener('mousedown',this.onMouseDown.bind(this),false);
document.addEventListener('mousemove',this.onMouseMove.bind(this),false);
document.addEventListener('mouseup',this.onMouseUp.bind(this),false);
document.addEventListener('keypress',this.keyPressed.bind(this), true);
};
/**
* Will update the star info in the starinfo container
* @param data - The data of an star from the JSONArr
*
*/
StarBurst.prototype.updateStarInfo = function(data){
"use strict";
$('.star').text(data.name);
};
/**
* Will make the stars from the Json Data once it is loaded
* @return Nothing
*/
StarBurst.prototype.constructFromJSONArr = function(){
"use strict";
if(this.JSONLoaded === true){
}else{
//TODO: Call this after an interval
//setTimeout();
}
};
/**
* Main render function for StarBurst.
* @return Nothing
*/
StarBurst.prototype.render = function(){
"use strict";
//Setting up of rpicking
//TODO: Also have these be options
//TODO: Have an additional
this.camera.position.x = this.focusPoint.x + Math.sin(this.cameraMovement.x) * this.distance;
this.camera.position.y = this.focusPoint.y + Math.sin(this.cameraMovement.y) * this.distance;
this.camera.position.z = this.focusPoint.z + Math.cos(this.cameraMovement.x) * this.distance;
this.cameraMovement.x += this.offsetX/20;
this.cameraMovement.y += this.offsetY/20;
this.cameraMovement.z += this.offsetX/20;
//Adjust camera
this.camera.lookAt(this.focusPoint);
//Render
this.renderer.render(this.scene, this.camera);
};
/**
* The main animate function
* @return
*/
StarBurst.prototype.animate = function(){
//The bind is needed to make sure that the animate function uses 'this'
requestAnimationFrame(this.animate.bind(this));
//Render scene
this.render();
};