ParticleEmitter.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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.effect.shapes.EmitterPointShape;
  38. import com.jme3.effect.shapes.EmitterShape;
  39. import com.jme3.export.JmeExporter;
  40. import com.jme3.export.JmeImporter;
  41. import com.jme3.export.InputCapsule;
  42. import com.jme3.export.OutputCapsule;
  43. import com.jme3.math.ColorRGBA;
  44. import com.jme3.math.FastMath;
  45. import com.jme3.math.Matrix3f;
  46. import com.jme3.math.Vector3f;
  47. import com.jme3.renderer.Camera;
  48. import com.jme3.renderer.RenderManager;
  49. import com.jme3.renderer.ViewPort;
  50. import com.jme3.renderer.queue.RenderQueue.Bucket;
  51. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  52. import com.jme3.scene.Geometry;
  53. import com.jme3.scene.Spatial;
  54. import com.jme3.scene.control.Control;
  55. import com.jme3.util.TempVars;
  56. import java.io.IOException;
  57. /**
  58. * <code>ParticleEmitter</code> is a special kind of geometry which simulates
  59. * a particle system.
  60. * <p>
  61. * Particle emitters can be used to simulate various kinds of phenomena,
  62. * such as fire, smoke, explosions and much more.
  63. * <p>
  64. * Particle emitters have many properties which are used to control the
  65. * simulation. The interpretation of these properties depends on the
  66. * {@link ParticleInfluencer} that has been assigned to the emitter via
  67. * {@link ParticleEmitter#setParticleInfluencer(com.jme3.effect.influencers.ParticleInfluencer) }.
  68. * By default the implementation {@link DefaultParticleInfluencer} is used.
  69. *
  70. * @author Kirill Vainer
  71. */
  72. public class ParticleEmitter extends Geometry {
  73. private static final EmitterShape DEFAULT_SHAPE = new EmitterPointShape(Vector3f.ZERO);
  74. private static final ParticleInfluencer DEFAULT_INFLUENCER = new DefaultParticleInfluencer();
  75. private ParticleEmitterControl control;
  76. private EmitterShape shape = DEFAULT_SHAPE;
  77. private ParticleMesh particleMesh;
  78. private ParticleInfluencer particleInfluencer = DEFAULT_INFLUENCER;
  79. private ParticleMesh.Type meshType;
  80. private Particle[] particles;
  81. private int firstUnUsed;
  82. private int lastUsed;
  83. // private int next = 0;
  84. // private ArrayList<Integer> unusedIndices = new ArrayList<Integer>();
  85. private boolean randomAngle;
  86. private boolean selectRandomImage;
  87. private boolean facingVelocity;
  88. private float particlesPerSec = 20;
  89. private float emitCarry;
  90. private float lowLife = 3f;
  91. private float highLife = 7f;
  92. private Vector3f gravity = new Vector3f(0.0f, 0.1f, 0.0f);
  93. private float rotateSpeed;
  94. private Vector3f faceNormal = new Vector3f(Vector3f.NAN);
  95. private int imagesX = 1;
  96. private int imagesY = 1;
  97. private boolean enabled = true;
  98. private ColorRGBA startColor = new ColorRGBA(0.4f, 0.4f, 0.4f, 0.5f);
  99. private ColorRGBA endColor = new ColorRGBA(0.1f, 0.1f, 0.1f, 0.0f);
  100. private float startSize = 0.2f;
  101. private float endSize = 2f;
  102. private boolean worldSpace = true;
  103. //variable that helps with computations
  104. private transient Vector3f temp = new Vector3f();
  105. public static class ParticleEmitterControl implements Control {
  106. ParticleEmitter parentEmitter;
  107. public ParticleEmitterControl() {
  108. }
  109. public ParticleEmitterControl(ParticleEmitter parentEmitter) {
  110. this.parentEmitter = parentEmitter;
  111. }
  112. public Control cloneForSpatial(Spatial spatial) {
  113. return this; // WARNING: Sets wrong control on spatial. Will be
  114. // fixed automatically by ParticleEmitter.clone() method.
  115. }
  116. public void setSpatial(Spatial spatial) {
  117. }
  118. public void setEnabled(boolean enabled) {
  119. parentEmitter.setEnabled(enabled);
  120. }
  121. public boolean isEnabled() {
  122. return parentEmitter.isEnabled();
  123. }
  124. public void update(float tpf) {
  125. parentEmitter.updateFromControl(tpf);
  126. }
  127. public void render(RenderManager rm, ViewPort vp) {
  128. parentEmitter.renderFromControl(rm, vp);
  129. }
  130. public void write(JmeExporter ex) throws IOException {
  131. }
  132. public void read(JmeImporter im) throws IOException {
  133. }
  134. }
  135. @Override
  136. public ParticleEmitter clone() {
  137. return clone(true);
  138. }
  139. @Override
  140. public ParticleEmitter clone(boolean cloneMaterial) {
  141. ParticleEmitter clone = (ParticleEmitter) super.clone(cloneMaterial);
  142. clone.shape = shape.deepClone();
  143. // Reinitialize particle list
  144. clone.setNumParticles(particles.length);
  145. clone.faceNormal = faceNormal.clone();
  146. clone.startColor = startColor.clone();
  147. clone.endColor = endColor.clone();
  148. clone.particleInfluencer = particleInfluencer.clone();
  149. // remove wrong control
  150. clone.controls.remove(control);
  151. // put correct control
  152. clone.controls.add(new ParticleEmitterControl(clone));
  153. // Reinitialize particle mesh
  154. switch (meshType) {
  155. case Point:
  156. clone.particleMesh = new ParticlePointMesh();
  157. clone.setMesh(clone.particleMesh);
  158. break;
  159. case Triangle:
  160. clone.particleMesh = new ParticleTriMesh();
  161. clone.setMesh(clone.particleMesh);
  162. break;
  163. default:
  164. throw new IllegalStateException("Unrecognized particle type: " + meshType);
  165. }
  166. clone.particleMesh.initParticleData(clone, clone.particles.length);
  167. clone.particleMesh.setImagesXY(clone.imagesX, clone.imagesY);
  168. return clone;
  169. }
  170. public ParticleEmitter(String name, Type type, int numParticles) {
  171. super(name);
  172. // ignore world transform, unless user sets inLocalSpace
  173. this.setIgnoreTransform(true);
  174. // particles neither receive nor cast shadows
  175. this.setShadowMode(ShadowMode.Off);
  176. // particles are usually transparent
  177. this.setQueueBucket(Bucket.Transparent);
  178. meshType = type;
  179. // Must create clone of shape/influencer so that a reference to a static is
  180. // not maintained
  181. shape = shape.deepClone();
  182. particleInfluencer = particleInfluencer.clone();
  183. control = new ParticleEmitterControl(this);
  184. controls.add(control);
  185. switch (meshType) {
  186. case Point:
  187. particleMesh = new ParticlePointMesh();
  188. this.setMesh(particleMesh);
  189. break;
  190. case Triangle:
  191. particleMesh = new ParticleTriMesh();
  192. this.setMesh(particleMesh);
  193. break;
  194. default:
  195. throw new IllegalStateException("Unrecognized particle type: " + meshType);
  196. }
  197. this.setNumParticles(numParticles);
  198. // particleMesh.initParticleData(this, particles.length);
  199. }
  200. /**
  201. * For serialization only. Do not use.
  202. */
  203. public ParticleEmitter() {
  204. super();
  205. }
  206. public void setShape(EmitterShape shape) {
  207. this.shape = shape;
  208. }
  209. public EmitterShape getShape() {
  210. return shape;
  211. }
  212. /**
  213. * Set the {@link ParticleInfluencer} to influence this particle emitter.
  214. *
  215. * @param particleInfluencer the {@link ParticleInfluencer} to influence
  216. * this particle emitter.
  217. *
  218. * @see ParticleInfluencer
  219. */
  220. public void setParticleInfluencer(ParticleInfluencer particleInfluencer) {
  221. this.particleInfluencer = particleInfluencer;
  222. }
  223. /**
  224. * Returns the {@link ParticleInfluencer} that influences this
  225. * particle emitter.
  226. *
  227. * @return the {@link ParticleInfluencer} that influences this
  228. * particle emitter.
  229. *
  230. * @see ParticleInfluencer
  231. */
  232. public ParticleInfluencer getParticleInfluencer() {
  233. return particleInfluencer;
  234. }
  235. /**
  236. * Returns the mesh type used by the particle emitter.
  237. *
  238. * <p>This value is set in the constructor and cannot be modified
  239. * afterwards.
  240. *
  241. * @return the mesh type used by the particle emitter.
  242. *
  243. * @see ParticleEmitter#ParticleEmitter(java.lang.String, com.jme3.effect.ParticleMesh.Type, int)
  244. */
  245. public ParticleMesh.Type getMeshType() {
  246. return meshType;
  247. }
  248. /**
  249. * Returns true if particles should spawn in world space.
  250. *
  251. * @return true if particles should spawn in world space.
  252. *
  253. * @see ParticleEmitter#setInWorldSpace(boolean)
  254. */
  255. public boolean isInWorldSpace() {
  256. return worldSpace;
  257. }
  258. /**
  259. * Set to true if particles should spawn in world space.
  260. *
  261. * <p>If set to true and the particle emitter is moved in the scene,
  262. * then particles that have already spawned won't be effected by this
  263. * motion. If set to false, the particles will emit in local space
  264. * and when the emitter is moved, so are all the particles that
  265. * were emitted previously.
  266. *
  267. * @param worldSpace true if particles should spawn in world space.
  268. */
  269. public void setInWorldSpace(boolean worldSpace) {
  270. this.setIgnoreTransform(worldSpace);
  271. this.worldSpace = worldSpace;
  272. }
  273. /**
  274. * Returns the number of visible particles (spawned but not dead).
  275. *
  276. * @return the number of visible particles
  277. */
  278. public int getNumVisibleParticles() {
  279. // return unusedIndices.size() + next;
  280. return lastUsed + 1;
  281. }
  282. /**
  283. * Set the maximum amount of particles that
  284. * can exist at the same time with this emitter.
  285. * Calling this method many times is not recommended.
  286. *
  287. * @param numParticles the maximum amount of particles that
  288. * can exist at the same time with this emitter.
  289. */
  290. public final void setNumParticles(int numParticles) {
  291. particles = new Particle[numParticles];
  292. for (int i = 0; i < numParticles; i++) {
  293. particles[i] = new Particle();
  294. }
  295. //We have to reinit the mesh's buffers with the new size
  296. particleMesh.initParticleData(this, particles.length);
  297. particleMesh.setImagesXY(this.imagesX, this.imagesY);
  298. firstUnUsed = 0;
  299. lastUsed = -1;
  300. }
  301. public int getMaxNumParticles() {
  302. return particles.length;
  303. }
  304. /**
  305. * Returns a list of all particles (shouldn't be used in most cases).
  306. *
  307. * <p>
  308. * This includes both existing and non-existing particles.
  309. * The size of the array is set to the <code>numParticles</code> value
  310. * specified in the constructor or {@link ParticleEmitter#setNumParticles(int) }
  311. * method.
  312. *
  313. * @return a list of all particles.
  314. */
  315. public Particle[] getParticles() {
  316. return particles;
  317. }
  318. /**
  319. * Get the normal which particles are facing.
  320. *
  321. * @return the normal which particles are facing.
  322. *
  323. * @see ParticleEmitter#setFaceNormal(com.jme3.math.Vector3f)
  324. */
  325. public Vector3f getFaceNormal() {
  326. if (Vector3f.isValidVector(faceNormal)) {
  327. return faceNormal;
  328. } else {
  329. return null;
  330. }
  331. }
  332. /**
  333. * Sets the normal which particles are facing.
  334. *
  335. * <p>By default, particles
  336. * will face the camera, but for some effects (e.g shockwave) it may
  337. * be necessary to face a specific direction instead. To restore
  338. * normal functionality, provide <code>null</code> as the argument for
  339. * <code>faceNormal</code>.
  340. *
  341. * @param faceNormal The normals particles should face, or <code>null</code>
  342. * if particles should face the camera.
  343. */
  344. public void setFaceNormal(Vector3f faceNormal) {
  345. if (faceNormal == null || !Vector3f.isValidVector(faceNormal)) {
  346. this.faceNormal.set(Vector3f.NAN);
  347. } else {
  348. this.faceNormal = faceNormal;
  349. }
  350. }
  351. /**
  352. * Returns the rotation speed in radians/sec for particles.
  353. *
  354. * @return the rotation speed in radians/sec for particles.
  355. *
  356. * @see ParticleEmitter#setRotateSpeed(float)
  357. */
  358. public float getRotateSpeed() {
  359. return rotateSpeed;
  360. }
  361. /**
  362. * Set the rotation speed in radians/sec for particles
  363. * spawned after the invocation of this method.
  364. *
  365. * @param rotateSpeed the rotation speed in radians/sec for particles
  366. * spawned after the invocation of this method.
  367. */
  368. public void setRotateSpeed(float rotateSpeed) {
  369. this.rotateSpeed = rotateSpeed;
  370. }
  371. /**
  372. * Returns true if every particle spawned
  373. * should have a random facing angle.
  374. *
  375. * @return true if every particle spawned
  376. * should have a random facing angle.
  377. *
  378. * @see ParticleEmitter#setRandomAngle(boolean)
  379. */
  380. public boolean isRandomAngle() {
  381. return randomAngle;
  382. }
  383. /**
  384. * Set to true if every particle spawned
  385. * should have a random facing angle.
  386. *
  387. * @param randomAngle if every particle spawned
  388. * should have a random facing angle.
  389. */
  390. public void setRandomAngle(boolean randomAngle) {
  391. this.randomAngle = randomAngle;
  392. }
  393. /**
  394. * Returns true if every particle spawned should get a random
  395. * image.
  396. *
  397. * @return True if every particle spawned should get a random
  398. * image.
  399. *
  400. * @see ParticleEmitter#setSelectRandomImage(boolean)
  401. */
  402. public boolean isSelectRandomImage() {
  403. return selectRandomImage;
  404. }
  405. /**
  406. * Set to true if every particle spawned
  407. * should get a random image from a pool of images constructed from
  408. * the texture, with X by Y possible images.
  409. *
  410. * <p>By default, X and Y are equal
  411. * to 1, thus allowing only 1 possible image to be selected, but if the
  412. * particle is configured with multiple images by using {@link ParticleEmitter#setImagesX(int) }
  413. * and {#link ParticleEmitter#setImagesY(int) } methods, then multiple images
  414. * can be selected. Setting to false will cause each particle to have an animation
  415. * of images displayed, starting at image 1, and going until image X*Y when
  416. * the particle reaches its end of life.
  417. *
  418. * @param selectRandomImage True if every particle spawned should get a random
  419. * image.
  420. */
  421. public void setSelectRandomImage(boolean selectRandomImage) {
  422. this.selectRandomImage = selectRandomImage;
  423. }
  424. /**
  425. * Check if particles spawned should face their velocity.
  426. *
  427. * @return True if particles spawned should face their velocity.
  428. *
  429. * @see ParticleEmitter#setFacingVelocity(boolean)
  430. */
  431. public boolean isFacingVelocity() {
  432. return facingVelocity;
  433. }
  434. /**
  435. * Set to true if particles spawned should face
  436. * their velocity (or direction to which they are moving towards).
  437. *
  438. * <p>This is typically used for e.g spark effects.
  439. *
  440. * @param followVelocity True if particles spawned should face their velocity.
  441. *
  442. */
  443. public void setFacingVelocity(boolean followVelocity) {
  444. this.facingVelocity = followVelocity;
  445. }
  446. /**
  447. * Get the end color of the particles spawned.
  448. *
  449. * @return the end color of the particles spawned.
  450. *
  451. * @see ParticleEmitter#setEndColor(com.jme3.math.ColorRGBA)
  452. */
  453. public ColorRGBA getEndColor() {
  454. return endColor;
  455. }
  456. /**
  457. * Set the end color of the particles spawned.
  458. *
  459. * <p>The
  460. * particle color at any time is determined by blending the start color
  461. * and end color based on the particle's current time of life relative
  462. * to its end of life.
  463. *
  464. * @param endColor the end color of the particles spawned.
  465. */
  466. public void setEndColor(ColorRGBA endColor) {
  467. this.endColor.set(endColor);
  468. }
  469. /**
  470. * Get the end size of the particles spawned.
  471. *
  472. * @return the end size of the particles spawned.
  473. *
  474. * @see ParticleEmitter#setEndSize(float)
  475. */
  476. public float getEndSize() {
  477. return endSize;
  478. }
  479. /**
  480. * Set the end size of the particles spawned.
  481. *
  482. * <p>The
  483. * particle size at any time is determined by blending the start size
  484. * and end size based on the particle's current time of life relative
  485. * to its end of life.
  486. *
  487. * @param endSize the end size of the particles spawned.
  488. */
  489. public void setEndSize(float endSize) {
  490. this.endSize = endSize;
  491. }
  492. /**
  493. * This method sets the gravity value of Y axis.
  494. *
  495. * By default the Y axis is the only one to have gravity value non zero.
  496. *
  497. * @param gravity
  498. * Set the gravity of Y axis, in units/sec/sec, of particles
  499. * spawned.
  500. *
  501. * @deprecated Use {@link ParticleEmitter#setGravity(float, float, float) instead.
  502. */
  503. @Deprecated
  504. public void setGravity(float gravity) {
  505. this.gravity.y = gravity;
  506. }
  507. /**
  508. * Get the gravity vector.
  509. *
  510. * @return the gravity vector.
  511. *
  512. * @see ParticleEmitter#setGravity(com.jme3.math.Vector3f)
  513. */
  514. public Vector3f getGravity() {
  515. return gravity;
  516. }
  517. /**
  518. * This method sets the gravity vector.
  519. *
  520. * @param gravity the gravity vector
  521. */
  522. public void setGravity(Vector3f gravity) {
  523. this.gravity.set(gravity);
  524. }
  525. /**
  526. * Sets the gravity vector.
  527. *
  528. * @param gravity the gravity vector
  529. */
  530. public void setGravity(float x, float y, float z) {
  531. this.gravity.x = x;
  532. this.gravity.y = y;
  533. this.gravity.z = z;
  534. }
  535. /**
  536. * Get the high value of life.
  537. *
  538. * @return the high value of life.
  539. *
  540. * @see ParticleEmitter#setHighLife(float)
  541. */
  542. public float getHighLife() {
  543. return highLife;
  544. }
  545. /**
  546. * Set the high value of life.
  547. *
  548. * <p>The particle's lifetime/expiration
  549. * is determined by randomly selecting a time between low life and high life.
  550. *
  551. * @param highLife the high value of life.
  552. */
  553. public void setHighLife(float highLife) {
  554. this.highLife = highLife;
  555. }
  556. /**
  557. * Get the number of images along the X axis (width).
  558. *
  559. * @return the number of images along the X axis (width).
  560. *
  561. * @see ParticleEmitter#setImagesX(int)
  562. */
  563. public int getImagesX() {
  564. return imagesX;
  565. }
  566. /**
  567. * Set the number of images along the X axis (width).
  568. *
  569. * <p>To determine
  570. * how multiple particle images are selected and used, see the
  571. * {@link ParticleEmitter#setSelectRandomImage(boolean) } method.
  572. *
  573. * @param imagesX the number of images along the X axis (width).
  574. */
  575. public void setImagesX(int imagesX) {
  576. this.imagesX = imagesX;
  577. particleMesh.setImagesXY(this.imagesX, this.imagesY);
  578. }
  579. /**
  580. * Get the number of images along the Y axis (height).
  581. *
  582. * @return the number of images along the Y axis (height).
  583. *
  584. * @see ParticleEmitter#setImagesY(int)
  585. */
  586. public int getImagesY() {
  587. return imagesY;
  588. }
  589. /**
  590. * Set the number of images along the Y axis (height).
  591. *
  592. * <p>To determine how multiple particle images are selected and used, see the
  593. * {@link ParticleEmitter#setSelectRandomImage(boolean) } method.
  594. *
  595. * @param imagesY the number of images along the Y axis (height).
  596. */
  597. public void setImagesY(int imagesY) {
  598. this.imagesY = imagesY;
  599. particleMesh.setImagesXY(this.imagesX, this.imagesY);
  600. }
  601. /**
  602. * Get the low value of life.
  603. *
  604. * @return the low value of life.
  605. *
  606. * @see ParticleEmitter#setLowLife(float)
  607. */
  608. public float getLowLife() {
  609. return lowLife;
  610. }
  611. /**
  612. * Set the low value of life.
  613. *
  614. * <p>The particle's lifetime/expiration
  615. * is determined by randomly selecting a time between low life and high life.
  616. *
  617. * @param lowLife the low value of life.
  618. */
  619. public void setLowLife(float lowLife) {
  620. this.lowLife = lowLife;
  621. }
  622. /**
  623. * Get the number of particles to spawn per
  624. * second.
  625. *
  626. * @return the number of particles to spawn per
  627. * second.
  628. *
  629. * @see ParticleEmitter#setParticlesPerSec(float)
  630. */
  631. public float getParticlesPerSec() {
  632. return particlesPerSec;
  633. }
  634. /**
  635. * Set the number of particles to spawn per
  636. * second.
  637. *
  638. * @param particlesPerSec the number of particles to spawn per
  639. * second.
  640. */
  641. public void setParticlesPerSec(float particlesPerSec) {
  642. this.particlesPerSec = particlesPerSec;
  643. }
  644. /**
  645. * Get the start color of the particles spawned.
  646. *
  647. * @return the start color of the particles spawned.
  648. *
  649. * @see ParticleEmitter#setStartColor(com.jme3.math.ColorRGBA)
  650. */
  651. public ColorRGBA getStartColor() {
  652. return startColor;
  653. }
  654. /**
  655. * Set the start color of the particles spawned.
  656. *
  657. * <p>The particle color at any time is determined by blending the start color
  658. * and end color based on the particle's current time of life relative
  659. * to its end of life.
  660. *
  661. * @param startColor the start color of the particles spawned
  662. */
  663. public void setStartColor(ColorRGBA startColor) {
  664. this.startColor.set(startColor);
  665. }
  666. /**
  667. * Get the start color of the particles spawned.
  668. *
  669. * @return the start color of the particles spawned.
  670. *
  671. * @see ParticleEmitter#setStartSize(float)
  672. */
  673. public float getStartSize() {
  674. return startSize;
  675. }
  676. /**
  677. * Set the start size of the particles spawned.
  678. *
  679. * <p>The particle size at any time is determined by blending the start size
  680. * and end size based on the particle's current time of life relative
  681. * to its end of life.
  682. *
  683. * @param startSize the start size of the particles spawned.
  684. */
  685. public void setStartSize(float startSize) {
  686. this.startSize = startSize;
  687. }
  688. /**
  689. * @deprecated Use ParticleEmitter.getParticleInfluencer().getInitialVelocity() instead.
  690. */
  691. @Deprecated
  692. public Vector3f getInitialVelocity() {
  693. return particleInfluencer.getInitialVelocity();
  694. }
  695. /**
  696. * @param initialVelocity Set the initial velocity a particle is spawned with,
  697. * the initial velocity given in the parameter will be varied according
  698. * to the velocity variation set in {@link ParticleEmitter#setVelocityVariation(float) }.
  699. * A particle will move toward its velocity unless it is effected by the
  700. * gravity.
  701. *
  702. * @deprecated
  703. * This method is deprecated.
  704. * Use ParticleEmitter.getParticleInfluencer().setInitialVelocity(initialVelocity); instead.
  705. *
  706. * @see ParticleEmitter#setVelocityVariation(float)
  707. * @see ParticleEmitter#setGravity(float)
  708. */
  709. @Deprecated
  710. public void setInitialVelocity(Vector3f initialVelocity) {
  711. this.particleInfluencer.setInitialVelocity(initialVelocity);
  712. }
  713. /**
  714. * @deprecated
  715. * This method is deprecated.
  716. * Use ParticleEmitter.getParticleInfluencer().getVelocityVariation(); instead.
  717. * @return the initial velocity variation factor
  718. */
  719. @Deprecated
  720. public float getVelocityVariation() {
  721. return particleInfluencer.getVelocityVariation();
  722. }
  723. /**
  724. * @param variation Set the variation by which the initial velocity
  725. * of the particle is determined. <code>variation</code> should be a value
  726. * from 0 to 1, where 0 means particles are to spawn with exactly
  727. * the velocity given in {@link ParticleEmitter#setStartVel(com.jme3.math.Vector3f) },
  728. * and 1 means particles are to spawn with a completely random velocity.
  729. *
  730. * @deprecated
  731. * This method is deprecated.
  732. * Use ParticleEmitter.getParticleInfluencer().setVelocityVariation(variation); instead.
  733. */
  734. @Deprecated
  735. public void setVelocityVariation(float variation) {
  736. this.particleInfluencer.setVelocityVariation(variation);
  737. }
  738. // private int newIndex(){
  739. // liveParticles ++;
  740. // return unusedIndices.remove(0);
  741. // if (unusedIndices.size() > 0){
  742. // liveParticles++;
  743. // return unusedIndices.remove(0);
  744. // }else if (next < particles.length){
  745. // liveParticles++;
  746. // return next++;
  747. // }else{
  748. // return -1;
  749. // }
  750. // }
  751. // private void freeIndex(int index){
  752. // liveParticles--;
  753. // if (index == next-1)
  754. // next--;
  755. // else
  756. // assert !unusedIndices.contains(index);
  757. // unusedIndices.add(index);
  758. // }
  759. private boolean emitParticle(Vector3f min, Vector3f max) {
  760. // int idx = newIndex();
  761. // if (idx == -1)
  762. // return false;
  763. int idx = lastUsed + 1;
  764. if (idx >= particles.length) {
  765. return false;
  766. }
  767. Particle p = particles[idx];
  768. if (selectRandomImage) {
  769. p.imageIndex = FastMath.nextRandomInt(0, imagesY - 1) * imagesX + FastMath.nextRandomInt(0, imagesX - 1);
  770. }
  771. p.startlife = lowLife + FastMath.nextRandomFloat() * (highLife - lowLife);
  772. p.life = p.startlife;
  773. p.color.set(startColor);
  774. p.size = startSize;
  775. //shape.getRandomPoint(p.position);
  776. particleInfluencer.influenceParticle(p, shape);
  777. if (worldSpace) {
  778. p.position.addLocal(worldTransform.getTranslation());
  779. }
  780. if (randomAngle) {
  781. p.angle = FastMath.nextRandomFloat() * FastMath.TWO_PI;
  782. }
  783. if (rotateSpeed != 0) {
  784. p.rotateSpeed = rotateSpeed * (0.2f + (FastMath.nextRandomFloat() * 2f - 1f) * .8f);
  785. }
  786. temp.set(p.position).addLocal(p.size, p.size, p.size);
  787. max.maxLocal(temp);
  788. temp.set(p.position).subtractLocal(p.size, p.size, p.size);
  789. min.minLocal(temp);
  790. ++lastUsed;
  791. firstUnUsed = idx + 1;
  792. return true;
  793. }
  794. /**
  795. * Instantly emits all the particles possible to be emitted. Any particles
  796. * which are currently inactive will be spawned immediately.
  797. */
  798. public void emitAllParticles() {
  799. // Force world transform to update
  800. this.getWorldTransform();
  801. TempVars vars = TempVars.get();
  802. BoundingBox bbox = (BoundingBox) this.getMesh().getBound();
  803. Vector3f min = vars.vect1;
  804. Vector3f max = vars.vect2;
  805. bbox.getMin(min);
  806. bbox.getMax(max);
  807. if (!Vector3f.isValidVector(min)) {
  808. min.set(Vector3f.POSITIVE_INFINITY);
  809. }
  810. if (!Vector3f.isValidVector(max)) {
  811. max.set(Vector3f.NEGATIVE_INFINITY);
  812. }
  813. while (emitParticle(min, max));
  814. bbox.setMinMax(min, max);
  815. this.setBoundRefresh();
  816. vars.release();
  817. }
  818. /**
  819. * Instantly kills all active particles, after this method is called, all
  820. * particles will be dead and no longer visible.
  821. */
  822. public void killAllParticles() {
  823. for (int i = 0; i < particles.length; ++i) {
  824. if (particles[i].life > 0) {
  825. this.freeParticle(i);
  826. }
  827. }
  828. }
  829. private void freeParticle(int idx) {
  830. Particle p = particles[idx];
  831. p.life = 0;
  832. p.size = 0f;
  833. p.color.set(0, 0, 0, 0);
  834. p.imageIndex = 0;
  835. p.angle = 0;
  836. p.rotateSpeed = 0;
  837. // freeIndex(idx);
  838. if (idx == lastUsed) {
  839. while (lastUsed >= 0 && particles[lastUsed].life == 0) {
  840. lastUsed--;
  841. }
  842. }
  843. if (idx < firstUnUsed) {
  844. firstUnUsed = idx;
  845. }
  846. }
  847. private void swap(int idx1, int idx2) {
  848. Particle p1 = particles[idx1];
  849. particles[idx1] = particles[idx2];
  850. particles[idx2] = p1;
  851. }
  852. private void updateParticleState(float tpf) {
  853. // Force world transform to update
  854. this.getWorldTransform();
  855. TempVars vars = TempVars.get();
  856. Vector3f min = vars.vect1.set(Vector3f.POSITIVE_INFINITY);
  857. Vector3f max = vars.vect2.set(Vector3f.NEGATIVE_INFINITY);
  858. for (int i = 0; i < particles.length; ++i) {
  859. Particle p = particles[i];
  860. if (p.life == 0) { // particle is dead
  861. // assert i <= firstUnUsed;
  862. continue;
  863. }
  864. p.life -= tpf;
  865. if (p.life <= 0) {
  866. this.freeParticle(i);
  867. continue;
  868. }
  869. // position += velocity * tpf
  870. //p.distToCam = -1;
  871. // applying gravity
  872. p.velocity.x -= gravity.x * tpf;
  873. p.velocity.y -= gravity.y * tpf;
  874. p.velocity.z -= gravity.z * tpf;
  875. temp.set(p.velocity).multLocal(tpf);
  876. p.position.addLocal(temp);
  877. // affecting color, size and angle
  878. float b = (p.startlife - p.life) / p.startlife;
  879. p.color.interpolate(startColor, endColor, b);
  880. p.size = FastMath.interpolateLinear(b, startSize, endSize);
  881. p.angle += p.rotateSpeed * tpf;
  882. // Computing bounding volume
  883. temp.set(p.position).addLocal(p.size, p.size, p.size);
  884. max.maxLocal(temp);
  885. temp.set(p.position).subtractLocal(p.size, p.size, p.size);
  886. min.minLocal(temp);
  887. if (!selectRandomImage) {
  888. p.imageIndex = (int) (b * imagesX * imagesY);
  889. }
  890. if (firstUnUsed < i) {
  891. this.swap(firstUnUsed, i);
  892. if (i == lastUsed) {
  893. lastUsed = firstUnUsed;
  894. }
  895. firstUnUsed++;
  896. }
  897. }
  898. float particlesToEmitF = particlesPerSec * tpf;
  899. int particlesToEmit = (int) particlesToEmitF;
  900. emitCarry += particlesToEmitF - particlesToEmit;
  901. while (emitCarry > 1f) {
  902. ++particlesToEmit;
  903. emitCarry -= 1f;
  904. }
  905. for (int i = 0; i < particlesToEmit; ++i) {
  906. this.emitParticle(min, max);
  907. }
  908. BoundingBox bbox = (BoundingBox) this.getMesh().getBound();
  909. bbox.setMinMax(min, max);
  910. this.setBoundRefresh();
  911. vars.release();
  912. }
  913. /**
  914. * Set to enable or disable the particle emitter
  915. *
  916. * <p>When a particle is
  917. * disabled, it will be "frozen in time" and not update.
  918. *
  919. * @param enabled True to enable the particle emitter
  920. */
  921. public void setEnabled(boolean enabled) {
  922. this.enabled = enabled;
  923. }
  924. /**
  925. * Check if a particle emitter is enabled for update.
  926. *
  927. * @return True if a particle emitter is enabled for update.
  928. *
  929. * @see ParticleEmitter#setEnabled(boolean)
  930. */
  931. public boolean isEnabled() {
  932. return enabled;
  933. }
  934. /**
  935. * Callback from Control.update(), do not use.
  936. * @param tpf
  937. */
  938. public void updateFromControl(float tpf) {
  939. if (enabled) {
  940. this.updateParticleState(tpf);
  941. }
  942. }
  943. /**
  944. * Callback from Control.render(), do not use.
  945. *
  946. * @param rm
  947. * @param vp
  948. */
  949. private void renderFromControl(RenderManager rm, ViewPort vp) {
  950. Camera cam = vp.getCamera();
  951. if (meshType == ParticleMesh.Type.Point) {
  952. float C = cam.getProjectionMatrix().m00;
  953. C *= cam.getWidth() * 0.5f;
  954. // send attenuation params
  955. this.getMaterial().setFloat("Quadratic", C);
  956. }
  957. Matrix3f inverseRotation = Matrix3f.IDENTITY;
  958. TempVars vars = null;
  959. if (!worldSpace) {
  960. vars = TempVars.get();
  961. inverseRotation = this.getWorldRotation().toRotationMatrix(vars.tempMat3).invertLocal();
  962. }
  963. particleMesh.updateParticleData(particles, cam, inverseRotation);
  964. if (!worldSpace) {
  965. vars.release();
  966. }
  967. }
  968. public void preload(RenderManager rm, ViewPort vp) {
  969. this.updateParticleState(0);
  970. particleMesh.updateParticleData(particles, vp.getCamera(), Matrix3f.IDENTITY);
  971. }
  972. @Override
  973. public void write(JmeExporter ex) throws IOException {
  974. super.write(ex);
  975. OutputCapsule oc = ex.getCapsule(this);
  976. oc.write(shape, "shape", DEFAULT_SHAPE);
  977. oc.write(meshType, "meshType", ParticleMesh.Type.Triangle);
  978. oc.write(enabled, "enabled", true);
  979. oc.write(particles.length, "numParticles", 0);
  980. oc.write(particlesPerSec, "particlesPerSec", 0);
  981. oc.write(lowLife, "lowLife", 0);
  982. oc.write(highLife, "highLife", 0);
  983. oc.write(gravity, "gravity", null);
  984. oc.write(imagesX, "imagesX", 1);
  985. oc.write(imagesY, "imagesY", 1);
  986. oc.write(startColor, "startColor", null);
  987. oc.write(endColor, "endColor", null);
  988. oc.write(startSize, "startSize", 0);
  989. oc.write(endSize, "endSize", 0);
  990. oc.write(worldSpace, "worldSpace", false);
  991. oc.write(facingVelocity, "facingVelocity", false);
  992. oc.write(selectRandomImage, "selectRandomImage", false);
  993. oc.write(randomAngle, "randomAngle", false);
  994. oc.write(rotateSpeed, "rotateSpeed", 0);
  995. oc.write(particleInfluencer, "influencer", DEFAULT_INFLUENCER);
  996. }
  997. @Override
  998. public void read(JmeImporter im) throws IOException {
  999. super.read(im);
  1000. InputCapsule ic = im.getCapsule(this);
  1001. shape = (EmitterShape) ic.readSavable("shape", DEFAULT_SHAPE);
  1002. if (shape == DEFAULT_SHAPE) {
  1003. // Prevent reference to static
  1004. shape = shape.deepClone();
  1005. }
  1006. meshType = ic.readEnum("meshType", ParticleMesh.Type.class, ParticleMesh.Type.Triangle);
  1007. int numParticles = ic.readInt("numParticles", 0);
  1008. enabled = ic.readBoolean("enabled", true);
  1009. particlesPerSec = ic.readFloat("particlesPerSec", 0);
  1010. lowLife = ic.readFloat("lowLife", 0);
  1011. highLife = ic.readFloat("highLife", 0);
  1012. gravity = (Vector3f) ic.readSavable("gravity", null);
  1013. imagesX = ic.readInt("imagesX", 1);
  1014. imagesY = ic.readInt("imagesY", 1);
  1015. startColor = (ColorRGBA) ic.readSavable("startColor", null);
  1016. endColor = (ColorRGBA) ic.readSavable("endColor", null);
  1017. startSize = ic.readFloat("startSize", 0);
  1018. endSize = ic.readFloat("endSize", 0);
  1019. worldSpace = ic.readBoolean("worldSpace", false);
  1020. this.setIgnoreTransform(worldSpace);
  1021. facingVelocity = ic.readBoolean("facingVelocity", false);
  1022. selectRandomImage = ic.readBoolean("selectRandomImage", false);
  1023. randomAngle = ic.readBoolean("randomAngle", false);
  1024. rotateSpeed = ic.readFloat("rotateSpeed", 0);
  1025. switch (meshType) {
  1026. case Point:
  1027. particleMesh = new ParticlePointMesh();
  1028. this.setMesh(particleMesh);
  1029. break;
  1030. case Triangle:
  1031. particleMesh = new ParticleTriMesh();
  1032. this.setMesh(particleMesh);
  1033. break;
  1034. default:
  1035. throw new IllegalStateException("Unrecognized particle type: " + meshType);
  1036. }
  1037. this.setNumParticles(numParticles);
  1038. // particleMesh.initParticleData(this, particles.length);
  1039. // particleMesh.setImagesXY(imagesX, imagesY);
  1040. particleInfluencer = (ParticleInfluencer) ic.readSavable("influencer", DEFAULT_INFLUENCER);
  1041. if (particleInfluencer == DEFAULT_INFLUENCER) {
  1042. particleInfluencer = particleInfluencer.clone();
  1043. }
  1044. if (im.getFormatVersion() == 0) {
  1045. // compatibility before the control inside particle emitter
  1046. // was changed:
  1047. // find it in the controls and take it out, then add the proper one in
  1048. for (int i = 0; i < controls.size(); i++) {
  1049. Object obj = controls.get(i);
  1050. if (obj instanceof ParticleEmitter) {
  1051. controls.remove(i);
  1052. // now add the proper one in
  1053. controls.add(new ParticleEmitterControl(this));
  1054. break;
  1055. }
  1056. }
  1057. // compatability before gravity was not a vector but a float
  1058. if (gravity == null) {
  1059. gravity = new Vector3f();
  1060. gravity.y = ic.readFloat("gravity", 0);
  1061. }
  1062. } else {
  1063. // since the parentEmitter is not loaded, it must be
  1064. // loaded separately
  1065. control = getControl(ParticleEmitterControl.class);
  1066. control.parentEmitter = this;
  1067. }
  1068. }
  1069. }