Raycasting world now returns intersection location and face

This commit is contained in:
Simon Lübeß
2021-03-02 22:25:38 +01:00
parent 488d2a55c4
commit f87c0e2ecd
2 changed files with 33 additions and 5 deletions
+10 -4
View File
@@ -380,7 +380,7 @@ namespace Sandbox.VoxelFun
Renderer.Submit(_quadGeometryBinding, _effect, transform);
}
_effect.Variables["BaseColor"].SetData(_squareColor1);
//_effect.Variables["BaseColor"].SetData(_squareColor1);
_texture.Bind();
Renderer.Submit(_quadGeometryBinding, _textureEffect, .Scaling(1.5f));
@@ -392,8 +392,6 @@ namespace Sandbox.VoxelFun
//Ray ray = .(.(0.5f, 128.5f, 0.5f), .(0, -0.1f, -0.1f));
_effect.Variables["BaseColor"].SetData(.Red);
_texture.Bind();
Matrix mat = .Translation(ray.Start);
@@ -415,15 +413,21 @@ namespace Sandbox.VoxelFun
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);
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));
_world.ChunkManager.Draw();
Renderer.EndScene();
}
String intersectedFaceName = new String() ~ delete _;
ColorRGBA _squareColor0 = ColorRGBA.CornflowerBlue;
ColorRGBA _squareColor1;
@@ -450,6 +454,8 @@ namespace Sandbox.VoxelFun
ImGui.Begin("Test");
ImGui.LabelText("Intersected Face", intersectedFaceName);
ImGui.DragFloat("Slow Speed", &movementSpeed, 1.0f, 0.01f, 100.0f);
ImGui.DragFloat("Fast Speed", &movementSpeedFast, 1.0f, 1f, 10000.0f);
+23 -1
View File
@@ -181,7 +181,7 @@ namespace Sandbox.VoxelFun
return Vector3.Floor(chunkPosition);
}
public Point3 RaycastBlock(Ray ray, float maxDistance, GeometryBinding geo, Effect effect)
public Point3 RaycastBlock(Ray ray, float maxDistance, GeometryBinding geo, Effect effect, out Vector3 intersectionPosition, out BlockFace intersectionFace)
{
Vector3 start = ray.Start;
Vector3 dir = ray.Direction.Normalized();
@@ -194,35 +194,45 @@ namespace Sandbox.VoxelFun
Point3 step;
BlockFace xFace;
BlockFace yFace;
BlockFace zFace;
if(dir.X < 0)
{
xFace = .Right;
step.X = -1;
rayLengths.X = (start.X - (float)(walker.X)) * unitStepSize.X;
}
else
{
xFace = .Left;
step.X = 1;
rayLengths.X = ((float)(walker.X + 1) - start.X) * unitStepSize.X;
}
if(dir.Y < 0)
{
yFace = .Top;
step.Y = -1;
rayLengths.Y = (start.Y - (float)(walker.Y)) * unitStepSize.Y;
}
else
{
yFace = .Bottom;
step.Y = 1;
rayLengths.Y = ((float)(walker.Y + 1) - start.Y) * unitStepSize.Y;
}
if(dir.Z < 0)
{
zFace = .Back;
step.Z = -1;
rayLengths.Z = (start.Z - (float)(walker.Z)) * unitStepSize.Z;
}
else
{
zFace = .Front;
step.Z = 1;
rayLengths.Z = ((float)(walker.Z + 1) - start.Z) * unitStepSize.Z;
}
@@ -238,12 +248,16 @@ namespace Sandbox.VoxelFun
walker.X += step.X;
distance = rayLengths.X;
rayLengths.X += unitStepSize.X;
intersectionFace = xFace;
}
else
{
walker.Z += step.Z;
distance = rayLengths.Z;
rayLengths.Z += unitStepSize.Z;
intersectionFace = zFace;
}
}
else
@@ -253,12 +267,16 @@ namespace Sandbox.VoxelFun
walker.Y += step.Y;
distance = rayLengths.Y;
rayLengths.Y += unitStepSize.Y;
intersectionFace = yFace;
}
else
{
walker.Z += step.Z;
distance = rayLengths.Z;
rayLengths.Z += unitStepSize.Z;
intersectionFace = zFace;
}
}
@@ -272,11 +290,15 @@ namespace Sandbox.VoxelFun
if(blockData != 0)
{
intersectionPosition = start + distance * dir;
return walker;
}
}
intersectionFace = .None;
intersectionPosition = .(float.NaN);
return .(int.MaxValue, int.MaxValue, int.MaxValue);
}
}