示例#1
0
        private void CreateTestScene()
        {
            var boxMesh    = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 0, 0), new Size3D(1, 1, 1), 1, 1, 1).Geometry;
            var sphereMesh = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 0.7, 30).Geometry;

            int modelsXCount = 10;
            int modelsYCount = 1;
            int modelsZCount = 10;

            _objectsModel3DGroup = new Model3DGroup();

            AddModels(_objectsModel3DGroup, boxMesh, new Point3D(0, 5, 0), new Size3D(500, modelsYCount * 10, 500), 10, modelsXCount, modelsYCount, modelsZCount);
            AddModels(_objectsModel3DGroup, sphereMesh, new Point3D(25, 5, 25), new Size3D(500, modelsYCount * 10, 500), 10, modelsXCount, modelsYCount, modelsZCount);

            MainViewport.Children.Add(_objectsModel3DGroup.CreateContentVisual3D());

            // It would be optimal to use WireGridVisual3D to create a wire grid.
            // But because WireGridVisual3D creates a MultiLineVisual3D behind the scene, all the lines can have only a single color.
            // Therefore we create multiple lines for this sample so we can easily change color of individual lines.
            // And what is more, this way the object id map can get us the hit 3D lines (otherwise the whole MultiLineVisual3D would be hit)
            // <visuals:WireGridVisual3D CenterPosition="0 0 0" Size="500 500" WidthCellsCount="20" HeightCellsCount="20" LineColor="#555555" LineThickness="5"/>

            var contentVisual3D = CreateWireGridLines(new Point3D(0, 0, 0), new Size(500, 500), 20, 20, new Vector3D(1, 0, 0), new Vector3D(0, 0, 1), Colors.Gray, 5);

            MainViewport.Children.Add(contentVisual3D);
        }
示例#2
0
        private void CreateInstancedObjects()
        {
            MainViewport.Children.Clear();


            _currentMeshType = (MeshTypes)Enum.Parse(typeof(MeshTypes), (string)ObjectsTypeComboBox.SelectedItem);
            _screenSize      = (float)ScreenSizeComboBox.SelectedItem;

            MeshGeometry3D mesh;

            switch (_currentMeshType)
            {
            case MeshTypes.Box:
                mesh = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 0, 0), new Size3D(1, 1, 1), 1, 1, 1).Geometry;
                break;

            case MeshTypes.Sphere:
                mesh = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 0.5, 30).Geometry;
                break;

            case MeshTypes.Arrow:
                mesh = new Ab3d.Meshes.ArrowMesh3D(new Point3D(-0.5, 0, 0), new Point3D(0.5, 0, 0), 0.1, 0.2, 45, 30, false).Geometry;
                break;

            default:
                mesh = null;
                break;
            }

            var instancedData = GetInstancedData(_currentMeshType, _screenSize);


            _instancedMeshGeometryVisual3D = new InstancedMeshGeometryVisual3D(mesh);
            _instancedMeshGeometryVisual3D.InstancesData = instancedData;

            // To enable screen-space scaling, set the UseScreenSpaceScaling to true.
            //
            // IMPORTANT:
            // For this to work correctly, the size of the instance mesh must be 1 and the center of the mesh must be at (0, 0, 0).
            // In this case the mesh will be scaled correctly to the screen size that is defined in the instance matrix's scale component.
            _instancedMeshGeometryVisual3D.UseScreenSpaceScaling = UseScreenSpaceScaleCheckBox.IsChecked ?? false;

            MainViewport.Children.Add(_instancedMeshGeometryVisual3D);

            _instanceWorldPositions = null; // Reset array of instance positions that is used for finding the closest instance in screen coordinates
            _selectedInstanceIndex  = -1;

            // Because we have cleared all MainViewport.Children, this also removed the camera's light.
            // Call Refresh to recreate the light
            Camera1.Refresh();
        }
示例#3
0
        private void CreateRootModel()
        {
            switch (ObjectComboBox.SelectedIndex)
            {
            case 0:
                Ab3d.Meshes.BoxMesh3D box = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 0, 0), new Size3D(130, 60, 100), 1, 1, 1);
                _rootMesh = box.Geometry;
                break;

            case 1:
                Ab3d.Meshes.BoxMesh3D box2 = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 0, 0), new Size3D(130, 60, 100), 4, 4, 4);
                _rootMesh = box2.Geometry;
                break;

            case 2:
                Ab3d.Meshes.SphereMesh3D sphere = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 80, 10);
                _rootMesh = sphere.Geometry;
                break;

            case 3:
                Ab3d.Meshes.SphereMesh3D sphere2 = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 80, 5);
                _rootMesh = sphere2.Geometry;
                break;

            case 4:
                Ab3d.Meshes.CylinderMesh3D cylinder = new Ab3d.Meshes.CylinderMesh3D(new Point3D(0, -50, 0), 60, 100, 12, true);
                _rootMesh = cylinder.Geometry;
                break;

            case 5:
                Ab3d.Meshes.ConeMesh3D cone = new Ab3d.Meshes.ConeMesh3D(new Point3D(0, -50, 0), 30, 60, 100, 12, true);
                _rootMesh = cone.Geometry;
                break;

            case 6:
                Ab3d.Meshes.ConeMesh3D cone2 = new Ab3d.Meshes.ConeMesh3D(new Point3D(0, -50, 0), 30, 60, 100, 6, false);
                _rootMesh = cone2.Geometry;
                break;

            case 7:
                var readerObj   = new Ab3d.ReaderObj();
                var teapotModel = readerObj.ReadModel3D(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resources\ObjFiles\Teapot.obj")) as GeometryModel3D;

                if (teapotModel == null)
                {
                    return;
                }

                // Get the teapot MeshGeometry3D
                _rootMesh = (MeshGeometry3D)teapotModel.Geometry;

                break;

            default:
                _rootMesh = null;
                break;
            }


            var geometryModel3D = new GeometryModel3D(_rootMesh, new DiffuseMaterial(Brushes.Silver));

            MainViewport.Children.Clear();
            MainViewport.Children.Add(geometryModel3D.CreateModelVisual3D());

            MeshInspector.MeshGeometry3D = _rootMesh;
        }
示例#4
0
        private void CreateScene()
        {
            int xCount = 40;
            int yCount = 1;
            int zCount = 40;

            float sphereRadius = 10;
            float sphereMargin = 10;

            var sphereMeshGeometry3D = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), sphereRadius, 10).Geometry;

            _oneMeshTriangleIndicesCount = sphereMeshGeometry3D.TriangleIndices.Count;


            PositionNormalTexture[] vertexBuffer;
            int[] indexBuffer;

            var size = new Vector3(xCount * (sphereRadius + sphereMargin), yCount * (sphereRadius + sphereMargin), zCount * (sphereRadius + sphereMargin));

            SubMeshesSample.CreateMultiMeshBuffer(center: new Vector3(0, 0, 0),
                                                  size: size,
                                                  xCount: xCount, yCount: yCount, zCount: zCount,
                                                  meshGeometry3D: sphereMeshGeometry3D,
                                                  vertexBuffer: out vertexBuffer,
                                                  indexBuffer: out indexBuffer);

            _multiMaterialMesh = new SimpleMesh <PositionNormalTexture>(vertexBuffer, indexBuffer,
                                                                        inputLayoutType: InputLayoutType.Position | InputLayoutType.Normal | InputLayoutType.TextureCoordinate);


            // Create all 3 SubMeshes at the beginning.
            // Though at first only the first SubMesh will be rendered (the other two have IndexCount set to 0),
            // this will allow us to simply change the StartIndexLocation and IndexCount of the SubMeshes
            // to show selected part without adding or removing any SubMesh (this would regenerate the RenderingQueues).
            // This way the selection is almost a no-op (only changing a few integer values and rendering the scene again).
            _multiMaterialMesh.SubMeshes = new SubMesh[]
            {
                // First sub-mesh will render triangles from the first to the start of selection (or all triangles if there is no selection)
                new SubMesh("MainSubMesh1")
                {
                    MaterialIndex = 0, StartIndexLocation = 0, IndexCount = indexBuffer.Length
                },

                // Second sub-mesh will render triangles after the selection (this one follows the first on to preserve the same material)
                new SubMesh("MainSubMesh2")
                {
                    MaterialIndex = 0, StartIndexLocation = 0, IndexCount = 0
                },

                // The third sub-mesh will render selected triangles and will use the second material for that.
                new SubMesh("SelectionSubMesh")
                {
                    MaterialIndex = 1, StartIndexLocation = 0, IndexCount = 0
                },
            };

            _disposables.Add(_multiMaterialMesh);

            // Create OctTree from vertexBuffer.
            // This will significantly improve hit testing performance (check this with uncommenting the dxScene.GetClosestHitObject call in OnMouseMouse method).
            _octTree = new OctTree(vertexBuffer, indexBuffer);


            var materials = new Ab3d.DirectX.Material[]
            {
                new Ab3d.DirectX.Materials.StandardMaterial()
                {
                    DiffuseColor = Colors.Green.ToColor3()
                },
                new Ab3d.DirectX.Materials.StandardMaterial()
                {
                    DiffuseColor = Colors.Red.ToColor3()
                }
            };

            _meshObjectNode = new Ab3d.DirectX.MeshObjectNode(_multiMaterialMesh, materials);

            _disposables.Add(_meshObjectNode);

            // Use SceneNodeVisual3D to show SceneNode in DXViewportView
            var sceneNodeVisual3D = new SceneNodeVisual3D(_meshObjectNode);

            MainViewport.Children.Add(sceneNodeVisual3D);
        }
        private void CreateRootModel()
        {
            bool show3DLine = false;

            switch (ObjectComboBox.SelectedIndex)
            {
            case 0:
                Ab3d.Meshes.SphereMesh3D sphere = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 80, 10);
                _rootMesh = sphere.Geometry;
                break;

            case 1:
                Ab3d.Meshes.SphereMesh3D sphere2 = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 80, 5);
                _rootMesh = sphere2.Geometry;
                break;

            case 2:
                // NOTE: Here we create an optimized version of sphere but it does not have texture coordinates
                // If we do not need texture coordinates (do not need to show texture), than we can slightly simplify the sphere model.
                // When we also need to create texture coordinates, the top most position that represent the same point in space is defined multiple times
                // - each time with slightly different texture coordinate (so that the whole top line on the texture is shown around the top sphere position).
                // Also there are x + 1 position around the sphere (in one row) - one additional position is added that the first and last position in the "row" are in the same space position,
                // but the first position has x texture coordinate 0, the last position has x texture coordinate 1 - this shows the whole texture nicely around the sphere.
                // If texture coordinates are not needed, than we do not need to define more than one upper and bottom positions,
                // and also do not need to add another position to the row.
                // Optimized sphere can be created only with SphereMesh3D object (not with SphereVisual3D)
                Ab3d.Meshes.SphereMesh3D sphere3 = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 80, 12, false);     // false: generateTextureCoordinates
                _rootMesh = sphere3.Geometry;
                break;

            case 3:
                Ab3d.Meshes.BoxMesh3D box = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 0, 0), new Size3D(130, 60, 100), 1, 1, 1);
                _rootMesh = box.Geometry;
                break;

            case 4:
                Ab3d.Meshes.BoxMesh3D box2 = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 0, 0), new Size3D(130, 60, 100), 4, 4, 4);
                _rootMesh = box2.Geometry;
                break;

            case 5:
                Ab3d.Meshes.CylinderMesh3D cylinder = new Ab3d.Meshes.CylinderMesh3D(new Point3D(0, -50, 0), 60, 100, 12, true);
                _rootMesh = cylinder.Geometry;
                break;

            case 6:
                Ab3d.Meshes.ConeMesh3D cone = new Ab3d.Meshes.ConeMesh3D(new Point3D(0, -50, 0), 30, 60, 100, 12, true);
                _rootMesh = cone.Geometry;
                break;

            case 7:
                Ab3d.Meshes.ConeMesh3D cone2 = new Ab3d.Meshes.ConeMesh3D(new Point3D(0, -50, 0), 30, 60, 100, 6, false);
                _rootMesh = cone2.Geometry;
                break;

            case 8:
                var readerObj   = new Ab3d.ReaderObj();
                var teapotModel = readerObj.ReadModel3D(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resources\ObjFiles\Teapot.obj")) as GeometryModel3D;

                if (teapotModel == null)
                {
                    return;
                }

                // Get the teapot MeshGeometry3D
                _rootMesh = (MeshGeometry3D)teapotModel.Geometry;

                break;

            case 9:
                var line = new Ab3d.Visuals.LineVisual3D()
                {
                    StartPosition = new Point3D(0, 0, 0),
                    EndPosition   = new Point3D(100, 0, 0),
                    LineColor     = Colors.Silver,
                    LineThickness = 20
                };

                Show3DLines(line);
                show3DLine = true;
                break;

            case 10:
                var polyLineVisual3D = new Ab3d.Visuals.PolyLineVisual3D()
                {
                    Positions     = new Point3DCollection(new Point3D[] { new Point3D(-75, 50, 0), new Point3D(-25, 0, 0), new Point3D(25, 50, 0), new Point3D(75, 0, 0) }),
                    LineThickness = 20
                };

                Show3DLines(polyLineVisual3D);
                show3DLine = true;
                break;

            case 11:
                // This is the same line as in the previous sample (PolyLineVisual3D), but this time
                // it is created as diconnected list of lines - note that this requires that the positions are duplicated.
                var multiLineVisual3D = new Ab3d.Visuals.MultiLineVisual3D()
                {
                    Positions = new Point3DCollection(new Point3D[] { new Point3D(-75, 50, 0), new Point3D(-25, 0, 0),
                                                                      new Point3D(-25, 0, 0), new Point3D(25, 50, 0),
                                                                      new Point3D(25, 50, 0), new Point3D(75, 0, 0) }),
                    LineThickness = 20
                };

                Show3DLines(multiLineVisual3D);
                show3DLine = true;
                break;

            default:
                _rootMesh = null;
                break;
            }

            // If we were looking at 3D lines before and now we are looking an standard 3D models,
            // we adjust the camera back to the side view (from direct front view)
            if (_isShowing3DLines && !show3DLine)
            {
                Camera1.Heading  = 30;
                Camera1.Attitude = -20;
                Camera1.Distance = 300;
            }

            _isShowing3DLines = show3DLine;


            _rootModel          = new GeometryModel3D();
            _rootModel.Geometry = _rootMesh;
            _rootModel.Material = new DiffuseMaterial(Brushes.DarkBlue);

            ObjectModelVisual.Content = _rootModel;

            Ab3d.Utilities.ModelEventSource3D modelEventSource = new Ab3d.Utilities.ModelEventSource3D();
            modelEventSource.TargetModel3D = _rootModel;
            modelEventSource.MouseClick   += new Ab3d.Common.EventManager3D.MouseButton3DEventHandler(modelEventSource_MouseClick);

            _eventsManager.ResetEventSources3D();
            _eventsManager.RegisterEventSource3D(modelEventSource);

            MainViewport.Cursor = Cursors.Hand;
        }
        public DXEventManager3DSample()
        {
            InitializeComponent();


            Box1Visual3D.SetName("Box1Visual3D");
            BoxesGroupVisual3D.SetName("BoxesGroupVisual3D");
            Box2Visual3D.SetName("Box2Visual3D");
            Box3Visual3D.SetName("Box3Visual3D");
            Box4Visual3D.SetName("Box4Visual3D");
            Line1.SetName("Line1");
            Line2.SetName("Line2");
            Rectangle1.SetName("Rectangle1");


            var meshGeometry3D = new Ab3d.Meshes.SphereMesh3D(centerPosition: new Point3D(0, 0, 0), radius: 5, segments: 20, generateTextureCoordinates: false).Geometry;

            // The following method prepare InstanceData array with data for each instance (WorldMatrix and Color)
            InstanceData[] instancedData = DXEnginePerformance.InstancedMeshGeometry3DTest.CreateInstancesData(center: new Point3D(0, 0, 0),
                                                                                                               size: new Size3D(200, 50, 50),
                                                                                                               modelScaleFactor: 1,
                                                                                                               xCount: 10,
                                                                                                               yCount: 3,
                                                                                                               zCount: 3,
                                                                                                               useTransparency: false);


            // Create InstancedGeometryVisual3D with selected meshGeometry and InstancesData
            _instancedMeshGeometryVisual3D = new InstancedMeshGeometryVisual3D(meshGeometry3D);
            _instancedMeshGeometryVisual3D.InstancesData = instancedData;
            _instancedMeshGeometryVisual3D.SetName("InstancedMeshGeometryVisual3D");
            _instancedMeshGeometryVisual3D.Transform = new TranslateTransform3D(0, 50, 0);

            MainViewport3D.Children.Add(_instancedMeshGeometryVisual3D);

            _selectedInstanceIndex = -1;


            var model3DGroup = new Model3DGroup();

            model3DGroup.SetName("Model3DGroup");

            var frozenModel3DGroup = new Model3DGroup();

            frozenModel3DGroup.SetName("FrozenModel3DGroup");

            var box1Material = new DiffuseMaterial(Brushes.LightPink);
            var box2Material = new DiffuseMaterial(Brushes.LightCyan);

            var boxModel3D = Ab3d.Models.Model3DFactory.CreateBox(new Point3D(-100, 2, 25), new Size3D(60, 4, 50), box2Material);

            frozenModel3DGroup.Children.Add(boxModel3D);

            var pyramidModel3D = Ab3d.Models.Model3DFactory.CreatePyramid(new Point3D(-100, 2, 20), new Size3D(20, 20, 20), box2Material);

            frozenModel3DGroup.Children.Add(pyramidModel3D);

            for (int i = 0; i < 5; i++)
            {
                var geometryModel3D = new GeometryModel3D(meshGeometry3D, box1Material);
                geometryModel3D.Transform = new TranslateTransform3D(-130 + i * 15, 5, 80);
                geometryModel3D.SetName("GroupedSphere_" + i.ToString());

                model3DGroup.Children.Add(geometryModel3D);

                geometryModel3D           = new GeometryModel3D(meshGeometry3D, box2Material);
                geometryModel3D.Transform = new TranslateTransform3D(-130 + i * 15, 10, 50);
                geometryModel3D.SetName("FrozenGroupedSphere_" + i.ToString());

                frozenModel3DGroup.Children.Add(geometryModel3D);
            }


            frozenModel3DGroup.Freeze();

            MainViewport3D.Children.Add(model3DGroup.CreateModelVisual3D());
            MainViewport3D.Children.Add(frozenModel3DGroup.CreateModelVisual3D());

            _dxEventManager3D = new DXEventManager3D(MainDXViewportView);

            RegisterEventSource(Box1Visual3D);
            RegisterEventSource(BoxesGroupVisual3D);
            RegisterEventSource(Line1);
            RegisterEventSource(Line2);
            RegisterEventSource(Rectangle1);
            RegisterEventSource(_instancedMeshGeometryVisual3D);

            RegisterEventSource(model3DGroup);
            RegisterEventSource(frozenModel3DGroup);


            // Prevent TransparentPlaneVisual3D to be used by hit-testing
            _dxEventManager3D.RegisterExcludedVisual3D(TransparentPlaneVisual3D);


            this.PreviewMouseMove += delegate(object sender, MouseEventArgs args)
            {
                var position = args.GetPosition(this);
                MousePositionTextBlock.Text = $"Mouse pos: {position.X:0} {position.Y:0}";
            };

            this.PreviewMouseDown += delegate(object sender, MouseButtonEventArgs e)
            {
                var position = e.GetPosition(this);
                if (position == _lastMouseDownPosition)
                {
                    return;
                }

                System.Diagnostics.Debug.WriteLine("Mouse position: " + position.ToString());

                _lastMouseDownPosition = position;
            };


            // 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) => MainDXViewportView.Dispose();
        }
        // See also:
        // - Objects3D/BooleanOperationsSample to see other boolean operations
        // - Utilities/TextureCoordinatesGeneratorSample to see other options to define TextureCoordinates that are needed to show texture images

        public ModelingWithBooleanOperations()
        {
            InitializeComponent();

            _orangeMaterial = new MaterialGroup();
            _orangeMaterial.Children.Add(new DiffuseMaterial(Brushes.Orange));
            _orangeMaterial.Children.Add(new SpecularMaterial(Brushes.White, 16));

            // We will start with a simple box
            // Create it manually with MeshGeometry3D
            MeshGeometry3D initialBoxMesh3D = new Ab3d.Meshes.BoxMesh3D(new Point3D(-100, 25, 0), new Size3D(200, 50, 100), 1, 1, 1).Geometry;

            // Without boolean operations, we could simply create the box with:
            //var boxVisual3D = new BoxVisual3D()
            //{
            //    CenterPosition = new Point3D(-100, 25, 0),
            //    Size = new Size3D(200, 50, 100),
            //    UseCachedMeshGeometry3D = false, // we should not use the cached
            //    Material = _orangeMaterial
            //};


            // Define MeshGeometry3D objects that will be used for subtractions
            var centerSphereMesh = new Ab3d.Meshes.SphereMesh3D(new Point3D(-100, 50, 0), 40, 16).Geometry;

            var box1Mesh = new Ab3d.Meshes.BoxMesh3D(new Point3D(-180, 0, -30), new Size3D(10, 200, 10), 1, 1, 1).Geometry;
            var box2Mesh = new Ab3d.Meshes.BoxMesh3D(new Point3D(-180, 0, 0), new Size3D(10, 200, 10), 1, 1, 1).Geometry;
            var box3Mesh = new Ab3d.Meshes.BoxMesh3D(new Point3D(-180, 0, 30), new Size3D(10, 200, 10), 1, 1, 1).Geometry;

            var cylinder1Mesh3D = new Ab3d.Meshes.CylinderMesh3D(new Point3D(-20, 0, -30), 8, 200, segments: 16, isSmooth: false).Geometry;
            var cylinder2Mesh3D = new Ab3d.Meshes.CylinderMesh3D(new Point3D(-20, 0, 0), 8, 200, segments: 16, isSmooth: false).Geometry;
            var cylinder3Mesh3D = new Ab3d.Meshes.CylinderMesh3D(new Point3D(-20, 0, 30), 8, 200, segments: 16, isSmooth: false).Geometry;


            // When doing multiple boolean operations one after another,
            // it is recommended (and mush faster) to use BooleanMesh3D object.

            var booleanMesh3D = new Ab3d.Meshes.BooleanMesh3D(initialBoxMesh3D);

            booleanMesh3D.Subtract(centerSphereMesh);

            booleanMesh3D.Subtract(box1Mesh);
            booleanMesh3D.Subtract(box2Mesh);
            booleanMesh3D.Subtract(box3Mesh);

            booleanMesh3D.Subtract(cylinder1Mesh3D);
            booleanMesh3D.Subtract(cylinder2Mesh3D);
            booleanMesh3D.Subtract(cylinder3Mesh3D);


            // It is also possible to use Subtract method on BoxVisual3D object:
            //boxVisual3D.Subtract(centerSphereMesh);
            //boxVisual3D.Subtract(box1Mesh);
            //boxVisual3D.Subtract(box2Mesh);
            //boxVisual3D.Subtract(box3Mesh);
            //boxVisual3D.Subtract(cylinder1Mesh3D);
            //boxVisual3D.Subtract(cylinder2Mesh3D);
            //boxVisual3D.Subtract(cylinder3Mesh3D);


            // We could also use the static methods on Ab3d.Utilities.MeshBooleanOperations class:
            //Ab3d.Utilities.MeshBooleanOperations.Subtract(boxVisual3D, sphereMesh);
            //Ab3d.Utilities.MeshBooleanOperations.Subtract(boxVisual3D, boxMesh);


            // When calling the Geometry getter, the BooleanMesh3D converts the internal object into MeshGeometry3D.
            var meshGeometry3D = booleanMesh3D.Geometry;

            var initialGeometryModel3D = new GeometryModel3D(meshGeometry3D, _orangeMaterial);
            var boxVisual3D            = initialGeometryModel3D.CreateModelVisual3D();

            // Now add the changed box to the scene
            MainViewport.Children.Add(boxVisual3D);



            // When working with more complex meshes, the boolean operations can take some time.
            // To prevent blocking UI thread, we can do the boolean operations in the background thread.
            // To do this we need to freeze the MeshGeometry3D that are send to the background thread and back to UI thread.
            //
            // The following code is using spheres with 30 segments to demonstrate a longer boolean operations.
            //

            // First define the original MeshGeometry3D
            initialBoxMesh3D = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 25, 0), new Size3D(100, 50, 100), 1, 1, 1).Geometry;

            ShowNewOriginalMesh(initialBoxMesh3D);


            var initialBooleanMesh3D = new Ab3d.Meshes.BooleanMesh3D(initialBoxMesh3D);

            var sphereCenters = new Point3D[]
            {
                new Point3D(-50, 50, 50),
                new Point3D(50, 50, 50),
                new Point3D(50, 50, -50),
                new Point3D(-50, 50, -50),
                new Point3D(-50, 0, 50),
                new Point3D(50, 0, 50),
                new Point3D(50, 0, -50),
                new Point3D(-50, 0, -50)
            };

            // We can create the MeshGeometry3D and prepare the BooleanMesh3D in parallel - each can be created in its own thread
            var sphereBooleanMeshes = sphereCenters.AsParallel().Select(sphereCenter =>
            {
                //System.Diagnostics.Debug.WriteLine($"Start creating SphereMesh3D with {sphereCenter} on {System.Threading.Thread.CurrentThread.ManagedThreadId}");

                var sphereMeshGeometry3D = new Ab3d.Meshes.SphereMesh3D(sphereCenter, radius: 15, segments: 30).Geometry;
                var sphereBooleanMesh3D  = new Ab3d.Meshes.BooleanMesh3D(sphereMeshGeometry3D);

                return(sphereBooleanMesh3D);
            });

            // After that we can subtract each of the sphere meshes from the initialBooleanMesh3D.
            // This can be done in background thread (we use Dispatcher.Invoke to "send" the mesh to the UI thread)
            Task.Factory.StartNew(() =>
            {
                // Note the order in which the sphereBooleanMesh are created is not determined (as the BooleanMesh3D are created, they get available to the foreach)
                foreach (var sphereBooleanMesh in sphereBooleanMeshes)
                {
                    // Subtract the sphereBooleanMesh from the initialBooleanMesh3D
                    initialBooleanMesh3D.Subtract(sphereBooleanMesh);

                    // Get the MeshGeometry3D
                    var mesh = initialBooleanMesh3D.Geometry;

                    // Freeze the MeshGeometry3D
                    // This will allow using the mesh in UI thread (different thread as the mesh was created)
                    mesh.Freeze();

                    // Send it to UI thread
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        ShowNewOriginalMesh(mesh);
                    }));
                }
            })
            // After all the boolean operations are completed, we generate texture coordinates
            // so we will be able to use texture image as material (meshes that are created with boolean operations do not have texture coordinates defined)
            .ContinueWith(_ => GenerateTextureCoordinates(), TaskScheduler.FromCurrentSynchronizationContext()); // The last one is run on UI thread
        }
        public ExtensionMethods()
        {
            InitializeComponent();


            var point1  = new Point3D(10, 10, 10);
            var vector1 = point1.ToVector3D();

            var vector2 = new Vector3D(10, 10, 10);
            var point2  = vector2.ToPoint3D();


            var sphereMesh    = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 10, 30).Geometry;
            var sphereModel3D = new GeometryModel3D(sphereMesh, new DiffuseMaterial(Brushes.Gold));

            var sphereModelVisual = sphereModel3D.CreateModelVisual3D();


            Point3D centerPosition = sphereModel3D.Bounds.GetCenterPosition();



            // Load sample model
            var readerObj        = new Ab3d.ReaderObj();
            var rootModel3DGroup = readerObj.ReadModel3D("pack://application:,,,/Ab3d.PowerToys.Samples;component/Resources/ObjFiles/robotarm.obj") as Model3DGroup;



            Viewport3D MainViewport3D = new Viewport3D();

            MainViewport3D.Name = "MainViewport3D";
            MainViewport3D.Children.Add(rootModel3DGroup.CreateModelVisual3D());


            var redDiffuseMaterial = new DiffuseMaterial(Brushes.Red);

            MainViewport3D.Children.ForEachGeometryModel3D((geometryModel3D) =>
            {
                // This code is called for every GeometryModel3D inside rootModel3DGroup
                geometryModel3D.Material = redDiffuseMaterial;
            });



            int totalPositions = 0;

            rootModel3DGroup.ForEachGeometryModel3D((geometryModel3D) =>
            {
                // This code is called for every GeometryModel3D inside rootModel3DGroup
                var meshGeometry3D = geometryModel3D.Geometry as MeshGeometry3D;
                if (meshGeometry3D != null && meshGeometry3D.Positions != null)
                {
                    totalPositions += meshGeometry3D.Positions.Count;
                }
            });


            MainViewport3D.Children.ForEachVisual3D((modelVisual3D) =>
            {
                // This code is called for every ModelVisual3D in MainViewport3D
                var sphereVisual3D = modelVisual3D as Ab3d.Visuals.SphereVisual3D;
                if (sphereVisual3D != null)
                {
                    sphereVisual3D.Radius *= 1.2;
                }
            });



            var allPositions = new List <Point3D>();

            Ab3d.Utilities.ModelIterator.IterateGeometryModel3DObjects(model3D : rootModel3DGroup,
                                                                       parentTransform3D : null,
                                                                       callback : delegate(GeometryModel3D geometryModel3D, Transform3D transform3D)
            {
                // This code is called for every GeometryModel3D inside rootModel3DGroup
                // transform3D is set to the Transform3D with all parent transformations or to null if there is no parent transformation
                var meshGeometry3D = geometryModel3D.Geometry as MeshGeometry3D;

                if (meshGeometry3D != null)
                {
                    var positions = meshGeometry3D.Positions;
                    if (positions != null)
                    {
                        int positionsCount = positions.Count;
                        for (var i = 0; i < positionsCount; i++)
                        {
                            Point3D onePosition = positions[i];

                            if (transform3D != null)
                            {
                                onePosition = transform3D.Transform(positions[i]);
                            }

                            allPositions.Add(onePosition);
                        }
                    }
                }
            });


            // MainViewport3D.DumpHierarchy()
            DumpHierarchyTextBlock.Text = Ab3d.Utilities.Dumper.GetObjectHierarchyString(MainViewport3D);



            // Most extension methods are meant to be used in Visual Studio Immediate Window
            // For example: geometryModel3D.Dump();
            // This writes detailed information about geometryModel3D into the Immediate Window (using Console.Write)
            // For this sample, we do not want to display info text into Colose.Write, but instead show the text in the UI
            // To do this we use GetDumpString and other methods that are also used by the Dump extension.

            // Same as: rootModel3DGroup.Dump()
            string model3DGroupDumpString = Ab3d.Utilities.Dumper.GetDumpString(rootModel3DGroup);

            var baseMotorGeometryModel3D = readerObj.NamedObjects["BaseMotor"] as GeometryModel3D;
            // Same as: geometryModel3D.Dump();
            string geometryModel3DDumpString = Ab3d.Utilities.Dumper.GetDumpString(baseMotorGeometryModel3D);

            // Same as geometryModel3D.Geometry.Dump(5, "0.0")
            // Max 6 lines of data
            // "0.0" is format string
            string geometryDumpString = Ab3d.Utilities.Dumper.GetDumpString(baseMotorGeometryModel3D.Geometry, 6, "0.0");


            // Create a custom specular material
            var materialGroup = new MaterialGroup();

            materialGroup.Children.Add(new DiffuseMaterial(Brushes.Gold));
            materialGroup.Children.Add(new SpecularMaterial(Brushes.White, 16));

            // Same as: materialGroup.Dump();
            string materialDump = Ab3d.Utilities.Dumper.GetDumpString(materialGroup);


            var axisAngleRotation3D = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 30);
            var rotateTransform3D   = new RotateTransform3D(axisAngleRotation3D, 100, 200, 300);

            var transform3DGroup = new Transform3DGroup();

            transform3DGroup.Children.Add(new ScaleTransform3D(1.0, 2.0, 0.5));
            transform3DGroup.Children.Add(rotateTransform3D);

            // Same as: transform3DGroup.Value.Dump();
            string matrixDump = Ab3d.Utilities.Dumper.GetMatrix3DText(transform3DGroup.Value);

            // Same as: transform3DGroup.Value.Dump(5);
            string matrix5Dump = Ab3d.Utilities.Dumper.GetMatrix3DText(transform3DGroup.Value, numberOfDecimals: 5);

            // Same as: transform3DGroup.Dump();
            string transformDump = Ab3d.Utilities.Dumper.GetTransformText(transform3DGroup);

            // Same as: rootModel3DGroup.Bounds.Dump();
            string boundDump = Ab3d.Utilities.Dumper.GetBoundsText(rootModel3DGroup.Bounds);

            string meshInitializationText = Ab3d.Utilities.Dumper.GetMeshInitializationCode(baseMotorGeometryModel3D.Geometry);


            ModelGroupDumpTextBlock.Text     = model3DGroupDumpString;
            GeometryModelDumpTextBlock.Text  = geometryModel3DDumpString;
            MeshGeometryDumpTextBlock.Text   = geometryDumpString;
            MaterialDumpTextBlock.Text       = materialDump;
            MatrixDumpTextBlock.Text         = matrixDump;
            MatrixDump5TextBlock.Text        = matrix5Dump;
            TransformDumpTextBlock.Text      = transformDump;
            BoundsTextBlock.Text             = boundDump;
            MeshInitializationTextBlock.Text = meshInitializationText;
        }
