using System; using System.IO; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Media.Media3D; namespace HelixToolkit { /// /// Creates diffuse/specular materials /// public static class MaterialHelper { public static double DefaultSpecularPower = 100; /// /// Creates a material for the specified color. /// /// The color. /// public static Material CreateMaterial(Color color) { return CreateMaterial(new SolidColorBrush(color)); } /// /// Creates a material for the specified color and opacity. /// /// The color. /// The opacity. /// public static Material CreateMaterial(Color color, double opacity) { return CreateMaterial(Color.FromArgb((byte)(opacity * 255), color.R, color.G, color.B)); } /// /// Creates a material for the specified brush. /// /// The brush. /// public static Material CreateMaterial(Brush brush) { return CreateMaterial(brush, DefaultSpecularPower); } /// /// Creates a material with the specifed brush as diffuse material. /// This method will also add a white specular material. /// /// The brush. /// The specular power. /// public static Material CreateMaterial(Brush brush, double specularPower) { var mg = new MaterialGroup(); mg.Children.Add(new DiffuseMaterial(brush)); if (specularPower > 0) mg.Children.Add(new SpecularMaterial(Brushes.White, specularPower)); return mg; } /// /// Creates a material with the specified diffuse, emissive and specular brushes. /// /// The diffuse. /// The emissive. /// The specular. /// The opacity. /// The specular power. /// public static Material CreateMaterial(Brush diffuse, Brush emissive = null, Brush specular = null, double opacity = 1, double specularPower = 85) { 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)); } return mg; } /// /// Creates a material from the specified bitmap file. /// /// The path. /// public static Material CreateImageMaterial(string path) { return CreateImageMaterial(path, 1); } /// /// Creates a material from the specified bitmap file. /// /// The path. /// The opacity. /// public static Material CreateImageMaterial(string path, double opacity) { var fullPath = Path.GetFullPath(path); if (!File.Exists(fullPath)) return null; var image = new BitmapImage(); image.BeginInit(); image.UriSource = new Uri(fullPath); image.EndInit(); return CreateImageMaterial(image, opacity); } /// /// Creates a material from the specified BitmapImage. /// /// The image. /// The opacity. /// public static Material CreateImageMaterial(BitmapImage image, double opacity) { var brush = new ImageBrush(image); brush.Opacity = opacity; return new DiffuseMaterial(brush); } public static void ChangeOpacity(Material material, double d) { var mg = material as MaterialGroup; if (mg != null) foreach (var m in mg.Children) ChangeOpacity(m, d); var dm = material as DiffuseMaterial; if (dm != null) { var scb = dm.Brush as SolidColorBrush; if (scb != null) scb.Opacity = d; } } } }