wpf从cs代码创建简单3D物体和3D Tools研究

前面已经说了,WPF项目中引入3DTools dll之后,在xaml中加入它的命名空间,

xmlns:tools="clr-namespace:_3DTools;assembly=3DTools"

把<Viewport3D>标签包含在<tools:TrackballDecorator>标签之中,就可以用鼠标控制 <Viewport3D> 中的3d模型;

看一下单独用3DTools dll能否加载3d模型;没有资料;

用ILSpy看一下;

直接从3DTools包含的类,类名看,有数学工具类,可能提供一些矩阵运算;有前面的TrackballDecorator类;还有碰撞检测类,hit;看不出是否有加载3d模型的类;有时间再看;

之前做过一些wpf 简单3D物体,都是直接写在xaml代码里;

下面从cs代码里做一下简单3D物体;

在xaml中给3d根节点加上名称属性,这样可以在cs代码中引用此节点;

<Viewport3D Name="v3">......</Viewport3D>

xaml代码如下;

XML 复制代码
<Window x:Class="mytest1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tools="clr-namespace:_3DTools;assembly=3DTools"
        Title="MainWindow" Height="350" Width="525">
    <Grid Width="300" Height="200" ShowGridLines="True">
        <tools:TrackballDecorator>
        <Viewport3D Name="v3">
            
        </Viewport3D>
        </tools:TrackballDecorator>
    </Grid>
</Window>

cs代码如下;

XML 复制代码
using System;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;

namespace mytest1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // 创建一个3D正方体
            MeshGeometry3D mesh = new MeshGeometry3D();
            mesh.Positions = new Point3DCollection()
            {
                new Point3D(-1, 1, 1),
                new Point3D(1, 1, 1),
                new Point3D(-1, -1, 1),
                new Point3D(1, -1, 1),
                new Point3D(-1, 1, -1),
                new Point3D(1, 1, -1),
                new Point3D(-1, -1, -1),
                new Point3D(1, -1, -1),
            };
            mesh.TriangleIndices = new Int32Collection()
            {
                0, 1, 2,
                1, 3, 2,
                1, 5, 3,
                5, 7, 3,
                5, 4, 7,
                4, 6, 7,
                4, 0, 6,
                0, 2, 6,
                2, 3, 6,
                3, 7, 6,
                4, 5, 0,
                5, 1, 0,
            };

            // 创建一个材质
            DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));

            // 创建一个模型
            GeometryModel3D model = new GeometryModel3D(mesh, material);

            // 创建一个模型组
            Model3DGroup group = new Model3DGroup();
            group.Children.Add(model);

            // 将模型组添加到Viewport3D中
            v3.Children.Add(new ModelVisual3D() { Content = group });

            PerspectiveCamera camera = new PerspectiveCamera();
            camera.Position = new Point3D(0, 0, 5);
            camera.LookDirection = new Vector3D(0, 0, -1);
            camera.FieldOfView = 60;
            v3.Camera = camera;
        }
    }
}

从cs代码中创建一个立方体模型;摄像机也在cs代码中创建;然后加入名为"v3"的节点;

运行如下;

顺带看一下ILSpy的许可证,它是MIT许可;

MIT许可证(The MIT License)是许多软件授权条款中,被广泛使用的其中一种;与其他常见的软件授权条款(如GPL、LGPL、BSD)相比,MIT是相对宽松的软件授权条款;

MIT内容与三条款BSD许可证(3-clause BSD license)内容颇为近似,但是赋予软件被授权人更大的权利与更少的限制;

相关推荐
廋到被风吹走10 小时前
【Spring】Spring AMQP 详细介绍
java·spring·wpf
kylezhao201918 小时前
WinForm/WPF界面开发(常用控件、布局设计、事件处理)
大数据·hadoop·wpf
Psycho_MrZhang20 小时前
REST/gRPC/队列通信模式对比
wpf
墨白曦煜1 天前
Seata AT 模式:应用层回滚 vs 引擎层回滚
wpf
@淡 定3 天前
分布式事务解决方案
分布式·wpf
棉晗榜3 天前
WPF将程序集里面嵌入的资源文件下载到本机磁盘中,将项目中的文件下载到桌面
开发语言·wpf
△曉風殘月〆3 天前
WPF MVVM实战系列教程(一、Prism框架介绍)
wpf·mvvm·prism
Aevget3 天前
DevExpress WPF中文教程:Data Grid - 如何绑定到有限制的自定义服务(三)?
wpf·界面控件·devexpress·ui开发·.net 10
△曉風殘月〆3 天前
WPF MVVM实战系列教程(二、使用Visual Studio 创建Prism项目)
wpf·mvvm·prism