water.adoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. = Simple Water
  2. :revnumber: 2.0
  3. :revdate: 2020/07/22
  4. jMonkeyEngine offers a SimpleWaterProcessor that turns any quad (flat rectangle) into a reflective water surface with waves. You can use this quad for simple limited water surfaces such as water troughs, shallow fountains, puddles, shallow water in channels. The SimpleWaterProcessor has less performance impact on your game than the full featured xref:effect/post-processor_water.adoc[SeaMonkey WaterFilter]; the main difference is that the SimpleWaterProcessor does not include under-water effects.
  5. Here is some background info for JME3's basic water implementation:
  6. // * link:http://www.jmonkeyengine.com/forum/index.php?topic=14740.0[http://www.jmonkeyengine.com/forum/index.php?topic=14740.0]
  7. * link:https://www.bonzaisoftware.com/tnp/gl-water-tutorial/[https://www.bonzaisoftware.com/tnp/gl-water-tutorial/]
  8. // * link:http://www.gametutorials.com/Articles/RealisticWater.pdf[http://www.gametutorials.com/Articles/RealisticWater.pdf]
  9. // image::http://www.jmonkeyengine.com/wp-content/uploads/2010/10/simplewaterdemo.jpg[simplewaterdemo.jpg,width="277",height="180",align="center"]
  10. == SimpleWaterProcessor
  11. A JME3 scene with water can use a `com.jme3.water.SimpleWaterProcessor` (which implements the SceneProcessor interface).
  12. To achieve a water effect, JME3 uses shaders and a special material, `Common/MatDefs/Water/SimpleWater.j3md`. The water surface is a quad, and we use normal map and dU/dV map texturing to simulate the waves.
  13. . Every frame, we render to three texture maps:
  14. ** For the water surface (reflection), we take a snapshot of the environment, flip it upside down, and clip it to the visible water surface. Note that we do not actually use a "`water`" texture color map: The "`texture`" of the water is solely a distorted reflection.
  15. ** For the "`wavy`" distortion (refraction), we use the derivative of a normal map, a dU/dV map.
  16. ** For the fogginess of water (depth) we use a depth map from the terrains z-buffer.
  17. . In the shaders, we add all of the texture maps together.
  18. ** For the "`bumpy`" displacement of the waves, we use a normal map and a du/dv map that are shifted against each other over time to create the wave effect.
  19. ** For the light reflection vectors on the water surface, we use the Fresnel formula, together with normal vectors.
  20. ** We add specular lighting.
  21. . (For the underwater caustics effect, we use splatted textures. – WIP/TODO)
  22. == Usage
  23. [.right.text-left]
  24. image::effect/simplewater.png[simplewater.png,width="384",height="288",align="right"]
  25. . Create a `mainScene` Node
  26. .. Attach the `mainScene` Node to the `rootNode`
  27. . Load your `scene` Spatial
  28. .. Add a light source to the `scene` Spatial
  29. .. Attach the `scene` Spatial to the `mainScene` Node
  30. . Load your xref:util/sky.adoc[sky] Geometry
  31. .. Attach the sky Geometry to the `mainScene` Node
  32. . Create the SimpleWaterProcessor `waterProcessor`
  33. .. Set the processor's ReflectionScene to the `mainScene` Spatial (!)
  34. .. Set the processor's Plane to where you want your water surface to be
  35. .. Set the processor's WaterDepth, DistortionScale, and WaveSpeed
  36. .. Attach the processor to the `viewPort`
  37. . Create a Quad `quad`
  38. .. Set the quad's TextureCoordinates to specify the size of the waves
  39. . Create a `water` Geometry from the Quad
  40. .. Set the water's translation and rotation (same Y value as Plane above!)
  41. .. Set the water's material to the processor's output material
  42. .. Attach the `water` Geometry to the `rootNode`. (Not to the mainScene!)
  43. == Sample Code
  44. The sample code can be found in `link:https://github.com/jMonkeyEngine/jmonkeyengine/blob/445f7ed010199d30c484fe75bacef4b87f2eb38e/jme3-examples/src/main/java/jme3test/water/TestSimpleWater.java[TestSimpleWater.java]` and `link:https://github.com/jMonkeyEngine/jmonkeyengine/blob/445f7ed010199d30c484fe75bacef4b87f2eb38e/jme3-examples/src/main/java/jme3test/water/TestSceneWater.java[TestSceneWater.java]`.
  45. Here is the most important part of the code:
  46. [source,java]
  47. ----
  48. // we create a water processor
  49. SimpleWaterProcessor waterProcessor = new SimpleWaterProcessor(assetManager);
  50. waterProcessor.setReflectionScene(mainScene);
  51. // we set the water plane
  52. Vector3f waterLocation=new Vector3f(0,-6,0);
  53. waterProcessor.setPlane(new Plane(Vector3f.UNIT_Y, waterLocation.dot(Vector3f.UNIT_Y)));
  54. viewPort.addProcessor(waterProcessor);
  55. // we set wave properties
  56. waterProcessor.setWaterDepth(40); // transparency of water
  57. waterProcessor.setDistortionScale(0.05f); // strength of waves
  58. waterProcessor.setWaveSpeed(0.05f); // speed of waves
  59. // we define the wave size by setting the size of the texture coordinates
  60. Quad quad = new Quad(400,400);
  61. quad.scaleTextureCoordinates(new Vector2f(6f,6f));
  62. // we create the water geometry from the quad
  63. Geometry water=new Geometry("water", quad);
  64. water.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
  65. water.setLocalTranslation(-200, -6, 250);
  66. water.setShadowMode(ShadowMode.Receive);
  67. water.setMaterial(waterProcessor.getMaterial());
  68. rootNode.attachChild(water);
  69. ----
  70. == Settings
  71. You can lower the render size to gain higher performance:
  72. [source,java]
  73. ----
  74. waterProcessor.setRenderSize(128,128);
  75. ----
  76. The deeper the water, the more transparent. (?)
  77. [source,java]
  78. ----
  79. waterProcessor.setWaterDepth(40);
  80. ----
  81. A higher distortion scale makes bigger waves.
  82. [source,java]
  83. ----
  84. waterProcessor.setDistortionScale(0.05f);
  85. ----
  86. A lower wave speed makes calmer water.
  87. [source,java]
  88. ----
  89. waterProcessor.setWaveSpeed(0.05f);
  90. ----
  91. If your scene does not have a lightsource, you can set the light direction for the water:
  92. [source,java]
  93. ----
  94. waterProcessor.setLightDirection( new Vector3f(0.55f, -0.82f, 0.15f));
  95. ----
  96. Instead of creating a quad and specifying a plane, you can get a default waterplane from the processor:
  97. [source,java]
  98. ----
  99. Geometry waterPlane = waterProcessor.createWaterGeometry(10, 10);
  100. waterPlane.setLocalTranslation(-5, 0, 5);
  101. waterPlane.setMaterial(waterProcessor.getMaterial());
  102. ----
  103. You can offer a switch to set the water Material to a static texture – for users with slow PCs.