private void CreateRandomScene() { SceneObjectsContainer.Children.Clear(); for (int i = 0; i < 10; i++) { var boxModel = new Ab3d.Visuals.BoxVisual3D() { CenterPosition = new Point3D(_rnd.NextDouble() * 400 - 200, _rnd.NextDouble() * 40 - 20, _rnd.NextDouble() * 400 - 200), Size = new Size3D(50, 20, 50), Material = _normalMaterial }; SceneObjectsContainer.Children.Add(boxModel); // Use EventManager from Ab3d.PowerToys to add support for click event on the box model var visualEventSource3D = new Ab3d.Utilities.VisualEventSource3D(boxModel); visualEventSource3D.MouseClick += delegate(object sender, MouseButton3DEventArgs e) { var selectedBoxModel = e.HitObject as Ab3d.Visuals.BoxVisual3D; SelectObject(selectedBoxModel); }; _eventManager.RegisterEventSource3D(visualEventSource3D); // Automatically select first box if (_selectedBoxModel == null) { SelectObject(boxModel); } } }
private void CreateRandomScene() { SceneObjectsContainer.Children.Clear(); for (int i = 0; i < 10; i++) { // Create simple box that user will be able to rotate // In order to support rotation, we need to create the box at (0,0,0) // and then after performing rotation, translate the object to its final location. // If we would create the object at its final rotation (basically applying translation before rotation), // then the box would not be rotated around its center but around the coordinate axes center. var boxModel = new Ab3d.UIElements.BoxUIElement3D() { CenterPosition = new Point3D(0, 0, 0), Size = new Size3D(50, 20, 50), Material = _normalMaterial }; // Create a StandardTransform3D // StandardTransform3D is a class that generates a MatrixTransform3D based on the translate, rotate and scale transform. // Note that because it is not possible to derive from WPF's Transform3D, the StandardTransform3D is a standalone class // that provides its own MatrixTransform3D object that can be assigned to the 3D model. // Its static SetStandardTransform3D and GetStandardTransform3D use a custom StandardTransform3DProperty // to "attach" the StandardTransform3D object to Model3D or Visual3D. But this does not automatically set the // object's transformation. var standardTransform3D = new StandardTransform3D() { TranslateX = _rnd.NextDouble() * 400 - 200, TranslateY = _rnd.NextDouble() * 40 - 20, TranslateZ = _rnd.NextDouble() * 400 - 200, }; // SetStandardTransform3D method will set the StandardTransform3DProperty to the standardTransform3D // The updateTransform3D argument will also set the boxModel.Transform to standardTransform3D.Transform StandardTransform3D.SetStandardTransform3D(boxModel, standardTransform3D, updateTransform3D: true); SceneObjectsContainer.Children.Add(boxModel); // Use EventManager from Ab3d.PowerToys to add support for click event on the box model var visualEventSource3D = new Ab3d.Utilities.VisualEventSource3D(boxModel); visualEventSource3D.MouseClick += delegate(object sender, MouseButton3DEventArgs e) { var selectedBoxModel = e.HitObject as Ab3d.UIElements.BoxUIElement3D; SelectObject(selectedBoxModel); }; _eventManager.RegisterEventSource3D(visualEventSource3D); // Automatically select first box if (_selectedBoxModel == null) { boxModel.Refresh(); // Force creating the model SelectObject(boxModel); } } }
void DXEventManager3DWrapperSample_Loaded(object sender, RoutedEventArgs e) { //_eventManager = new Ab3d.Utilities.EventManager3D(Viewport3D1); //_eventManager.CustomEventsSourceElement = ViewportBorder; _eventManager = new DXEventManager3DWrapper(MainDXViewportView); // Exclude TransparentPlaneVisual3D from hit testing _eventManager.RegisterExcludedVisual3D(TransparentPlaneVisual3D); var multiEventSource3D = new Ab3d.Utilities.MultiVisualEventSource3D(); multiEventSource3D.TargetVisuals3D = new Visual3D[] { LowerBoxVisual3D, PassageBoxVisual3D, UpperBoxVisual3D }; multiEventSource3D.IsDragSurface = true; _eventManager.RegisterEventSource3D(multiEventSource3D); var eventSource3D = new Ab3d.Utilities.VisualEventSource3D(); eventSource3D.TargetVisual3D = MovableBoxVisual3D; eventSource3D.Name = "Movable"; eventSource3D.MouseEnter += new Ab3d.Common.EventManager3D.Mouse3DEventHandler(eventSource3D_MouseEnter); eventSource3D.MouseLeave += new Ab3d.Common.EventManager3D.Mouse3DEventHandler(eventSource3D_MouseLeave); eventSource3D.MouseClick += new Ab3d.Common.EventManager3D.MouseButton3DEventHandler(movableEventSource3D_MouseClick); eventSource3D.MouseDrag += new Ab3d.Common.EventManager3D.MouseDrag3DEventHandler(movableEventSource3D_MouseDrag); _eventManager.RegisterEventSource3D(eventSource3D); }
void EventManagerDragSample_Loaded(object sender, RoutedEventArgs e) { _eventManager = new Ab3d.Utilities.EventManager3D(Viewport3D1); // When using EventManager3D from Ab3d.PowerToys inside DXEngine, // it is recommended to set the CustomEventsSourceElement to the DXViewportView or its parent element (for example ViewportBorder). // If this is not done, then EventManager3D tries to find the DXViewportView and when found uses it as an element that used to subscribe to mouse events. _eventManager.CustomEventsSourceElement = ViewportBorder; // Exclude TransparentPlaneVisual3D from hit testing _eventManager.RegisterExcludedVisual3D(TransparentPlaneVisual3D); var multiEventSource3D = new Ab3d.Utilities.MultiVisualEventSource3D(); multiEventSource3D.TargetVisuals3D = new Visual3D[] { LowerBoxVisual3D, PassageBoxVisual3D, UpperBoxVisual3D }; multiEventSource3D.IsDragSurface = true; _eventManager.RegisterEventSource3D(multiEventSource3D); var eventSource3D = new Ab3d.Utilities.VisualEventSource3D(); eventSource3D.TargetVisual3D = MovableBoxVisual3D; eventSource3D.Name = "Movable"; eventSource3D.MouseEnter += new Ab3d.Common.EventManager3D.Mouse3DEventHandler(eventSource3D_MouseEnter); eventSource3D.MouseLeave += new Ab3d.Common.EventManager3D.Mouse3DEventHandler(eventSource3D_MouseLeave); eventSource3D.MouseClick += new Ab3d.Common.EventManager3D.MouseButton3DEventHandler(movableEventSource3D_MouseClick); eventSource3D.MouseDrag += new Ab3d.Common.EventManager3D.MouseDrag3DEventHandler(movableEventSource3D_MouseDrag); _eventManager.RegisterEventSource3D(eventSource3D); }
private void CreateRandomScene() { SceneObjectsContainer.Children.Clear(); for (int i = 0; i < 10; i++) { // Create simple box that user will be able to rotate // In order to support rotation, we need to create the box at (0,0,0) // and then after performing rotation, translate the object to its final location. // If we would create the object at its final rotation (basically applying translation before rotation), // then the box would not be rotated around its center but around the coordinate axes center. var boxModel = new Ab3d.UIElements.BoxUIElement3D() { CenterPosition = new Point3D(0, 0, 0), Size = new Size3D(50, 20, 50), Material = _normalMaterial }; // Create Transform3DGroup that will hold the var transform3DGroup = new Transform3DGroup(); transform3DGroup.Children.Add(new TranslateTransform3D(_rnd.NextDouble() * 400 - 200, _rnd.NextDouble() * 40 - 20, _rnd.NextDouble() * 400 - 200)); boxModel.Transform = transform3DGroup; SceneObjectsContainer.Children.Add(boxModel); // Use EventManager from Ab3d.PowerToys to add support for click event on the box model var visualEventSource3D = new Ab3d.Utilities.VisualEventSource3D(boxModel); visualEventSource3D.MouseClick += delegate(object sender, MouseButton3DEventArgs e) { var selectedBoxModel = e.HitObject as Ab3d.UIElements.BoxUIElement3D; SelectObject(selectedBoxModel); }; _eventManager.RegisterEventSource3D(visualEventSource3D); // Automatically select first box if (_selectedBoxModel == null) { boxModel.Refresh(); // Force creating the model SelectObject(boxModel); } } }
private void CreateScene(bool useDXEngineHitTesting) { ObjectsPlaceholder.Children.Clear(); if (_dxEventManager3D != null) { _dxEventManager3D.ResetEventSources3D(); _dxEventManager3D = null; } if (_wpfEventManager != null) { _wpfEventManager.ResetEventSources3D(); _wpfEventManager = null; } if (_instancedMeshGeometryVisual3D != null) { _instancedMeshGeometryVisual3D.Dispose(); } _stopwatch = new Stopwatch(); _stopwatch.Start(); Mouse.OverrideCursor = Cursors.Wait; // Create InstancedGeometryVisual3D _instancedMeshGeometryVisual3D = new InstancedMeshGeometryVisual3D(_instanceMeshGeometry3D); _instancedMeshGeometryVisual3D.InstancesData = _instancedData; // Setup hit testing if (useDXEngineHitTesting) { // Use DXEventManager3D from Ab3d.DXEngine - it has optimized hit testing for instanced objects _dxEventManager3D = new Ab3d.DirectX.Utilities.DXEventManager3D(MainDXViewportView); var visualEventSource3D = new Ab3d.DirectX.Utilities.VisualEventSource3D(_instancedMeshGeometryVisual3D); visualEventSource3D.MouseEnter += delegate(object sender, DirectX.Common.EventManager3D.Mouse3DEventArgs e) { var dxRayInstancedHitTestResult = e.RayHitResult as DXRayInstancedHitTestResult; if (dxRayInstancedHitTestResult != null) { ProcessMouseEnter(dxRayInstancedHitTestResult.HitInstanceIndex); } }; visualEventSource3D.MouseMove += delegate(object sender, DirectX.Common.EventManager3D.Mouse3DEventArgs e) { var dxRayInstancedHitTestResult = e.RayHitResult as DXRayInstancedHitTestResult; if (dxRayInstancedHitTestResult != null) { ProcessMouseMove(dxRayInstancedHitTestResult.HitInstanceIndex); } }; visualEventSource3D.MouseLeave += delegate(object sender, DirectX.Common.EventManager3D.Mouse3DEventArgs e) { ProcessMouseLeave(); }; _dxEventManager3D.RegisterEventSource3D(visualEventSource3D); } else { // // IMPORTANT: // // To make WPF hit testing work (also used by EventManager3D), you need to set the IsWpfHitTestVisible to true. // This increases initialization time because WPF objects needs to be created for each instance, but this makes the WPF hit testing work. _instancedMeshGeometryVisual3D.IsWpfHitTestVisible = true; _wpfEventManager = new Ab3d.Utilities.EventManager3D(MainViewport); // Because Viewport3D is actually not shown, we need to specify different WPF's object for the source of mouse events - this could be MainDXViewportView or even better a parent Border _wpfEventManager.CustomEventsSourceElement = MainDXViewportView; var visualEventSource3D = new Ab3d.Utilities.VisualEventSource3D(_instancedMeshGeometryVisual3D); visualEventSource3D.MouseEnter += delegate(object sender, Mouse3DEventArgs e) { if (e.RayHitResult == null || e.RayHitResult.ModelHit == null) { return; // This should not happen, but it is safer to have this check anyway } // Get instance index of the hit object int hitInstanceIndex = GetHitInstanceIndex(e.RayHitResult); ProcessMouseEnter(hitInstanceIndex); }; visualEventSource3D.MouseMove += delegate(object sender, Mouse3DEventArgs e) { if (e.RayHitResult == null || e.RayHitResult.ModelHit == null) { return; // This should not happen, but it is safer to have this check anyway } // Get instance index of the hit object int hitInstanceIndex = GetHitInstanceIndex(e.RayHitResult); ProcessMouseMove(hitInstanceIndex); }; visualEventSource3D.MouseLeave += delegate(object sender, Mouse3DEventArgs e) { ProcessMouseLeave(); }; _wpfEventManager.RegisterEventSource3D(visualEventSource3D); } ObjectsPlaceholder.Children.Add(_instancedMeshGeometryVisual3D); Mouse.OverrideCursor = null; // If we would only change the InstancedData we would need to call Update method (but here this is not needed because we have set the data for the first time) //_instancedGeometryVisual3D.Update(); }