Voici une version affinée de la classe TerrainGenerator qui utilise FastNoiseLite avec plusieurs configurations de bruit (OpenSimplex2, Perlin, fractal FBm/Ridged, heightmap…) pour générer des chunks de terrain procédural.
Je suis parti du principe que vous avez déjà un type Voxel contenant au moins un membre TextureType type;.
#pragma once
#include <FastNoiseLite.h>
#include <array>
#include <cstdint>
// Vos enums
enum BiomeType
{
DESERT,
FOREST,
PLAINS,
MOUNTAINS,
SNOWY
};
enum TextureType
{
BEDROCK,
BRICKS,
COBBLESTONE,
DIRT,
GLASS,
GRASS_TOP,
GRASS_SIDE,
GRAVEL,
OAK_LEAVES,
OAK_LOG_TOP,
OAK_LOG,
OAK_PLANKS,
SAND,
SNOW,
STONE_BRICKS,
STONE,
COAL_ORE,
COPPER_ORE,
DIAMOND_ORE,
EMERALD_ORE,
GOLD_ORE,
IRON_ORE,
LAPIS_ORE,
REDSTONE_ORE,
WATER,
COUNT,
AIR
};
// Structure de base d'un voxel
struct Voxel
{
TextureType type = TextureType::AIR;
};
class TerrainGenerator
{
public:
static constexpr int CHUNK_SIZE = 16;
static constexpr int CHUNK_HEIGHT = 256;
static constexpr int CHUNK_VOLUME = CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT;
TerrainGenerator(uint32_t seed = 1337)
{
// —————————————————————————————————————————————————————————————
// Configuration du bruit de hauteur (terrain principal)
// —————————————————————————————————————————————————————————————
heightNoise.SetSeed(seed + 0);
heightNoise.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
heightNoise.SetFrequency(1.0f / 100.0f);
heightNoise.SetFractalType(FastNoiseLite::FractalType_FBm);
heightNoise.SetFractalOctaves(6);
heightNoise.SetFractalLacunarity(2.2f);
heightNoise.SetFractalGain(0.5f);
// —————————————————————————————————————————————————————————————
// Configuration du bruit de biome
// —————————————————————————————————————————————————————————————
biomeNoise.SetSeed(seed + 1);
biomeNoise.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
biomeNoise.SetFrequency(1.0f / 500.0f);
biomeNoise.SetFractalType(FastNoiseLite::FractalType_Ridged);
biomeNoise.SetFractalOctaves(4);
biomeNoise.SetFractalLacunarity(2.0f);
biomeNoise.SetFractalGain(0.8f);
// —————————————————————————————————————————————————————————————
// Configuration du bruit de cavernes
// —————————————————————————————————————————————————————————————
caveNoise.SetSeed(seed + 2);
caveNoise.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
caveNoise.SetFrequency(1.0f / 50.0f);
caveNoise.SetFractalType(FastNoiseLite::FractalType_FBm);
caveNoise.SetFractalOctaves(3);
caveNoise.SetFractalLacunarity(2.5f);
caveNoise.SetFractalGain(0.7f);
}
// Génère un chunk complet et renvoie un tableau de Voxel
std::array<Voxel, CHUNK_VOLUME> generateChunk(int chunkX, int chunkZ)
{
std::array<Voxel, CHUNK_VOLUME> chunk{};
const int SEA_LEVEL = 64;
const int MOUNTAIN_HEIGHT = 120;
for (int lx = 0; lx < CHUNK_SIZE; lx++)
for (int lz = 0; lz < CHUNK_SIZE; lz++)
{
int worldX = chunkX + lx;
int worldZ = chunkZ + lz;
// 1) Choix du biome
BiomeType biome = getBiomeAt(worldX, worldZ);
// 2) Calcul de la hauteur de surface
float h = heightNoise.GetNoise((float)worldX, (float)worldZ);
int surfaceY = SEA_LEVEL;
switch (biome)
{
case BiomeType::MOUNTAINS: surfaceY += int(h * MOUNTAIN_HEIGHT); break;
case BiomeType::FOREST:
case BiomeType::PLAINS: surfaceY += int(h * 20); break;
case BiomeType::DESERT: surfaceY += int(h * 15); break;
case BiomeType::SNOWY: surfaceY += int(h * 25); break;
}
// Clamp
if (surfaceY < 1) surfaceY = 1;
if (surfaceY >= CHUNK_HEIGHT) surfaceY = CHUNK_HEIGHT - 1;
// 3) Remplissage voxel par voxel
for (int y = 0; y < CHUNK_HEIGHT; y++)
{
int index = y * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
Voxel &voxel = chunk[index];
// 3a) Au-dessus de la surface : eau ou air
if (y > surfaceY)
{
voxel.type = (y <= SEA_LEVEL) ? TextureType::WATER : TextureType::AIR;
continue;
}
// 3b) Carve caves
float c = caveNoise.GetNoise((float)worldX, (float)y, (float)worldZ);
bool isCave = (c > 0.55f) && (y < surfaceY - 5) && (y > 10);
if (isCave)
{
voxel.type = TextureType::AIR;
continue;
}
// 3c) Sol, sous-sol et minerais
if (y == 0)
{
voxel.type = TextureType::BEDROCK;
}
else if (y == surfaceY)
{
// top block selon biome
switch (biome)
{
case BiomeType::DESERT: voxel.type = TextureType::SAND; break;
case BiomeType::SNOWY: voxel.type = TextureType::SNOW; break;
default: voxel.type = TextureType::GRASS_TOP; break;
}
}
else if (y > surfaceY - 4)
{
// couche de terre
voxel.type = (biome == BiomeType::DESERT) ? TextureType::SAND : TextureType::DIRT;
}
else if (y > surfaceY - 8)
{
// couche rocheuse
voxel.type = TextureType::STONE;
}
else
{
// génération de minerais simplifiée
float oreN = heightNoise.GetNoise((float)worldX * 2, (float)y * 2, (float)worldZ * 2);
if (oreN > 0.82f && y < 16) voxel.type = TextureType::DIAMOND_ORE;
else if (oreN > 0.75f && y < 32) voxel.type = TextureType::GOLD_ORE;
else if (oreN > 0.65f && y < 64) voxel.type = TextureType::IRON_ORE;
else if (oreN > 0.55f) voxel.type = TextureType::COAL_ORE;
else voxel.type = TextureType::STONE;
}
}
}
return chunk;
}
private:
FastNoiseLite heightNoise;
FastNoiseLite biomeNoise;
FastNoiseLite caveNoise;
// Retourne le biome en (x,z)
BiomeType getBiomeAt(int x, int z) const
{
float v = biomeNoise.GetNoise((float)x, (float)z);
if (v < -0.6f) return BiomeType::SNOWY;
else if (v < -0.2f) return BiomeType::MOUNTAINS;
else if (v < 0.2f) return BiomeType::PLAINS;
else if (v < 0.6f) return BiomeType::FOREST;
else return BiomeType::DESERT;
}
};
Points clés :
- On utilise OpenSimplex2 pour le bruit de base et les cavernes, Perlin ridgé pour les biomes.
- Chaque générateur est « fractal » (FBm ou Ridged) pour plus de détails.
- La fonction
generateChunk prend en entrée les coordonnées monde (en voxels) chunkX, chunkZ et renvoie un std::array<Voxel, CHUNK_VOLUME>.
- On gère eau, air, couche supérieure, sous-sol, bedrock et quelques minerais de base.
- Vous pouvez ajuster Frequency, FractalOctaves, Gain et Lacunarity pour varier dramatiquement vos paysages ! 🌄
Bon codage et bon fun en procédural ! 🚀