ParticleEmitter.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Copyright (c) 2009-2010 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.effect;
  33. import com.jme3.bounding.BoundingBox;
  34. import com.jme3.effect.ParticleMesh.Type;
  35. import com.jme3.effect.influencers.DefaultParticleInfluencer;
  36. import com.jme3.effect.influencers.ParticleInfluencer;
  37. import com.jme3.export.JmeExporter;
  38. import com.jme3.export.JmeImporter;
  39. import com.jme3.export.InputCapsule;
  40. import com.jme3.export.OutputCapsule;
  41. import com.jme3.math.ColorRGBA;
  42. import com.jme3.math.FastMath;
  43. import com.jme3.math.Matrix3f;
  44. import com.jme3.math.Vector3f;
  45. import com.jme3.renderer.Camera;
  46. import com.jme3.renderer.RenderManager;
  47. import com.jme3.renderer.ViewPort;
  48. import com.jme3.renderer.queue.RenderQueue.Bucket;
  49. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  50. import com.jme3.scene.Geometry;
  51. import com.jme3.scene.Spatial;
  52. import com.jme3.scene.control.Control;
  53. import com.jme3.util.TempVars;
  54. import java.io.IOException;
  55. public class ParticleEmitter extends Geometry implements Control {
  56. private static final EmitterShape DEFAULT_SHAPE = new EmitterPointShape(Vector3f.ZERO);
  57. private static final ParticleInfluencer DEFAULT_INFLUENCER = new DefaultParticleInfluencer();
  58. private EmitterShape shape = DEFAULT_SHAPE;
  59. private ParticleMesh particleMesh;
  60. private ParticleInfluencer particleInfluencer = DEFAULT_INFLUENCER;
  61. private ParticleMesh.Type meshType;
  62. private Particle[] particles;
  63. private int firstUnUsed;
  64. private int lastUsed;
  65. // private int next = 0;
  66. // private ArrayList<Integer> unusedIndices = new ArrayList<Integer>();
  67. private boolean randomAngle;
  68. private boolean selectRandomImage;
  69. private boolean facingVelocity;
  70. private float particlesPerSec = 20;
  71. private float emitCarry;
  72. private float lowLife = 3f;
  73. private float highLife = 7f;
  74. private Vector3f gravity = new Vector3f(0.0f, 0.1f, 0.0f);
  75. private float rotateSpeed;
  76. private Vector3f faceNormal = new Vector3f(Vector3f.NAN);
  77. private int imagesX = 1;
  78. private int imagesY = 1;
  79. private boolean enabled = true;
  80. private ColorRGBA startColor = new ColorRGBA(0.4f,0.4f,0.4f,0.5f);
  81. private ColorRGBA endColor = new ColorRGBA(0.1f,0.1f,0.1f,0.0f);
  82. private float startSize = 0.2f;
  83. private float endSize = 2f;
  84. private boolean worldSpace = true;
  85. //variable that helps with computations
  86. private transient Vector3f temp = new Vector3f();
  87. @Override
  88. public ParticleEmitter clone(){
  89. ParticleEmitter clone = (ParticleEmitter) super.clone();
  90. clone.shape = shape.deepClone();
  91. clone.setNumParticles(particles.length);
  92. clone.faceNormal = faceNormal.clone();
  93. clone.startColor = startColor.clone();
  94. clone.endColor = endColor.clone();
  95. clone.particleInfluencer = particleInfluencer.clone();
  96. clone.controls.add(clone);
  97. return clone;
  98. }
  99. public ParticleEmitter(String name, Type type, int numParticles){
  100. super(name);
  101. // ignore world transform, unless user sets inLocalSpace
  102. this.setIgnoreTransform(true);
  103. // particles neither receive nor cast shadows
  104. this.setShadowMode(ShadowMode.Off);
  105. // particles are usually transparent
  106. this.setQueueBucket(Bucket.Transparent);
  107. meshType = type;
  108. this.setNumParticles(numParticles);
  109. controls.add(this);
  110. switch (meshType){
  111. case Point:
  112. particleMesh = new ParticlePointMesh();
  113. this.setMesh(particleMesh);
  114. break;
  115. case Triangle:
  116. particleMesh = new ParticleTriMesh();
  117. this.setMesh(particleMesh);
  118. break;
  119. default:
  120. throw new IllegalStateException("Unrecognized particle type: "+meshType);
  121. }
  122. particleMesh.initParticleData(this, particles.length);
  123. }
  124. public ParticleEmitter(){
  125. super();
  126. }
  127. @Override
  128. public Control cloneForSpatial(Spatial spatial){
  129. return (Control) spatial;
  130. }
  131. public void setShape(EmitterShape shape) {
  132. this.shape = shape;
  133. }
  134. public EmitterShape getShape(){
  135. return shape;
  136. }
  137. public void setParticleInfluencer(ParticleInfluencer particleInfluencer) {
  138. this.particleInfluencer = particleInfluencer;
  139. }
  140. public ParticleInfluencer getParticleInfluencer() {
  141. return particleInfluencer;
  142. }
  143. public ParticleMesh.Type getMeshType() {
  144. return meshType;
  145. }
  146. public boolean isInWorldSpace() {
  147. return worldSpace;
  148. }
  149. public void setInWorldSpace(boolean worldSpace) {
  150. this.setIgnoreTransform(worldSpace);
  151. this.worldSpace = worldSpace;
  152. }
  153. public int getNumVisibleParticles(){
  154. // return unusedIndices.size() + next;
  155. return lastUsed + 1;
  156. }
  157. /**
  158. * @param numParticles The maximum amount of particles that
  159. * can exist at the same time with this emitter.
  160. * Calling this method many times is not recommended.
  161. */
  162. public final void setNumParticles(int numParticles){
  163. particles = new Particle[numParticles];
  164. for (int i = 0; i < numParticles; i++){
  165. particles[i] = new Particle();
  166. }
  167. firstUnUsed = 0;
  168. lastUsed = -1;
  169. }
  170. /**
  171. * @return A list of all particles (shouldn't be used in most cases).
  172. * This includes both existing and non-existing particles.
  173. * The size of the array is set to the <code>numParticles</code> value
  174. * specified in the constructor or {@link ParticleEmitter#setNumParticles(int) }
  175. * method.
  176. */
  177. public Particle[] getParticles(){
  178. return particles;
  179. }
  180. public Vector3f getFaceNormal() {
  181. if (Vector3f.isValidVector(faceNormal)) {
  182. return faceNormal;
  183. } else {
  184. return null;
  185. }
  186. }
  187. /**
  188. * Sets the normal which particles are facing. By default, particles
  189. * will face the camera, but for some effects (e.g shockwave) it may
  190. * be necessary to face a specific direction instead. To restore
  191. * normal functionality, provide <code>null</code> as the argument for
  192. * <code>faceNormal</code>.
  193. *
  194. * @param faceNormal The normals particles should face, or <code>null</code>
  195. * if particles should face the camera.
  196. */
  197. public void setFaceNormal(Vector3f faceNormal) {
  198. if (faceNormal == null || !Vector3f.isValidVector(faceNormal)) {
  199. this.faceNormal.set(Vector3f.NAN);
  200. } else {
  201. this.faceNormal = faceNormal;
  202. }
  203. }
  204. public float getRotateSpeed() {
  205. return rotateSpeed;
  206. }
  207. /**
  208. * @param rotateSpeed Set the rotation speed in radians/sec for particles
  209. * spawned after the invocation of this method.
  210. */
  211. public void setRotateSpeed(float rotateSpeed) {
  212. this.rotateSpeed = rotateSpeed;
  213. }
  214. public boolean isRandomAngle() {
  215. return randomAngle;
  216. }
  217. /**
  218. * @param randomAngle Set to <code>true</code> if every particle spawned
  219. * should have a random facing angle.
  220. */
  221. public void setRandomAngle(boolean randomAngle) {
  222. this.randomAngle = randomAngle;
  223. }
  224. public boolean isSelectRandomImage() {
  225. return selectRandomImage;
  226. }
  227. /**
  228. * @param selectRandomImage Set to true if every particle spawned
  229. * should get a random image from a pool of images constructed from
  230. * the texture, with X by Y possible images. By default, X and Y are equal
  231. * to 1, thus allowing only 1 possible image to be selected, but if the
  232. * particle is configured with multiple images by using {@link ParticleEmitter#setImagesX(int) }
  233. * and {#link ParticleEmitter#setImagesY(int) } methods, then multiple images
  234. * can be selected. Setting to false will cause each particle to have an animation
  235. * of images displayed, starting at image 1, and going until image X*Y when
  236. * the particle reaches its end of life.
  237. */
  238. public void setSelectRandomImage(boolean selectRandomImage) {
  239. this.selectRandomImage = selectRandomImage;
  240. }
  241. public boolean isFacingVelocity() {
  242. return facingVelocity;
  243. }
  244. /**
  245. * @param followVelocity Set to true if particles spawned should face
  246. * their velocity (or direction to which they are moving towards).
  247. * This is typically used for e.g spark effects.
  248. */
  249. public void setFacingVelocity(boolean followVelocity) {
  250. this.facingVelocity = followVelocity;
  251. }
  252. public ColorRGBA getEndColor() {
  253. return endColor;
  254. }
  255. /**
  256. * @param endColor Set the end color of the particles spawned. The
  257. * particle color at any time is determined by blending the start color
  258. * and end color based on the particle's current time of life relative
  259. * to its end of life.
  260. */
  261. public void setEndColor(ColorRGBA endColor) {
  262. this.endColor.set(endColor);
  263. }
  264. public float getEndSize() {
  265. return endSize;
  266. }
  267. /**
  268. * @param endSize Set the end size of the particles spawned.The
  269. * particle size at any time is determined by blending the start size
  270. * and end size based on the particle's current time of life relative
  271. * to its end of life.
  272. */
  273. public void setEndSize(float endSize) {
  274. this.endSize = endSize;
  275. }
  276. /**
  277. * This method sets the gravity value of Y axis.
  278. * By default the Y axis is the only one to have gravity value non zero.
  279. * @param gravity
  280. * Set the gravity of Y axis, in units/sec/sec, of particles
  281. * spawned.
  282. */
  283. @Deprecated
  284. public void setGravity(float gravity) {
  285. this.gravity.y = gravity;
  286. }
  287. /**
  288. * This method returns the gravity vector.
  289. * @return the gravity vector
  290. */
  291. public Vector3f getGravity() {
  292. return gravity;
  293. }
  294. /**
  295. * This method sets the gravity vector.
  296. * @param gravity
  297. * the gravity vector
  298. */
  299. public void setGravity(Vector3f gravity) {
  300. this.gravity.set(gravity);
  301. }
  302. /**
  303. * This method sets the gravity vector.
  304. * @param gravity
  305. * the gravity vector
  306. */
  307. public void setGravity(float[] gravity) {
  308. this.setGravity(gravity[0], gravity[1], gravity[2]);
  309. }
  310. /**
  311. * This method sets the gravity vector.
  312. * @param gravity
  313. * the gravity vector
  314. */
  315. public void setGravity(float x, float y, float z) {
  316. this.gravity.x = x;
  317. this.gravity.y = y;
  318. this.gravity.z = z;
  319. }
  320. public float getHighLife() {
  321. return highLife;
  322. }
  323. /**
  324. * @param highLife Set the high value of life. The particle's lifetime/expiration
  325. * is determined by randomly selecting a time between low life and high life.
  326. */
  327. public void setHighLife(float highLife) {
  328. this.highLife = highLife;
  329. }
  330. public int getImagesX() {
  331. return imagesX;
  332. }
  333. /**
  334. * @param imagesX Set the number of images along the X axis (width). To determine
  335. * how multiple particle images are selected and used, see the
  336. * {@link ParticleEmitter#setSelectRandomImage(boolean) } method.
  337. */
  338. public void setImagesX(int imagesX) {
  339. this.imagesX = imagesX;
  340. particleMesh.setImagesXY(this.imagesX, this.imagesY);
  341. }
  342. public int getImagesY() {
  343. return imagesY;
  344. }
  345. /**
  346. * @param imagesY Set the number of images along the Y axis (height). To determine
  347. * how multiple particle images are selected and used, see the
  348. * {@link ParticleEmitter#setSelectRandomImage(boolean) } method.
  349. */
  350. public void setImagesY(int imagesY) {
  351. this.imagesY = imagesY;
  352. particleMesh.setImagesXY(this.imagesX, this.imagesY);
  353. }
  354. public float getLowLife() {
  355. return lowLife;
  356. }
  357. /**
  358. * @param lowLife Set the low value of life. The particle's lifetime/expiration
  359. * is determined by randomly selecting a time between low life and high life.
  360. */
  361. public void setLowLife(float lowLife) {
  362. this.lowLife = lowLife;
  363. }
  364. public float getParticlesPerSec() {
  365. return particlesPerSec;
  366. }
  367. /**
  368. * @param particlesPerSec Set the number of particles to spawn per
  369. * second.
  370. */
  371. public void setParticlesPerSec(float particlesPerSec) {
  372. this.particlesPerSec = particlesPerSec;
  373. }
  374. public ColorRGBA getStartColor() {
  375. return startColor;
  376. }
  377. /**
  378. * @param startColor Set the start color of the particles spawned. The
  379. * particle color at any time is determined by blending the start color
  380. * and end color based on the particle's current time of life relative
  381. * to its end of life.
  382. */
  383. public void setStartColor(ColorRGBA startColor) {
  384. this.startColor.set(startColor);
  385. }
  386. public float getStartSize() {
  387. return startSize;
  388. }
  389. /**
  390. * @param startSize Set the start size of the particles spawned.The
  391. * particle size at any time is determined by blending the start size
  392. * and end size based on the particle's current time of life relative
  393. * to its end of life.
  394. */
  395. public void setStartSize(float startSize) {
  396. this.startSize = startSize;
  397. }
  398. /**
  399. * This method is deprecated.
  400. * Use ParticleEmitter.getParticleInfluencer().getInitialVelocity() instead.
  401. * @return the initial velocity for particles
  402. */
  403. @Deprecated
  404. public Vector3f getInitialVelocity(){
  405. return particleInfluencer.getInitialVelocity();
  406. }
  407. /**
  408. * @param initialVelocity Set the initial velocity a particle is spawned with,
  409. * the initial velocity given in the parameter will be varied according
  410. * to the velocity variation set in {@link ParticleEmitter#setVelocityVariation(float) }.
  411. * A particle will move toward its velocity unless it is effected by the
  412. * gravity.
  413. *
  414. * @deprecated
  415. * This method is deprecated.
  416. * Use ParticleEmitter.getParticleInfluencer().setInitialVelocity(initialVelocity); instead.
  417. *
  418. * @see ParticleEmitter#setVelocityVariation(float)
  419. * @see ParticleEmitter#setGravity(float)
  420. */
  421. @Deprecated
  422. public void setInitialVelocity(Vector3f initialVelocity){
  423. this.particleInfluencer.setInitialVelocity(initialVelocity);
  424. }
  425. /**
  426. * @deprecated
  427. * This method is deprecated.
  428. * Use ParticleEmitter.getParticleInfluencer().getVelocityVariation(); instead.
  429. * @return the initial velocity variation factor
  430. */
  431. @Deprecated
  432. public float getVelocityVariation() {
  433. return particleInfluencer.getVelocityVariation();
  434. }
  435. /**
  436. * @param variation Set the variation by which the initial velocity
  437. * of the particle is determined. <code>variation</code> should be a value
  438. * from 0 to 1, where 0 means particles are to spawn with exactly
  439. * the velocity given in {@link ParticleEmitter#setStartVel(com.jme3.math.Vector3f) },
  440. * and 1 means particles are to spawn with a completely random velocity.
  441. *
  442. * @deprecated
  443. * This method is deprecated.
  444. * Use ParticleEmitter.getParticleInfluencer().setVelocityVariation(variation); instead.
  445. */
  446. @Deprecated
  447. public void setVelocityVariation(float variation) {
  448. this.particleInfluencer.setVelocityVariation(variation);
  449. }
  450. // private int newIndex(){
  451. // liveParticles ++;
  452. // return unusedIndices.remove(0);
  453. // if (unusedIndices.size() > 0){
  454. // liveParticles++;
  455. // return unusedIndices.remove(0);
  456. // }else if (next < particles.length){
  457. // liveParticles++;
  458. // return next++;
  459. // }else{
  460. // return -1;
  461. // }
  462. // }
  463. // private void freeIndex(int index){
  464. // liveParticles--;
  465. // if (index == next-1)
  466. // next--;
  467. // else
  468. // assert !unusedIndices.contains(index);
  469. // unusedIndices.add(index);
  470. // }
  471. private boolean emitParticle(Vector3f min, Vector3f max){
  472. // int idx = newIndex();
  473. // if (idx == -1)
  474. // return false;
  475. int idx = lastUsed + 1;
  476. if (idx >= particles.length) {
  477. return false;
  478. }
  479. Particle p = particles[idx];
  480. if (selectRandomImage) {
  481. p.imageIndex = FastMath.nextRandomInt(0, imagesY-1) * imagesX + FastMath.nextRandomInt(0, imagesX-1);
  482. }
  483. p.startlife = lowLife + FastMath.nextRandomFloat() * (highLife - lowLife);
  484. p.life = p.startlife;
  485. p.color.set(startColor);
  486. p.size = startSize;
  487. //shape.getRandomPoint(p.position);
  488. particleInfluencer.influenceParticle(p, shape);
  489. if (worldSpace){
  490. p.position.addLocal(worldTransform.getTranslation());
  491. }
  492. if (randomAngle) {
  493. p.angle = FastMath.nextRandomFloat() * FastMath.TWO_PI;
  494. }
  495. if (rotateSpeed != 0) {
  496. p.rotateSpeed = rotateSpeed * (0.2f + (FastMath.nextRandomFloat() * 2f - 1f) * .8f);
  497. }
  498. temp.set(p.position).addLocal(p.size, p.size, p.size);
  499. max.maxLocal(temp);
  500. temp.set(p.position).subtractLocal(p.size, p.size, p.size);
  501. min.minLocal(temp);
  502. ++lastUsed;
  503. firstUnUsed = idx + 1;
  504. return true;
  505. }
  506. /**
  507. * Instantly emits all the particles possible to be emitted. Any particles
  508. * which are currently inactive will be spawned immediately.
  509. */
  510. public void emitAllParticles(){
  511. // Force world transform to update
  512. this.getWorldTransform();
  513. TempVars vars = TempVars.get();
  514. assert vars.lock();
  515. BoundingBox bbox = (BoundingBox) this.getMesh().getBound();
  516. Vector3f min = vars.vect1;
  517. Vector3f max = vars.vect2;
  518. bbox.getMin(min);
  519. bbox.getMax(max);
  520. if (!Vector3f.isValidVector(min)){
  521. min.set(Vector3f.POSITIVE_INFINITY);
  522. }
  523. if (!Vector3f.isValidVector(max)){
  524. max.set(Vector3f.NEGATIVE_INFINITY);
  525. }
  526. while (this.emitParticle(min, max)) {
  527. ;
  528. }
  529. bbox.setMinMax(min, max);
  530. this.setBoundRefresh();
  531. assert vars.unlock();
  532. }
  533. /**
  534. * Instantly kills all active particles, after this method is called, all
  535. * particles will be dead and no longer visible.
  536. */
  537. public void killAllParticles(){
  538. for (int i = 0; i < particles.length; ++i){
  539. if (particles[i].life > 0) {
  540. this.freeParticle(i);
  541. }
  542. }
  543. }
  544. private void freeParticle(int idx){
  545. Particle p = particles[idx];
  546. p.life = 0;
  547. p.size = 0f;
  548. p.color.set(0,0,0,0);
  549. p.imageIndex = 0;
  550. p.angle = 0;
  551. p.rotateSpeed = 0;
  552. // freeIndex(idx);
  553. if (idx == lastUsed) {
  554. while (lastUsed >= 0 && particles[lastUsed].life == 0) {
  555. lastUsed--;
  556. }
  557. }
  558. if (idx < firstUnUsed) {
  559. firstUnUsed = idx;
  560. }
  561. }
  562. private void swap(int idx1, int idx2) {
  563. Particle p1 = particles[idx1];
  564. particles[idx1] = particles[idx2];
  565. particles[idx2] = p1;
  566. }
  567. private void updateParticleState(float tpf){
  568. // Force world transform to update
  569. this.getWorldTransform();
  570. TempVars vars = TempVars.get();
  571. assert vars.lock();
  572. Vector3f min = vars.vect1.set(Vector3f.POSITIVE_INFINITY);
  573. Vector3f max = vars.vect2.set(Vector3f.NEGATIVE_INFINITY);
  574. for (int i = 0; i < particles.length; ++i){
  575. Particle p = particles[i];
  576. if (p.life == 0){ // particle is dead
  577. // assert i <= firstUnUsed;
  578. continue;
  579. }
  580. p.life -= tpf;
  581. if (p.life <= 0){
  582. this.freeParticle(i);
  583. continue;
  584. }
  585. // position += velocity * tpf
  586. p.distToCam = -1;
  587. // applying gravity
  588. p.velocity.x -= gravity.x * tpf;
  589. p.velocity.y -= gravity.y * tpf;
  590. p.velocity.z -= gravity.z * tpf;
  591. temp.set(p.velocity).multLocal(tpf);
  592. p.position.addLocal(temp);
  593. // affecting color, size and angle
  594. float b = (p.startlife - p.life) / p.startlife;
  595. p.color.interpolate(startColor, endColor, b);
  596. p.size = FastMath.interpolateLinear(b, startSize, endSize);
  597. p.angle += p.rotateSpeed * tpf;
  598. // Computing bounding volume
  599. temp.set(p.position).addLocal(p.size, p.size, p.size);
  600. max.maxLocal(temp);
  601. temp.set(p.position).subtractLocal(p.size, p.size, p.size);
  602. min.minLocal(temp);
  603. if (!selectRandomImage) {
  604. p.imageIndex = (int) (b * imagesX * imagesY);
  605. }
  606. if (firstUnUsed < i) {
  607. this.swap(firstUnUsed, i);
  608. if (i == lastUsed) {
  609. lastUsed = firstUnUsed;
  610. }
  611. firstUnUsed++;
  612. }
  613. }
  614. float particlesToEmitF = particlesPerSec * tpf;
  615. int particlesToEmit = (int) particlesToEmitF;
  616. emitCarry += particlesToEmitF - particlesToEmit;
  617. while (emitCarry > 1f){
  618. ++particlesToEmit;
  619. emitCarry -= 1f;
  620. }
  621. for (int i = 0; i < particlesToEmit; ++i){
  622. this.emitParticle(min, max);
  623. }
  624. BoundingBox bbox = (BoundingBox) this.getMesh().getBound();
  625. bbox.setMinMax(min, max);
  626. this.setBoundRefresh();
  627. assert vars.unlock();
  628. }
  629. /**
  630. * Do not use.
  631. */
  632. @Override
  633. public void setSpatial(Spatial spatial) {
  634. }
  635. /**
  636. * @param enabled Set to enable or disable a particle. When a particle is
  637. * disabled, it will be "frozen in time" and not update.
  638. */
  639. @Override
  640. public void setEnabled(boolean enabled) {
  641. this.enabled = enabled;
  642. }
  643. @Override
  644. public boolean isEnabled() {
  645. return enabled;
  646. }
  647. @Override
  648. public void update(float tpf) {
  649. if (enabled) {
  650. this.updateParticleState(tpf);
  651. }
  652. }
  653. @Override
  654. public void render(RenderManager rm, ViewPort vp) {
  655. Camera cam = vp.getCamera();
  656. if (meshType == ParticleMesh.Type.Point){
  657. float C = cam.getProjectionMatrix().m00;
  658. C *= cam.getWidth() * 0.5f;
  659. // send attenuation params
  660. this.getMaterial().setFloat("Quadratic", C);
  661. }
  662. Matrix3f inverseRotation = Matrix3f.IDENTITY;
  663. if (!worldSpace){
  664. TempVars vars = TempVars.get();
  665. assert vars.lock();
  666. inverseRotation = this.getWorldRotation().toRotationMatrix(vars.tempMat3).invertLocal();
  667. }
  668. particleMesh.updateParticleData(particles, cam, inverseRotation);
  669. if (!worldSpace){
  670. assert TempVars.get().unlock();
  671. }
  672. }
  673. public void preload(RenderManager rm, ViewPort vp){
  674. this.updateParticleState(0);
  675. particleMesh.updateParticleData(particles, vp.getCamera(), Matrix3f.IDENTITY);
  676. }
  677. @Override
  678. public void write(JmeExporter ex) throws IOException{
  679. super.write(ex);
  680. OutputCapsule oc = ex.getCapsule(this);
  681. oc.write(shape, "shape", DEFAULT_SHAPE);
  682. oc.write(meshType, "meshType", ParticleMesh.Type.Triangle);
  683. oc.write(enabled, "enabled", true);
  684. oc.write(particles.length, "numParticles", 0);
  685. oc.write(particlesPerSec, "particlesPerSec", 0);
  686. oc.write(lowLife, "lowLife", 0);
  687. oc.write(highLife, "highLife", 0);
  688. oc.write(gravity, "gravity", null);
  689. oc.write(imagesX, "imagesX", 1);
  690. oc.write(imagesY, "imagesY", 1);
  691. oc.write(startColor, "startColor", null);
  692. oc.write(endColor, "endColor", null);
  693. oc.write(startSize, "startSize", 0);
  694. oc.write(endSize, "endSize", 0);
  695. oc.write(worldSpace, "worldSpace", false);
  696. oc.write(facingVelocity, "facingVelocity", false);
  697. oc.write(selectRandomImage, "selectRandomImage", false);
  698. oc.write(randomAngle, "randomAngle", false);
  699. oc.write(rotateSpeed, "rotateSpeed", 0);
  700. oc.write(particleInfluencer, "influencer", DEFAULT_INFLUENCER);
  701. }
  702. @Override
  703. public void read(JmeImporter im) throws IOException{
  704. super.read(im);
  705. InputCapsule ic = im.getCapsule(this);
  706. shape = (EmitterShape) ic.readSavable("shape", DEFAULT_SHAPE);
  707. meshType = ic.readEnum("meshType", ParticleMesh.Type.class, ParticleMesh.Type.Triangle);
  708. int numParticles = ic.readInt("numParticles", 0);
  709. this.setNumParticles(numParticles);
  710. enabled = ic.readBoolean("enabled", true);
  711. particlesPerSec = ic.readFloat("particlesPerSec", 0);
  712. lowLife = ic.readFloat("lowLife", 0);
  713. highLife = ic.readFloat("highLife", 0);
  714. gravity = (Vector3f) ic.readSavable("gravity", null);
  715. imagesX = ic.readInt("imagesX", 1);
  716. imagesY = ic.readInt("imagesY", 1);
  717. startColor = (ColorRGBA) ic.readSavable("startColor", null);
  718. endColor = (ColorRGBA) ic.readSavable("endColor", null);
  719. startSize = ic.readFloat("startSize", 0);
  720. endSize = ic.readFloat("endSize", 0);
  721. worldSpace = ic.readBoolean("worldSpace", false);
  722. facingVelocity = ic.readBoolean("facingVelocity", false);
  723. selectRandomImage = ic.readBoolean("selectRandomImage", false);
  724. randomAngle = ic.readBoolean("randomAngle", false);
  725. rotateSpeed = ic.readFloat("rotateSpeed", 0);
  726. switch (meshType){
  727. case Point:
  728. particleMesh = new ParticlePointMesh();
  729. this.setMesh(particleMesh);
  730. break;
  731. case Triangle:
  732. particleMesh = new ParticleTriMesh();
  733. this.setMesh(particleMesh);
  734. break;
  735. default:
  736. throw new IllegalStateException("Unrecognized particle type: "+meshType);
  737. }
  738. particleMesh.initParticleData(this, particles.length);
  739. particleInfluencer = (ParticleInfluencer) ic.readSavable("influencer", DEFAULT_INFLUENCER);
  740. }
  741. }