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 SetupContent() { var boxVisual3D = new Ab3d.Visuals.BoxVisual3D() { Size = new Size3D(50, 20, 30), Material = new DiffuseMaterial(Brushes.Green) }; _viewport3D.Children.Add(boxVisual3D); }
private void SetupContent() { var box1 = new Ab3d.Visuals.BoxVisual3D() { CenterPosition = new Point3D(0, 5, 0), Size = new Size3D(40, 10, 40), Material = new DiffuseMaterial(Brushes.Orange) }; _viewport3D.Children.Add(box1); var box2 = new Ab3d.Visuals.BoxVisual3D() { CenterPosition = new Point3D(0, 20, 0), Size = new Size3D(32, 20, 32), Material = new DiffuseMaterial(Brushes.Aqua) }; _viewport3D.Children.Add(box2); var wireGridVisual3D = new Ab3d.Visuals.WireGridVisual3D() { CenterPosition = new Point3D(0, -0.1, 0), Size = new Size(160, 160), WidthCellsCount = 16, HeightCellsCount = 16, MajorLinesFrequency = 4, LineColor = Colors.LightGray, LineThickness = 1, MajorLineColor = Colors.DimGray, MajorLineThickness = 1.5, }; _viewport3D.Children.Add(wireGridVisual3D); // Uncomment this code to add 11 boxes with different transparency levels. // This can be used to test saving to png files with using pre-multiplied or non pre-multiplied alpha color. //for (int i = 0; i <= 10; i++) //{ // var box = new Ab3d.Visuals.BoxVisual3D() // { // CenterPosition = new Point3D(-100 + i * 20, -30, 0), // Size = new Size3D(15, 15, 15), // Material = new DiffuseMaterial(new SolidColorBrush(Color.FromArgb((byte)(25.5 * i), (byte)255, (byte)255,(byte)0))) // }; // _viewport3D.Children.Add(box); //} }
private void SelectBox(BoxVisual3D selectedBoxVisual3D) { if (_selectedBoxVisual3D != null) { _selectedBoxVisual3D.Material = _standardMaterial; } if (selectedBoxVisual3D != null) { selectedBoxVisual3D.Material = _selectedMaterial; _selectedBoxVisual3D = selectedBoxVisual3D; MoveCameraTo(selectedBoxVisual3D); } }
private void CreateRandomScene() { ObjectsRootVisual3D.Children.Clear(); for (int i = 0; i < 6; i++) { var randomCenterPosition = new Point3D(_rnd.NextDouble() * 100 - 50, _rnd.NextDouble() * 60 - 30, _rnd.NextDouble() * 100 - 50); var boxVisual3D = new Ab3d.Visuals.BoxVisual3D() { CenterPosition = randomCenterPosition, Size = new Size3D(10, 8, 10), Material = new DiffuseMaterial(Brushes.Silver) }; ObjectsRootVisual3D.Children.Add(boxVisual3D); } }
public void SelectObject(Ab3d.Visuals.BoxVisual3D selectedBox) { _selectedBoxModel = selectedBox; if (_selectedBoxModel == null) { return; } ModelMover.Position = GetSelectedModelWorldPosition(); // GetSelectedModelPosition gets the _selectedBoxModel.CenterPosition and transforms it with the transformations of parent ModelVisual3D objects // Tell ModelDecoratorVisual3D which Model3D to show SelectedModelDecorator.TargetModel3D = _selectedBoxModel.Content; // NOTE: // When the 3D models are organized into hierarchy of models with using different ModelVisual3D or Model3DGroup objects, // you also need to so specify the SelectedModelDecorator.RootModelVisual3D in order to get the correct position of the TargetModel3D }
private void SelectObject(Ab3d.Visuals.BoxVisual3D selectedBox) { _selectedBoxModel = selectedBox; if (_selectedBoxModel == null) { return; } ModelMover.Position = _selectedBoxModel.CenterPosition; // Tell ModelDecoratorVisual3D which Model3D to show SelectedModelDecorator.TargetModel3D = _selectedBoxModel.Content; // NOTE: // When the 3D models are organized into hierarchy of models with using different ModelVisual3D or Model3DGroup objects, // you also need to so specify the SelectedModelDecorator.RootModelVisual3D in order to get the correct position of the TargetModel3D }
private void CreateSceneObjects() { // We store bounding box and current visibility status into a FrustumVisibilityStatus struct. // This way the frustum checks are optimized because we do not need to convert from WPF positions to SharpDX Vector3. // Also accessing DependencyProperties (CenterPosition, Size) is very slow. // // Also the organization of all FrustumVisibilityStatus structs in one array will give us // very memory cache friendly organization of data. var frustumStatuses = new List <FrustumVisibilityStatus>(); var halfBoxSize = 5; var boxSize = new Size3D(halfBoxSize * 2, halfBoxSize * 2, halfBoxSize * 2); for (int x = 0; x < 10; x++) { for (int z = 0; z < 10; z++) { for (int y = 0; y < 4; y++) { var centerPosition = new Point3D(-200 + 40 * x, y * 40, -200 + 40 * z); var boxVisual3D = new Ab3d.Visuals.BoxVisual3D() { CenterPosition = centerPosition, Size = boxSize, Material = _fullyVisibleMaterial }; BoxesRootVisual3D.Children.Add(boxVisual3D); frustumStatuses.Add(new FrustumVisibilityStatus(boxVisual3D, new BoundingBox(new Vector3((float)centerPosition.X - halfBoxSize, (float)centerPosition.Y - halfBoxSize, (float)centerPosition.Z - halfBoxSize), new Vector3((float)centerPosition.X + halfBoxSize, (float)centerPosition.Y + halfBoxSize, (float)centerPosition.Z + halfBoxSize)))); } } } // Convert to array. This will allow use to change Visibility field with passing array item by ref. _frustumStatuses = frustumStatuses.ToArray(); Camera1.Refresh(); UpdateVisibleBoxes(); }
public void SelectObject(Ab3d.Visuals.BoxVisual3D selectedBox) { _selectedBoxModel = selectedBox; if (_selectedBoxModel == null) { return; } var position = GetSelectedModelWorldPosition(); // GetSelectedModelPosition gets the _selectedBoxModel.CenterPosition and transforms it with the transformations of parent ModelVisual3D objects // Because ModelMoverVisual3D is in a separate Viewport3D that does not use the ZUpMatrix transformation, // we need to transform the position from z up coordinate system into the standard WPF 3D coordinate system: _modelMover.Position = _zUpTransform3D.Transform(position); // Tell ModelDecoratorVisual3D which Model3D to show SelectedModelDecorator.TargetModel3D = _selectedBoxModel.Content; // NOTE: // When the 3D models are organized into hierarchy of models with using different ModelVisual3D or Model3DGroup objects, // you also need to so specify the SelectedModelDecorator.RootModelVisual3D in order to get the correct position of the TargetModel3D }
private void CreateSceneObjects() { for (int y = 0; y < 5; y++) { for (int x = 0; x < 6; x++) { var boxVisual3D = new Ab3d.Visuals.BoxVisual3D() { CenterPosition = new Point3D(40 * x - 100, 6, y * 40 - 80), Size = new Size3D(10, 10, 10), Material = _standardMaterial }; SelectionRootModelVisual3D.Children.Add(boxVisual3D); } } // Use EventManager3D to handle clicking on selected boxes var eventManager3D = new Ab3d.Utilities.EventManager3D(MainViewport); var multiVisualEventSource3D = new MultiVisualEventSource3D(SelectionRootModelVisual3D.Children); multiVisualEventSource3D.MouseEnter += delegate(object sender, Mouse3DEventArgs e) { Mouse.OverrideCursor = Cursors.Hand; var hitBoxVisual3D = e.HitObject as Ab3d.Visuals.BoxVisual3D; if (hitBoxVisual3D != null) { _wireBoxVisual3D.CenterPosition = hitBoxVisual3D.CenterPosition; _wireBoxVisual3D.Size = hitBoxVisual3D.Size; MainViewport.Children.Add(_wireBoxVisual3D); } }; multiVisualEventSource3D.MouseLeave += delegate(object sender, Mouse3DEventArgs e) { Mouse.OverrideCursor = null; if (_wireBoxVisual3D != null) { MainViewport.Children.Remove(_wireBoxVisual3D); } }; multiVisualEventSource3D.MouseClick += delegate(object sender, MouseButton3DEventArgs e) { if (_selectedBoxVisual3D != null) { _selectedBoxVisual3D.Material = _standardMaterial; } var hitBoxVisual3D = e.HitObject as Ab3d.Visuals.BoxVisual3D; if (hitBoxVisual3D != null) { hitBoxVisual3D.Material = _selectedMaterial; _selectedBoxVisual3D = hitBoxVisual3D; if (_wireBoxVisual3D != null) { MainViewport.Children.Remove(_wireBoxVisual3D); } MoveCameraTo(hitBoxVisual3D); } else { _selectedBoxVisual3D = null; } }; eventManager3D.RegisterEventSource3D(multiVisualEventSource3D); // Exclude _wireBoxVisual3D from receiving mouse events eventManager3D.RegisterExcludedVisual3D(_wireBoxVisual3D); }