From f617f799c215f8e61b7d53a112edd302d5b310ce Mon Sep 17 00:00:00 2001 From: Halbear Date: Fri, 13 Feb 2026 14:54:30 +0000 Subject: [PATCH] Player Object --- VoxelIsometricRenderer/PlayerObject.cs | 142 ++++++++++++++++++++++--- VoxelIsometricRenderer/Terrain4.cs | 67 +++++++----- VoxelIsometricRenderer/Voxel.cs | 113 +++++++++++--------- 3 files changed, 231 insertions(+), 91 deletions(-) diff --git a/VoxelIsometricRenderer/PlayerObject.cs b/VoxelIsometricRenderer/PlayerObject.cs index 78c4df5..85c062f 100644 --- a/VoxelIsometricRenderer/PlayerObject.cs +++ b/VoxelIsometricRenderer/PlayerObject.cs @@ -1,22 +1,134 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Forms; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar; namespace VoxelIsometricRenderer { public static class PlayerObject { - static int PositionX = 0; - static int PositionY = 0; - static int PositionZ = 0; + static int BlockID = 0; + static float PositionX = 0; + static float PositionY = 0; + static float PositionZ = 0; + static int BlockposX = 5; + static int BlockposY = 17; + static int BlockposZ =5; static float PrecisePositionX = 0.0f; static float PrecisePositionY = 0.0f; static float PrecisePositionZ = 0.0f; + static Rectangle CameraCentre = new Rectangle(0,0,100,100); + static int[] LastPositions = new int[] { 0, -192 }; + static Rectangle PlayerPosition = new Rectangle(0,0,100,100); + public static void Draw(Graphics g) + { + BlockRegistry.RenderBlock(g,BlockID, BlockposX, BlockposY, BlockposZ, 1); + int[] Positions = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ,PositionX,PositionY,PositionZ); + if (Positions[0] != LastPositions[0] || Positions[1] != LastPositions[1]) + { + //MessageBox.Show(Positions[0] + "_" + Positions[1]); + LastPositions = Positions; + } + PlayerPosition = new Rectangle(Positions[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), Positions[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize); + g.DrawRectangle(new Pen(Brushes.Blue), PlayerPosition); + g.DrawRectangle(new Pen(Brushes.Red), CameraCentre); + } + public static void UpdateCamera() + { + if (!CameraCentre.IntersectsWith(PlayerPosition)) + { + float YPosition = PositionY; + float XPosition = PositionX; + float ZPosition = PositionZ; + int[] Positions = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX, PositionY + 1, PositionZ); + Rectangle NextPosition = new Rectangle(Positions[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), Positions[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize); + int[] PositionsX = BlockRegistry.GetDrawLocation(BlockposX, BlockposY, BlockposZ, PositionX + 1, PositionY, PositionZ - 1); + Rectangle NextPositionX = new Rectangle(PositionsX[0] + (int)(Terrain4.width / (2 * TextureManager.RenderScale)), PositionsX[1] + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), TextureManager.VoxelSize, TextureManager.VoxelSize); + if (LightingManager.Square(CameraCentre.Y - PlayerPosition.Y) > LightingManager.Square(CameraCentre.Y - NextPosition.Y) && !CameraCentre.Contains(CameraCentre.X, PlayerPosition.Y)) + { + float Iterator = 0; + while (!CameraCentre.Contains(CameraCentre.X,NextPosition.Y) && Iterator < 100) + { + Iterator+=0.1f; + 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); + } + YPosition += Iterator; + } + else if (LightingManager.Square(CameraCentre.Y - PlayerPosition.Y) < LightingManager.Square(CameraCentre.Y - NextPosition.Y)) + { + float Iterator = 0; + while (!CameraCentre.Contains(CameraCentre.X, NextPosition.Y)) + { + Iterator--; + 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); + } + YPosition += Iterator; + } + if (LightingManager.Square(CameraCentre.X - PlayerPosition.X) < LightingManager.Square(CameraCentre.X - NextPositionX.X) && !CameraCentre.Contains(PlayerPosition.X, CameraCentre.Y)) + { + float IteratorX = 0; + float IteratorZ = 0; + while (!CameraCentre.Contains(NextPositionX.X, CameraCentre.Y) && IteratorX > -2 && IteratorZ < 2) + { + IteratorX-=0.1f; + IteratorZ+=0.1f; + 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); + } + XPosition += IteratorX; + ZPosition += IteratorZ; + } + else if (PlayerPosition.X < CameraCentre.X) + { + float IteratorX = 0; + float IteratorZ = 0; + while (!CameraCentre.Contains(NextPositionX.X, CameraCentre.Y) && IteratorZ > -1 && IteratorX < 1) + { + IteratorX+=0.1f; + IteratorZ-=0.1f; + 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); + } + XPosition += IteratorX; + ZPosition += IteratorZ; + } + + PositionX = XPosition; + PositionY = YPosition; + PositionZ = ZPosition; + } + } + 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)); + } + public static void CreatePlayer(int[] Faces, int RenderType, BlockState blockState) + { + Voxel voxel = new Voxel(RenderType, Faces[0], Faces[1], Faces[2], Faces[3], Faces[4], Faces[5], blockState.hasBlocksLight()); + int index = VoxelRegistry.VoxelExists(voxel); + if (index == -1) + { + index = VoxelRegistry.IndexVoxel(voxel); + } + Block newBlock = new Block(index, BlockstateRegistry.GetBlockstateIndex(blockState), -1); + int x = BlockposX; + int y = BlockposY; + int z = BlockposZ; + BlockID = BlockRegistry.CheckInBlock(newBlock, x, y, z); + } + public static float[] GetCameraPos() + { + return new float[] { PositionX, PositionY, PositionZ }; + } public static int[] GetPos() { - return new int[] { PositionX, PositionY, PositionZ }; + return new int[] { BlockposX, BlockposY, BlockposZ }; } public static float[] GetPresicePos() { @@ -24,9 +136,9 @@ namespace VoxelIsometricRenderer } public static void SetPos(int[] pos, bool Reoccure) { - PositionX = pos[0]; - PositionY = pos[1]; - PositionZ = pos[2]; + BlockposX = pos[0]; + BlockposY = pos[1]; + BlockposZ = pos[2]; if (Reoccure) SetPrecisePos(new float[] { pos[0], pos[1], pos[2] },false); } public static void SetPrecisePos(float[] pos, bool Reoccure) @@ -41,11 +153,11 @@ namespace VoxelIsometricRenderer { switch (Axis) { - case 0: PositionX += Increment; break; - case 1: PositionY += Increment; break; - case 2: PositionZ += Increment; break; + case 0: BlockposX += Increment; break; + case 1: BlockposY += Increment; break; + case 2: BlockposZ += Increment; break; } - if (Reoccure) SetPrecisePos(new float[] { (float)PositionX, (float)PositionY, (float)PositionZ },false); + if (Reoccure) SetPrecisePos(new float[] { (float)BlockposX, (float)BlockposY, (float)BlockposZ },false); } public static void IncrementPrecisePos(int Axis, float Increment, bool Reoccure) { @@ -61,11 +173,11 @@ namespace VoxelIsometricRenderer { switch (Axis) { - case 0:PositionX = Pos; break; - case 1: PositionY = Pos; break; - case 2: PositionZ = Pos; break; + case 0: BlockposX = Pos; break; + case 1: BlockposY = Pos; break; + case 2: BlockposZ = Pos; break; } - if (Reoccure) SetPrecisePos(new float[] { PositionX, PositionY, PositionZ },false); + if (Reoccure) SetPrecisePos(new float[] { BlockposX, BlockposY, BlockposZ },false); } } } diff --git a/VoxelIsometricRenderer/Terrain4.cs b/VoxelIsometricRenderer/Terrain4.cs index 77e4cce..5dada27 100644 --- a/VoxelIsometricRenderer/Terrain4.cs +++ b/VoxelIsometricRenderer/Terrain4.cs @@ -55,18 +55,22 @@ namespace VoxelIsometricRenderer TextureManager.AddBitmap(Resources.OrangeSquare); TextureManager.AddBitmap(Resources.ColourEdges); TextureManager.GenerateVoxelFaces(); + PlayerObject.CreatePlayer(new int[] { 0, 0, 0, 0, 0, 0 }, 0, new BlockState(false, BlockType.Ground, false)); Chunk TestChunk = ChunkRegistry.IndexChunk(0, 0, 0); Random rnd = new Random(); for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { for (int k = 0; k < 16; k++) { - if (i < 5 && k < 5 && rnd.Next(0, 3) > 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); } } } - LightingManager.Lights.Add(new LightSource(LightShape.Sphere,12,12,12, 5, 18, 5, new float[] {0.8f,0.0f,0.0f},64)); - LightingManager.Lights.Add(new LightSource(LightShape.Sphere,12,12,12, 5, 8, 5, new float[] {0.0f,0.0f,0.6f},64)); + //LightingManager.Lights.Add(new LightSource(LightShape.Sphere,7,7,7, 6, 14, 7, new float[] {0.8f,0.8f,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,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)); TestChunk.GenerateLightMap(); TestChunk.CalculateCull(); MainThread = new Thread(Runtime); @@ -77,9 +81,11 @@ namespace VoxelIsometricRenderer private void Runtime() { - bool up = false; + bool up = true; + bool Down = true; double LightingFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; double SecondFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; + double _64ms = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; while (running) { Display.Refresh(); @@ -87,26 +93,31 @@ namespace VoxelIsometricRenderer { LightingFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; LightingManager.UpdateLighting(); + //LightingManager.Lights[0].SetPosition(PlayerObject.GetPos()); //LightingManager.sunAngles[0] += 5f; //LightingManager.sunAngles[1] += 1f; } - if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - SecondFrameTime > 1000) { - int[] pos = LightingManager.Lights[0].GetPosition(); - int[] pos2 = LightingManager.Lights[1].GetPosition(); - if (up && pos[0] > 0) + if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - _64ms > 64) + { + PlayerObject.UpdateCamera(); + _64ms = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; + int[] Pos = LightingManager.Lights[0].GetPosition(); + if (up && Pos[1] < 25) { - pos[0]--; - pos2[1]--; - } - else if (up) up = false; - else if (pos[0] < 10) + Pos[1]++; + } else if (up) up = false; + else if (Pos[1] > -5) { - pos[0]++; - pos2[1]++; + Pos[1]--; } - else up = true; - //LightingManager.Lights[0].SetPosition(pos); - //LightingManager.Lights[1].SetPosition(pos2); + else + { + up = true; + } + LightingManager.Lights[0].SetPosition(Pos); + } + if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - SecondFrameTime > 1000) + { TotalRamUsage = Math.Round(System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1000000.0); RamUsage = Math.Round(GC.GetTotalMemory(false) / 1000000.0); SecondFrameTime = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds; @@ -120,8 +131,9 @@ namespace VoxelIsometricRenderer Graphics g = e.Graphics; g.PixelOffsetMode = PixelOffsetMode.Half; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; - g.ScaleTransform(scale, scale); + g.ScaleTransform(TextureManager.RenderScale, TextureManager.RenderScale); ChunkRegistry.RenderChunk(g, 0); + PlayerObject.Draw(g); 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)); Frames++; @@ -131,9 +143,10 @@ namespace VoxelIsometricRenderer Display.Left = 0; Display.Top = 0; Display.Width = Width; - Display.Height = Height; + Display.Height = Height -32; width = Width; - height = Height; + height = Height -32; + PlayerObject.UpdateCameraCentre(Width, Height - 32); } private void ResizeAspectRatio(object sender, EventArgs e) { @@ -147,8 +160,8 @@ namespace VoxelIsometricRenderer private void KeyDownEvent(object sender, KeyEventArgs e) { - if (e.KeyCode == Keys.Add) scale += 0.25f; - if (e.KeyCode == Keys.Subtract) scale -= 0.25f; + if (e.KeyCode == Keys.Add) TextureManager.RenderScale += 0.25f; + if (e.KeyCode == Keys.Subtract) TextureManager.RenderScale -= 0.25f; if (e.KeyCode == Keys.Up) { LightingManager.sunAngles[1] += 10f; @@ -167,19 +180,19 @@ namespace VoxelIsometricRenderer } if (e.KeyCode == Keys.W) { - PlayerObject.IncrementPos(2, 1); + PlayerObject.IncrementPos(2, -1); } if (e.KeyCode == Keys.S) { - PlayerObject.IncrementPos(2, -1); + PlayerObject.IncrementPos(2, 1); } if (e.KeyCode == Keys.A) { - PlayerObject.IncrementPos(0, 1); + PlayerObject.IncrementPos(0, -1); } if (e.KeyCode == Keys.D) { - PlayerObject.IncrementPos(0, -1); + PlayerObject.IncrementPos(0, 1); } if (e.KeyCode == Keys.Space) { diff --git a/VoxelIsometricRenderer/Voxel.cs b/VoxelIsometricRenderer/Voxel.cs index 1006a19..1baa994 100644 --- a/VoxelIsometricRenderer/Voxel.cs +++ b/VoxelIsometricRenderer/Voxel.cs @@ -80,13 +80,17 @@ namespace VoxelIsometricRenderer RelativeBlockZ >= 0 && RelativeBlockZ < LightLevels.GetLength(2)) { LightLevel = LightLevels[RelativeBlockX,RelativeBlockY,RelativeBlockZ]; + float DistanceX = RelativeBlockX - CentreX; + float DistanceZ = RelativeBlockZ - CentreZ; + double CorrectLightLevel =Math.Atan2(RelativeBlockX - CentreX, RelativeBlockZ - CentreZ); int DirectionX = 0; if (BlocksLight) { switch (face) { case 0: - if (CentreZ < RelativeBlockZ) + CorrectLightLevel += (Math.PI * 0 / 180); + if (CentreZ > RelativeBlockZ) { DirectionX = 180; } @@ -94,13 +98,14 @@ namespace VoxelIsometricRenderer { DirectionX = 90; } - if (CentreZ > RelativeBlockZ) + if (CentreZ < RelativeBlockZ) { DirectionX = 0; } break; case 1: - if (CentreZ > RelativeBlockZ) + CorrectLightLevel += (Math.PI * 180 / 180); + if (CentreZ < RelativeBlockZ) { DirectionX = 180; } @@ -108,12 +113,13 @@ namespace VoxelIsometricRenderer { DirectionX = 90; } - if (CentreZ < RelativeBlockZ) + if (CentreZ > RelativeBlockZ) { DirectionX = 0; } break; case 2: + CorrectLightLevel += (Math.PI * 90 / 180); if (CentreX < RelativeBlockX) { DirectionX = 180; @@ -128,6 +134,7 @@ namespace VoxelIsometricRenderer } break; case 3: + CorrectLightLevel += (Math.PI * 270 / 180); if (CentreX > RelativeBlockX) { DirectionX = 180; @@ -170,37 +177,38 @@ namespace VoxelIsometricRenderer } break; } - /* if (CentreZ < RelativeBlockZ && RelativeBlockZ > RelativeBlockX) - { - for (int i = RelativeBlockZ; i < LightLevels.GetLength(2); i++) - { - LightLevels[RelativeBlockX, RelativeBlockY, i] = 0; - } - } else if (CentreZ > RelativeBlockZ && RelativeBlockZ < RelativeBlockX) - { - for (int i = RelativeBlockZ; i >= 0; i--) - { - LightLevels[RelativeBlockX, RelativeBlockY, i] = 0; - } - } else - { - if (CentreX < RelativeBlockX && RelativeBlockX > RelativeBlockZ) - { - for (int i = RelativeBlockX; i < LightLevels.GetLength(0); i++) - { - LightLevels[i, RelativeBlockY, RelativeBlockZ] = 0; - } - } - else if (CentreX > RelativeBlockX && RelativeBlockX < RelativeBlockZ) - { - for (int i = RelativeBlockZ; i >= 0; i--) - { - LightLevels[i, RelativeBlockY, RelativeBlockZ] = 0; - } - } - }*/ - LightLevel = (int)(LightLevel * ((1 + Math.Cos(Math.PI * DirectionX / 180)) / 2)); - // MessageBox.Show(LightLevel + ""); + /* if (CentreZ < RelativeBlockZ && RelativeBlockZ > RelativeBlockX) + { + for (int i = RelativeBlockZ; i < LightLevels.GetLength(2); i++) + { + LightLevels[RelativeBlockX, RelativeBlockY, i] = 0; + } + } else if (CentreZ > RelativeBlockZ && RelativeBlockZ < RelativeBlockX) + { + for (int i = RelativeBlockZ; i >= 0; i--) + { + LightLevels[RelativeBlockX, RelativeBlockY, i] = 0; + } + } else + { + if (CentreX < RelativeBlockX && RelativeBlockX > RelativeBlockZ) + { + for (int i = RelativeBlockX; i < LightLevels.GetLength(0); i++) + { + LightLevels[i, RelativeBlockY, RelativeBlockZ] = 0; + } + } + else if (CentreX > RelativeBlockX && RelativeBlockX < RelativeBlockZ) + { + for (int i = RelativeBlockZ; i >= 0; i--) + { + LightLevels[i, RelativeBlockY, RelativeBlockZ] = 0; + } + } + }*/ + LightLevel = face > 3? (int)(LightLevel * ((1 + Math.Cos(Math.PI * DirectionX / 180)) / 2)) : (int)(LightLevel * ((1 + Math.Cos(CorrectLightLevel)) / 2)); + //LightLevel = (int)(LightLevel * ((1 + Math.Cos(Math.PI * DirectionX / 180)) / 2)); + // MessageBox.Show(LightLevel + ""); } } return LightLevel; @@ -229,14 +237,16 @@ namespace VoxelIsometricRenderer { int[] Distances = new int[] { CentreX - PosX, CentreY - PosY, CentreZ - PosZ}; int[] Centres = new int[] { CentreX, CentreY, CentreZ }; - float[] LightLevel = new float[] {255.0f/ RangeX * (MaxLevel / 255.0f), 255.0f/ RangeY * (MaxLevel / 255.0f), 255.0f/ RangeZ * (MaxLevel / 255.0f) }; + float[] LightLevel = new float[] {(255.0f/ RangeX) * (MaxLevel / 255.0f), (255.0f/ RangeY) * (MaxLevel / 255.0f), (255.0f/ RangeZ) * (MaxLevel / 255.0f) }; float TotalLightLevel = 0; int Dimensions = 0; for (int i = 0; i < 3; i++) { Distances[i] = Distances[i] > 0 ? Distances[i] : -Distances[i]; } - LightLevel = new float[] { LightLevel[0] * (float)Distances[0], LightLevel[1] * (float)Distances[1], LightLevel[2] * (float)Distances[2] }; + //if (PosY < CentreY && PosX == CentreX && PosZ == CentreZ) { MessageBox.Show(Distances[1] + ""); } + //if (RangeX >= Distances[0] && PosY == CentreX && PosZ == CentreZ) MessageBox.Show(String.Format("LightLevel: {0}\nRange:{1}\nDistance:{2}\nRange/Distance:{3}", LightLevel[0], RangeX, Distances[0], RangeX / (float)Distances[0])); + LightLevel = new float[] { RangeX >= Distances[0] ? LightLevel[0] * (RangeX / ((float)Distances[0] > 0 ? (float)Distances[0] : 1)) : 0, RangeY >= Distances[1] ? LightLevel[1] * (RangeY / ((float)Distances[1] > 0 ? (float)Distances[1] : 1)) : 0, RangeZ >= Distances[2] ? LightLevel[2] * (RangeZ / ((float)Distances[2] > 0 ? (float)Distances[2] : 1)) : 0 }; for (int i = 0; i < 3; i++) { if (Centres[i] >= 0) @@ -343,7 +353,7 @@ namespace VoxelIsometricRenderer int DistX = WorldX - (Light.GetPosition()[0] - Light.GetRadius()[0]); int DistY = WorldY - (Light.GetPosition()[1] - Light.GetRadius()[1]); int DistZ = WorldZ - (Light.GetPosition()[2] - Light.GetRadius()[2]); - if (DistX > 0 && DistY > 0 && DistZ > 0 && DistX - Light.GetRadius()[0] < 0 && DistY - Light.GetRadius()[1] < 0 && DistZ - Light.GetRadius()[2] < 0) + if (DistX >= 0 && DistY >= 0 && DistZ >= 0 && DistX - Light.GetRadius()[0] <= 0 && DistY - Light.GetRadius()[1] <= 0 && DistZ - Light.GetRadius()[2] <= 0) { //MessageBox.Show("X: " + DistX + " Y: " + DistY + " Z: " + DistZ); float[] LightRGB = new float[3]; @@ -356,7 +366,6 @@ namespace VoxelIsometricRenderer } }); - //return RGBLevels; return new float[] { RGBLevels[0] != 0 ? RGBLevels[0]/LightCount : 0, RGBLevels[1] != 0 ? RGBLevels[1]/LightCount : 0, RGBLevels[2] != 0 ? RGBLevels[2]/LightCount : 0 }; } public static void UpdateLighting() @@ -367,11 +376,11 @@ namespace VoxelIsometricRenderer if (sunAngles[0] > 360) sunAngles[0] -= 360; if (sunAngles[1] > 360) sunAngles[1] -= 360; if (sunAngles[0] <= -360) sunAngles[0] += 360; - if (sunAngles[1] <= -360) sunAngles[1] += 360; + if (sunAngles[1] <= -360) sunAngles[1] += 360; float CurrentAngle = 0; int AngleIndex = 0; - float MoonAngle = (float)((1 + Math.Sin(Math.PI * (sunAngles[1] - 60) / 180)) / 2.0); - float SunLightAngle = (float)((1 + Math.Cos(Math.PI * sunAngles[1] / 180)) / 1.8); + float MoonAngle = (float)((1 + Math.Sin(Math.PI * (sunAngles[1] - 90) / 180)) / 2.0); + float SunLightAngle = (float)((1 + Math.Cos(Math.PI * sunAngles[1] / 180)) / 2.0); for (int i = 0; i < AngleRanges.Length; i++) { if (sunAngles[0] < AngleRanges[i]) @@ -392,7 +401,7 @@ namespace VoxelIsometricRenderer } for (int i = SkyLightModifiedValues.Length - 2; i < SkyLightModifiedValues.Length; i++) { - SkyLightModifiedValues[i] = (float)Math.Min(Math.Max(SunLightAngle, MoonAngle/2) + (MoonAngle/ 4),MaxBloom); + SkyLightModifiedValues[i] = (float)Math.Min(SunLightAngle + (MoonAngle/ 2),MaxBloom); } for (int i = 0; i < AmbientColouring.Length; i++) { @@ -401,7 +410,7 @@ namespace VoxelIsometricRenderer } AmbientColouring[2] -= sunAngles[1] > 180 || sunAngles[1] < 90 ? Square(Square((float)(Math.Max(Math.Cos(Math.PI * (sunAngles[1] + 45) / 180), 0)))) / 2.0f : 0;//(float)(Math.Max(Math.Cos(Math.PI * (sunAngles[1] + 45) / 180),0)); AmbientColouring[1] -= sunAngles[1] > 180 || sunAngles[1] < 90 ? Square(Square((float)(Math.Max(Math.Cos(Math.PI * (sunAngles[1] + 45) / 180), 0))) / 3.0f) : 0;// (float)(Math.Max(Math.Cos(Math.PI * (sunAngles[1] + 45) / 180),0))/2.0f; - SkyLightMetrics[0] = (float)Math.Max(SunLightAngle, 0.1f) + (float)(MoonAngle / 5); + SkyLightMetrics[0] = (float)SunLightAngle + (float)(MoonAngle / 5); SkyLightMetrics[2] = MoonAngle; } public static float Square(float ValueIn) @@ -415,6 +424,8 @@ namespace VoxelIsometricRenderer public static List PreCompiledModels = new List(); public static Bitmap[,] GeneratedFaceTextures = new Bitmap[0,0]; public static int VoxelCount = 0; + public static int VoxelSize = 32; + public static float RenderScale = 2.0f; public static Bitmap GetModel(int modelNum) { return PreCompiledModels[modelNum]; @@ -447,8 +458,7 @@ namespace VoxelIsometricRenderer } public static float GetShadingValue(float LightValue, int RGBIndex, int FaceIndex, float[] Lights) { - //if (Lights[RGBIndex] > 0) MessageBox.Show(Lights[RGBIndex] + ""); - return ((float)Math.Max(LightValue * LightingManager.AmbientColouring[RGBIndex], LightingManager.AmbientMinimums[FaceIndex])) + Lights[RGBIndex] * 10; + return ((float)Math.Max(LightValue * LightingManager.AmbientColouring[RGBIndex], LightingManager.AmbientMinimums[FaceIndex])) + Lights[RGBIndex]; } public static ImageAttributes GetShadow(int face) { return GetShadow(face, 0, 0,0,0, false); } public static ImageAttributes GetShadow(int face, int WorldX, int WorldY, int WorldZ, int ShadowLevel, bool BlocksLight) @@ -1191,15 +1201,20 @@ namespace VoxelIsometricRenderer if (!block_pos_lookup.Contains(pos_lookup)) return -1; return block_pos_lookup.IndexOf(pos_lookup); } - public static int[] GetDrawLocation(int x, int y, int z, int offsetX, int offsetY, int offsetZ) + public static int[] GetDrawLocation(int x, int y, int z, float offsetX, float offsetY, float offsetZ) { return new int[]{(int)((((double)(x + offsetX) * 0.5) + ((double)(z + offsetZ) * -0.5))*(double)BlockSize), (int)((((double)(x + offsetX) * 0.5) + ((double)(z + offsetZ) * 0.5) - (-offsetY + (y)))*((double)BlockSize/2.0)) }; } public static void RenderBlock(Graphics g, int BlockID,int WorldX, int WorldY, int WorldZ, int ShadowLevel) { - int[] location = GetDrawLocation(WorldX, WorldY, WorldZ, PlayerObject.GetPos()[0], PlayerObject.GetPos()[1], PlayerObject.GetPos()[2]); - VoxelRegistry.FetchVoxel(blocks[BlockID].FetchVoxelID()).Draw(g, location[0] + (Terrain4.width/4) , location[1] + (Terrain4.height/2), WorldX, WorldY, WorldZ, ShadowLevel, FetchBlockState(BlockID).hasBlocksLight()); + RenderBlock(g, BlockID, WorldX, WorldY, WorldZ, ShadowLevel, false); + } + public static void RenderBlock(Graphics g, int BlockID,int WorldX, int WorldY, int WorldZ, int ShadowLevel, bool player) + { + int[] locationWOffset = GetDrawLocation(WorldX, WorldY, WorldZ, PlayerObject.GetCameraPos()[0], PlayerObject.GetCameraPos()[1], PlayerObject.GetCameraPos()[2]); + int [] location = GetDrawLocation(WorldX, WorldY, WorldZ, 0,0,0); + VoxelRegistry.FetchVoxel(blocks[BlockID].FetchVoxelID()).Draw(g, (player? location[0] : locationWOffset[0])+ (int)(Terrain4.width / (2 * TextureManager.RenderScale)), (player ? location[1] : locationWOffset[1]) + (int)(Terrain4.height / (2 * TextureManager.RenderScale)), WorldX, WorldY, WorldZ, ShadowLevel, FetchBlockState(BlockID).hasBlocksLight()); } public static int CheckInBlock(Block block,int x, int y, int z) {