【问题标题】:Helix Toolkit WPF, change camera vector on mouse clickHelix Toolkit WPF,单击鼠标更改相机矢量
【发布时间】:2019-03-12 09:17:00
【问题描述】:

我有一个带有 4 个框的螺旋视图,当我单击其中一个时,我希望相机查看该框。我的代码工作正常,但只要我用鼠标滚轮滚动一点(放大/缩小),就不再可能像我想要的那样单击和查看一个框。相同的鼠标单击事件正在触发,调试时我没有注意到任何区别。只是相机没有移动。滚动以某种方式搞砸了一切,但我不知道该怎么做。编辑:同样右键单击并以这种方式移动相机会导致同样的错误。

<helix:HelixViewport3D x:Name="View"
                       Background="{DynamicResource ControlBackgroundBrush}"
                       ItemsSource="{Binding Visuals}"
                       ShowCameraInfo="False"
                       ShowCoordinateSystem="True"
                       ShowViewCube="False"
                       ZoomExtentsWhenLoaded="False"
                       ModelUpDirection="0,1,0"
                       DefaultCamera="{Binding DefCamera}"
                       >

    <helix:HelixViewport3D.Camera>
        <PerspectiveCamera FieldOfView="61"
                           LookDirection="{Binding LookDirection}"
                           Position="{Binding CameraPosition}"
                           UpDirection="0,1,0" />
    </helix:HelixViewport3D.Camera>

    <!-- Selecting is done by first deselecting all boxes on mouse down
         then selecting the correct one on mouse up -->

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonDown">
            <cal:ActionMessage MethodName="DeselectAll" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

</helix:HelixViewport3D>

这个UIElement 类定义了一个围绕原始框的透明框。单击时,它会在父类中运行 selectionChanged 操作。该操作会计算一个新的 Position 和 Lookdirection,它们绑定在上面的 XAML 中。

public class InteractiveBoxVisual3D : System.Windows.UIElement3D
{
    public int SensorID { get; set; }
    public bool IsSelected { get; set; }

    private System.Action SelectionChanged;

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="mat">Matrix which defines the position and rotation</param>
    /// <param name="box">Box defines the box size in XYZ</param>
    /// <param name="sensorID">Sensor ID of the sensor</param>
    /// <param name="selectionChanged">Action run when box is selected or deselected</param>
    public InteractiveBoxVisual3D(Matrix3D mat, Box3D box, int sensorID, System.Action selectionChanged)
    {
        SensorID = sensorID;
        IsSelected = false;
        SelectionChanged = selectionChanged;
        Material material = MaterialHelper.CreateMaterial(Colors.Transparent);
        MeshBuilder meshBuilder = new MeshBuilder();

        Vector3D VX = new Vector3D(mat.M11, mat.M12, mat.M13);
        Vector3D VY = new Vector3D(mat.M21, mat.M22, mat.M23);

        // This selection box is larger by this amount, to avoid graphical errors
        int margin = 10;

        meshBuilder.AddBox(new Point3D(mat.OffsetX, mat.OffsetY, mat.OffsetZ), VX, VY, box.Lx + margin, box.Wy + margin, box.Hz + margin);
        Visual3DModel = new GeometryModel3D(meshBuilder.ToMesh(), material);
    }

    public void Select()
    {
        GeometryModel3D box = Visual3DModel as GeometryModel3D;
        box.Material = MaterialHelper.CreateMaterial(Colors.White);
        IsSelected = true;
        SelectionChanged.Invoke();
    }

    public void DeSelect()
    {
        GeometryModel3D box = Visual3DModel as GeometryModel3D;
        box.Material = MaterialHelper.CreateMaterial(Colors.Transparent);
        IsSelected = false;
        SelectionChanged.Invoke();
    }

    // There is a simulaneous mouse DOWN event which runs DeselectAll()
    protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.LeftButton == System.Windows.Input.MouseButtonState.Released)
        {
            Select();
            e.Handled = true;
        }
    }
}

【问题讨论】:

    标签: c# xaml helix-3d-toolkit


    【解决方案1】:

    您可能希望使用 Viewport3D.LookAt 或 Camera.LookAt 函数来直接操作您的相机。

    【讨论】:

    • 我尝试过从代码隐藏访问 viewport3d 对象确实效果更好。然而,我正在工作的架构禁止这样做,我们使用绑定。并且无法完成从 xaml 到 viewport3d 或 cameracontroller 的绑定。此外,由于我的解决方案仅在相机移动或缩放时停止工作,我觉得它不应该需要大量返工。
    • 您可以使用 Camera.LookAt 并在您的视图模型中创建一个相机。
    • 不,我无法绑定到相机。 System.Windows.Markup.XamlParseException:“无法在“HelixViewport3D”类型的“相机”属性上设置“绑定”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。当我写
    猜你喜欢
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多