Example #1
0
        private Model3D GetBall()
        {
            GeometryModel3D retVal   = new GeometryModel3D();
            MaterialGroup   material = new MaterialGroup();
            DiffuseMaterial diffuse  = new DiffuseMaterial(new SolidColorBrush(this.EditorColors.DraggableModifier));

            this.MaterialBrushes.Add(new MaterialColorProps(diffuse, diffuse.Brush, this.EditorColors.DraggableModifier));
            material.Children.Add(diffuse);
            SpecularMaterial specular = new SpecularMaterial(new SolidColorBrush(this.EditorColors.DraggableModifier_SpecularColor), this.EditorColors.DraggableModifier_SpecularPower);

            this.MaterialBrushes.Add(new MaterialColorProps(specular));
            material.Children.Add(specular);

            retVal.Material     = material;
            retVal.BackMaterial = material;
            retVal.Geometry     = UtilityWPF.GetSphere_LatLon(20, .1);

            Transform3DGroup transform = new Transform3DGroup();

            transform.Children.Add(new TranslateTransform3D(_initialOffset * _radius));

            retVal.Transform = transform;

            return(retVal);
        }
Example #2
0
        public void SetGeometry(int index = -1)
        {
            if (!(DataContext is MeshRenderer vm))
            {
                return;
            }

            if (vm.Meshes.Any() && viewport.Children.Count == 2)
            {
                viewport.Children.RemoveAt(1);
            }

            var meshIndex  = 0;
            var modelGroup = new Model3DGroup();

            foreach (var mesh in vm.Meshes)
            {
                //Skip meshes that should not be displayed
                if (index != -1 && meshIndex != index)
                {
                    meshIndex++;
                    continue;
                }

                var mesh3D = new MeshGeometry3D()
                {
                    Positions          = mesh.Positions,
                    Normals            = mesh.Normals,
                    TriangleIndices    = mesh.Indices,
                    TextureCoordinates = mesh.UVs,
                };

                var diffuse  = new DiffuseMaterial(mesh.Diffuse);
                var specular = new SpecularMaterial(mesh.Specular, 50);
                var matGroup = new MaterialGroup();
                matGroup.Children.Add(diffuse);
                matGroup.Children.Add(specular);

                var model = new GeometryModel3D(mesh3D, matGroup);
                modelGroup.Children.Add(model);

                var binding = new Binding(nameof(mesh.Diffuse))
                {
                    Source = mesh
                };
                BindingOperations.SetBinding(diffuse, DiffuseMaterial.BrushProperty, binding);

                if (meshIndex == index)
                {
                    break;
                }
            }

            var visual = new ModelVisual3D()
            {
                Content = modelGroup
            };

            viewport.Children.Add(visual);
        }
Example #3
0
        //创建pcb板
        private void CreatePcbBoard()
        {
            GeometryModel3D geoModel3d = new GeometryModel3D();

            //创建pcb板子几何形状
            MeshGeometry3D mesh3d = new MeshGeometry3D();

            mesh3d.Positions       = CreatePcbBoardPoints3D(30, 20, 1);
            mesh3d.TriangleIndices = CreatePcbBoardPointsIndices();
            geoModel3d.Geometry    = mesh3d;

            //PCB表面材质
            MaterialGroup    frontMaterialGroup = new MaterialGroup();
            DiffuseMaterial  material1          = new DiffuseMaterial(Brushes.DarkGreen);
            SpecularMaterial material2          = new SpecularMaterial(Brushes.Green, 100);

            frontMaterialGroup.Children.Add(material1);
            frontMaterialGroup.Children.Add(material2);
            geoModel3d.Material = frontMaterialGroup;

            // //PCB背面材质
            DiffuseMaterial backMaterial = new DiffuseMaterial(Brushes.Gray);

            geoModel3d.BackMaterial = backMaterial;

            ModelUIElement3D pcbUIEle3d = new ModelUIElement3D();

            pcbUIEle3d.Model = geoModel3d;

            PCBContainer.Children.Add(pcbUIEle3d);
        }
Example #4
0
        private GeometryModel3D GetArrow(Quaternion rotation)
        {
            GeometryModel3D retVal   = new GeometryModel3D();
            MaterialGroup   material = new MaterialGroup();
            DiffuseMaterial diffuse  = new DiffuseMaterial(new SolidColorBrush(this.EditorColors.DraggableModifier));

            this.MaterialBrushes.Add(new MaterialColorProps(diffuse, diffuse.Brush, this.EditorColors.DraggableModifier));
            material.Children.Add(diffuse);
            SpecularMaterial specular = new SpecularMaterial(new SolidColorBrush(this.EditorColors.DraggableModifier_SpecularColor), this.EditorColors.DraggableModifier_SpecularPower);

            this.MaterialBrushes.Add(new MaterialColorProps(specular));
            material.Children.Add(specular);

            retVal.Material     = material;
            retVal.BackMaterial = material;

            List <TubeRingBase> rings = new List <TubeRingBase>();

            rings.Add(new TubeRingDome(0, false, 10));
            rings.Add(new TubeRingRegularPolygon(.025, false, .05, .05, false));
            rings.Add(new TubeRingRegularPolygon(.3, false, .05, .05, false));
            rings.Add(new TubeRingRegularPolygon(-.0375, false, .125, .125, false));
            rings.Add(new TubeRingPoint(.2, false));

            RotateTransform3D transform = new RotateTransform3D(new QuaternionRotation3D(rotation));

            retVal.Geometry = UtilityWPF.GetMultiRingedTube(35, rings, true, true, transform);

            return(retVal);
        }
