hello_effects.adoc 11 KB

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