better world gen
This commit is contained in:
@@ -7,7 +7,8 @@
|
||||
Dirt,
|
||||
OakPlanks,
|
||||
Grass,
|
||||
Bedrock
|
||||
Bedrock,
|
||||
Sand
|
||||
}
|
||||
|
||||
public enum Orientation : byte
|
||||
@@ -74,6 +75,10 @@
|
||||
Voxel.Blocks.Bedrock, Textures.Bedrock
|
||||
)},
|
||||
|
||||
{Voxel.Blocks.Sand, new BlockDefinition(
|
||||
Voxel.Blocks.Sand, Textures.Sand
|
||||
)},
|
||||
|
||||
{ Voxel.Blocks.Grass, new BlockDefinition(
|
||||
Voxel.Blocks.Grass,
|
||||
Voxel.Textures.GrassSide, // West
|
||||
|
||||
34
Chunk.cs
34
Chunk.cs
@@ -24,15 +24,18 @@
|
||||
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);
|
||||
if (i == -1) return;
|
||||
_blocks[i] = block;
|
||||
UpdateChunkMesh();
|
||||
|
||||
if (updateMesh)
|
||||
{
|
||||
UpdateChunkMesh();
|
||||
Renderer.MarkBuffersDirty();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetBlockIndex(int i, Blocks block)
|
||||
{
|
||||
@@ -53,12 +56,20 @@
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
for (int i = 0; i < Size * Size; i++)
|
||||
_blocks[i] = Blocks.Bedrock;
|
||||
for (int i = Size * Size * 1; i < Size * Size * 15; i++)
|
||||
_blocks[i] = Blocks.Stone;
|
||||
for (int i = Size * Size * 15; i < Size * Size * 16; i++)
|
||||
_blocks[i] = Blocks.Grass;
|
||||
for (int x = 0; x < Size; x++)
|
||||
{
|
||||
for (int z = 0; z < Size; z++)
|
||||
{
|
||||
var position = GetWorldCoordinates(x, 0, z);
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -72,6 +83,13 @@
|
||||
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)
|
||||
{
|
||||
if (x < 0 || x > 15 || y < 0 || y > 255 || z < 0 || z > 15)
|
||||
|
||||
2506
Noise/FastNoiseLite.cs
Normal file
2506
Noise/FastNoiseLite.cs
Normal file
File diff suppressed because it is too large
Load Diff
24
Program.cs
24
Program.cs
@@ -15,17 +15,33 @@ internal class Program
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Voxel
|
||||
GL.BindVertexArray(_vao);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
9
World.cs
9
World.cs
@@ -67,10 +67,10 @@ namespace Voxel
|
||||
{
|
||||
var orientations = new List<Orientation>();
|
||||
|
||||
if (localX == 0) orientations.Add(Orientation.West);
|
||||
if (localX == size - 1) orientations.Add(Orientation.East);
|
||||
if (localZ == size - 1) orientations.Add(Orientation.South); // assuming Z+ is North
|
||||
if (localZ == 0) orientations.Add(Orientation.North);
|
||||
if (localX == size - 1) orientations.Add(Orientation.West);
|
||||
if (localX == 0) orientations.Add(Orientation.East);
|
||||
if (localZ == size - 1) orientations.Add(Orientation.North);
|
||||
if (localZ == 0) orientations.Add(Orientation.South);
|
||||
|
||||
return orientations;
|
||||
}
|
||||
@@ -94,7 +94,6 @@ namespace Voxel
|
||||
if (chunk.Neighbors.TryGetValue(orientation, out var neighbor) && neighbor != null)
|
||||
{
|
||||
neighbor.UpdateChunkMesh();
|
||||
Console.WriteLine($"Updated neighbor at {orientation}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
87
Worldgen.cs
Normal file
87
Worldgen.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user