public SolidColorInstanceRendering() { InitializeComponent(); MainDXViewportView.DXSceneDeviceCreated += delegate(object sender, EventArgs args) { _currentRenderingType = GetCurrentRenderingType(); CreateInstances(); ChangeRenderingType(); }; // IMPORTANT: // It is very important to call Dispose method on DXSceneView after the control is not used any more (see help file for more info) this.Unloaded += (sender, args) => { if (_solidColorEffectWithOutline != null) { _solidColorEffectWithOutline.Dispose(); _solidColorEffectWithOutline = null; } MainDXViewportView.Dispose(); }; }
private void ChangeRenderingType() { if (MainDXViewportView.DXScene == null) { return; // Probably WPF 3D rendering } var color = GetColorFromComboBox(SolidModelColorComboBox); if (_currentRenderingType == RenderingTypes.SolidColorEffect) { MainDXViewportView.DXScene.DefaultRenderObjectsRenderingStep.OverrideEffect = null; } _currentRenderingType = GetCurrentRenderingType(); if (_currentRenderingType == RenderingTypes.SolidColorEffect) { _solidColorEffectWithOutline = new Ab3d.DirectX.Effects.SolidColorEffect { Color = color.ToColor4(), OverrideModelColor = true }; _solidColorEffectWithOutline.InitializeResources(MainDXViewportView.DXScene.DXDevice); MainDXViewportView.DXScene.DefaultRenderObjectsRenderingStep.OverrideEffect = _solidColorEffectWithOutline; } else if (_currentRenderingType == RenderingTypes.UseSingleObjectColor) { // Set IsSolidColorMaterial to render tube instances with solid color without any shading based on lighting _instancedMeshGeometryVisual3D.IsSolidColorMaterial = IsSolidColorMaterialCheckBox.IsChecked ?? false; var instancesData = _instancedMeshGeometryVisual3D.InstancesData; int instancesCount = instancesData.Length; for (var i = 0; i < instancesCount; i++) { instancesData[i].DiffuseColor = color.ToColor4(); } _instancedMeshGeometryVisual3D.Update(0, instancesCount, updateBounds: false); } else { CreateInstances(); } }
private void SetSpecialRenderingType(DXViewportView dxViewportView, RenderingTypes renderingType) { if (dxViewportView.DXScene.DefaultRenderObjectsRenderingStep == null) { return; // Not yet initialized } // First reset any previous customizations dxViewportView.DXScene.DefaultRenderObjectsRenderingStep.OverrideEffect = null; dxViewportView.DXScene.DefaultRenderObjectsRenderingStep.FilterObjectsFunction = null; dxViewportView.DXScene.DefaultRenderObjectsRenderingStep.FilterRenderingQueuesFunction = null; if (renderingType == RenderingTypes.Wireframe) { // Use ModelColorLineEffect to render the 3D objects as wireframe (model colors define the color of the lines). // Note that instanced objects and some other advanced objects are not rendered. if (_modelColorLineEffect == null) { _modelColorLineEffect = _masterDXViewportView.DXScene.DXDevice.EffectsManager.GetEffect <ModelColorLineEffect>(); _modelColorLineEffect.LineThickness = 1; } dxViewportView.DXScene.DefaultRenderObjectsRenderingStep.OverrideEffect = _modelColorLineEffect; } else if (renderingType == RenderingTypes.FilterByObjects) { dxViewportView.DXScene.DefaultRenderObjectsRenderingStep.FilterObjectsFunction = delegate(RenderablePrimitiveBase renderablePrimitive) { var originalSceneNode = renderablePrimitive.OriginalObject as SceneNode; if (originalSceneNode != null) { return(originalSceneNode.Name.StartsWith("Sphere") || originalSceneNode.Name.StartsWith("Cylinder")); } return(false); }; } else if (renderingType == RenderingTypes.FilerByRenderingQueue) { dxViewportView.DXScene.DefaultRenderObjectsRenderingStep.FilterRenderingQueuesFunction = renderingQueue => renderingQueue.Name == "CustomRenderingQueue"; } dxViewportView.DXScene.NotifyChange(DXScene.ChangeNotifications.RenderingStepsChanged); }
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 DXViewportView AddNewDXViewportView(DXViewportView masterDXViewportView, int rowIndex, int columnIndex, RenderingTypes renderingType, bool addToRootGrid = true, bool addDrawSelectionComboBox = true) { TargetPositionCamera targetPositionCamera; MouseCameraController mouseCameraController; DXViewportView dxViewportView = CreateSimpleDXSceneViewFromCode(masterDXViewportView, renderingType, out targetPositionCamera, out mouseCameraController); // Set random BackgroundColor - but do not make it to dark so start with 128 dxViewportView.BackgroundColor = Color.FromRgb((byte)(_rnd.Next(128) + 128), (byte)(_rnd.Next(128) + 128), (byte)(_rnd.Next(128) + 128)); _dxViewportViews.Add(dxViewportView); if (addToRootGrid) { var viewRootGrid = new Grid(); var border = new Border() { Background = Brushes.Transparent, Margin = new Thickness(1, 1, 3, 3) }; border.Child = dxViewportView; mouseCameraController.EventsSourceElement = border; viewRootGrid.Children.Add(border); viewRootGrid.Children.Add(targetPositionCamera); viewRootGrid.Children.Add(mouseCameraController); Grid.SetColumn(viewRootGrid, columnIndex); Grid.SetRow(viewRootGrid, rowIndex); ViewsGrid.Children.Add(viewRootGrid); var textBlock = new TextBlock() { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(5, 5, 0, 0) }; textBlock.Text = masterDXViewportView == null ? "Master DXScene:" : "Child DXScene:"; viewRootGrid.Children.Add(textBlock); if (addDrawSelectionComboBox) { var comboBox = new ComboBox() { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(0, 3, 5, 0) }; comboBox.ItemsSource = RenderingTypeStrings; comboBox.SelectedIndex = (int)renderingType; if (masterDXViewportView == null) { comboBox.IsEditable = false; // Lock Standard for master view } comboBox.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args) { var newRenderingType = (RenderingTypes)comboBox.SelectedIndex; SetSpecialRenderingType(dxViewportView, newRenderingType); dxViewportView.Refresh(); // Render the scene again }; viewRootGrid.Children.Add(comboBox); } } return(dxViewportView); }