c# - SkiaSharp Calc 应用 3d 旋转后的新点坐标
问题描述
我正在使用矩阵进行平移,然后使用xRotate
, yRotate
, zRotate
, depth == 300
vars 在 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, 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
推荐阅读
- php - 如何插入可翻译字段?
- asp.net - IIS 和 ASP.NET 模拟
- java - JSON 属性中的特殊字符
- sql - jasper中的Oracle查询不返回数据类型为varchar的数据?
- javascript - 带有正文的Nodejs请求帖子包括api密钥
- sql - SQL:用同一张表中的最小值更新最大值
- python - 带有 Python 的 ArangoDB - 401 未授权执行此请求
- javascript - 如何在不使用媒体源扩展的情况下显示 arraybuffer 视频流数据?
- java - 在 Jsoup 中获取响应的整个位置标头值
- javascript - 使用带有角度 4 的 Hello.js