文章目录
前言
一、HelixToolkit安装
二、使用步骤
1.引入库
2.xmal代码设置
3.后端程序
4.3D显示
总结
前言
3D显示、旋转、部件移动(位置获取)
开发一个可导入.stl文件,可视化3D显示模型,部件之间鼠标移动、键盘设置位置等。
WPF开发库HelixToolkit
一、HelixToolkit安装
二、使用步骤
1.引入库
引入库命名空间
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:lab2._3D"
xmlns:hv="http://helix-toolkit.org/wpf" <!-- 添加helixToolkit命名空间-->
mc:Ignorable="d"
Title="Window3D" Height="450" Width="800">
2.xmal代码设置
UI设置3D光源、相机、网格等信息
<Window x:Class="lab2._3D.Window3D"
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:lab2._3D"
xmlns:hv="http://helix-toolkit.org/wpf"
mc:Ignorable="d"
Title="Window3D" Height="450" Width="800">
<Grid>
<!--加载模型:设置光源、相机、坐标网格-->
<hv:HelixViewport3D Name="viewPort3D" ZoomExtentsWhenLoaded="True"
ShowViewCube="false" ShowCoordinateSystem="True"
CoordinateSystemLabelForeground="White"
CoordinateSystemVerticalPosition="Center"
CoordinateSystemHorizontalPosition="Right"
CoordinateSystemHeight="50"
CoordinateSystemWidth="50"
RenderOptions.EdgeMode="Unspecified"
BorderBrush="White"
BorderThickness="0"
ShowFrameRate="False"
ShowCameraInfo="True"
IsManipulationEnabled="True"
Background="Transparent"
>
<!-- Remember to add light to the scene -->
<hv:HelixViewport3D.DefaultCamera>
<PerspectiveCamera LookDirection="-587.475,-330.619,-229.365" Position="587.475,330.619,256.278" UpDirection="-0.248,-0.139,0.959" FieldOfView="45" NearPlaneDistance="0.1"/>
</hv:HelixViewport3D.DefaultCamera>
<hv:SunLight/>
<hv:GridLinesVisual3D Width="1000" Length="1000" MinorDistance="50" MajorDistance="50" Thickness="1" Fill="#E5CFCECE"/>
</hv:HelixViewport3D>
</Grid>
</Window>
3.后端程序
模型导入。
using HelixToolkit.Wpf;
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.Media.Media3D;
using System.Windows.Shapes;
namespace lab2._3D
{
/// <summary>
/// Window3D.xaml 的交互逻辑
/// </summary>
public partial class Window3D : Window
{
public Window3D()
{
InitializeComponent();
//模型加载
string modelPath1 = AppDomain.CurrentDomain.BaseDirectory + "Corvus_Building_Lower_NO_Windows.stl"; //模型地址
ModelImporter import = new ModelImporter();
var initGroup1 = import.Load(modelPath1);
string modelPath2 = AppDomain.CurrentDomain.BaseDirectory + "Corvus_Building_Roof_V2.stl";
var initGroup2 = import.Load(modelPath2);
材质
//GeometryModel3D geometryModel3D = initGroup.Children[0] as GeometryModel3D;
//DiffuseMaterial diffMat = new DiffuseMaterial(new SolidColorBrush(Colors.White));
//geometryModel3D.Material = diffMat;
//ViewPort3D进行显示
ModelVisual3D modelVisual3d1 = new ModelVisual3D();
modelVisual3d1.Content = initGroup1;
viewPort3D.Children.Add(modelVisual3d1);
ModelVisual3D modelVisual3d2= new ModelVisual3D();
modelVisual3d2.Content = initGroup2;
viewPort3D.Children.Add(modelVisual3d2);
}
}
}
4.3D显示
鼠标滚轮实现模型放大与缩小;按住鼠标右键拖动实现模型方位调节。
文章来源:https://www.toymoban.com/news/detail-528728.html
总结
helixToolkit工具实现3D模型加载到WPF,开发流程效率大大提高。后期实现模型部件运动控制。。。文章来源地址https://www.toymoban.com/news/detail-528728.html
到了这里,关于WPF 插件HelixToolkit库实现3D显示的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!