[csharp] Fixed port of ConvexDecomposer, SkeletonClipping

This commit is contained in:
badlogic 2017-04-19 15:15:37 +02:00
parent 69711476fc
commit 4cd5ccc5b8
2 changed files with 249 additions and 252 deletions

View File

@ -42,9 +42,9 @@ namespace Spine {
private readonly Pool<ExposedList<float>> polygonPool = new Pool<ExposedList<float>>(); private readonly Pool<ExposedList<float>> polygonPool = new Pool<ExposedList<float>>();
private readonly Pool<ExposedList<short>> polygonIndicesPool = new Pool<ExposedList<short>>(); private readonly Pool<ExposedList<short>> polygonIndicesPool = new Pool<ExposedList<short>>();
public ExposedList<ExposedList<float>> Decompose(float[] input) { public ExposedList<ExposedList<float>> Decompose(ExposedList<float> input) {
var vertices = input; var vertices = input.Items;
int vertexCount = input.Length >> 1; int vertexCount = input.Count >> 1;
var indicesArray = this.indicesArray; var indicesArray = this.indicesArray;
indicesArray.Clear(); indicesArray.Clear();

View File

@ -32,288 +32,285 @@ using System;
namespace Spine { namespace Spine {
public class SkeletonClipping { public class SkeletonClipping {
// private final ConvexDecomposer decomposer = new ConvexDecomposer(); private readonly ConvexDecomposer decomposer = new ConvexDecomposer();
// private final FloatArray clippingPolygon = new FloatArray(); private readonly ExposedList<float> clippingPolygon = new ExposedList<float>();
// private final FloatArray clipOutput = new FloatArray(128); private readonly ExposedList<float> clipOutput = new ExposedList<float>(128);
// private final FloatArray clippedVertices = new FloatArray(128); private readonly ExposedList<float> clippedVertices = new ExposedList<float>(128);
// private final ShortArray clippedTriangles = new ShortArray(128); private readonly ExposedList<short> clippedTriangles = new ExposedList<short>(128);
// private final FloatArray scratch = new FloatArray(); private readonly ExposedList<float> scratch = new ExposedList<float>();
// private ClippingAttachment clipAttachment; private ClippingAttachment clipAttachment;
// private Array<FloatArray> clippingPolygons; private ExposedList<ExposedList<float>> clippingPolygons;
// public void clipStart(Slot slot, ClippingAttachment clip) { public ExposedList<float> ClippedVertices { get { return clippedVertices; } }
// if (clipAttachment != null) return; public ExposedList<short> ClippedTriangles { get { return clippedTriangles; } }
// clipAttachment = clip;
// int n = clip.getWorldVerticesLength(); public void ClipStart(Slot slot, ClippingAttachment clip) {
// float[] vertices = clippingPolygon.setSize(n); if (clipAttachment != null) return;
// clip.computeWorldVertices(slot, 0, n, vertices, 0, 2); clipAttachment = clip;
// makeClockwise(clippingPolygon);
// clippingPolygons = decomposer.decompose(clippingPolygon);
// for (FloatArray polygon : clippingPolygons) {
// makeClockwise(polygon);
// polygon.add(polygon.items[0]);
// polygon.add(polygon.items[1]);
// }
// }
// public void clipEnd(Slot slot) { int n = clip.worldVerticesLength;
// if (clipAttachment != null && clipAttachment.getEndSlot() == slot.getData()) clipEnd(); float[] vertices = clippingPolygon.Resize(n).Items;
// } clip.ComputeWorldVertices(slot, 0, n, vertices, 0, 2);
MakeClockwise(clippingPolygon);
// public void clipEnd() { clippingPolygons = decomposer.Decompose(clippingPolygon);
// if (clipAttachment == null) return; foreach (var polygon in clippingPolygons) {
// clipAttachment = null; MakeClockwise(polygon);
// clippingPolygons = null; polygon.Add(polygon.Items[0]);
// clippedVertices.clear(); polygon.Add(polygon.Items[1]);
// clippedTriangles.clear(); }
// clippingPolygon.clear(); }
// }
public void ClipEnd(Slot slot) {
// public boolean isClipping() { if (clipAttachment != null && clipAttachment.endSlot == slot.data) ClipEnd();
// return clipAttachment != null; }
// }
public void ClipEnd() {
// public void clipTriangles(float[] vertices, int verticesLength, short[] triangles, int trianglesLength, float[] uvs, if (clipAttachment == null) return;
// float light, float dark, boolean twoColor) { clipAttachment = null;
clippingPolygons = null;
// FloatArray clipOutput = this.clipOutput, clippedVertices = this.clippedVertices; clippedVertices.Clear();
// ShortArray clippedTriangles = this.clippedTriangles; clippedTriangles.Clear();
// Object[] polygons = clippingPolygons.items; clippingPolygon.Clear();
// int polygonsCount = clippingPolygons.size; }
// int vertexSize = twoColor ? 6 : 5;
public bool IsClipping() {
// short index = 0; return clipAttachment != null;
// clippedVertices.clear(); }
// clippedTriangles.clear();
// outer: public void ClipTriangles(float[] vertices, int verticesLength, short[] triangles, int trianglesLength, float[] uvs,
// for (int i = 0; i < trianglesLength; i += 3) { float light, float dark, bool twoColor) {
// int vertexOffset = triangles[i] << 1;
// float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1]; ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
// float u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1]; var clippedTriangles = this.clippedTriangles;
var polygons = clippingPolygons.Items;
// vertexOffset = triangles[i + 1] << 1; int polygonsCount = clippingPolygons.Count;
// float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1]; int vertexSize = twoColor ? 6 : 5;
// float u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1];
short index = 0;
// vertexOffset = triangles[i + 2] << 1; clippedVertices.Clear();
// float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1]; clippedTriangles.Clear();
// float u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1]; outer:
for (int i = 0; i < trianglesLength; i += 3) {
// for (int p = 0; p < polygonsCount; p++) { int vertexOffset = triangles[i] << 1;
// int s = clippedVertices.size; float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
// if (clip(x1, y1, x2, y2, x3, y3, (FloatArray)polygons[p], clipOutput)) { float u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1];
// int clipOutputLength = clipOutput.size;
// if (clipOutputLength == 0) continue; vertexOffset = triangles[i + 1] << 1;
// float d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1; float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
// float d = 1 / (d0 * d2 + d1 * (y1 - y3)); float u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1];
// int clipOutputCount = clipOutputLength >> 1; vertexOffset = triangles[i + 2] << 1;
// float[] clipOutputItems = clipOutput.items; float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
// float[] clippedVerticesItems = clippedVertices.setSize(s + clipOutputCount * vertexSize); float u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1];
// for (int ii = 0; ii < clipOutputLength; ii += 2) {
// float x = clipOutputItems[ii], y = clipOutputItems[ii + 1]; for (int p = 0; p < polygonsCount; p++) {
// clippedVerticesItems[s] = x; int s = clippedVertices.Count;
// clippedVerticesItems[s + 1] = y; if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) {
// clippedVerticesItems[s + 2] = light; int clipOutputLength = clipOutput.Count;
// if (twoColor) { if (clipOutputLength == 0) continue;
// clippedVerticesItems[s + 3] = dark; float d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1;
// s += 4; float d = 1 / (d0 * d2 + d1 * (y1 - y3));
// }
// else int clipOutputCount = clipOutputLength >> 1;
// s += 3; float[] clipOutputItems = clipOutput.Items;
// float c0 = x - x3, c1 = y - y3; float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * vertexSize).Items;
// float a = (d0 * c0 + d1 * c1) * d; for (int ii = 0; ii < clipOutputLength; ii += 2) {
// float b = (d4 * c0 + d2 * c1) * d; float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
// float c = 1 - a - b; clippedVerticesItems[s] = x;
// clippedVerticesItems[s] = u1 * a + u2 * b + u3 * c; clippedVerticesItems[s + 1] = y;
// clippedVerticesItems[s + 1] = v1 * a + v2 * b + v3 * c; clippedVerticesItems[s + 2] = light;
// s += 2; if (twoColor) {
// } clippedVerticesItems[s + 3] = dark;
s += 4;
// s = clippedTriangles.size; }
// short[] clippedTrianglesItems = clippedTriangles.setSize(s + 3 * (clipOutputCount - 2)); else
// clipOutputCount--; s += 3;
// for (int ii = 1; ii < clipOutputCount; ii++) { float c0 = x - x3, c1 = y - y3;
// clippedTrianglesItems[s] = index; float a = (d0 * c0 + d1 * c1) * d;
// clippedTrianglesItems[s + 1] = (short)(index + ii); float b = (d4 * c0 + d2 * c1) * d;
// clippedTrianglesItems[s + 2] = (short)(index + ii + 1); float c = 1 - a - b;
// s += 3; clippedVerticesItems[s] = u1 * a + u2 * b + u3 * c;
// } clippedVerticesItems[s + 1] = v1 * a + v2 * b + v3 * c;
// index += clipOutputCount + 1; s += 2;
}
// }
// else { s = clippedTriangles.Count;
// float[] clippedVerticesItems = clippedVertices.setSize(s + 3 * vertexSize); short[] clippedTrianglesItems = clippedTriangles.Resize(s + 3 * (clipOutputCount - 2)).Items;
// clippedVerticesItems[s] = x1; clipOutputCount--;
// clippedVerticesItems[s + 1] = y1; for (int ii = 1; ii < clipOutputCount; ii++) {
// clippedVerticesItems[s + 2] = light; clippedTrianglesItems[s] = index;
// if (!twoColor) { clippedTrianglesItems[s + 1] = (short)(index + ii);
// clippedVerticesItems[s + 3] = u1; clippedTrianglesItems[s + 2] = (short)(index + ii + 1);
// clippedVerticesItems[s + 4] = v1; s += 3;
}
// clippedVerticesItems[s + 5] = x2; index += (short)(clipOutputCount + 1);
// clippedVerticesItems[s + 6] = y2;
// clippedVerticesItems[s + 7] = light; }
// clippedVerticesItems[s + 8] = u2; else {
// clippedVerticesItems[s + 9] = v2; float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * vertexSize).Items;
clippedVerticesItems[s] = x1;
// clippedVerticesItems[s + 10] = x3; clippedVerticesItems[s + 1] = y1;
// clippedVerticesItems[s + 11] = y3; clippedVerticesItems[s + 2] = light;
// clippedVerticesItems[s + 12] = light; if (!twoColor) {
// clippedVerticesItems[s + 13] = u3; clippedVerticesItems[s + 3] = u1;
// clippedVerticesItems[s + 14] = v3; clippedVerticesItems[s + 4] = v1;
// }
// else { clippedVerticesItems[s + 5] = x2;
// clippedVerticesItems[s + 3] = dark; clippedVerticesItems[s + 6] = y2;
// clippedVerticesItems[s + 4] = u1; clippedVerticesItems[s + 7] = light;
// clippedVerticesItems[s + 5] = v1; clippedVerticesItems[s + 8] = u2;
clippedVerticesItems[s + 9] = v2;
// clippedVerticesItems[s + 6] = x2;
// clippedVerticesItems[s + 7] = y2; clippedVerticesItems[s + 10] = x3;
// clippedVerticesItems[s + 8] = light; clippedVerticesItems[s + 11] = y3;
// clippedVerticesItems[s + 9] = dark; clippedVerticesItems[s + 12] = light;
// clippedVerticesItems[s + 10] = u2; clippedVerticesItems[s + 13] = u3;
// clippedVerticesItems[s + 11] = v2; clippedVerticesItems[s + 14] = v3;
}
// clippedVerticesItems[s + 12] = x3; else {
// clippedVerticesItems[s + 13] = y3; clippedVerticesItems[s + 3] = dark;
// clippedVerticesItems[s + 14] = light; clippedVerticesItems[s + 4] = u1;
// clippedVerticesItems[s + 15] = dark; clippedVerticesItems[s + 5] = v1;
// clippedVerticesItems[s + 16] = u3;
// clippedVerticesItems[s + 17] = v3; clippedVerticesItems[s + 6] = x2;
// } clippedVerticesItems[s + 7] = y2;
clippedVerticesItems[s + 8] = light;
// s = clippedTriangles.size; clippedVerticesItems[s + 9] = dark;
// short[] clippedTrianglesItems = clippedTriangles.setSize(s + 3); clippedVerticesItems[s + 10] = u2;
// clippedTrianglesItems[s] = index; clippedVerticesItems[s + 11] = v2;
// clippedTrianglesItems[s + 1] = (short)(index + 1);
// clippedTrianglesItems[s + 2] = (short)(index + 2); clippedVerticesItems[s + 12] = x3;
// index += 3; clippedVerticesItems[s + 13] = y3;
// continue outer; clippedVerticesItems[s + 14] = light;
// } clippedVerticesItems[s + 15] = dark;
// } clippedVerticesItems[s + 16] = u3;
// } clippedVerticesItems[s + 17] = v3;
// } }
// /** Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping s = clippedTriangles.Count;
// * area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list. */ short[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;
// boolean clip(float x1, float y1, float x2, float y2, float x3, float y3, FloatArray clippingArea, FloatArray output) { clippedTrianglesItems[s] = index;
// FloatArray originalOutput = output; clippedTrianglesItems[s + 1] = (short)(index + 1);
// boolean clipped = false; clippedTrianglesItems[s + 2] = (short)(index + 2);
index += 3;
// // Avoid copy at the end. goto outer;
// FloatArray input = null; }
// if (clippingArea.size % 4 >= 2) { }
// input = output; }
// output = scratch; }
// }
// else /** Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping
// input = scratch; * area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list. */
internal bool Clip(float x1, float y1, float x2, float y2, float x3, float y3, ExposedList<float> clippingArea, ExposedList<float> output) {
// input.clear(); var originalOutput = output;
// input.add(x1); var clipped = false;
// input.add(y1);
// input.add(x2); // Avoid copy at the end.
// input.add(y2); ExposedList<float> input = null;
// input.add(x3); if (clippingArea.Count % 4 >= 2) {
// input.add(y3); input = output;
// input.add(x1); output = scratch;
// input.add(y1); }
// output.clear(); else
input = scratch;
// float[] clippingVertices = clippingArea.items;
// int clippingVerticesLast = clippingArea.size - 4; input.Clear();
// for (int i = 0; ; i += 2) { input.Add(x1);
// float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1]; input.Add(y1);
// float edgeX2 = clippingVertices[i + 2], edgeY2 = clippingVertices[i + 3]; input.Add(x2);
// float deltaX = edgeX - edgeX2, deltaY = edgeY - edgeY2; input.Add(y2);
input.Add(x3);
// float[] inputVertices = input.items; input.Add(y3);
// int inputVerticesLength = input.size - 2, outputStart = output.size; input.Add(x1);
// for (int ii = 0; ii < inputVerticesLength; ii += 2) { input.Add(y1);
// float inputX = inputVertices[ii], inputY = inputVertices[ii + 1]; output.Clear();
// float inputX2 = inputVertices[ii + 2], inputY2 = inputVertices[ii + 3];
// boolean side2 = deltaX * (inputY2 - edgeY2) - deltaY * (inputX2 - edgeX2) > 0; float[] clippingVertices = clippingArea.Items;
// if (deltaX * (inputY - edgeY2) - deltaY * (inputX - edgeX2) > 0) { int clippingVerticesLast = clippingArea.Count - 4;
// if (side2) { // v1 inside, v2 inside for (int i = 0; ; i += 2) {
// output.add(inputX2); float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1];
// output.add(inputY2); float edgeX2 = clippingVertices[i + 2], edgeY2 = clippingVertices[i + 3];
// continue; float deltaX = edgeX - edgeX2, deltaY = edgeY - edgeY2;
// }
// // v1 inside, v2 outside float[] inputVertices = input.Items;
// float c0 = inputY2 - inputY, c2 = inputX2 - inputX; int inputVerticesLength = input.Count - 2, outputStart = output.Count;
// float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / (c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY)); for (int ii = 0; ii < inputVerticesLength; ii += 2) {
// output.add(edgeX + (edgeX2 - edgeX) * ua); float inputX = inputVertices[ii], inputY = inputVertices[ii + 1];
// output.add(edgeY + (edgeY2 - edgeY) * ua); float inputX2 = inputVertices[ii + 2], inputY2 = inputVertices[ii + 3];
// } bool side2 = deltaX * (inputY2 - edgeY2) - deltaY * (inputX2 - edgeX2) > 0;
// else if (side2) { // v1 outside, v2 inside if (deltaX * (inputY - edgeY2) - deltaY * (inputX - edgeX2) > 0) {
// float c0 = inputY2 - inputY, c2 = inputX2 - inputX; if (side2) { // v1 inside, v2 inside
// float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / (c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY)); output.Add(inputX2);
// output.add(edgeX + (edgeX2 - edgeX) * ua); output.Add(inputY2);
// output.add(edgeY + (edgeY2 - edgeY) * ua); continue;
// output.add(inputX2); }
// output.add(inputY2); // v1 inside, v2 outside
// } float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
// clipped = true; float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / (c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY));
// } output.Add(edgeX + (edgeX2 - edgeX) * ua);
output.Add(edgeY + (edgeY2 - edgeY) * ua);
// if (outputStart == output.size) { // All edges outside. }
// originalOutput.clear(); else if (side2) { // v1 outside, v2 inside
// return true; float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
// } float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / (c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY));
output.Add(edgeX + (edgeX2 - edgeX) * ua);
// output.add(output.items[0]); output.Add(edgeY + (edgeY2 - edgeY) * ua);
// output.add(output.items[1]); output.Add(inputX2);
output.Add(inputY2);
// if (i == clippingVerticesLast) break; }
// FloatArray temp = output; clipped = true;
// output = input; }
// output.clear();
// input = temp; if (outputStart == output.Count) { // All edges outside.
// } originalOutput.Clear();
return true;
// if (originalOutput != output) { }
// originalOutput.clear();
// originalOutput.addAll(output.items, 0, output.size - 2); output.Add(output.Items[0]);
// } output.Add(output.Items[1]);
// else
// originalOutput.setSize(originalOutput.size - 2); if (i == clippingVerticesLast) break;
var temp = output;
// return clipped; output = input;
// } output.Clear();
input = temp;
// public FloatArray getClippedVertices() { }
// return clippedVertices;
// } if (originalOutput != output) {
originalOutput.Clear();
// public ShortArray getClippedTriangles() { for (int i = 0, n = output.Count - 2; i < n; i++) {
// return clippedTriangles; originalOutput.Add(output.Items[i]);
// } }
}
// static void makeClockwise(FloatArray polygon) { else
// float[] vertices = polygon.items; originalOutput.Resize(originalOutput.Count - 2);
// int verticeslength = polygon.size;
return clipped;
// float area = vertices[verticeslength - 2] * vertices[1] - vertices[0] * vertices[verticeslength - 1], p1x, p1y, p2x, p2y; }
// for (int i = 0, n = verticeslength - 3; i < n; i += 2) {
// p1x = vertices[i]; static void MakeClockwise(ExposedList<float> polygon) {
// p1y = vertices[i + 1]; float[] vertices = polygon.Items;
// p2x = vertices[i + 2]; int verticeslength = polygon.Count;
// p2y = vertices[i + 3];
// area += p1x * p2y - p2x * p1y; float area = vertices[verticeslength - 2] * vertices[1] - vertices[0] * vertices[verticeslength - 1], p1x, p1y, p2x, p2y;
// } for (int i = 0, n = verticeslength - 3; i < n; i += 2) {
// if (area < 0) return; p1x = vertices[i];
p1y = vertices[i + 1];
// for (int i = 0, lastX = verticeslength - 2, n = verticeslength >> 1; i < n; i += 2) { p2x = vertices[i + 2];
// float x = vertices[i], y = vertices[i + 1]; p2y = vertices[i + 3];
// int other = lastX - i; area += p1x * p2y - p2x * p1y;
// vertices[i] = vertices[other]; }
// vertices[i + 1] = vertices[other + 1]; if (area < 0) return;
// vertices[other] = x;
// vertices[other + 1] = y; for (int i = 0, lastX = verticeslength - 2, n = verticeslength >> 1; i < n; i += 2) {
// } float x = vertices[i], y = vertices[i + 1];
// } int other = lastX - i;
vertices[i] = vertices[other];
vertices[i + 1] = vertices[other + 1];
vertices[other] = x;
vertices[other + 1] = y;
}
}
} }
} }