Exemplo n.º 1
0
        /// <summary>
        /// Creates a material with the specified diffuse, emissive and specular brushes.
        /// </summary>
        /// <param name="diffuse">The diffuse color.</param>
        /// <param name="emissive">The emissive color.</param>
        /// <param name="specular">The specular color.</param>
        /// <param name="opacity">The opacity.</param>
        /// <param name="specularPower">The specular power.</param>
        /// <param name="freeze">Freeze the material if set to <c>true</c>.</param>
        /// <returns>The material.</returns>
        public static Material CreateMaterial(Brush diffuse, Brush emissive, Brush specular = null, double opacity = 1, double specularPower = 85, bool freeze = true)
        {
            var mg = new MaterialGroup();

            if (diffuse != null)
            {
                diffuse         = diffuse.Clone();
                diffuse.Opacity = opacity;
                mg.Children.Add(new DiffuseMaterial(diffuse));
            }

            if (emissive != null)
            {
                emissive         = emissive.Clone();
                emissive.Opacity = opacity;
                mg.Children.Add(new EmissiveMaterial(emissive));
            }

            if (specular != null)
            {
                specular         = specular.Clone();
                specular.Opacity = opacity;
                mg.Children.Add(new SpecularMaterial(specular, specularPower));
            }

            if (freeze)
            {
                mg.Freeze();
            }

            return(mg);
        }
 /// <summary>
 /// Changes the material when the color changed.
 /// </summary>
 private void ColorChanged()
 {
     var mg = new MaterialGroup();
     mg.Children.Add(new DiffuseMaterial(Brushes.Black));
     mg.Children.Add(new EmissiveMaterial(new SolidColorBrush(this.Color)));
     mg.Freeze();
     this.Model.Material = mg;
 }
Exemplo n.º 3
0
        static ScreenSpaceLines3D()
        {
            MaterialGroup mat = new MaterialGroup();

            mat.Children.Add(new DiffuseMaterial(new SolidColorBrush(Colors.Black)));
            mat.Children.Add(new EmissiveMaterial(new SolidColorBrush(Colors.Black)));
            mat.Freeze();
            _material = mat;
        }
Exemplo n.º 4
0
        private void SetColor(Color color)
        {
            MaterialGroup unlitMaterial = new MaterialGroup();

            unlitMaterial.Children.Add(new DiffuseMaterial(new SolidColorBrush(Colors.Black)));
            unlitMaterial.Children.Add(new EmissiveMaterial(new SolidColorBrush(color)));
            unlitMaterial.Freeze();

            _model.Material     = unlitMaterial;
            _model.BackMaterial = unlitMaterial;
        }
