chunk stuff and camera smoothing

This commit is contained in:
Halbear
2026-02-14 01:21:57 +00:00
parent f617f799c2
commit 70acbd2526
3 changed files with 53 additions and 17 deletions

View File

@@ -26,7 +26,8 @@ namespace VoxelIsometricRenderer
static Rectangle PlayerPosition = new Rectangle(0,0,100,100); static Rectangle PlayerPosition = new Rectangle(0,0,100,100);
public static void Draw(Graphics g) public static void Draw(Graphics g)
{ {
BlockRegistry.RenderBlock(g,BlockID, BlockposX, BlockposY, BlockposZ, 1); Chunk PlayerChunk = ChunkRegistry.FetchChunk(String.Format("x={0};y={1};z={2}", (int)Math.Floor(BlockposX / (double)ChunkRegistry.FetchDimensions()[0]), (int)Math.Floor(BlockposY / (double)ChunkRegistry.FetchDimensions()[1]), (int)Math.Floor(BlockposZ / (double)ChunkRegistry.FetchDimensions()[2])));
BlockRegistry.RenderBlock(g,BlockID, BlockposX, BlockposY, BlockposZ, PlayerChunk != null ? PlayerChunk.SkyLightHeightMap[Math.Min(BlockposX - (PlayerChunk.ChunkPos[0] * ChunkRegistry.FetchDimensions()[0]) + 1, PlayerChunk.SkyLightHeightMap.GetLength(0) - 1), Math.Min(BlockposY - (PlayerChunk.ChunkPos[1] * ChunkRegistry.FetchDimensions()[1]) + 1, ChunkRegistry.FetchDimensions()[1] - 1), Math.Max(BlockposZ - (PlayerChunk.ChunkPos[2] * ChunkRegistry.FetchDimensions()[2]) - 1, 0)] : 1);
int[] Positions = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ,PositionX,PositionY,PositionZ); int[] Positions = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ,PositionX,PositionY,PositionZ);
if (Positions[0] != LastPositions[0] || Positions[1] != LastPositions[1]) if (Positions[0] != LastPositions[0] || Positions[1] != LastPositions[1])
{ {
@@ -51,9 +52,9 @@ namespace VoxelIsometricRenderer
if (LightingManager.Square(CameraCentre.Y - PlayerPosition.Y) > LightingManager.Square(CameraCentre.Y - NextPosition.Y) && !CameraCentre.Contains(CameraCentre.X, PlayerPosition.Y)) if (LightingManager.Square(CameraCentre.Y - PlayerPosition.Y) > LightingManager.Square(CameraCentre.Y - NextPosition.Y) && !CameraCentre.Contains(CameraCentre.X, PlayerPosition.Y))
{ {
float Iterator = 0; float Iterator = 0;
while (!CameraCentre.Contains(CameraCentre.X,NextPosition.Y) && Iterator < 100) //while (!CameraCentre.Contains(CameraCentre.X,NextPosition.Y) && Iterator < 2)
{ {
Iterator+=0.1f; Iterator+= LightingManager.Square(CameraCentre.Y - NextPosition.Y) <= 10000 ? 0.1f : 1;
Positions = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX, PositionY + Iterator, PositionZ); Positions = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX, PositionY + Iterator, PositionZ);
NextPosition = new Rectangle(Positions[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), Positions[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize); NextPosition = new Rectangle(Positions[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), Positions[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize);
} }
@@ -62,9 +63,9 @@ namespace VoxelIsometricRenderer
else if (LightingManager.Square(CameraCentre.Y - PlayerPosition.Y) < LightingManager.Square(CameraCentre.Y - NextPosition.Y)) else if (LightingManager.Square(CameraCentre.Y - PlayerPosition.Y) < LightingManager.Square(CameraCentre.Y - NextPosition.Y))
{ {
float Iterator = 0; float Iterator = 0;
while (!CameraCentre.Contains(CameraCentre.X, NextPosition.Y)) //while (!CameraCentre.Contains(CameraCentre.X, NextPosition.Y) && Iterator > -2)
{ {
Iterator--; Iterator-= LightingManager.Square(CameraCentre.Y - NextPosition.Y) <= 10000 ? 0.1f : 1;
Positions = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX, PositionY + Iterator, PositionZ); Positions = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX, PositionY + Iterator, PositionZ);
NextPosition = new Rectangle(Positions[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), Positions[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize); NextPosition = new Rectangle(Positions[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), Positions[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize);
} }
@@ -74,10 +75,10 @@ namespace VoxelIsometricRenderer
{ {
float IteratorX = 0; float IteratorX = 0;
float IteratorZ = 0; float IteratorZ = 0;
while (!CameraCentre.Contains(NextPositionX.X, CameraCentre.Y) && IteratorX > -2 && IteratorZ < 2) //while (!CameraCentre.Contains(NextPositionX.X, CameraCentre.Y) && IteratorX > -2 && IteratorZ < 2)
{ {
IteratorX-=0.1f; IteratorX-= LightingManager.Square(CameraCentre.X - NextPositionX.X) <= 10000 ? 0.1f : 1;
IteratorZ+=0.1f; IteratorZ+= LightingManager.Square(CameraCentre.X - NextPositionX.X) <= 10000 ? 0.1f : 1;
PositionsX = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX + IteratorX, PositionY, PositionZ + IteratorZ); PositionsX = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX + IteratorX, PositionY, PositionZ + IteratorZ);
NextPositionX = new Rectangle(PositionsX[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), PositionsX[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize); NextPositionX = new Rectangle(PositionsX[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), PositionsX[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize);
} }
@@ -88,10 +89,10 @@ namespace VoxelIsometricRenderer
{ {
float IteratorX = 0; float IteratorX = 0;
float IteratorZ = 0; float IteratorZ = 0;
while (!CameraCentre.Contains(NextPositionX.X, CameraCentre.Y) && IteratorZ > -1 && IteratorX < 1) //while (!CameraCentre.Contains(NextPositionX.X, CameraCentre.Y) && IteratorZ > -1 && IteratorX < 1)
{ {
IteratorX+=0.1f; IteratorX+= LightingManager.Square(CameraCentre.X - NextPositionX.X) <= 10000 ? 0.1f : 1;
IteratorZ-=0.1f; IteratorZ -= LightingManager.Square(CameraCentre.X - NextPositionX.X) <= 10000 ? 0.1f : 1;
PositionsX = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX + IteratorX, PositionY, PositionZ + IteratorZ); PositionsX = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX + IteratorX, PositionY, PositionZ + IteratorZ);
NextPositionX = new Rectangle(PositionsX[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), PositionsX[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize); NextPositionX = new Rectangle(PositionsX[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), PositionsX[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize);
} }
@@ -106,7 +107,7 @@ namespace VoxelIsometricRenderer
} }
public static void UpdateCameraCentre(int Width, int Height) public static void UpdateCameraCentre(int Width, int Height)
{ {
CameraCentre = new Rectangle((int)((Width/(2 * TextureManager.RenderScale)) - TextureManager.VoxelSize), (int)((Height/ (2 * TextureManager.RenderScale)) - (TextureManager.VoxelSize)), (int)(TextureManager.VoxelSize*2), (int)(TextureManager.VoxelSize*2)); CameraCentre = new Rectangle((int)((Width/(2 * TextureManager.RenderScale)) - TextureManager.VoxelSize/6), (int)((Height/ (2 * TextureManager.RenderScale)) - (TextureManager.VoxelSize/6)), (int)(TextureManager.VoxelSize/3), (int)(TextureManager.VoxelSize/3));
} }
public static void CreatePlayer(int[] Faces, int RenderType, BlockState blockState) public static void CreatePlayer(int[] Faces, int RenderType, BlockState blockState)
{ {

View File

@@ -20,6 +20,7 @@ namespace VoxelIsometricRenderer
{ {
public static bool running = true; public static bool running = true;
public static List<Voxel> Voxels = new List<Voxel>(); public static List<Voxel> Voxels = new List<Voxel>();
public static List<int> ChunksToRender = new List<int>();
public static Thread MainThread; public static Thread MainThread;
public static float scale = 2f; public static float scale = 2f;
public static int FPS = 0; public static int FPS = 0;
@@ -57,12 +58,26 @@ namespace VoxelIsometricRenderer
TextureManager.GenerateVoxelFaces(); TextureManager.GenerateVoxelFaces();
PlayerObject.CreatePlayer(new int[] { 0, 0, 0, 0, 0, 0 }, 0, new BlockState(false, BlockType.Ground, false)); PlayerObject.CreatePlayer(new int[] { 0, 0, 0, 0, 0, 0 }, 0, new BlockState(false, BlockType.Ground, false));
Chunk TestChunk = ChunkRegistry.IndexChunk(0, 0, 0); Chunk TestChunk = ChunkRegistry.IndexChunk(0, 0, 0);
Chunk TestChunk2 = ChunkRegistry.IndexChunk(0, 1, 0);
//Chunk TestChunk3 = ChunkRegistry.IndexChunk(0, 0, -1);
//Chunk TestChunk4 = ChunkRegistry.IndexChunk(0, 1, -1);
Random rnd = new Random(); Random rnd = new Random();
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {
for (int k = 0; k < 16; k++) { for (int k = 0; k < 16; k++) {
if(LightSource.IsWithinSphere(3, 4, 7, 4, i, j, k))
{
TestChunk2.CreateBlock(new int[] { 8,8,8,8,8,8 }, 0, new BlockState(false, BlockType.Ground, true), i, j, k);
} else if(i == 4 && k == 4 && j < 6){
TestChunk2.CreateBlock(new int[] { 5,5,5,5,6,6 }, 0, new BlockState(false, BlockType.Ground, true), i, j, k);
}
else TestChunk2.CreateAir(i, j, k);
if (i < 8 && k < 8 && rnd.Next(0,1)==0) TestChunk.CreateBlock(new int[] { rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13) }, 0, new BlockState(false, BlockType.Ground, true), i, j, k); if (i < 8 && k < 8 && rnd.Next(0,1)==0) TestChunk.CreateBlock(new int[] { rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13) }, 0, new BlockState(false, BlockType.Ground, true), i, j, k);
else TestChunk.CreateAir(i, j, k); else TestChunk.CreateAir(i, j, k);
//if (LightSource.IsWithinSphere(7,8,8,8,i,j,k)) TestChunk4.CreateBlock(new int[] { rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13), rnd.Next(0, 13) }, 0, new BlockState(false, BlockType.Ground, true), i, j, k);
//else TestChunk4.CreateAir(i, j, k);
//if (j == 15) TestChunk3.CreateBlock(new int[] {1,1,1,1,2,2 }, 0, new BlockState(false, BlockType.Ground, true), i, j, k);
//else TestChunk3.CreateAir(i, j, k);
} }
} }
} }
@@ -70,9 +85,17 @@ namespace VoxelIsometricRenderer
//LightingManager.Lights.Add(new LightSource(LightShape.Sphere, 15, 15, 15, 7,18,7, new float[] {1.8f,0.0f,0.0f},255)); //LightingManager.Lights.Add(new LightSource(LightShape.Sphere, 15, 15, 15, 7,18,7, new float[] {1.8f,0.0f,0.0f},255));
//LightingManager.Lights.Add(new LightSource(LightShape.Sphere,15,15,15, 3, 8, 15, new float[] {0.0f,1.8f,0.0f},255)); //LightingManager.Lights.Add(new LightSource(LightShape.Sphere,15,15,15, 3, 8, 15, new float[] {0.0f,1.8f,0.0f},255));
LightingManager.Lights.Add(new LightSource(LightShape.Sphere,12,12,12, 10, 18, 10, new float[] {0.0f,0.0f,1.8f}, 255)); LightingManager.Lights.Add(new LightSource(LightShape.Sphere,12,12,12, 10, 18, 10, new float[] {0.0f,0.0f,1.8f}, 255));
LightingManager.Lights.Add(new LightSource(LightShape.Sphere, 5, 5, 5, 4,18,4, new float[] {0.8f,0.0f,0.0f},255)); LightingManager.Lights.Add(new LightSource(LightShape.Sphere, 7, 7, 7, 4,18,4, new float[] {1.8f,0.0f,0.0f},255));
TestChunk2.GenerateLightMap();
//TestChunk4.GenerateLightMap();
// TestChunk3.GenerateLightMap();
TestChunk.GenerateLightMap(); TestChunk.GenerateLightMap();
TestChunk.CalculateCull(); TestChunk.CalculateCull();
//ChunksToRender.Add(2);
//ChunksToRender.Add(3);
ChunksToRender.Add(0);
ChunksToRender.Add(1);
//TestChunk2.CalculateCull();
MainThread = new Thread(Runtime); MainThread = new Thread(Runtime);
MainThread.SetApartmentState(ApartmentState.STA); MainThread.SetApartmentState(ApartmentState.STA);
MainThread.Start(); MainThread.Start();
@@ -91,6 +114,7 @@ namespace VoxelIsometricRenderer
Display.Refresh(); Display.Refresh();
if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - LightingFrameTime > 16) if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - LightingFrameTime > 16)
{ {
PlayerObject.UpdateCamera();
LightingFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; LightingFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
LightingManager.UpdateLighting(); LightingManager.UpdateLighting();
//LightingManager.Lights[0].SetPosition(PlayerObject.GetPos()); //LightingManager.Lights[0].SetPosition(PlayerObject.GetPos());
@@ -99,7 +123,6 @@ namespace VoxelIsometricRenderer
} }
if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - _64ms > 64) if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - _64ms > 64)
{ {
PlayerObject.UpdateCamera();
_64ms = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; _64ms = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
int[] Pos = LightingManager.Lights[0].GetPosition(); int[] Pos = LightingManager.Lights[0].GetPosition();
if (up && Pos[1] < 25) if (up && Pos[1] < 25)
@@ -115,6 +138,7 @@ namespace VoxelIsometricRenderer
up = true; up = true;
} }
LightingManager.Lights[0].SetPosition(Pos); LightingManager.Lights[0].SetPosition(Pos);
//LightingManager.Lights[1].SetPosition(PlayerObject.GetPos());
} }
if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - SecondFrameTime > 1000) if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - SecondFrameTime > 1000)
{ {
@@ -132,8 +156,14 @@ namespace VoxelIsometricRenderer
g.PixelOffsetMode = PixelOffsetMode.Half; g.PixelOffsetMode = PixelOffsetMode.Half;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.ScaleTransform(TextureManager.RenderScale, TextureManager.RenderScale); g.ScaleTransform(TextureManager.RenderScale, TextureManager.RenderScale);
ChunkRegistry.RenderChunk(g, 0); //ChunkRegistry.RenderChunk(g, 1);
ChunksToRender.ForEach(chunk =>
{
ChunkRegistry.RenderChunk(g, chunk);
});
PlayerObject.Draw(g); PlayerObject.Draw(g);
int[] Positions = BlockRegistry.GetDrawLocation(LightingManager.Lights[0].GetPosition()[0], LightingManager.Lights[0].GetPosition()[1], LightingManager.Lights[0].GetPosition()[2], PlayerObject.GetCameraPos()[0], PlayerObject.GetCameraPos()[1], PlayerObject.GetCameraPos()[2]);
g.FillEllipse(Brushes.Blue, new Rectangle(Positions[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), Positions[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize/2, TextureManager.VoxelSize/2));
g.ResetTransform(); g.ResetTransform();
g.DrawString(String.Format("FPS: {0} Ram Usage: {9}MB Ram Allocated: {8}MB\nSunValue: {1}\nMoonValue:{3}\nLightDirection:{4}\nAmbientColourGrading: R{5} G{6} B{7}\nSunAngles:{2}", FPS, LightingManager.SkyLightMetrics[0], String.Format(" x={0}° y={1}°", LightingManager.sunAngles[0], LightingManager.sunAngles[1]), LightingManager.SkyLightMetrics[2], LightingManager.SunAngle, LightingManager.SkyLightMetrics[3], LightingManager.SkyLightMetrics[4], LightingManager.SkyLightMetrics[5], TotalRamUsage, RamUsage), SystemFonts.DefaultFont, Brushes.Red, new PointF(0, 0)); g.DrawString(String.Format("FPS: {0} Ram Usage: {9}MB Ram Allocated: {8}MB\nSunValue: {1}\nMoonValue:{3}\nLightDirection:{4}\nAmbientColourGrading: R{5} G{6} B{7}\nSunAngles:{2}", FPS, LightingManager.SkyLightMetrics[0], String.Format(" x={0}° y={1}°", LightingManager.sunAngles[0], LightingManager.sunAngles[1]), LightingManager.SkyLightMetrics[2], LightingManager.SunAngle, LightingManager.SkyLightMetrics[3], LightingManager.SkyLightMetrics[4], LightingManager.SkyLightMetrics[5], TotalRamUsage, RamUsage), SystemFonts.DefaultFont, Brushes.Red, new PointF(0, 0));
Frames++; Frames++;

View File

@@ -1059,11 +1059,12 @@ namespace VoxelIsometricRenderer
} }
public void GenerateLightMap() public void GenerateLightMap()
{ {
Chunk ChunkAbove = ChunkRegistry.FetchChunk(String.Format("x={0};y={1};z={2}", ChunkPos[0], ChunkPos[1] +1, ChunkPos[2]));
for (int x = 0;x < SkyLightHeightMap.GetLength(0); x++) for (int x = 0;x < SkyLightHeightMap.GetLength(0); x++)
{ {
for(int z = 0; z < SkyLightHeightMap.GetLength(1); z++) for(int z = 0; z < SkyLightHeightMap.GetLength(1); z++)
{ {
int Y = 1; int Y = ChunkAbove != null ? ChunkAbove.SkyLightHeightMap[x,1,z] != 0 ? ChunkAbove.SkyLightHeightMap[x, 1, z] : 1 : 1;
for (int y = SkyLightHeightMap.GetLength(1) - 1; y > 0; y--) for (int y = SkyLightHeightMap.GetLength(1) - 1; y > 0; y--)
{ {
SkyLightHeightMap[x, y, z] = Y; SkyLightHeightMap[x, y, z] = Y;
@@ -1087,7 +1088,7 @@ namespace VoxelIsometricRenderer
{ {
if (BlockReg[x, y, z] != -1 && Culled[x, y, z] == false) if (BlockReg[x, y, z] != -1 && Culled[x, y, z] == false)
{ {
BlockRegistry.RenderBlock(g, BlockReg[x, y, z], x + ChunkPos[0] * ChunkRegistry.FetchDimensions()[0], y + ChunkPos[1] * ChunkRegistry.FetchDimensions()[1], z + ChunkPos[1] * ChunkRegistry.FetchDimensions()[1], SkyLightHeightMap[x, y, z]); BlockRegistry.RenderBlock(g, BlockReg[x, y, z], x + ChunkPos[0] * ChunkRegistry.FetchDimensions()[0], y + ChunkPos[1] * ChunkRegistry.FetchDimensions()[1], z + ChunkPos[2] * ChunkRegistry.FetchDimensions()[2], SkyLightHeightMap[x, y, z]);
} }
} }
} }
@@ -1153,6 +1154,10 @@ namespace VoxelIsometricRenderer
static int ChunkLength = 16; static int ChunkLength = 16;
static List<Chunk> Chunks = new List<Chunk>(); static List<Chunk> Chunks = new List<Chunk>();
static List<string> Chunk_Pos_Lookup = new List<string>(); static List<string> Chunk_Pos_Lookup = new List<string>();
public static int FetchChunkCount()
{
return Chunks.Count;
}
public static Chunk IndexChunk(int x, int y, int z) public static Chunk IndexChunk(int x, int y, int z)
{ {
Chunk newChunk = new Chunk(x, y, z, ChunkWidth, ChunkHeight, ChunkLength, Chunks.Count); Chunk newChunk = new Chunk(x, y, z, ChunkWidth, ChunkHeight, ChunkLength, Chunks.Count);