better world gen

This commit is contained in:
maxwes08
2025-09-29 10:55:37 +02:00
parent 9a61dfd74c
commit 38dccf0a84
7 changed files with 2653 additions and 22 deletions

View File

@@ -7,7 +7,8 @@
Dirt, Dirt,
OakPlanks, OakPlanks,
Grass, Grass,
Bedrock Bedrock,
Sand
} }
public enum Orientation : byte public enum Orientation : byte
@@ -74,6 +75,10 @@
Voxel.Blocks.Bedrock, Textures.Bedrock Voxel.Blocks.Bedrock, Textures.Bedrock
)}, )},
{Voxel.Blocks.Sand, new BlockDefinition(
Voxel.Blocks.Sand, Textures.Sand
)},
{ Voxel.Blocks.Grass, new BlockDefinition( { Voxel.Blocks.Grass, new BlockDefinition(
Voxel.Blocks.Grass, Voxel.Blocks.Grass,
Voxel.Textures.GrassSide, // West Voxel.Textures.GrassSide, // West

View File

@@ -24,15 +24,18 @@
Initialize(); Initialize();
} }
public void SetBlock(int x, int y, int z, Blocks block) public void SetBlock(int x, int y, int z, Blocks block, bool updateMesh = true)
{ {
int i = GetBlockIndex(x, y, z); int i = GetBlockIndex(x, y, z);
if (i == -1) return; if (i == -1) return;
_blocks[i] = block; _blocks[i] = block;
UpdateChunkMesh();
if (updateMesh)
{
UpdateChunkMesh();
Renderer.MarkBuffersDirty(); Renderer.MarkBuffersDirty();
} }
}
public void SetBlockIndex(int i, Blocks block) public void SetBlockIndex(int i, Blocks block)
{ {
@@ -53,12 +56,20 @@
private void Initialize() private void Initialize()
{ {
for (int i = 0; i < Size * Size; i++) for (int x = 0; x < Size; x++)
_blocks[i] = Blocks.Bedrock; {
for (int i = Size * Size * 1; i < Size * Size * 15; i++) for (int z = 0; z < Size; z++)
_blocks[i] = Blocks.Stone; {
for (int i = Size * Size * 15; i < Size * Size * 16; i++) var position = GetWorldCoordinates(x, 0, z);
_blocks[i] = Blocks.Grass; int height = Worldgen.GetHeight(position.x, position.z);
for (int y = 0; y < 256; y++)
{
Blocks block = Worldgen.GetBlock(y, height);
SetBlock(x, y, z, block, false);
}
}
}
UpdateChunkMesh(); UpdateChunkMesh();
} }
@@ -72,6 +83,13 @@
return (x, y, z); return (x, y, z);
} }
public (int x, int y, int z) GetWorldCoordinates(int x, int y, int z)
{
x += (Size * X);
z += (Size * Y);
return (x, y, z);
}
private int GetBlockIndex(int x, int y, int z) private int GetBlockIndex(int x, int y, int z)
{ {
if (x < 0 || x > 15 || y < 0 || y > 255 || z < 0 || z > 15) if (x < 0 || x > 15 || y < 0 || y > 255 || z < 0 || z > 15)

2506
Noise/FastNoiseLite.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -15,17 +15,33 @@ internal class Program
window.Player = player; window.Player = player;
for (int x = 0; x < 4; x++) Console.WriteLine("Generating map...");
int worldSizeX = 32;
int worldSizeY = 32;
float maxI = worldSizeX * worldSizeY;
int i = 0;
int lastPercentage = 0;
for (int x = 0; x < worldSizeX; x++)
{ {
for (int y = 0; y < 4; y++) for (int y = 0; y < worldSizeY; y++)
{ {
i++;
Chunk chunk = new Chunk(x, y); Chunk chunk = new Chunk(x, y);
world.AddChunk(chunk); world.AddChunk(chunk);
int percentage = (int)((i / maxI) * 100);
if (percentage > lastPercentage)
{
lastPercentage = percentage;
Console.WriteLine((percentage).ToString() + "%");
}
} }
} }
// xmax ymin gör bugg Console.WriteLine("Generated " + maxI.ToString() + " chunks");
Renderer.SetWorld(world); Renderer.SetWorld(world);

View File

@@ -32,7 +32,7 @@ namespace Voxel
GL.BindVertexArray(_vao); GL.BindVertexArray(_vao);
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _ssbo); GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _ssbo);
GL.BufferData(BufferTarget.ShaderStorageBuffer, 1024 * 1024 * 2, IntPtr.Zero, BufferUsageHint.DynamicDraw); GL.BufferData(BufferTarget.ShaderStorageBuffer, 1024 * 1024 * 128, IntPtr.Zero, BufferUsageHint.DynamicDraw);
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 0, _ssbo); GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 0, _ssbo);
} }

View File

@@ -67,10 +67,10 @@ namespace Voxel
{ {
var orientations = new List<Orientation>(); var orientations = new List<Orientation>();
if (localX == 0) orientations.Add(Orientation.West); if (localX == size - 1) orientations.Add(Orientation.West);
if (localX == size - 1) orientations.Add(Orientation.East); if (localX == 0) orientations.Add(Orientation.East);
if (localZ == size - 1) orientations.Add(Orientation.South); // assuming Z+ is North if (localZ == size - 1) orientations.Add(Orientation.North);
if (localZ == 0) orientations.Add(Orientation.North); if (localZ == 0) orientations.Add(Orientation.South);
return orientations; return orientations;
} }
@@ -94,7 +94,6 @@ namespace Voxel
if (chunk.Neighbors.TryGetValue(orientation, out var neighbor) && neighbor != null) if (chunk.Neighbors.TryGetValue(orientation, out var neighbor) && neighbor != null)
{ {
neighbor.UpdateChunkMesh(); neighbor.UpdateChunkMesh();
Console.WriteLine($"Updated neighbor at {orientation}");
} }
} }
} }

87
Worldgen.cs Normal file
View File

@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Voxel
{
public static class Worldgen
{
private static int baseHeight = 32;
private static float res1 = 2;
private static float res2 = 4;
private static float amplitude1 = 4;
private static float amplitude2 = 2;
private static float mountainMapRes = (float)1/2;
private static float mountainMapAmplitude = 8;
private static float elevationMapRes = (float)1/8;
private static float elevationMapAmplitude = 32;
private static FastNoiseLite noise = new FastNoiseLite();
private static Random random = new Random();
public static int GetHeight(int x, int z)
{
float map1 = noise.GetNoise(
x * res1,
z * res1
) * amplitude1;
float map2 = noise.GetNoise(x * res2,
z * res2
) * amplitude2;
float mountainMap = noise.GetNoise(
x * mountainMapRes,
z * mountainMapRes
) * mountainMapAmplitude;
float elevationMap = (noise.GetNoise(
x * elevationMapRes,
z * elevationMapRes
) + 0.25f) * elevationMapAmplitude;
return baseHeight + (int)(elevationMap + ((map1 + map2) * 1 + mountainMap));
}
public static Blocks GetBlock(int y, int maxY)
{
if (y > maxY)
{
return Blocks.Air;
}
if (y == maxY)
{
if (y < baseHeight)
return Blocks.Sand;
else
return Blocks.Grass;
}
if (y < 4)
{
if (y == 0) return Blocks.Bedrock;
float randomValue = random.NextSingle();
if (randomValue < (1f / y))
{
return Blocks.Bedrock;
}
return Blocks.Stone;
}
if (y > maxY - 6)
{
return Blocks.Dirt;
}
return Blocks.Stone;
}
}
}