private void GetSelection3D(MouseEventArgs e)
 {
     device.Transform.View = Matrix.LookAtLH(CamEye, CamEye + CamDir, new Vector3(0.0f, 0.0f, 1.0f));
     device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 1000000.0f);
     Vector3 near = Vector3.Unproject(new Vector3(e.X, e.Y, 0), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity);
     Vector3 far = Vector3.Unproject(new Vector3(e.X, e.Y, 1), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity);
     Vector3 dir = far - near;
     dir.Normalize();
     List<RayCastSelection> selection = new List<RayCastSelection>();
     for (int i = 0; i < Level.UStat.Count; i++)
         if (Level.UStat[i].DirectX.visible)
         {
             ULevel.DirectXObject d = Level.UStat[i].DirectX;
             Vector2 result = RaySphereIntersect(CamEye - d.tfpos, dir, d.tfr);
             if (result != new Vector2(-1, -1))
             {
                 RayCastSelection r = new RayCastSelection();
                 r.dist = result.Y;
                 r.entry = i;
                 if (!float.IsNaN(r.dist))
                     selection.Add(r);
             }
         }
     bool run = true;
     while (run)
     {
         run = false;
         for (int i = 0; i < selection.Count - 1; i++)
             if (selection[i].dist > selection[i + 1].dist)
             {
                 run = true;
                 RayCastSelection r = selection[i];
                 selection[i] = selection[i + 1];
                 selection[i + 1] = r;
             }
     }
     int found = -1;
     for (int i = 0; i < selection.Count - 1; i++)
         if (selection[i].entry == SelectStat)
             found = i;
     if (found != -1)
     {
         if (found < Level.UStat.Count - 1) found++;
         else found = 0;
         SelectStat = found;
         CheckSelect();
         TreeNode t = treeView1.Nodes[0];
         treeView1.SelectedNode = t.Nodes[selection[found].entry];
     }
     else
     {
         SelectStat = selection[0].entry;
         CheckSelect();
         TreeNode t = treeView1.Nodes[0];
         treeView1.SelectedNode = t.Nodes[selection[0].entry];
     }
 }
        private void GetSelection3D(MouseEventArgs e)
        {
            device.Transform.View       = Matrix.LookAtLH(CamEye, CamEye + CamDir, new Vector3(0.0f, 0.0f, 1.0f));
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 1000000.0f);
            Vector3 near = Vector3.Unproject(new Vector3(e.X, e.Y, 0), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity);
            Vector3 far  = Vector3.Unproject(new Vector3(e.X, e.Y, 1), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity);
            Vector3 dir  = far - near;

            dir.Normalize();
            List <RayCastSelection> selection = new List <RayCastSelection>();

            for (int i = 0; i < Level.UStat.Count; i++)
            {
                if (Level.UStat[i].DirectX.visible)
                {
                    ULevel.DirectXObject d = Level.UStat[i].DirectX;
                    Vector2 result         = RaySphereIntersect(CamEye - d.tfpos, dir, d.tfr);
                    if (result != new Vector2(-1, -1))
                    {
                        RayCastSelection r = new RayCastSelection();
                        r.dist  = result.Y;
                        r.entry = i;
                        if (!float.IsNaN(r.dist))
                        {
                            selection.Add(r);
                        }
                    }
                }
            }
            bool run = true;

            while (run)
            {
                run = false;
                for (int i = 0; i < selection.Count - 1; i++)
                {
                    if (selection[i].dist > selection[i + 1].dist)
                    {
                        run = true;
                        RayCastSelection r = selection[i];
                        selection[i]     = selection[i + 1];
                        selection[i + 1] = r;
                    }
                }
            }
            int found = -1;

            for (int i = 0; i < selection.Count - 1; i++)
            {
                if (selection[i].entry == SelectStat)
                {
                    found = i;
                }
            }
            if (found != -1)
            {
                if (found < Level.UStat.Count - 1)
                {
                    found++;
                }
                else
                {
                    found = 0;
                }
                SelectStat = found;
                CheckSelect();
                TreeNode t = treeView1.Nodes[0];
                treeView1.SelectedNode = t.Nodes[selection[found].entry];
            }
            else
            {
                SelectStat = selection[0].entry;
                CheckSelect();
                TreeNode t = treeView1.Nodes[0];
                treeView1.SelectedNode = t.Nodes[selection[0].entry];
            }
        }