Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 2565655

Browse files
committed
performance improvements...
1 parent 47c4c94 commit 2565655

File tree

6 files changed

+217
-124
lines changed

6 files changed

+217
-124
lines changed

Runtime/ui/matrix.cs

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -702,23 +702,54 @@ public bool mapRect(out Rect dst, Rect src) {
702702
return true;
703703
}
704704
else {
705-
var points = new[] {
706-
this.mapXY(src.left, src.top),
707-
this.mapXY(src.right, src.top),
708-
this.mapXY(src.right, src.bottom),
709-
this.mapXY(src.left, src.bottom),
710-
};
711-
712-
var minX = points[0].dx;
713-
var minY = points[0].dy;
714-
var maxX = points[0].dx;
715-
var maxY = points[0].dy;
716-
717-
for (int i = 1; i < 4; ++i) {
718-
minX = Mathf.Min(minX, points[i].dx);
719-
minY = Mathf.Min(minY, points[i].dy);
720-
maxX = Mathf.Max(maxX, points[i].dx);
721-
maxY = Mathf.Max(maxY, points[i].dy);
705+
float x1, y1, x2, y2, x3, y3, x4, y4;
706+
this.mapXY(src.left, src.top, out x1, out y1);
707+
this.mapXY(src.right, src.top, out x2, out y2);
708+
this.mapXY(src.right, src.bottom, out x3, out y3);
709+
this.mapXY(src.left, src.bottom, out x4, out y4);
710+
711+
var minX = x1;
712+
var minY = y1;
713+
var maxX = x1;
714+
var maxY = y1;
715+
716+
if (x2 < minX) {
717+
minX = x2;
718+
}
719+
if (x2 > maxX) {
720+
maxX = x2;
721+
}
722+
if (y2 < minY) {
723+
minY = y2;
724+
}
725+
if (y2 > maxY) {
726+
maxY = y2;
727+
}
728+
729+
if (x3 < minX) {
730+
minX = x3;
731+
}
732+
if (x3 > maxX) {
733+
maxX = x3;
734+
}
735+
if (y3 < minY) {
736+
minY = y3;
737+
}
738+
if (y3 > maxY) {
739+
maxY = y3;
740+
}
741+
742+
if (x4 < minX) {
743+
minX = x4;
744+
}
745+
if (x4 > maxX) {
746+
maxX = x4;
747+
}
748+
if (y4 < minY) {
749+
minY = y4;
750+
}
751+
if (y4 > maxY) {
752+
maxY = y4;
722753
}
723754

724755
dst = Rect.fromLTRB(minX, minY, maxX, maxY);

Runtime/ui/painting/ArrayRef.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace Unity.UIWidgets.ui {
4+
public class ArrayRef<T> {
5+
public T[] array;
6+
7+
public int length;
8+
9+
public ArrayRef() {
10+
this.array = Array.Empty<T>();
11+
this.length = 0;
12+
}
13+
14+
public void add(T item) {
15+
if (this.length == this.array.Length) {
16+
int newCapacity = this.array.Length == 0 ? 4 : this.array.Length * 2;
17+
Array.Resize(ref this.array, newCapacity);
18+
}
19+
20+
this.array[this.length++] = item;
21+
}
22+
}
23+
}

Runtime/ui/painting/ArrayRef.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/ui/painting/canvas_clip.cs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -91,46 +91,50 @@ public uint getGenID() {
9191
return this._genId;
9292
}
9393

94-
// bool _convexContains(float x, float y) {
95-
// if (this.mesh.vertices.Count <= 2) {
96-
// return false;
97-
// }
98-
//
99-
// if (this.mesh.matrix != null) {
100-
// if (this._invMat == null) {
101-
// this._invMat = Matrix3.I();
102-
// this.mesh.matrix.invert(this._invMat); // ignore if not invertible for now.
103-
// }
104-
//
105-
// var offset = this._invMat.mapXY(x, y);
106-
// x = offset.dx;
107-
// y = offset.dy;
108-
// }
109-
//
110-
// for (var i = 0; i < this.mesh.vertices.Count; i++) {
111-
// var p0 = this.mesh.vertices[i];
112-
// var p1 = this.mesh.vertices[i == this.mesh.vertices.Count - 1 ? 0 : i + 1];
113-
//
114-
// if (PathUtils.triarea2(p0.x, p0.y, p1.x, p1.y, x, y) < 0.0f) {
115-
// return false;
116-
// }
117-
// }
118-
//
119-
// return true;
120-
// }
94+
bool _convexContains(Rect rect) {
95+
if (this.mesh.vertices.Count <= 2) {
96+
return false;
97+
}
98+
99+
for (var i = 0; i < this.mesh.vertices.Count; i++) {
100+
var p1 = this.mesh.vertices[i];
101+
var p0 = this.mesh.vertices[i == this.mesh.vertices.Count - 1 ? 0 : i + 1];
102+
103+
var v = p1 - p0;
104+
if (v.x == 0.0 && v.y == 0.0) {
105+
continue;
106+
}
107+
108+
float yL = v.y * (rect.left - p0.x);
109+
float xT = v.x * (rect.top - p0.y);
110+
float yR = v.y * (rect.right - p0.x);
111+
float xB = v.x * (rect.bottom - p0.y);
112+
113+
if ((xT < yL) || (xT < yR) || (xB < yL) || (xB < yR)) {
114+
return false;
115+
}
116+
}
117+
118+
return true;
119+
}
121120

122121
public bool contains(Rect rect) {
123122
if (this.isRect) {
124123
return this.rect.contains(rect);
125124
}
126125

127-
// this seems to be inefficient. disable it for now.
128-
// if (this.convex) {
129-
// return this._convexContains(rect.left, rect.top) &&
130-
// this._convexContains(rect.left, rect.bottom) &&
131-
// this._convexContains(rect.right, rect.top) &&
132-
// this._convexContains(rect.right, rect.bottom);
133-
// }
126+
if (this.convex) {
127+
if (this.mesh.matrix != null && !this.mesh.matrix.isIdentity()) {
128+
if (this._invMat == null) {
129+
this._invMat = Matrix3.I();
130+
this.mesh.matrix.invert(this._invMat); // ignore if not invertible for now.
131+
}
132+
133+
rect = this._invMat.mapRect(rect);
134+
}
135+
136+
return this._convexContains(rect);
137+
}
134138

135139
return false;
136140
}

0 commit comments

Comments
 (0)