chunbk face culling
This commit is contained in:
62
World.cs
62
World.cs
@@ -1,5 +1,6 @@
|
||||
using OpenTK.Mathematics;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
|
||||
namespace Voxel
|
||||
{
|
||||
@@ -21,6 +22,22 @@ namespace Voxel
|
||||
public void AddChunk(Chunk chunk)
|
||||
{
|
||||
_chunks[(chunk.X, chunk.Y)] = chunk;
|
||||
|
||||
Dictionary<Orientation, Orientation> oppositeOrientation = new()
|
||||
{
|
||||
{Orientation.West, Orientation.East},
|
||||
{Orientation.East, Orientation.West},
|
||||
{Orientation.North, Orientation.South},
|
||||
{Orientation.South, Orientation.North},
|
||||
};
|
||||
|
||||
foreach (var (orientation, neighbor) in GetChunkNeighbors(chunk))
|
||||
{
|
||||
neighbor.Neighbors[oppositeOrientation[orientation]] = chunk;
|
||||
chunk.Neighbors[orientation] = neighbor;
|
||||
neighbor.UpdateChunkMesh();
|
||||
}
|
||||
chunk.UpdateChunkMesh();
|
||||
}
|
||||
|
||||
public void RemoveChunk(int chunkX, int chunkZ)
|
||||
@@ -46,6 +63,18 @@ namespace Voxel
|
||||
return chunk.GetBlock(localX, worldY, localZ);
|
||||
}
|
||||
|
||||
List<Orientation> GetEdgeOrientations(int localX, int localZ, int size)
|
||||
{
|
||||
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);
|
||||
|
||||
return orientations;
|
||||
}
|
||||
|
||||
public void SetBlock(int worldX, int worldY, int worldZ, Blocks block)
|
||||
{
|
||||
int chunkX = worldX / Chunk.Size;
|
||||
@@ -57,6 +86,39 @@ namespace Voxel
|
||||
int localZ = worldZ % Chunk.Size;
|
||||
|
||||
chunk.SetBlock(localX, worldY, localZ, block);
|
||||
|
||||
if (block == Blocks.Air && (localX == 15 || localX == 0) || (localZ == 15 || localZ == 0))
|
||||
{
|
||||
foreach (var orientation in GetEdgeOrientations(localX, localZ, Chunk.Size))
|
||||
{
|
||||
if (chunk.Neighbors.TryGetValue(orientation, out var neighbor) && neighbor != null)
|
||||
{
|
||||
neighbor.UpdateChunkMesh();
|
||||
Console.WriteLine($"Updated neighbor at {orientation}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<(Orientation orientation, Chunk neighbor)> GetChunkNeighbors(Chunk chunk)
|
||||
{
|
||||
Dictionary<Orientation, (int x, int y)> offsets = new()
|
||||
{
|
||||
{ Orientation.West, (1, 0) },
|
||||
{ Orientation.East, (-1, 0) },
|
||||
{ Orientation.North, (0, 1) },
|
||||
{ Orientation.South, (0, -1) }
|
||||
};
|
||||
|
||||
foreach (var kv in offsets)
|
||||
{
|
||||
int nx = chunk.X + kv.Value.x;
|
||||
int ny = chunk.Y + kv.Value.y;
|
||||
|
||||
Chunk neighbor = GetChunk(nx, ny);
|
||||
if (neighbor != null)
|
||||
yield return (kv.Key, neighbor);
|
||||
}
|
||||
}
|
||||
|
||||
public (bool success, Blocks block, int x, int y, int z, Vector3i normal) Raycast(Vector3 origin, Vector3 direction, float maxDistance)
|
||||
|
||||
Reference in New Issue
Block a user