Example #1
0
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            var dxScene = MainDXViewportView.DXScene;

            if (dxScene == null)
            {
                return;
            }

            var mousePosition = e.GetPosition(ViewportBorder);

            int xPos = (int)mousePosition.X;
            int yPos = (int)mousePosition.Y;

            if (xPos == _lastMousePosition.X && yPos == _lastMousePosition.Y)
            {
                return;
            }


            _lastMousePosition = new SharpDX.Point(xPos, yPos);

            var mouseRay = dxScene.GetRayFromCamera(xPos, yPos);


            // Using OctTree significantly improve hit testing performance
            // Check this with uncommenting the following line (and commenting the use of OctTree):

            //var hitResult = dxScene.GetClosestHitObject(mouseRay);
            var hitResult = _octTree.HitTest(ref mouseRay, new DXHitTestContext(dxScene));


            int selectedSphereIndex;

            if (hitResult == null)
            {
                selectedSphereIndex = -1;
            }
            else
            {
                selectedSphereIndex = (hitResult.TriangleIndex * 3) / _oneMeshTriangleIndicesCount;
            }

            if (selectedSphereIndex == _lastSelectedSphereIndex)
            {
                return;
            }

            SelectSphere(selectedSphereIndex);
        }