Commit 4bc4c978 authored by Viktor Lidholt's avatar Viktor Lidholt

Merge pull request #1299 from vlidholt/master

Removes artifacts from textured lines in sprites
parents fa119c38 ac6cda4d
...@@ -75,6 +75,8 @@ class TexturedLinePainter { ...@@ -75,6 +75,8 @@ class TexturedLinePainter {
_calculatedTextureStops = null; _calculatedTextureStops = null;
} }
bool removeArtifacts = true;
sky.TransferMode transferMode = sky.TransferMode.srcOver; sky.TransferMode transferMode = sky.TransferMode.srcOver;
Paint _cachedPaint = new Paint(); Paint _cachedPaint = new Paint();
...@@ -184,8 +186,26 @@ class TexturedLinePainter { ...@@ -184,8 +186,26 @@ class TexturedLinePainter {
Offset offset0 = new Offset(miter[0] * halfWidth, miter[1] * halfWidth); Offset offset0 = new Offset(miter[0] * halfWidth, miter[1] * halfWidth);
Offset offset1 = new Offset(-miter[0] * halfWidth, -miter[1] * halfWidth); Offset offset1 = new Offset(-miter[0] * halfWidth, -miter[1] * halfWidth);
vertices.add(point + offset0); Point vertex0 = point + offset0;
vertices.add(point + offset1); Point vertex1 = point + offset1;
int vertexCount = vertices.length;
if (removeArtifacts && vertexCount >= 2) {
Point oldVertex0 = vertices[vertexCount - 2];
Point oldVertex1 = vertices[vertexCount - 1];
Point intersection = GameMath.lineIntersection(oldVertex0, oldVertex1, vertex0, vertex1);
if (intersection != null) {
if (GameMath.pointQuickDist(vertex0, intersection) < GameMath.pointQuickDist(vertex1, intersection)) {
vertex0 = oldVertex0;
} else {
vertex1 = oldVertex1;
}
}
}
vertices.add(vertex0);
vertices.add(vertex1);
} }
void _calculateTextureStops() { void _calculateTextureStops() {
......
...@@ -105,4 +105,29 @@ class GameMath { ...@@ -105,4 +105,29 @@ class GameMath {
static Point filterPoint(Point a, Point b, double filterFactor) { static Point filterPoint(Point a, Point b, double filterFactor) {
return new Point(filter(a.x, b.x, filterFactor), filter(a.y, b.y, filterFactor)); return new Point(filter(a.x, b.x, filterFactor), filter(a.y, b.y, filterFactor));
} }
static Point lineIntersection(Point p, Point p2, Point q, Point q2) {
double epsilon = 1e-10;
Vector2 r = new Vector2(p2.x - p.x, p2.y - p.y);
Vector2 s = new Vector2(q2.x - q.x, q2.y - q.y);
Vector2 qp = new Vector2(q.x - p.x, q.y - p.y);
double rxs = cross2(r, s);
if (rxs.abs() < epsilon) {
// The lines are linear or collinear
return null;
}
double t = cross2(qp, s) / rxs;
double u = cross2(qp, r) / rxs;
if ((0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0)) {
return new Point(p.x + t * r.x, p.y + t * r.y);
}
// No intersection between the lines
return null;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment