コード例 #1
0
        public void CreateExtrudedModel()
        {
            Vector3D extrudeVector, shapeYVector3D;

            bool isSuccess = GetExtrudeAndUpVectors(out extrudeVector, out shapeYVector3D);

            if (!isSuccess)
            {
                return;
            }

            bool isSmooth = IsSmoothCheckBox.IsChecked ?? false;

            Vector3D modelOffset = new Vector3D(0, 0, 0);

            // Adjust shape positions so that (0,0) is at the center of the area
            // We also invert y so that y increased upwards (as the blue arrows shows) and not downwards as in Canvas coordinate system
            var centeredShapePositions = InvertYAndCenterPoints(_shapePositions);

            try
            {
                MeshGeometry3D extrudedMesh = Mesh3DFactory.CreateExtrudedMeshGeometry(positions: centeredShapePositions,
                                                                                       isSmooth: isSmooth,
                                                                                       modelOffset: modelOffset,
                                                                                       extrudeVector: extrudeVector,
                                                                                       shapeYVector: shapeYVector3D,
                                                                                       textureCoordinatesGenerationType: ExtrudeTextureCoordinatesGenerationType.Cylindrical,
                                                                                       addBottomTriangles: AddBottomTrianglesCheckBox.IsChecked ?? false,
                                                                                       addTopTriangles: AddTopTrianglesCheckBox.IsChecked ?? false);

                CreateGeometryModel(extrudedMesh);

                _isExtrudedModelShown = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error extruding shape:\r\n" + ex.Message);
            }
        }
        public ExtrudeAlongPathSample()
        {
            InitializeComponent();


            // First prepare two 2D shapes:
            var letterTShapePositions = new Point[]
            {
                new Point(5, 0),
                new Point(-5, 0),
                new Point(-5, 40),
                new Point(-20, 40),
                new Point(-20, 50),
                new Point(20, 50),
                new Point(20, 40),
                new Point(5, 40),
            };

            var ellipsePositionList = new List <Point>();

            for (int i = 0; i < 360; i += 20)
            {
                ellipsePositionList.Add(new Point(Math.Sin(i / 180.0 * Math.PI) * 20, Math.Cos(i / 180.0 * Math.PI) * 10));
            }


            // Now define a simple 3D path:
            var extrudePath = new Point3D[]
            {
                new Point3D(0, 0, 0),
                new Point3D(0, 90, 0),
                new Point3D(-20, 110, 0),
                new Point3D(-50, 130, 20),
                new Point3D(-50, 130, 100),
            };


            // Create extruded models:

            MeshGeometry3D extrudedMesh1 = Mesh3DFactory.CreateExtrudedMeshGeometry(
                shapePositions: ellipsePositionList,
                extrudePathPositions: extrudePath,
                shapeYVector3D: new Vector3D(0, 0, -1),
                isClosed: true,
                isSmooth: true);

            CreateGeometryModel(extrudedMesh1, offset: new Vector3D(-150, 0, -50), setBackMaterial: false);



            var extrudedMesh2 = Mesh3DFactory.CreateExtrudedMeshGeometry(
                shapePositions: ellipsePositionList,
                extrudePathPositions: extrudePath,
                shapeYVector3D: new Vector3D(0, 0, -1),
                isClosed: false,
                isSmooth: false);

            // Because this mesh will not be closed, we will be able to see inside - so set the back material to dim gray.
            CreateGeometryModel(extrudedMesh2, offset: new Vector3D(0, 0, -50), setBackMaterial: true);



            // Until now we only provided the shape positions to the CreateExtrudedMeshGeometry.
            // This method then triangulated the shape in case it was closed.
            // Here we manually triangulate the shape and provide the shapeTriangleIndices to CreateExtrudedMeshGeometry:
            var triangulator = new Ab3d.Utilities.Triangulator(letterTShapePositions);

            // NOTE: CreateTriangleIndices can throw FormatException when the positions are not correctly defined (for example if the lines intersect each other).
            List <int> triangleIndices = triangulator.CreateTriangleIndices();

            MeshGeometry3D extrudedMesh = Mesh3DFactory.CreateExtrudedMeshGeometry(
                shapePositions: letterTShapePositions,
                shapeTriangleIndices: triangleIndices,
                extrudePathPositions: extrudePath,
                shapeYVector3D: new Vector3D(0, 0, -1),
                isClosed: true,
                isSmooth: false,
                flipNormals: triangulator.IsClockwise); // If true than normals are flipped - used when positions are defined in a counter clockwise order

            CreateGeometryModel(extrudedMesh, offset: new Vector3D(150, 0, -50), setBackMaterial: false);
        }
        private void CreateScene()
        {
            // Create material with 10 x 10 numbers grid
            var imageBrush = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/Resources/10x10-texture.png")));

            var material = new DiffuseMaterial(imageBrush);
            //var material = new DiffuseMaterial(Brushes.Silver);


            // Create box with 10 x 10 x 10 cells
            var boxVisual3D = new BoxVisual3D()
            {
                UseCachedMeshGeometry3D = false, // This will generate a new MeshGeometry3D for this BoxVisual3D,
                FreezeMeshGeometry3D    = false, // This will allow us to change the TextureCoordinates
                CenterPosition          = new Point3D(-150, 0, -50),
                Size        = new Size3D(60, 60, 60),
                XCellsCount = 10,
                YCellsCount = 10,
                ZCellsCount = 10,
                Material    = material,
            };

            MainViewport.Children.Add(boxVisual3D);


            var cylinderVisual3D = new CylinderVisual3D()
            {
                BottomCenterPosition = new Point3D(-50, -30, -50),
                Radius   = 30,
                Height   = 60,
                Segments = 10,
                IsSmooth = false,
                Material = material
            };

            MainViewport.Children.Add(cylinderVisual3D);


            var sphereVisual3D = new SphereVisual3D()
            {
                UseCachedMeshGeometry3D = false, // This will generate a new MeshGeometry3D for this BoxVisual3D,
                FreezeMeshGeometry3D    = false, // This will allow us to change the TextureCoordinates
                CenterPosition          = new Point3D(50, 0, -50),
                Radius   = 30,
                Material = material
            };

            MainViewport.Children.Add(sphereVisual3D);


            // Add dragon model
            string fileName         = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\ObjFiles\\dragon_vrip_res3.obj");
            var    objModelVisual3D = new ObjModelVisual3D()
            {
                Source          = new Uri(fileName, UriKind.Absolute),
                SizeX           = 70,
                Position        = new Point3D(-150, 0, 100),
                PositionType    = ObjModelVisual3D.VisualPositionType.Center,
                DefaultMaterial = material,
            };

            Ab3d.Utilities.ModelUtils.ChangeMaterial(objModelVisual3D.Content, material, newBackMaterial: null);

            MainViewport.Children.Add(objModelVisual3D);


            // Add simple extruded shape
            var extrudePositions = new Point[]
            {
                new Point(-1, 54),
                new Point(13, 35),
                new Point(8, 32),
                new Point(18, 13),
                new Point(11, 9),
                new Point(23, -13),
                new Point(6, -14),
                new Point(6, -29),
                new Point(-8, -30),
                new Point(-7, -13),
                new Point(-25, -9),
                new Point(-12, 9),
                new Point(-25, 16),
                new Point(-8, 31),
                new Point(-16, 38)
            };

            var extrudedMesh = Mesh3DFactory.CreateExtrudedMeshGeometry(positions: extrudePositions.ToList(),
                                                                        isSmooth: false,
                                                                        modelOffset: new Vector3D(-50, -15, 100),
                                                                        extrudeVector: new Vector3D(0, 30, 0),
                                                                        shapeYVector: new Vector3D(0, 0, -1),
                                                                        textureCoordinatesGenerationType: ExtrudeTextureCoordinatesGenerationType.AddAdditionalPositions);

            var geometryModel3D = new GeometryModel3D(extrudedMesh, material);

            var modelVisual3D = new ModelVisual3D();

            modelVisual3D.Content = geometryModel3D;

            MainViewport.Children.Add(modelVisual3D);
        }