Example #5
0
        private static Material MaterialFromColour(XbimColour colour)
        {
            var   col   = Color.FromScRgb(colour.Alpha, colour.Red, colour.Green, colour.Blue);
            Brush brush = new SolidColorBrush(col);

            // build material
            Material mat;

            if (colour.SpecularFactor > 0)
            {
                mat = new SpecularMaterial(brush, colour.SpecularFactor * 100);
            }
            else if (colour.ReflectionFactor > 0)
            {
                mat = new  EmissiveMaterial(brush);
            }
            else
            {
                mat = new DiffuseMaterial(brush);
            }

            // freeze and return
            mat.Freeze();
            return(mat);
        }
        public void AddBox(double side, Vector3D center)
        {
            const double specularCoefficient = 1;

            MeshGeometry3D mesh = new MeshGeometry3D();

            for (int ix = -1; ix <= 1; ix += 2)
            {
                for (int iy = -1; iy <= 1; iy += 2)
                {
                    for (int iz = -1; iz <= 1; iz += 2)
                    {
                        mesh.Positions.Add(new Point3D(center.X + ix * side / 2, center.Y + iy * side / 2, center.Z + iz * side / 2));
                    }
                }
            }

            mesh.TriangleIndices = new Int32Collection()
            {
                0, 1, 2, 1, 3, 2, // low yz
                0, 2, 4, 2, 6, 4, // low xy
                0, 4, 1, 1, 4, 5, // low xz
                4, 6, 5, 5, 6, 7, // high yz
                1, 5, 3, 3, 5, 7, // high xy
                3, 6, 2, 3, 7, 6  // high xz
            };

            var             brush    = new SolidColorBrush(Colors.LightSlateGray);
            var             material = new SpecularMaterial(brush, specularCoefficient);
            GeometryModel3D geo      = new GeometryModel3D(mesh, material);

            collection.Add(geo);
        }
        /// <summary>
        /// Draw a single triangle from a set of 3 points ..
        /// </summary>
        /// <param name="pt1"></param>
        /// <param name="pt2"></param>
        /// <param name="pt3"></param>
        private GeometryModel3D DrawSingleTriangle(Point3D pt1, Point3D pt2, Point3D pt3)
        {
            MeshGeometry3D triangleMesh = new MeshGeometry3D();

            triangleMesh.Positions.Add(pt1);
            triangleMesh.Positions.Add(pt2);
            triangleMesh.Positions.Add(pt3);

            triangleMesh.TriangleIndices.Add(0);
            triangleMesh.TriangleIndices.Add(1);
            triangleMesh.TriangleIndices.Add(2);

            Vector3D normalVectorToTrianglePlane = Helper.CalculateNormal(pt1, pt2, pt3);

            triangleMesh.Normals.Add(normalVectorToTrianglePlane);

            Material      materialDiffuse  = new DiffuseMaterial(new SolidColorBrush(Colors.Green));
            Material      materialSpecular = new SpecularMaterial(new SolidColorBrush(Colors.White), 30);
            MaterialGroup materialGroup    = new MaterialGroup();

            materialGroup.Children.Add(materialDiffuse);
            materialGroup.Children.Add(materialSpecular);

            return(new GeometryModel3D(triangleMesh, materialGroup));
        }
Example #8
0
        private void SerializeGeneratedWorkaround(Material material, string masterFileName, List <string> supportFiles)
        {
            // This is method is needed because of this issue:
            // (



            if (material is MaterialGroup)
            {
                foreach (Material m in ((MaterialGroup)material).Children)
                {
                    SerializeGeneratedWorkaround(m, masterFileName, supportFiles);
                }
            }
            else
            {
                Brush brush = null;
                if (material is DiffuseMaterial)
                {
                    DiffuseMaterial dm = (DiffuseMaterial)material;
                    brush = dm.Brush;
                }
                else if (material is EmissiveMaterial)
                {
                    EmissiveMaterial em = (EmissiveMaterial)material;
                    brush = em.Brush;
                }
                else if (material is SpecularMaterial)
                {
                    SpecularMaterial sm = (SpecularMaterial)material;
                    brush = sm.Brush;
                }

                if (brush is ImageBrush)
                {
                    ImageBrush ib = (ImageBrush)brush;

                    // NOTE: Other forms of memory-generated bitmaps may need the same treatment.
                    if (ib.ImageSource is CachedBitmap)
                    {
                        CachedBitmap cb = (CachedBitmap)ib.ImageSource;
                        // Generate new file Name
                        int    fileIndex = supportFiles.Count;
                        string fileName  = masterFileName.Replace(".xaml", "_support_")
                                           + fileIndex.ToString() + ".png";
                        // Save as an image file (PNG to keep transparency)

                        PhotoConverter.SaveImageAs(cb, fileName);

                        // Remember the name
                        supportFiles.Add(fileName);
                        // Now replace this in the old brush
                        ib.ImageSource  = new BitmapImage(new Uri(fileName, UriKind.RelativeOrAbsolute));
                        ib.ViewboxUnits = BrushMappingMode.RelativeToBoundingBox;
                    }
                }
                // The default case is for this to be a NO-OP
            }
        }