Exemplo n.º 5
0
        private void SetColor(Color color)
        {
            MaterialGroup unlitMaterial = new MaterialGroup();
            Color         diffuse       = Colors.Black;

            diffuse.ScA = color.ScA;
            unlitMaterial.Children.Add(new DiffuseMaterial(new SolidColorBrush(diffuse)));
            unlitMaterial.Children.Add(new EmissiveMaterial(new SolidColorBrush(color)));
            unlitMaterial.Freeze();

            _content.Material     = unlitMaterial;
            _content.BackMaterial = unlitMaterial;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates an emissive material from the specified image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="diffuseBrush">The diffuse brush.</param>
        /// <param name="freeze">Freeze the material if set to <c>true</c>.</param>
        /// <returns>The image material</returns>
        public static Material CreateEmissiveImageMaterial(BitmapImage image, Brush diffuseBrush, bool freeze = true)
        {
            var brush = new ImageBrush(image);
            var em    = new EmissiveMaterial(brush);
            var dm    = new DiffuseMaterial(diffuseBrush);
            var mg    = new MaterialGroup();

            mg.Children.Add(dm);
            mg.Children.Add(em);
            if (freeze)
            {
                mg.Freeze();
            }

            return(mg);
        }
        /// <summary>
        /// Creates a material with the specified brush as diffuse material.
        /// This method will also add a white specular material.
        /// </summary>
        /// <param name="brush">The brush.</param>
        /// <param name="specularPower">The specular power.</param>
        /// <param name="ambient">The ambient component.</param>
        /// <param name="freeze">Freeze the material if set to <c>true</c>.</param>
        /// <returns>The material.</returns>
        public static Material CreateMaterial(Brush brush, double specularPower = 100, byte ambient = 255, bool freeze = true)
        {
            var mg = new MaterialGroup();

            mg.Children.Add(new DiffuseMaterial(brush)
            {
                AmbientColor = Color.FromRgb(ambient, ambient, ambient)
            });
            if (specularPower > 0)
            {
                mg.Children.Add(new SpecularMaterial(Brushes.White, specularPower));
            }

            if (freeze)
            {
                mg.Freeze();
            }

            return(mg);
        }
Exemplo n.º 8
0
        public Model3D Create()
        {
            MeshGeometry3D mesh = new MeshGeometry3D();

            var halfThickness = Thickness / 2;

            mesh.Positions.Add(new Point3D(1, 0, 0));
            mesh.Positions.Add(new Point3D(1 + halfThickness, 0, 0));
            mesh.Positions.Add(new Point3D(1 + halfThickness, 2.5, 0));
            mesh.Positions.Add(new Point3D(1 + halfThickness, 2.5, halfThickness));

            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(2);
            mesh.TriangleIndices.Add(2);
            mesh.TriangleIndices.Add(3);
            mesh.TriangleIndices.Add(1);

            var normal = MathHelper.CalculateNormal(mesh.Positions[0], mesh.Positions[1], mesh.Positions[2]);

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

            GeometryModel3D model = new GeometryModel3D();

            model.Geometry = mesh;
            //model.Material = new EmissiveMaterial(new SolidColorBrush(Colors.Blue));
            //model.BackMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Blue)); ;

            var mg = new MaterialGroup();

            mg.Children.Add(new DiffuseMaterial(Brushes.Black));
            mg.Children.Add(new EmissiveMaterial(new SolidColorBrush(Colors.Blue)));
            mg.Freeze();

            model.Material     = mg;
            model.BackMaterial = mg;
            return(model);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a material with the specified brush as diffuse material.
        /// This method will also add a white specular material.
        /// </summary>
        /// <param name="brush">The brush of the diffuse material.</param>
        /// <param name="specularBrightness">The brightness of the specular material.</param>
        /// <param name="specularPower">The specular power.</param>
        /// <param name="ambient">The ambient component.</param>
        /// <param name="freeze">Freeze the material if set to <c>true</c>.</param>
        /// <returns>
        /// The material.
        /// </returns>
        public static Material CreateMaterial(Brush brush, double specularBrightness, double specularPower = 100, byte ambient = 255, bool freeze = true)
        {
            var mg = new MaterialGroup();

            mg.Children.Add(new DiffuseMaterial(brush)
            {
                AmbientColor = Color.FromRgb(ambient, ambient, ambient)
            });
            if (specularPower > 0)
            {
                var b = (byte)(255 * specularBrightness);
                mg.Children.Add(new SpecularMaterial(new SolidColorBrush(Color.FromRgb(b, b, b)), specularPower));
            }

            if (freeze)
            {
                mg.Freeze();
            }

            return(mg);
        }
Exemplo n.º 10
0
        private Model3D createModel(MeshGeometry3D geometry, Brush frontColor = null, Brush backColor = null, bool useComplexMaterial = false)
        {
            if (frontColor == null)
            {
                frontColor = Brushes.Red;
            }

            if (backColor == null)
            {
                backColor = Brushes.Yellow;
            }

            var materialGroup = new MaterialGroup();

            materialGroup.Children.Add(new DiffuseMaterial(frontColor));
            if (useComplexMaterial)
            {
                materialGroup.Children.Add(new EmissiveMaterial(frontColor));
            }

            var backMaterialGroup = new MaterialGroup();

            backMaterialGroup.Children.Add(new DiffuseMaterial(backColor));
            if (useComplexMaterial)
            {
                backMaterialGroup.Children.Add(new EmissiveMaterial(backColor));
            }


            var model = new GeometryModel3D(geometry, materialGroup);

            model.BackMaterial = backMaterialGroup;


            materialGroup.Freeze();
            backMaterialGroup.Freeze();
            model.Freeze();
            return(model);
        }
Exemplo n.º 11
0
        public SceneEditor()
        {
            InitializeComponent();

            _currentEditorMode = EditorModes.Create;

            var materialGroup = new MaterialGroup();

            materialGroup.Children.Add(new DiffuseMaterial(new SolidColorBrush(Color.FromArgb(200, 36, 117, 137)))); // #247589
            materialGroup.Children.Add(new SpecularMaterial(Brushes.White, 16));
            materialGroup.Freeze();
            _standardMaterial = materialGroup;

            _standardBackMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(36, 117, 137)));
            _standardBackMaterial.Freeze();


            UpdateEditorModeUI();

            SetupOverlayViewport3D();


            // We need to synchronize the Lights in OverlayViewport with the camera in the MainViewport
            Camera1.CameraChanged += delegate(object s, CameraChangedRoutedEventArgs args)
            {
                // When camera is changed, we need to update the 2D positions that are get from 3D points
                OverlayCanvas.UpdateScreenPositions();
            };

            MainViewport.SizeChanged += delegate(object sender, SizeChangedEventArgs args)
            {
                // When MainViewport's size is changed, we need to update the 2D positions that are get from 3D points
                OverlayCanvas.UpdateScreenPositions();
            };


            this.Loaded += SceneEditor_Loaded;
        }
