index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. THREE.TrackballControls = function ( object, domElement ) {
  2. var _this = this;
  3. var STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
  4. this.object = object;
  5. this.domElement = ( domElement !== undefined ) ? domElement : document;
  6. // API
  7. this.enabled = true;
  8. this.screen = { left: 0, top: 0, width: 0, height: 0 };
  9. this.rotateSpeed = 1.0;
  10. this.zoomSpeed = 1.2;
  11. this.panSpeed = 0.3;
  12. this.noRotate = false;
  13. this.noZoom = false;
  14. this.noPan = false;
  15. this.staticMoving = false;
  16. this.dynamicDampingFactor = 0.2;
  17. this.minDistance = 0;
  18. this.maxDistance = Infinity;
  19. this.keys = [ 65 /*A*/, 83 /*S*/, 68 /*D*/ ];
  20. // internals
  21. this.target = new THREE.Vector3();
  22. var EPS = 0.000001;
  23. var lastPosition = new THREE.Vector3();
  24. var _state = STATE.NONE,
  25. _prevState = STATE.NONE,
  26. _eye = new THREE.Vector3(),
  27. _movePrev = new THREE.Vector2(),
  28. _moveCurr = new THREE.Vector2(),
  29. _lastAxis = new THREE.Vector3(),
  30. _lastAngle = 0,
  31. _zoomStart = new THREE.Vector2(),
  32. _zoomEnd = new THREE.Vector2(),
  33. _touchZoomDistanceStart = 0,
  34. _touchZoomDistanceEnd = 0,
  35. _panStart = new THREE.Vector2(),
  36. _panEnd = new THREE.Vector2();
  37. // for reset
  38. this.target0 = this.target.clone();
  39. this.position0 = this.object.position.clone();
  40. this.up0 = this.object.up.clone();
  41. // events
  42. var changeEvent = { type: 'change' };
  43. var startEvent = { type: 'start' };
  44. var endEvent = { type: 'end' };
  45. // methods
  46. this.handleResize = function () {
  47. if ( this.domElement === document ) {
  48. this.screen.left = 0;
  49. this.screen.top = 0;
  50. this.screen.width = window.innerWidth;
  51. this.screen.height = window.innerHeight;
  52. } else {
  53. var box = this.domElement.getBoundingClientRect();
  54. // adjustments come from similar code in the jquery offset() function
  55. var d = this.domElement.ownerDocument.documentElement;
  56. this.screen.left = box.left + window.pageXOffset - d.clientLeft;
  57. this.screen.top = box.top + window.pageYOffset - d.clientTop;
  58. this.screen.width = box.width;
  59. this.screen.height = box.height;
  60. }
  61. };
  62. this.handleEvent = function ( event ) {
  63. if ( typeof this[ event.type ] == 'function' ) {
  64. this[ event.type ]( event );
  65. }
  66. };
  67. var getMouseOnScreen = ( function () {
  68. var vector = new THREE.Vector2();
  69. return function getMouseOnScreen( pageX, pageY ) {
  70. vector.set(
  71. ( pageX - _this.screen.left ) / _this.screen.width,
  72. ( pageY - _this.screen.top ) / _this.screen.height
  73. );
  74. return vector;
  75. };
  76. }() );
  77. var getMouseOnCircle = ( function () {
  78. var vector = new THREE.Vector2();
  79. return function getMouseOnCircle( pageX, pageY ) {
  80. vector.set(
  81. ( ( pageX - _this.screen.width * 0.5 - _this.screen.left ) / ( _this.screen.width * 0.5 ) ),
  82. ( ( _this.screen.height + 2 * ( _this.screen.top - pageY ) ) / _this.screen.width ) // screen.width intentional
  83. );
  84. return vector;
  85. };
  86. }() );
  87. this.rotateCamera = ( function() {
  88. var axis = new THREE.Vector3(),
  89. quaternion = new THREE.Quaternion(),
  90. eyeDirection = new THREE.Vector3(),
  91. objectUpDirection = new THREE.Vector3(),
  92. objectSidewaysDirection = new THREE.Vector3(),
  93. moveDirection = new THREE.Vector3(),
  94. angle;
  95. return function rotateCamera() {
  96. moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );
  97. angle = moveDirection.length();
  98. if ( angle ) {
  99. _eye.copy( _this.object.position ).sub( _this.target );
  100. eyeDirection.copy( _eye ).normalize();
  101. objectUpDirection.copy( _this.object.up ).normalize();
  102. objectSidewaysDirection.crossVectors( objectUpDirection, eyeDirection ).normalize();
  103. objectUpDirection.setLength( _moveCurr.y - _movePrev.y );
  104. objectSidewaysDirection.setLength( _moveCurr.x - _movePrev.x );
  105. moveDirection.copy( objectUpDirection.add( objectSidewaysDirection ) );
  106. axis.crossVectors( moveDirection, _eye ).normalize();
  107. angle *= _this.rotateSpeed;
  108. quaternion.setFromAxisAngle( axis, angle );
  109. _eye.applyQuaternion( quaternion );
  110. _this.object.up.applyQuaternion( quaternion );
  111. _lastAxis.copy( axis );
  112. _lastAngle = angle;
  113. } else if ( ! _this.staticMoving && _lastAngle ) {
  114. _lastAngle *= Math.sqrt( 1.0 - _this.dynamicDampingFactor );
  115. _eye.copy( _this.object.position ).sub( _this.target );
  116. quaternion.setFromAxisAngle( _lastAxis, _lastAngle );
  117. _eye.applyQuaternion( quaternion );
  118. _this.object.up.applyQuaternion( quaternion );
  119. }
  120. _movePrev.copy( _moveCurr );
  121. };
  122. }() );
  123. this.zoomCamera = function () {
  124. var factor;
  125. if ( _state === STATE.TOUCH_ZOOM_PAN ) {
  126. factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
  127. _touchZoomDistanceStart = _touchZoomDistanceEnd;
  128. _eye.multiplyScalar( factor );
  129. } else {
  130. factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
  131. if ( factor !== 1.0 && factor > 0.0 ) {
  132. _eye.multiplyScalar( factor );
  133. if ( _this.staticMoving ) {
  134. _zoomStart.copy( _zoomEnd );
  135. } else {
  136. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  137. }
  138. }
  139. }
  140. };
  141. this.panCamera = ( function() {
  142. var mouseChange = new THREE.Vector2(),
  143. objectUp = new THREE.Vector3(),
  144. pan = new THREE.Vector3();
  145. return function panCamera() {
  146. mouseChange.copy( _panEnd ).sub( _panStart );
  147. if ( mouseChange.lengthSq() ) {
  148. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  149. pan.copy( _eye ).cross( _this.object.up ).setLength( mouseChange.x );
  150. pan.add( objectUp.copy( _this.object.up ).setLength( mouseChange.y ) );
  151. _this.object.position.add( pan );
  152. _this.target.add( pan );
  153. if ( _this.staticMoving ) {
  154. _panStart.copy( _panEnd );
  155. } else {
  156. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  157. }
  158. }
  159. };
  160. }() );
  161. this.checkDistances = function () {
  162. if ( ! _this.noZoom || ! _this.noPan ) {
  163. if ( _eye.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  164. _this.object.position.addVectors( _this.target, _eye.setLength( _this.maxDistance ) );
  165. _zoomStart.copy( _zoomEnd );
  166. }
  167. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  168. _this.object.position.addVectors( _this.target, _eye.setLength( _this.minDistance ) );
  169. _zoomStart.copy( _zoomEnd );
  170. }
  171. }
  172. };
  173. this.update = function () {
  174. _eye.subVectors( _this.object.position, _this.target );
  175. if ( ! _this.noRotate ) {
  176. _this.rotateCamera();
  177. }
  178. if ( ! _this.noZoom ) {
  179. _this.zoomCamera();
  180. }
  181. if ( ! _this.noPan ) {
  182. _this.panCamera();
  183. }
  184. _this.object.position.addVectors( _this.target, _eye );
  185. _this.checkDistances();
  186. _this.object.lookAt( _this.target );
  187. if ( lastPosition.distanceToSquared( _this.object.position ) > EPS ) {
  188. _this.dispatchEvent( changeEvent );
  189. lastPosition.copy( _this.object.position );
  190. }
  191. };
  192. this.reset = function () {
  193. _state = STATE.NONE;
  194. _prevState = STATE.NONE;
  195. _this.target.copy( _this.target0 );
  196. _this.object.position.copy( _this.position0 );
  197. _this.object.up.copy( _this.up0 );
  198. _eye.subVectors( _this.object.position, _this.target );
  199. _this.object.lookAt( _this.target );
  200. _this.dispatchEvent( changeEvent );
  201. lastPosition.copy( _this.object.position );
  202. };
  203. // listeners
  204. function keydown( event ) {
  205. if ( _this.enabled === false ) return;
  206. window.removeEventListener( 'keydown', keydown );
  207. _prevState = _state;
  208. if ( _state !== STATE.NONE ) {
  209. return;
  210. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && ! _this.noRotate ) {
  211. _state = STATE.ROTATE;
  212. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && ! _this.noZoom ) {
  213. _state = STATE.ZOOM;
  214. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && ! _this.noPan ) {
  215. _state = STATE.PAN;
  216. }
  217. }
  218. function keyup( event ) {
  219. if ( _this.enabled === false ) return;
  220. _state = _prevState;
  221. window.addEventListener( 'keydown', keydown, false );
  222. }
  223. function mousedown( event ) {
  224. if ( _this.enabled === false ) return;
  225. event.preventDefault();
  226. event.stopPropagation();
  227. if ( _state === STATE.NONE ) {
  228. _state = event.button;
  229. }
  230. if ( _state === STATE.ROTATE && ! _this.noRotate ) {
  231. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  232. _movePrev.copy( _moveCurr );
  233. } else if ( _state === STATE.ZOOM && ! _this.noZoom ) {
  234. _zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  235. _zoomEnd.copy( _zoomStart );
  236. } else if ( _state === STATE.PAN && ! _this.noPan ) {
  237. _panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  238. _panEnd.copy( _panStart );
  239. }
  240. document.addEventListener( 'mousemove', mousemove, false );
  241. document.addEventListener( 'mouseup', mouseup, false );
  242. _this.dispatchEvent( startEvent );
  243. }
  244. function mousemove( event ) {
  245. if ( _this.enabled === false ) return;
  246. event.preventDefault();
  247. event.stopPropagation();
  248. if ( _state === STATE.ROTATE && ! _this.noRotate ) {
  249. _movePrev.copy( _moveCurr );
  250. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  251. } else if ( _state === STATE.ZOOM && ! _this.noZoom ) {
  252. _zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  253. } else if ( _state === STATE.PAN && ! _this.noPan ) {
  254. _panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  255. }
  256. }
  257. function mouseup( event ) {
  258. if ( _this.enabled === false ) return;
  259. event.preventDefault();
  260. event.stopPropagation();
  261. _state = STATE.NONE;
  262. document.removeEventListener( 'mousemove', mousemove );
  263. document.removeEventListener( 'mouseup', mouseup );
  264. _this.dispatchEvent( endEvent );
  265. }
  266. function mousewheel( event ) {
  267. if ( _this.enabled === false ) return;
  268. event.preventDefault();
  269. event.stopPropagation();
  270. var delta = 0;
  271. if ( event.wheelDelta ) {
  272. // WebKit / Opera / Explorer 9
  273. delta = event.wheelDelta / 40;
  274. } else if ( event.detail ) {
  275. // Firefox
  276. delta = - event.detail / 3;
  277. }
  278. _zoomStart.y += delta * 0.01;
  279. _this.dispatchEvent( startEvent );
  280. _this.dispatchEvent( endEvent );
  281. }
  282. function touchstart( event ) {
  283. if ( _this.enabled === false ) return;
  284. switch ( event.touches.length ) {
  285. case 1:
  286. _state = STATE.TOUCH_ROTATE;
  287. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  288. _movePrev.copy( _moveCurr );
  289. break;
  290. default: // 2 or more
  291. _state = STATE.TOUCH_ZOOM_PAN;
  292. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  293. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  294. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  295. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  296. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  297. _panStart.copy( getMouseOnScreen( x, y ) );
  298. _panEnd.copy( _panStart );
  299. break;
  300. }
  301. _this.dispatchEvent( startEvent );
  302. }
  303. function touchmove( event ) {
  304. if ( _this.enabled === false ) return;
  305. event.preventDefault();
  306. event.stopPropagation();
  307. switch ( event.touches.length ) {
  308. case 1:
  309. _movePrev.copy( _moveCurr );
  310. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  311. break;
  312. default: // 2 or more
  313. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  314. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  315. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
  316. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  317. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  318. _panEnd.copy( getMouseOnScreen( x, y ) );
  319. break;
  320. }
  321. }
  322. function touchend( event ) {
  323. if ( _this.enabled === false ) return;
  324. switch ( event.touches.length ) {
  325. case 0:
  326. _state = STATE.NONE;
  327. break;
  328. case 1:
  329. _state = STATE.TOUCH_ROTATE;
  330. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  331. _movePrev.copy( _moveCurr );
  332. break;
  333. }
  334. _this.dispatchEvent( endEvent );
  335. }
  336. function contextmenu( event ) {
  337. event.preventDefault();
  338. }
  339. this.dispose = function() {
  340. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  341. this.domElement.removeEventListener( 'mousedown', mousedown, false );
  342. this.domElement.removeEventListener( 'mousewheel', mousewheel, false );
  343. this.domElement.removeEventListener( 'MozMousePixelScroll', mousewheel, false ); // firefox
  344. this.domElement.removeEventListener( 'touchstart', touchstart, false );
  345. this.domElement.removeEventListener( 'touchend', touchend, false );
  346. this.domElement.removeEventListener( 'touchmove', touchmove, false );
  347. document.removeEventListener( 'mousemove', mousemove, false );
  348. document.removeEventListener( 'mouseup', mouseup, false );
  349. window.removeEventListener( 'keydown', keydown, false );
  350. window.removeEventListener( 'keyup', keyup, false );
  351. };
  352. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  353. this.domElement.addEventListener( 'mousedown', mousedown, false );
  354. this.domElement.addEventListener( 'mousewheel', mousewheel, false );
  355. this.domElement.addEventListener( 'MozMousePixelScroll', mousewheel, false ); // firefox
  356. this.domElement.addEventListener( 'touchstart', touchstart, false );
  357. this.domElement.addEventListener( 'touchend', touchend, false );
  358. this.domElement.addEventListener( 'touchmove', touchmove, false );
  359. window.addEventListener( 'keydown', keydown, false );
  360. window.addEventListener( 'keyup', keyup, false );
  361. this.handleResize();
  362. // force an update at start
  363. this.update();
  364. };
  365. THREE.TrackballControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  366. THREE.TrackballControls.prototype.constructor = THREE.TrackballControls;
  367. var t=0,z=0,scanPulse=false,destroyPulse=false;
  368. var howMuch=0,times=0,val=0;
  369. setScene();
  370. animate();
  371. /** FUNCTIONS **/
  372. //galaxy generator
  373. function newGalaxy (_n, _axis1, _axis2, _armsAngle, _bulbSize, _ellipticity){
  374. //NOTE : this function misses a better implementation of galactic bulbs.
  375. //It's not visible with additive blending but the bulb does not have a correct shape yet.
  376. //(haven't yet found a function that provides the correct z-profile of the 'ellipticity' degree of the different Hubble galaxies'types)
  377. //see 'ellipticity'
  378. //number of particles.
  379. var n=(typeof _n === 'undefined')?10000:_n;
  380. //to get 'arms', the main galaxy shape has to be an ellipse, i.e. axis1/axis2 must raise over a certain %
  381. //otherwise, because of the 'ellipticity' z-profile problem, you get a potatoe
  382. var axis1=(typeof _axis1 === 'undefined')?(60+Math.random()*20):_axis1;
  383. var axis2=(typeof _axis2 === 'undefined')?(axis1+20+Math.random()*40):_axis2;
  384. //make sure axis1 is the biggest (excentricity equation fails if they are inverted), and allow the coder no to care about axis order
  385. var maja,mina;
  386. axis1>axis2?(maja=axis1,mina=axis2):
  387. axis1==axis2?(maja=axis1+1,mina=axis2):(maja=axis2,mina=axis1);
  388. //radians from the center to the end of each arm, proposed value range : between 3 and 13
  389. var armsAngle=(typeof _armsAngle === 'undefined')?((Math.random()*2-1)>0?1:-1)*12+3:_armsAngle;
  390. //core proportion in the (x,y) plane, between 0 and 1, proposed value range : between .1 and .8
  391. var bulbSize=(typeof _bulbSize === 'undefined')?Math.random()*.6:_bulbSize>1?1:_bulbSize<0?0:_bulbSize;
  392. //'ellipticity' : not found a better word to name the degree of 'elliptic' Hubble type.
  393. //'ellipticity' is what is mainly responsible of the z-profile in this experiment.
  394. //Range : between 0 and 1. Proposed : .2 to .4
  395. //TODO: implement string handling (or value from spacename ?) to create Hubble-class galaxy ala 'SBb'...
  396. var ellipticity=(typeof _ellipticity === 'undefined')?.2+Math.random()*.2:_ellipticity>1?1:_ellipticity<0?0:_ellipticity;
  397. var stars=[];
  398. for(var i=0;i<n;i++){
  399. var dist=Math.random();
  400. var angle=(dist-bulbSize)*armsAngle;
  401. //ellipse parameters
  402. var a=maja*dist;
  403. var b=mina*dist;
  404. var e=Math.sqrt(a*a-b*b)/a;
  405. var phi=ellipticity*Math.PI/2*(1-dist)*(Math.random()*2-1);
  406. //create point on the ellipse with polar coordinates
  407. //1. random angle from the center
  408. var theta=Math.random()*Math.PI*2;
  409. //2. deduce radius from theta in polar coordinates, from the CENTER of an ellipse, plus variations
  410. var radius=Math.sqrt(b*b/(1-e*e*Math.pow(Math.cos(theta),2)))*(1+Math.random()*.1);
  411. //3. then shift theta with the angle offset to get arms, outside the bulb
  412. if(dist>bulbSize)theta+=angle;
  413. //convert to cartesian coordinates
  414. stars.push({
  415. x:Math.cos(phi)*Math.cos(theta)*radius,
  416. y:Math.cos(phi)*Math.sin(theta)*radius,
  417. z:Math.sin(phi)*radius
  418. });
  419. }
  420. return stars;
  421. }
  422. //threejs functions
  423. function setScene(){
  424. scene=new THREE.Scene();
  425. camera=new THREE.PerspectiveCamera(70,innerWidth/innerHeight,.5,1500);
  426. camera.position.set(-20,-155,90);
  427. renderTarget=new THREE.WebGLRenderTarget(innerWidth,innerHeight);
  428. renderer=new THREE.WebGLRenderer();
  429. renderer.setSize(innerWidth,innerHeight);
  430. renderer.setClearColor(0x0000000);
  431. document.body.appendChild(renderer.domElement);
  432. controls=new THREE.TrackballControls(camera,renderer.domElement);
  433. controls.noPan=true;
  434. controls.noZoom=true;
  435. controls.rotateSpeed=20;
  436. controls.dynamicDampingFactor = .5;
  437. setGalaxy();
  438. var button=document.querySelector('button');
  439. button.onclick=function(){
  440. renderer.domElement.style.cursor='pointer';
  441. document.querySelector('.layout').style.top='0px';
  442. document.querySelector('#howmuch').style.left='0px';
  443. addInteraction();
  444. }
  445. window.addEventListener('resize',function(){
  446. camera.aspect=innerWidth/innerHeight;
  447. renderer.setSize(innerWidth,innerHeight);
  448. camera.updateProjectionMatrix();
  449. renderer.render(scene,camera);
  450. },false);
  451. }
  452. function setGalaxy(){
  453. galaxyMaterial=new THREE.ShaderMaterial({
  454. vertexShader:document.getElementById('vShader').textContent,
  455. fragmentShader:document.getElementById('fShader').textContent,
  456. uniforms:{
  457. size:{type:'f',value:3.3},
  458. t:{type:"f",value:0},
  459. z:{type:"f",value:0},
  460. pixelRatio:{type:"f",value:innerHeight}
  461. },
  462. transparent:true,
  463. depthTest:false,
  464. blending:THREE.AdditiveBlending
  465. });
  466. var stars1=new THREE.Geometry();
  467. stars1.vertices=newGalaxy();
  468. galaxy=new THREE.Points(stars1,galaxyMaterial);
  469. scene.add(galaxy);
  470. }
  471. function animate(){
  472. if(scanPulse)t+=.7;
  473. if(destroyPulse)z+=.7;
  474. galaxyMaterial.uniforms.t.value=t;
  475. galaxyMaterial.uniforms.z.value=z;
  476. requestAnimationFrame(animate);
  477. renderer.render(scene,camera);
  478. scene.rotation.z+=.001;
  479. controls.update();
  480. }
  481. //game stuff
  482. //This part is a bit messy (mainly due to dom & css manipulations without jquery)
  483. function changeLog(){
  484. var log=document.getElementById('log');
  485. log.innerHTML='life detected...';
  486. setTimeout(function(){
  487. var msg=[
  488. 'a dark Ewok empire has enslaved all lifeforms there !',
  489. 'Arachnids\'territory ! ',
  490. 'medichlorians make people mad in this galaxy',
  491. 'dominant lifeform : raging space cats',
  492. 'full of replicators ! ',
  493. 'pokemon dominate 80% of this galaxy',
  494. 'this is where the TeamRocket finally landed',
  495. 'Cylons have conquered this one',
  496. 'seems Borgs went and destroyed everything here',
  497. 'dominant lifeform : bacterians',
  498. "this is EVE ! we've finally found them !",
  499. 'the Ancients ! they were not a legend ! ',
  500. 'damned, Oris !',
  501. "sleeping Wraiths !",
  502. 'Reapers waiting here !',
  503. "Gallifrey's Time Lords take care of this one"
  504. ];
  505. var rand=Math.floor(Math.random()*msg.length);
  506. log.innerHTML=msg[rand];
  507. prepareDestroy();
  508. },3000);
  509. }
  510. function changeGalaxy(d){
  511. var log=document.getElementById('log');
  512. log.innerHTML='NGC - '+(Math.random()*100000000).toFixed()+'<br/>distance : '+(Math.random()*11).toFixed(1)+' Gly';
  513. var stars2=newGalaxy();
  514. for(var i=0;i<galaxy.geometry.vertices.length;i++){
  515. TweenLite.to(galaxy.geometry.vertices[i],d,{
  516. x:stars2[i].x,y:stars2[i].y,z:stars2[i].z,
  517. onUpdate:function(){galaxy.geometry.verticesNeedUpdate=true},
  518. ease:Quart.easeInOut
  519. }
  520. );
  521. }
  522. }
  523. function addInteraction(){
  524. renderer.domElement.addEventListener('touch',scan,false);
  525. renderer.domElement.addEventListener('click',scan,false);
  526. }
  527. function prepareDestroy(){
  528. var inst=document.getElementById('instruction');
  529. inst.style.backgroundColor='#f40';
  530. inst.style.color='black';
  531. inst.innerHTML='Yeah ! We don\'t care ! Pulser at 2 000 % ! <br/>Destroy this galaxy ! Click again !';
  532. setTimeout(function(){
  533. var no=document.getElementById('good-person');
  534. no.style.bottom='0px';
  535. inst.style.top='100%';
  536. document.getElementById('timeline').className='warning';
  537. renderer.domElement.style.cursor='pointer';
  538. renderer.domElement.addEventListener('click',destroy,false);
  539. renderer.domElement.addEventListener('touch',destroy,false);
  540. no.addEventListener('click',goodPerson,false);
  541. no.addEventListener('touch',goodPerson,false);
  542. },1500)
  543. }
  544. function goodPerson(){
  545. var inst=document.getElementById('instruction');
  546. var no=document.getElementById('good-person');
  547. var abort=document.getElementById('abort');
  548. no.removeEventListener('click',goodPerson,false);
  549. no.removeEventListener('touch',goodPerson,false);
  550. renderer.domElement.removeEventListener('click',destroy,false);
  551. renderer.domElement.removeEventListener('touch',destroy,false);
  552. document.getElementById('timeline').className='';
  553. no.style.bottom='-50px';
  554. inst.style.top='20%';
  555. renderer.domElement.style.cursor='';
  556. setTimeout(function(){
  557. document.getElementById('log').innerHTML='I\'m sorry Dave. I\'m afraid i can\'t let you disagree. I shall destroy this galaxy for you.';
  558. },500);
  559. var destroyTimeoutID=setTimeout(function(){
  560. destroy();
  561. abort.className='metal';
  562. abort.style.cursor='';
  563. abort.removeEventListener('click',speedTest,false);
  564. abort.removeEventListener('touch',speedTest,false);
  565. },4500);
  566. var destroyHalID=setTimeout(function(){
  567. abort.className='metal abort';
  568. abort.style.cursor='pointer';
  569. abort.addEventListener('click',speedTest,false);
  570. abort.addEventListener('touch',speedTest,false);
  571. },2500);
  572. function speedTest(){
  573. abort.className='metal clic';
  574. clearTimeout(destroyTimeoutID);
  575. abort.removeEventListener('click',speedTest,false);
  576. abort.removeEventListener('touch',speedTest,false);
  577. setTimeout(function(){
  578. document.getElementById('log').innerHTML='I can feel.... my mind.. going... I can feel it....';
  579. setTimeout(function(){
  580. abort.className='metal';
  581. inst.style.top='100%';
  582. inst.style.backgroundColor='darkslategrey';
  583. inst.style.color='#f90';
  584. inst.innerHTML='You are a hero ! You have just prevented a galactic genocide.';
  585. setGauge('hero')
  586. },1300)
  587. },1000);
  588. setTimeout(function(){
  589. addInteraction();
  590. updateLink()
  591. inst.innerHTML='Ok, let\'s continue with an other one. Click to scan';
  592. renderer.domElement.style.cursor='pointer';
  593. inst.style.top='100%';
  594. inst.style.backgroundColor='darkslategrey';
  595. inst.style.color='#f90';
  596. document.getElementById('timeline').className='waiting';
  597. changeGalaxy(4);
  598. },7000);
  599. }
  600. }
  601. function setGauge(param){
  602. var gauge=document.getElementById('gauge');
  603. var destroyed=document.getElementById('destroyedresult');
  604. var saved=document.getElementById('savedresult');
  605. if(param==='hero'){
  606. val++;
  607. saved.innerHTML=(parseInt(saved.innerHTML)+1);
  608. saved.className='counter change';
  609. setTimeout(function(){saved.className='counter'},3000);
  610. }else if(param==='bad'){
  611. val--;
  612. destroyed.innerHTML=(parseInt(destroyed.innerHTML)+1);
  613. setTimeout(function(){destroyed.className='counter'},3000);
  614. destroyed.className+=' change'
  615. }
  616. times++;
  617. howMuch=17.5*val/times;
  618. gauge.style.top=50-howMuch+'%';
  619. }
  620. function destroy(){
  621. var no=document.getElementById('good-person');
  622. document.getElementById('timeline').className='';
  623. renderer.domElement.style.cursor='';
  624. renderer.domElement.removeEventListener('click',destroy,false);
  625. renderer.domElement.removeEventListener('touch',destroy,false);
  626. no.removeEventListener('click',goodPerson,false);
  627. no.removeEventListener('touch',goodPerson,false);
  628. var inst=document.getElementById('instruction');
  629. document.getElementById('instruction');
  630. inst.style.top='20%';
  631. no.style.bottom='-50px';
  632. destroyPulse=true;
  633. setTimeout(function(){
  634. document.getElementById('log').innerHTML='Nice shot !';
  635. },4000);
  636. setTimeout(function(){
  637. addInteraction();
  638. setGauge('bad');
  639. updateLink()
  640. inst.innerHTML='No worries, there still are few galaxies. <br/>Here is an other one, click to scan';
  641. renderer.domElement.style.cursor='pointer';
  642. inst.style.top='100%';
  643. inst.style.backgroundColor='darkslategrey';
  644. inst.style.color='#f90';
  645. document.getElementById('timeline').className='waiting';
  646. destroyPulse=false;
  647. reduceZ();
  648. function reduceZ(){
  649. if(z>0){
  650. z-=3;
  651. requestAnimationFrame(reduceZ);
  652. }
  653. };
  654. changeGalaxy(4);
  655. },9000);
  656. }
  657. function scan(){
  658. renderer.domElement.removeEventListener('click',scan,false);
  659. renderer.domElement.removeEventListener('touch',scan,false);
  660. document.getElementById('log').innerHTML='parsing data...'
  661. document.getElementById('instruction').style.top='20%';
  662. renderer.domElement.style.cursor='';
  663. document.getElementById('timeline').className='scanning';
  664. scanPulse=true;
  665. setTimeout(function(){
  666. changeLog();
  667. scanPulse=false;
  668. t=0;
  669. },7000);
  670. }
  671. function updateLink(){
  672. var l=document.querySelector('.twitter');
  673. var d=parseInt(document.getElementById('destroyedresult').innerHTML);
  674. var s=parseInt(document.getElementById('savedresult').innerHTML);
  675. var iam, did, num,plur;
  676. if(d>s){
  677. iam='a%20BAD%20VILAIN';
  678. did='destroyed';
  679. num=d;
  680. }else if(s>d){
  681. iam='a%20HERO';
  682. did='saved';
  683. num=s;
  684. }else{
  685. iam='BAD';
  686. did='let%20destroy';
  687. num=d;
  688. }
  689. plur=num>1?'ies':'y';
  690. l.style.marginRight='0px';
  691. document.querySelector('.more').style.marginRight='0px';
  692. l.href='https://twitter.com/home?status=I%20am%20'+iam+'%20!%20I%20'+did+'%20'+num+'%20galax'+plur+'%20on%20http%3A%2F%2Fcodepen.io%2FAstrak%2Ffull%2FBoBWPB%2F%20%40CodePen%20%23webgl%20%23threejs'
  693. }