TerrainFractalGridTest.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package jme3test.terrain;
  2. import com.jme3.app.SimpleApplication;
  3. import com.jme3.app.state.ScreenshotAppState;
  4. import com.jme3.material.Material;
  5. import com.jme3.math.ColorRGBA;
  6. import com.jme3.math.Quaternion;
  7. import com.jme3.math.Vector3f;
  8. import com.jme3.terrain.geomipmap.TerrainGrid;
  9. import com.jme3.terrain.geomipmap.TerrainGridLodControl;
  10. import com.jme3.terrain.geomipmap.TerrainLodControl;
  11. import com.jme3.terrain.geomipmap.grid.FractalTileLoader;
  12. import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
  13. import com.jme3.terrain.noise.ShaderUtils;
  14. import com.jme3.terrain.noise.basis.FilteredBasis;
  15. import com.jme3.terrain.noise.filter.IterativeFilter;
  16. import com.jme3.terrain.noise.filter.OptimizedErode;
  17. import com.jme3.terrain.noise.filter.PerturbFilter;
  18. import com.jme3.terrain.noise.filter.SmoothFilter;
  19. import com.jme3.terrain.noise.fractal.FractalSum;
  20. import com.jme3.terrain.noise.modulator.NoiseModulator;
  21. import com.jme3.texture.Texture;
  22. import com.jme3.texture.Texture.WrapMode;
  23. public class TerrainFractalGridTest extends SimpleApplication {
  24. final private float grassScale = 64;
  25. final private float dirtScale = 16;
  26. final private float rockScale = 128;
  27. public static void main(final String[] args) {
  28. TerrainFractalGridTest app = new TerrainFractalGridTest();
  29. app.start();
  30. }
  31. @Override
  32. public void simpleInitApp() {
  33. this.flyCam.setMoveSpeed(100f);
  34. ScreenshotAppState state = new ScreenshotAppState();
  35. this.stateManager.attach(state);
  36. // TERRAIN TEXTURE material
  37. Material mat_terrain = new Material(this.assetManager,
  38. "Common/MatDefs/Terrain/HeightBasedTerrain.j3md");
  39. // Parameters to material:
  40. // regionXColorMap: X = 1..4 the texture that should be appliad to state X
  41. // regionX: a Vector3f containing the following information:
  42. // regionX.x: the start height of the region
  43. // regionX.y: the end height of the region
  44. // regionX.z: the texture scale for the region
  45. // it might not be the most elegant way for storing these 3 values, but it packs the data nicely :)
  46. // slopeColorMap: the texture to be used for cliffs, and steep mountain sites
  47. // slopeTileFactor: the texture scale for slopes
  48. // terrainSize: the total size of the terrain (used for scaling the texture)
  49. // GRASS texture
  50. Texture grass = this.assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
  51. grass.setWrap(WrapMode.Repeat);
  52. mat_terrain.setTexture("region1ColorMap", grass);
  53. mat_terrain.setVector3("region1", new Vector3f(15, 200, this.grassScale));
  54. // DIRT texture
  55. Texture dirt = this.assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
  56. dirt.setWrap(WrapMode.Repeat);
  57. mat_terrain.setTexture("region2ColorMap", dirt);
  58. mat_terrain.setVector3("region2", new Vector3f(0, 20, this.dirtScale));
  59. // ROCK texture
  60. Texture rock = this.assetManager.loadTexture("Textures/Terrain/Rock2/rock.jpg");
  61. rock.setWrap(WrapMode.Repeat);
  62. mat_terrain.setTexture("region3ColorMap", rock);
  63. mat_terrain.setVector3("region3", new Vector3f(198, 260, this.rockScale));
  64. mat_terrain.setTexture("region4ColorMap", rock);
  65. mat_terrain.setVector3("region4", new Vector3f(198, 260, this.rockScale));
  66. mat_terrain.setTexture("slopeColorMap", rock);
  67. mat_terrain.setFloat("slopeTileFactor", 32);
  68. mat_terrain.setFloat("terrainSize", 513);
  69. FractalSum base = new FractalSum();
  70. base.setRoughness(0.7f);
  71. base.setFrequency(1.0f);
  72. base.setAmplitude(1.0f);
  73. base.setLacunarity(2.12f);
  74. base.setOctaves(8);
  75. base.setScale(0.02125f);
  76. base.addModulator(new NoiseModulator() {
  77. @Override
  78. public float value(float... in) {
  79. return ShaderUtils.clamp(in[0] * 0.5f + 0.5f, 0, 1);
  80. }
  81. });
  82. FilteredBasis ground = new FilteredBasis(base);
  83. PerturbFilter perturb = new PerturbFilter();
  84. perturb.setMagnitude(0.119f);
  85. OptimizedErode therm = new OptimizedErode();
  86. therm.setRadius(5);
  87. therm.setTalus(0.011f);
  88. SmoothFilter smooth = new SmoothFilter();
  89. smooth.setRadius(1);
  90. smooth.setEffect(0.7f);
  91. IterativeFilter iterate = new IterativeFilter();
  92. iterate.addPreFilter(perturb);
  93. iterate.addPostFilter(smooth);
  94. iterate.setFilter(therm);
  95. iterate.setIterations(1);
  96. ground.addPreFilter(iterate);
  97. TerrainGrid terrain
  98. = new TerrainGrid("terrain", 33, 129, new FractalTileLoader(ground, 256f));
  99. terrain.setMaterial(mat_terrain);
  100. terrain.setLocalTranslation(0, 0, 0);
  101. terrain.setLocalScale(2f, 1f, 2f);
  102. this.rootNode.attachChild(terrain);
  103. TerrainLodControl control
  104. = new TerrainGridLodControl(terrain, this.getCamera());
  105. control.setLodCalculator(new DistanceLodCalculator(33, 2.7f)); // patch size, and a multiplier
  106. terrain.addControl(control);
  107. this.getCamera().setLocation(new Vector3f(0, 300, 0));
  108. cam.setRotation(new Quaternion(0.51176f, -0.14f, 0.085f, 0.84336f));
  109. this.viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
  110. }
  111. @Override
  112. public void simpleUpdate(final float tpf) {
  113. }
  114. }