public static UVOrientation GetNorthEastVectors(Polygon polygon, Transform brushTransform) { Vertex vertex1; Vertex vertex2; Vertex vertex3; // Get three vertices which will reliably give us good UV information (i.e. not collinear) GetPrimaryPolygonDescribers(polygon, out vertex1, out vertex2, out vertex3); // Take 3 positions and their corresponding UVs Vector3 pos1 = brushTransform.TransformPoint(vertex1.Position); Vector3 pos2 = brushTransform.TransformPoint(vertex2.Position); Vector3 pos3 = brushTransform.TransformPoint(vertex3.Position); Vector2 uv1 = vertex1.UV; Vector2 uv2 = vertex2.UV; Vector2 uv3 = vertex3.UV; // Construct a matrix to map to the triangle's UV space Matrix2x2 uvMatrix = new Matrix2x2() { m00 = uv2.x - uv1.x, m10 = uv3.x - uv1.x, m01 = uv2.y - uv1.y, m11 = uv3.y - uv1.y, }; // Invert the matrix to map from UV space Matrix2x2 uvMatrixInverted = uvMatrix.Inverse; // Construct a matrix to map to the triangle's world space Matrix3x2 positionMatrix = new Matrix3x2() { m00 = pos2.x - pos1.x, m10 = pos3.x - pos1.x, m01 = pos2.y - pos1.y, m11 = pos3.y - pos1.y, m02 = pos2.z - pos1.z, m12 = pos3.z - pos1.z, }; // Multiply the inverted UVs by the positional matrix to get the UV vectors in world space Matrix3x2 multipliedMatrix = positionMatrix.Multiply(uvMatrixInverted); // Extract the world vectors that correspond to UV north (0,1) and UV east (1,0). Note that these aren't // normalized and their magnitude is the reciprocal of tiling Vector3 eastVectorScaled = new Vector3(multipliedMatrix.m00, multipliedMatrix.m01, multipliedMatrix.m02); Vector3 northVectorScaled = new Vector3(multipliedMatrix.m10, multipliedMatrix.m11, multipliedMatrix.m12); return(new UVOrientation() { NorthVector = northVectorScaled.normalized, EastVector = eastVectorScaled.normalized, NorthScale = northVectorScaled.magnitude, EastScale = eastVectorScaled.magnitude, }); }
public Matrix3x2 Multiply(Matrix2x2 o) { return(new Matrix3x2() { m00 = this.m00 * o.m00 + this.m10 * o.m01, m10 = this.m00 * o.m10 + this.m10 * o.m11, m01 = this.m01 * o.m00 + this.m11 * o.m01, m11 = this.m01 * o.m10 + this.m11 * o.m11, m02 = this.m02 * o.m00 + this.m12 * o.m01, m12 = this.m02 * o.m10 + this.m12 * o.m11, }); }