diff --git a/Player.cs b/Player.cs index 0885e1a..8b67037 100644 --- a/Player.cs +++ b/Player.cs @@ -23,7 +23,7 @@ namespace Voxel public void PlaceBlock() { - var (success, hit, x, y, z, normal) = _world.Raycast(Camera.Position, Camera.Front.Normalized(), 10); + var (success, hit, x, y, z, normal) = _world.Raycast(Camera.Position, Camera.Front.Normalized(), 8); if (!success) return; x += normal.X; @@ -35,7 +35,7 @@ namespace Voxel public void BreakBlock() { - var (success, hit, x, y, z, normal) = _world.Raycast(Camera.Position, Camera.Front.Normalized(), 10); + var (success, hit, x, y, z, normal) = _world.Raycast(Camera.Position, Camera.Front.Normalized(), 8); if (!success) return; _world.SetBlock(x, y, z, Blocks.Air); diff --git a/Shaders/shader.vert b/Shaders/shader.vert index 31b9f16..c7cd954 100644 --- a/Shaders/shader.vert +++ b/Shaders/shader.vert @@ -25,52 +25,52 @@ const float lightMult[6] = float[6](0.6, 0.6, 1.0, 0.5, 0.8, 0.8); const vec3 offsets[6][6] = vec3[6][6]( vec3[6]( // +X - vec3(0.5, -0.5, -0.5), - vec3(0.5, 0.5, 0.5), - vec3(0.5, -0.5, 0.5), - vec3(0.5, 0.5, 0.5), - vec3(0.5, -0.5, -0.5), - vec3(0.5, 0.5, -0.5) + vec3(1, 0, 0), + vec3(1, 1, 1), + vec3(1, 0, 1), + vec3(1, 1, 1), + vec3(1, 0, 0), + vec3(1, 1, 0) ), vec3[6]( // -X - vec3(-0.5, -0.5, 0.5), - vec3(-0.5, 0.5, -0.5), - vec3(-0.5, -0.5, -0.5), - vec3(-0.5, 0.5, -0.5), - vec3(-0.5, -0.5, 0.5), - vec3(-0.5, 0.5, 0.5) + vec3(0, 0, 1), + vec3(0, 1, 0), + vec3(0, 0, 0), + vec3(0, 1, 0), + vec3(0, 0, 1), + vec3(0, 1, 1) ), vec3[6]( // +Y - vec3(-0.5, 0.5, -0.5), - vec3( 0.5, 0.5, 0.5), - vec3( 0.5, 0.5, -0.5), - vec3( 0.5, 0.5, 0.5), - vec3(-0.5, 0.5, -0.5), - vec3(-0.5, 0.5, 0.5) + vec3(0, 1, 0), + vec3(1, 1, 1), + vec3(1, 1, 0), + vec3(1, 1, 1), + vec3(0, 1, 0), + vec3(0, 1, 1) ), - vec3[6]( - vec3(-0.5, -0.5, 0.5), - vec3( 0.5, -0.5, -0.5), - vec3( 0.5, -0.5, 0.5), - vec3( 0.5, -0.5, -0.5), - vec3(-0.5, -0.5, 0.5), - vec3(-0.5, -0.5, -0.5) + vec3[6]( // -Y + vec3(0, 0, 1), + vec3(1, 0, 0), + vec3(1, 0, 1), + vec3(1, 0, 0), + vec3(0, 0, 1), + vec3(0, 0, 0) ), vec3[6]( // +Z - vec3( 0.5, -0.5, 0.5), - vec3(-0.5, 0.5, 0.5), - vec3(-0.5, -0.5, 0.5), - vec3(-0.5, 0.5, 0.5), - vec3( 0.5, -0.5, 0.5), - vec3( 0.5, 0.5, 0.5) + vec3(1, 0, 1), + vec3(0, 1, 1), + vec3(0, 0, 1), + vec3(0, 1, 1), + vec3(1, 0, 1), + vec3(1, 1, 1) ), vec3[6]( // -Z - vec3(-0.5, -0.5, -0.5), - vec3( 0.5, 0.5, -0.5), - vec3( 0.5, -0.5, -0.5), - vec3( 0.5, 0.5, -0.5), - vec3(-0.5, -0.5, -0.5), - vec3(-0.5, 0.5, -0.5) + vec3(0, 0, 0), + vec3(1, 1, 0), + vec3(1, 0, 0), + vec3(1, 1, 0), + vec3(0, 0, 0), + vec3(0, 1, 0) ) ); diff --git a/World.cs b/World.cs index ddc5d8b..54ffab4 100644 --- a/World.cs +++ b/World.cs @@ -73,14 +73,11 @@ namespace Voxel float tDeltaY = direction.Y != 0 ? MathF.Abs(1 / direction.Y) : float.MaxValue; float tDeltaZ = direction.Z != 0 ? MathF.Abs(1 / direction.Z) : float.MaxValue; - float tMaxX = direction.X > 0 - ? (MathF.Ceiling(origin.X) - origin.X) * tDeltaX + float tMaxX = direction.X > 0 ? (MathF.Floor(origin.X) + 1 - origin.X) * tDeltaX : (origin.X - MathF.Floor(origin.X)) * tDeltaX; - float tMaxY = direction.Y > 0 - ? (MathF.Ceiling(origin.Y) + 1 - origin.Y) * tDeltaY + float tMaxY = direction.Y > 0 ? (MathF.Floor(origin.Y) + 1 - origin.Y) * tDeltaY : (origin.Y - MathF.Floor(origin.Y)) * tDeltaY; - float tMaxZ = direction.Z > 0 - ? (MathF.Ceiling(origin.Z) + 1 - origin.Z) * tDeltaZ + float tMaxZ = direction.Z > 0 ? (MathF.Floor(origin.Z) + 1 - origin.Z) * tDeltaZ : (origin.Z - MathF.Floor(origin.Z)) * tDeltaZ; float distance = 0f; @@ -89,36 +86,34 @@ namespace Voxel while (distance <= maxDistance) { Blocks block = GetBlock(x, y, z); - if (block != Blocks.Air) - { - return (true, block, x, y, z, normal); - } - // step to next voxel + if (block != Blocks.Air) + return (true, block, x, y, z, normal); + if (tMaxX < tMaxY && tMaxX < tMaxZ) { x += stepX; + normal = new Vector3i(-stepX, 0, 0); distance = tMaxX; tMaxX += tDeltaX; - normal = new Vector3i(stepX > 0 ? -1 : 1, 0, 0); } else if (tMaxY < tMaxZ) { y += stepY; + normal = new Vector3i(0, -stepY, 0); distance = tMaxY; tMaxY += tDeltaY; - normal = new Vector3i(0, stepY > 0 ? -1 : 1, 0); } else { z += stepZ; + normal = new Vector3i(0, 0, -stepZ); distance = tMaxZ; tMaxZ += tDeltaZ; - normal = new Vector3i(0, 0, stepZ > 0 ? -1 : 1); } } - return (false, Blocks.Air, 0, 0, 0, normal); + return (false, Blocks.Air, 0, 0, 0, Vector3i.Zero); } public void Update()