从Code中添加Viewport2DVisual3D时出现问题

时间:2010-03-06 00:57:04

标签: wpf

我正在尝试将Viewport2DVisual3D添加到代码中的Viewport3D,但视觉效果未显示。任何帮助理解为什么不会被赞赏。以下是主窗口的代码。

将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);
    }
}

1 个答案:

答案 0 :(得分:0)

事实证明问题是偏移值。因此,将子项添加到Viewport3D以使其呈现就足够了。干杯