Helix toolkit 旋转 3D 模型
Helix toolkit Rotate 3D Model
我是 WPF 的新手,我正在尝试制作一个程序来显示 3d 模型(保存在我的计算机上)并根据按钮点击来旋转它。我想要三个按钮来围绕 x、y 和 z 轴旋转对象。我有可以显示模型的代码,但我不确定如何使用按钮点击来旋转它。这是我目前所拥有的;
C#
public MainWindow()
{
InitializeComponent();
ModelVisual3D device3D = new ModelVisual3D();
device3D.Content = Display3d(MODEL_PATH);
// Add to view port
viewPort3d.Children.Add(device3D);
}
private Model3D Display3d(string model)
{
Model3D device = null;
try
{
//Adding a gesture here
viewPort3d.RotateGesture = new MouseGesture(MouseAction.LeftClick);
//Import 3D model file
ModelImporter import = new ModelImporter();
//Load the 3D model file
device = import.Load(model);
}
catch (Exception e)
{
// Handle exception in case can not find the 3D model file
MessageBox.Show("Exception Error : " + e.StackTrace);
}
return device;
}
private void buttonX_Click(object sender, RoutedEventArgs e)
{
//not sure what to put in here
}
XAML
<Grid Margin="0,0,6,94" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-0.275"/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<helix:HelixViewport3D x:Name="viewPort3d" ZoomExtentsWhenLoaded="true" Margin="0,0,10,64" >
<!-- Remember to add light to the scene -->
<helix:DefaultLights/>
<ModelVisual3D x:Name="Models"/>
</helix:HelixViewport3D>
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="417,219,0,0" Stroke="Black" VerticalAlignment="Top" Width="100" RenderTransformOrigin="0.146,-0.196"/>
<Button x:Name="buttonX" Content="ButtonX" HorizontalAlignment="Left" Height="30" Margin="216,356,0,-60" VerticalAlignment="Top" Width="104" Click="buttonX_Click"/>
</Grid>
我目前正在使用 Helix 工具包,但如果有更简单的方法,请告诉我。
根据你问题的措辞,我假设你想旋转模型而不是相机,在这种情况下,将 device3D
保存在某处并执行此操作:
private void buttonX_Click(object sender, RoutedEventArgs e)
{
var axis = new Vector3D(0, 0, 1);
var angle = 10;
var matrix = device3D.Transform.Value;
matrix.Rotate(new Quaternion(axis, angle));
device3D.Transform = new MatrixTransform3D(matrix);
}
我是 WPF 的新手,我正在尝试制作一个程序来显示 3d 模型(保存在我的计算机上)并根据按钮点击来旋转它。我想要三个按钮来围绕 x、y 和 z 轴旋转对象。我有可以显示模型的代码,但我不确定如何使用按钮点击来旋转它。这是我目前所拥有的;
C#
public MainWindow()
{
InitializeComponent();
ModelVisual3D device3D = new ModelVisual3D();
device3D.Content = Display3d(MODEL_PATH);
// Add to view port
viewPort3d.Children.Add(device3D);
}
private Model3D Display3d(string model)
{
Model3D device = null;
try
{
//Adding a gesture here
viewPort3d.RotateGesture = new MouseGesture(MouseAction.LeftClick);
//Import 3D model file
ModelImporter import = new ModelImporter();
//Load the 3D model file
device = import.Load(model);
}
catch (Exception e)
{
// Handle exception in case can not find the 3D model file
MessageBox.Show("Exception Error : " + e.StackTrace);
}
return device;
}
private void buttonX_Click(object sender, RoutedEventArgs e)
{
//not sure what to put in here
}
XAML
<Grid Margin="0,0,6,94" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-0.275"/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<helix:HelixViewport3D x:Name="viewPort3d" ZoomExtentsWhenLoaded="true" Margin="0,0,10,64" >
<!-- Remember to add light to the scene -->
<helix:DefaultLights/>
<ModelVisual3D x:Name="Models"/>
</helix:HelixViewport3D>
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="417,219,0,0" Stroke="Black" VerticalAlignment="Top" Width="100" RenderTransformOrigin="0.146,-0.196"/>
<Button x:Name="buttonX" Content="ButtonX" HorizontalAlignment="Left" Height="30" Margin="216,356,0,-60" VerticalAlignment="Top" Width="104" Click="buttonX_Click"/>
</Grid>
我目前正在使用 Helix 工具包,但如果有更简单的方法,请告诉我。
根据你问题的措辞,我假设你想旋转模型而不是相机,在这种情况下,将 device3D
保存在某处并执行此操作:
private void buttonX_Click(object sender, RoutedEventArgs e)
{
var axis = new Vector3D(0, 0, 1);
var angle = 10;
var matrix = device3D.Transform.Value;
matrix.Rotate(new Quaternion(axis, angle));
device3D.Transform = new MatrixTransform3D(matrix);
}