[libgdx] Added convex decomposition to SkeletonRenderer.

This commit is contained in:
badlogic 2017-03-31 10:22:25 +02:00
parent b8a603d6af
commit ace38dca5f
2 changed files with 132 additions and 124 deletions

View File

@ -90,13 +90,13 @@ public class ClippingTest extends ApplicationAdapter {
ClippingAttachment clip = new ClippingAttachment("clip");
// Rectangle:
clip.setVertices(
new float[] { 87, 288, 217, 371, 456, 361, 539, 175, 304, 194, 392, 290, 193, 214, 123, 15, 14, 137 });
// new float[] { //
// -140, 50, //
// 250, 50, //
// 250, 350, //
// -140, 350, //
// });
// new float[] { 87, 288, 217, 371, 456, 361, 539, 175, 304, 194, 392, 290, 193, 214, 123, 15, 14, 137 });
new float[] { //
-140, 50, //
250, 50, //
250, 350, //
-140, 350, //
});
// Self intersection:
// clip.setVertices(new float[] { //
// -140, -50, //

View File

@ -49,6 +49,7 @@ import com.esotericsoftware.spine.attachments.MeshAttachment;
import com.esotericsoftware.spine.attachments.RegionAttachment;
import com.esotericsoftware.spine.attachments.SkeletonAttachment;
import com.esotericsoftware.spine.utils.Clipper;
import com.esotericsoftware.spine.utils.ConvexDecomposer;
import com.esotericsoftware.spine.utils.TwoColorPolygonBatch;
public class SkeletonRenderer {
@ -58,11 +59,12 @@ public class SkeletonRenderer {
private boolean premultipliedAlpha;
private final FloatArray vertices = new FloatArray(32);
private Clipper clipper = new Clipper();
private ClippingAttachment clipAttachment;
private Slot clipEnd;
private FloatArray clippingArea = new FloatArray();
private boolean clippingAreaClockwise;
private Clipper clipper = new Clipper();
private ConvexDecomposer decomposer = new ConvexDecomposer();
private FloatArray clippingPolygon = new FloatArray(400);
private Array<FloatArray> convexClippingPolygons;
private FloatArray clipOutput = new FloatArray(400);
private FloatArray clippedVertices = new FloatArray(400);
private ShortArray clippedTriangles = new ShortArray(400);
@ -404,18 +406,22 @@ public class SkeletonRenderer {
Gdx.gl.glStencilOp(GL20.GL_KEEP, GL20.GL_KEEP, GL20.GL_KEEP);
} else {
int n = clip.getWorldVerticesLength();
float[] vertices = this.clippingArea.setSize(n);
float[] vertices = this.clippingPolygon.setSize(n);
clip.computeWorldVertices(slot, 0, n, vertices, 0, 2);
clippingAreaClockwise = Clipper.isClockwise(this.clippingArea);
if (!clippingAreaClockwise) {
Clipper.makeClockwise(clippingArea);
convexClippingPolygons = decomposer.decompose(clippingPolygon);
for (FloatArray poly: convexClippingPolygons) {
Clipper.makeClockwise(poly);
poly.add(poly.items[0]);
poly.add(poly.items[1]);
}
clippingArea.add(clippingArea.items[0]);
clippingArea.add(clippingArea.items[1]);
}
}
private void clipEnd() {
clippedVertices.clear();
clippedTriangles.clear();
clippingPolygon.clear();
convexClippingPolygons = null;
clipAttachment = null;
clipEnd = null;
if (!softwareClipping) Gdx.gl.glDisable(GL20.GL_STENCIL_TEST);
@ -425,6 +431,7 @@ public class SkeletonRenderer {
short idx = 0;
clippedVertices.clear();
clippedTriangles.clear();
for (FloatArray convexClippingPolygon: convexClippingPolygons) {
for (int i = 0; i < trianglesLength; i += 3) {
int vertexOffset = triangles[i] << 1;
float x1 = vertices[vertexOffset];
@ -444,7 +451,7 @@ public class SkeletonRenderer {
float u3 = uvs[vertexOffset];
float v3 = uvs[vertexOffset + 1];
boolean clipped = clipper.clip(x1, y1, x2, y2, x3, y3, clippingArea, clipOutput);
boolean clipped = clipper.clip(x1, y1, x2, y2, x3, y3, convexClippingPolygon, clipOutput);
if (clipped) {
if (clipOutput.size == 0) continue;
float d0 = y2 - y3;
@ -546,6 +553,7 @@ public class SkeletonRenderer {
}
}
}
}
public void setPremultipliedAlpha(boolean premultipliedAlpha) {
this.premultipliedAlpha = premultipliedAlpha;