Esempio n. 1
0
        public override void ShowRotationAdorner(Point rotationCenterPosition)
        {
            if (Viewport3D != null)
            {
                // In this sample we show WireBoxVisual3D around selected box (when there is a box selected)
                if (SelectedBoxVisual3D != null)
                {
                    _wireBoxVisual3D = new WireBoxVisual3D()
                    {
                        LineColor      = Colors.Red,
                        LineThickness  = 3,
                        CenterPosition = SelectedBoxVisual3D.CenterPosition,
                        Size           = SelectedBoxVisual3D.Size
                    };

                    Viewport3D.Children.Add(_wireBoxVisual3D);

                    return;
                }

                // If no box is selected, we show WireCrossVisual3D at the RotationCenterPosition (if set)

                // When MouseCameraController.RotateAroundMousePosition is set to true,
                // MouseCameraController calculates the RotationCenterPosition with hit testing current mouse position on the 3D scene.
                var targetPositionCamera = TargetCamera as TargetPositionCamera;
                if (targetPositionCamera != null && targetPositionCamera.RotationCenterPosition != null)
                {
                    _wireCrossVisual3D = new WireCrossVisual3D()
                    {
                        LineColor     = Colors.Red,
                        LineThickness = 3,
                        LinesLength   = 30,
                        Position      = targetPositionCamera.RotationCenterPosition.Value
                    };

                    Viewport3D.Children.Add(_wireCrossVisual3D);

                    return;
                }
            }

            // If we came here then Viewport3D and SelectedBoxVisual3D are not set or there is no 3D object behind the mouse position (RotationCenterPosition is null)
            // In this case we show standard rotation marker
            base.ShowRotationAdorner(rotationCenterPosition);
        }
Esempio n. 2
0
        public override void HideRotationAdorner()
        {
            if (Viewport3D != null)
            {
                if (_wireBoxVisual3D != null)
                {
                    Viewport3D.Children.Remove(_wireBoxVisual3D);
                    _wireBoxVisual3D = null;

                    return;
                }

                if (_wireCrossVisual3D != null)
                {
                    Viewport3D.Children.Remove(_wireCrossVisual3D);
                    _wireCrossVisual3D = null;

                    return;
                }
            }

            base.HideRotationAdorner();
        }
