Partage
  • Partager sur Facebook
  • Partager sur Twitter

Génération de Terrain avec des Caves | MINECRAFT

    11 juillet 2019 à 15:27:47

    Hey, je suis entrain de me faire un petit Minecraft-Like(un voxel) et je me confronte avec des problèmes lors de la génération du terrain. En effet, j'utilise le 3D Perlin Noise qui marche relativement bien et qui me génére de belles Caves mais ce dernier est très lent.

    En voici le Code: 

    public class Noise {
    
    	private long seed;
    	private Random rand;
    	private int octave;
    	
    	public Noise(long seed, int octave){
    		this.seed = seed;
    		this.octave = octave;
    		
    		rand = new Random();
    	}
    	
    	public float getNoise(float x, float z, int octave){
    		int xmin = (int) (double) x / octave;
    		if(x < 0) xmin -= 1;
    		int xmax = (int) xmin + 1;
    		int zmin = (int) (double) z / octave;
    		if(z < 0) zmin -= 1;
    		int zmax = (int) zmin + 1;
    		
    		Vector2f a = new Vector2f(xmin, zmin);
    		Vector2f b = new Vector2f(xmax, zmin);
    		Vector2f c = new Vector2f(xmax, zmax);
    		Vector2f d = new Vector2f(xmin, zmax);
    		
    		float ra = (float) noise(a);
    		float rb = (float) noise(b);
    		float rc = (float) noise(c);
    		float rd = (float) noise(d);
    		
    		float t1 = (x - xmin * octave) / octave;
    		float t2 = (z - zmin * octave) / octave;
    		
    		float i1 = interpolate(ra, rb, t1);
    		float i2 = interpolate(rd, rc, t1);
    		
    		float h = interpolate(i1, i2, t2);
    		
    		return h;
    	}
    	
    	//NOISE POUR LES CAVES
    	//C'EST SUREMENT CETTE PARTIE DU CODE QUI EST TRES LENTE
    	public float get3DNoise(float x, float y, float z){
    		float AB = getNoise(x,y, 5);
    		float BC = getNoise(y, z, 5);
    		float AC = getNoise(x, z, 5);
    		
    		float BA = getNoise(y,x, 5);
    		float CB = getNoise(z,y, 5);
    		float CA = getNoise(z,x, 5);
    		
    		float ABC = AB+BC+AC+BA+CB+CA;
    		return ABC / 6f;
    	}
    	
    	private float interpolate(float a, float b, float t){
    		float ft = (float) (t * Math.PI);
    		float f = (float) ((1f - Math.cos(ft)) * 0.5f);
    		float ret = a * (1f - f) + b * f;
    		
    		return ret;
    		
    	}
    	
    	private double noise(Vector2f coord){
    		double var = 10000 * (Math.sin(coord.x + Math.cos(coord.y)) + Math.tan(seed));
    		rand.setSeed((long) var);
    		double ret = rand.nextDouble();
    		
    		return ret;
    	}
    	
    }
    

    Et voici le code de la Génération du Chunk:

    public static List<Block> generateChunk(int posX, int posZ, Noise noise){
    		
    		List<Block> blocks = new ArrayList<Block>();
    	
    		for(int x = 0; x < SIZE; x++){
    			for(int z = 0; z < SIZE; z++){
    				
    				int xx = posX * SIZE + x;
    				int zz = posZ * SIZE + z;
    				int yy = (int) (noise.getNoise(xx+10000, zz+10000, 10)*5f) + 64;
    				
    				for(int y = 0; y < yy; y++){
    					
    					
    					if(noise.get3DNoise(xx, y, zz) < 0.3 && y > 7) continue;
    					
    					Vector3f pos = new Vector3f(xx, y, zz);
    					
    					if(y == yy - 1)blocks.add(new GrassBlock(pos));
    					else blocks.add(new Stone(pos));
    				}
    				
    			}		
    		}
    		return blocks;
    	}

    J'ai entendu parler du Simplex 3D Noise qui serait plus rapide mais je n'est aucune connaissance de cette algorithme. Quelqu'un pourrait m'éclairer ?

    Merci d'avance.

    • Partager sur Facebook
    • Partager sur Twitter

    Génération de Terrain avec des Caves | MINECRAFT

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown