/// <summary> /// When implemented in a derived class, returns an object that is set as the value of the target property for this markup extension. /// </summary> /// <param name="serviceProvider"> /// Object that can provide services for the markup extension. /// </param> /// <returns> /// The object value to set on the property where the extension is applied. /// </returns> public override object ProvideValue(IServiceProvider serviceProvider) { var diffuse = new SolidColorBrush(this.color); var specular = BrushHelper.CreateGrayBrush(this.SpecularIntensity); return(MaterialHelper.CreateMaterial(diffuse, null, specular, this.Opacity, this.SpecularPower)); }
/// <summary> /// Updates the model. /// </summary> public void UpdateModel() { this.CreateGeometry(); var c = new Model3DGroup(); var mat = MaterialHelper.CreateMaterial(this.Fill); double l = this.HeadLength * this.Diameter; for (int i = 0; i < this.Positions.Count; i++) { var p = this.Positions[i]; var d = this.Directions[i]; var headModel = new GeometryModel3D { Geometry = this.head, Material = mat, Transform = CreateHeadTransform(p + d, d) }; c.Children.Add(headModel); var u = d; u.Normalize(); var bodyModel = new GeometryModel3D { Geometry = this.body, Material = mat, Transform = CreateBodyTransform(p, u * (1.0 - l / d.Length)) }; c.Children.Add(bodyModel); } this.model.Content = c; }
private Material CreateTextMaterial(Brush b, string text) { var grid = new Grid { Width = 20, Height = 20, Background = b }; grid.Children.Add( new TextBlock { Text = text, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 15, Foreground = Brushes.White }); grid.Arrange(new Rect(new Point(0, 0), new Size(20, 20))); var bmp = new RenderTargetBitmap((int)grid.Width, (int)grid.Height, 96, 96, PixelFormats.Default); bmp.Render(grid); bmp.Freeze(); var m = MaterialHelper.CreateMaterial(new ImageBrush(bmp)); m.Freeze(); return(m); }
/// <summary> /// Reads ascii. /// </summary> /// <param name="s"> /// The s. /// </param> public void ReadA(Stream s) { this.AsciiReader = new StreamReader(s); this.Meshes.Add(new MeshBuilder(true, true)); this.Materials.Add(MaterialHelper.CreateMaterial(Brushes.Blue)); while (!this.AsciiReader.EndOfStream) { var line = this.AsciiReader.ReadLine().Trim(); if (line.Length == 0 || line.StartsWith("\0") || line.StartsWith("#") || line.StartsWith("!") || line.StartsWith("$")) { continue; } string id, values; SplitLine(line, out id, out values); switch (id) { case "solid": break; case "facet": this.ReadFacetA(values); break; case "endsolid": break; } } this.AsciiReader.Close(); }
private void AddEdge(Point3D center, double x, double y, double z, Vector3D faceNormal) { var builder = new MeshBuilder(false, true); builder.AddBox(center, x, y, z); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = MaterialHelper.CreateMaterial(EdgeBrush) }; var element = new ModelUIElement3D { Model = model }; faceNormals.Add(element, faceNormal); faceUpVectors.Add(element, ModelUpDirection); element.MouseLeftButtonDown += FaceMouseLeftButtonDown; element.MouseEnter += EdggesMouseEnters; element.MouseLeave += EdgesMouseLeaves; Children.Add(element); EdgeModels.Add(element); }
/// <summary> /// Calculates the texture for the specified model. /// </summary> /// <param name="model"> /// The model. /// </param> /// <param name="mesh"> /// The mesh. /// </param> public override void Calculate(TerrainModel model, MeshGeometry3D mesh) { var normals = MeshGeometryHelper.CalculateNormals(mesh); var texcoords = new PointCollection(); var up = new Vector3D(0, 0, 1); for (int i = 0; i < normals.Count; i++) { double slope = Math.Acos(Vector3D.DotProduct(normals[i], up)) * 180 / Math.PI; double u = slope / 40; if (u > 1) { u = 1; } if (u < 0) { u = 0; } texcoords.Add(new Point(u, u)); } this.TextureCoordinates = texcoords; this.Material = MaterialHelper.CreateMaterial(this.Brush); }
/// <summary> /// Gets the material with the specified name. /// </summary> /// <param name="materialName"> /// The material name. /// </param> /// <returns> /// The material. /// </returns> private Material GetMaterial(string materialName) { MaterialDefinition mat; if (!string.IsNullOrEmpty(materialName) && this.Materials.TryGetValue(materialName, out mat)) { return(mat.GetMaterial(this.TexturePath)); } return(MaterialHelper.CreateMaterial(new SolidColorBrush(this.DefaultColor))); }
/// <summary> /// Adds a face. /// </summary> /// <param name="normal"> /// The normal. /// </param> /// <param name="up"> /// The up. /// </param> /// <param name="b"> /// The b. /// </param> /// <param name="text"> /// The text. /// </param> private void AddFace(Vector3D normal, Vector3D up, Brush b, string text) { var grid = new Grid { Width = 20, Height = 20, Background = b }; grid.Children.Add( new TextBlock { Text = text, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 15, Foreground = Brushes.White }); grid.Arrange(new Rect(new Point(0, 0), new Size(20, 20))); var bmp = new RenderTargetBitmap((int)grid.Width, (int)grid.Height, 96, 96, PixelFormats.Default); bmp.Render(grid); var material = MaterialHelper.CreateMaterial(new ImageBrush(bmp)); double a = this.Size; var builder = new MeshBuilder(false, true); builder.AddCubeFace(this.Center, normal, up, a, a, a); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = material }; var element = new ModelUIElement3D { Model = model }; element.MouseLeftButtonDown += this.FaceMouseLeftButtonDown; // element.MouseEnter += face_MouseEnter; // element.MouseLeave += face_MouseLeave; this.faceNormals.Add(element, normal); this.faceUpVectors.Add(element, up); this.Children.Add(element); }
private void EnableDisableEdgeClicks() { if (EnableEdgeClicks) { foreach (var item in EdgeModels) { item.MouseLeftButtonDown -= FaceMouseLeftButtonDown; item.MouseEnter -= EdggesMouseEnters; item.MouseLeave -= EdgesMouseLeaves; item.MouseLeftButtonDown += FaceMouseLeftButtonDown; item.MouseEnter += EdggesMouseEnters; item.MouseLeave += EdgesMouseLeaves; ModelUIElement3D s = item as ModelUIElement3D; (s.Model as GeometryModel3D).Material = MaterialHelper.CreateMaterial(EdgeBrush); } foreach (var item in CornerModels) { item.MouseLeftButtonDown -= FaceMouseLeftButtonDown; item.MouseEnter -= CornersMouseEnters; item.MouseLeave -= CornersMouseLeave; item.MouseLeftButtonDown += FaceMouseLeftButtonDown; item.MouseEnter += CornersMouseEnters; item.MouseLeave += CornersMouseLeave; ModelUIElement3D s = item as ModelUIElement3D; (s.Model as GeometryModel3D).Material = MaterialHelper.CreateMaterial(CornerBrush); } } else { foreach (var item in EdgeModels) { item.MouseLeftButtonDown -= FaceMouseLeftButtonDown; item.MouseEnter -= EdggesMouseEnters; item.MouseLeave -= EdgesMouseLeaves; ModelUIElement3D s = item as ModelUIElement3D; (s.Model as GeometryModel3D).Material = MaterialHelper.CreateMaterial(Colors.Transparent); } foreach (var item in CornerModels) { item.MouseLeftButtonDown -= FaceMouseLeftButtonDown; item.MouseEnter -= CornersMouseEnters; item.MouseLeave -= CornersMouseLeave; ModelUIElement3D s = item as ModelUIElement3D; (s.Model as GeometryModel3D).Material = MaterialHelper.CreateMaterial(Colors.Transparent); } } }
/// <summary> /// Updates the visuals. /// </summary> protected virtual void OnMeshChanged() { this.Children.Clear(); var builder = new MeshBuilder(); for (int i = 0; i < this.Mesh.Positions.Count; i++) { builder.AddArrow( this.Mesh.Positions[i], this.Mesh.Positions[i] + this.Mesh.Normals[i], this.Diameter, 3, 10); } this.Content = new GeometryModel3D { Geometry = builder.ToMesh(true), Material = MaterialHelper.CreateMaterial(this.Color) }; }
private void AddEdge(ModelUIElement3D element, Point3D center, double x, double y, double z, Vector3D faceNormal) { var builder = new MeshBuilder(false, true); builder.AddBox(center, x, y, z); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = MaterialHelper.CreateMaterial(EdgeBrush) }; element.Model = model; faceNormals.Add(element, faceNormal); faceUpVectors.Add(element, ModelUpDirection); }
/// <summary> /// Calculates the texture of the specified model. /// </summary> /// <param name="model"> /// The model. /// </param> /// <param name="mesh"> /// The mesh. /// </param> public override void Calculate(TerrainModel model, MeshGeometry3D mesh) { var normals = MeshGeometryHelper.CalculateNormals(mesh); var texcoords = new PointCollection(); for (int i = 0; i < normals.Count; i++) { double slopedir = Math.Atan2(normals[i].Y, normals[i].X) * 180 / Math.PI; if (slopedir < 0) { slopedir += 360; } double u = slopedir / 360; texcoords.Add(new Point(u, u)); } this.TextureCoordinates = texcoords; this.Material = MaterialHelper.CreateMaterial(this.Brush); }
/// <summary> /// The read triangle a. /// </summary> /// <param name="normal"> /// The normal. /// </param> private void ReadFacetA(string normal) { Vector3D n = this.ParseNormalA(normal); var points = new List <Point3D>(); this.ReadLineA("outer"); while (true) { var line = this.ReadLineA(); Point3D point; if (this.TryParseVertex(line, out point)) { points.Add(point); continue; } string id, values; SplitLine(line, out id, out values); if (id == "endloop") { break; } } this.ReadLineA("endfacet"); if (this.Materials.Count < this.index + 1) { this.Materials.Add(MaterialHelper.CreateMaterial(Brushes.Blue)); } if (this.Meshes.Count < this.index + 1) { this.Meshes.Add(new MeshBuilder(true, true)); } this.Meshes[this.index].AddPolygon(points); // todo: add normals }
private void AddCorners() { var a = Size / 2; var sideLength = a / 2; Point3D[] points = { new Point3D(-1, -1, -1), new Point3D(1, -1, -1), new Point3D(1, 1, -1), new Point3D(-1, 1, -1), new Point3D(-1, -1, 1), new Point3D(1, -1, 1), new Point3D(1, 1, 1), new Point3D(-1, 1, 1) }; foreach (var p in points) { var builder = new MeshBuilder(false, true); Point3D center = p.Multiply(a); builder.AddBox(center, sideLength, sideLength, sideLength); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = MaterialHelper.CreateMaterial(CornerBrush) }; var element = new ModelUIElement3D { Model = model }; faceNormals.Add(element, p.ToVector3D()); faceUpVectors.Add(element, ModelUpDirection); element.MouseLeftButtonDown += FaceMouseLeftButtonDown; element.MouseEnter += CornersMouseEnters; element.MouseLeave += CornersMouseLeave; Children.Add(element); CornerModels.Add(element); } }
/// <summary> /// Reads a binary stream. /// </summary> /// <param name="s"> /// The s. /// </param> public void ReadB(Stream s) { long length = this.BinaryReader.BaseStream.Length; if (length < 84) { throw new FileFormatException("Incomplete file"); } string header = this.ReadHeaderB(); uint numberTriangles = this.ReadNumberTrianglesB(); this.index = 0; this.Meshes.Add(new MeshBuilder(true, true)); this.Materials.Add(MaterialHelper.CreateMaterial(Brushes.Blue)); for (int i = 0; i < numberTriangles; i++) { this.ReadTriangleB(); } this.BinaryReader.Close(); }
/// <summary> /// Read a surface. /// </summary> /// <param name="reader">The reader.</param> /// <param name="size">The size.</param> private void ReadSurface(BinaryReader reader, int size) { this.Surfaces = new List <string>(); this.Meshes = new List <MeshBuilder>(); this.Materials = new List <Material>(); string name = this.ReadString(reader, size); var names = name.Split('\0'); for (int i = 0; i < names.Length; i++) { string n = names[i]; this.Surfaces.Add(n); this.Meshes.Add(new MeshBuilder(false, false)); this.Materials.Add(MaterialHelper.CreateMaterial(Brushes.Blue)); // If the length of the string (including the null) is odd, an extra null byte is added. // Then skip the next empty string. if ((n.Length + 1) % 2 == 1) { i++; } } }
private void EnableDisableEdgeClicks() { foreach (var item in EdgeModels) { Children.Remove(item); } foreach (var item in CornerModels) { Children.Remove(item); } if (EnableEdgeClicks) { foreach (var item in EdgeModels) { (item.Model as GeometryModel3D).Material = MaterialHelper.CreateMaterial(EdgeBrush); Children.Add(item); } foreach (var item in CornerModels) { (item.Model as GeometryModel3D).Material = MaterialHelper.CreateMaterial(CornerBrush); Children.Add(item); } } }
private void AddCorners() { var a = Size / 2; var sideLength = a / 2; int counter = 0; foreach (var p in cornerPoints) { var builder = new MeshBuilder(false, true); Point3D center = p.Multiply(a); builder.AddBox(center, sideLength, sideLength, sideLength); var geometry = builder.ToMesh(); geometry.Freeze(); var model = new GeometryModel3D { Geometry = geometry, Material = MaterialHelper.CreateMaterial(CornerBrush) }; var element = CornerModels[counter++]; element.Model = model; faceNormals.Add(element, p.ToVector3D()); faceUpVectors.Add(element, ModelUpDirection); } }
/// <summary> /// Initializes a new instance of the <see cref="StLReader"/> class. /// </summary> public StLReader() { this.Meshes = new List <MeshBuilder>(); this.Materials = new List <Material>(); this.DefaultMaterial = MaterialHelper.CreateMaterial(Brushes.Blue); }
/// <summary> /// Handles changes in the Color property (this will override the materials). /// </summary> private void ColorChanged() { this.Material = MaterialHelper.CreateMaterial(this.Color); this.BackMaterial = this.Material; }
/// <summary> /// The read triangle b. /// </summary> private void ReadTriangleB() { Color current; bool hasColor = false; bool sameColor = true; float ni = this.ReadFloatB(); float nj = this.ReadFloatB(); float nk = this.ReadFloatB(); var n = new Vector3D(ni, nj, nk); float v1x = this.ReadFloatB(); float v1y = this.ReadFloatB(); float v1z = this.ReadFloatB(); var v1 = new Point3D(v1x, v1y, v1z); float v2x = this.ReadFloatB(); float v2y = this.ReadFloatB(); float v2z = this.ReadFloatB(); var v2 = new Point3D(v2x, v2y, v2z); float v3x = this.ReadFloatB(); float v3y = this.ReadFloatB(); float v3z = this.ReadFloatB(); var v3 = new Point3D(v3x, v3y, v3z); // UInt16 attrib = ReadUInt16(); var attrib = Convert.ToString(this.ReadUInt16B(), 2).PadLeft(16, '0').ToCharArray(); hasColor = attrib[0].Equals('1'); if (hasColor) { int blue = attrib[15].Equals('1') ? 1 : 0; blue = attrib[14].Equals('1') ? blue + 2 : blue; blue = attrib[13].Equals('1') ? blue + 4 : blue; blue = attrib[12].Equals('1') ? blue + 8 : blue; blue = attrib[11].Equals('1') ? blue + 16 : blue; int b = blue * 8; int green = attrib[10].Equals('1') ? 1 : 0; green = attrib[9].Equals('1') ? green + 2 : green; green = attrib[8].Equals('1') ? green + 4 : green; green = attrib[7].Equals('1') ? green + 8 : green; green = attrib[6].Equals('1') ? green + 16 : green; int g = green * 8; int red = attrib[5].Equals('1') ? 1 : 0; red = attrib[4].Equals('1') ? red + 2 : red; red = attrib[3].Equals('1') ? red + 4 : red; red = attrib[2].Equals('1') ? red + 8 : red; red = attrib[1].Equals('1') ? red + 16 : red; int r = red * 8; current = Color.FromRgb(Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)); sameColor = Color.Equals(this.last, current); if (!sameColor) { this.last = current; this.index++; } if (this.Materials.Count < this.index + 1) { this.Materials.Add(MaterialHelper.CreateMaterial(current)); } } else { if (this.Materials.Count < this.index + 1) { this.Materials.Add(MaterialHelper.CreateMaterial(Brushes.Blue)); } } if (this.Meshes.Count < this.index + 1) { this.Meshes.Add(new MeshBuilder(true, true)); } this.Meshes[this.index].AddTriangle(v1, v2, v3); }
private void CornersMouseEnters(object sender, MouseEventArgs e) { ModelUIElement3D s = sender as ModelUIElement3D; (s.Model as GeometryModel3D).Material = MaterialHelper.CreateMaterial(Colors.Goldenrod); }
private void EdgesMouseLeaves(object sender, MouseEventArgs e) { ModelUIElement3D s = sender as ModelUIElement3D; (s.Model as GeometryModel3D).Material = MaterialHelper.CreateMaterial(Colors.Silver); }
/// <summary> /// The Fill property was changed. /// </summary> protected virtual void OnFillChanged() { this.Material = MaterialHelper.CreateMaterial(this.Fill); this.BackMaterial = this.Material; }
/// <summary> /// Reads a triangle from a binary STL file. /// </summary> /// <param name="reader"> /// The reader. /// </param> private void ReadTriangle(BinaryReader reader) { float ni = ReadFloat(reader); float nj = ReadFloat(reader); float nk = ReadFloat(reader); #pragma warning disable 168 var n = new Vector3D(ni, nj, nk); #pragma warning restore 168 float x1 = ReadFloat(reader); float y1 = ReadFloat(reader); float z1 = ReadFloat(reader); var v1 = new Point3D(x1, y1, z1); float x2 = ReadFloat(reader); float y2 = ReadFloat(reader); float z2 = ReadFloat(reader); var v2 = new Point3D(x2, y2, z2); float x3 = ReadFloat(reader); float y3 = ReadFloat(reader); float z3 = ReadFloat(reader); var v3 = new Point3D(x3, y3, z3); var attrib = Convert.ToString(ReadUInt16(reader), 2).PadLeft(16, '0').ToCharArray(); var hasColor = attrib[0].Equals('1'); if (hasColor) { int blue = attrib[15].Equals('1') ? 1 : 0; blue = attrib[14].Equals('1') ? blue + 2 : blue; blue = attrib[13].Equals('1') ? blue + 4 : blue; blue = attrib[12].Equals('1') ? blue + 8 : blue; blue = attrib[11].Equals('1') ? blue + 16 : blue; int b = blue * 8; int green = attrib[10].Equals('1') ? 1 : 0; green = attrib[9].Equals('1') ? green + 2 : green; green = attrib[8].Equals('1') ? green + 4 : green; green = attrib[7].Equals('1') ? green + 8 : green; green = attrib[6].Equals('1') ? green + 16 : green; int g = green * 8; int red = attrib[5].Equals('1') ? 1 : 0; red = attrib[4].Equals('1') ? red + 2 : red; red = attrib[3].Equals('1') ? red + 4 : red; red = attrib[2].Equals('1') ? red + 8 : red; red = attrib[1].Equals('1') ? red + 16 : red; int r = red * 8; var currentColor = Color.FromRgb(Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)); if (!Color.Equals(this.lastColor, currentColor)) { this.lastColor = currentColor; this.index++; } if (this.Materials.Count < this.index + 1) { this.Materials.Add(MaterialHelper.CreateMaterial(currentColor)); } } else { if (this.Materials.Count < this.index + 1) { this.Materials.Add(this.DefaultMaterial); } } if (this.Meshes.Count < this.index + 1) { this.Meshes.Add(new MeshBuilder(true, true)); } this.Meshes[this.index].AddTriangle(v1, v2, v3); // todo: add normal }