Example #9
0
        /// <summary>
        /// Builds the 3D model for the cartoon view a the given residue.
        /// </summary>
        /// <param name="residue">A residue.</param>
        /// <param name="initialColor">The residue's current color.</param>
        internal Cartoon(Residue residue, Color initialColor)
        {
            this.residue = residue;

            this.materialGroup = new MaterialGroup();

            this.diffuseMaterial = new DiffuseMaterial(new SolidColorBrush(initialColor));
            this.materialGroup.Children.Add(diffuseMaterial);

            SpecularMaterial specularMaterial = new SpecularMaterial();

            specularMaterial.Brush         = new SolidColorBrush(Color.FromArgb(192, 255, 255, 255));
            specularMaterial.SpecularPower = 50;
            this.materialGroup.Children.Add(specularMaterial);

            this.model = new Model3DGroup();

            this.residue.Ribbon.GetResidueSpline(this.residue, out this.ribbonPoints,
                                                 out this.torsionVectors, out this.normalVectors);

            if (this.residue.IsHelix)
            {
                this.AddTube(Cartoon.helixWidth, Cartoon.helixHeight);

                if (this.residue.IsStructureStart)
                {
                    this.AddTubeCap(Cartoon.helixWidth, Cartoon.helixHeight);
                }

                if (this.residue.IsStructureEnd)
                {
                    this.AddTubeCap(Cartoon.helixWidth, Cartoon.helixHeight);
                }
            }
            else if (this.residue.IsSheet)
            {
                this.AddSheet();

                if (this.residue.IsStructureStart || this.residue.IsStructureEnd)
                {
                    this.AddSheetCap();
                }
            }
            else
            {
                this.AddTube(Cartoon.turnWidth, Cartoon.turnWidth);

                if (this.residue.IsStructureStart)
                {
                    this.AddTubeCap(Cartoon.turnWidth, Cartoon.turnWidth);
                }

                if (this.residue.IsStructureEnd)
                {
                    this.AddTubeCap(Cartoon.turnWidth, Cartoon.turnWidth);
                }
            }
        }
Example #10
0
        private void Prepare3DModel()
        {
            ObjReader helixObjReader = new ObjReader();
            string    path           = @"texture.png";
            var       point          = helixObjReader.Read(@"mi24.obj");

            //var importer = new HelixToolkit.Wpf.ModelImporter();
            //var point = importer.Load("mi24.obj");

            //GeometryModel3D model = point.Children[0] as GeometryModel3D;


            //Material material = MaterialHelper.CreateImageMaterial(path, 1);

            ImageBrush colors_brush = new ImageBrush();

            colors_brush.ImageSource =
                new BitmapImage(new Uri("texture.png", UriKind.Relative));
            DiffuseMaterial colors_material =
                new DiffuseMaterial(colors_brush);
            GeometryModel3D model;

            DiffuseMaterial diffuseMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Black));

            SpecularMaterial specularMaterial = new SpecularMaterial(new SolidColorBrush(Colors.Chocolate), 2.55);

            foreach (var pointChild in point.Children)
            {
                model = pointChild as GeometryModel3D;

                model.Material = specularMaterial;
                // model.Material = diffuseMaterial;
            }

            //DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Colors.Black));

            //SpecularMaterial specularMaterial = new SpecularMaterial(new SolidColorBrush(Colors.Chocolate), 2.55);

            //model.Material = material;
            //model.Material = specularMaterial;

            modelVisual3D.Content = point;



            //model.Content = modelAircraft3D;
            //model.Children.Add(new DefaultLights());
            //GeometryModel3D model1 = modelAircraft3D.Children[0] as GeometryModel3D;
            //DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Colors.Black));
            //model1.Material = material;
            //modelAircraft3D.Children[0] = model1;
            //model.Content = modelAircraft3D;
            modelVisual3D.Children.Add(new DefaultLights());
            myView.Camera.UpDirection   = new Vector3D(0, 1, 0);
            myView.Camera.LookDirection = new Vector3D(0, 0, 100);
            myView.Camera.Position      = new Point3D(0, 0, -25);
        }
        private static Material GetRoadMaterial(IFeature feature)
        {
            var brush     = new SolidColorBrush(Colors.Orange);
            var material1 = new DiffuseMaterial(brush);
            var material2 = new SpecularMaterial(brush, 90);
            var material  = new MaterialGroup();

            material.Children.Add(material1);
            material.Children.Add(material2);
            return(material);
        }
