簡體   English   中英

c#WPF 3D hitTest錯誤:不支持奇異的MatrixCamera

[英]c# WPF 3D hitTest error: singular MatrixCamera not supported

這是我用來執行hitTest的代碼:

RayHitTestResult hit = VisualTreeHelper.HitTest(
      App.MainWin.Viewport, location) as RayHitTestResult;

當攝像機的寬度較小(即對象較大)時,此代碼有效。 當我將相機縮小(即對象變小)到某個點時,然后單擊視口時,上面的代碼顯示錯誤,並且錯誤詳細信息如下所示:

未處理System.NotSupportedException:不支持使用單個MatrixCamera進行命中測試。

我用谷歌搜索,沒有人遇到此錯誤。 知道如何解決嗎? 謝謝!

我用來創建/更新MatrixCamera的代碼:(它們受到Charles Petzold第7章“ Windows的3D編程:Windows Presentation Foundation的三維圖形編程”一書中的示例的啟發)

    //create a new camera, initialize it and attach it to the viewport.
    public void initCamera(Point3D position, Point3D AimPoint, 
                           Vector3D upDirection, double farDistance, 
                           double nearDistance, double Width)
    {
        this._Camera = new MatrixCamera();

        this.CameraAimPoint = (Vector3D)AimPoint;

        //check and adjust the camera depth if needed
        this.CameraZAxis = Point3D.Subtract(position, AimPoint); 

        this.CameraDepth = CameraZAxis.Length;

        this.CameraZAxis.Normalize();
        this.CameraPosition = this.CameraAimPoint + (this.CameraZAxis *  this.CameraDepth);

        this.CameraFarDistance = farDistance;
        this.CameraNearDistance = nearDistance;
        this.CameraWidth = Width;

        this.CameraXAxis = Vector3D.CrossProduct(upDirection, this.CameraZAxis);
        this.CameraXAxis.Normalize();

        this.CameraYAxis = Vector3D.CrossProduct(this.CameraZAxis, this.CameraXAxis);

        this._CameraViewMatrix = new Matrix3D();
        this._CameraViewMatrix.M14 = 0;
        this._CameraViewMatrix.M24 = 0;
        this._CameraViewMatrix.M34 = 0;
        this._CameraViewMatrix.M44 = 1;

        this.updateViewMatrix();

        this._CameraProjectMatrix = new Matrix3D();
        this._CameraProjectMatrix.M14 = 0;
        this._CameraProjectMatrix.M24 = 0;
        this._CameraProjectMatrix.M34 = 0;
        this._CameraProjectMatrix.M44 = 1;

        this.updateProjectionMatrix();

        this._Viewport.Camera = this._Camera;

    }

    private void updateViewMatrix(bool axisChanged=true)
    {
        if (axisChanged==true)
        {
            this._CameraViewMatrix.M11 = this.CameraXAxis.X;
            this._CameraViewMatrix.M12 = this.CameraYAxis.X;
            this._CameraViewMatrix.M13 = this.CameraZAxis.X;

            this._CameraViewMatrix.M21 = this.CameraXAxis.Y;
            this._CameraViewMatrix.M22 = this.CameraYAxis.Y;
            this._CameraViewMatrix.M23 = this.CameraZAxis.Y;

            this._CameraViewMatrix.M31 = this.CameraXAxis.Z;
            this._CameraViewMatrix.M32 = this.CameraYAxis.Z;
            this._CameraViewMatrix.M33 = this.CameraZAxis.Z;
        }

        this._CameraViewMatrix.OffsetX = -Vector3D.DotProduct(this.CameraXAxis, this.CameraPosition);
        this._CameraViewMatrix.OffsetY = -Vector3D.DotProduct(this.CameraYAxis, this.CameraPosition);
        this._CameraViewMatrix.OffsetZ = -Vector3D.DotProduct(this.CameraZAxis, this.CameraPosition);

        this._Camera.ViewMatrix = this._CameraViewMatrix;
        this._3DTo2DTransformMatrixIsDefined = false;

        AfterDraw();
    }

    //
    private void updateProjectionMatrix() {
        double ScaleX = 2 / CameraWidth;
        double ScaleY = _Viewport.ActualWidth / _Viewport.ActualHeight * ScaleX;
        double ScaleZ = 1 / (CameraNearDistance - CameraFarDistance);
        double zOffset = CameraNearDistance * ScaleZ;

        _CameraProjectMatrix.M11 = ScaleX;
        _CameraProjectMatrix.M22 = ScaleY;
        _CameraProjectMatrix.M33 = ScaleZ;
        _CameraProjectMatrix.OffsetZ = zOffset;

        _Camera.ProjectionMatrix = _CameraProjectMatrix;
        _3DTo2DTransformMatrixIsDefined = false;

        _PixelToWorldUnit = CameraWidth / _Viewport.ActualWidth;

        AfterDraw();

    }

執行相機縮放的代碼:

    private void _cameraZoomToScale(double width)
    {
        if (width< 1) width = 1;
        this.CameraWidth = width;
        updateProjectionMatrix();

    }

很明顯,您正在縮放的​​范圍超出了MatrixCamera支持的范圍。 找出此異常發生在什么縮放級別,並防止用戶走得太遠。 if (scale < 1) scale = 1;您可能已經嘗試這樣做if (scale < 1) scale = 1; 但是在發布的代碼中似乎沒有使用scale

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM