Sparks.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. }
  243. else {
  244. var t = this._easing( particle.age / particle.lifetime );
  245. particle.energy = - 1 * t + 1;
  246. }
  247. };
  248. /*
  249. // Mark particle as dead when particle's < 0
  250. SPARKS.Death = function(easing) {
  251. this._easing = (easing == null) ? TWEEN.Linear.None : easing;
  252. };
  253. SPARKS.Death.prototype.update = function (emitter, particle, time) {
  254. if (particle.life <= 0) {
  255. particle.isDead = true;
  256. }
  257. };
  258. */
  259. SPARKS.Move = function() {
  260. };
  261. SPARKS.Move.prototype.update = function( emitter, particle, time ) {
  262. // attempt verlet velocity updating.
  263. var p = particle.position;
  264. var v = particle.velocity;
  265. var old = particle._oldvelocity;
  266. if ( this._velocityVerlet ) {
  267. p.x += ( v.x + old.x ) * 0.5 * time;
  268. p.y += ( v.y + old.y ) * 0.5 * time;
  269. p.z += ( v.z + old.z ) * 0.5 * time;
  270. } else {
  271. p.x += v.x * time;
  272. p.y += v.y * time;
  273. p.z += v.z * time;
  274. }
  275. // OldVel = Vel;
  276. // Vel = Vel + Accel * dt;
  277. // Pos = Pos + (vel + Vel + Accel * dt) * 0.5 * dt;
  278. };
  279. /* Marks particles found in specified zone dead */
  280. SPARKS.DeathZone = function( zone ) {
  281. this.zone = zone;
  282. };
  283. SPARKS.DeathZone.prototype.update = function( emitter, particle, time ) {
  284. if ( this.zone.contains( particle.position ) ) {
  285. particle.isDead = true;
  286. }
  287. };
  288. /*
  289. * SPARKS.ActionZone applies an action when particle is found in zone
  290. */
  291. SPARKS.ActionZone = function( action, zone ) {
  292. this.action = action;
  293. this.zone = zone;
  294. };
  295. SPARKS.ActionZone.prototype.update = function( emitter, particle, time ) {
  296. if ( this.zone.contains( particle.position ) ) {
  297. this.action.update( emitter, particle, time );
  298. }
  299. };
  300. /*
  301. * Accelerate action affects velocity in specified 3d direction
  302. */
  303. SPARKS.Accelerate = function( x,y,z ) {
  304. if ( x instanceof THREE.Vector3 ) {
  305. this.acceleration = x;
  306. return;
  307. }
  308. this.acceleration = new THREE.Vector3( x,y,z );
  309. };
  310. SPARKS.Accelerate.prototype.update = function( emitter, particle, time ) {
  311. var acc = this.acceleration;
  312. var v = particle.velocity;
  313. particle._oldvelocity.set( v.x, v.y, v.z );
  314. v.x += acc.x * time;
  315. v.y += acc.y * time;
  316. v.z += acc.z * time;
  317. };
  318. /*
  319. * Accelerate Factor accelerate based on a factor of particle's velocity.
  320. */
  321. SPARKS.AccelerateFactor = function( factor ) {
  322. this.factor = factor;
  323. };
  324. SPARKS.AccelerateFactor.prototype.update = function( emitter, particle, time ) {
  325. var factor = this.factor;
  326. var v = particle.velocity;
  327. var len = v.length();
  328. var adjFactor;
  329. if ( len > 0 ) {
  330. adjFactor = factor * time / len;
  331. adjFactor += 1;
  332. v.multiplyScalar( adjFactor );
  333. // v.x *= adjFactor;
  334. // v.y *= adjFactor;
  335. // v.z *= adjFactor;
  336. }
  337. };
  338. /*
  339. AccelerateNormal
  340. * AccelerateVelocity affects velocity based on its velocity direction
  341. */
  342. SPARKS.AccelerateVelocity = function( factor ) {
  343. this.factor = factor;
  344. };
  345. SPARKS.AccelerateVelocity.prototype.update = function( emitter, particle, time ) {
  346. var factor = this.factor;
  347. var v = particle.velocity;
  348. v.z += - v.x * factor;
  349. v.y += v.z * factor;
  350. v.x += v.y * factor;
  351. };
  352. /* Set the max ammount of x,y,z drift movements in a second */
  353. SPARKS.RandomDrift = function( x,y,z ) {
  354. if ( x instanceof THREE.Vector3 ) {
  355. this.drift = x;
  356. return;
  357. }
  358. this.drift = new THREE.Vector3( x,y,z );
  359. };
  360. SPARKS.RandomDrift.prototype.update = function( emitter, particle, time ) {
  361. var drift = this.drift;
  362. var v = particle.velocity;
  363. v.x += ( Math.random() - 0.5 ) * drift.x * time;
  364. v.y += ( Math.random() - 0.5 ) * drift.y * time;
  365. v.z += ( Math.random() - 0.5 ) * drift.z * time;
  366. };
  367. /********************************
  368. * Zone Classes
  369. *
  370. * An abstract classes which have
  371. * getLocation() function
  372. *********************************/
  373. SPARKS.Zone = function() {
  374. };
  375. // TODO, contains() for Zone
  376. SPARKS.PointZone = function( pos ) {
  377. this.pos = pos;
  378. };
  379. SPARKS.PointZone.prototype.getLocation = function() {
  380. return this.pos;
  381. };
  382. SPARKS.PointZone = function( pos ) {
  383. this.pos = pos;
  384. };
  385. SPARKS.PointZone.prototype.getLocation = function() {
  386. return this.pos;
  387. };
  388. SPARKS.LineZone = function( start, end ) {
  389. this.start = start;
  390. this.end = end;
  391. this._length = end.clone().sub( start );
  392. };
  393. SPARKS.LineZone.prototype.getLocation = function() {
  394. var len = this._length.clone();
  395. len.multiplyScalar( Math.random() );
  396. return len.add( this.start );
  397. };
  398. // Basically a RectangleZone
  399. SPARKS.ParallelogramZone = function( corner, side1, side2 ) {
  400. this.corner = corner;
  401. this.side1 = side1;
  402. this.side2 = side2;
  403. };
  404. SPARKS.ParallelogramZone.prototype.getLocation = function() {
  405. var d1 = this.side1.clone().multiplyScalar( Math.random() );
  406. var d2 = this.side2.clone().multiplyScalar( Math.random() );
  407. d1.add( d2 );
  408. return d1.add( this.corner );
  409. };
  410. SPARKS.CubeZone = function( position, x, y, z ) {
  411. this.position = position;
  412. this.x = x;
  413. this.y = y;
  414. this.z = z;
  415. };
  416. SPARKS.CubeZone.prototype.getLocation = function() {
  417. //TODO use pool?
  418. var location = this.position.clone();
  419. location.x += Math.random() * this.x;
  420. location.y += Math.random() * this.y;
  421. location.z += Math.random() * this.z;
  422. return location;
  423. };
  424. SPARKS.CubeZone.prototype.contains = function( position ) {
  425. var startX = this.position.x;
  426. var startY = this.position.y;
  427. var startZ = this.position.z;
  428. var x = this.x; // width
  429. var y = this.y; // depth
  430. var z = this.z; // height
  431. if ( x < 0 ) {
  432. startX += x;
  433. x = Math.abs( x );
  434. }
  435. if ( y < 0 ) {
  436. startY += y;
  437. y = Math.abs( y );
  438. }
  439. if ( z < 0 ) {
  440. startZ += z;
  441. z = Math.abs( z );
  442. }
  443. var diffX = position.x - startX;
  444. var diffY = position.y - startY;
  445. var diffZ = position.z - startZ;
  446. if ( ( diffX > 0 ) && ( diffX < x ) &&
  447. ( diffY > 0 ) && ( diffY < y ) &&
  448. ( diffZ > 0 ) && ( diffZ < z ) ) {
  449. return true;
  450. }
  451. return false;
  452. };
  453. /**
  454. * The constructor creates a DiscZone 3D zone.
  455. *
  456. * @param centre The point at the center of the disc.
  457. * @param normal A vector normal to the disc.
  458. * @param outerRadius The outer radius of the disc.
  459. * @param innerRadius The inner radius of the disc. This defines the hole
  460. * in the center of the disc. If set to zero, there is no hole.
  461. */
  462. /*
  463. // BUGGY!!
  464. SPARKS.DiscZone = function(center, radiusNormal, outerRadius, innerRadius) {
  465. this.center = center;
  466. this.radiusNormal = radiusNormal;
  467. this.outerRadius = (outerRadius==undefined) ? 0 : outerRadius;
  468. this.innerRadius = (innerRadius==undefined) ? 0 : innerRadius;
  469. };
  470. SPARKS.DiscZone.prototype.getLocation = function() {
  471. var rand = Math.random();
  472. var _innerRadius = this.innerRadius;
  473. var _outerRadius = this.outerRadius;
  474. var center = this.center;
  475. var _normal = this.radiusNormal;
  476. _distToOrigin = _normal.dot( center );
  477. var radius = _innerRadius + (1 - rand * rand ) * ( _outerRadius - _innerRadius );
  478. var angle = Math.random() * SPARKS.Utils.TWOPI;
  479. var _distToOrigin = _normal.dot( center );
  480. var axes = SPARKS.Utils.getPerpendiculars( _normal.clone() );
  481. var _planeAxis1 = axes[0];
  482. var _planeAxis2 = axes[1];
  483. var p = _planeAxis1.clone();
  484. p.multiplyScalar( radius * Math.cos( angle ) );
  485. var p2 = _planeAxis2.clone();
  486. p2.multiplyScalar( radius * Math.sin( angle ) );
  487. p.add( p2 );
  488. return _center.add( p );
  489. };
  490. */
  491. SPARKS.SphereCapZone = function( x, y, z, minr, maxr, angle ) {
  492. this.x = x;
  493. this.y = y;
  494. this.z = z;
  495. this.minr = minr;
  496. this.maxr = maxr;
  497. this.angle = angle;
  498. };
  499. SPARKS.SphereCapZone.prototype.getLocation = function() {
  500. var theta = Math.PI * 2 * SPARKS.Utils.random();
  501. var r = SPARKS.Utils.random();
  502. //new THREE.Vector3
  503. var v = SPARKS.VectorPool.get().set( r * Math.cos( theta ), - 1 / Math.tan( this.angle * SPARKS.Utils.DEGREE_TO_RADIAN ), r * Math.sin( theta ) );
  504. //v.length = StardustMath.interpolate(0, _minRadius, 1, _maxRadius, Math.random());
  505. var i = this.minr - ( ( this.minr - this.maxr ) * Math.random() );
  506. v.multiplyScalar( i );
  507. v.__markedForReleased = true;
  508. return v;
  509. };
  510. /********************************
  511. * Initializer Classes
  512. *
  513. * Classes which initializes
  514. * particles. Implements initialize( emitter:Emitter, particle:Particle )
  515. *********************************/
  516. // Specifies random life between max and min
  517. SPARKS.Lifetime = function( min, max ) {
  518. this._min = min;
  519. this._max = max ? max : min;
  520. };
  521. SPARKS.Lifetime.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) {
  522. particle.lifetime = this._min + SPARKS.Utils.random() * ( this._max - this._min );
  523. };
  524. SPARKS.Position = function( zone ) {
  525. this.zone = zone;
  526. };
  527. SPARKS.Position.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) {
  528. var pos = this.zone.getLocation();
  529. particle.position.set( pos.x, pos.y, pos.z );
  530. };
  531. SPARKS.Velocity = function( zone ) {
  532. this.zone = zone;
  533. };
  534. SPARKS.Velocity.prototype.initialize = function( emitter/*Emitter*/, particle/*Particle*/ ) {
  535. var pos = this.zone.getLocation();
  536. particle.velocity.set( pos.x, pos.y, pos.z );
  537. if ( pos.__markedForReleased ) {
  538. //console.log("release");
  539. SPARKS.VectorPool.release( pos );
  540. pos.__markedForReleased = false;
  541. }
  542. };
  543. SPARKS.Target = function( target, callback ) {
  544. this.target = target;
  545. this.callback = callback;
  546. };
  547. SPARKS.Target.prototype.initialize = function( emitter, particle ) {
  548. if ( this.callback ) {
  549. particle.target = this.callback();
  550. } else {
  551. particle.target = this.target;
  552. }
  553. };
  554. /********************************
  555. * VectorPool
  556. *
  557. * Reuse much of Vectors if possible
  558. *********************************/
  559. SPARKS.VectorPool = {
  560. __pools: [],
  561. // Get a new Vector
  562. get: function() {
  563. if ( this.__pools.length > 0 ) {
  564. return this.__pools.pop();
  565. }
  566. return this._addToPool();
  567. },
  568. // Release a vector back into the pool
  569. release: function( v ) {
  570. this.__pools.push( v );
  571. },
  572. // Create a bunch of vectors and add to the pool
  573. _addToPool: function() {
  574. //console.log("creating some pools");
  575. for ( var i = 0, size = 100; i < size; i ++ ) {
  576. this.__pools.push( new THREE.Vector3() );
  577. }
  578. return new THREE.Vector3();
  579. }
  580. };
  581. /********************************
  582. * Util Classes
  583. *
  584. * Classes which initializes
  585. * particles. Implements initialize( emitter:Emitter, particle:Particle )
  586. *********************************/
  587. SPARKS.Utils = {
  588. random: function() {
  589. return Math.random();
  590. },
  591. DEGREE_TO_RADIAN: Math.PI / 180,
  592. TWOPI: Math.PI * 2,
  593. getPerpendiculars: function( normal ) {
  594. var p1 = this.getPerpendicular( normal );
  595. var p2 = normal.cross( p1 );
  596. p2.normalize();
  597. return [ p1, p2 ];
  598. },
  599. getPerpendicular: function( v ) {
  600. if ( v.x == 0 ) {
  601. return new THREE.Vector3D( 1, 0, 0 );
  602. }
  603. else {
  604. var temp = new THREE.Vector3( v.y, - v.x, 0 );
  605. return temp.normalize();
  606. }
  607. }
  608. };