protected ModelVisual3D MakeVisualModel(LoadResult objmesh, ZipArchiveEntry texture) { var r = new ModelVisual3D(); r.Content = MakeModel(objmesh, texture); Points.Freeze(); // We won't change them anymore Normals.Freeze(); Indices.Freeze(); TexCoords.Freeze(); r.Transform = new Transform3DGroup(); Transform3DGroup transformGroup = r.Transform as Transform3DGroup; TranslateTransform3D translation = new TranslateTransform3D( -(r.Content.Bounds.X + r.Content.Bounds.SizeX / 2), -(r.Content.Bounds.Y + r.Content.Bounds.SizeY / 2), -(r.Content.Bounds.Z + r.Content.Bounds.SizeZ / 2) ); transformGroup.Children.Add(translation); double scale = Math.Abs(1 / (r.Content.Bounds.SizeX)); scale = Math.Min(scale, Math.Abs(1 / (r.Content.Bounds.SizeY))); scale = Math.Min(scale, Math.Abs(1 / (r.Content.Bounds.SizeZ))); ScaleTransform3D scaletr = new ScaleTransform3D(scale, scale, scale); transformGroup.Children.Add(scaletr); return(r); }
private void SendGraphicsToView(Point3DCollection points, Point3DCollection pointsSelected, Point3DCollection lines, Point3DCollection linesSelected, Point3DCollection redLines, Point3DCollection greenLines, Point3DCollection blueLines, Point3DCollection verts, Vector3DCollection norms, Int32Collection tris, Point3DCollection vertsSel, Vector3DCollection normsSel, Int32Collection trisSel, MeshGeometry3D mesh, MeshGeometry3D meshSel, List <BillboardTextItem> text) { 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(); Points = points; PointsSelected = pointsSelected; Lines = lines; LinesSelected = linesSelected; XAxes = redLines; YAxes = greenLines; ZAxes = blueLines; mesh.Positions = verts; mesh.Normals = norms; mesh.TriangleIndices = tris; meshSel.Positions = vertsSel; meshSel.Normals = normsSel; meshSel.TriangleIndices = trisSel; Mesh = mesh; MeshSelected = meshSel; Text = text; }
/// <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; } }
/// <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)); }