Example #12
0
        void ApplyColor(GeometryModel3D model, Brush frontBrush, Brush backBrush)
        {
            MaterialGroup    group    = new MaterialGroup();
            DiffuseMaterial  material = new DiffuseMaterial(frontBrush);
            SpecularMaterial specular = new SpecularMaterial(backBrush, 1000);

            group.Children.Add(material);
            group.Children.Add(specular);

            model.Material     = group;
            model.BackMaterial = group;
        }
Example #13
0
        public MaterialGroup GetSurfaceMaterial(Color colour)
        {
            var materialGroup = new MaterialGroup();
            var emmMat        = new EmissiveMaterial(new SolidColorBrush(colour));

            materialGroup.Children.Add(emmMat);
            materialGroup.Children.Add(new DiffuseMaterial(new SolidColorBrush(colour)));
            var specMat = new SpecularMaterial(new SolidColorBrush(Colors.White), 30);

            materialGroup.Children.Add(specMat);
            return(materialGroup);
        }
Example #14
0
        private Model3D CreateGeometry(bool isFinal)
        {
            Model3DGroup retVal = new Model3DGroup();

            #region box

            MaterialGroup material = new MaterialGroup();

            Color           ringColor = WorldColors.DirectionControllerRing_Color;
            DiffuseMaterial diffuse   = new DiffuseMaterial(new SolidColorBrush(ringColor));
            MaterialBrushes.Add(new MaterialColorProps(diffuse, ringColor));
            material.Children.Add(diffuse);

            SpecularMaterial specular = WorldColors.DirectionControllerRing_Specular;
            MaterialBrushes.Add(new MaterialColorProps(specular));
            material.Children.Add(specular);

            if (!isFinal)
            {
                EmissiveMaterial selectionEmissive = new EmissiveMaterial(Brushes.Transparent);
                material.Children.Add(selectionEmissive);
                SelectionEmissives.Add(selectionEmissive);
            }

            GeometryModel3D geometry = new GeometryModel3D
            {
                Material     = material,
                BackMaterial = material,

                Geometry = ConverterMatterToFuelDesign.GetMesh(.36d * SCALE, .04d * SCALE, 3),
            };

            retVal.Children.Add(geometry);

            #endregion

            #region brain

            ScaleTransform3D scaleTransform = new ScaleTransform3D(SCALE, SCALE, SCALE);

            if (!isFinal)
            {
                retVal.Children.AddRange(BrainDesign.CreateInsideVisuals(SCALE * .6, MaterialBrushes, SelectionEmissives, scaleTransform));
            }

            retVal.Children.Add(BrainDesign.CreateShellVisual(isFinal, MaterialBrushes, SelectionEmissives, scaleTransform));

            #endregion

            retVal.Transform = GetTransformForGeometry(isFinal);

            return(retVal);
        }