Esempio n. 3
0
        private void OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var mousePosition = Mouse.GetPosition(MainDXViewportView);
                var mouseRay      = MainDXViewportView.DXScene.GetRayFromCamera((int)mousePosition.X, (int)mousePosition.Y);

                if (_optimizedPointMesh != null)
                {
                    //// Uncomment the following code to test the GetPositionIndexesAroundRay method
                    //var positionIndexes = _optimizedPointMesh.GetPositionIndexesAroundRay(mouseRay,
                    //                                                                      maxDistance: 0.1f,
                    //                                                                      orderByDistanceToRayPosition: true,
                    //                                                                      maxResultsCount: 0);

                    //for (var i = 0; i < positionIndexes.Count; i++)
                    //{
                    //    var wireCrossVisual3D = new WireCrossVisual3D()
                    //    {
                    //        LineColor = Colors.Yellow,
                    //        LineThickness = 1,
                    //        LinesLength = 10,
                    //        Position = _optimizedPointMesh.PositionsArray[positionIndexes[i]].ToWpfPoint3D()
                    //    };

                    //    MainViewport.Children.Add(wireCrossVisual3D);
                    //}

                    //_pixelMaterial.ReadZBuffer = true;
                    //_customRenderableNode.NotifySceneNodeChange(SceneNode.SceneNodeDirtyFlags.MaterialChanged);

                    //return;

                    float positionDistance;
                    var   closestPositionIndex = _optimizedPointMesh.GetClosestPositionIndex(mouseRay, out positionDistance);

                    if (closestPositionIndex != -1)
                    {
                        var position = _optimizedPointMesh.PositionsArray[closestPositionIndex];

                        if (_wireCrossVisual3D == null)
                        {
                            _wireCrossVisual3D = new WireCrossVisual3D()
                            {
                                LineColor     = Colors.Blue,
                                LineThickness = 2,
                                LinesLength   = 2
                            };

                            MainViewport.Children.Add(_wireCrossVisual3D);
                        }
                        else
                        {
                            _wireCrossVisual3D.IsVisible = true;
                        }

                        _wireCrossVisual3D.Position = position.ToWpfPoint3D();

                        // In this sample the Point cloud is rendered without reading and writing to depth buffer (see ShowPositionsArray method).
                        // This means that all the points will be rendered regardless of other 3D objects in the scene.
                        // To correctly show other 3D object (in our case WireCrossVisual3D), we need to enable reading depth buffer.
                        // This will prevent rendering points where some other object is closer to the camera.
                        _pixelMaterial.ReadZBuffer = true;

                        // SceneNode objects do not get automatic notifications when some properties are changed.
                        // We need to do that manually:
                        _customRenderableNode.NotifySceneNodeChange(SceneNode.SceneNodeDirtyFlags.MaterialChanged);
                    }
                    else
                    {
                        MessageBox.Show("No closest position found");
                        _wireCrossVisual3D.IsVisible = false;

                        _pixelMaterial.ReadZBuffer = false;
                        _customRenderableNode.NotifySceneNodeChange(SceneNode.SceneNodeDirtyFlags.MaterialChanged);
                    }
                }
            }
        }
        public ManuallyCreatedSceneNodes()
        {
            InitializeComponent();


            // Wait until the DirectX device is created and then create the sample objects
            MainDXViewportView.DXSceneDeviceCreated += delegate(object sender, EventArgs args)
            {
                CreateScene();
                ResetCamera();
            };

            MainDXViewportView.SceneRendered += MainDXViewportViewOnSceneRendered;


            // Show 3D WireCrossVisual3D at the location of camera rotation
            MouseCameraController1.CameraRotateStarted += delegate(object sender, EventArgs args)
            {
                if (_wireCrossVisual3D != null)
                {
                    MainViewport.Children.Remove(_wireCrossVisual3D);
                    _wireCrossVisual3D = null;
                }

                Point3D rotationCenter;

                if (Camera1.RotationCenterPosition.HasValue)
                {
                    rotationCenter = Camera1.RotationCenterPosition.Value;
                }
                else
                {
                    rotationCenter = Camera1.TargetPosition;
                }


                _wireCrossVisual3D = new WireCrossVisual3D()
                {
                    LineColor     = Colors.Red,
                    LineThickness = 3,
                    LinesLength   = 100,
                    Position      = rotationCenter
                };

                MainViewport.Children.Add(_wireCrossVisual3D);
            };

            MouseCameraController1.CameraRotateEnded += delegate(object sender, EventArgs args)
            {
                if (_wireCrossVisual3D != null)
                {
                    MainViewport.Children.Remove(_wireCrossVisual3D);
                    _wireCrossVisual3D = null;
                }
            };


            this.Unloaded += delegate(object sender, RoutedEventArgs e)
            {
                if (_disposables != null)
                {
                    _disposables.Dispose();
                    _disposables = null;
                }

                MainDXViewportView.Dispose();
            };
        }
        void eventSource3D_MouseEnter(object sender, Ab3d.DirectX.Common.EventManager3D.Mouse3DEventArgs e)
        {
            if (e.RayHitResult != null)
            {
                Log("MouseEnter: " + e.RayHitResult.HitSceneNode);

                // We need to save the hit SceneNode.
                // This way we will be able to change the material back in the eventSource3D_MouseLeave event handler.
                _selectedVisual3D = e.RayHitResult.HitSceneNode.GetVisual3D();

                var wpfGeometryModel3DNode = e.RayHitResult.HitSceneNode as WpfGeometryModel3DNode;
                if (wpfGeometryModel3DNode != null)
                {
                    _selectedGeometryModel3D = wpfGeometryModel3DNode.GeometryModel3D;
                }
                else
                {
                    _selectedGeometryModel3D = null;
                }


                var dxRayInstancedHitTestResult = e.RayHitResult as DXRayInstancedHitTestResult;
                if (dxRayInstancedHitTestResult != null)
                {
                    _selectedInstanceIndex = dxRayInstancedHitTestResult.HitInstanceIndex;
                }


                if (_wireCrossVisual3D == null)
                {
                    _wireCrossVisual3D = new WireCrossVisual3D()
                    {
                        LineColor     = Colors.DeepPink,
                        LineThickness = 2,
                        LinesLength   = 10
                    };
                }

                _wireCrossVisual3D.Position = e.RayHitResult.HitPosition.ToWpfPoint3D();

                if (!MainViewport3D.Children.Contains(_wireCrossVisual3D))
                {
                    MainViewport3D.Children.Add(_wireCrossVisual3D);
                }
            }
            else
            {
                _selectedVisual3D        = null;
                _selectedGeometryModel3D = null;
                _selectedInstanceIndex   = -1;

                Log("MouseEnter: <null>");
                return;
            }

            if (_selectedInstanceIndex != -1)
            {
                ChangeInstanceColor(_selectedInstanceIndex, Colors.Red);
            }
            else
            {
                ChangeColor(e, Colors.Red);
            }
        }