2010-03-06 56 views
2

我試圖在代碼中添加一個Viewport2DVisual3D到Viewport3D,但視覺不顯示。任何幫助理解爲什麼不會感激。以下是主窗口的代碼。問題從代碼中添加Viewport2DVisual3D

只需將Viewport2DVisual3D添加到Viewport3D的子項以便呈現它就足夠了嗎?

public partial class Window1 : System.Windows.Window 
{ 

    public Window1() 
    { 
     InitializeComponent(); 

     this.Loaded += new RoutedEventHandler(temp); 
    } 


    public void temp(object sender, RoutedEventArgs e) 
    { 
     Viewport2DVisual3D test = new Viewport2DVisual3D(); 
     MeshGeometry3D testGeometry = new MeshGeometry3D(); 

     Vector3D CameraLookDirection = Main_Target_CameraOR20.LookDirection; 

     // Calculate the Positions based on the Camera 
     Point3DCollection myPoint3DCollection = new Point3DCollection(); 
     myPoint3DCollection.Add(new Point3D(-1, 1, 0)); 
     myPoint3DCollection.Add(new Point3D(-1, -1, 0)); 
     myPoint3DCollection.Add(new Point3D(1, -1, 0)); 
     myPoint3DCollection.Add(new Point3D(1, 1, 0)); 
     testGeometry.Positions = myPoint3DCollection; 

     PointCollection myPointCollection = new PointCollection(); 
     myPointCollection.Add(new Point(0, 0)); 
     myPointCollection.Add(new Point(0, 1)); 
     myPointCollection.Add(new Point(1, 1)); 
     myPointCollection.Add(new Point(1, 0)); 
     testGeometry.TextureCoordinates = myPointCollection; 

     Int32Collection triangleIndicesCollection = new Int32Collection(); 
     triangleIndicesCollection.Add(0); 
     triangleIndicesCollection.Add(1); 
     triangleIndicesCollection.Add(2); 
     triangleIndicesCollection.Add(2); 
     triangleIndicesCollection.Add(3); 
     triangleIndicesCollection.Add(0); 
     testGeometry.TriangleIndices = triangleIndicesCollection; 


     DiffuseMaterial myDiffuseMaterial = new DiffuseMaterial(Brushes.White); 
     Viewport2DVisual3D.SetIsVisualHostMaterial(myDiffuseMaterial, true); 

     Transform3DGroup myTransform3DGroup = new Transform3DGroup(); 
     ScaleTransform3D myScaleTransform3D = new ScaleTransform3D(); 
     myScaleTransform3D.ScaleX = 2; 
     myScaleTransform3D.ScaleY = 2; 
     myScaleTransform3D.ScaleZ = 2; 

     TranslateTransform3D myTranslateTransform3D = new TranslateTransform3D(); 

     myTranslateTransform3D.OffsetX = -27; 
     myTranslateTransform3D.OffsetY = 13; 
     myTranslateTransform3D.OffsetZ = 6; 


     RotateTransform3D rotateTransform = new RotateTransform3D() 
     { 
      Rotation = new AxisAngleRotation3D 
      { 
       Angle = -50, 
       Axis = new Vector3D(0, 1, 0) 
      } 
     }; 

     myTransform3DGroup.Children.Add(myTranslateTransform3D); 
     myTransform3DGroup.Children.Add(myScaleTransform3D); 
     myTransform3DGroup.Children.Add(rotateTransform); 
     test.Transform = myTransform3DGroup; 

     Button myButton = new Button(); 
     myButton.Content = "Test Button"; 

     test.Material = myDiffuseMaterial; 
     test.Geometry = testGeometry; 
     test.Visual = myButton; 

     ZAM3DViewport3D.Children.Add(test); 
    } 
} 

回答

0

事實證明,問題是偏移值。所以,只需將該子項添加到Viewport3D即可呈現它。乾杯