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

j13ufse2  于 2022-12-14  发布在  其他
关注(0)|答案(2)|浏览(299)

我可以直接在XAML中设置放置在viewport3d中的透视相机的位置和方向。但是我想知道如何使用鼠标输入来旋转相机。我更喜欢C#语言。我实际上在如何使用鼠标输入来旋转相机这一点上卡住了。请帮助我。如果有人给我一个示例代码会很有帮助...

zour9fqk

zour9fqk1#

我想这两个链接可以帮到你很多...
Animating the Position of a 3D Camera in WPF(还有一个示例项目可以尝试!)
Rotating the Camera with the Mouse
我同意XNA可能是3D环境的最佳解决方案,但原生3D支持和硬件加速渲染也是WPF和XAML的出色特性!
正如您所看到的,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;
    }
}
sczxawaw

sczxawaw2#

下面的扩展方法实现了投影相机在任意方向上的自由飞行和旋转到欧几里得3D空间中。

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);
        }
    }

The image source

相关问题