public void CreateMaterial()
        {
            if (this.Material != null)
            {
                return;
            }

            byte[] data = this.Image.GetImageData();

            if (data != null)
            {
                var bitmap   = BitmapSource.Create(this.Image.Width, this.Image.Height, 96, 96, PixelFormats.Bgra32, null, data, this.Image.Width * 4);
                var material = new EmissiveMaterial(new ImageBrush(bitmap));
                material.Freeze();

                this.Material = material;
            }

            this.Image = null;
        }
Example #2
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
            });
        }