首页 > 解决方案 > SkiaSharp Calc 应用 3d 旋转后的新点坐标

问题描述

我正在使用矩阵进行平移,然后使用xRotate, yRotate, zRotate, depth == 300vars 在 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,我想在绘制转换后的图像后计算它的新位置。我怎样才能做到这一点?我会重复使用矩阵来计算它吗?

标签: c#skiasharp

解决方案


找到了答案。设点为 中的 (1, 2) currentImage。然后简单地说:

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

推荐阅读