如何在WPF中使用鼠标旋转ViewPort3D相机?

6

我能够直接在XAML中设置位于viewport3d中的透视相机的位置和方向。 但我想知道如何使用鼠标输入旋转相机。 我更喜欢使用C#语言。 实际上,我卡在了如何使用鼠标输入旋转相机的点上。 请帮帮我。 如果有人能给我一个样例代码就太好了....

2个回答

3
我认为以下两个链接可以帮助您很多... 在WPF中动画化3D相机的位置(还有一个示例项目可供尝试!) 使用鼠标旋转相机 我同意,也许XNA是处理3D情况的最佳解决方案,但WPF和XAML的本地3D支持和硬件加速渲染功能也非常出色!
正如您所看到的,针对XAML 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;
    }
}

1

enter image description here

以下扩展方法实现了投影相机在欧几里得三维空间中自由飞行的任意方向和旋转。
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);
        }
    }

图片来源


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接