目录
1. 环境
2. NuGet导入依赖
3. 编辑MainWindow.xaml.cs
4. 编辑MainWindow.xaml
源码:https://github.com/liugang198409/WpfDemo/tree/master/3DDemo
视频:WPF编程--3D Demo_哔哩哔哩_bilibili
stl文件:https://download.csdn.net/download/liugang590/87426104
1. 环境
Visual Studio 2019 + .NET Framework 4.8.1
2. NuGet导入依赖
HelixToolkit
HelixToolkit.Wpf文章来源:https://www.toymoban.com/news/detail-564868.html
文章来源地址https://www.toymoban.com/news/detail-564868.html
3. 编辑MainWindow.xaml.cs
using HelixToolkit.Wpf;
using System;
using System.Windows;
using System.Windows.Media.Media3D;
namespace _3DDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//模型加载
string modelPath1 = AppDomain.CurrentDomain.BaseDirectory + "A.stl"; //模型地址
ModelImporter import = new ModelImporter();
var initGroup1 = import.Load(modelPath1);
string modelPath2 = AppDomain.CurrentDomain.BaseDirectory + "B.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. 编辑MainWindow.xaml
<Window x:Class="_3DDemo.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:_3DDemo"
xmlns:hv="http://helix-toolkit.org/wpf"
mc:Ignorable="d"
Title="MainWindow" 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>
到了这里,关于WPF编程--3D控件HelixToolkit的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!