[csharp] Refactored SkeletonClipping, spits out vertices and uvs separately so it can be easily reused in XNA and Unity

This commit is contained in:
badlogic 2017-04-19 15:38:51 +02:00
parent 4cd5ccc5b8
commit cd788ebbba
5 changed files with 75 additions and 63 deletions

View File

@ -235,6 +235,7 @@ cp -f ../spineboy/export/spineboy.png ../../spine-ts/widget/example/assets/
echo "spine-xna" echo "spine-xna"
rm -f ../../spine-xna/example/data/* rm -f ../../spine-xna/example/data/*
cp -f ../coin/export/coin.json ../../spine-xna/example/data/ cp -f ../coin/export/coin.json ../../spine-xna/example/data/
cp -f ../coin/export/coin.json ../../spine-xna/example/data/
cp -f ../coin/export/coin.atlas ../../spine-xna/example/data/ cp -f ../coin/export/coin.atlas ../../spine-xna/example/data/
cp -f ../coin/export/coin.png ../../spine-xna/example/data/ cp -f ../coin/export/coin.png ../../spine-xna/example/data/

View File

@ -37,6 +37,7 @@ namespace Spine {
private readonly ExposedList<float> clipOutput = new ExposedList<float>(128); private readonly ExposedList<float> clipOutput = new ExposedList<float>(128);
private readonly ExposedList<float> clippedVertices = new ExposedList<float>(128); private readonly ExposedList<float> clippedVertices = new ExposedList<float>(128);
private readonly ExposedList<short> clippedTriangles = new ExposedList<short>(128); private readonly ExposedList<short> clippedTriangles = new ExposedList<short>(128);
private readonly ExposedList<float> clippedUVs = new ExposedList<float>(128);
private readonly ExposedList<float> scratch = new ExposedList<float>(); private readonly ExposedList<float> scratch = new ExposedList<float>();
private ClippingAttachment clipAttachment; private ClippingAttachment clipAttachment;
@ -44,6 +45,7 @@ namespace Spine {
public ExposedList<float> ClippedVertices { get { return clippedVertices; } } public ExposedList<float> ClippedVertices { get { return clippedVertices; } }
public ExposedList<short> ClippedTriangles { get { return clippedTriangles; } } public ExposedList<short> ClippedTriangles { get { return clippedTriangles; } }
public ExposedList<float> ClippedUVs { get { return clippedUVs; } }
public void ClipStart(Slot slot, ClippingAttachment clip) { public void ClipStart(Slot slot, ClippingAttachment clip) {
if (clipAttachment != null) return; if (clipAttachment != null) return;
@ -78,17 +80,16 @@ namespace Spine {
return clipAttachment != null; return clipAttachment != null;
} }
public void ClipTriangles(float[] vertices, int verticesLength, short[] triangles, int trianglesLength, float[] uvs, public void ClipTriangles(float[] vertices, int verticesLength, short[] triangles, int trianglesLength, float[] uvs) {
float light, float dark, bool twoColor) {
ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices; ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
var clippedTriangles = this.clippedTriangles; var clippedTriangles = this.clippedTriangles;
var polygons = clippingPolygons.Items; var polygons = clippingPolygons.Items;
int polygonsCount = clippingPolygons.Count; int polygonsCount = clippingPolygons.Count;
int vertexSize = twoColor ? 6 : 5;
short index = 0; short index = 0;
clippedVertices.Clear(); clippedVertices.Clear();
clippedUVs.Clear();
clippedTriangles.Clear(); clippedTriangles.Clear();
outer: outer:
for (int i = 0; i < trianglesLength; i += 3) { for (int i = 0; i < trianglesLength; i += 3) {
@ -114,24 +115,18 @@ namespace Spine {
int clipOutputCount = clipOutputLength >> 1; int clipOutputCount = clipOutputLength >> 1;
float[] clipOutputItems = clipOutput.Items; float[] clipOutputItems = clipOutput.Items;
float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * vertexSize).Items; float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items;
float[] clippedUVsItems = clippedUVs.Resize(s + clipOutputCount * 2).Items;
for (int ii = 0; ii < clipOutputLength; ii += 2) { for (int ii = 0; ii < clipOutputLength; ii += 2) {
float x = clipOutputItems[ii], y = clipOutputItems[ii + 1]; float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
clippedVerticesItems[s] = x; clippedVerticesItems[s] = x;
clippedVerticesItems[s + 1] = y; clippedVerticesItems[s + 1] = y;
clippedVerticesItems[s + 2] = light;
if (twoColor) {
clippedVerticesItems[s + 3] = dark;
s += 4;
}
else
s += 3;
float c0 = x - x3, c1 = y - y3; float c0 = x - x3, c1 = y - y3;
float a = (d0 * c0 + d1 * c1) * d; float a = (d0 * c0 + d1 * c1) * d;
float b = (d4 * c0 + d2 * c1) * d; float b = (d4 * c0 + d2 * c1) * d;
float c = 1 - a - b; float c = 1 - a - b;
clippedVerticesItems[s] = u1 * a + u2 * b + u3 * c; clippedUVsItems[s] = u1 * a + u2 * b + u3 * c;
clippedVerticesItems[s + 1] = v1 * a + v2 * b + v3 * c; clippedUVsItems[s + 1] = v1 * a + v2 * b + v3 * c;
s += 2; s += 2;
} }
@ -148,45 +143,21 @@ namespace Spine {
} }
else { else {
float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * vertexSize).Items; float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * 2).Items;
float[] clippedUVsItems = clippedUVs.Resize(s + 3 * 2).Items;
clippedVerticesItems[s] = x1; clippedVerticesItems[s] = x1;
clippedVerticesItems[s + 1] = y1; clippedVerticesItems[s + 1] = y1;
clippedVerticesItems[s + 2] = light; clippedVerticesItems[s + 2] = x2;
if (!twoColor) { clippedVerticesItems[s + 3] = y2;
clippedVerticesItems[s + 3] = u1; clippedVerticesItems[s + 4] = x3;
clippedVerticesItems[s + 4] = v1; clippedVerticesItems[s + 5] = y3;
clippedVerticesItems[s + 5] = x2; clippedUVsItems[s] = u1;
clippedVerticesItems[s + 6] = y2; clippedUVsItems[s + 1] = v1;
clippedVerticesItems[s + 7] = light; clippedUVsItems[s + 2] = u2;
clippedVerticesItems[s + 8] = u2; clippedUVsItems[s + 3] = v2;
clippedVerticesItems[s + 9] = v2; clippedUVsItems[s + 4] = u3;
clippedUVsItems[s + 5] = v3;
clippedVerticesItems[s + 10] = x3;
clippedVerticesItems[s + 11] = y3;
clippedVerticesItems[s + 12] = light;
clippedVerticesItems[s + 13] = u3;
clippedVerticesItems[s + 14] = v3;
}
else {
clippedVerticesItems[s + 3] = dark;
clippedVerticesItems[s + 4] = u1;
clippedVerticesItems[s + 5] = v1;
clippedVerticesItems[s + 6] = x2;
clippedVerticesItems[s + 7] = y2;
clippedVerticesItems[s + 8] = light;
clippedVerticesItems[s + 9] = dark;
clippedVerticesItems[s + 10] = u2;
clippedVerticesItems[s + 11] = v2;
clippedVerticesItems[s + 12] = x3;
clippedVerticesItems[s + 13] = y3;
clippedVerticesItems[s + 14] = light;
clippedVerticesItems[s + 15] = dark;
clippedVerticesItems[s + 16] = u3;
clippedVerticesItems[s + 17] = v3;
}
s = clippedTriangles.Count; s = clippedTriangles.Count;
short[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items; short[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;

Binary file not shown.

View File

@ -116,13 +116,35 @@
<Compile Include="src\ExampleProgram.cs" /> <Compile Include="src\ExampleProgram.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="data\goblins-ffd.png" /> <None Include="data\coin.png">
<Content Include="data\raptor.png" /> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Content Include="data\spineboy.png"> </None>
<None Include="data\coin.skel">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\goblins.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\raptor.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\spineboy.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="data\tank.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="Game.ico" /> <Content Include="Game.ico" />
<Content Include="GameThumbnail.png" /> <Content Include="GameThumbnail.png" />
<None Include="data\tank.atlas">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\tank.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\tank.skel">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\spine-csharp\spine-csharp.csproj"> <ProjectReference Include="..\..\spine-csharp\spine-csharp.csproj">
@ -162,10 +184,27 @@
</BootstrapperPackage> </BootstrapperPackage>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="data\goblins-ffd.atlas" /> <None Include="data\coin.atlas">
<None Include="data\goblins-ffd.json" /> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<None Include="data\raptor.atlas" /> </None>
<None Include="data\raptor.json" /> <None Include="data\coin.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\goblins-mesh.atlas">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\goblins-mesh.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\goblins-mesh.skel">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\raptor.atlas">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\raptor.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="data\spineboy.atlas"> <None Include="data\spineboy.atlas">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>

View File

@ -76,8 +76,8 @@ namespace Spine {
// String name = "spineboy"; // String name = "spineboy";
// String name = "goblins-mesh"; // String name = "goblins-mesh";
// String name = "raptor"; // String name = "raptor";
String name = "tank"; // String name = "tank";
// String name = "star"; String name = "coin";
bool binaryData = true; bool binaryData = true;
Atlas atlas = new Atlas(assetsFolder + name + ".atlas", new XnaTextureLoader(GraphicsDevice)); Atlas atlas = new Atlas(assetsFolder + name + ".atlas", new XnaTextureLoader(GraphicsDevice));
@ -86,6 +86,7 @@ namespace Spine {
if (name == "spineboy") scale = 0.6f; if (name == "spineboy") scale = 0.6f;
if (name == "raptor") scale = 0.5f; if (name == "raptor") scale = 0.5f;
if (name == "tank") scale = 0.3f; if (name == "tank") scale = 0.3f;
if (name == "coin") scale = 1;
SkeletonData skeletonData; SkeletonData skeletonData;
if (binaryData) { if (binaryData) {
@ -123,8 +124,8 @@ namespace Spine {
state.SetAnimation(0, "walk", true); state.SetAnimation(0, "walk", true);
state.AddAnimation(1, "gungrab", false, 2); state.AddAnimation(1, "gungrab", false, 2);
} }
else if (name == "star") { else if (name == "coin") {
// no animation in star state.SetAnimation(0, "rotate", true);
} }
else if (name == "tank") { else if (name == "tank") {
state.SetAnimation(0, "drive", true); state.SetAnimation(0, "drive", true);