Helix Toolkit WPF,单击鼠标即可更改相机矢量

时间:2019-03-12 09:17:00

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

我有一个带有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操作。该操作将计算出上面XAML中绑定的新的Position和Lookdirection。

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

1 个答案:

答案 0 :(得分:0)

您可能想使用Viewport3D.LookAt或Camera.LookAt函数直接操作相机。