TerrainGridTest.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package jme3test.terrain;
  2. import com.jme3.app.SimpleApplication;
  3. import com.jme3.app.state.ScreenshotAppState;
  4. import com.jme3.asset.plugins.HttpZipLocator;
  5. import com.jme3.asset.plugins.ZipLocator;
  6. import com.jme3.bullet.BulletAppState;
  7. import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
  8. import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape;
  9. import com.jme3.bullet.control.CharacterControl;
  10. import com.jme3.bullet.control.RigidBodyControl;
  11. import com.jme3.input.KeyInput;
  12. import com.jme3.input.controls.ActionListener;
  13. import com.jme3.input.controls.KeyTrigger;
  14. import com.jme3.light.DirectionalLight;
  15. import com.jme3.material.Material;
  16. import com.jme3.math.ColorRGBA;
  17. import com.jme3.math.Vector3f;
  18. import com.jme3.terrain.geomipmap.TerrainGrid;
  19. import com.jme3.terrain.geomipmap.TerrainGridListener;
  20. import com.jme3.terrain.geomipmap.TerrainGridLodControl;
  21. import com.jme3.terrain.geomipmap.TerrainLodControl;
  22. import com.jme3.terrain.geomipmap.TerrainQuad;
  23. import com.jme3.terrain.geomipmap.grid.ImageTileLoader;
  24. import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
  25. import com.jme3.terrain.heightmap.Namer;
  26. import com.jme3.texture.Texture;
  27. import com.jme3.texture.Texture.WrapMode;
  28. import java.io.File;
  29. public class TerrainGridTest extends SimpleApplication {
  30. private Material mat_terrain;
  31. private TerrainGrid terrain;
  32. private float grassScale = 64;
  33. private float dirtScale = 16;
  34. private float rockScale = 128;
  35. private boolean usePhysics = false;
  36. private boolean physicsAdded = false;
  37. public static void main(final String[] args) {
  38. TerrainGridTest app = new TerrainGridTest();
  39. app.start();
  40. }
  41. private CharacterControl player3;
  42. @Override
  43. public void simpleInitApp() {
  44. File file = new File("TerrainGridTestData.zip");
  45. if (!file.exists()) {
  46. assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/TerrainGridTestData.zip", HttpZipLocator.class);
  47. } else {
  48. assetManager.registerLocator("TerrainGridTestData.zip", ZipLocator.class);
  49. }
  50. this.flyCam.setMoveSpeed(100f);
  51. ScreenshotAppState state = new ScreenshotAppState();
  52. this.stateManager.attach(state);
  53. // TERRAIN TEXTURE material
  54. this.mat_terrain = new Material(this.assetManager, "Common/MatDefs/Terrain/HeightBasedTerrain.j3md");
  55. // Parameters to material:
  56. // regionXColorMap: X = 1..4 the texture that should be appliad to state X
  57. // regionX: a Vector3f containing the following information:
  58. // regionX.x: the start height of the region
  59. // regionX.y: the end height of the region
  60. // regionX.z: the texture scale for the region
  61. // it might not be the most elegant way for storing these 3 values, but it packs the data nicely :)
  62. // slopeColorMap: the texture to be used for cliffs, and steep mountain sites
  63. // slopeTileFactor: the texture scale for slopes
  64. // terrainSize: the total size of the terrain (used for scaling the texture)
  65. // GRASS texture
  66. Texture grass = this.assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
  67. grass.setWrap(WrapMode.Repeat);
  68. this.mat_terrain.setTexture("region1ColorMap", grass);
  69. this.mat_terrain.setVector3("region1", new Vector3f(88, 200, this.grassScale));
  70. // DIRT texture
  71. Texture dirt = this.assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
  72. dirt.setWrap(WrapMode.Repeat);
  73. this.mat_terrain.setTexture("region2ColorMap", dirt);
  74. this.mat_terrain.setVector3("region2", new Vector3f(0, 90, this.dirtScale));
  75. // ROCK texture
  76. Texture rock = this.assetManager.loadTexture("Textures/Terrain/Rock2/rock.jpg");
  77. rock.setWrap(WrapMode.Repeat);
  78. this.mat_terrain.setTexture("region3ColorMap", rock);
  79. this.mat_terrain.setVector3("region3", new Vector3f(198, 260, this.rockScale));
  80. this.mat_terrain.setTexture("region4ColorMap", rock);
  81. this.mat_terrain.setVector3("region4", new Vector3f(198, 260, this.rockScale));
  82. this.mat_terrain.setTexture("slopeColorMap", rock);
  83. this.mat_terrain.setFloat("slopeTileFactor", 32);
  84. this.mat_terrain.setFloat("terrainSize", 129);
  85. this.terrain = new TerrainGrid("terrain", 65, 257, new ImageTileLoader(assetManager, new Namer() {
  86. public String getName(int x, int y) {
  87. return "Scenes/TerrainMountains/terrain_" + x + "_" + y + ".png";
  88. }
  89. }));
  90. this.terrain.setMaterial(mat_terrain);
  91. this.terrain.setLocalTranslation(0, 0, 0);
  92. this.terrain.setLocalScale(1f, 1f, 1f);
  93. this.rootNode.attachChild(this.terrain);
  94. TerrainLodControl control = new TerrainGridLodControl(this.terrain, getCamera());
  95. control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) ); // patch size, and a multiplier
  96. this.terrain.addControl(control);
  97. final BulletAppState bulletAppState = new BulletAppState();
  98. stateManager.attach(bulletAppState);
  99. this.getCamera().setLocation(new Vector3f(0, 400, 0));
  100. this.getCamera().lookAt(new Vector3f(0,0,0), Vector3f.UNIT_Y);
  101. this.viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
  102. DirectionalLight light = new DirectionalLight();
  103. light.setDirection((new Vector3f(-0.5f, -1f, -0.5f)).normalize());
  104. rootNode.addLight(light);
  105. if (usePhysics) {
  106. CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(0.5f, 1.8f, 1);
  107. player3 = new CharacterControl(capsuleShape, 0.5f);
  108. player3.setJumpSpeed(20);
  109. player3.setFallSpeed(10);
  110. player3.setGravity(10);
  111. player3.setPhysicsLocation(new Vector3f(cam.getLocation().x, 256, cam.getLocation().z));
  112. bulletAppState.getPhysicsSpace().add(player3);
  113. terrain.addListener(new TerrainGridListener() {
  114. public void gridMoved(Vector3f newCenter) {
  115. }
  116. public void tileAttached(Vector3f cell, TerrainQuad quad) {
  117. while(quad.getControl(RigidBodyControl.class)!=null){
  118. quad.removeControl(RigidBodyControl.class);
  119. }
  120. quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), terrain.getLocalScale()), 0));
  121. bulletAppState.getPhysicsSpace().add(quad);
  122. }
  123. public void tileDetached(Vector3f cell, TerrainQuad quad) {
  124. if (quad.getControl(RigidBodyControl.class) != null) {
  125. bulletAppState.getPhysicsSpace().remove(quad);
  126. quad.removeControl(RigidBodyControl.class);
  127. }
  128. }
  129. });
  130. }
  131. this.initKeys();
  132. }
  133. private void initKeys() {
  134. // You can map one or several inputs to one named action
  135. this.inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
  136. this.inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
  137. this.inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
  138. this.inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
  139. this.inputManager.addMapping("Jumps", new KeyTrigger(KeyInput.KEY_SPACE));
  140. this.inputManager.addListener(this.actionListener, "Lefts");
  141. this.inputManager.addListener(this.actionListener, "Rights");
  142. this.inputManager.addListener(this.actionListener, "Ups");
  143. this.inputManager.addListener(this.actionListener, "Downs");
  144. this.inputManager.addListener(this.actionListener, "Jumps");
  145. }
  146. private boolean left;
  147. private boolean right;
  148. private boolean up;
  149. private boolean down;
  150. private final ActionListener actionListener = new ActionListener() {
  151. @Override
  152. public void onAction(final String name, final boolean keyPressed, final float tpf) {
  153. if (name.equals("Lefts")) {
  154. if (keyPressed) {
  155. TerrainGridTest.this.left = true;
  156. } else {
  157. TerrainGridTest.this.left = false;
  158. }
  159. } else if (name.equals("Rights")) {
  160. if (keyPressed) {
  161. TerrainGridTest.this.right = true;
  162. } else {
  163. TerrainGridTest.this.right = false;
  164. }
  165. } else if (name.equals("Ups")) {
  166. if (keyPressed) {
  167. TerrainGridTest.this.up = true;
  168. } else {
  169. TerrainGridTest.this.up = false;
  170. }
  171. } else if (name.equals("Downs")) {
  172. if (keyPressed) {
  173. TerrainGridTest.this.down = true;
  174. } else {
  175. TerrainGridTest.this.down = false;
  176. }
  177. } else if (name.equals("Jumps")) {
  178. TerrainGridTest.this.player3.jump();
  179. }
  180. }
  181. };
  182. private final Vector3f walkDirection = new Vector3f();
  183. @Override
  184. public void simpleUpdate(final float tpf) {
  185. Vector3f camDir = this.cam.getDirection().clone().multLocal(0.6f);
  186. Vector3f camLeft = this.cam.getLeft().clone().multLocal(0.4f);
  187. this.walkDirection.set(0, 0, 0);
  188. if (this.left) {
  189. this.walkDirection.addLocal(camLeft);
  190. }
  191. if (this.right) {
  192. this.walkDirection.addLocal(camLeft.negate());
  193. }
  194. if (this.up) {
  195. this.walkDirection.addLocal(camDir);
  196. }
  197. if (this.down) {
  198. this.walkDirection.addLocal(camDir.negate());
  199. }
  200. if (usePhysics) {
  201. this.player3.setWalkDirection(this.walkDirection);
  202. this.cam.setLocation(this.player3.getPhysicsLocation());
  203. }
  204. }
  205. }