SkiaSharp Calc координаты новой точки после применения 3D вращения - PullRequest
0 голосов
/ 11 июня 2019

Я использую матрицу для перевода и поворота в 3d (x, y, z) с использованием xRotate, yRotate, zRotate, depth == 300 переменных.

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 голосов
/ 11 июня 2019

Нашел ответ.Пусть точка будет (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
...