视频: WPF helix-toolkit 官方SimpleDemo_哔哩哔哩_bilibili
源码: https://github.com/liugang198409/WpfDemo/tree/master/3DSimpleDemo
1. 环境
Visual Studio 2019 + .NET Framework 4.8.1
2. NuGet导入依赖
HelixToolkit
HelixToolkit.wpf文章来源:https://www.toymoban.com/news/detail-677534.html
3. 创建类MainViewModel.cs
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainViewModel.cs" company="Helix Toolkit">
// Copyright (c) 2014 Helix Toolkit contributors
// </copyright>
// <summary>
// Provides a ViewModel for the Main window.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace _3DSimpleDemo
{
using System.Windows.Media;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;
/// <summary>
/// Provides a ViewModel for the Main window.
/// </summary>
public class MainViewModel
{
/// <summary>
/// Initializes a new instance of the <see cref="MainViewModel"/> class.
/// </summary>
public MainViewModel()
{
// Create a model group
var modelGroup = new Model3DGroup();
// Create a mesh builder and add a box to it
var meshBuilder = new MeshBuilder(false, false);
meshBuilder.AddBox(new Point3D(0, 0, 1), 1, 2, 0.5);
meshBuilder.AddBox(new Rect3D(0, 0, 1.2, 0.5, 1, 0.4));
// Create a mesh from the builder (and freeze it)
var mesh = meshBuilder.ToMesh(true);
// Create some materials
var greenMaterial = MaterialHelper.CreateMaterial(Colors.Green);
var redMaterial = MaterialHelper.CreateMaterial(Colors.Red);
var blueMaterial = MaterialHelper.CreateMaterial(Colors.Blue);
var insideMaterial = MaterialHelper.CreateMaterial(Colors.Yellow);
// Add 3 models to the group (using the same mesh, that's why we had to freeze it)
modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Material = greenMaterial, BackMaterial = insideMaterial });
modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(-2, 0, 0), Material = redMaterial, BackMaterial = insideMaterial });
modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(2, 0, 0), Material = blueMaterial, BackMaterial = insideMaterial });
// Set the property, which will be bound to the Content property of the ModelVisual3D (see MainWindow.xaml)
this.Model = modelGroup;
}
/// <summary>
/// Gets or sets the model.
/// </summary>
/// <value>The model.</value>
public Model3D Model { get; set; }
}
}
4. 编辑MainWindow.xaml
<Window x:Class="_3DSimpleDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
xmlns:local="clr-namespace:_3DSimpleDemo"
Title="SimpleDemo"
Width="640"
Height="480">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<Grid>
<Grid.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<HelixToolkit:NotNullToVisibilityConverter x:Key="NotNullToVisibilityConverter" />
<Style TargetType="{x:Type HelixToolkit:HelixViewport3D}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HelixToolkit:HelixViewport3D}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid FocusVisualStyle="{x:Null}">
<HelixToolkit:CameraController
x:Name="PART_CameraController"
CameraMode="{TemplateBinding CameraMode}"
CameraRotationMode="{TemplateBinding CameraRotationMode}"
ChangeFieldOfViewCursor="{TemplateBinding ChangeFieldOfViewCursor}"
DefaultCamera="{TemplateBinding DefaultCamera}"
InertiaFactor="{TemplateBinding CameraInertiaFactor}"
InfiniteSpin="{TemplateBinding InfiniteSpin}"
IsChangeFieldOfViewEnabled="{TemplateBinding IsChangeFieldOfViewEnabled}"
IsInertiaEnabled="{TemplateBinding IsInertiaEnabled}"
IsMoveEnabled="{TemplateBinding IsMoveEnabled}"
IsPanEnabled="{TemplateBinding IsPanEnabled}"
IsRotationEnabled="{TemplateBinding IsRotationEnabled}"
IsTouchZoomEnabled="{TemplateBinding IsTouchZoomEnabled}"
IsZoomEnabled="{TemplateBinding IsZoomEnabled}"
LeftRightPanSensitivity="{TemplateBinding LeftRightPanSensitivity}"
LeftRightRotationSensitivity="{TemplateBinding LeftRightRotationSensitivity}"
MaximumFieldOfView="{TemplateBinding MaximumFieldOfView}"
MinimumFieldOfView="{TemplateBinding MinimumFieldOfView}"
ModelUpDirection="{TemplateBinding ModelUpDirection}"
PageUpDownZoomSensitivity="{TemplateBinding PageUpDownZoomSensitivity}"
PanCursor="{TemplateBinding PanCursor}"
RotateAroundMouseDownPoint="{TemplateBinding RotateAroundMouseDownPoint}"
RotateCursor="{TemplateBinding RotateCursor}"
RotationSensitivity="{TemplateBinding RotationSensitivity}"
ShowCameraTarget="{TemplateBinding ShowCameraTarget}"
UpDownPanSensitivity="{TemplateBinding UpDownPanSensitivity}"
UpDownRotationSensitivity="{TemplateBinding UpDownRotationSensitivity}"
ZoomAroundMouseDownPoint="{TemplateBinding ZoomAroundMouseDownPoint}"
ZoomCursor="{TemplateBinding ZoomCursor}"
ZoomRectangleCursor="{TemplateBinding ZoomRectangleCursor}"
ZoomSensitivity="{TemplateBinding ZoomSensitivity}">
<HelixToolkit:CameraController.InputBindings>
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.ResetCameraCommand}" Gezture="{Binding ResetCameraKeyGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.RotateCommand}" Gezture="{Binding RotateGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.RotateCommand}" Gezture="{Binding RotateGesture2, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.PanCommand}" Gezture="{Binding PanGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.PanCommand}" Gezture="{Binding PanGesture2, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.ZoomCommand}" Gezture="{Binding ZoomGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.ZoomCommand}" Gezture="{Binding ZoomGesture2, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.ZoomRectangleCommand}" Gezture="{Binding ZoomRectangleGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.ChangeFieldOfViewCommand}" Gezture="{Binding ChangeFieldOfViewGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.ChangeLookAtCommand}" Gezture="{Binding ChangeLookAtGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.ZoomExtentsCommand}" Gezture="{Binding ZoomExtentsGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.ResetCameraCommand}" Gezture="{Binding ResetCameraGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.TopViewCommand}" Gezture="{Binding TopViewGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.BottomViewCommand}" Gezture="{Binding BottomViewGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.LeftViewCommand}" Gezture="{Binding LeftViewGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.RightViewCommand}" Gezture="{Binding RightViewGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.FrontViewCommand}" Gezture="{Binding FrontViewGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:CameraController.BackViewCommand}" Gezture="{Binding BackViewGesture, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:InputBindingX Command="{x:Static HelixToolkit:HelixViewport3D.OrthographicToggleCommand}" Gezture="{Binding OrthographicToggleGesture, RelativeSource={RelativeSource TemplatedParent}}" />
</HelixToolkit:CameraController.InputBindings>
<Grid x:Name="PART_ViewportGrid">
<AdornerDecorator
x:Name="PART_AdornerLayer"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
DataContext="{TemplateBinding DataContext}"
IsHitTestVisible="False" />
<!-- the main viewport3D is inserted here -->
</Grid>
</HelixToolkit:CameraController>
<!-- Coordinate system -->
<Viewport3D
x:Name="PART_CoordinateView"
Width="{TemplateBinding CoordinateSystemWidth}"
Height="{TemplateBinding CoordinateSystemHeight}"
Margin="0"
HorizontalAlignment="{TemplateBinding CoordinateSystemHorizontalPosition}"
VerticalAlignment="{TemplateBinding CoordinateSystemVerticalPosition}"
ClipToBounds="False"
IsHitTestVisible="False"
Visibility="{TemplateBinding ShowCoordinateSystem,
Converter={StaticResource BooleanToVisibilityConverter}}">
<HelixToolkit:ArrowVisual3D Fill="#964B4B" Point2="8 0 0" />
<HelixToolkit:ArrowVisual3D Fill="#4B964B" Point2="0 8 0" />
<HelixToolkit:ArrowVisual3D Fill="#4B4B96" Point2="0 0 8" />
<HelixToolkit:BillboardTextVisual3D
Foreground="{Binding CoordinateSystemLabelForeground, RelativeSource={RelativeSource TemplatedParent}}"
Position="10 0 0"
Text="{Binding CoordinateSystemLabelX, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:BillboardTextVisual3D
Foreground="{Binding CoordinateSystemLabelForeground, RelativeSource={RelativeSource TemplatedParent}}"
Position="0 10 0"
Text="{Binding CoordinateSystemLabelY, RelativeSource={RelativeSource TemplatedParent}}" />
<HelixToolkit:BillboardTextVisual3D
Foreground="{Binding CoordinateSystemLabelForeground, RelativeSource={RelativeSource TemplatedParent}}"
Position="0 0 10"
Text="{Binding CoordinateSystemLabelZ, RelativeSource={RelativeSource TemplatedParent}}" />
<!--<local:PieSliceVisual3D Normal="1,0,0" UpVector="0,0,1" InnerRadius="3" OuterRadius="6" Fill="#80ff0000"/>
<local:PieSliceVisual3D Normal="0,1,0" UpVector="1,0,0" InnerRadius="3" OuterRadius="6" Fill="#8000ff00"/>
<local:PieSliceVisual3D Normal="0,0,1" UpVector="0,1,0" InnerRadius="3" OuterRadius="6" Fill="#800000ff"/>-->
</Viewport3D>
<Grid IsHitTestVisible="False">
<!-- Titles -->
<StackPanel
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="{TemplateBinding TitleBackground}">
<TextBlock
FontFamily="{TemplateBinding TitleFontFamily}"
FontSize="{TemplateBinding TitleSize}"
FontWeight="Bold"
Foreground="{TemplateBinding TextBrush}"
Text="{TemplateBinding Title}"
Visibility="{TemplateBinding Title,
Converter={StaticResource NotNullToVisibilityConverter}}" />
<TextBlock
FontFamily="{TemplateBinding TitleFontFamily}"
FontSize="{TemplateBinding SubTitleSize}"
Foreground="{TemplateBinding TextBrush}"
Text="{TemplateBinding SubTitle}"
Visibility="{TemplateBinding SubTitle,
Converter={StaticResource NotNullToVisibilityConverter}}" />
</StackPanel>
<StackPanel
Margin="6"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Background="{TemplateBinding InfoBackground}">
<TextBlock
Padding="4"
HorizontalAlignment="Left"
Foreground="{TemplateBinding InfoForeground}"
Text="{TemplateBinding FrameRateText}"
Visibility="{TemplateBinding ShowFrameRate,
Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBlock
Padding="4"
HorizontalAlignment="Right"
Foreground="{TemplateBinding InfoForeground}"
Text="{TemplateBinding FieldOfViewText}"
Visibility="{TemplateBinding ShowFieldOfView,
Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBlock
HorizontalAlignment="Right"
Foreground="{TemplateBinding InfoForeground}"
Text="{TemplateBinding Status}"
Visibility="{TemplateBinding Status,
Converter={StaticResource NotNullToVisibilityConverter}}" />
</StackPanel>
<StackPanel
Margin="6"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Background="{TemplateBinding InfoBackground}">
<TextBlock
Padding="4"
Foreground="{TemplateBinding InfoForeground}"
Text="{TemplateBinding CameraInfo}"
Visibility="{TemplateBinding ShowCameraInfo,
Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBlock
Padding="4"
Foreground="{TemplateBinding InfoForeground}"
Text="{TemplateBinding TriangleCountInfo}"
Visibility="{TemplateBinding ShowTriangleCountInfo,
Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBlock
Foreground="{TemplateBinding InfoForeground}"
Text="{TemplateBinding DebugInfo}"
Visibility="{TemplateBinding DebugInfo,
Converter={StaticResource NotNullToVisibilityConverter}}" />
</StackPanel>
</Grid>
<Viewport3D
x:Name="PART_ViewCubeViewport"
Width="{TemplateBinding ViewCubeWidth}"
Height="{TemplateBinding ViewCubeHeight}"
Margin="0"
HorizontalAlignment="{TemplateBinding ViewCubeHorizontalPosition}"
VerticalAlignment="{TemplateBinding ViewCubeVerticalPosition}"
Opacity="0.5"
Visibility="{TemplateBinding ShowViewCube,
Converter={StaticResource BooleanToVisibilityConverter}}">
<HelixToolkit:ViewCubeVisual3D
x:Name="PART_ViewCube"
BackText="{Binding ViewCubeBackText, RelativeSource={RelativeSource TemplatedParent}}"
BottomText="{Binding ViewCubeBottomText, RelativeSource={RelativeSource TemplatedParent}}"
EnableEdgeClicks="{Binding IsViewCubeEdgeClicksEnabled, RelativeSource={RelativeSource TemplatedParent}}"
FrontText="{Binding ViewCubeFrontText, RelativeSource={RelativeSource TemplatedParent}}"
IsEnabled="{Binding IsRotationEnabled, RelativeSource={RelativeSource TemplatedParent}}"
IsTopBottomViewOrientedToFrontBack="{Binding IsTopBottomViewOrientedToFrontBack, RelativeSource={RelativeSource TemplatedParent}}"
LeftText="{Binding ViewCubeLeftText, RelativeSource={RelativeSource TemplatedParent}}"
ModelUpDirection="{Binding ModelUpDirection, RelativeSource={RelativeSource TemplatedParent}}"
RightText="{Binding ViewCubeRightText, RelativeSource={RelativeSource TemplatedParent}}"
TopText="{Binding ViewCubeTopText, RelativeSource={RelativeSource TemplatedParent}}" />
</Viewport3D>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<!-- The HelixViewport3D supports camera manipulation, and can be used just like the Viewport3D -->
<HelixToolkit:HelixViewport3D ShowFrameRate="True" ZoomExtentsWhenLoaded="True" ZoomAroundMouseDownPoint="True" RotateAroundMouseDownPoint="True" IsTopBottomViewOrientedToFrontBack="True" IsViewCubeEdgeClicksEnabled="True">
<!-- Remember to add light to the scene -->
<HelixToolkit:SunLight />
<!-- The content of this visual is defined in MainViewModel.cs -->
<ModelVisual3D Content="{Binding Model}" />
<!-- You can also add elements here in the xaml -->
<HelixToolkit:GridLinesVisual3D
Width="8"
Length="8"
MajorDistance="1"
MinorDistance="1"
Thickness="0.01" />
</HelixToolkit:HelixViewport3D>
</Grid>
</Window>
文章来源地址https://www.toymoban.com/news/detail-677534.html
到了这里,关于WPF编程--helix-toolkit 官方SimpleDemo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!