示例#9
0
        public BasicWpf3dObjectsTutorial()
        {
            InitializeComponent();

            var mesh2 = new MeshGeometry3D()
            {
                Positions = new Point3DCollection(new[]
                {
                    new Point3D(5, 0, 5),
                    new Point3D(100, 0, 5),
                    new Point3D(100, 0, 50),
                    new Point3D(5, 0, 50)
                }),

                TriangleIndices = new Int32Collection(new[]
                {
                    0, 2, 1,
                    3, 2, 0
                })
            };

            var geometryModel2 = new GeometryModel3D()
            {
                Geometry     = mesh2,
                Material     = new DiffuseMaterial(Brushes.LightGreen),
                BackMaterial = new DiffuseMaterial(Brushes.Red),
            };

            var modelVisual2 = new ModelVisual3D()
            {
                Content = geometryModel2
            };

            //// Using CreateModelVisual3D extensiton method from Ab3d.PowerToys
            //var modelVisual2 = geometryModel2.CreateModelVisual3D();
            //Viewport2.Children.Add(modelVisual2);

            //// in one line:
            //Viewport2.Children.Add(geometryModel2.CreateModelVisual3D());

            Viewport2.Children.Add(modelVisual2);

            MeshInspector2.MeshGeometry3D = mesh2;

            // #############

            var mesh3 = new MeshGeometry3D()
            {
                Positions       = mesh2.Positions,
                TriangleIndices = new Int32Collection(new[]
                {
                    2, 0, 1, // changed from 0, 2, 1
                    3, 2, 0
                }),
                Normals = new Vector3DCollection(new [] { new Vector3D(0, 1, 0), new Vector3D(0, 1, 0), new Vector3D(0, 1, 0), new Vector3D(0, 1, 0) }) // We define Normals because the automatically generated normals are not correct because the two triangles are oriented differently and this produces zero length normals for the shared positions; note that for mesh2 this was not needed because triangles are correctly oriented and WPF was able to correctly calculate normals.
            };

            _geometryModel3 = new GeometryModel3D()
            {
                Geometry     = mesh3,
                Material     = new DiffuseMaterial(Brushes.LightGreen),
                BackMaterial = new DiffuseMaterial(Brushes.Red),
            };

            var modelVisual3 = new ModelVisual3D()
            {
                Content = _geometryModel3
            };

            Viewport3.Children.Add(modelVisual3);

            MeshInspector3.MeshGeometry3D = mesh3;

            // #############

            var modelVisual4 = new ModelVisual3D()
            {
                Content = _geometryModel3
            };

            Viewport4.Children.Add(modelVisual4);

            MeshInspector4.MeshGeometry3D = mesh3;

            // #############

            var mesh5 = new MeshGeometry3D()
            {
                Positions          = mesh2.Positions,
                TriangleIndices    = mesh2.TriangleIndices,
                TextureCoordinates = new PointCollection(new[]
                {
                    new Point(0, 0),
                    new Point(1, 0),
                    new Point(1, 1),
                    new Point(0, 1),
                })
            };

            var textureImage = new BitmapImage(new Uri("pack://*****:*****@"c:\images\texture.png")); // Read image from file
            //var textureImage2 = new BitmapImage(new Uri("pack://*****:*****@"Resources\robotarm-upper-part.3ds");

            var assimpWpfImporter = new AssimpWpfImporter();
            var robotModel3D      = assimpWpfImporter.ReadModel3D(fileName);

            string dumpString = Ab3d.Utilities.Dumper.GetObjectHierarchyString(robotModel3D);

            RobotArmSampleTextBox.Text = dumpString;


            //var transform3DGroup = new Transform3DGroup();
            //transform3DGroup.Children.Add(new ScaleTransform3D(2, 3, 2));
            //transform3DGroup.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 45)));
            //transform3DGroup.Children.Add(new TranslateTransform3D(100, 0, 0));

            //geometryModel2.Transform = transform3DGroup;
        }
        private void CreateTestScene(Viewport3D wpfViewport3D)
        {
            var boxMaterial = new DiffuseMaterial(Brushes.Orange);

            var sphereMaterial = new MaterialGroup();

            sphereMaterial.Children.Add(new DiffuseMaterial(Brushes.SkyBlue));
            sphereMaterial.Children.Add(new SpecularMaterial(Brushes.White, 20));

            var lightMaterialGroup = new MaterialGroup();

            lightMaterialGroup.Children.Add(new DiffuseMaterial(Brushes.Black));
            lightMaterialGroup.Children.Add(new EmissiveMaterial(Brushes.Yellow));


            var objectsGroup = new Model3DGroup();

            //var boxMesh = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 0, 0), new Size3D(30, 30, 30), 1, 1, 1).Geometry;
            //var sphereMesh = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 15, 20, false).Geometry;


            //for (int y = 0; y < 5; y++)
            //{
            //    for (int x = -200; x <= 200; x += 100)
            //    {
            //        for (int z = -200; z <= 200; z += 100)
            //        {
            //            var geometryModel3D = new GeometryModel3D();

            //            if ((y % 2) == 0)
            //            {
            //                geometryModel3D.Geometry = boxMesh;
            //                geometryModel3D.Material = boxMaterial;
            //            }
            //            else
            //            {
            //                geometryModel3D.Geometry = sphereMesh;
            //                geometryModel3D.Material = sphereMaterial;
            //            }

            //            geometryModel3D.Transform = new TranslateTransform3D(x, y * 50, z);

            //            objectsGroup.Children.Add(geometryModel3D);
            //        }
            //    }
            //}

            var boxMesh    = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, 0, 0), new Size3D(1, 1, 1), 1, 1, 1).Geometry;
            var sphereMesh = new Ab3d.Meshes.SphereMesh3D(new Point3D(0, 0, 0), 10, 20, false).Geometry;

            double yPos      = 0;
            double firstSize = 50;

            for (int y = 0; y < 5; y++)
            {
                int    samples     = (y * 2) + 2;
                int    halfSamples = samples / 2;
                double size        = 400 / (samples * 1.5);

                if (y == 0)
                {
                    firstSize = size;
                }

                double xPos = -200;

                for (int x = -halfSamples; x <= halfSamples; x++)
                {
                    double zPos = -200;

                    for (int z = -halfSamples; z <= halfSamples; z++)
                    {
                        var geometryModel3D = new GeometryModel3D();

                        geometryModel3D.Geometry = boxMesh;
                        geometryModel3D.Material = boxMaterial;

                        var transform3DGroup = new Transform3DGroup();
                        transform3DGroup.Children.Add(new ScaleTransform3D(size, size, size));
                        transform3DGroup.Children.Add(new TranslateTransform3D(xPos, yPos, zPos));

                        geometryModel3D.Transform = transform3DGroup;

                        objectsGroup.Children.Add(geometryModel3D);


                        zPos += size * 1.5;
                    }

                    xPos += size * 1.5;
                }

                yPos += size + y * 7 + 10;
            }


            for (int a = 0; a < 360; a += 20)
            {
                double rad = MathUtil.DegreesToRadians(a);
                double x   = Math.Sin(rad) * 600;
                double z   = Math.Cos(rad) * 600;


                var geometryModel3D = new GeometryModel3D();

                geometryModel3D.Geometry = sphereMesh;
                geometryModel3D.Material = sphereMaterial;

                var transform3DGroup = new Transform3DGroup();
                transform3DGroup.Children.Add(new ScaleTransform3D(8, 8, 8));
                transform3DGroup.Children.Add(new TranslateTransform3D(x, 20, z));

                geometryModel3D.Transform = transform3DGroup;

                objectsGroup.Children.Add(geometryModel3D);
            }

            var bottomBoxModel3D = new GeometryModel3D();

            bottomBoxModel3D.Geometry = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, -(firstSize / 2), 0), new Size3D(2000, 10, 2000), 1, 1, 1).Geometry;
            //bottomBoxModel3D.Geometry = new Ab3d.Meshes.BoxMesh3D(new Point3D(0, -(firstSize / 2), 0), new Size3D(2000, 10, 2000), 50, 1, 50).Geometry; // To see at least some spot light on bottom box in WPF, create the box from 50 x 50 mesh (or more)
            bottomBoxModel3D.Material = new DiffuseMaterial(Brushes.Silver);

            objectsGroup.Children.Add(bottomBoxModel3D);

            var wireGrid = new Ab3d.Visuals.WireGridVisual3D();

            wireGrid.CenterPosition   = new Point3D(0, -(firstSize / 2) + 11, 0);
            wireGrid.Size             = new Size(1800, 1800);
            wireGrid.WidthCellsCount  = 9;
            wireGrid.HeightCellsCount = 9;
            wireGrid.LineColor        = Colors.SkyBlue;
            wireGrid.LineThickness    = 3;

            wpfViewport3D.Children.Add(wireGrid);



            var lightsGroup = new Model3DGroup();

            _spotLightModel          = new GeometryModel3D();
            _spotLightModel.Geometry = sphereMesh;
            _spotLightModel.Material = lightMaterialGroup;

            _spotLightTranslate       = new TranslateTransform3D(_wpfSpotLight.Position.X, _wpfSpotLight.Position.Y, _wpfSpotLight.Position.Z);
            _spotLightModel.Transform = _spotLightTranslate;

            lightsGroup.Children.Add(_spotLightModel);


            _pointLightModel          = new GeometryModel3D();
            _pointLightModel.Geometry = sphereMesh;
            _pointLightModel.Material = lightMaterialGroup;

            _pointLightTranslate       = new TranslateTransform3D(_wpfPointLight1.Position.X, _wpfPointLight1.Position.Y, _wpfPointLight1.Position.Z);
            _pointLightModel.Transform = _pointLightTranslate;

            lightsGroup.Children.Add(_pointLightModel);



            var modelVisual3D = new ModelVisual3D();

            modelVisual3D.Content = objectsGroup;

            wpfViewport3D.Children.Add(modelVisual3D);

            var lightsVisual3D = new ModelVisual3D();

            lightsVisual3D.Content = lightsGroup;

            wpfViewport3D.Children.Add(lightsVisual3D);
        }