Voici une version améliorée de la classe TerrainGenerator avec une configuration plus sophistiquée des bruits :
#include <FastNoiseLite.h>
#include <array>
#include <cmath>
#include <algorithm>
class TerrainGenerator {
private:
// Terrain height generation
FastNoiseLite continentalNoise; // Large scale terrain features
FastNoiseLite erosionNoise; // Medium scale erosion
FastNoiseLite peaksNoise; // Mountain peaks
// Biome generation
FastNoiseLite temperatureNoise; // Temperature map
FastNoiseLite humidityNoise; // Humidity map
// Cave generation
FastNoiseLite caveNoise; // Main cave system
FastNoiseLite wormsNoise; // Worm caves
// Ore generation
FastNoiseLite oreNoise; // Ore distribution
static constexpr int SEA_LEVEL = 64;
static constexpr int MAX_HEIGHT = 200;
static constexpr int MIN_HEIGHT = 40;
public:
TerrainGenerator(int seed) {
// Continental noise - large scale terrain features
continentalNoise.SetSeed(seed);
continentalNoise.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
continentalNoise.SetFractalType(FastNoiseLite::FractalType_FBm);
continentalNoise.SetFractalOctaves(6);
continentalNoise.SetFractalLacunarity(2.0f);
continentalNoise.SetFractalGain(0.5f);
continentalNoise.SetFrequency(0.001f);
// Erosion noise - medium scale details
erosionNoise.SetSeed(seed + 1);
erosionNoise.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
erosionNoise.SetFractalType(FastNoiseLite::FractalType_Ridged);
erosionNoise.SetFractalOctaves(4);
erosionNoise.SetFractalLacunarity(2.5f);
erosionNoise.SetFractalGain(0.4f);
erosionNoise.SetFrequency(0.004f);
// Peaks noise - sharp mountain features
peaksNoise.SetSeed(seed + 2);
peaksNoise.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
peaksNoise.SetFractalType(FastNoiseLite::FractalType_PingPong);
peaksNoise.SetFractalOctaves(3);
peaksNoise.SetFractalLacunarity(3.0f);
peaksNoise.SetFractalGain(0.3f);
peaksNoise.SetFrequency(0.008f);
// Temperature map
temperatureNoise.SetSeed(seed + 3);
temperatureNoise.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
temperatureNoise.SetFractalType(FastNoiseLite::FractalType_FBm);
temperatureNoise.SetFractalOctaves(4);
temperatureNoise.SetFrequency(0.0008f);
// Humidity map
humidityNoise.SetSeed(seed + 4);
humidityNoise.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
humidityNoise.SetFractalType(FastNoiseLite::FractalType_FBm);
humidityNoise.SetFractalOctaves(4);
humidityNoise.SetFrequency(0.0009f);
// Cave system noise
caveNoise.SetSeed(seed + 5);
caveNoise.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2S);
caveNoise.SetFractalType(FastNoiseLite::FractalType_FBm);
caveNoise.SetFractalOctaves(3);
caveNoise.SetFrequency(0.03f);
// Worm caves noise
wormsNoise.SetSeed(seed + 6);
wormsNoise.SetNoiseType(FastNoiseLite::NoiseType_Cellular);
wormsNoise.SetCellularDistanceFunction(FastNoiseLite::CellularDistanceFunction_EuclideanSq);
wormsNoise.SetCellularReturnType(FastNoiseLite::CellularReturnType_Distance2);
wormsNoise.SetFrequency(0.02f);
// Ore distribution
oreNoise.SetSeed(seed + 7);
oreNoise.SetNoiseType(FastNoiseLite::NoiseType_Value);
oreNoise.SetFractalType(FastNoiseLite::FractalType_FBm);
oreNoise.SetFractalOctaves(2);
oreNoise.SetFrequency(0.1f);
}
float getHeightAt(int x, int z) {
// Continental shelf
float continental = continentalNoise.GetNoise((float)x, (float)z);
continental = (continental + 1.0f) * 0.5f; // Normalize to 0-1
// Erosion
float erosion = erosionNoise.GetNoise((float)x, (float)z);
erosion = (erosion + 1.0f) * 0.5f; // Normalize to 0-1
// Peaks and valleys
float peaks = peaksNoise.GetNoise((float)x, (float)z);
peaks = std::max(0.0f, peaks); // Only positive values for peaks
// Combine noises for final height
float baseHeight = continental * 0.4f + (1.0f - erosion) * 0.3f;
float mountainFactor = std::pow(std::max(0.0f, continental - 0.6f) * 2.5f, 2.0f);
float finalHeight = baseHeight + peaks * mountainFactor * 0.3f;
// Scale to world height
return MIN_HEIGHT + finalHeight * (MAX_HEIGHT - MIN_HEIGHT);
}
BiomeType getBiomeAt(int x, int z, float height) {
float temperature = temperatureNoise.GetNoise((float)x, (float)z);
float humidity = humidityNoise.GetNoise((float)x, (float)z);
// Height influence on temperature
float heightInfluence = (height - SEA_LEVEL) / (float)(MAX_HEIGHT - SEA_LEVEL);
temperature -= heightInfluence * 0.5f;
// Biome determination based on temperature and humidity
if (temperature < -0.5f) {
return BiomeType::SNOWY;
} else if (height > SEA_LEVEL + 60) {
return BiomeType::MOUNTAINS;
} else if (humidity < -0.3f && temperature > 0.3f) {
return BiomeType::DESERT;
} else if (humidity > 0.2f && temperature > -0.2f) {
return BiomeType::FOREST;
} else {
return BiomeType::PLAINS;
}
}
bool isCave(int x, int y, int z) {
// Main cave system
float cave = caveNoise.GetNoise((float)x, (float)y, (float)z);
// Worm caves
float worms = wormsNoise.GetNoise((float)x * 0.5f, (float)y * 0.5f, (float)z * 0.5f);
// Cave density decreases near surface
float depthFactor = 1.0f - std::pow((float)y / CHUNK_HEIGHT, 2.0f);
return (cave > 0.65f || worms < 0.05f) && depthFactor > 0.3f && y > 5;
}
TextureType getOreType(int x, int y, int z) {
float oreValue = oreNoise.GetNoise((float)x, (float)y, (float)z);
float rarity = oreNoise.GetNoise((float)x * 3.14f, (float)y * 2.71f, (float)z * 1.41f);
if (oreValue > 0.9f) {
if (y < 16 && rarity > 0.95f) return TextureType::DIAMOND_ORE;
else if (y < 20 && rarity > 0.9f) return TextureType::EMERALD_ORE;
else if (y < 32 && rarity > 0.85f) return TextureType::GOLD_ORE;
else if (y < 40 && rarity > 0.8f) return TextureType::REDSTONE_ORE;
else if (y < 50 && rarity > 0.75f) return TextureType::LAPIS_ORE;
else if (y < 64 && rarity > 0.6f) return TextureType::IRON_ORE;
else if (y < 96 && rarity > 0.5f) return TextureType::COPPER_ORE;
else if (rarity > 0.3f) return TextureType::COAL_ORE;
}
return TextureType::STONE;
}
std::array<Voxel, CHUNK_VOLUME> generateChunk(int chunkX, int chunkZ) {
std::array<Voxel, CHUNK_VOLUME> chunk{};
for (int x = 0; x < CHUNK_SIZE; x++) {
for (int z = 0; z < CHUNK_SIZE; z++) {
int worldX = chunkX + x;
int worldZ = chunkZ + z;
float height = getHeightAt(worldX, worldZ);
BiomeType biome = getBiomeAt(worldX, worldZ, height);
for (int y = 0; y < CHUNK_HEIGHT; y++) {
int index = y * CHUNK_SIZE * CHUNK_SIZE + z * CHUNK_SIZE + x;
if (y > height) {
// Above ground
chunk[index].type = (y <= SEA_LEVEL) ? TextureType::WATER : TextureType::AIR;
} else if (y == 0) {
// Bedrock layer
chunk[index].type = TextureType::BEDROCK;
} else if (y < 3 && oreNoise.GetNoise((float)worldX, (float)y, (float)worldZ) > 0.3f) {
// Bedrock mix near bottom
chunk[index].type = TextureType::BEDROCK;
} else if (isCave(worldX, y, worldZ)) {
// Cave system
chunk[index].type = TextureType::AIR;
} else {
// Underground layers
int depthFromSurface = (int)height - y;
if (depthFromSurface == 0) {
// Surface block
switch (biome) {
case BiomeType::DESERT:
chunk[index].type = TextureType::SAND;
break;
case BiomeType::SNOWY:
case BiomeType::MOUNTAINS:
chunk[index].type = (height > SEA_LEVEL + 80) ? TextureType::SNOW : TextureType::STONE;
break;
default:
chunk[index].type = (y > SEA_LEVEL - 2) ? TextureType::GRASS_TOP : TextureType::DIRT;
}
} else if (depthFromSurface <= 3) {
// Subsurface
switch (biome) {
case BiomeType::DESERT:
chunk[index].type = TextureType::SAND;
break;
case BiomeType::SNOWY:
case BiomeType::MOUNTAINS:
chunk[index].type = (depthFromSurface == 1) ? TextureType::DIRT : TextureType::STONE;
break;
default:
chunk[index].type = TextureType::DIRT;
}
} else if (depthFromSurface <= 6) {
// Transition layer
chunk[index].type = (biome == BiomeType::DESERT) ? TextureType::SANDSTONE : TextureType::STONE;
} else {
// Deep underground - stone and ores
chunk[index].type = getOreType(worldX, y, worldZ);
}
}
}
}
}
return chunk;
}
};
Cette version améliorée utilise :
-
Plusieurs couches de bruit pour le terrain :
- Continental noise (OpenSimplex2 + FBm) pour les grandes formations
- Erosion noise (Perlin + Ridged) pour l'érosion naturelle
- Peaks noise (OpenSimplex2 + PingPong) pour les pics montagneux
-
Système de biomes basé sur température/humidité :
- Cartes de température et d'humidité séparées
- Influence de l'altitude sur la température
-
Génération de caves avancée :
- Caves principales avec OpenSimplex2S
- Caves "vers" avec bruit cellulaire
- Densité variable selon la profondeur
-
Distribution réaliste des minerais :
- Différentes raretés selon la profondeur
- Utilisation de bruit Value pour la distribution
-
Paramètres fractals optimisés :
- Octaves, lacunarité et gain ajustés pour chaque type de bruit
- Différentes fréquences pour créer de la variété
Cette configuration produit un terrain plus réaliste et varié avec des transitions naturelles entre les biomes.