private void processUserInput() { var ray = TW.Data.Get <CameraInfo>().GetCenterScreenRay(); if (TW.Graphics.Mouse.RelativeScrollWheel > 0) { placementGridSize *= 2; } if (TW.Graphics.Mouse.RelativeScrollWheel < 0) { placementGridSize /= 2; } placementGridSize = (int)MathHelper.Clamp(placementGridSize, 1, 20); var raycaster = new Raycaster <Chunk>(); chunks.ForEach((c, p) => { TW.Graphics.LineManager3D.AddBox(c.Box, Color.Black); c.Raycast(raycaster, ray); }); if (raycaster.GetClosest().IsHit) { Vector3 hitpoint = ray.GetPoint(raycaster.GetClosest().Distance); Vector3 normal = -raycaster.GetClosest().HitNormal; //TODO: invert hack TW.Graphics.LineManager3D.AddCenteredBox(hitpoint, 0.02f, Color.Yellow); TW.Graphics.LineManager3D.AddLine(hitpoint, hitpoint + normal * 0.06f, Color.CadetBlue); var targetCube = CalculatePlacementCube(hitpoint - normal * voxelSize * 0.1f, placementGridSize); var halfVoxel = new Vector3(voxelSize / 2); var placementWorldSize = placementGridSize * voxelSize; var targetBoundingBox = new BoundingBox((Vector3)targetCube.ToVector3() * placementWorldSize + halfVoxel, (targetCube + new Vector3(1, 1, 1)) * placementWorldSize + halfVoxel); TW.Graphics.LineManager3D.AddBox(targetBoundingBox, Color.GreenYellow); if (TW.Graphics.Mouse.LeftMouseJustPressed) { var addCube = CalculatePlacementCube(hitpoint + normal * 0.06f, placementGridSize); Point3 placeOffset = (addCube.ToVector3() * placementWorldSize / voxelSize).ToPoint3Rounded(); var placer = new BasicShapeBuilder().CreateCube(placementGridSize); PlaceInWorld(placer, placeOffset); } if (TW.Graphics.Mouse.RightMouseJustPressed) { Point3 placeOffset = (targetCube.ToVector3() * placementWorldSize / voxelSize).ToPoint3Rounded(); var placer = new BasicShapeBuilder().CreateCube(placementGridSize); RemoveFromWorld(placer, placeOffset); } } }
public void Raycast(Raycaster <Chunk> raycaster, Ray ray) { float?dist = ray.Intersects(Box); if (!dist.HasValue) { return; } if (surface == null) { return; } foreach (var mesh in surface.Meshes) { raycaster.AddIntersection(ray, mesh.Positions.Select(v => (Vector3)v).ToArray(), surface.WorldMatrix, this); } }