locked
Manually rotating using Skiasharp Matrix44 RRS feed

  • Question

  • User399274 posted

    Dear Xamarin Skiasharp Forum,

    I am using the SkisharpFormsDemos->Transforms code to learn how to translate rotate and scale in 3d a 2d SKSvg. I hooked up the rotation and translation to some sliders but only the translation seems to work! This is progressing from the mCanvas.Translate(...) previous way of doing things.

    Please advise could you advise me where I am going wrong. Plus I remember learning a long time ago that there is a particular order that one is supposed to multiply the matrices together... Is it Translate * Rotate * Scale or some other order.

    Also, I can't debug the values of the matrices because VS Community 2019 just hangs on evaluating both in the immediate window and when hovering over the matrix variables.

    Note: that gData.dTransX (etc.) is set to the slider value.
    ``` public void CanvasViewOnPaintSurface(object sender, SKPaintSurfaceEventArgs e) { SKImageInfo imgInfo = e.Info; mSurface = e.Surface; mCanvas = mSurface.Canvas;

            mCanvas.Clear();
    
            double dX = DeviceDisplay.MainDisplayInfo.Width / 2d;
            double dY = DeviceDisplay.MainDisplayInfo.Height / 2d;
    
            double dD = DeviceDisplay.MainDisplayInfo.Density;
    
            float fXCenter = (imgInfo.Width - mSvg.ViewBox.Width) / 2;
            float fYCenter = (imgInfo.Height - mSvg.ViewBox.Height) / 2;
    
            // Gets the size of the svg thst will fill the screen in portrait(min) else (max)
            double dRatioX = DeviceDisplay.MainDisplayInfo.Width / (double)mSvg.ViewBox.Width;
            double dRatioY = DeviceDisplay.MainDisplayInfo.Height / (double)mSvg.ViewBox.Height;
            double dRatioMin = Math.Min(dRatioX, dRatioY);
            dRatioMin /= 9;
    
            // Custom movement by sliders
            //mCanvas.Translate((float)gData.dValXSet, (float)gData.dValYSet);
    
            #region MatrixOps
            // Translate center to origin
            mMatrix = SKMatrix.CreateTranslation((float)gData.dTransX, (float)gData.dTransY);
    
            // Scale so text fits
            float fScale = (float)dRatioMin;
            mMatrix.PostConcat(SKMatrix.CreateScale(fScale, fScale));
    
            // Calculate composite 3D transforms
            float fDepth = 0.75f * fScale * mSvg.CanvasSize.Width;
    
            // Calculate compound rotation x, y, z
            mMatrix44 = SKMatrix44.CreateIdentity();
            mMatrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, (float)gData.dRotX));
            mMatrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, (float)gData.dRotY));
            mMatrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, (float)gData.dRotZ));
    
            // Calculate perspective
            mPerspectiveMatrix = SKMatrix44.CreateIdentity();
            mPerspectiveMatrix[3, 2] = -1 / fDepth;
            mMatrix44.PostConcat(mPerspectiveMatrix);
    
            // Concatenate with 2D matrix
            mMatrix.PostConcat(mMatrix44.Matrix);
    
            // Translate back to center
            mMatrix.PostConcat(SKMatrix.CreateTranslation((float)-gData.dTransX, (float)-gData.dTransY));
    
            // Set the matrix and display the text
            mCanvas.SetMatrix(mMatrix);
            #endregion MatrixOps
    
            mCanvas.DrawPicture(mSvg.Picture);
        }
    

    ``` Thank you in advance for you considered advice :)

    Friday, February 26, 2021 6:57 PM

Answers

  • User399274 posted

    I'm sorry to answer my own questions all the time but... It seems that when upgrading to Forms v5 and obeying intellisense' prompts to use a non obsolete way of doing things ( which is what I dod I the OP code) results in the inspection evaluation bugs I reported. It seems that according to this post: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/internals/experimental-flags I should have need to specify experimental flags at the app level to allow for the use of these new features.

    I suppose I will have to just put up with the warnings about obsolete MakeTranslation and MakeRotation.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, February 26, 2021 8:07 PM

All replies

  • User399274 posted

    *Update: *this 'evaluation hang' doesn't happen in the demo code...I can easily inspect the code, but I must have messed something up in my solution/project!?

    Friday, February 26, 2021 7:24 PM
  • User399274 posted

    I'm sorry to answer my own questions all the time but... It seems that when upgrading to Forms v5 and obeying intellisense' prompts to use a non obsolete way of doing things ( which is what I dod I the OP code) results in the inspection evaluation bugs I reported. It seems that according to this post: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/internals/experimental-flags I should have need to specify experimental flags at the app level to allow for the use of these new features.

    I suppose I will have to just put up with the warnings about obsolete MakeTranslation and MakeRotation.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, February 26, 2021 8:07 PM