/// <summary> /// Updates the model for this shape. /// </summary> /// <remarks> /// The model is determined by what needs to be displayed. If no /// interactive visual needs to be displayed, then a simple /// ModelVisual3D is used to display a GeometryModel3D. If an /// interactive visual needs to be displayed, then a /// Viewport2DVisual3D is used. /// </remarks> protected override void OnUpdateModel() { MeshGeometry3D geometry = GetGeometry(); for (int i = 0; i < geometry.Positions.Count; i++) { geometry.Positions[i] = MeshTransform.Transform(geometry.Positions[i]); } Size surfaceSize = geometry.CalculateIdealTextureSize(); SurfaceWidth = surfaceSize.Width; SurfaceHeight = surfaceSize.Height; MeshGeometry3D frontGeometry = null; Shape3DMaterial frontMaterial = FrontMaterial; if (frontMaterial != null) { frontGeometry = geometry; if (frontGeometry == null) { frontMaterial = null; } } MeshGeometry3D backGeometry = null; Shape3DMaterial backMaterial = BackMaterial; if (backMaterial != null) { backGeometry = CreateBackGeometry(geometry); if (backGeometry == null) { backMaterial = null; } } EnsureChildren(frontMaterial, backMaterial); UpdateChild(_frontChild, frontGeometry, frontMaterial); UpdateChild(_backChild, backGeometry, backMaterial); return; }
private void UpdateChild(Visual3D child, MeshGeometry3D geometry, Shape3DMaterial material) { if (material != null) { if (material.HasElement) { Viewport2DVisual3D viewport = (Viewport2DVisual3D)child; viewport.Geometry = geometry; viewport.Material = HostingMaterial; // Set the size of the root element to the surface size. Border border = (Border)viewport.Visual; border.Width = SurfaceWidth; border.Height = SurfaceHeight; border.Child = material.GetElement(this); // Add the material. //Viewbox viewbox = (Viewbox)border.Child; //viewbox.Width = SurfaceWidth; //viewbox.Height = SurfaceHeight; //viewbox.Child = material.GetElement(this); // Set the mesh into the visualizer. //viewbox.Child = new MeshTextureCoordinateVisualizer(); //((MeshTextureCoordinateVisualizer)viewbox.Child).Mesh = geometry; } else { Debug.Assert(material.HasMaterial); GeometryModel3D model = (GeometryModel3D)((ModelVisual3D)child).Content; model.Geometry = geometry; model.Material = material.GetMaterial(this); } } }
private void EnsureChildren(Shape3DMaterial frontMaterial, Shape3DMaterial backMaterial) { Visual3D frontChild = EnsureChild(_frontChild, frontMaterial); if (frontChild != _frontChild) { if (_frontChild != null) { RemoveVisual3DChild(_frontChild); } _frontChild = frontChild; if (_frontChild != null) { AddVisual3DChild(_frontChild); } } Visual3D backChild = EnsureChild(_backChild, backMaterial); if (backChild != _backChild) { if (_backChild != null) { RemoveVisual3DChild(_backChild); } _backChild = backChild; if (_backChild != null) { AddVisual3DChild(_backChild); } } }
private Visual3D EnsureChild(Visual3D currentChild, Shape3DMaterial material) { Visual3D newChild = null; if (material != null) { if (material.HasElement) { // We need a Viewport2DVisual3D to display an element. if (currentChild is Viewport2DVisual3D) { newChild = currentChild; } else { //Viewbox viewbox = new Viewbox(); //viewbox.StretchDirection = StretchDirection.Both; //viewbox.Stretch = Stretch.Fill; Border border = new Border(); border.UseLayoutRounding = true; border.Background = Brushes.Green; //border.Child = viewbox; Viewport2DVisual3D viewport = new Viewport2DVisual3D(); viewport.Visual = border; newChild = viewport; } // Set the appropriate caching strategy. CacheScale cacheScale = CacheScale; if (cacheScale == null) { // Remove any VisualBrush caching. RenderOptions.SetCachingHint(newChild, CachingHint.Unspecified); // Remove any BitmapCache. ((Viewport2DVisual3D)newChild).CacheMode = null; } else if (cacheScale.IsAuto) { // Remove any BitmapCache. ((Viewport2DVisual3D)newChild).CacheMode = null; // Specify VisualBrush caching with 2x min and max // thresholds. RenderOptions.SetCachingHint(newChild, CachingHint.Cache); RenderOptions.SetCacheInvalidationThresholdMinimum(newChild, 0.5); RenderOptions.SetCacheInvalidationThresholdMaximum(newChild, 2.0); } else { // Remove any VisualBrush caching. RenderOptions.SetCachingHint(newChild, CachingHint.Unspecified); // Set a BitmapCache with the appropriate scale. BitmapCache bitmapCache = ((Viewport2DVisual3D)newChild).CacheMode as BitmapCache; if (bitmapCache == null) { ((Viewport2DVisual3D)newChild).CacheMode = new BitmapCache(cacheScale.Scale); } else { bitmapCache.RenderAtScale = cacheScale.Scale; } } } else { Debug.Assert(material.HasMaterial); // We need a ModelVisual3D to display the material. if (currentChild is ModelVisual3D) { Debug.Assert(((ModelVisual3D)currentChild).Content is GeometryModel3D); newChild = currentChild; } else { newChild = new ModelVisual3D(); ((ModelVisual3D)newChild).Content = new GeometryModel3D(); } } } return(newChild); }