您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
89 行
3.5 KiB
89 行
3.5 KiB
using System;
|
|
using Unity.UIWidgets.foundation;
|
|
using Unity.UIWidgets.ui;
|
|
using UnityEngine;
|
|
using Rect = Unity.UIWidgets.ui.Rect;
|
|
|
|
namespace Unity.UIWidgets.painting {
|
|
public static class MatrixUtils {
|
|
public static Offset transformPoint(this Matrix4x4 transform, Offset point) {
|
|
var position3 = new Vector3((float) point.dx, (float) point.dy, 0);
|
|
var transformed3 = transform.MultiplyPoint(position3);
|
|
return new Offset(transformed3.x, transformed3.y);
|
|
}
|
|
|
|
public static Rect transformRect(this Matrix4x4 transform, Offset[] points, out bool isRect) {
|
|
D.assert(points != null && points.Length == 4, "expected 4 points");
|
|
|
|
var topLeft = transform.transformPoint(points[0]);
|
|
var topRight = transform.transformPoint(points[1]);
|
|
var bottomLeft = transform.transformPoint(points[2]);
|
|
var bottomRight = transform.transformPoint(points[3]);
|
|
|
|
isRect = topLeft.dy == topRight.dy
|
|
&& topRight.dx == bottomRight.dx
|
|
&& bottomRight.dy == bottomLeft.dy
|
|
&& bottomLeft.dx == topLeft.dx;
|
|
|
|
var left = Math.Min(Math.Min(Math.Min(topLeft.dx, topRight.dx), bottomLeft.dx), bottomRight.dx);
|
|
var right = Math.Max(Math.Max(Math.Max(topLeft.dx, topRight.dx), bottomLeft.dx), bottomRight.dx);
|
|
var top = Math.Min(Math.Min(Math.Min(topLeft.dy, topRight.dy), bottomLeft.dy), bottomRight.dy);
|
|
var bottom = Math.Max(Math.Max(Math.Max(topLeft.dy, topRight.dy), bottomLeft.dy), bottomRight.dy);
|
|
|
|
return Rect.fromLTRB(left, top, right, bottom);
|
|
}
|
|
|
|
public static Rect transformRect(this Matrix4x4 transform, Rect rect, out bool isRect) {
|
|
return transform.transformRect(
|
|
new[] {rect.topLeft, rect.topRight, rect.bottomLeft, rect.bottomRight},
|
|
out isRect);
|
|
}
|
|
|
|
public static Rect transformRect(this Matrix4x4 transform, Offset[] points) {
|
|
bool isRect;
|
|
return transform.transformRect(points, out isRect);
|
|
}
|
|
|
|
public static Rect transformRect(this Matrix4x4 transform, Rect rect) {
|
|
bool isRect;
|
|
return transform.transformRect(rect, out isRect);
|
|
}
|
|
|
|
public static Rect inverseTransformRect(this Matrix4x4 transform, Rect rect) {
|
|
D.assert(rect != null);
|
|
D.assert(transform.determinant != 0.0);
|
|
|
|
if (transform.isIdentity) {
|
|
return rect;
|
|
}
|
|
|
|
var inverse = transform.inverse;
|
|
return inverse.transformRect(rect);
|
|
}
|
|
|
|
public static Offset getAsTranslation(this Matrix4x4 transform) {
|
|
if (transform.m00 == 1.0 &&
|
|
transform.m10 == 0.0 &&
|
|
transform.m20 == 0.0 &&
|
|
transform.m30 == 0.0 &&
|
|
transform.m01 == 0.0 &&
|
|
transform.m11 == 1.0 &&
|
|
transform.m21 == 0.0 &&
|
|
transform.m31 == 0.0 &&
|
|
transform.m02 == 0.0 &&
|
|
transform.m12 == 0.0 &&
|
|
transform.m22 == 1.0 &&
|
|
transform.m32 == 0.0 &&
|
|
transform.m23 == 0.0 &&
|
|
transform.m33 == 1.0) {
|
|
return new Offset(transform.m03, transform.m13);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static bool isPerspective(this Matrix4x4 transform) {
|
|
return transform[3, 0] != 0 || transform[3, 1] != 0 || transform[3, 2] != 0 || transform[3, 3] != 1;
|
|
}
|
|
}
|
|
}
|