TerrainTestAdvanced.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 jme3test.terrain;
  33. import com.jme3.app.SimpleApplication;
  34. import com.jme3.bounding.BoundingBox;
  35. import com.jme3.font.BitmapText;
  36. import com.jme3.input.KeyInput;
  37. import com.jme3.input.controls.ActionListener;
  38. import com.jme3.input.controls.KeyTrigger;
  39. import com.jme3.light.DirectionalLight;
  40. import com.jme3.light.PointLight;
  41. import com.jme3.material.Material;
  42. import com.jme3.math.ColorRGBA;
  43. import com.jme3.math.Vector3f;
  44. import com.jme3.scene.Geometry;
  45. import com.jme3.scene.Node;
  46. import com.jme3.scene.Spatial;
  47. import com.jme3.scene.debug.Arrow;
  48. import com.jme3.terrain.geomipmap.TerrainLodControl;
  49. import com.jme3.terrain.geomipmap.TerrainQuad;
  50. import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
  51. import com.jme3.terrain.heightmap.AbstractHeightMap;
  52. import com.jme3.terrain.heightmap.ImageBasedHeightMap;
  53. import com.jme3.texture.Texture;
  54. import com.jme3.texture.Texture.WrapMode;
  55. import com.jme3.util.SkyFactory;
  56. /**
  57. * Uses the terrain's lighting texture with normal maps and lights.
  58. *
  59. * @author bowens
  60. */
  61. public class TerrainTestAdvanced extends SimpleApplication {
  62. private TerrainQuad terrain;
  63. Material matTerrain;
  64. Material matWire;
  65. boolean wireframe = false;
  66. boolean triPlanar = false;
  67. boolean wardiso = false;
  68. boolean minnaert = false;
  69. protected BitmapText hintText;
  70. PointLight pl;
  71. Geometry lightMdl;
  72. private float grassScale = 64;
  73. private float dirtScale = 16;
  74. private float rockScale = 128;
  75. public static void main(String[] args) {
  76. TerrainTestAdvanced app = new TerrainTestAdvanced();
  77. app.start();
  78. }
  79. @Override
  80. public void initialize() {
  81. super.initialize();
  82. loadHintText();
  83. }
  84. @Override
  85. public void simpleInitApp() {
  86. setupKeys();
  87. // First, we load up our textures and the heightmap texture for the terrain
  88. // TERRAIN TEXTURE material
  89. matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
  90. matTerrain.setBoolean("useTriPlanarMapping", false);
  91. matTerrain.setFloat("Shininess", 0.0f);
  92. // ALPHA map (for splat textures)
  93. matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alpha1.png"));
  94. matTerrain.setTexture("AlphaMap_1", assetManager.loadTexture("Textures/Terrain/splat/alpha2.png"));
  95. // this material also supports 'AlphaMap_2', so you can get up to 12 diffuse textures
  96. // HEIGHTMAP image (for the terrain heightmap)
  97. Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
  98. // DIRT texture, Diffuse textures 0 to 3 use the first AlphaMap
  99. Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
  100. dirt.setWrap(WrapMode.Repeat);
  101. matTerrain.setTexture("DiffuseMap", dirt);
  102. matTerrain.setFloat("DiffuseMap_0_scale", dirtScale);
  103. // GRASS texture
  104. Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
  105. grass.setWrap(WrapMode.Repeat);
  106. matTerrain.setTexture("DiffuseMap_1", grass);
  107. matTerrain.setFloat("DiffuseMap_1_scale", grassScale);
  108. // ROCK texture
  109. Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
  110. rock.setWrap(WrapMode.Repeat);
  111. matTerrain.setTexture("DiffuseMap_2", rock);
  112. matTerrain.setFloat("DiffuseMap_2_scale", rockScale);
  113. // BRICK texture
  114. Texture brick = assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg");
  115. brick.setWrap(WrapMode.Repeat);
  116. matTerrain.setTexture("DiffuseMap_3", brick);
  117. matTerrain.setFloat("DiffuseMap_3_scale", rockScale);
  118. // RIVER ROCK texture, this texture will use the next alphaMap: AlphaMap_1
  119. Texture riverRock = assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg");
  120. riverRock.setWrap(WrapMode.Repeat);
  121. matTerrain.setTexture("DiffuseMap_4", riverRock);
  122. matTerrain.setFloat("DiffuseMap_4_scale", rockScale);
  123. // diffuse textures 4 to 7 use AlphaMap_1
  124. // diffuse textures 8 to 11 use AlphaMap_2
  125. Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg");
  126. normalMap0.setWrap(WrapMode.Repeat);
  127. Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png");
  128. normalMap1.setWrap(WrapMode.Repeat);
  129. Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png");
  130. normalMap2.setWrap(WrapMode.Repeat);
  131. //matTerrain.setTexture("NormalMap", normalMap0);
  132. matTerrain.setTexture("NormalMap_1", normalMap2);
  133. matTerrain.setTexture("NormalMap_2", normalMap2);
  134. matTerrain.setTexture("NormalMap_4", normalMap2);
  135. createSky();
  136. // CREATE HEIGHTMAP
  137. AbstractHeightMap heightmap = null;
  138. try {
  139. heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.5f);
  140. heightmap.load();
  141. heightmap.smooth(0.9f, 1);
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. }
  145. /*
  146. * Here we create the actual terrain. The tiles will be 65x65, and the total size of the
  147. * terrain will be 513x513. It uses the heightmap we created to generate the height values.
  148. */
  149. /**
  150. * Optimal terrain patch size is 65 (64x64).
  151. * The total size is up to you. At 1025 it ran fine for me (200+FPS), however at
  152. * size=2049, it got really slow. But that is a jump from 2 million to 8 million triangles...
  153. */
  154. terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());//, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations
  155. TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
  156. control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) ); // patch size, and a multiplier
  157. terrain.addControl(control);
  158. terrain.setMaterial(matTerrain);
  159. terrain.setModelBound(new BoundingBox());
  160. terrain.updateModelBound();
  161. terrain.setLocalTranslation(0, -100, 0);
  162. terrain.setLocalScale(1f, 1f, 1f);
  163. rootNode.attachChild(terrain);
  164. //Material debugMat = assetManager.loadMaterial("Common/Materials/VertexColor.j3m");
  165. //terrain.generateDebugTangents(debugMat);
  166. DirectionalLight light = new DirectionalLight();
  167. light.setDirection((new Vector3f(-0.1f, -0.1f, -0.1f)).normalize());
  168. rootNode.addLight(light);
  169. cam.setLocation(new Vector3f(0, 10, -10));
  170. cam.lookAtDirection(new Vector3f(0, -1.5f, -1).normalizeLocal(), Vector3f.UNIT_Y);
  171. flyCam.setMoveSpeed(400);
  172. rootNode.attachChild(createAxisMarker(20));
  173. }
  174. public void loadHintText() {
  175. hintText = new BitmapText(guiFont, false);
  176. hintText.setSize(guiFont.getCharSet().getRenderedSize());
  177. hintText.setLocalTranslation(0, getCamera().getHeight(), 0);
  178. hintText.setText("Hit T to switch to wireframe, P to switch to tri-planar texturing");
  179. guiNode.attachChild(hintText);
  180. }
  181. private void setupKeys() {
  182. flyCam.setMoveSpeed(50);
  183. inputManager.addMapping("wireframe", new KeyTrigger(KeyInput.KEY_T));
  184. inputManager.addListener(actionListener, "wireframe");
  185. inputManager.addMapping("triPlanar", new KeyTrigger(KeyInput.KEY_P));
  186. inputManager.addListener(actionListener, "triPlanar");
  187. inputManager.addMapping("WardIso", new KeyTrigger(KeyInput.KEY_9));
  188. inputManager.addListener(actionListener, "WardIso");
  189. inputManager.addMapping("Minnaert", new KeyTrigger(KeyInput.KEY_0));
  190. inputManager.addListener(actionListener, "Minnaert");
  191. }
  192. private ActionListener actionListener = new ActionListener() {
  193. public void onAction(String name, boolean pressed, float tpf) {
  194. if (name.equals("wireframe") && !pressed) {
  195. wireframe = !wireframe;
  196. if (!wireframe) {
  197. terrain.setMaterial(matWire);
  198. } else {
  199. terrain.setMaterial(matTerrain);
  200. }
  201. } else if (name.equals("triPlanar") && !pressed) {
  202. triPlanar = !triPlanar;
  203. if (triPlanar) {
  204. matTerrain.setBoolean("useTriPlanarMapping", true);
  205. // planar textures don't use the mesh's texture coordinates but real world coordinates,
  206. // so we need to convert these texture coordinate scales into real world scales so it looks
  207. // the same when we switch to/from tr-planar mode
  208. matTerrain.setFloat("DiffuseMap_0_scale", 1f / (float) (512f / grassScale));
  209. matTerrain.setFloat("DiffuseMap_1_scale", 1f / (float) (512f / dirtScale));
  210. matTerrain.setFloat("DiffuseMap_2_scale", 1f / (float) (512f / rockScale));
  211. matTerrain.setFloat("DiffuseMap_3_scale", 1f / (float) (512f / rockScale));
  212. matTerrain.setFloat("DiffuseMap_4_scale", 1f / (float) (512f / rockScale));
  213. } else {
  214. matTerrain.setBoolean("useTriPlanarMapping", false);
  215. matTerrain.setFloat("DiffuseMap_0_scale", grassScale);
  216. matTerrain.setFloat("DiffuseMap_1_scale", dirtScale);
  217. matTerrain.setFloat("DiffuseMap_2_scale", rockScale);
  218. matTerrain.setFloat("DiffuseMap_3_scale", rockScale);
  219. matTerrain.setFloat("DiffuseMap_4_scale", rockScale);
  220. }
  221. }
  222. }
  223. };
  224. private void createSky() {
  225. Texture west = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_west.jpg");
  226. Texture east = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_east.jpg");
  227. Texture north = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_north.jpg");
  228. Texture south = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_south.jpg");
  229. Texture up = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_up.jpg");
  230. Texture down = assetManager.loadTexture("Textures/Sky/Lagoon/lagoon_down.jpg");
  231. Spatial sky = SkyFactory.createSky(assetManager, west, east, north, south, up, down);
  232. rootNode.attachChild(sky);
  233. }
  234. protected Node createAxisMarker(float arrowSize) {
  235. Material redMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  236. redMat.getAdditionalRenderState().setWireframe(true);
  237. redMat.setColor("Color", ColorRGBA.Red);
  238. Material greenMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  239. greenMat.getAdditionalRenderState().setWireframe(true);
  240. greenMat.setColor("Color", ColorRGBA.Green);
  241. Material blueMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  242. blueMat.getAdditionalRenderState().setWireframe(true);
  243. blueMat.setColor("Color", ColorRGBA.Blue);
  244. Node axis = new Node();
  245. // create arrows
  246. Geometry arrowX = new Geometry("arrowX", new Arrow(new Vector3f(arrowSize, 0, 0)));
  247. arrowX.setMaterial(redMat);
  248. Geometry arrowY = new Geometry("arrowY", new Arrow(new Vector3f(0, arrowSize, 0)));
  249. arrowY.setMaterial(greenMat);
  250. Geometry arrowZ = new Geometry("arrowZ", new Arrow(new Vector3f(0, 0, arrowSize)));
  251. arrowZ.setMaterial(blueMat);
  252. axis.attachChild(arrowX);
  253. axis.attachChild(arrowY);
  254. axis.attachChild(arrowZ);
  255. //axis.setModelBound(new BoundingBox());
  256. return axis;
  257. }
  258. }