Example #15
0
        private static bool CompareMaterials(Material firstMaterial, Material secondMaterial)
        {
            if (firstMaterial.GetType() != secondMaterial.GetType())
            {
                return(false);
            }
            MaterialGroup materialGroup1 = firstMaterial as MaterialGroup;

            if (materialGroup1 != null)
            {
                MaterialGroup materialGroup2 = (MaterialGroup)secondMaterial;
                if (materialGroup1.Children.Count != materialGroup2.Children.Count)
                {
                    return(false);
                }
                for (int index = 0; index < materialGroup1.Children.Count; ++index)
                {
                    if (!PropertyUtilities.CompareMaterials(materialGroup1.Children[index], materialGroup2.Children[index]))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            DiffuseMaterial diffuseMaterial1 = firstMaterial as DiffuseMaterial;

            if (diffuseMaterial1 != null)
            {
                DiffuseMaterial diffuseMaterial2 = (DiffuseMaterial)secondMaterial;
                return(PropertyUtilities.CompareBrushes(diffuseMaterial1.Brush, diffuseMaterial2.Brush));
            }
            EmissiveMaterial emissiveMaterial1 = firstMaterial as EmissiveMaterial;

            if (emissiveMaterial1 != null)
            {
                EmissiveMaterial emissiveMaterial2 = (EmissiveMaterial)secondMaterial;
                return(PropertyUtilities.CompareBrushes(emissiveMaterial1.Brush, emissiveMaterial2.Brush));
            }
            SpecularMaterial specularMaterial1 = firstMaterial as SpecularMaterial;

            if (specularMaterial1 == null)
            {
                return(object.Equals((object)firstMaterial, (object)secondMaterial));
            }
            SpecularMaterial specularMaterial2 = (SpecularMaterial)secondMaterial;

            if (specularMaterial1.SpecularPower == specularMaterial2.SpecularPower)
            {
                return(PropertyUtilities.CompareBrushes(specularMaterial1.Brush, specularMaterial2.Brush));
            }
            return(false);
        }
Example #16
0
        private GeometryModel3D CreateGeometry(bool isFinal)
        {
            MaterialGroup   material = new MaterialGroup();
            DiffuseMaterial diffuse  = new DiffuseMaterial(new SolidColorBrush(WorldColors.EnergyTank_Color));

            this.MaterialBrushes.Add(new MaterialColorProps(diffuse, WorldColors.EnergyTank_Color));
            material.Children.Add(diffuse);
            SpecularMaterial specular = WorldColors.EnergyTank_Specular;

            this.MaterialBrushes.Add(new MaterialColorProps(specular));
            material.Children.Add(specular);
            EmissiveMaterial emissive = WorldColors.EnergyTank_Emissive;

            this.MaterialBrushes.Add(new MaterialColorProps(emissive));
            material.Children.Add(emissive);

            if (!isFinal)
            {
                EmissiveMaterial selectionEmissive = new EmissiveMaterial(Brushes.Transparent);
                material.Children.Add(selectionEmissive);
                base.SelectionEmissives.Add(selectionEmissive);
            }

            GeometryModel3D retVal = new GeometryModel3D();

            retVal.Material     = material;
            retVal.BackMaterial = material;

            int domeSegments     = isFinal ? 2 : 10;
            int cylinderSegments = isFinal ? 6 : 35;

            List <TubeRingBase> rings = new List <TubeRingBase>();

            rings.Add(new TubeRingDome(0, false, domeSegments));
            rings.Add(new TubeRingRegularPolygon(.4, false, 1, 1, false));
            rings.Add(new TubeRingRegularPolygon(2.5, false, 1, 1, false));
            rings.Add(new TubeRingDome(.4, false, domeSegments));

            // Scale this so the height is 1
            double scale = 1d / rings.Sum(o => o.DistFromPrevRing);

            Transform3DGroup transformInitial = new Transform3DGroup();

            transformInitial.Children.Add(new ScaleTransform3D(scale, scale, scale));
            retVal.Geometry = UtilityWPF.GetMultiRingedTube(cylinderSegments, rings, true, true, transformInitial);

            // Transform
            retVal.Transform = GetTransformForGeometry(isFinal);

            // Exit Function
            return(retVal);
        }
Example #17
0
        ///<summary></summary>
        public void SetSpecular(string objName, double specularPower)
        {
            if (!objectNames.Contains(objName))
            {
                return;
            }
            Color            colorw        = Colors.White;                                //Color.FromArgb(color.A,color.R,color.G,color.B);
            MaterialGroup    materialGroup = (MaterialGroup)((GeometryModel3D)_model3Dgroup.Children[objectNames.IndexOf(objName)]).Material;
            SpecularMaterial material      = (SpecularMaterial)materialGroup.Children[1]; //by convention, our specular material is at 1

            material.Brush         = new SolidColorBrush(colorw);
            material.SpecularPower = specularPower;
        }
Example #18
0
        public Window1()
        {
            InitializeComponent();
            basePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\3D_Models\\";

            /** Debug sphere to check in which point the joint is rotating**/
            var builder  = new MeshBuilder(true, true);
            var position = new Point3D(0, 0, 0);

            builder.AddSphere(position, 5, 15, 15);
            visualGrid     = new GeometryModel3D(builder.ToMesh(), Materials.Brown);
            mvGrid         = new ModelVisual3D();
            mvGrid.Content = visualGrid;

            //add board
            ModelImporter import = new ModelImporter();
            var           link   = import.Load(basePath + MODEL_PATH21);

            mvBoard           = new ModelVisual3D();
            mvBoard.Content   = link;
            mvBoard.Transform = new TranslateTransform3D(basePoint);

            var              materialGroup = new MaterialGroup();
            Color            mainColor     = Colors.White;
            EmissiveMaterial emissMat      = new EmissiveMaterial(new SolidColorBrush(mainColor));
            DiffuseMaterial  diffMat       = new DiffuseMaterial(new SolidColorBrush(mainColor));
            SpecularMaterial specMat       = new SpecularMaterial(new SolidColorBrush(mainColor), 200);

            materialGroup.Children.Add(emissMat);
            materialGroup.Children.Add(diffMat);
            materialGroup.Children.Add(specMat);

            GeometryModel3D model = link.Children[0] as GeometryModel3D;

            model.Material     = materialGroup;
            model.BackMaterial = materialGroup;

            //
            viewPort3d.RotateGesture = new MouseGesture(MouseAction.RightClick);
            viewPort3d.PanGesture    = new MouseGesture(MouseAction.LeftClick);
            viewPort3d.Children.Add(mvGrid);

            viewPort3d.Children.Add(mvBoard);



            viewPort3d.Camera.LookDirection = new Vector3D(20, -52, -29);
            viewPort3d.Camera.UpDirection   = new Vector3D(-0.145, 0.372, 0.917);
            viewPort3d.Camera.Position      = new Point3D(-15, 48, 37);
        }
        private static Material GetBlockMaterial(IFeature feature)
        {
            var brush = new SolidColorBrush(Colors.White)
            {
                Opacity = 0.8
            };
            var material1 = new DiffuseMaterial(brush);
            var material2 = new SpecularMaterial(brush, 90);
            var material  = new MaterialGroup();

            material.Children.Add(material1);
            material.Children.Add(material2);
            return(material);
        }
Example #20
0
        private void CreateWorld()
        {
            ModelVisual3D objectMv3D   = new ModelVisual3D();
            ModelVisual3D lightingMv3D = new ModelVisual3D();

            // Declare scene object
            PerspectiveCamera mainCam = new PerspectiveCamera();

            mainCam.Position      = new Point3D(0, 5, 5);
            mainCam.LookDirection = new Vector3D(0, -5, -5);
            mainCam.FieldOfView   = 75;
            vp3D.Camera           = mainCam;

            DirectionalLight directionalLight = new DirectionalLight();

            directionalLight.Direction = new Vector3D(-1, -1, -0.5);
            directionalLight.Color     = Colors.GhostWhite;

            lightingGroup.Children.Add(directionalLight);
            lightingMv3D.Content = lightingGroup;

            DiffuseMaterial  diffuseMaterial  = new DiffuseMaterial();
            SpecularMaterial specularMaterial = new SpecularMaterial();

            diffuseMaterial.Brush = Brushes.Chocolate;
            //diffuseMaterial.Color = Colors.WhiteSmoke;
            specularMaterial.Brush = Brushes.Aquamarine;

            MeshGeometry3D newSphere = new MeshGeometry3D();

            ObjLoader("sphere.obj", newSphere);

            GeometryModel3D newMesh = new GeometryModel3D();

            newMesh.Material = diffuseMaterial;
            newMesh.Geometry = newSphere;

            newMesh.BackMaterial = specularMaterial;

            Transform3D sphereTransform = new TranslateTransform3D(new Vector3D(-2, -2, 0));

            newMesh.Transform = sphereTransform;

            objectGroup.Children.Add(newMesh);

            objectMv3D.Content = objectGroup;

            vp3D.Children.Add(objectMv3D);
            vp3D.Children.Add(lightingMv3D);
        }
Example #21
0
        public static Material Simple(Color color, double opacity, double specularPower = 85)
        {
            var brush = new SolidColorBrush(color)
            {
                Opacity = opacity
            };
            var material1 = new DiffuseMaterial(brush);
            var material2 = new SpecularMaterial(brush, specularPower);
            var material  = new MaterialGroup();

            material.Children.Add(material1);
            material.Children.Add(material2);
            return(material);
        }
Example #22
0
        private void ExportMaterial(string matName, Material material, Material backMaterial)
        {
            SolidColorBrush brush;

            this.mwriter.WriteLine(string.Format("newmtl {0}", matName));
            DiffuseMaterial  material2 = material as DiffuseMaterial;
            SpecularMaterial material3 = material as SpecularMaterial;
            MaterialGroup    group     = material as MaterialGroup;

            if (group != null)
            {
                foreach (Material material4 in group.Children)
                {
                    if (material4 is DiffuseMaterial)
                    {
                        material2 = material4 as DiffuseMaterial;
                    }
                    if (material4 is SpecularMaterial)
                    {
                        material3 = material4 as SpecularMaterial;
                    }
                }
            }
            if (material2 != null)
            {
                this.mwriter.WriteLine(string.Format("Ka {0}", this.ToColorString(material2.AmbientColor)));
                this.mwriter.WriteLine(string.Format("Kd {0}", this.ToColorString(material2.Color)));
                this.mwriter.WriteLine(string.Format(CultureInfo.InvariantCulture, "d {0:F4}", new object[] { material2.Brush.Opacity }));
                brush = material2.Brush as SolidColorBrush;
                if (brush == null)
                {
                    string     str    = matName + ".png";
                    string     path   = Path.Combine(this.directory, str);
                    ImageBrush brush2 = (ImageBrush)material2.Brush;
                    Exporter.RenderBrush(path, material2.Brush, (int)brush2.ImageSource.Width, (int)brush2.ImageSource.Height);
                    this.mwriter.WriteLine(string.Format("map_Kd {0}", str));
                    this.mwriter.WriteLine(string.Format("map_d {0}", str));
                }
            }
            if (material3 != null)
            {
                brush = material3.Brush as SolidColorBrush;
                if (brush != null)
                {
                    this.mwriter.WriteLine(string.Format("Ks {0}", this.ToColorString(brush.Color)));
                }
                this.mwriter.WriteLine(string.Format(CultureInfo.InvariantCulture, "Ns {0:F4}", new object[] { material3.SpecularPower }));
            }
        }
Example #23
0
        private unsafe Material CreateVisibleSpectrumMaterial()
        {
            WriteableBitmap writeableBitmap = new WriteableBitmap(this.colorCount, 1, 96.0, 96.0, PixelFormats.Bgra32, null);

            writeableBitmap.Lock();
            unsafe
            {
                // Get a pointer to the back buffer.
                byte *pStart = (byte *)(void *)writeableBitmap.BackBuffer;
                //int nL = writeableBitmap.BackBufferStride;
                double step = (waveLengRange.Max - waveLengRange.Min) / (double)colorCount;

                for (int ix = 0; ix < colorCount; ix += 1)
                {
                    Color color = ColorHelper.WavelengthToColor(waveLengRange.Min + Convert.ToInt32(ix * step));

                    *(pStart + ix * 4 + 0) = (byte)(color.B);
                    *(pStart + ix * 4 + 1) = (byte)(color.G);
                    *(pStart + ix * 4 + 2) = (byte)(color.R);
                    *(pStart + ix * 4 + 3) = (byte)(color.A);
                }
            }

            writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, colorCount, 1));

            // Release the back buffer and make it available for display.
            writeableBitmap.Unlock();

            MaterialGroup materials = new MaterialGroup();

            ImageBrush imageBrush = new ImageBrush(writeableBitmap);

            imageBrush.ViewportUnits = BrushMappingMode.Absolute;

            var material = new DiffuseMaterial();

            material.Brush = imageBrush;
            //material.Brush = Brushes.Blue;
            materials.Children.Add(material);

            var spectMaterial = new SpecularMaterial(
                imageBrush, 90);

            //materials.Children.Add(spectMaterial);


            bitMap = writeableBitmap;
            return(materials);// material;
        }
