hello_effects.adoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. = jMonkeyEngine 3 Tutorial (12) - Hello Effects
  2. :revnumber: 2.0
  3. :revdate: 2020/07/24
  4. :keywords: beginner, documentation, intro, transparency, effect
  5. [.right]
  6. image::beginner/beginner-effect-fire.png[beginner-effect-fire.png,150,165]
  7. When you see one of the following in a game, then a particle system is likely behind it:
  8. * Fire, flames, sparks;
  9. * Rain, snow, waterfalls, leaves;
  10. * Explosions, debris, shockwaves;
  11. * Dust, fog, clouds, smoke;
  12. * Insects swarms, meteor showers;
  13. * Magic spells.
  14. These scene elements cannot be modeled by meshes. In very simple terms:
  15. * The difference between an explosion and a dust cloud is the speed of the particle effect.
  16. * The difference between flames and a waterfall is the direction and the color of the particle effect.
  17. Particle effects can be animated (e.g. sparks, drops) and static (strands of grass, hair). Non-particle effects include bloom/glow, and motion blur/afterimage. In this tutorial you learn how to make animated particles (com.jme3.effect).
  18. include::partial$add-testdata-tip.adoc[]
  19. == Sample Code
  20. [source,java]
  21. ----
  22. package jme3test.helloworld;
  23. import com.jme3.app.SimpleApplication;
  24. import com.jme3.effect.ParticleEmitter;
  25. import com.jme3.effect.ParticleMesh;
  26. import com.jme3.material.Material;
  27. import com.jme3.math.ColorRGBA;
  28. import com.jme3.math.Vector3f;
  29. /** Sample 11 - how to create fire, water, and explosion effects. */
  30. public class HelloEffects extends SimpleApplication {
  31. public static void main(String[] args) {
  32. HelloEffects app = new HelloEffects();
  33. app.start();
  34. }
  35. @Override
  36. public void simpleInitApp() {
  37. ParticleEmitter fire =
  38. new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
  39. Material mat_red = new Material(assetManager,
  40. "Common/MatDefs/Misc/Particle.j3md");
  41. mat_red.setTexture("Texture", assetManager.loadTexture(
  42. "Effects/Explosion/flame.png"));
  43. fire.setMaterial(mat_red);
  44. fire.setImagesX(2);
  45. fire.setImagesY(2); // 2x2 texture animation
  46. fire.setEndColor( new ColorRGBA(1f, 0f, 0f, 1f)); // red
  47. fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f)); // yellow
  48. fire.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 2, 0));
  49. fire.setStartSize(1.5f);
  50. fire.setEndSize(0.1f);
  51. fire.setGravity(0, 0, 0);
  52. fire.setLowLife(1f);
  53. fire.setHighLife(3f);
  54. fire.getParticleInfluencer().setVelocityVariation(0.3f);
  55. rootNode.attachChild(fire);
  56. ParticleEmitter debris =
  57. new ParticleEmitter("Debris", ParticleMesh.Type.Triangle, 10);
  58. Material debris_mat = new Material(assetManager,
  59. "Common/MatDefs/Misc/Particle.j3md");
  60. debris_mat.setTexture("Texture", assetManager.loadTexture(
  61. "Effects/Explosion/Debris.png"));
  62. debris.setMaterial(debris_mat);
  63. debris.setImagesX(3);
  64. debris.setImagesY(3); // 3x3 texture animation
  65. debris.setRotateSpeed(4);
  66. debris.setSelectRandomImage(true);
  67. debris.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 4, 0));
  68. debris.setStartColor(ColorRGBA.White);
  69. debris.setGravity(0, 6, 0);
  70. debris.getParticleInfluencer().setVelocityVariation(.60f);
  71. rootNode.attachChild(debris);
  72. debris.emitAllParticles();
  73. // ParticleEmitter water =
  74. // new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 20);
  75. // Material mat_blue = new Material(assetManager,
  76. // "Common/MatDefs/Misc/Particle.j3md");
  77. // mat_blue.setTexture("Texture", assetManager.loadTexture(
  78. // "Effects/Explosion/flame.png"));
  79. // water.setMaterial(mat_blue);
  80. // water.setImagesX(2);
  81. // water.setImagesY(2); // 2x2 texture animation
  82. // water.setStartColor( ColorRGBA.Blue);
  83. // water.setEndColor( ColorRGBA.Cyan);
  84. // water.getParticleInfluencer().setInitialVelocity(new Vector3f(0, -4, 0));
  85. // water.setStartSize(1f);
  86. // water.setEndSize(1.5f);
  87. // water.setGravity(0,1,0);
  88. // water.setLowLife(1f);
  89. // water.setHighLife(1f);
  90. // water.getParticleInfluencer().setVelocityVariation(0.1f);
  91. // water.setLocalTranslation(0, 6, 0);
  92. // rootNode.attachChild(water);
  93. }
  94. }
  95. ----
  96. You should see an explosion that sends debris flying, and a fire.
  97. link:https://github.com/jMonkeyEngine/jmonkeyengine/tree/master/jme3-examples/src/main/java/jme3test/effect[More example code is here.]
  98. === Texture Animation and Variation
  99. [.right]
  100. image::beginner/Debris.png[Debris.png,96,96]
  101. Start by choosing a material texture for your effect. If you provide the emitter with a set of textures (see image), it can use them either for variation (random order), or as animation steps (fixed order).
  102. Setting emitter textures works just as you have already learned in previous chapters. This time you base the material on the `Particle.j3md` material definition. Let's have a closer look at the material for the Debris effect.
  103. [source,java]
  104. ----
  105. ParticleEmitter debris =
  106. new ParticleEmitter("Debris", ParticleMesh.Type.Triangle, 10);
  107. Material debris_mat = new Material(assetManager,
  108. "Common/MatDefs/Misc/Particle.j3md");
  109. debris_mat.setTexture("Texture", assetManager.loadTexture(
  110. "Effects/Explosion/Debris.png"));
  111. debris.setMaterial(debris_mat);
  112. debris.setImagesX(3);
  113. debris.setImagesY(3); // 3x3 texture animation
  114. debris.setSelectRandomImage(true);
  115. ...
  116. ----
  117. . Create a material and load the texture.
  118. . Tell the Emitter into how many animation steps (x*y) the texture is divided. +
  119. The debris texture has 3x3 frames.
  120. . Optionally, tell the Emitter whether the animation steps are to be at random, or in order. +
  121. For the debris, the frames play at random.
  122. As you see in the debris example, texture animations improve effects because each "`flame`" or "`piece`" of debris now looks different. Also think of electric or magic effects, where you can create very interesting animations by using an ordered morphing series of lightning bolts; or flying leaves or snow flakes, for instance.
  123. The fire material is created the same way, just using "`Effects/Explosion/flame.png`" texture, which has with 2x2 ordered animation steps.
  124. === Default Particle Textures
  125. The following particle textures are included in `test-data.jar`. You can copy and use them in your own effects.
  126. include::partial$default-particles-table.adoc[]
  127. Copy them into your `assets/Effects` directory to use them.
  128. == Creating Custom Textures
  129. For your game, you will likely create custom particle textures. Look at the fire example again.
  130. [source,java]
  131. ----
  132. ParticleEmitter fire =
  133. new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
  134. Material mat_red = new Material(assetManager,
  135. "Common/MatDefs/Misc/Particle.j3md");
  136. mat_red.setTexture("Texture", assetManager.loadTexture(
  137. "Effects/Explosion/flame.png"));
  138. fire.setMaterial(mat_red);
  139. fire.setImagesX(2);
  140. fire.setImagesY(2); // 2x2 texture animation
  141. fire.setEndColor( new ColorRGBA(1f, 0f, 0f, 1f)); // red
  142. fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f)); // yellow
  143. ----
  144. [.right]
  145. image::beginner/flame.png[flame.png,96,96]
  146. Compare the texture with the resulting effect.
  147. * Black parts of the image become fully transparent.
  148. * White/gray parts of the image are translucent and get colorized.
  149. * You set the color using `setStartColor()` and `setEndColor()`. +
  150. For fire, it's a gradient from yellow to red.
  151. * By default, the animation is played in order and loops.
  152. Create a grayscale texture in a graphic editor, and save it to your `assets/Effects` directory. If you split up one image file into x*y animation steps, make sure each square is of equal size–just as you see in the examples here.
  153. === Emitter Parameters
  154. A particle system is always centered around an emitter.
  155. Use the `setShape()` method to change the EmitterShape:
  156. * EmitterPointShape(Vector3f.ZERO) – particles emit from a point (default)
  157. * EmitterSphereShape(Vector3f.ZERO,2f) – particles emit from a sphere-sized area
  158. * EmitterBoxShape(new Vector3f(-1f,-1f,-1f),new Vector3f(1f,1f,1f)) – particles emit from a box-sized area
  159. Example:
  160. [source,java]
  161. ----
  162. emitter.setShape(new EmitterPointShape(Vector3f.ZERO));
  163. ----
  164. You create different effects by changing the emitter parameters:
  165. [cols="10,50,15,25", options="header"]
  166. |===
  167. <a| Parameter
  168. a| Method
  169. a| Default
  170. a| Description
  171. <a| number
  172. a| `setNumParticles()`
  173. a| N/A
  174. a| The maximum number of particles visible at the same time. Value is specified by user in constructor. This influences the density and length of the "`trail`".
  175. <a| velocity
  176. <a| `getParticleInfluencer().setInitialVelocity()`
  177. a| Vector3f.ZERO
  178. a| Specify a vector how fast particles move and in which start direction.
  179. <a| direction
  180. a| `getParticleInfluencer().setVelocityVariation()` +
  181. `setFacingVelocity()` +
  182. `setRandomAngle()` +
  183. `setFaceNormal()` +
  184. `setRotateSpeed()`
  185. a| 0.2f +
  186. false +
  187. false +
  188. Vector3f.NAN +
  189. 0.0f
  190. a| Optional accessors that control in which direction particles face while flying.
  191. <a| lifetime
  192. a| `setLowLife()` +
  193. `setHighLife()`
  194. <a| 3f +
  195. 7f
  196. a| Minimum and maximum time period before particles fade.
  197. <a| emission rate
  198. a| `setParticlesPerSec()`
  199. a| 20
  200. a| How many new particles are emitted per second.
  201. <a| color
  202. a| `setStartColor()` +
  203. `setEndColor()`
  204. a| gray
  205. a| Set to the same colors, or to two different colors for a gradient effect.
  206. <a| size
  207. a| `setStartSize()` +
  208. `setEndSize()`
  209. a| 0.2f +
  210. 2f
  211. a| Set to two different values for shrink/grow effect, or to same size for constant effect.
  212. <a| gravity
  213. a| `setGravity()`
  214. a| 0,1,0
  215. a| Whether particles fall down (positive) or fly up (negative). Set to 0f for a zero-g effect where particles keep flying.
  216. |===
  217. You can find details about <<core:effect/particle_emitters.adoc#configure_parameters,effect parameters>> here.
  218. Add and modify one parameter at a time, and try different values until you get the effect you want.
  219. [TIP]
  220. ====
  221. Use the SceneComposer in the jMonkeyEngine SDK to create effects more easily. Create an empty scene and add an emitter object to it. Change the emitter properties and watch the outcome live. You can save created effects as .j3o file and load them like scenes or models.
  222. ====
  223. == Exercise
  224. Can you "`invert`" the fire effect into a small waterfall? Here some tips:
  225. * Change the Red and Yellow color to Cyan and Blue
  226. * Invert the velocity vector (direction) by using a negative number
  227. * Swap start and end size
  228. * Activate gravity by setting it to 0,1,0
  229. == Conclusion
  230. You have learned that many different effects can be created by changing the parameters and textures of one general emitter object.