WPF:画布(XAML)内的Viewport3D(C#)

时间:2018-07-09 03:54:06

标签: c# wpf xaml viewport3d

我是WPF的初学者,所以我不完全了解C#如何与XAML一起制作应用程序。

我正在尝试制作一个用于绘制和操纵3D几何图形的软件。我已经按照this example的操作来创建3D场景,并且操作成功。

但是,当我尝试将Viewport3D放在XAML的canvas内时(以便可以将其他元素(如按钮)放置在UI中以操纵3D场景),找出为什么Viewport3D不显示的原因。

我不想在XAML中定义Viewport3D的所有内容,因为我计划进行更复杂的图形绘制和几何操作。而且我认为最好使用C#。

C#文件中的MainWindow与XAML文件中的MainWindow不同吗?

这是我得到的窗口:

enter image description here

代码如下:

XAML

<Window x:Name="Geology3D_MainWindow" x:Class="Geology3D.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Geology3D"
        mc:Ignorable="d"
        Title="Geology3D" Height="600" Width="800" Background="Silver">
    <Canvas x:Name="Canvas3D" Background="White" Margin="10,29,16.6,7.4">
        <Viewport3D x:Name="ViewPort3D">

        </Viewport3D>
    </Canvas>
</Window>

C#

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Geology3D
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //Viewport3D ViewPort3D = new Viewport3D();

            Model3DGroup ModelsGroup = new Model3DGroup();
            ModelVisual3D GModelVisual3D = new ModelVisual3D();

            GeometryModel3D CubeGeometryModel = new GeometryModel3D();
            GeometryModel3D PlaneGeometryModel = new GeometryModel3D();

            PerspectiveCamera Camera = new PerspectiveCamera();

            // Specify where in the 3D scene the camera is.
            Camera.Position = new Point3D(-5, 5, -5);

            // Specify the direction that the camera is pointing.
            Camera.LookDirection = new Vector3D(1, -1, 1);

            // Asign the camera to the viewport
            ViewPort3D.Camera = Camera;

            // Define the lights cast in the scene. Without light, the 3D object cannot 
            // be seen. Note: to illuminate an object from additional directions, create 
            // additional lights.
            DirectionalLight GDirectionalLight = new DirectionalLight();
            GDirectionalLight.Color = Colors.White;
            GDirectionalLight.Direction = new Vector3D(-1, -1, -1);

            ModelsGroup.Children.Add(GDirectionalLight);

            // The material specifies the material applied to the 3D object.
            // Define material and apply to the mesh geometries.
            DiffuseMaterial BlueMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.DodgerBlue));
            DiffuseMaterial PlaneMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Gray));

            CubeGeometryModel.Material = BlueMaterial;
            PlaneGeometryModel.Material = PlaneMaterial;

            // Apply the mesh to the geometry model.
            CubeGeometryModel.Geometry = DrawCube();
            PlaneGeometryModel.Geometry = DrawPlane();

            // Apply a transform to the object. In this sample, a rotation transform is applied,  
            // rendering the 3D object rotated.
            RotateTransform3D GRotateTransform3D = new RotateTransform3D();
            AxisAngleRotation3D GAxisAngleRotation3d = new AxisAngleRotation3D();
            GAxisAngleRotation3d.Axis = new Vector3D(0, 1, 0);
            GAxisAngleRotation3d.Angle = 0;
            GRotateTransform3D.Rotation = GAxisAngleRotation3d;
            CubeGeometryModel.Transform = GRotateTransform3D;

            // Add the geometry model to the model group.
            ModelsGroup.Children.Add(CubeGeometryModel);
            ModelsGroup.Children.Add(PlaneGeometryModel);

            // Add the group of models to the ModelVisual3d.
            GModelVisual3D.Content = ModelsGroup;

            ViewPort3D.Children.Add(GModelVisual3D);
        }

        MeshGeometry3D DrawCube()[...]

        MeshGeometry3D DrawPlane()[...]

    }
}

1 个答案:

答案 0 :(得分:0)

如果打算覆盖UI元素,则可能更容易将画布和Viewport3D嵌套在网格中。

<Grid>
    <Viewport3D>
    ...
    </Viewport3D>
    <Canvas/>
</Grid>

这样,视口和画布将始终具有相同的大小,并且添加到画布中的元素将始终位于顶部。