private void RebuildGeometry() { double halfThickness = Thickness / 2.0; int numLines = Points.Count / 2; Point3DCollection positions = new Point3DCollection(numLines * 4); for (int i = 0; i < numLines; i++) { int startIndex = i * 2; Point3D startPoint = Points[startIndex]; Point3D endPoint = Points[startIndex + 1]; AddSegment(positions, startPoint, endPoint, halfThickness); } positions.Freeze(); _mesh.Positions = positions; Int32Collection indices = new Int32Collection(Points.Count * 3); for (int i = 0; i < Points.Count / 2; i++) { indices.Add(i * 4 + 2); indices.Add(i * 4 + 1); indices.Add(i * 4 + 0); indices.Add(i * 4 + 2); indices.Add(i * 4 + 3); indices.Add(i * 4 + 1); } indices.Freeze(); _mesh.TriangleIndices = indices; }
/// <summary> /// Use the render packages returned from the visualization manager to update the visuals. /// The visualization event arguments will contain a set of render packages and an id representing /// the associated node. Visualizations for the background preview will return an empty id. /// </summary> /// <param name="e"></param> public void RenderDrawables(VisualizationEventArgs e) { //check the id, if the id is meant for another watch, //then ignore it if (e.Id != _id) { return; } Debug.WriteLine(string.Format("Rendering visuals for {0}", e.Id)); var sw = new Stopwatch(); sw.Start(); Points = null; Lines = null; Mesh = null; XAxes = null; YAxes = null; ZAxes = null; PointsSelected = null; LinesSelected = null; MeshSelected = null; Text = null; MeshCount = 0; //separate the selected packages var packages = e.Packages.Where(x => x.Selected == false) .Where(rp => rp.TriangleVertices.Count % 9 == 0) .ToArray(); var selPackages = e.Packages .Where(x => x.Selected) .Where(rp => rp.TriangleVertices.Count % 9 == 0) .ToArray(); //pre-size the points collections var pointsCount = packages.Select(x => x.PointVertices.Count / 3).Sum(); var selPointsCount = selPackages.Select(x => x.PointVertices.Count / 3).Sum(); var points = new Point3DCollection(pointsCount); var pointsSelected = new Point3DCollection(selPointsCount); //pre-size the lines collections //these sizes are conservative as the axis lines will be //taken from the linestripvertex collections as well. var lineCount = packages.Select(x => x.LineStripVertices.Count / 3).Sum(); var lineSelCount = selPackages.Select(x => x.LineStripVertices.Count / 3).Sum(); var lines = new Point3DCollection(lineCount); var linesSelected = new Point3DCollection(lineSelCount); var redLines = new Point3DCollection(lineCount); var greenLines = new Point3DCollection(lineCount); var blueLines = new Point3DCollection(lineCount); //pre-size the text collection var textCount = e.Packages.Count(x => x.DisplayLabels); var text = new List <BillboardTextItem>(textCount); //http://blogs.msdn.com/b/timothyc/archive/2006/08/31/734308.aspx //presize the mesh collections var meshVertCount = packages.Select(x => x.TriangleVertices.Count / 3).Sum(); var meshVertSelCount = selPackages.Select(x => x.TriangleVertices.Count / 3).Sum(); var mesh = new MeshGeometry3D(); var meshSel = new MeshGeometry3D(); var verts = new Point3DCollection(meshVertCount); var vertsSel = new Point3DCollection(meshVertSelCount); var norms = new Vector3DCollection(meshVertCount); var normsSel = new Vector3DCollection(meshVertSelCount); var tris = new Int32Collection(meshVertCount); var trisSel = new Int32Collection(meshVertSelCount); foreach (var package in packages) { ConvertPoints(package, points, text); ConvertLines(package, lines, redLines, greenLines, blueLines, text); ConvertMeshes(package, verts, norms, tris); } foreach (var package in selPackages) { ConvertPoints(package, pointsSelected, text); ConvertLines(package, linesSelected, redLines, greenLines, blueLines, text); ConvertMeshes(package, vertsSel, normsSel, trisSel); } sw.Stop(); Debug.WriteLine(string.Format("RENDER: {0} ellapsed for updating background preview.", sw.Elapsed)); var vm = (IWatchViewModel)DataContext; if (vm.CheckForLatestRenderCommand.CanExecute(e.TaskId)) { vm.CheckForLatestRenderCommand.Execute(e.TaskId); } points.Freeze(); pointsSelected.Freeze(); lines.Freeze(); linesSelected.Freeze(); redLines.Freeze(); greenLines.Freeze(); blueLines.Freeze(); verts.Freeze(); norms.Freeze(); tris.Freeze(); vertsSel.Freeze(); normsSel.Freeze(); trisSel.Freeze(); Dispatcher.Invoke(new Action <Point3DCollection, Point3DCollection, Point3DCollection, Point3DCollection, Point3DCollection, Point3DCollection, Point3DCollection, Point3DCollection, Vector3DCollection, Int32Collection, Point3DCollection, Vector3DCollection, Int32Collection, MeshGeometry3D, MeshGeometry3D, List <BillboardTextItem> >(SendGraphicsToView), DispatcherPriority.Render, new object[] { points, pointsSelected, lines, linesSelected, redLines, greenLines, blueLines, verts, norms, tris, vertsSel, normsSel, trisSel, mesh, meshSel, text }); }
private void CreateSurface() { if (this.Points != null && this.TextureBuilder != null) { Material texture = this.TextureBuilder.CreateTexture(); int xWidth = this.Points.GetLength(0); int yWidth = this.Points.GetLength(1); int capacity = (xWidth - 1) * (yWidth - 1); Point3DCollection positions = new Point3DCollection(capacity); Int32Collection indices = new Int32Collection(capacity); PointCollection texCoords = new PointCollection(capacity); Vector3DCollection normals = new Vector3DCollection(capacity); int indiceCount = 0; for (int ix = 0; ix < xWidth - 1; ix++) { if (!double.IsNaN(this.Points[ix, 0].Z)) { if (!double.IsNaN(this.Points[ix + 1, 0].Z)) { for (int iy = 0; iy < yWidth - 1; iy++) { // V0-----V3 // | | // | | // V1-----V2 //Add Triangle V0--V1--V2 positions.Add(this.Points[ix, iy]); positions.Add(this.Points[ix + 1, iy]); positions.Add(this.Points[ix + 1, iy + 1]); double middleZ = (this.Points[ix, iy].Z + this.Points[ix + 1, iy].Z + this.Points[ix + 1, iy + 1].Z + this.Points[ix, iy + 1].Z) / 4.0; Point texturePt = this.TextureBuilder.GetTextureMapping(middleZ); texCoords.Add(texturePt); texCoords.Add(texturePt); texCoords.Add(texturePt); indices.Add(indiceCount++); indices.Add(indiceCount++); indices.Add(indiceCount++); Vector3D normal = MathHelper.CalculateNormal(this.Points[ix + 1, iy + 1], this.Points[ix + 1, iy], this.Points[ix, iy]); normals.Add(normal); normals.Add(normal); normals.Add(normal); //Add Triangle V2--V3-V0 positions.Add(this.Points[ix + 1, iy + 1]); positions.Add(this.Points[ix, iy + 1]); positions.Add(this.Points[ix, iy]); texCoords.Add(texturePt); texCoords.Add(texturePt); texCoords.Add(texturePt); indices.Add(indiceCount++); indices.Add(indiceCount++); indices.Add(indiceCount++); Vector3D normal2 = MathHelper.CalculateNormal(this.Points[ix, iy], this.Points[ix, iy + 1], this.Points[ix + 1, iy + 1]); normals.Add(normal2); normals.Add(normal2); normals.Add(normal2); } } } } positions.Freeze(); _mesh.Positions = positions; indices.Freeze(); _mesh.TriangleIndices = indices; texCoords.Freeze(); _mesh.TextureCoordinates = texCoords; normals.Freeze(); _mesh.Normals = normals; _model3D.Material = texture; _model3D.BackMaterial = texture; } }
private static Model3D GenerateTreeMap3DModel(int index, int count) { MeshGeometry3D meshGeometry3D = new MeshGeometry3D(); Point3DCollection positions = new Point3DCollection(); positions.Add(new Point3D(0, 0, 1)); positions.Add(new Point3D(0, 0, 0)); positions.Add(new Point3D(1, 0, 0)); positions.Add(new Point3D(1, 0, 1)); positions.Add(new Point3D(0, 1, 1)); positions.Add(new Point3D(0, 1, 0)); positions.Add(new Point3D(1, 1, 0)); positions.Add(new Point3D(1, 1, 1)); positions.Freeze(); Int32Collection triangleIndices = new Int32Collection(); triangleIndices.Add(0); triangleIndices.Add(1); triangleIndices.Add(2); triangleIndices.Add(2); triangleIndices.Add(3); triangleIndices.Add(0); triangleIndices.Add(4); triangleIndices.Add(7); triangleIndices.Add(6); triangleIndices.Add(6); triangleIndices.Add(5); triangleIndices.Add(4); triangleIndices.Add(0); triangleIndices.Add(3); triangleIndices.Add(7); triangleIndices.Add(7); triangleIndices.Add(4); triangleIndices.Add(0); triangleIndices.Add(1); triangleIndices.Add(5); triangleIndices.Add(6); triangleIndices.Add(6); triangleIndices.Add(2); triangleIndices.Add(1); triangleIndices.Add(3); triangleIndices.Add(2); triangleIndices.Add(6); triangleIndices.Add(6); triangleIndices.Add(7); triangleIndices.Add(3); triangleIndices.Add(0); triangleIndices.Add(4); triangleIndices.Add(5); triangleIndices.Add(5); triangleIndices.Add(7); triangleIndices.Add(0); triangleIndices.Freeze(); // finally set the data meshGeometry3D.TriangleIndices = triangleIndices; meshGeometry3D.Positions = positions; // create the geometry model GeometryModel3D geom3D = new GeometryModel3D(); geom3D.Geometry = meshGeometry3D; Color color = ColorHelper.HsbToRgb(index / (float)count, .9f, 1f); SolidColorBrush solidColorBrush = color.ToBrush(); solidColorBrush.Freeze(); geom3D.Material = new DiffuseMaterial(solidColorBrush); return(geom3D); }
public Transition3D() : base(new Viewport3D()) { // camera to ue WrappedElement.Camera = new PerspectiveCamera(); // the model visual 3D ModelVisual3D mv3D = new ModelVisual3D(); mv3D.Content = new PointLight(Colors.White, new Point3D(0, 0, 0)); WrappedElement.Children.Add(mv3D); MeshGeometry3D plane = new MeshGeometry3D(); Point3DCollection positions = new Point3DCollection(); positions.Add(new Point3D(-1, -1, 0)); positions.Add(new Point3D(-1, 1, 0)); positions.Add(new Point3D(1, 1, 0)); positions.Add(new Point3D(1, -1, 0)); positions.Freeze(); plane.Positions = positions; PointCollection textureCoords = new PointCollection(); textureCoords.Add(new Point(0, 1)); textureCoords.Add(new Point(0, 0)); textureCoords.Add(new Point(1, 0)); textureCoords.Add(new Point(1, 1)); textureCoords.Freeze(); plane.TextureCoordinates = textureCoords; Int32Collection indices = new Int32Collection(); indices.Add(0); indices.Add(3); indices.Add(1); indices.Add(1); indices.Add(3); indices.Add(2); indices.Freeze(); plane.TriangleIndices = indices; Material planeMaterial = new DiffuseMaterial(Brushes.Blue); planeMaterial.SetValue(Viewport2DVisual3D.IsVisualHostMaterialProperty, true); m_visual3D = new Viewport2DVisual3D(); m_visual3D.Geometry = plane; m_visual3D.Material = planeMaterial; Transform3DGroup transform = new Transform3DGroup(); m_rotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 35); m_scale = new ScaleTransform3D(0, 0, 0); m_translation = new TranslateTransform3D(-2.5, 0, -10); transform.Children.Add(m_scale); transform.Children.Add(new RotateTransform3D(m_rotation)); transform.Children.Add(m_translation); m_visual3D.Transform = transform; WrappedElement.Children.Add(m_visual3D); }
/// <summary> /// Use the render packages returned from the visualization manager to update the visuals. /// The visualization event arguments will contain a set of render packages and an id representing /// the associated node. Visualizations for the background preview will return an empty id. /// </summary> /// <param name="e"></param> private void RenderDrawables(VisualizationEventArgs e) { //check the id, if the id is meant for another watch, //then ignore it if (e.Id != _id) { return; } var sw = new Stopwatch(); sw.Start(); Points = null; Lines = null; Mesh = null; XAxes = null; YAxes = null; ZAxes = null; PointsSelected = null; LinesSelected = null; MeshSelected = null; Text = null; MeshCount = 0; //separate the selected packages var packages = e.Packages.Where(x => x.Selected == false).ToArray(); var selPackages = e.Packages.Where(x => x.Selected).ToArray(); //pre-size the points collections var pointsCount = packages.Select(x => x.PointVertices.Count / 3).Sum(); var selPointsCount = selPackages.Select(x => x.PointVertices.Count / 3).Sum(); var points = new Point3DCollection(pointsCount); var pointsSelected = new Point3DCollection(selPointsCount); //pre-size the lines collections //these sizes are conservative as the axis lines will be //taken from the linestripvertex collections as well. var lineCount = packages.Select(x => x.LineStripVertices.Count / 3).Sum(); var lineSelCount = selPackages.Select(x => x.LineStripVertices.Count / 3).Sum(); var lines = new Point3DCollection(lineCount); var linesSelected = new Point3DCollection(lineSelCount); var redLines = new Point3DCollection(lineCount); var greenLines = new Point3DCollection(lineCount); var blueLines = new Point3DCollection(lineCount); //pre-size the text collection var textCount = e.Packages.Count(x => x.DisplayLabels); var text = new List <BillboardTextItem>(textCount); //http://blogs.msdn.com/b/timothyc/archive/2006/08/31/734308.aspx //presize the mesh collections var meshVertCount = packages.Select(x => x.TriangleVertices.Count / 3).Sum(); var meshVertSelCount = selPackages.Select(x => x.TriangleVertices.Count / 3).Sum(); var mesh = new MeshGeometry3D(); var meshSel = new MeshGeometry3D(); var verts = new Point3DCollection(meshVertCount); var vertsSel = new Point3DCollection(meshVertSelCount); var norms = new Vector3DCollection(meshVertCount); var normsSel = new Vector3DCollection(meshVertSelCount); var tris = new Int32Collection(meshVertCount); var trisSel = new Int32Collection(meshVertSelCount); foreach (var package in packages) { ConvertPoints(package, points, text); ConvertLines(package, lines, redLines, greenLines, blueLines, text); ConvertMeshes(package, verts, norms, tris); } foreach (var package in selPackages) { ConvertPoints(package, pointsSelected, text); ConvertLines(package, linesSelected, redLines, greenLines, blueLines, text); ConvertMeshes(package, vertsSel, normsSel, trisSel); } points.Freeze(); pointsSelected.Freeze(); Points = points; PointsSelected = pointsSelected; lines.Freeze(); linesSelected.Freeze(); redLines.Freeze(); greenLines.Freeze(); blueLines.Freeze(); Lines = lines; LinesSelected = linesSelected; XAxes = redLines; YAxes = greenLines; ZAxes = blueLines; verts.Freeze(); norms.Freeze(); tris.Freeze(); vertsSel.Freeze(); normsSel.Freeze(); trisSel.Freeze(); mesh.Positions = verts; mesh.Normals = norms; mesh.TriangleIndices = tris; meshSel.Positions = vertsSel; meshSel.Normals = normsSel; meshSel.TriangleIndices = trisSel; Mesh = mesh; MeshSelected = meshSel; Text = text; sw.Stop(); GC.Collect(); Debug.WriteLine(string.Format("{0} ellapsed for updating background preview.", sw.Elapsed)); }