我能够直接在XAML中设置位于viewport3d中的透视相机的位置和方向。 但我想知道如何使用鼠标输入旋转相机。 我更喜欢使用C#语言。 实际上,我卡在了如何使用鼠标输入旋转相机的点上。 请帮帮我。 如果有人能给我一个样例代码就太好了....
我能够直接在XAML中设置位于viewport3d中的透视相机的位置和方向。 但我想知道如何使用鼠标输入旋转相机。 我更喜欢使用C#语言。 实际上,我卡在了如何使用鼠标输入旋转相机的点上。 请帮帮我。 如果有人能给我一个样例代码就太好了....
Viewport3D
的3D相机与应用程序完美契合,并且还可以使用绑定:<Viewport3D.Camera>
<PerspectiveCamera x:Name="camera"
UpDirection="0,0,1"
LookDirection="{Binding RelativeSource={RelativeSource Self}, Path=Position, Converter={StaticResource lookBackConverter}}"
Position="0,0,0" />
</Viewport3D.Camera>
...并且只需通常的IValueConverter
实现即可让相机移动:
public class LookBackConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new Point3D(0,0,0) - (Point3D)value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
using System.Windows.Input;
using System.Windows.Media.Media3D;
using static System.Windows.Input.Key;
using static System.Windows.Input.ModifierKeys;
public static class ProjectionCameraExtensions
{
public static TCamera Move<TCamera>(this TCamera camera, Vector3D axis, double step)
where TCamera : ProjectionCamera
{
camera.Position += axis * step;
return camera;
}
public static TCamera Rotate<TCamera>(this TCamera camera, Vector3D axis, double angle)
where TCamera : ProjectionCamera
{
Matrix3D matrix3D = new();
matrix3D.RotateAt(new(axis, angle), camera.Position);
camera.LookDirection *= matrix3D;
return camera;
}
public static Vector3D GetYawAxis(this ProjectionCamera camera) => camera.UpDirection;
public static Vector3D GetRollAxis(this ProjectionCamera camera) => camera.LookDirection;
public static Vector3D GetPitchAxis(this ProjectionCamera camera) => Vector3D.CrossProduct(camera.UpDirection, camera.LookDirection);
public static PerspectiveCamera MoveBy(this PerspectiveCamera camera, Key key) => camera.MoveBy(key, camera.FieldOfView / 180d);
public static PerspectiveCamera RotateBy(this PerspectiveCamera camera, Key key) => camera.RotateBy(key, camera.FieldOfView / 45d);
public static TCamera MoveBy<TCamera>(this TCamera camera, Key key, double step) where TCamera : ProjectionCamera => key switch
{
W => camera.Move(Keyboard.Modifiers.HasFlag(Shift) ? camera.GetYawAxis() : camera.GetRollAxis(), +step),
S => camera.Move(Keyboard.Modifiers.HasFlag(Shift) ? camera.GetYawAxis() : camera.GetRollAxis(), -step),
A => camera.Move(camera.GetPitchAxis(), +step),
D => camera.Move(camera.GetPitchAxis(), -step),
_ => camera
};
public static TCamera RotateBy<TCamera>(this TCamera camera, Key key, double angle) where TCamera : ProjectionCamera => key switch
{
Left => camera.Rotate(camera.GetYawAxis(), +angle),
Right => camera.Rotate(camera.GetYawAxis(), -angle),
Down => camera.Rotate(camera.GetPitchAxis(), +angle),
Up => camera.Rotate(camera.GetPitchAxis(), -angle),
_ => camera
};
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e) =>
Camera.MoveBy(e.Key).RotateBy(e.Key);
Point from;
private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
{
var till = e.GetPosition(sender as IInputElement);
double dx = till.X - from.X;
double dy = till.Y - from.Y;
from = till;
var distance = dx * dx + dy * dy;
if (distance <= 0d)
return;
if (e.MouseDevice.LeftButton is MouseButtonState.Pressed)
{
var angle = (distance / Camera.FieldOfView) % 45d;
Camera.Rotate(new(dy, -dx, 0d), angle);
}
}