mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-08 03:36:52 +08:00
[xna] Refactored SkeletonMeshRenderer in preparation for clipping
This commit is contained in:
parent
cd788ebbba
commit
e5f9795d15
@ -78,7 +78,7 @@ namespace Spine {
|
|||||||
// String name = "raptor";
|
// String name = "raptor";
|
||||||
// String name = "tank";
|
// String name = "tank";
|
||||||
String name = "coin";
|
String name = "coin";
|
||||||
bool binaryData = true;
|
bool binaryData = false;
|
||||||
|
|
||||||
Atlas atlas = new Atlas(assetsFolder + name + ".atlas", new XnaTextureLoader(GraphicsDevice));
|
Atlas atlas = new Atlas(assetsFolder + name + ".atlas", new XnaTextureLoader(GraphicsDevice));
|
||||||
|
|
||||||
|
|||||||
@ -46,7 +46,7 @@ namespace Spine {
|
|||||||
MeshBatcher batcher;
|
MeshBatcher batcher;
|
||||||
RasterizerState rasterizerState;
|
RasterizerState rasterizerState;
|
||||||
float[] vertices = new float[8];
|
float[] vertices = new float[8];
|
||||||
int[] quadTriangles = { 0, 1, 2, 1, 3, 2 };
|
int[] quadTriangles = { 0, 1, 2, 2, 3, 0 };
|
||||||
BlendState defaultBlendState;
|
BlendState defaultBlendState;
|
||||||
|
|
||||||
BasicEffect effect;
|
BasicEffect effect;
|
||||||
@ -88,107 +88,89 @@ namespace Spine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw (Skeleton skeleton) {
|
public void Draw(Skeleton skeleton) {
|
||||||
float[] vertices = this.vertices;
|
|
||||||
var drawOrder = skeleton.DrawOrder;
|
var drawOrder = skeleton.DrawOrder;
|
||||||
var drawOrderItems = skeleton.DrawOrder.Items;
|
var drawOrderItems = skeleton.DrawOrder.Items;
|
||||||
float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A;
|
float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A;
|
||||||
|
Color color = new Color();
|
||||||
|
|
||||||
for (int i = 0, n = drawOrder.Count; i < n; i++) {
|
for (int i = 0, n = drawOrder.Count; i < n; i++) {
|
||||||
Slot slot = drawOrderItems[i];
|
Slot slot = drawOrderItems[i];
|
||||||
Attachment attachment = slot.Attachment;
|
Attachment attachment = slot.Attachment;
|
||||||
|
|
||||||
|
float attachmentColorR, attachmentColorG, attachmentColorB, attachmentColorA;
|
||||||
|
Texture2D texture = null;
|
||||||
|
int verticesCount = 0;
|
||||||
|
float[] vertices = this.vertices;
|
||||||
|
int indicesCount = 0;
|
||||||
|
int[] indices = null;
|
||||||
|
float[] uvs = null;
|
||||||
|
|
||||||
if (attachment is RegionAttachment) {
|
if (attachment is RegionAttachment) {
|
||||||
RegionAttachment regionAttachment = (RegionAttachment)attachment;
|
RegionAttachment regionAttachment = (RegionAttachment)attachment;
|
||||||
BlendState blend = slot.Data.BlendMode == BlendMode.Additive ? BlendState.Additive : defaultBlendState;
|
attachmentColorR = regionAttachment.R; attachmentColorG = regionAttachment.G; attachmentColorB = regionAttachment.B; attachmentColorA = regionAttachment.A;
|
||||||
if (device.BlendState != blend) {
|
|
||||||
End();
|
|
||||||
device.BlendState = blend;
|
|
||||||
}
|
|
||||||
|
|
||||||
MeshItem item = batcher.NextItem(4, 6);
|
|
||||||
item.triangles = quadTriangles;
|
|
||||||
VertexPositionColorTexture[] itemVertices = item.vertices;
|
|
||||||
|
|
||||||
AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject;
|
AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject;
|
||||||
item.texture = (Texture2D)region.page.rendererObject;
|
texture = (Texture2D)region.page.rendererObject;
|
||||||
|
verticesCount = 4;
|
||||||
Color color;
|
|
||||||
float a = skeletonA * slot.A * regionAttachment.A;
|
|
||||||
if (premultipliedAlpha) {
|
|
||||||
color = new Color(
|
|
||||||
skeletonR * slot.R * regionAttachment.R * a,
|
|
||||||
skeletonG * slot.G * regionAttachment.G * a,
|
|
||||||
skeletonB * slot.B * regionAttachment.B * a, a);
|
|
||||||
} else {
|
|
||||||
color = new Color(
|
|
||||||
skeletonR * slot.R * regionAttachment.R,
|
|
||||||
skeletonG * slot.G * regionAttachment.G,
|
|
||||||
skeletonB * slot.B * regionAttachment.B, a);
|
|
||||||
}
|
|
||||||
itemVertices[TL].Color = color;
|
|
||||||
itemVertices[BL].Color = color;
|
|
||||||
itemVertices[BR].Color = color;
|
|
||||||
itemVertices[TR].Color = color;
|
|
||||||
|
|
||||||
regionAttachment.ComputeWorldVertices(slot.Bone, vertices, 0, 2);
|
regionAttachment.ComputeWorldVertices(slot.Bone, vertices, 0, 2);
|
||||||
itemVertices[TL].Position.X = vertices[0];
|
indicesCount = 6;
|
||||||
itemVertices[TL].Position.Y = vertices[1];
|
indices = quadTriangles;
|
||||||
itemVertices[TL].Position.Z = 0;
|
uvs = regionAttachment.UVs;
|
||||||
itemVertices[BL].Position.X = vertices[2];
|
}
|
||||||
itemVertices[BL].Position.Y = vertices[3];
|
else if (attachment is MeshAttachment) {
|
||||||
itemVertices[BL].Position.Z = 0;
|
|
||||||
itemVertices[BR].Position.X = vertices[4];
|
|
||||||
itemVertices[BR].Position.Y = vertices[5];
|
|
||||||
itemVertices[BR].Position.Z = 0;
|
|
||||||
itemVertices[TR].Position.X = vertices[6];
|
|
||||||
itemVertices[TR].Position.Y = vertices[7];
|
|
||||||
itemVertices[TR].Position.Z = 0;
|
|
||||||
|
|
||||||
float[] uvs = regionAttachment.UVs;
|
|
||||||
itemVertices[TL].TextureCoordinate.X = uvs[0];
|
|
||||||
itemVertices[TL].TextureCoordinate.Y = uvs[1];
|
|
||||||
itemVertices[BL].TextureCoordinate.X = uvs[2];
|
|
||||||
itemVertices[BL].TextureCoordinate.Y = uvs[3];
|
|
||||||
itemVertices[BR].TextureCoordinate.X = uvs[4];
|
|
||||||
itemVertices[BR].TextureCoordinate.Y = uvs[5];
|
|
||||||
itemVertices[TR].TextureCoordinate.X = uvs[6];
|
|
||||||
itemVertices[TR].TextureCoordinate.Y = uvs[7];
|
|
||||||
} else if (attachment is MeshAttachment) {
|
|
||||||
MeshAttachment mesh = (MeshAttachment)attachment;
|
MeshAttachment mesh = (MeshAttachment)attachment;
|
||||||
|
attachmentColorR = mesh.R; attachmentColorG = mesh.G; attachmentColorB = mesh.B; attachmentColorA = mesh.A;
|
||||||
|
AtlasRegion region = (AtlasRegion)mesh.RendererObject;
|
||||||
|
texture = (Texture2D)region.page.rendererObject;
|
||||||
int vertexCount = mesh.WorldVerticesLength;
|
int vertexCount = mesh.WorldVerticesLength;
|
||||||
if (vertices.Length < vertexCount) vertices = new float[vertexCount];
|
if (vertices.Length < vertexCount) vertices = new float[vertexCount];
|
||||||
|
verticesCount = vertexCount >> 1;
|
||||||
mesh.ComputeWorldVertices(slot, vertices);
|
mesh.ComputeWorldVertices(slot, vertices);
|
||||||
|
indicesCount = mesh.Triangles.Length;
|
||||||
|
indices = mesh.Triangles;
|
||||||
|
uvs = mesh.UVs;
|
||||||
|
}
|
||||||
|
else if (attachment is ClippingAttachment) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
int[] triangles = mesh.Triangles;
|
// set blend state
|
||||||
MeshItem item = batcher.NextItem(vertexCount, triangles.Length);
|
BlendState blend = slot.Data.BlendMode == BlendMode.Additive ? BlendState.Additive : defaultBlendState;
|
||||||
item.triangles = triangles;
|
if (device.BlendState != blend) {
|
||||||
|
End();
|
||||||
|
device.BlendState = blend;
|
||||||
|
}
|
||||||
|
|
||||||
AtlasRegion region = (AtlasRegion)mesh.RendererObject;
|
// calculate color
|
||||||
item.texture = (Texture2D)region.page.rendererObject;
|
float a = skeletonA * slot.A * attachmentColorA;
|
||||||
|
if (premultipliedAlpha) {
|
||||||
|
color = new Color(
|
||||||
|
skeletonR * slot.R * attachmentColorR * a,
|
||||||
|
skeletonG * slot.G * attachmentColorG * a,
|
||||||
|
skeletonB * slot.B * attachmentColorB * a, a);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
color = new Color(
|
||||||
|
skeletonR * slot.R * attachmentColorR,
|
||||||
|
skeletonG * slot.G * attachmentColorG,
|
||||||
|
skeletonB * slot.B * attachmentColorB, a);
|
||||||
|
}
|
||||||
|
|
||||||
Color color;
|
// submit to batch
|
||||||
float a = skeletonA * slot.A * mesh.A;
|
MeshItem item = batcher.NextItem(verticesCount, indicesCount);
|
||||||
if (premultipliedAlpha) {
|
item.texture = texture;
|
||||||
color = new Color(
|
item.triangles = indices;
|
||||||
skeletonR * slot.R * mesh.R * a,
|
VertexPositionColorTexture[] itemVertices = item.vertices;
|
||||||
skeletonG * slot.G * mesh.G * a,
|
for (int ii = 0, v = 0, nn = verticesCount << 1; v < nn; ii++, v += 2) {
|
||||||
skeletonB * slot.B * mesh.B * a, a);
|
itemVertices[ii].Color = color;
|
||||||
} else {
|
itemVertices[ii].Position.X = vertices[v];
|
||||||
color = new Color(
|
itemVertices[ii].Position.Y = vertices[v + 1];
|
||||||
skeletonR * slot.R * mesh.R,
|
itemVertices[ii].Position.Z = 0;
|
||||||
skeletonG * slot.G * mesh.G,
|
itemVertices[ii].TextureCoordinate.X = uvs[v];
|
||||||
skeletonB * slot.B * mesh.B, a);
|
itemVertices[ii].TextureCoordinate.Y = uvs[v + 1];
|
||||||
}
|
|
||||||
|
|
||||||
float[] uvs = mesh.UVs;
|
|
||||||
VertexPositionColorTexture[] itemVertices = item.vertices;
|
|
||||||
for (int ii = 0, v = 0; v < vertexCount; ii++, v += 2) {
|
|
||||||
itemVertices[ii].Color = color;
|
|
||||||
itemVertices[ii].Position.X = vertices[v];
|
|
||||||
itemVertices[ii].Position.Y = vertices[v + 1];
|
|
||||||
itemVertices[ii].Position.Z = 0;
|
|
||||||
itemVertices[ii].TextureCoordinate.X = uvs[v];
|
|
||||||
itemVertices[ii].TextureCoordinate.Y = uvs[v + 1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user