fix render offset

This commit is contained in:
2025-09-21 22:55:22 +02:00
parent e1bb0b3683
commit 81ef6d8a29
3 changed files with 49 additions and 54 deletions

View File

@@ -23,7 +23,7 @@ namespace Voxel
public void PlaceBlock() 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; if (!success) return;
x += normal.X; x += normal.X;
@@ -35,7 +35,7 @@ namespace Voxel
public void BreakBlock() 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; if (!success) return;
_world.SetBlock(x, y, z, Blocks.Air); _world.SetBlock(x, y, z, Blocks.Air);

View File

@@ -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]( const vec3 offsets[6][6] = vec3[6][6](
vec3[6]( // +X vec3[6]( // +X
vec3(0.5, -0.5, -0.5), vec3(1, 0, 0),
vec3(0.5, 0.5, 0.5), vec3(1, 1, 1),
vec3(0.5, -0.5, 0.5), vec3(1, 0, 1),
vec3(0.5, 0.5, 0.5), vec3(1, 1, 1),
vec3(0.5, -0.5, -0.5), vec3(1, 0, 0),
vec3(0.5, 0.5, -0.5) vec3(1, 1, 0)
), ),
vec3[6]( // -X vec3[6]( // -X
vec3(-0.5, -0.5, 0.5), vec3(0, 0, 1),
vec3(-0.5, 0.5, -0.5), vec3(0, 1, 0),
vec3(-0.5, -0.5, -0.5), vec3(0, 0, 0),
vec3(-0.5, 0.5, -0.5), vec3(0, 1, 0),
vec3(-0.5, -0.5, 0.5), vec3(0, 0, 1),
vec3(-0.5, 0.5, 0.5) vec3(0, 1, 1)
), ),
vec3[6]( // +Y vec3[6]( // +Y
vec3(-0.5, 0.5, -0.5), vec3(0, 1, 0),
vec3( 0.5, 0.5, 0.5), vec3(1, 1, 1),
vec3( 0.5, 0.5, -0.5), vec3(1, 1, 0),
vec3( 0.5, 0.5, 0.5), vec3(1, 1, 1),
vec3(-0.5, 0.5, -0.5), vec3(0, 1, 0),
vec3(-0.5, 0.5, 0.5) vec3(0, 1, 1)
), ),
vec3[6]( vec3[6]( // -Y
vec3(-0.5, -0.5, 0.5), vec3(0, 0, 1),
vec3( 0.5, -0.5, -0.5), vec3(1, 0, 0),
vec3( 0.5, -0.5, 0.5), vec3(1, 0, 1),
vec3( 0.5, -0.5, -0.5), vec3(1, 0, 0),
vec3(-0.5, -0.5, 0.5), vec3(0, 0, 1),
vec3(-0.5, -0.5, -0.5) vec3(0, 0, 0)
), ),
vec3[6]( // +Z vec3[6]( // +Z
vec3( 0.5, -0.5, 0.5), vec3(1, 0, 1),
vec3(-0.5, 0.5, 0.5), vec3(0, 1, 1),
vec3(-0.5, -0.5, 0.5), vec3(0, 0, 1),
vec3(-0.5, 0.5, 0.5), vec3(0, 1, 1),
vec3( 0.5, -0.5, 0.5), vec3(1, 0, 1),
vec3( 0.5, 0.5, 0.5) vec3(1, 1, 1)
), ),
vec3[6]( // -Z vec3[6]( // -Z
vec3(-0.5, -0.5, -0.5), vec3(0, 0, 0),
vec3( 0.5, 0.5, -0.5), vec3(1, 1, 0),
vec3( 0.5, -0.5, -0.5), vec3(1, 0, 0),
vec3( 0.5, 0.5, -0.5), vec3(1, 1, 0),
vec3(-0.5, -0.5, -0.5), vec3(0, 0, 0),
vec3(-0.5, 0.5, -0.5) vec3(0, 1, 0)
) )
); );

View File

@@ -73,14 +73,11 @@ namespace Voxel
float tDeltaY = direction.Y != 0 ? MathF.Abs(1 / direction.Y) : float.MaxValue; 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 tDeltaZ = direction.Z != 0 ? MathF.Abs(1 / direction.Z) : float.MaxValue;
float tMaxX = direction.X > 0 float tMaxX = direction.X > 0 ? (MathF.Floor(origin.X) + 1 - origin.X) * tDeltaX
? (MathF.Ceiling(origin.X) - origin.X) * tDeltaX
: (origin.X - MathF.Floor(origin.X)) * tDeltaX; : (origin.X - MathF.Floor(origin.X)) * tDeltaX;
float tMaxY = direction.Y > 0 float tMaxY = direction.Y > 0 ? (MathF.Floor(origin.Y) + 1 - origin.Y) * tDeltaY
? (MathF.Ceiling(origin.Y) + 1 - origin.Y) * tDeltaY
: (origin.Y - MathF.Floor(origin.Y)) * tDeltaY; : (origin.Y - MathF.Floor(origin.Y)) * tDeltaY;
float tMaxZ = direction.Z > 0 float tMaxZ = direction.Z > 0 ? (MathF.Floor(origin.Z) + 1 - origin.Z) * tDeltaZ
? (MathF.Ceiling(origin.Z) + 1 - origin.Z) * tDeltaZ
: (origin.Z - MathF.Floor(origin.Z)) * tDeltaZ; : (origin.Z - MathF.Floor(origin.Z)) * tDeltaZ;
float distance = 0f; float distance = 0f;
@@ -89,36 +86,34 @@ namespace Voxel
while (distance <= maxDistance) while (distance <= maxDistance)
{ {
Blocks block = GetBlock(x, y, z); 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) if (tMaxX < tMaxY && tMaxX < tMaxZ)
{ {
x += stepX; x += stepX;
normal = new Vector3i(-stepX, 0, 0);
distance = tMaxX; distance = tMaxX;
tMaxX += tDeltaX; tMaxX += tDeltaX;
normal = new Vector3i(stepX > 0 ? -1 : 1, 0, 0);
} }
else if (tMaxY < tMaxZ) else if (tMaxY < tMaxZ)
{ {
y += stepY; y += stepY;
normal = new Vector3i(0, -stepY, 0);
distance = tMaxY; distance = tMaxY;
tMaxY += tDeltaY; tMaxY += tDeltaY;
normal = new Vector3i(0, stepY > 0 ? -1 : 1, 0);
} }
else else
{ {
z += stepZ; z += stepZ;
normal = new Vector3i(0, 0, -stepZ);
distance = tMaxZ; distance = tMaxZ;
tMaxZ += tDeltaZ; 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() public void Update()