TerrainGridTest.java 9.3 KB

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