Example #24
0
        /*
         * 设置物体的材质
         * 这里使用的是 自发光-渐变色画刷
         * 返回值:材质
         */
        public MaterialGroup GetMaterial(int Kindmaterial = 0)
        {
            MaterialGroup materialGroup = new MaterialGroup();
            Material      myMaterial;

            //myMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.LightSkyBlue));//自发光-传统画刷
            //myMaterial = new DiffuseMaterial(new RadialGradientBrush(Colors.LightSkyBlue, Colors.Blue));//自发光-渐变色画刷
            myMaterial = new SpecularMaterial(new SolidColorBrush(Colors.White), 10);
            materialGroup.Children.Add(myMaterial);
            //myMaterial = new EmissiveMaterial(new SolidColorBrush(Colors.LightGray));
            myMaterial = new DiffuseMaterial(new LinearGradientBrush(Colors.Blue, Colors.Gray, 10));//自发光-渐变色画刷
            materialGroup.Children.Add(myMaterial);

            return(materialGroup);
        }
Example #25
0
            public MaterialColorProps(SpecularMaterial specular)
            {
                this.Specular = specular;

                SolidColorBrush brush = specular.Brush as SolidColorBrush;

                if (brush == null)
                {
                    throw new ApplicationException("The specular was expected to be set up with a solid color brush.  Expand this method");
                }

                this.OrigBrush    = brush;
                this.OrigColor    = brush.Color;
                this.OrigSpecular = specular.SpecularPower;
            }
