Guest User

Untitled

a guest
Jun 3rd, 2014
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package mygame;
  6.  
  7. import com.jme3.app.SimpleApplication;
  8. import com.jme3.input.controls.ActionListener;
  9. import com.jme3.light.AmbientLight;
  10. import com.jme3.light.DirectionalLight;
  11. import com.jme3.material.Material;
  12. import com.jme3.math.ColorRGBA;
  13. import com.jme3.math.Vector2f;
  14. import com.jme3.math.Vector3f;
  15. import com.jme3.renderer.RenderManager;
  16. import com.jme3.renderer.ViewPort;
  17. import com.jme3.scene.control.AbstractControl;
  18. import com.jme3.terrain.geomipmap.TerrainLodControl;
  19. import com.jme3.terrain.geomipmap.TerrainQuad;
  20. import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
  21. import com.jme3.terrain.heightmap.AbstractHeightMap;
  22. import com.jme3.terrain.heightmap.ImageBasedHeightMap;
  23. import com.jme3.texture.Texture;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28.  
  29.  
  30. public class BugReport extends SimpleApplication
  31. implements ActionListener {
  32.  
  33.  
  34. private TerrainQuad terrain;
  35. Material matTerrain;
  36. private float grassScale = 64;
  37. private float dirtScale = 16;
  38. private float rockScale = 128;
  39.  
  40. public static void main(String[] args) {
  41. BugReport app = new BugReport();
  42. app.setShowSettings(false);
  43. app.start();
  44. }
  45.  
  46. public void simpleInitApp() {
  47.  
  48. createTerrain();
  49.  
  50. DirectionalLight light = new DirectionalLight();
  51. light.setDirection((new Vector3f(-0.5f, -1f, -0.5f)).normalize());
  52. rootNode.addLight(light);
  53.  
  54. AmbientLight ambLight = new AmbientLight();
  55. ambLight.setColor(new ColorRGBA(1f, 1f, 0.8f, 0.2f));
  56. rootNode.addLight(ambLight);
  57. cam.setLocation(new Vector3f(0,-60f,0));
  58.  
  59. new Thread(new Runnable(){public void run(){
  60. try {
  61. Thread.sleep(1000);
  62. } catch (InterruptedException ex) {
  63. Logger.getLogger(BugReport.class.getName()).log(Level.SEVERE, null, ex);
  64. }
  65. terrain.addControl(new RullalControl(new Vector3f(-0.1f,-0.5f,0f), terrain,BugReport.this));
  66. }}).start();
  67.  
  68. }
  69.  
  70. public void onAction(String binding, boolean isPressed, float tpf) {
  71. }
  72.  
  73. @Override
  74. public void simpleUpdate(float tpf) {
  75. }
  76.  
  77. public void rulla(Vector3f loc){
  78. }
  79.  
  80. private void createTerrain() {
  81. // First, we load up our textures and the heightmap texture for the terrain
  82.  
  83. // TERRAIN TEXTURE material
  84. matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
  85. matTerrain.setBoolean("useTriPlanarMapping", false);
  86. matTerrain.setBoolean("WardIso", true);
  87. matTerrain.setFloat("Shininess", 0);
  88.  
  89. // ALPHA map (for splat textures)
  90. matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
  91.  
  92. // GRASS texture
  93. Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
  94. grass.setWrap(Texture.WrapMode.Repeat);
  95. matTerrain.setTexture("DiffuseMap", grass);
  96. matTerrain.setFloat("DiffuseMap_0_scale", grassScale);
  97.  
  98. // DIRT texture
  99. Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
  100. dirt.setWrap(Texture.WrapMode.Repeat);
  101. matTerrain.setTexture("DiffuseMap_1", dirt);
  102. matTerrain.setFloat("DiffuseMap_1_scale", dirtScale);
  103.  
  104. // ROCK texture
  105. Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
  106. rock.setWrap(Texture.WrapMode.Repeat);
  107. matTerrain.setTexture("DiffuseMap_2", rock);
  108. matTerrain.setFloat("DiffuseMap_2_scale", rockScale);
  109.  
  110. // HEIGHTMAP image (for the terrain heightmap)
  111. Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
  112. AbstractHeightMap heightmap = null;
  113. try {
  114. heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.5f);
  115. heightmap.load();
  116. heightmap.smooth(0.9f, 1);
  117.  
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. }
  121.  
  122. // CREATE THE TERRAIN
  123. terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
  124. TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
  125. control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) ); // patch size, and a multiplier
  126. terrain.addControl(control);
  127. terrain.setMaterial(matTerrain);
  128. terrain.setLocalTranslation(0, -100, 0);
  129. terrain.setLocalScale(2.5f, 0.5f, 2.5f);
  130. rootNode.attachChild(terrain);
  131. }
  132.  
  133.  
  134.  
  135.  
  136. class RullalControl extends AbstractControl{
  137.  
  138. Vector3f loc;
  139. TerrainQuad terrain;
  140. BugReport app;
  141. float radius=64;
  142. float tpf=0;
  143. List<Vector2f> locs = new ArrayList<Vector2f>();
  144. List<Float> heights = new ArrayList<Float>();
  145. float height=10;
  146.  
  147. public RullalControl(Vector3f loc, TerrainQuad terrain,BugReport app){
  148. this.loc=loc;
  149. this.terrain=terrain;
  150. this.app=app;
  151. //start = System.currentTimeMillis();
  152. }
  153.  
  154. @Override
  155. protected void controlUpdate(float tpf) {
  156.  
  157. //long tTime=System.currentTimeMillis()-start;
  158. //radius-=(tpf);
  159.  
  160. // offset it by radius because in the loop we iterate through 2 radii
  161. int radiusStepsX = (int) (radius / terrain.getLocalScale().x);
  162. int radiusStepsZ = (int) (radius / terrain.getLocalScale().z);
  163.  
  164. float xStepAmount = terrain.getLocalScale().x;
  165. float zStepAmount = terrain.getLocalScale().z;
  166.  
  167. for (int z = -radiusStepsZ; z < radiusStepsZ; z++) {
  168. for (int x = -radiusStepsX; x < radiusStepsX; x++) {
  169.  
  170. float locX = loc.x + (x * xStepAmount);
  171. float locZ = loc.z + (z * zStepAmount);
  172.  
  173. // see if it is in the radius of the tool
  174. float hLocal=terrain.getHeight(new Vector2f(locX, locZ));
  175. if ((hLocal-height)>=1){
  176. heights.add(hLocal-0.0001f);
  177. }
  178. else if((hLocal-height)<0){
  179. heights.add(hLocal+0.0001f);
  180. }
  181. else heights.add(height);
  182. //float h = calculateHeightRulla(radius, height, locX - loc.x, locZ - loc.z);
  183. locs.add(new Vector2f(locX, locZ));
  184. //heights.add(height);
  185. }
  186. }
  187. terrain.setHeight(locs, heights);
  188.  
  189.  
  190. //System.out.println("Modified "+locs.size()+" points, took: " + (System.currentTimeMillis() - start)+" ms");
  191. heights.clear();
  192. locs.clear();
  193. terrain.updateModelBound();
  194. this.tpf+=tpf;
  195. //if (this.tpf>5){
  196. spatial.removeControl(this);
  197. //}
  198. }
  199. @Override
  200. protected void controlRender(RenderManager rm, ViewPort vp) {
  201. //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  202. }
  203.  
  204. }
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment