private void DisposeDXViewportView() { if (_mainDXViewportView == null) { return; } if (_camera1 != null) { _camera1.StopRotation(); RootGrid.Children.Remove(_camera1); _camera1 = null; } if (_mouseCameraController != null) { RootGrid.Children.Remove(_mouseCameraController); _mouseCameraController = null; } _mainDXViewportView.SceneRendered -= MainDxViewportViewOnSceneRendered; _mainDXViewportView.Dispose(); _mainDXViewportView = null; }
private void UpdateBackgroundRendererCamera(TargetPositionCamera targetPositionCamera) { if (_backgroundDXEngineRenderer != null) { _backgroundDXEngineRenderer.ChangeCamera(targetPositionCamera.Heading, targetPositionCamera.Attitude, targetPositionCamera.Distance, targetPositionCamera.TargetPosition); } }
private DXViewportView CreateSimpleDXSceneViewFromCode(DXViewportView masterDXViewportView, RenderingTypes renderingType, out TargetPositionCamera targetPositionCamera, out MouseCameraController mouseCameraController) { Viewport3D viewport3D = new Viewport3D(); DXViewportView createDXViewportView; if (masterDXViewportView != null) { // Create a child DXViewportView with using a constructor that takes a masterDXViewportView. // When useMasterRenderingSteps parameter is true (by default), then the RenderingSteps in the master DXViewportView.DXScene are used. // To customize rendering of a child DXViewportView, set useMasterRenderingSteps to false. This will create new RenderingSteps that can be customized (see below). bool useMasterRenderingSteps = renderingType == RenderingTypes.Standard; createDXViewportView = new DXViewportView(masterDXViewportView, viewport3D, useMasterRenderingSteps); } else { // Create master DXViewportView with using constructor that takes only Viewport3D. createDXViewportView = new DXViewportView(viewport3D); } createDXViewportView.DXSceneDeviceCreated += delegate(object sender, EventArgs e) { // Enable transparency sorting for each View (this way transparent objects are correctly rendered for each child view camera). createDXViewportView.DXScene.IsTransparencySortingEnabled = true; SetSpecialRenderingType(createDXViewportView, renderingType); }; // Because each view supports its own camera, we need to create a new TargetPositionCamera and MouseCameraController for each view. double cameraHeading = masterDXViewportView == null ? 30 : _rnd.Next(360); targetPositionCamera = new TargetPositionCamera() { TargetPosition = new Point3D(0, 0, 0), Heading = cameraHeading, Attitude = -20, ShowCameraLight = ShowCameraLightType.Never, // If we have a camera light then the light position will be defined (and changed) with changing the camera position in one view Distance = 1000, TargetViewport3D = viewport3D }; mouseCameraController = new MouseCameraController() { TargetCamera = targetPositionCamera, RotateCameraConditions = MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, MoveCameraConditions = MouseCameraController.MouseAndKeyboardConditions.ControlKey | MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed }; return(createDXViewportView); }
private void CreateMouseCameraController() { // To support mouse camera rotation and movement // we create a dummy Viewport3D, a TargetPositionCamera and a MouseCameraController. // We subscribe to the TargetPositionCamera changes and propagate all the changes to the camera in the background rendering thread. var dummyViewport3D = new Viewport3D(); _targetPositionCamera = new TargetPositionCamera() { TargetViewport3D = dummyViewport3D, ShowCameraLight = ShowCameraLightType.Never }; // Because the camera will not be added to the UI three, we need to manually Refresh it _targetPositionCamera.Refresh(); // On each camera change we update the camera in the background rendering thread _targetPositionCamera.CameraChanged += delegate(object sender, CameraChangedRoutedEventArgs args) { UpdateBackgroundRendererCamera(_targetPositionCamera); }; var mouseCameraController = new Ab3d.Controls.MouseCameraController() { TargetCamera = _targetPositionCamera, EventsSourceElement = DXViewportBorder, RotateCameraConditions = Ab3d.Controls.MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, MoveCameraConditions = Ab3d.Controls.MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed | Ab3d.Controls.MouseCameraController.MouseAndKeyboardConditions.ControlKey, }; mouseCameraController.CameraRotateStarted += delegate(object sender, EventArgs args) { // When the rotation is stared we need to synchronize the camera's data UpdateMainUICamera(); // If background camera is retating, we pause the rotation during the user's mouse rotation if (_isBackgroundThreadCameraRotating) { _backgroundDXEngineRenderer.StopCameraRotation(); } }; mouseCameraController.CameraRotateEnded += delegate(object sender, EventArgs args) { // resume the background camera rotation if (_isBackgroundThreadCameraRotating) { _backgroundDXEngineRenderer.StartCameraRotation(headingChangeInSecond: 40); } }; }
private void CreateCamera() { _targetPositionCamera = new TargetPositionCamera() { TargetViewport3D = wpfViewport3D, TargetPosition = new Point3D(0, 0.5, 0), Distance = 8, Heading = 30, Attitude = -10, ShowCameraLight = ShowCameraLightType.Never }; // Because this _targetPositionCamera is never added to the UI three, we need to manually call Refresh method _targetPositionCamera.Refresh(); }
public void SyncCamera(TargetPositionCamera sourceCamera) { _isSyncingCamera = true; // Do not trigger CameraChanged event MainCamera.BeginInit(); MainCamera.Heading = sourceCamera.Heading; MainCamera.Attitude = sourceCamera.Attitude; MainCamera.Distance = sourceCamera.Distance; MainCamera.Offset = sourceCamera.Offset; MainCamera.EndInit(); _isSyncingCamera = false; }
private void SetupDXEngine() { _dxViewportView = new DXViewportView(); // To use render to bitmap on a server, it is possible to use software rendering with the following line: //_dxViewportView.GraphicsProfiles = new GraphicsProfile[] { GraphicsProfile.HighQualitySoftwareRendering }; // Because the DXViewportView is not shown in the UI, we need to manually sets its size (without this the current version will not be initialized correctly - this will be improved in the future) _dxViewportView.Width = 128; _dxViewportView.Height = 128; // By default the BackgroundColor is set to transparent. // We set that to white so that the saved bitmap has a white background. _dxViewportView.BackgroundColor = Colors.White; _viewport3D = new Viewport3D(); _camera = new TargetPositionCamera() { //Heading = HeadingSlider.Value, Attitude = -15, Distance = 300, TargetPosition = new Point3D(0, 10, 0), ShowCameraLight = ShowCameraLightType.Always, TargetViewport3D = _viewport3D }; UpdateCamera(); _dxViewportView.Viewport3D = _viewport3D; // Initialize the scene with creating DirectX device and required resources try { _dxViewportView.InitializeScene(); } catch (Exception ex) { MessageBox.Show("Error initializing DXEngine:\r\n" + ex.Message); RenderButton.IsEnabled = false; } _isFirstRender = true; }
private void SetupDXEngine() { _dxViewportView = new DXViewportView(); // For this sample we force software rendering (for example to be used on server or other computer without graphics card) if (ForceSoftwareRendering) { _dxViewportView.GraphicsProfiles = new GraphicsProfile[] { GraphicsProfile.HighQualitySoftwareRendering } } ; // Because the DXViewportView is not shown in the UI, we need to manually sets its size (without this the current version will not be initialized correctly - this will be improved in the future) _dxViewportView.Width = 128; _dxViewportView.Height = 128; _viewport3D = new Viewport3D(); _camera = new TargetPositionCamera() { //Heading = HeadingSlider.Value, Attitude = -20, Distance = 200, ShowCameraLight = ShowCameraLightType.Always, TargetViewport3D = _viewport3D }; UpdateCamera(); _dxViewportView.Viewport3D = _viewport3D; // Initialize the scene with creating DirectX device and required resources try { _dxViewportView.InitializeScene(); } catch (Exception ex) { MessageBox.Show("Error initializing DXEngine:\r\n" + ex.Message); RenderButton.IsEnabled = false; } _isFirstRender = true; }
private void CreateDXViewportView(bool isDirectXOverlay) { _mainViewport3D = new Viewport3D(); _lightsGroup = new Model3DGroup(); _mainViewport3D.Children.Add(_lightsGroup.CreateModelVisual3D()); UpdateLightingMode(); _camera1 = new TargetPositionCamera() { TargetPosition = new Point3D(0, 0, 0), Heading = 30, Attitude = -10, Distance = 1500, ShowCameraLight = ShowCameraLightType.Never, TargetViewport3D = _mainViewport3D }; _mouseCameraController = new MouseCameraController() { RotateCameraConditions = MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, MoveCameraConditions = MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed | MouseCameraController.MouseAndKeyboardConditions.ControlKey, EventsSourceElement = ViewportBorder, TargetCamera = _camera1 }; RootGrid.Children.Add(_camera1); RootGrid.Children.Add(_mouseCameraController); _mainDXViewportView = new DXViewportView(_mainViewport3D) { BackgroundColor = Colors.White, PresentationType = isDirectXOverlay ? DXView.PresentationTypes.DirectXOverlay : DXView.PresentationTypes.DirectXImage }; var maxBackgroundThreadsCount = (int)ThreadsCountSlider.Value; _mainDXViewportView.DXSceneDeviceCreated += delegate(object sender, EventArgs args) { if (_mainDXViewportView.DXScene != null) { _mainDXViewportView.DXScene.MaxBackgroundThreadsCount = maxBackgroundThreadsCount; } }; _mainDXViewportView.SceneRendered += MainDxViewportViewOnSceneRendered; ViewportBorder.Child = _mainDXViewportView; _camera1.StartRotation(40, 0); // Notify MainWindow about a new MainDXViewportView - so we can open DiagnosticsWindow if (_parentWindow != null) { ((MainWindow)_parentWindow).UnsubscribeLastShownDXViewportView(); ((MainWindow)_parentWindow).SubscribeDXViewportView(_mainDXViewportView); ((MainWindow)_parentWindow).MaxBackgroundThreadsCount = maxBackgroundThreadsCount; // prevent setting MaxBackgroundThreadsCount from the setting dialog } }
private void SetUpWpf3D() { // The following controls are usually defined in XAML in Wpf project // But here we can also define them in code. // We need a root grid because we will host more than one control _rootGrid = new Grid(); _rootGrid.Background = System.Windows.Media.Brushes.White; // Viewport3D is a WPF control that can show 3D graphics _viewport3D = new Viewport3D(); _rootGrid.Children.Add(_viewport3D); // Specify TargetPositionCamera that will show our 3D scene //<cameras:TargetPositionCamera Name="Camera1" // Heading="30" Attitude="-20" Bank="0" // Distance="1300" TargetPosition="0 0 0" // ShowCameraLight="Always" // TargetViewport3D="{Binding ElementName=MainViewport}"/> _targetPositionCamera = new Ab3d.Cameras.TargetPositionCamera() { TargetPosition = new Point3D(0, 0, 0), Distance = 1300, Heading = 30, Attitude = -20, ShowCameraLight = ShowCameraLightType.Always, TargetViewport3D = _viewport3D }; _rootGrid.Children.Add(_targetPositionCamera); // Set rotate to right mouse button // and move to CRTL + right mouse button // Left mouse button is left for clicking on the 3D objects //<controls:MouseCameraController Name="MouseCameraController1" // RotateCameraConditions="RightMouseButtonPressed" // MoveCameraConditions="ControlKey, RightMouseButtonPressed" // EventsSourceElement="{Binding ElementName=RootViewportBorder}" // TargetCamera="{Binding ElementName=Camera1}"/> _mouseCameraController = new Ab3d.Controls.MouseCameraController() { RotateCameraConditions = MouseCameraController.MouseAndKeyboardConditions.RightMouseButtonPressed, MoveCameraConditions = MouseCameraController.MouseAndKeyboardConditions.RightMouseButtonPressed | MouseCameraController.MouseAndKeyboardConditions.ControlKey, EventsSourceElement = _rootGrid, TargetCamera = _targetPositionCamera }; _rootGrid.Children.Add(_mouseCameraController); // Show buttons that can be used to rotate and move the camera //<controls:CameraControlPanel VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="5" Width="225" Height="75" ShowMoveButtons="True" // TargetCamera="{Binding ElementName=Camera1}"/> var cameraControlPanel = new Ab3d.Controls.CameraControlPanel() { VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Thickness(5, 5, 5, 5), Width = 225, Height = 75, ShowMoveButtons = true, TargetCamera = _targetPositionCamera }; _rootGrid.Children.Add(cameraControlPanel); // Finally add the root WPF Grid to elementHost1 elementHost1.Child = _rootGrid; }
private void AddDXViewport3DGrid(string title, string subTitle, int multisamplingCount, int supersamplingCount, int rowIndex, int columnIndex) { var viewport3D = new Viewport3D(); var sceneModelVisual3D = CreateScene(); viewport3D.Children.Add(sceneModelVisual3D); var dxViewportView = new DXViewportView(viewport3D); // Set GraphicsProfile: var graphicsProfile = new GraphicsProfile(string.Format("{0}MSAA_{1}SSAA_HardwareRendering", multisamplingCount, supersamplingCount), GraphicsProfile.DriverTypes.DirectXHardware, ShaderQuality.High, preferedMultisampleCount: multisamplingCount, supersamplingCount: supersamplingCount, textureFiltering: TextureFilteringTypes.Anisotropic_x4); dxViewportView.GraphicsProfiles = new GraphicsProfile[] { graphicsProfile, GraphicsProfile.Wpf3D }; // Add WPF 3D as fallback var border = new Border() { Background = Brushes.Transparent, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1, 1, 1, 1), Margin = new Thickness(1, 1, 3, 3), UseLayoutRounding = true, SnapsToDevicePixels = true }; border.Child = dxViewportView; var targetPositionCamera = new TargetPositionCamera() { TargetPosition = new Point3D(15, 50, 5), Heading = -17, Attitude = -25, ShowCameraLight = ShowCameraLightType.Always, Distance = 80, TargetViewport3D = viewport3D }; var mouseCameraController = new MouseCameraController() { TargetCamera = targetPositionCamera, EventsSourceElement = border, RotateCameraConditions = MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, MoveCameraConditions = MouseCameraController.MouseAndKeyboardConditions.ControlKey | MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, }; var titlesPanel = new StackPanel() { Orientation = Orientation.Vertical, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(10, 10, 10, 0) }; if (title != null) { var textBlock = new TextBlock() { Text = title, FontSize = 18, FontWeight = FontWeights.Bold, TextWrapping = TextWrapping.Wrap, Foreground = new SolidColorBrush(Color.FromRgb(32, 32, 32)), }; titlesPanel.Children.Add(textBlock); } if (subTitle != null) { var textBlock = new TextBlock() { Text = subTitle, FontSize = 14, TextWrapping = TextWrapping.Wrap, Margin = new Thickness(0, 3, 0, 0) }; titlesPanel.Children.Add(textBlock); } var viewRootGrid = new Grid(); viewRootGrid.Children.Add(border); viewRootGrid.Children.Add(titlesPanel); viewRootGrid.Children.Add(targetPositionCamera); viewRootGrid.Children.Add(mouseCameraController); Grid.SetColumn(viewRootGrid, columnIndex); Grid.SetRow(viewRootGrid, rowIndex); RootGrid.Children.Add(viewRootGrid); }
private void CreateOffline3DScene() { var viewport3D = new Viewport3D(); // IMPORTANT !!! // Because this Viewport3D will not be actually shown, we need to manually set its size // because this is needed for projection matrix. viewport3D.Width = 800; viewport3D.Height = 600; var targetPositionCamera = new TargetPositionCamera() { Heading = 0, Attitude = 0, CameraType = BaseCamera.CameraTypes.OrthographicCamera, CameraWidth = viewport3D.Width, // Use same width as viewport3D so we get 1:1 scale TargetViewport3D = viewport3D }; var polyLineVisual3D = new PolyLineVisual3D() { Positions = new Point3DCollection(new Point3D[] { //new Point3D(-100, 0, -50), //new Point3D(100, 0, -50), //new Point3D(100, 0, 50), //new Point3D(50, 0, 0), //new Point3D(-100, 0, 50), new Point3D(-100, -50, 0), new Point3D(100, -50, 0), new Point3D(100, 50, 0), new Point3D(50, 0, 0), new Point3D(-100, 50, 0), }), LineColor = Colors.Green, LineThickness = 30, // NOTE: // If you require that each line segment uses exactly 4 positions, // then you need to disable turning mitered joints into beveled joints // (beveled joints have 3 additional positions that cut the sharp joint). // It is not possible to know in advance how many bevel joints will be // because this also depends on the angle of the camera. // To disable creating beveled joints set MiterLimit to some high number // (for example the following will create a beveled joint when the joint length is 100 times the line thickness). //MiterLimit = 100 }; viewport3D.Children.Add(polyLineVisual3D); targetPositionCamera.Refresh(); Ab3d.Utilities.LinesUpdater.Instance.Refresh(); // Force regeneration of all 3D lines var geometryModel3D = polyLineVisual3D.Content as GeometryModel3D; if (geometryModel3D != null) { var lineMesh = (MeshGeometry3D)geometryModel3D.Geometry; _shownLineModel3D = new GeometryModel3D(); _shownLineModel3D.Geometry = lineMesh; _shownLineModel3D.Material = new DiffuseMaterial(Brushes.LightGray); _shownLineModel3D.BackMaterial = new DiffuseMaterial(Brushes.Black); if (BillboardCheckbox.IsChecked ?? false) { ApplyBillboardMatrix(); } MainViewport.Children.Add(_shownLineModel3D.CreateContentVisual3D()); MeshInspector.MeshGeometry3D = lineMesh; } }
private DXViewportView AddDXViewport3DGrid(string title, string graphicsProfileName, int multisamplingCount, int supersamplingCount, int rowIndex, int columnIndex) { var viewport3D = new Viewport3D(); var sceneModelVisual3D = CreateScene(); viewport3D.Children.Add(sceneModelVisual3D); var dxViewportView = new DXViewportView(viewport3D); dxViewportView.BackgroundColor = Colors.White; dxViewportView.UseLayoutRounding = true; dxViewportView.SnapsToDevicePixels = true; // Set GraphicsProfile: var graphicsProfile = new GraphicsProfile(string.Format("{0}xMSAA_{1}xSSAA_HardwareRendering", multisamplingCount, supersamplingCount), GraphicsProfile.DriverTypes.DirectXHardware, ShaderQuality.High, preferedMultisampleCount: multisamplingCount, supersamplingCount: supersamplingCount, textureFiltering: TextureFilteringTypes.Anisotropic_x4); dxViewportView.GraphicsProfiles = new GraphicsProfile[] { graphicsProfile, GraphicsProfile.Wpf3D }; // Add WPF 3D as fallback var border = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1, 1, 1, 1), Margin = new Thickness(1, 1, 2, 2), UseLayoutRounding = true, SnapsToDevicePixels = true }; border.Child = dxViewportView; var targetPositionCamera = new TargetPositionCamera() { TargetPosition = new Point3D(0, 0, 0), Heading = 0, Attitude = 0, ShowCameraLight = ShowCameraLightType.Always, CameraType = BaseCamera.CameraTypes.OrthographicCamera, CameraWidth = 800, Distance = 800, TargetViewport3D = viewport3D }; dxViewportView.DXRenderSizeChanged += delegate(object sender, DXViewSizeChangedEventArgs args) { targetPositionCamera.CameraWidth = dxViewportView.DXRenderSize.Width; }; var mouseCameraController = new MouseCameraController() { TargetCamera = targetPositionCamera, EventsSourceElement = border, RotateCameraConditions = MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, MoveCameraConditions = MouseCameraController.MouseAndKeyboardConditions.ControlKey | MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, }; var titlesPanel = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(10, 5, 10, 0) }; if (title != null) { var textBlock = new TextBlock() { Text = title, FontSize = 18, FontWeight = FontWeights.Bold, TextWrapping = TextWrapping.Wrap, Foreground = new SolidColorBrush(Color.FromRgb(32, 32, 32)), }; titlesPanel.Children.Add(textBlock); } if (graphicsProfileName != null) { var textBlock = new TextBlock() { Text = "GraphicsProfile: " + graphicsProfileName, FontSize = 12, TextWrapping = TextWrapping.Wrap, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(15, 0, 0, 0) }; titlesPanel.Children.Add(textBlock); } var viewRootGrid = new Grid(); viewRootGrid.Children.Add(border); viewRootGrid.Children.Add(titlesPanel); viewRootGrid.Children.Add(targetPositionCamera); viewRootGrid.Children.Add(mouseCameraController); Grid.SetColumn(viewRootGrid, columnIndex); Grid.SetRow(viewRootGrid, rowIndex); RootGrid.Children.Add(viewRootGrid); return(dxViewportView); }
public Grid CreateScene() { var rootGrid = new Grid(); var textBlock = new TextBlock() { Text = _title, FontSize = 16, FontWeight = FontWeights.Bold, Margin = new Thickness(10, 5, 10, 10), VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left }; _reportTextBlock = new TextBlock() { Text = "", FontSize = 10, Margin = new Thickness(10, 30, 10, 10), VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left, TextWrapping = TextWrapping.Wrap }; var rootBorder = new Border() { Background = Brushes.Transparent, BorderBrush = Brushes.Black, BorderThickness = new Thickness(2, 2, 2, 2), Margin = new Thickness(1, 2, 1, 2) }; MainViewport3D = new Viewport3D(); var rootModel3DGroup = Create3DScene(new Point3D(0, -300, 0), XCount, YCount, ZCount, 30); MainCamera = new TargetPositionCamera() { TargetViewport3D = MainViewport3D, TargetPosition = new Point3D(0, 0, 0), Heading = 130, // start from back where sorting errors are most obvious Attitude = -20, Distance = 500, ShowCameraLight = ShowCameraLightType.Always }; MainCamera.CameraChanged += OnMainCameraChanged; var mouseCameraController = new MouseCameraController() { TargetCamera = MainCamera, EventsSourceElement = rootBorder, RotateCameraConditions = MouseCameraController.MouseAndKeyboardConditions.LeftMouseButtonPressed, MoveCameraConditions = MouseCameraController.MouseAndKeyboardConditions.Disabled // disable mouse move }; if (_useDXEngine) { _dxViewportView = new DXViewportView(); _dxViewportView.Viewport3D = MainViewport3D; rootBorder.Child = _dxViewportView; _dxViewportView.DXSceneDeviceCreated += delegate(object sender, EventArgs e) { // Always disable transparency sorting on start // If _useDXEngineSorting the IsTransparencySortingEnabled will be set to true after // the first rendering statistics will be collected (see CollectStatistics method for more info) _dxViewportView.DXScene.IsTransparencySortingEnabled = false; }; _dxViewportView.SceneRendered += delegate(object sender, EventArgs e) { CollectStatistics(); }; // To manually change the order of objects after they have been sorted use SortingCompleted event: //_dxViewportView.DXScene.TransparentRenderingQueue.SortingCompleted += delegate(object sender, RenderingQueueSortingCompletedEventArgs e) //{ // // Here it is possible to change the order of item with changing the indexes in the e.SortedIndexes array. // // IMPORTANT: // // To get objects count use e.RenderablePrimitives.Count and not e.SortedIndexes.Length as it may be too big! //}; Ab3d.DirectX.DXDiagnostics.IsCollectingStatistics = true; } else { rootBorder.Child = MainViewport3D; } if (_useTransparencySorter) { _transparencySorter = new TransparencySorter(rootModel3DGroup, MainCamera); } MainCamera.Refresh(); rootGrid.Children.Add(rootBorder); rootGrid.Children.Add(textBlock); rootGrid.Children.Add(_reportTextBlock); return(rootGrid); }
private void AddNewDXViewportView(Grid parentGrid, int rowIndex, int columnIndex, double sphereRadius, double depthBias, bool createDXEngine, string title, out Border createdBorder, out Viewport3D createdViewport3D, out DXViewportView createdDXViewportView, out TargetPositionCamera createdTargetPositionCamera) { createdBorder = new Border() { BorderBrush = Brushes.Black, BorderThickness = new Thickness(1, 1, 1, 1), SnapsToDevicePixels = true }; createdViewport3D = new Viewport3D(); createdTargetPositionCamera = new TargetPositionCamera() { Heading = 30, Attitude = -20, Distance = 200, TargetPosition = new Point3D(0, 0, 0), ShowCameraLight = ShowCameraLightType.Always, TargetViewport3D = createdViewport3D }; bool showSphere = ShowSphereRadioButton.IsChecked ?? false; CreateScene(createdViewport3D, sphereRadius, depthBias, createdTargetPositionCamera, showSphere); if (createDXEngine) { createdDXViewportView = new DXViewportView(createdViewport3D); createdDXViewportView.SnapsToDevicePixels = true; createdBorder.Child = createdDXViewportView; } else { createdBorder.Child = createdViewport3D; createdDXViewportView = null; } Grid.SetRow(createdBorder, rowIndex); Grid.SetColumn(createdBorder, columnIndex); parentGrid.Children.Add(createdBorder); parentGrid.Children.Add(createdTargetPositionCamera); if (!string.IsNullOrEmpty(title)) { var textBlock = new TextBlock() { Text = title, Foreground = Brushes.Black, FontSize = 12, Margin = new Thickness(10, 5, 5, 5), HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top }; Grid.SetRow(textBlock, rowIndex); Grid.SetColumn(textBlock, columnIndex); parentGrid.Children.Add(textBlock); } }
private void CreateScene(Viewport3D parentViewport3D, double sphereRadius, double depthBias, TargetPositionCamera targetPositionCamera, bool showSphere) { parentViewport3D.Children.Clear(); if (showSphere) { var sphereVisual3D = new Ab3d.Visuals.SphereVisual3D() { CenterPosition = new Point3D(0, 0, 0), Radius = sphereRadius, Segments = 10, Material = new DiffuseMaterial(Brushes.SkyBlue), UseCachedMeshGeometry3D = false // This will create a new MeshGeometry3D and will not use the shared MeshGeometry3D with radius = 1 }; parentViewport3D.Children.Add(sphereVisual3D); var sphereMesh = ((GeometryModel3D)sphereVisual3D.Content).Geometry as MeshGeometry3D; var sphereLinePositions = CollectWireframeLinePositions(sphereMesh); var multiLineVisual3D = new Ab3d.Visuals.MultiLineVisual3D() { Positions = sphereLinePositions, LineThickness = 0.5, LineColor = Colors.Black }; // To specify line depth bias to the Ab3d.PowerToys line Visual3D objects, // we use SetDXAttribute extension method and use LineDepthBias as DXAttributeType // NOTE: This can be used only before the Visual3D is created by DXEngine. // If you want to change the line bias after the object has been rendered, use the SetDepthBias method (defined below) multiLineVisual3D.SetDXAttribute(DXAttributeType.LineDepthBias, depthBias); // It would be also possible to set the depth with changing the DepthBias on the WpfWireframeVisual3DNode. // This is done with using the SetDepthBias method //SetDepthBias(dxViewportView, wireframeVisual3D, depthBias); parentViewport3D.Children.Add(multiLineVisual3D); _shownLineVisual3D = multiLineVisual3D; } else { if (_sampleModel == null) { var readerObj = new Ab3d.ReaderObj(); _sampleModel = readerObj.ReadModel3D(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resources\Models\Teapot.obj"), null, new DiffuseMaterial(Brushes.SkyBlue)); _sampleModel.Freeze(); } double readObjectRadius = Math.Sqrt(_sampleModel.Bounds.SizeX * _sampleModel.Bounds.SizeX + _sampleModel.Bounds.SizeZ + _sampleModel.Bounds.SizeZ) / 2; double scaleFactor = sphereRadius / readObjectRadius; var finalModel = new Model3DGroup(); finalModel.Children.Add(_sampleModel); finalModel.Transform = new ScaleTransform3D(scaleFactor, scaleFactor, scaleFactor); finalModel.Freeze(); var wireframeVisual3D = new WireframeVisual3D() { OriginalModel = finalModel, WireframeType = WireframeVisual3D.WireframeTypes.WireframeWithOriginalSolidModel, UseModelColor = false, LineColor = Colors.Black, LineThickness = 0.5 }; // To specify line depth bias to the WireframeVisual3D, // we use SetDXAttribute extension method and use LineDepthBias as DXAttributeType wireframeVisual3D.SetDXAttribute(DXAttributeType.LineDepthBias, depthBias); // It would be also possible to set the depth with changing the DepthBias on the WpfWireframeVisual3DNode. // This is done with using the SetDepthBias method //SetDepthBias(dxViewportView, wireframeVisual3D, depthBias); parentViewport3D.Children.Add(wireframeVisual3D); _shownLineVisual3D = wireframeVisual3D; } _previousCameraDistance = sphereRadius * 4; targetPositionCamera.Distance = _previousCameraDistance; targetPositionCamera.Offset = new Vector3D(0, sphereRadius * 0.4, 0); }
private void RenderOfflineViewportButton_OnClick(object sender, RoutedEventArgs e) { // This method shows how to render a bitmap from a Viewport3D that is not shown in WPF visual tree (offline Viewport3D) // First create a new instance of Viewport3D var viewport3D = new Viewport3D(); // IMPORTANT !!! // You need to specify the size of the Viewport3D (this is needed because projection matrix requires an aspect ratio value). viewport3D.Width = 600; viewport3D.Height = 400; // It is recommended to create the camera before the objects are added to the scene. // This is especially true for 3D lines because this will generate the initial line geometry with the correct camera setting. // If the camera is added later or changed after the 3D lines are added to the scene, you need to manually call Refresh on LinesUpdater to regenerate 3D lines: // Ab3d.Utilities.LinesUpdater.Instance.Refresh(); // // This is not needed when the Viewport3D is shown in WPF visual tree. var targetPositionCamera = new TargetPositionCamera() { Heading = 30, Attitude = -20, Distance = 50, ShowCameraLight = ShowCameraLightType.Always, TargetViewport3D = viewport3D }; // When Viewport3D is not shown, we need to manually refresh the camera to initialize property and add the light to the scene. targetPositionCamera.Refresh(); // Now add 3D objects var boxVisual3D = new Ab3d.Visuals.BoxVisual3D() { CenterPosition = new Point3D(0, 0, 0), Size = new Size3D(8, 8, 8), Material = new DiffuseMaterial(Brushes.Orange), }; viewport3D.Children.Add(boxVisual3D); var wireBoxVisual3D = new Ab3d.Visuals.WireBoxVisual3D() { CenterPosition = new Point3D(0, 0, 0), Size = new Size3D(10, 10, 10), LineColor = Colors.Red, LineThickness = 1 }; viewport3D.Children.Add(wireBoxVisual3D); // If the camera was changed after the 3D lines were added, we need to manually regenerate 3D line geometry by calling Refresh on LinesUpdater: //Ab3d.Utilities.LinesUpdater.Instance.Refresh(); // // This is not needed when the Viewport3D is shown in WPF visual tree. // Now we can render Viewport3D to bitmap // When using Ab3d.DXEngine, then use the DXViewportView.RenderToBitmap method to also render DXEngine's objects // and get support for super-sampling that gets superior image quality. var renderedBitmap = RenderToBitmap(targetPositionCamera); SaveBitmap(renderedBitmap); // To make a simple demonstration on how to create a sequence of bitmaps // with changes the camera and object uncomment the following coed (you can also comment calling the SaveBitmap method in the RenderToBitmap) //for (int i = 1; i < 5; i++) //{ // targetPositionCamera.Heading = i * 20; // boxVisual3D.Transform = new ScaleTransform3D(1, (double)i / 4, 1); // // Now we can render Viewport3D to bitmap // var bitmap = RenderToBitmap(targetPositionCamera); // SaveBitmap(bitmap, $"c:\\temp\\bitmap_{i}.png"); // Change the path to a path that exist on your computer //} }