From 9d52f77b438858eb90384e4a788f3c963914666e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Wed, 3 Mar 2021 16:34:25 +0100 Subject: [PATCH] Switched to Int32 vector from Point --- Sandbox/src/VoxelFun/ChunkManager.bf | 86 ++++++-------------------- Sandbox/src/VoxelFun/VoxelChunk.bf | 2 +- Sandbox/src/VoxelFun/VoxelTestLayer.bf | 45 ++++---------- Sandbox/src/VoxelFun/World.bf | 24 +++---- 4 files changed, 45 insertions(+), 112 deletions(-) diff --git a/Sandbox/src/VoxelFun/ChunkManager.bf b/Sandbox/src/VoxelFun/ChunkManager.bf index 6bc7d21..f2803e1 100644 --- a/Sandbox/src/VoxelFun/ChunkManager.bf +++ b/Sandbox/src/VoxelFun/ChunkManager.bf @@ -16,53 +16,7 @@ namespace Sandbox.VoxelFun public GeometryBinding Geometry ~ _?.ReleaseRef(); public VoxelChunk Data; public Matrix Transform; - public Point3 Position; - } - - public struct Point3 : IHashable - { - public int X, Y, Z; - - public this() => this = default; - - public this(int x, int y, int z) - { - X = x; - Y = y; - Z = z; - } - - public int GetHashCode() - { - return (((X * 39) ^ Y) * 39) ^ Z; - } - - public static int DistanceSq(Point3 value1, Point3 value2) - { - int dstX = value1.X - value2.X; - int dstY = value1.Y - value2.Y; - int dstZ = value1.Z - value2.Z; - - return dstX * dstX + dstY * dstY + dstZ * dstZ; - } - - public static Point3 operator +(Point3 left, Point3 right) => .(left.X + right.X, left.Y + right.Y, left.Z + right.Z); - public static Point3 operator -(Point3 left, Point3 right) => .(left.X - right.X, left.Y - right.Y, left.Z - right.Z); - public static Point3 operator *(Point3 left, Point3 right) => .(left.X * right.X, left.Y * right.Y, left.Z * right.Z); - public static Point3 operator /(Point3 left, Point3 right) => .(left.X / right.X, left.Y / right.Y, left.Z / right.Z); - public static Point3 operator %(Point3 left, Point3 right) => .(left.X % right.X, left.Y % right.Y, left.Z % right.Z); - - public Point3 Abs() - { - return .(Math.Abs(X), Math.Abs(Y), Math.Abs(Z)); - } - - public static explicit operator Vector3(Point3 point) => .(point.X, point.Y, point.Z); - - public static explicit operator Point3(Vector3 point) => .((int)point.X, (int)point.Y, (int)point.Z); - - public static bool operator ==(Point3 left, Point3 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z; - public static bool operator !=(Point3 left, Point3 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z; + public Int32_3 Position; } public class ChunkManager @@ -74,7 +28,7 @@ namespace Sandbox.VoxelFun private String _chunkBasePath ~ delete _; - Dictionary _chunks = new .() ~ DeleteDictionaryAndValues!(_); + Dictionary _chunks = new .() ~ DeleteDictionaryAndValues!(_); public Texture2D Texture ~ _?.ReleaseRef(); public Effect TextureEffect ~ _?.ReleaseRef(); @@ -85,8 +39,8 @@ namespace Sandbox.VoxelFun bool stopChunkLoader; Monitor chunkListLock = new Monitor() ~ delete _; - Point3 chunkPosition; - Point3 oldChunkPosition = .(Int.MaxValue, Int.MaxValue, Int.MaxValue); + Int32_3 chunkPosition; + Int32_3 oldChunkPosition = .(Int.MaxValue, Int.MaxValue, Int.MaxValue); Monitor chunkPosLock = new Monitor() ~ delete _; @@ -125,7 +79,7 @@ namespace Sandbox.VoxelFun * Returns whether or not the chunk with the given coordinate is currently loaded. */ [Inline] - public bool IsChunkLoaded(Point3 chunkCoordinate) + public bool IsChunkLoaded(Int32_3 chunkCoordinate) { using(chunkListLock.Enter()) { @@ -137,7 +91,7 @@ namespace Sandbox.VoxelFun * Returns the chunk with the given chunk coordinate or null if the chunk isn't loaded. */ [Inline] - public Chunk GetChunk(Point3 chunkCoordinate) + public Chunk GetChunk(Int32_3 chunkCoordinate) { using(chunkListLock.Enter()) { @@ -148,7 +102,7 @@ namespace Sandbox.VoxelFun /** * Loads and returns the chunk with the given coordinate. If the chunk doesn't exist it will be generated. */ - public Chunk LoadChunk(Point3 chunkCoordinate) + public Chunk LoadChunk(Int32_3 chunkCoordinate) { Chunk chunk = GetChunk(chunkCoordinate); @@ -175,7 +129,7 @@ namespace Sandbox.VoxelFun return chunk; } - void SetChunkPos(Point3 chunkPos) + void SetChunkPos(Int32_3 chunkPos) { chunkPosLock.Enter(); @@ -191,7 +145,7 @@ namespace Sandbox.VoxelFun { Vector3 chunkPosition = cameraPosition / .(VoxelChunk.SizeX, VoxelChunk.SizeY, VoxelChunk.SizeZ); - Point3 p = (Point3)chunkPosition; + Int32_3 p = (Int32_3)chunkPosition; p.Y = 0; SetChunkPos(p); @@ -202,8 +156,8 @@ namespace Sandbox.VoxelFun */ void ChunkLoaderThread_Entry() { - Point3 curChunkPosition = .(0, 0, 0); - Point3 oldChunkPosition = .(Int.MaxValue, Int.MaxValue, Int.MaxValue); + Int32_3 curChunkPosition = .(0, 0, 0); + Int32_3 oldChunkPosition = .(Int.MaxValue, Int.MaxValue, Int.MaxValue); while(!stopChunkLoader) { @@ -225,11 +179,11 @@ namespace Sandbox.VoxelFun } } - public void ChunkLoaderThread_GenerateChunks(Point3 chunkPos) + public void ChunkLoaderThread_GenerateChunks(Int32_3 chunkPos) { for(var chunk in _chunks) { - Point3 dist = (chunk.key - chunkPos).Abs(); + Int32_3 dist = (chunk.key - chunkPos).Abs(); if(dist.X > _viewDistance || dist.Z > _viewDistance) { @@ -247,25 +201,25 @@ namespace Sandbox.VoxelFun { for(; x < r; x++) { - Point3 chunkCoordinate = (Point3)chunkPos + .(x, 0, z); + Int32_3 chunkCoordinate = (Int32_3)chunkPos + .(x, 0, z); LoadChunk(chunkCoordinate); } for(; z < r; z++) { - Point3 chunkCoordinate = (Point3)chunkPos + .(x, 0, z); + Int32_3 chunkCoordinate = (Int32_3)chunkPos + .(x, 0, z); LoadChunk(chunkCoordinate); } for(; x > -r; x--) { - Point3 chunkCoordinate = (Point3)chunkPos + .(x, 0, z); + Int32_3 chunkCoordinate = (Int32_3)chunkPos + .(x, 0, z); LoadChunk(chunkCoordinate); } for(; z > -r; z--) { - Point3 chunkCoordinate = (Point3)chunkPos + .(x, 0, z); + Int32_3 chunkCoordinate = (Int32_3)chunkPos + .(x, 0, z); LoadChunk(chunkCoordinate); } } @@ -276,14 +230,14 @@ namespace Sandbox.VoxelFun for(int z = -_viewDistance; z < _viewDistance; z++) { int y = 0; - Point3 chunkCoordinate = (Point3)chunkPos + .(x, y, z); + Int32_3 chunkCoordinate = (Int32_3)chunkPos + .(x, y, z); LoadChunk(chunkCoordinate); } */ } - Result LoadChunkFromFile(Point3 chunkCoordinate, Chunk outChunk) + Result LoadChunkFromFile(Int32_3 chunkCoordinate, Chunk outChunk) { String chunkFileName = scope String(_chunkBasePath); chunkFileName.AppendF($"{chunkCoordinate.X}_{chunkCoordinate.Y}_{chunkCoordinate.Z}.cdata"); @@ -302,7 +256,7 @@ namespace Sandbox.VoxelFun return .Ok; } - Result SaveChunkToFile(Point3 chunkCoordinate, Chunk outChunk) + Result SaveChunkToFile(Int32_3 chunkCoordinate, Chunk outChunk) { String chunkFileName = scope String(_chunkBasePath); chunkFileName.AppendF($"{chunkCoordinate.X}_{chunkCoordinate.Y}_{chunkCoordinate.Z}.cdata"); diff --git a/Sandbox/src/VoxelFun/VoxelChunk.bf b/Sandbox/src/VoxelFun/VoxelChunk.bf index e8b6fdf..ef784ff 100644 --- a/Sandbox/src/VoxelFun/VoxelChunk.bf +++ b/Sandbox/src/VoxelFun/VoxelChunk.bf @@ -8,7 +8,7 @@ namespace Sandbox.VoxelFun public const int SizeY = 256; public const int SizeZ = 16; - public const Point3 Size = .(SizeX, SizeY, SizeZ); + public const Int32_3 Size = .(SizeX, SizeY, SizeZ); public const Vector3 VectorSize = .(SizeX, SizeY, SizeZ); public uint8[SizeX][SizeY][SizeZ] Data; diff --git a/Sandbox/src/VoxelFun/VoxelTestLayer.bf b/Sandbox/src/VoxelFun/VoxelTestLayer.bf index 82ec78e..5872217 100644 --- a/Sandbox/src/VoxelFun/VoxelTestLayer.bf +++ b/Sandbox/src/VoxelFun/VoxelTestLayer.bf @@ -390,43 +390,27 @@ namespace Sandbox.VoxelFun _ge_logo.Bind(); Renderer.Submit(_quadGeometryBinding, _textureEffect, .Scaling(1.5f)); - //Ray ray = .(.(0.5f, 128.5f, 0.5f), .(0, -0.1f, -0.1f)); - _texture.Bind(); - Matrix mat = .Translation(ray.Start); + intersectInfo.Coordinate = _world.RaycastBlock(.(_camera.Position, _camera.Transform.Forward), 10, _cubeGeo, _textureEffect, out intersectInfo.Location, out intersectInfo.Face); - Matrix mat2 = .Identity; - mat2.Up = .(0, 1, 0); - - mat2.Forward = ray.Direction; - mat2.Forward.Normalize(); + Renderer.Submit(_cubeGeo, _textureEffect, .Translation((Vector3)intersectInfo.Coordinate)); - mat2.Right = Vector3.Cross(mat2.Up, mat2.Forward); - mat2.Right.Normalize(); - - mat2.Up = Vector3.Cross(mat2.Forward, mat2.Right); - mat2.Up.Normalize(); - - Matrix mat3 = mat * mat2; - - Renderer.Submit(_lineGeometryBinding, _textureEffect, mat3); - - //Point3 lookedAtBlock = _world.RaycastBlock(ray, 10, _cubeGeo, _textureEffect); - Point3 lookedAtBlock = _world.RaycastBlock(.(_camera.Position, _camera.Transform.Forward), 10, _cubeGeo, _textureEffect, let intersection, let intersectFace); - - intersectFace.ToString(intersectedFaceName..Clear()); - - Renderer.Submit(_cubeGeo, _textureEffect, .Translation((Vector3)lookedAtBlock)); - - Renderer.Submit(_cubeGeo, _textureEffect, .Translation(intersection) * .Scaling(0.1f) * .Translation(-0.5f, -0.5f, -0.5f)); + Renderer.Submit(_cubeGeo, _textureEffect, .Translation(intersectInfo.Location) * .Scaling(0.1f) * .Translation(-0.5f.XXX)); _world.ChunkManager.Draw(); Renderer.EndScene(); } - String intersectedFaceName = new String() ~ delete _; + struct IntersectionInfo + { + public Int32_3 Coordinate; + public Vector3 Location; + public BlockFace Face; + } + + IntersectionInfo intersectInfo; ColorRGBA _squareColor0 = ColorRGBA.CornflowerBlue; ColorRGBA _squareColor1; @@ -454,7 +438,7 @@ namespace Sandbox.VoxelFun ImGui.Begin("Test"); - ImGui.LabelText("Intersected Face", intersectedFaceName); + ImGui.LabelText("Looked at block", $"Coord: {intersectInfo.Coordinate}, Block Face: {intersectInfo.Face}, Location: {intersectInfo.Location}"); ImGui.DragFloat("Slow Speed", &movementSpeed, 1.0f, 0.01f, 100.0f); ImGui.DragFloat("Fast Speed", &movementSpeedFast, 1.0f, 1f, 10000.0f); @@ -473,11 +457,6 @@ namespace Sandbox.VoxelFun _squareColor1 = ColorRGBA.White - _squareColor0; - - ImGui.DragFloat3("Ray Position", *(float[3]*)(void*)&ray.Start, 0.1f, float.NegativeInfinity, float.PositiveInfinity); - - ImGui.DragFloat3("Ray Direction", *(float[3]*)(void*)&ray.Direction, 0.001f, -1f, 1.0f); - _camera.Position = camPos; diff --git a/Sandbox/src/VoxelFun/World.bf b/Sandbox/src/VoxelFun/World.bf index 215e0ce..007028e 100644 --- a/Sandbox/src/VoxelFun/World.bf +++ b/Sandbox/src/VoxelFun/World.bf @@ -97,11 +97,11 @@ namespace Sandbox.VoxelFun } /* - public Point3 GetChunkCoordinate(Vector3 blockPosition) + public Int32_3 GetChunkCoordinate(Vector3 blockPosition) { Vector3 v = blockPosition / (Vector3)VoxelChunk.Size; - Point3 p = .(); + Int32_3 p = .(); p.X = (int)Math.Round(v.X); p.Y = (int)Math.Round(v.Y); p.Z = (int)Math.Round(v.Z); @@ -125,9 +125,9 @@ namespace Sandbox.VoxelFun /** * Calculates the coordinate of the chunk that contains the given block coordinate. */ - public Point3 ChunkCoordFromBlockCoord(Point3 blockCoordinate) + public Int32_3 ChunkCoordFromBlockCoord(Int32_3 blockCoordinate) { - Point3 coordinate = blockCoordinate; + Int32_3 coordinate = blockCoordinate; if(coordinate.X < 0) coordinate.X -= VoxelChunk.Size.X; @@ -146,9 +146,9 @@ namespace Sandbox.VoxelFun * For Example: Consider the block at the global location of (25, 17, -12) * The coordinate of this block inside its chunk would be (9, 17, 4) because the chunk starts at (16, 0, -16) */ - public Point3 BlockCoordInChunk(Point3 blockCoordinate) + public Int32_3 BlockCoordInChunk(Int32_3 blockCoordinate) { - Point3 chunkPosition = blockCoordinate % VoxelChunk.Size; + Int32_3 chunkPosition = blockCoordinate % VoxelChunk.Size; if(chunkPosition.X < 0) chunkPosition.X += VoxelChunk.Size.X; @@ -181,18 +181,18 @@ namespace Sandbox.VoxelFun return Vector3.Floor(chunkPosition); } - public Point3 RaycastBlock(Ray ray, float maxDistance, GeometryBinding geo, Effect effect, out Vector3 intersectionPosition, out BlockFace intersectionFace) + public Int32_3 RaycastBlock(Ray ray, float maxDistance, GeometryBinding geo, Effect effect, out Vector3 intersectionPosition, out BlockFace intersectionFace) { Vector3 start = ray.Start; Vector3 dir = ray.Direction.Normalized(); Vector3 unitStepSize = .((dir / dir.X).Magnitude(), (dir / dir.Y).Magnitude(), (dir / dir.Z).Magnitude()); - Point3 walker = (Point3)Vector3.Floor(start); + Int32_3 walker = (Int32_3)Vector3.Floor(start); Vector3 rayLengths = .(); - Point3 step; + Int32_3 step; BlockFace xFace; BlockFace yFace; @@ -280,11 +280,11 @@ namespace Sandbox.VoxelFun } } - Point3 chunkCoordinate = ChunkCoordFromBlockCoord(walker); + Int32_3 chunkCoordinate = ChunkCoordFromBlockCoord(walker); Chunk chunk = _chunkManager.LoadChunk(chunkCoordinate); - Point3 coordInChunk = BlockCoordInChunk(walker); + Int32_3 coordInChunk = BlockCoordInChunk(walker); uint8 blockData = chunk.Data.Data[coordInChunk.X][coordInChunk.Y][coordInChunk.Z]; @@ -299,7 +299,7 @@ namespace Sandbox.VoxelFun intersectionFace = .None; intersectionPosition = .(float.NaN); - return .(int.MaxValue, int.MaxValue, int.MaxValue); + return .(int32.MaxValue, int32.MaxValue, int32.MaxValue); } } }