Example #26
0
        private void Add_Sphere_Click(object sender, RoutedEventArgs e)
        {
            MeshGeometry3D   newSphere        = new MeshGeometry3D();
            GeometryModel3D  newMesh          = new GeometryModel3D();
            DiffuseMaterial  diffuseMaterial  = new DiffuseMaterial();
            SpecularMaterial specularMaterial = new SpecularMaterial();

            diffuseMaterial.Brush = Brushes.GhostWhite;

            int count = 0;

            foreach (var sphere in objectGroup.Children)
            {
                if (sphere != null && ((GeometryModel3D)sphere).BackMaterial == null)
                {
                    count++;
                }
            }

            if (count == objectGroup.Children.Count)
            {
                specularMaterial.Brush = Brushes.Aquamarine;
            }

            ObjLoader("sphere.obj", newSphere);

            newMesh.Material = diffuseMaterial;

            if (specularMaterial.Brush != null)
            {
                newMesh.BackMaterial = specularMaterial;
            }

            newMesh.Geometry = newSphere;

            objectGroup.Children.Add(newMesh);

            framesCollection.childFrameList.Add(new FramesList());

            scaleArray.Add(1.0);

            listBoxSize++;
            ClearFrameList(listBoxSize);

            UpdateList();
        }
Example #27
0
        private Model3DGroup CreateTriangleModel(Point3D p0, Point3D p1, Point3D p2)
        {
            MeshGeometry3D mesh = new MeshGeometry3D();

            mesh.Positions.Add(p0);
            mesh.Positions.Add(p1);
            mesh.Positions.Add(p2);
            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);
            Vector3D normal = CalculateNormal(p0, p1, p2);

            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);
            mesh.Normals.Add(normal);

            Material material;

            if (!IsBlackKey)
            {
                material = new DiffuseMaterial(
                    new SolidColorBrush(Colors.White));
            }
            else
            {
                material = new DiffuseMaterial(
                    new SolidColorBrush(Colors.Black));
            }
            Material      specMat = new SpecularMaterial(Brushes.Red, 100.0);
            MaterialGroup matGrp  = new MaterialGroup();

            //matGrp.Children.Add(specMat);
            //matGrp.Children.Add(material);

            //

            GeometryModel3D model = new GeometryModel3D(
                mesh, material);



            Model3DGroup group = new Model3DGroup();

            group.Children.Add(model);
            return(group);
        }
        // MTL explanation: http://www.kixor.net/dev/objloader/
        protected Material MakeMaterial(LoadResult objmesh, ZipArchiveEntry texture)
        {
            MaterialGroup matGroup = new MaterialGroup();

            ObjLoader.Loader.Data.Material material = objmesh.Materials [0];

            Stream imgStream = texture.Open();

            Byte [] buffer = new Byte [texture.Length];
            imgStream.Read(buffer, 0, buffer.Length);
            var byteStream = new System.IO.MemoryStream(buffer);

            //ImageBrush imgBrush =new ImageBrush (new BitmapImage (new Uri (@"C:\Users\cyrille\Documents\Visual Studio 2012\Projects\tex_0.jpg"))) ;
            BitmapImage bitmap = new BitmapImage();

            bitmap.BeginInit();
            bitmap.CacheOption  = BitmapCacheOption.OnLoad;
            bitmap.StreamSource = byteStream;
            bitmap.EndInit();
            ImageBrush imgBrush = new ImageBrush(bitmap);

            imgBrush.ViewportUnits = BrushMappingMode.Absolute;
            //imgBrush.ViewportUnits =BrushMappingMode.RelativeToBoundingBox ;

            //Brush brush =new SolidColorBrush (Color.FromScRgb (material.Transparency, material.DiffuseColor.X, material.DiffuseColor.Y, material.DiffuseColor.Z)) ;
            //brush.Opacity =material.Transparency ;

            DiffuseMaterial diffuse = new DiffuseMaterial(imgBrush);

            diffuse.AmbientColor = Color.FromScRgb(material.Transparency, material.AmbientColor.X, material.AmbientColor.Y, material.AmbientColor.Z);
            // no more attributes
            matGroup.Children.Add(diffuse);

            SpecularMaterial specular = new SpecularMaterial(new SolidColorBrush(Color.FromScRgb(material.Transparency, material.SpecularColor.X, material.SpecularColor.Y, material.SpecularColor.Z)), material.SpecularCoefficient);

            // no more attributes
            matGroup.Children.Add(specular);

            // Default to Blue
            if (matGroup.Children.Count == 0)
            {
                matGroup.Children.Add(new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(0, 0, 255))));
            }
            return(matGroup);
        }
Example #29
0
        private Material CreateVisibleSpectrumMaterial(Point3D[,] points)
        {
            int xWidth = points.GetLength(0);
            int yWidth = points.GetLength(1);

            colorCount = xWidth * yWidth;

            WriteableBitmap writeableBitmap = new WriteableBitmap(colorCount, 1, 96, 96, PixelFormats.Bgr24, null);

            writeableBitmap.Lock();

            unsafe
            {
                // Get a pointer to the back buffer.
                byte *pStart = (byte *)(void *)writeableBitmap.BackBuffer;
                int   nL     = writeableBitmap.BackBufferStride;

                for (int ix = 0; ix < colorCount; ix += 1)
                {
                    Color color = ColorHelper.GetGradientColor(Colors.Blue, Colors.Red, (double)ix / colorCount);

                    *(pStart + ix * 3 + 0) = (byte)(color.B);
                    *(pStart + ix * 3 + 1) = (byte)(color.G);
                    *(pStart + ix * 3 + 2) = (byte)(color.R);
                }
            }

            writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, colorCount, 1));

            // Release the back buffer and make it available for display.
            writeableBitmap.Unlock();

            ImageBrush imageBrush = new ImageBrush(writeableBitmap);

            imageBrush.ViewportUnits = BrushMappingMode.Absolute;

            SpecularMaterial material = new SpecularMaterial();

            material.Brush = imageBrush;

            bitMap = writeableBitmap;
            return(material);
        }
Example #30
0
        private void PaintChildOntoHostMaterial(Material material, VisualBrush childVisual)
        {
            bool             foundMaterialToSwap = false;
            Stack <Material> materialStack       = new Stack <Material>();

            materialStack.Push(material);

            //Stop searching for swappable material after first. This is how WPF logic behaves.
            while (materialStack.Count > 0 && !foundMaterialToSwap)
            {
                Material currMaterial = materialStack.Pop();
                bool     isChildHost  = (Boolean)currMaterial.GetValue(Viewport2DVisual3D.IsVisualHostMaterialProperty);

                if (isChildHost)
                {
                    if (currMaterial is DiffuseMaterial)
                    {
                        DiffuseMaterial diffMaterial = (DiffuseMaterial)currMaterial;
                        diffMaterial.Brush = childVisual;
                    }
                    else if (currMaterial is EmissiveMaterial)
                    {
                        EmissiveMaterial emmMaterial = (EmissiveMaterial)currMaterial;
                        emmMaterial.Brush = childVisual;
                    }
                    else if (currMaterial is SpecularMaterial)
                    {
                        SpecularMaterial specMaterial = (SpecularMaterial)currMaterial;
                        specMaterial.Brush = childVisual;
                    }
                    foundMaterialToSwap = true;
                }
                if (currMaterial is MaterialGroup)
                {
                    MaterialGroup matGroup = (MaterialGroup)currMaterial;
                    foreach (Material m in matGroup.Children)
                    {
                        materialStack.Push(m);
                    }
                }
            }
        }