Exemplo n.º 12
0
        internal static GeometryModel3D MakeLine3D(Vector3D start, Vector3D end, Brush brush, double scale = 0.01)
        {
            MeshGeometry3D mesh   = new MeshGeometry3D();
            var            length = (end - start).Length;

            var positions = new List <Point3D>();

            positions.Add(new Point3D(-scale, +scale, 0));
            positions.Add(new Point3D(-scale, +scale, +length));
            positions.Add(new Point3D(+scale, +scale, +length));
            positions.Add(new Point3D(+scale, +scale, 0));
            positions.Add(new Point3D(-scale, -scale, 0));
            positions.Add(new Point3D(-scale, -scale, +length));
            positions.Add(new Point3D(+scale, -scale, +length));
            positions.Add(new Point3D(+scale, -scale, 0));

            var segment     = end - start;
            var defaultAxis = new Vector3D(0, 0, length);

            var rot     = Vector3D.AngleBetween(defaultAxis, segment);
            var rotAxis = Vector3D.CrossProduct(defaultAxis.NormalizeTo(), segment.NormalizeTo());

            var matRot = new Matrix3D();

            if (rotAxis.Length > 0.0)
            {
                matRot.Rotate(new Quaternion(rotAxis, rot));
            }

            foreach (var pos in positions)
            {
                mesh.Positions.Add(matRot.Transform(pos) + start);
            }

            // top
            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(3);
            mesh.TriangleIndices.Add(2);
            mesh.TriangleIndices.Add(3);
            mesh.TriangleIndices.Add(1);

            // bottom
            mesh.TriangleIndices.Add(4);
            mesh.TriangleIndices.Add(5);
            mesh.TriangleIndices.Add(7);
            mesh.TriangleIndices.Add(6);
            mesh.TriangleIndices.Add(7);
            mesh.TriangleIndices.Add(5);

            // left
            mesh.TriangleIndices.Add(0);
            mesh.TriangleIndices.Add(1);
            mesh.TriangleIndices.Add(4);
            mesh.TriangleIndices.Add(5);
            mesh.TriangleIndices.Add(4);
            mesh.TriangleIndices.Add(1);

            // right
            mesh.TriangleIndices.Add(3);
            mesh.TriangleIndices.Add(2);
            mesh.TriangleIndices.Add(7);
            mesh.TriangleIndices.Add(6);
            mesh.TriangleIndices.Add(7);
            mesh.TriangleIndices.Add(2);

            mesh.Freeze();

            var diffuse  = new DiffuseMaterial(Brushes.Black);
            var emissive = new EmissiveMaterial(brush);

            diffuse.Freeze();
            emissive.Freeze();

            var material = new MaterialGroup();

            material.Children.Add(diffuse);
            material.Children.Add(emissive);
            material.Freeze();

            return(new GeometryModel3D(mesh, material)
            {
                BackMaterial = material
            });
        }
