Sparks.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * @author zz85 (http://github.com/zz85 http://www.lab4games.net/zz85/blog)
  3. *
  4. * a simple to use javascript 3d particles system inspired by FliNT and Stardust
  5. * created with TWEEN.js and THREE.js
  6. *
  7. * for feature requests or bugs, please visit https://github.com/zz85/sparks.js
  8. *
  9. * licensed under the MIT license
  10. */
  11. var SPARKS = {};
  12. /********************************
  13. * Emitter Class
  14. *
  15. * Creates and Manages Particles
  16. *********************************/
  17. SPARKS.Emitter = function ( counter ) {
  18. this._counter = counter ? counter : new SPARKS.SteadyCounter( 10 ); // provides number of particles to produce
  19. this._particles = [];
  20. this._initializers = []; // use for creation of particles
  21. this._actions = []; // uses action to update particles
  22. this._activities = []; // not supported yet
  23. this._handlers = [];
  24. this.callbacks = {};
  25. };
  26. SPARKS.Emitter.prototype = {
  27. _TIMESTEP: 15,
  28. _timer: null,
  29. _lastTime: null,
  30. _timerStep: 10,
  31. _velocityVerlet: true,
  32. // run its built in timer / stepping
  33. start: function() {
  34. this._lastTime = Date.now();
  35. this._timer = setTimeout( this.step, this._timerStep, this );
  36. this._isRunning = true;
  37. },
  38. stop: function() {
  39. this._isRunning = false;
  40. clearTimeout( this._timer );
  41. },
  42. isRunning: function() {
  43. return this._isRunning & true;
  44. },
  45. // Step gets called upon by the engine
  46. // but attempts to call update() on a regular basics
  47. // This method is also described in http://gameclosure.com/2011/04/11/deterministic-delta-tee-in-js-games/
  48. step: function( emitter ) {
  49. var time = Date.now();
  50. var elapsed = time - emitter._lastTime;
  51. if ( ! this._velocityVerlet ) {
  52. // if elapsed is way higher than time step, (usually after switching tabs, or excution cached in ff)
  53. // we will drop cycles. perhaps set to a limit of 10 or something?
  54. var maxBlock = emitter._TIMESTEP * 20;
  55. if ( elapsed >= maxBlock ) {
  56. //console.log('warning: sparks.js is fast fowarding engine, skipping steps', elapsed / emitter._TIMESTEP);
  57. //emitter.update( (elapsed - maxBlock) / 1000);
  58. elapsed = maxBlock;
  59. }
  60. while ( elapsed >= emitter._TIMESTEP ) {
  61. emitter.update( emitter._TIMESTEP / 1000 );
  62. elapsed -= emitter._TIMESTEP;
  63. }
  64. emitter._lastTime = time - elapsed;
  65. } else {
  66. emitter.update( elapsed / 1000 );
  67. emitter._lastTime = time;
  68. }
  69. if ( emitter._isRunning )
  70. setTimeout( emitter.step, emitter._timerStep, emitter );
  71. },
  72. // Update particle engine in seconds, not milliseconds
  73. update: function( time ) {
  74. var i, j;
  75. var len = this._counter.updateEmitter( this, time );
  76. // Create particles
  77. for ( i = 0; i < len; i ++ ) {
  78. this.createParticle();
  79. }
  80. // Update activities
  81. len = this._activities.length;
  82. for ( i = 0; i < len; i ++ ) {
  83. this._activities[ i ].update( this, time );
  84. }
  85. len = this._actions.length;
  86. var particle;
  87. var action;
  88. var len2 = this._particles.length;
  89. for ( j = 0; j < len; j ++ ) {
  90. action = this._actions[ j ];
  91. for ( i = 0; i < len2; ++ i ) {
  92. particle = this._particles[ i ];
  93. action.update( this, particle, time );
  94. }
  95. }
  96. // remove dead particles
  97. for ( i = len2; i --; ) {
  98. particle = this._particles[ i ];
  99. if ( particle.isDead ) {
  100. //particle =
  101. this._particles.splice( i, 1 );
  102. this.dispatchEvent( "dead", particle );
  103. SPARKS.VectorPool.release( particle.position ); //
  104. SPARKS.VectorPool.release( particle.velocity );
  105. } else {
  106. this.dispatchEvent( "updated", particle );
  107. }
  108. }
  109. this.dispatchEvent( "loopUpdated" );
  110. },
  111. createParticle: function() {
  112. var particle = new SPARKS.Particle();
  113. // In future, use a Particle Factory
  114. var len = this._initializers.length, i;
  115. for ( i = 0; i < len; i ++ ) {
  116. this._initializers[ i ].initialize( this, particle );
  117. }
  118. this._particles.push( particle );
  119. this.dispatchEvent( "created", particle ); // ParticleCreated
  120. return particle;
  121. },
  122. addInitializer: function ( initializer ) {
  123. this._initializers.push( initializer );
  124. },
  125. addAction: function ( action ) {
  126. this._actions.push( action );
  127. },
  128. removeInitializer: function ( initializer ) {
  129. var index = this._initializers.indexOf( initializer );
  130. if ( index > - 1 ) {
  131. this._initializers.splice( index, 1 );
  132. }
  133. },
  134. removeAction: function ( action ) {
  135. var index = this._actions.indexOf( action );
  136. if ( index > - 1 ) {
  137. this._actions.splice( index, 1 );
  138. }
  139. //console.log('removeAction', index, this._actions);
  140. },
  141. addCallback: function( name, callback ) {
  142. this.callbacks[ name ] = callback;
  143. },
  144. dispatchEvent: function( name, args ) {
  145. var callback = this.callbacks[ name ];
  146. if ( callback ) {
  147. callback( args );
  148. }
  149. }
  150. };
  151. /*
  152. * Constant Names for
  153. * Events called by emitter.dispatchEvent()
  154. *
  155. */
  156. SPARKS.EVENT_PARTICLE_CREATED = "created";
  157. SPARKS.EVENT_PARTICLE_UPDATED = "updated";
  158. SPARKS.EVENT_PARTICLE_DEAD = "dead";
  159. SPARKS.EVENT_LOOP_UPDATED = "loopUpdated";
  160. /*
  161. * Steady Counter attempts to produces a particle rate steadily
  162. *
  163. */
  164. // Number of particles per seconds
  165. SPARKS.SteadyCounter = function( rate ) {
  166. this.rate = rate;
  167. // we use a shortfall counter to make up for slow emitters
  168. this.leftover = 0;
  169. };
  170. SPARKS.SteadyCounter.prototype.updateEmitter = function( emitter, time ) {
  171. var targetRelease = time * this.rate + this.leftover;
  172. var actualRelease = Math.floor( targetRelease );
  173. this.leftover = targetRelease - actualRelease;
  174. return actualRelease;
  175. };
  176. /*
  177. * Shot Counter produces specified particles
  178. * on a single impluse or burst
  179. */
  180. SPARKS.ShotCounter = function( particles ) {
  181. this.particles = particles;
  182. this.used = false;
  183. };
  184. SPARKS.ShotCounter.prototype.updateEmitter = function( emitter, time ) {
  185. if ( this.used ) {
  186. return 0;
  187. } else {
  188. this.used = true;
  189. }
  190. return this.particles;
  191. };
  192. /********************************
  193. * Particle Class
  194. *
  195. * Represents a single particle
  196. *********************************/
  197. SPARKS.Particle = function() {
  198. /**
  199. * The lifetime of the particle, in seconds.
  200. */
  201. this.lifetime = 0;
  202. /**
  203. * The age of the particle, in seconds.
  204. */
  205. this.age = 0;
  206. /**
  207. * The energy of the particle.
  208. */
  209. this.energy = 1;
  210. /**
  211. * Whether the particle is dead and should be removed from the stage.
  212. */
  213. this.isDead = false;
  214. this.target = null; // tag
  215. /**
  216. * For 3D
  217. */
  218. this.position = SPARKS.VectorPool.get().set( 0, 0, 0 ); //new THREE.Vector3( 0, 0, 0 );
  219. this.velocity = SPARKS.VectorPool.get().set( 0, 0, 0 ); //new THREE.Vector3( 0, 0, 0 );
  220. this._oldvelocity = SPARKS.VectorPool.get().set( 0, 0, 0 );
  221. // rotation vec3
  222. // angVelocity vec3
  223. // faceAxis vec3
  224. };
  225. /********************************
  226. * Action Classes
  227. *
  228. * An abstract class which have
  229. * update function
  230. *********************************/
  231. SPARKS.Action = function() {
  232. this._priority = 0;
  233. };
  234. SPARKS.Age = function( easing ) {
  235. this._easing = ( easing == null ) ? TWEEN.Easing.Linear.None : easing;
  236. };
  237. SPARKS.Age.prototype.update = function ( emitter, particle, time ) {
  238. particle.age += time;
  239. if ( particle.age >= particle.lifetime ) {
  240. particle.energy = 0;
  241. particle.isDead = true;
  242. } else {
  243. var t = this._easing( particle.age / particle.lifetime );
  244. particle.energy = - 1 * t + 1;
  245. }
  246. };
  247. /*
  248. // Mark particle as dead when particle's < 0
  249. SPARKS.Death = function(easing) {
  250. this._easing = (easing == null) ? TWEEN.Linear.None : easing;
  251. };
  252. SPARKS.Death.prototype.update = function (emitter, particle, time) {
  253. if (particle.life <= 0) {
  254. particle.isDead = true;
  255. }
  256. };
  257. */
  258. SPARKS.Move = function() {
  259. };
  260. SPARKS.Move.prototype.update = function( emitter, particle, time ) {
  261. // attempt verlet velocity updating.
  262. var p = particle.position;
  263. var v = particle.velocity;
  264. var old = particle._oldvelocity;
  265. if ( this._velocityVerlet ) {
  266. p.x += ( v.x + old.x ) * 0.5 * time;
  267. p.y += ( v.y + old.y ) * 0.5 * time;
  268. p.z += ( v.z + old.z ) * 0.5 * time;
  269. } else {
  270. p.x += v.x * time;
  271. p.y += v.y * time;
  272. p.z += v.z * time;
  273. }
  274. // OldVel = Vel;
  275. // Vel = Vel + Accel * dt;
  276. // Pos = Pos + (vel + Vel + Accel * dt) * 0.5 * dt;
  277. };
  278. /* Marks particles found in specified zone dead */
  279. SPARKS.DeathZone = function( zone ) {
  280. this.zone = zone;
  281. };
  282. SPARKS.DeathZone.prototype.update = function( emitter, particle, time ) {
  283. if ( this.zone.contains( particle.position ) ) {
  284. particle.isDead = true;
  285. }
  286. };
  287. /*
  288. * SPARKS.ActionZone applies an action when particle is found in zone
  289. */
  290. SPARKS.ActionZone = function( action, zone ) {
  291. this.action = action;
  292. this.zone = zone;
  293. };
  294. SPARKS.ActionZone.prototype.update = function( emitter, particle, time ) {
  295. if ( this.zone.contains( particle.position ) ) {
  296. this.action.update( emitter, particle, time );
  297. }
  298. };
  299. /*
  300. * Accelerate action affects velocity in specified 3d direction
  301. */
  302. SPARKS.Accelerate = function( x, y, z ) {
  303. if ( x instanceof THREE.Vector3 ) {
  304. this.acceleration = x;
  305. return;
  306. }
  307. this.acceleration = new THREE.Vector3( x, y, z );
  308. };
  309. SPARKS.Accelerate.prototype.update = function( emitter, particle, time ) {
  310. var acc = this.acceleration;
  311. var v = particle.velocity;
  312. particle._oldvelocity.set( v.x, v.y, v.z );
  313. v.x += acc.x * time;
  314. v.y += acc.y * time;
  315. v.z += acc.z * time;
  316. };
  317. /*
  318. * Accelerate Factor accelerate based on a factor of particle's velocity.
  319. */
  320. SPARKS.AccelerateFactor = function( factor ) {
  321. this.factor = factor;
  322. };
  323. SPARKS.AccelerateFactor.prototype.update = function( emitter, particle, time ) {
  324. var factor = this.factor;
  325. var v = particle.velocity;
  326. var len = v.length();
  327. var adjFactor;
  328. if ( len > 0 ) {
  329. adjFactor = factor * time / len;
  330. adjFactor += 1;
  331. v.multiplyScalar( adjFactor );
  332. // v.x *= adjFactor;
  333. // v.y *= adjFactor;
  334. // v.z *= adjFactor;
  335. }
  336. };
  337. /*
  338. AccelerateNormal
  339. * AccelerateVelocity affects velocity based on its velocity direction
  340. */
  341. SPARKS.AccelerateVelocity = function( factor ) {
  342. this.factor = factor;
  343. };
  344. SPARKS.AccelerateVelocity.prototype.update = function( emitter, particle, time ) {
  345. var factor = this.factor;
  346. var v = particle.velocity;
  347. v.z += - v.x * factor;
  348. v.y += v.z * factor;
  349. v.x += v.y * factor;
  350. };
  351. /* Set the max ammount of x,y,z drift movements in a second */
  352. SPARKS.RandomDrift = function( x, y, z ) {
  353. if ( x instanceof THREE.Vector3 ) {
  354. this.drift = x;
  355. return;
  356. }
  357. this.drift = new THREE.Vector3( x, y, z );
  358. };
  359. SPARKS.RandomDrift.prototype.update = function( emitter, particle, time ) {
  360. var drift = this.drift;
  361. var v = particle.velocity;
  362. v.x += ( Math.random() - 0.5 ) * drift.x * time;
  363. v.y += ( Math.random() - 0.5 ) * drift.y * time;
  364. v.z += ( Math.random() - 0.5 ) * drift.z * time;
  365. };
  366. /********************************
  367. * Zone Classes
  368. *
  369. * An abstract classes which have
  370. * getLocation() function
  371. *********************************/
  372. SPARKS.Zone = function() {
  373. };
  374. // TODO, contains() for Zone
  375. SPARKS.PointZone = function( pos ) {
  376. this.pos = pos;
  377. };
  378. SPARKS.PointZone.prototype.getLocation = function() {
  379. return this.pos;
  380. };
  381. SPARKS.PointZone = function( pos ) {
  382. this.pos = pos;
  383. };
  384. SPARKS.PointZone.prototype.getLocation = function() {
  385. return this.pos;
  386. };
  387. SPARKS.LineZone = function( start, end ) {
  388. this.start = start;
  389. this.end = end;
  390. this._length = end.clone().sub( start );
  391. };
  392. SPARKS.LineZone.prototype.getLocation = function() {
  393. var len = this._length.clone();
  394. len.multiplyScalar( Math.random() );
  395. return len.add( this.start );
  396. };
  397. // Basically a RectangleZone
  398. SPARKS.ParallelogramZone = function( corner, side1, side2 ) {
  399. this.corner = corner;
  400. this.side1 = side1;
  401. this.side2 = side2;
  402. };
  403. SPARKS.ParallelogramZone.prototype.getLocation = function() {
  404. var d1 = this.side1.clone().multiplyScalar( Math.random() );
  405. var d2 = this.side2.clone().multiplyScalar( Math.random() );
  406. d1.add( d2 );
  407. return d1.add( this.corner );
  408. };
  409. SPARKS.CubeZone = function( position, x, y, z ) {
  410. this.position = position;
  411. this.x = x;
  412. this.y = y;
  413. this.z = z;
  414. };
  415. SPARKS.CubeZone.prototype.getLocation = function() {
  416. //TODO use pool?
  417. var location = this.position.clone();
  418. location.x += Math.random() * this.x;
  419. location.y += Math.random() * this.y;
  420. location.z += Math.random() * this.z;
  421. return location;
  422. };
  423. SPARKS.CubeZone.prototype.contains = function( position ) {
  424. var startX = this.position.x;
  425. var startY = this.position.y;
  426. var startZ = this.position.z;
  427. var x = this.x; // width
  428. var y = this.y; // depth
  429. var z = this.z; // height
  430. if ( x < 0 ) {
  431. startX += x;
  432. x = Math.abs( x );
  433. }
  434. if ( y < 0 ) {
  435. startY += y;
  436. y = Math.abs( y );
  437. }
  438. if ( z < 0 ) {
  439. startZ += z;
  440. z = Math.abs( z );
  441. }
  442. var diffX = position.x - startX;
  443. var diffY = position.y - startY;
  444. var diffZ = position.z - startZ;
  445. if ( ( diffX > 0 ) && ( diffX < x ) &&
  446. ( diffY > 0 ) && ( diffY < y ) &&
  447. ( diffZ > 0 ) && ( diffZ < z ) ) {
  448. return true;
  449. }
  450. return false;
  451. };
  452. /**
  453. * The constructor creates a DiscZone 3D zone.
  454. *
  455. * @param centre The point at the center of the disc.
  456. * @param normal A vector normal to the disc.
  457. * @param outerRadius The outer radius of the disc.
  458. * @param innerRadius The inner radius of the disc. This defines the hole
  459. * in the center of the disc. If set to zero, there is no hole.
  460. */
  461. /*
  462. // BUGGY!!
  463. SPARKS.DiscZone = function(center, radiusNormal, outerRadius, innerRadius) {
  464. this.center = center;
  465. this.radiusNormal = radiusNormal;
  466. this.outerRadius = (outerRadius==undefined) ? 0 : outerRadius;
  467. this.innerRadius = (innerRadius==undefined) ? 0 : innerRadius;
  468. };
  469. SPARKS.DiscZone.prototype.getLocation = function() {
  470. var rand = Math.random();
  471. var _innerRadius = this.innerRadius;
  472. var _outerRadius = this.outerRadius;
  473. var center = this.center;
  474. var _normal = this.radiusNormal;
  475. _distToOrigin = _normal.dot( center );
  476. var radius = _innerRadius + (1 - rand * rand ) * ( _outerRadius - _innerRadius );
  477. var angle = Math.random() * SPARKS.Utils.TWOPI;
  478. var _distToOrigin = _normal.dot( center );
  479. var axes = SPARKS.Utils.getPerpendiculars( _normal.clone() );
  480. var _planeAxis1 = axes[0];
  481. var _planeAxis2 = axes[1];
  482. var p = _planeAxis1.clone();
  483. p.multiplyScalar( radius * Math.cos( angle ) );
  484. var p2 = _planeAxis2.clone();
  485. p2.multiplyScalar( radius * Math.sin( angle ) );
  486. p.add( p2 );
  487. return _center.add( p );
  488. };
  489. */
  490. SPARKS.SphereCapZone = function( x, y, z, minr, maxr, angle ) {
  491. this.x = x;
  492. this.y = y;
  493. this.z = z;
  494. this.minr = minr;
  495. this.maxr = maxr;
  496. this.angle = angle;
  497. };
  498. SPARKS.SphereCapZone.prototype.getLocation = function() {
  499. var theta = Math.PI * 2 * SPARKS.Utils.random();
  500. var r = SPARKS.Utils.random();
  501. //new THREE.Vector3
  502. var v = SPARKS.VectorPool.get().set( r * Math.cos( theta ), - 1 / Math.tan( this.angle * SPARKS.Utils.DEGREE_TO_RADIAN ), r * Math.sin( theta ) );
  503. //v.length = StardustMath.interpolate(0, _minRadius, 1, _maxRadius, Math.random());
  504. var i = this.minr - ( ( this.minr - this.maxr ) * Math.random() );
  505. v.multiplyScalar( i );
  506. v.__markedForReleased = true;
  507. return v;
  508. };
  509. /********************************
  510. * Initializer Classes
  511. *
  512. * Classes which initializes
  513. * particles. Implements initialize( emitter:Emitter, particle:Particle )
  514. *********************************/
  515. // Specifies random life between max and min
  516. SPARKS.Lifetime = function( min, max ) {
  517. this._min = min;
  518. this._max = max ? max : min;
  519. };
  520. SPARKS.Lifetime.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) {
  521. particle.lifetime = this._min + SPARKS.Utils.random() * ( this._max - this._min );
  522. };
  523. SPARKS.Position = function( zone ) {
  524. this.zone = zone;
  525. };
  526. SPARKS.Position.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) {
  527. var pos = this.zone.getLocation();
  528. particle.position.set( pos.x, pos.y, pos.z );
  529. };
  530. SPARKS.Velocity = function( zone ) {
  531. this.zone = zone;
  532. };
  533. SPARKS.Velocity.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) {
  534. var pos = this.zone.getLocation();
  535. particle.velocity.set( pos.x, pos.y, pos.z );
  536. if ( pos.__markedForReleased ) {
  537. //console.log("release");
  538. SPARKS.VectorPool.release( pos );
  539. pos.__markedForReleased = false;
  540. }
  541. };
  542. SPARKS.Target = function( target, callback ) {
  543. this.target = target;
  544. this.callback = callback;
  545. };
  546. SPARKS.Target.prototype.initialize = function( emitter, particle ) {
  547. if ( this.callback ) {
  548. particle.target = this.callback();
  549. } else {
  550. particle.target = this.target;
  551. }
  552. };
  553. /********************************
  554. * VectorPool
  555. *
  556. * Reuse much of Vectors if possible
  557. *********************************/
  558. SPARKS.VectorPool = {
  559. __pools: [],
  560. // Get a new Vector
  561. get: function() {
  562. if ( this.__pools.length > 0 ) {
  563. return this.__pools.pop();
  564. }
  565. return this._addToPool();
  566. },
  567. // Release a vector back into the pool
  568. release: function( v ) {
  569. this.__pools.push( v );
  570. },
  571. // Create a bunch of vectors and add to the pool
  572. _addToPool: function() {
  573. //console.log("creating some pools");
  574. for ( var i = 0, size = 100; i < size; i ++ ) {
  575. this.__pools.push( new THREE.Vector3() );
  576. }
  577. return new THREE.Vector3();
  578. }
  579. };
  580. /********************************
  581. * Util Classes
  582. *
  583. * Classes which initializes
  584. * particles. Implements initialize( emitter:Emitter, particle:Particle )
  585. *********************************/
  586. SPARKS.Utils = {
  587. random: function() {
  588. return Math.random();
  589. },
  590. DEGREE_TO_RADIAN: Math.PI / 180,
  591. TWOPI: Math.PI * 2,
  592. getPerpendiculars: function( normal ) {
  593. var p1 = this.getPerpendicular( normal );
  594. var p2 = normal.cross( p1 );
  595. p2.normalize();
  596. return [ p1, p2 ];
  597. },
  598. getPerpendicular: function( v ) {
  599. if ( v.x == 0 ) {
  600. return new THREE.Vector3D( 1, 0, 0 );
  601. } else {
  602. var temp = new THREE.Vector3( v.y, - v.x, 0 );
  603. return temp.normalize();
  604. }
  605. }
  606. };