许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  wpf从cs代码创建简单3D物体和3D Tools研究

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

阅读数 6
点赞 0
article_banner

前面已经说了,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代码如下;

  1. <Window x:Class="mytest1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:tools="clr-namespace:_3DTools;assembly=3DTools"
  5. Title="MainWindow" Height="350" Width="525">
  6. <Grid Width="300" Height="200" ShowGridLines="True">
  7. <tools:TrackballDecorator>
  8. <Viewport3D Name="v3">
  9. </Viewport3D>
  10. </tools:TrackballDecorator>
  11. </Grid>
  12. </Window>
XML

cs代码如下;

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Media.Media3D;
  16. namespace mytest1
  17. {
  18. /// <summary>
  19. /// MainWindow.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. // 创建一个3D正方体
  27. MeshGeometry3D mesh = new MeshGeometry3D();
  28. mesh.Positions = new Point3DCollection()
  29. {
  30. new Point3D(-1, 1, 1),
  31. new Point3D(1, 1, 1),
  32. new Point3D(-1, -1, 1),
  33. new Point3D(1, -1, 1),
  34. new Point3D(-1, 1, -1),
  35. new Point3D(1, 1, -1),
  36. new Point3D(-1, -1, -1),
  37. new Point3D(1, -1, -1),
  38. };
  39. mesh.TriangleIndices = new Int32Collection()
  40. {
  41. 0, 1, 2,
  42. 1, 3, 2,
  43. 1, 5, 3,
  44. 5, 7, 3,
  45. 5, 4, 7,
  46. 4, 6, 7,
  47. 4, 0, 6,
  48. 0, 2, 6,
  49. 2, 3, 6,
  50. 3, 7, 6,
  51. 4, 5, 0,
  52. 5, 1, 0,
  53. };
  54. // 创建一个材质
  55. DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));
  56. // 创建一个模型
  57. GeometryModel3D model = new GeometryModel3D(mesh, material);
  58. // 创建一个模型组
  59. Model3DGroup group = new Model3DGroup();
  60. group.Children.Add(model);
  61. // 将模型组添加到Viewport3D中
  62. v3.Children.Add(new ModelVisual3D() { Content = group });
  63. PerspectiveCamera camera = new PerspectiveCamera();
  64. camera.Position = new Point3D(0, 0, 5);
  65. camera.LookDirection = new Vector3D(0, 0, -1);
  66. camera.FieldOfView = 60;
  67. v3.Camera = camera;
  68. }
  69. }
  70. }
XML

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

运行如下;

 


       

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

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


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空