Ejemplo n.º 1
0
        public void SelectObject(Ab3d.UIElements.BoxUIElement3D selectedBox)
        {
            // Deselect currently selected model
            if (_selectedBoxModel != null)
            {
                // Set material back to normal
                _selectedBoxModel.Material     = _normalMaterial;
                _selectedBoxModel.BackMaterial = null;

                // Allow hit testing again - so user can select that object again
                _selectedBoxModel.IsHitTestVisible = true;

                _selectedBoxModel = null;
            }

            _selectedBoxModel = selectedBox;
            if (_selectedBoxModel == null)
            {
                return;
            }


            // Prevent hit-testing in selected model
            // This will allow clicking on the parts of move arrows that are inside the selected model
            // Note that IsHitTestVisible is available only on models derived from UIElement3D (if you need that on GeometryModel3D or ModelVisual3D, then use ModelUIElement3D as parent of your model)
            _selectedBoxModel.IsHitTestVisible = false;


            // Change material to semi-transparent Silver
            _selectedBoxModel.Material     = _selectedMaterial;
            _selectedBoxModel.BackMaterial = _selectedMaterial; // We also set BackMaterial so the inner side of boxes will be visible

            // To render the transparent object correctly, we need to sort the objects so that the transparent objects are rendered after other objects
            // We can use the TransparencySorter from Ab3d.PowerToys
            // Note that it is also possible to use TransparencySorter with many advanced features - see the Model3DTransparencySortingSample for more info
            TransparencySorter.SimpleSort(SceneObjectsContainer.Children);

            // In our simple case (we have only one transparent object), we could also manually "sort" the objects with moving the transparent object to the back of the Children collection:
            //SceneObjectsContainer.Children.Remove(_selectedBoxVisual3D);
            //SceneObjectsContainer.Children.Add(_selectedBoxVisual3D);

            _standardTransform3D           = StandardTransform3D.GetStandardTransform3D(_selectedBoxModel);
            SelectedModelRotator.Transform = _standardTransform3D.Transform;
        }
Ejemplo n.º 2
0
        private void SelectDuckVisual3D(ModelVisual3D duckModelVisual3D)
        {
            _selectedVisual3D = duckModelVisual3D;

            _standardTransform3D = StandardTransform3D.GetStandardTransform3D(duckModelVisual3D);
            TransformEditor.StandardTransform3D = _standardTransform3D;

            if (MoveButton.IsChecked ?? false)
            {
                ShowModelMover();
            }
            else if (RotateButton.IsChecked ?? false)
            {
                ShowModelRotator();
            }
            else if (ScaleButton.IsChecked ?? false)
            {
                ShowModelScalar();
            }
            else
            {
                MoveButton.IsChecked = true; // Show model mover
            }
        }
Ejemplo n.º 3
0
        private void GenerateRandomDucks(int ducksCount)
        {
            var rnd = new Random();

            double  lakeRadius = LakeCircleVisual3D.Radius;
            Point3D lakeCenter = LakeCircleVisual3D.CenterPosition;

            _duckModels = new List <ModelVisual3D>(ducksCount);
            RootDucksVisual3D.Children.Clear();

            for (int i = 0; i < ducksCount; i++)
            {
                bool findNewPosition;

                do
                {
                    var position = new Point3D(rnd.NextDouble() * lakeRadius * 2 - lakeRadius,
                                               0,
                                               rnd.NextDouble() * lakeRadius * 2 - lakeRadius);

                    // If position is outside of 80% of lake radius
                    double distanceToLakeCenter = (position - lakeCenter).Length;
                    findNewPosition = distanceToLakeCenter > (lakeRadius * 0.8);

                    if (!findNewPosition)
                    {
                        // Now check if too close to any other duck
                        foreach (var duckModel in _duckModels)
                        {
                            var standardTransform3D = StandardTransform3D.GetStandardTransform3D(duckModel);

                            var distanceToDuck = (position - standardTransform3D.GetTranslateVector3D().ToPoint3D()).Length;
                            if (distanceToDuck < DuckSize * 4)
                            {
                                findNewPosition = true;
                                break;
                            }
                        }
                    }

                    if (!findNewPosition)
                    {
                        // The position is ok

                        double scale = rnd.NextDouble() + 1;

                        var standardTransform3D = new StandardTransform3D()
                        {
                            TranslateX = position.X,
                            TranslateY = position.Y,
                            TranslateZ = position.Z,

                            RotateY = rnd.NextDouble() * 360,

                            ScaleX = scale,
                            ScaleY = scale,
                            ScaleZ = scale
                        };

                        var duckVisual3D = new ModelVisual3D()
                        {
                            Content = _duckModel3D,
                        };

                        StandardTransform3D.SetStandardTransform3D(duckVisual3D, standardTransform3D, updateTransform3D: true);


                        _duckModels.Add(duckVisual3D);
                        RootDucksVisual3D.Children.Add(duckVisual3D);


                        var visualEventSource3D = new VisualEventSource3D(duckVisual3D);
                        visualEventSource3D.MouseEnter += OnDuckMouseEnter;
                        visualEventSource3D.MouseLeave += OnDuckMouseLeave;
                        visualEventSource3D.MouseDown  += OnDuckMouseDown;
                        visualEventSource3D.MouseUp    += OnDuckMouseUp;

                        _eventManager3D.RegisterEventSource3D(visualEventSource3D);
                    }
                } while (findNewPosition);
            }
        }