应用3D旋转后,SkiaSharp Calc计算新点坐标

时间:2019-06-11 13:21:02

标签: c# skiasharp

我正在使用矩阵进行转换,然后使用xRotateyRotatezRotatedepth == 300 var进行3d(x,y,z)旋转。

using (var bmp = new SKBitmap(800, 600))
using (var canvas = new SKCanvas(bmp))
using (var paint = new SKPaint())
{
    canvas.Clear(SKColors.White);
    paint.IsAntialias = true;

    // Find center of canvas
    var info = bmp.Info;
    float xCenter = info.Width / 2;
    float yCenter = info.Height / 2;

    // Translate center to origin
    SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);
    // Use 3D matrix for 3D rotations and perspective
    SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();
    matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, xRotate));
    matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, yRotate));
    matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, zRotate));

    SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();
    perspectiveMatrix[3, 2] = -1 / depth;
    matrix44.PostConcat(perspectiveMatrix);

    // Concatenate with 2D matrix
    SKMatrix.PostConcat(ref matrix, matrix44.Matrix);

    // Translate back to center
    SKMatrix.PostConcat(ref matrix,
        SKMatrix.MakeTranslation(xCenter, yCenter));

    // Set the matrix and display the bitmap
    canvas.SetMatrix(matrix);
    canvas.DrawBitmap(currentImage, 50, 25, paint);

    pictureBox1.Image = bmp.ToBitmap();
}

如果原始Point中有currentImage,我想在绘制转换后的图像后计算其新位置。我怎样才能做到这一点?我会重用矩阵进行计算吗?

1 个答案:

答案 0 :(得分:0)

找到了答案。在currentImage中将点设为(1、2)。然后简单地:

var newPoint = matrix.MapPoint(1, 2);
newPoint =new SkPoint(50 + newPoint.X, 25 + newPoint.Y);  // + offsets of DrawImage 

或者在已经使用canvas.SetMatrix映射的画布上绘制

var newPoint = new SKPoint(1, 2);
canvas.DrawCircle(newPoint.X + 50, newPoint.Y + 25, 7, paint); // + offsets of DrawImage
相关问题