Exemplo n.º 13
0
    private void generate_model()
    {
        MeshGeometry3D mesh = new MeshGeometry3D();


        for (int x = 0; x < x_node_count; x++)           // n -> x
        {
            for (int y = 0; y < y_node_count; y++)       // m -> y
            {
                mesh.Positions.Add(grid[x, y].position);
            }
        }



        for (int x = 0; x < x_node_count; x++)           // n -> x
        {
            for (int y = 0; y < y_node_count; y++)       // m -> y
            {
                mesh.TextureCoordinates.Add(new Point(1.0 - (((double)x) / ((double)(x_node_count - 1))), 1.0 - (((double)y) / ((double)(y_node_count - 1)))));
            }
        }



        for (int x = 0; x < x_node_count - 1; x++)
        {
            for (int y = 0; y < y_node_count - 1; y++)
            {
                if ((x + y) % 2 == 1)
                {
                    mesh.TriangleIndices.Add(y + (x * y_node_count));
                    mesh.TriangleIndices.Add((y + 1) + x * y_node_count);
                    mesh.TriangleIndices.Add((y) + ((x + 1) * y_node_count));

                    mesh.TriangleIndices.Add(y + ((x + 1) * y_node_count));
                    mesh.TriangleIndices.Add((y + 1) + ((x) * y_node_count));
                    mesh.TriangleIndices.Add((y + 1) + ((x + 1) * y_node_count));
                }

                else
                {
                    mesh.TriangleIndices.Add(y + (x * y_node_count));
                    mesh.TriangleIndices.Add((y + 1) + x * y_node_count);
                    mesh.TriangleIndices.Add((y + 1) + ((x + 1) * y_node_count));

                    mesh.TriangleIndices.Add(y + (x * y_node_count));
                    mesh.TriangleIndices.Add((y + 1) + ((x + 1) * y_node_count));
                    mesh.TriangleIndices.Add(y + ((x + 1) * y_node_count));
                }
            }
        }


        MaterialGroup material_group = new MaterialGroup();

        material_group.Children.Add(new EmissiveMaterial(MakeText(Brushes.DarkOrange)));
        material_group.Children.Add(new SpecularMaterial(MakeText(Brushes.White), 20));
        material_group.Children.Add(new EmissiveMaterial(MakeGrid()));
        material_group.Children.Add(new SpecularMaterial((Brushes.Gray), 10));

        model = new GeometryModel3D(mesh, material_group);
        model.BackMaterial = material_group;
        material_group.Freeze();
    }
        public void CreateScene(bool createExtremlyComplexScene)
        {
            // This is called in background thread

            var rootVisual3D = new ModelVisual3D();

            var floorBox = new BoxVisual3D()
            {
                CenterPosition = new Point3D(0, -0.05, 0),
                Size           = new Size3D(15, 0.1, 15),
                Material       = new DiffuseMaterial(Brushes.Green)
            };

            rootVisual3D.Children.Add(floorBox);


            double centerX = 0;
            double centerZ = 0;

            double boxesHeight = 1.4;

            double bigCircleRadius    = 2;
            double singleSphereRadius = 0.1;


            int    circleElementsCount;
            double spheresStartRadius;
            double spheresEndRadius;
            int    yObjectsCount;

            if (createExtremlyComplexScene)
            {
                // This will create a 3D scene with almost 6000 3D objects
                // This will put a big load on GPU and especially on the CPU (the DirectX API and driver).
                //
                // NOTE:
                // When rendering many instances of the same object, it is possible to render the same scene
                // much much more efficiently with using InstancedMeshGeometryVisual3D.
                boxesHeight         = 3;
                circleElementsCount = 40;
                spheresStartRadius  = bigCircleRadius * 0.3;
                spheresEndRadius    = bigCircleRadius;
                yObjectsCount       = 30;
            }
            else
            {
                boxesHeight         = 1.4;
                circleElementsCount = 18;
                spheresStartRadius  = bigCircleRadius;
                spheresEndRadius    = bigCircleRadius;
                yObjectsCount       = 1;
            }


            var boxMaterial    = new DiffuseMaterial(Brushes.Gray);
            var sphereMaterial = new MaterialGroup();

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

            // IMPORTANT:
            // Freezing materials speeds up object creation by a many times !!!
            boxMaterial.Freeze();
            sphereMaterial.Freeze();

            for (int y = 0; y < yObjectsCount; y++)
            {
                double oneBoxHeight;
                double oneBoxHeightSpace = boxesHeight / yObjectsCount;

                if (yObjectsCount == 1)
                {
                    oneBoxHeight = oneBoxHeightSpace;
                }
                else
                {
                    oneBoxHeight = oneBoxHeightSpace * 0.7;
                }

                for (int a = 0; a < 360; a += (int)(Math.Ceiling(360.0 / circleElementsCount)))
                {
                    double rad = SharpDX.MathUtil.DegreesToRadians(a);
                    double sin = Math.Sin(rad);
                    double cos = Math.Cos(rad);


                    double x = sin * bigCircleRadius + centerX;
                    double z = cos * bigCircleRadius + centerZ;

                    var boxVisual3D = new BoxVisual3D()
                    {
                        CenterPosition = new Point3D(x, (y + 0.5) * oneBoxHeightSpace, z),
                        Size           = new Size3D(0.2, oneBoxHeight, 0.2),
                        Material       = boxMaterial
                    };

                    rootVisual3D.Children.Add(boxVisual3D);

                    for (double radius = spheresStartRadius; radius <= spheresEndRadius; radius += singleSphereRadius * 2)
                    {
                        x = sin * radius + centerX;
                        z = cos * radius + centerZ;

                        var sphereVisual3D = new SphereVisual3D()
                        {
                            CenterPosition = new Point3D(x, boxesHeight + singleSphereRadius * (y + 0.5) * 2, z),
                            Radius         = singleSphereRadius,
                            Material       = sphereMaterial
                        };

                        rootVisual3D.Children.Add(sphereVisual3D);
                    }
                }
            }


            wpfViewport3D.Children.Clear();
            wpfViewport3D.Children.Add(rootVisual3D);


            // Add lights
            var lightsVisual3D = new ModelVisual3D();
            var lightsGroup    = new Model3DGroup();

            var directionalLight = new DirectionalLight(Colors.White, new Vector3D(1, -0.3, 0));

            lightsGroup.Children.Add(directionalLight);

            var ambientLight = new AmbientLight(System.Windows.Media.Color.FromRgb(60, 60, 60));

            lightsGroup.Children.Add(ambientLight);

            lightsVisual3D.Content = lightsGroup;
            wpfViewport3D.Children.Add(lightsVisual3D);


            if (_targetPositionCamera != null)
            {
                if (createExtremlyComplexScene)
                {
                    _targetPositionCamera.TargetPosition = new Point3D(0, 2, 0);
                    _targetPositionCamera.Attitude       = -30;
                    _targetPositionCamera.Distance       = 15;
                }
                else
                {
                    _targetPositionCamera.TargetPosition = new Point3D(0, 0.5, 0);
                    _targetPositionCamera.Attitude       = -10;
                    _targetPositionCamera.Distance       = 8;
                }
            }

            isSceneDirty = true;
        }