首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Viewport3D (C#)在画布中(XAML)

Viewport3D (C#)在画布中(XAML)
EN

Stack Overflow用户
提问于 2018-07-09 03:54:06
回答 1查看 3.5K关注 0票数 0

我是WPF的初学者,所以我不太明白C#是如何与XAML一起工作的。

我试图制作一个软件来绘制和操作三维几何。我跟踪了这个例子关于如何创建一个3D场景,并取得了成功。

但是,当我试图将Viewport3D放在XAML中的canvas中(以便在UI中放置按钮等其他元素来操作3D场景)时,我无法理解为什么Viewport3D没有出现。

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

来自MainWindow文件的C#文件在XAML文件上不是相同的吗?

这是我看到的窗户:

以下是代码:

XAML

代码语言:javascript
运行
复制
<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#

代码语言:javascript
运行
复制
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()[...]

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-09 04:33:12

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

代码语言:javascript
运行
复制
<Grid>
    <Viewport3D>
    ...
    </Viewport3D>
    <Canvas/>
</Grid>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51238016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档