Advertisement
ComputerCraft32

Terrian

Jul 16th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.97 KB | None | 0 0
  1. package mygame;
  2.  
  3. import com.jayfella.fastnoise.FastNoise;
  4. import com.jayfella.fastnoise.LayeredNoise;
  5. import com.jayfella.fastnoise.NoiseLayer;
  6. import com.jme3.app.SimpleApplication;
  7. import com.jme3.light.AmbientLight;
  8. import com.jme3.light.DirectionalLight;
  9. import com.jme3.math.ColorRGBA;
  10. import com.jme3.math.Vector2f;
  11. import com.jme3.math.Vector3f;
  12. import com.jme3.scene.Node;
  13. import com.rvandoosselaer.blocks.Block;
  14. import com.rvandoosselaer.blocks.BlockIds;
  15. import com.rvandoosselaer.blocks.BlocksConfig;
  16. import com.rvandoosselaer.blocks.Chunk;
  17. import com.rvandoosselaer.blocks.ChunkGenerator;
  18. import com.rvandoosselaer.blocks.ChunkManager;
  19. import com.rvandoosselaer.blocks.ChunkManagerState;
  20. import com.rvandoosselaer.blocks.ChunkPagerState;
  21. import com.rvandoosselaer.blocks.PagerListener;
  22. import com.simsilica.mathd.Vec3i;
  23.  
  24.  
  25.  
  26. import java.util.Random;
  27.  
  28. /**
  29.  * An application that renders a terrain generated from noise.
  30.  *
  31.  * Default key mappings:
  32.  * print camera position:            c
  33.  * print direct memory information:  m
  34.  * toggle wireframe:                 p
  35.  * toggle profiler:                  F6
  36.  *
  37.  * @author rvandoosselaer
  38.  */
  39. public class Main extends SimpleApplication {
  40.  
  41.     public static void main(String[] args) {
  42.  
  43.  
  44.         Main proceduralTerrain = new Main();
  45.         proceduralTerrain.start();
  46.     }
  47.  
  48.    
  49.     @Override
  50.     public void simpleInitApp() {
  51.        
  52.         AmbientLight light = new AmbientLight(new ColorRGBA(2, 2, 2, 2));
  53.         rootNode.addLight(light);
  54.        
  55.         DirectionalLight light1 = new DirectionalLight(new Vector3f(-1, 0, 0));
  56.         rootNode.addLight(light1);
  57.        
  58.         BlocksConfig.initialize(assetManager);
  59.  
  60.         BlocksConfig.getInstance().setGrid(new Vec3i(11, 1, 11));
  61.         BlocksConfig.getInstance().setChunkSize(new Vec3i(32, 256, 32));
  62.  
  63.         ChunkManager chunkManager = ChunkManager.builder()
  64.                 .generatorPoolSize(2)
  65.                 .meshPoolSize(2)
  66.                 .generator(new ChunkNoiseGenerator(System.currentTimeMillis()))
  67.                 .triggerAdjacentChunkUpdates(true)
  68.                 .build();
  69.  
  70.         stateManager.attachAll(new ChunkManagerState(chunkManager), new ChunkPagerState(rootNode, chunkManager));
  71.         stateManager.getState(ChunkPagerState.class).getChunkPager().addListener(new PagerListenerOutput());
  72.  
  73.         hideCursor();
  74. //
  75. //        Spatial sky = SkyFactory.createSky(assetManager, "Scenes/Beach/FullskiesSunset0068.dds", SkyFactory.EnvMapType.CubeMap);
  76. //        rootNode.attachChild(sky);
  77.  
  78.         viewPort.setBackgroundColor(new ColorRGBA(0.5f, 0.6f, 0.7f, 1.0f));
  79.         flyCam.setMoveSpeed(10f);
  80.         cam.setLocation(new Vector3f(0, 256, 0));
  81.         cam.lookAt(new Vector3f(128, 128, 128), Vector3f.UNIT_Y);
  82.     }
  83.  
  84.     @Override
  85.     public void simpleUpdate(float tpf) {
  86.         stateManager.getState(ChunkPagerState.class).setLocation(new Vector3f(cam.getLocation().x, 255, cam.getLocation().z));
  87.     }
  88.  
  89.     private void hideCursor() {
  90.        
  91.         inputManager.setCursorVisible(false);
  92.     }
  93.  
  94.     private static class PagerListenerOutput implements PagerListener<Node> {
  95.  
  96.         @Override
  97.         public void onPageDetached(Vec3i location, Node page) {
  98.             System.out.println("Chunk detached - " + location + " - " + page);
  99.         }
  100.  
  101.         @Override
  102.         public void onPageAttached(Vec3i location, Node page) {
  103.             System.out.println("Chunk attached - " + location + " - " + page);
  104.         }
  105.  
  106.         @Override
  107.         public void onPageUpdated(Vec3i location, Node oldPage, Node newPage) {
  108.             System.out.println("Chunk updated - " + location + " - " + " old page: " + oldPage + ", new page: " + newPage);
  109.         }
  110.  
  111.     }
  112.  
  113.     private static class ChunkNoiseGenerator implements ChunkGenerator {
  114.  
  115.         private final long seed;
  116.         private LayeredNoise layeredNoise;
  117.         private float waterHeight = 30f;
  118.  
  119.         public ChunkNoiseGenerator(long seed) {
  120.             this.seed = seed;
  121.             createWorldNoise();
  122.         }
  123.  
  124.         @Override
  125.         public Chunk generate(Vec3i location) {
  126.             Vec3i chunkSize = BlocksConfig.getInstance().getChunkSize();
  127.  
  128.             Chunk chunk = Chunk.createAt(location);
  129.             for (int x = 0; x < BlocksConfig.getInstance().getChunkSize().x; x++) {
  130.                 for (int y = 0; y < BlocksConfig.getInstance().getChunkSize().y; y++) {
  131.                     for (int z = 0; z < BlocksConfig.getInstance().getChunkSize().z; z++) {
  132.                         float height = getHeight(getWorldLocation(new Vector3f(x, 0, z), chunk));
  133.                        
  134.  
  135.                        
  136.                         for ( int i = 0; i <= height; i++) {
  137.                             Block block = BlocksConfig.getInstance().getBlockRegistry().get(BlockIds.GRASS);//worldY <= height ? BlocksConfig.getInstance().getBlockRegistry().get(BlockIds.GRASS) : BlocksConfig.getInstance().getBlockRegistry().get(BlockIds.WATER);
  138.                             chunk.addBlock(x, y, z, block);
  139.                         }  
  140.                     }
  141.                 }
  142.             }
  143.             return chunk;
  144.         }
  145.  
  146.         private Vector3f getWorldLocation(Vector3f blockLocation, Chunk chunk) {
  147.             Vec3i chunkSize = BlocksConfig.getInstance().getChunkSize();
  148.  
  149.             return new Vector3f((chunk.getLocation().x * chunkSize.x) + blockLocation.x,
  150.                     (chunk.getLocation().y * chunkSize.y) + blockLocation.y,
  151.                     (chunk.getLocation().z * chunkSize.z) + blockLocation.z);
  152.         }
  153.  
  154.         private void createWorldNoise() {
  155.             Random random = new Random(seed);
  156.             layeredNoise = new LayeredNoise();
  157.  
  158. //            layeredNoise.setHardFloor(true);
  159. //            layeredNoise.setHardFloorHeight(waterHeight);
  160. //            layeredNoise.setHardFloorStrength(0.6f);
  161.  
  162.             NoiseLayer mountains = new NoiseLayer("mountains");
  163.             mountains.setSeed(random.nextInt());
  164.             mountains.setNoiseType(FastNoise.NoiseType.PerlinFractal);
  165.             mountains.setStrength(128);
  166.             mountains.setFrequency(mountains.getFrequency() / 4);
  167.             layeredNoise.addLayer(mountains);
  168.  
  169.             NoiseLayer hills = new NoiseLayer("Hills");
  170.             hills.setSeed(random.nextInt());
  171.             hills.setNoiseType(FastNoise.NoiseType.PerlinFractal);
  172.             hills.setStrength(64);
  173.             hills.setFrequency(hills.getFrequency() / 2);
  174.             layeredNoise.addLayer(hills);
  175.  
  176.             NoiseLayer details = new NoiseLayer("Details");
  177.             details.setSeed(random.nextInt());
  178.             details.setNoiseType(FastNoise.NoiseType.PerlinFractal);
  179.             details.setStrength(15);
  180.             layeredNoise.addLayer(details);
  181.         }
  182.  
  183.         private float getHeight(Vector3f blockLocation) {
  184.          //   float height = 32f;
  185.             return layeredNoise.evaluate(new Vector2f(blockLocation.x, blockLocation.z));// + height;
  186.         }
  187.  
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement