TerrainDisplay.Collision.SelectedTriangleManager.UpdateSelectedTriangle C# (CSharp) Method

UpdateSelectedTriangle() public method

public UpdateSelectedTriangle ( Ray selectRay, bool add ) : void
selectRay Ray A ray in Xna Coords that was cast according to the viewport
add bool Whether to add the selected triangle to the selection
return void
        public void UpdateSelectedTriangle(Ray selectRay, bool add)
        {
            if (!add)
            {
                Vertices.Clear();
                Indices.Clear();
            }

            var pos3D = selectRay.Position;
            var dir3D = selectRay.Direction;
            PositionUtil.TransformXnaCoordsToWoWCoords(ref pos3D);
            PositionUtil.TransformXnaCoordsToWoWCoords(ref dir3D);
            var ray3D = new Ray(pos3D, dir3D);

            var pos2D = new Vector2(pos3D.X, pos3D.Y);
            var dir2D = new Vector2(dir3D.X, dir3D.Y).NormalizedCopy();
            var ray2D = new Ray2D(pos2D, dir2D);

            var closestTime = float.MaxValue;
            var closestVec0 = Vector3.Zero;
            var closestVec1 = Vector3.Zero;
            var closestVec2 = Vector3.Zero;

            foreach (var tile in _adtManager.MapTiles)
            {
                var results = new List<Index3>();
                if (!tile.GetPotentialColliders(ray2D, results)) continue;

                foreach (var tri in results)
                {
                    var vec0 = tile.TerrainVertices[tri.Index0];
                    var vec1 = tile.TerrainVertices[tri.Index1];
                    var vec2 = tile.TerrainVertices[tri.Index2];

                    float time;
                    if (!Intersection.RayTriangleIntersect(ray3D, vec0, vec1, vec2, out time)) continue;
                    if (time > closestTime) continue;

                    closestTime = time;
                    closestVec0 = vec0;
                    closestVec1 = vec1;
                    closestVec2 = vec2;
                }
            }
            if (closestTime == float.MaxValue) return;
            AddSelectedTriangle(closestVec0, closestVec1, closestVec2);
        }