hello_effects.adoc 9.5 KB

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