Exemplo n.º 1
0
        public void Draw(OpenGL gl, Skeleton skeleton)
        {
            if (skeleton == null)
            {
                return;
            }

            var   drawOrder = skeleton.DrawOrder;
            var   drawOrderItems = skeleton.DrawOrder.Items;
            float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A;

            for (int i = 0, n = drawOrder.Count; i < n; i++)
            {
                float[]    vertices   = this.vertices;
                Slot       slot       = drawOrderItems[i];
                Attachment attachment = slot.Attachment;
                if (attachment is RegionAttachment)
                {
                    RegionAttachment regionAttachment = (RegionAttachment)attachment;

                    MeshItem item = batcher.NextItem(4, 6);
                    item.triangles = quadTriangles;
                    VertexPositionColorTexture[] itemVertices = item.vertices;

                    AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject;
                    item.texture = (uint)region.page.rendererObject;

                    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);
                    itemVertices[TL].position = new Vector2(vertices[RegionAttachment.X1], vertices[RegionAttachment.Y1]);
                    itemVertices[BL].position = new Vector2(vertices[RegionAttachment.X2], vertices[RegionAttachment.Y2]);
                    itemVertices[BR].position = new Vector2(vertices[RegionAttachment.X3], vertices[RegionAttachment.Y3]);
                    itemVertices[TR].position = new Vector2(vertices[RegionAttachment.X4], vertices[RegionAttachment.Y4]);

                    float[] uvs = regionAttachment.UVs;
                    itemVertices[TL].textureCoordinate = new Vector2(uvs[RegionAttachment.X1], uvs[RegionAttachment.Y1]);
                    itemVertices[BL].textureCoordinate = new Vector2(uvs[RegionAttachment.X2], uvs[RegionAttachment.Y2]);
                    itemVertices[BR].textureCoordinate = new Vector2(uvs[RegionAttachment.X3], uvs[RegionAttachment.Y3]);
                    itemVertices[TR].textureCoordinate = new Vector2(uvs[RegionAttachment.X4], uvs[RegionAttachment.Y4]);
                }
                else if (attachment is MeshAttachment)
                {
                    MeshAttachment mesh        = (MeshAttachment)attachment;
                    int            vertexCount = mesh.WorldVerticesLength;
                    if (vertices.Length < vertexCount)
                    {
                        vertices = new float[vertexCount];
                    }
                    mesh.ComputeWorldVertices(slot, vertices);

                    int[]    triangles = mesh.Triangles;
                    MeshItem item      = batcher.NextItem(vertexCount, triangles.Length);
                    item.triangles = triangles;

                    AtlasRegion region = (AtlasRegion)mesh.RendererObject;
                    item.texture = (uint)region.page.rendererObject;

                    Color color;
                    float a = skeletonA * slot.A * mesh.A;
                    if (premultipliedAlpha)
                    {
                        color = new Color(
                            skeletonR * slot.R * mesh.R * a,
                            skeletonG * slot.G * mesh.G * a,
                            skeletonB * slot.B * mesh.B * a, a);
                    }
                    else
                    {
                        color = new Color(
                            skeletonR * slot.R * mesh.R,
                            skeletonG * slot.G * mesh.G,
                            skeletonB * slot.B * mesh.B, a);
                    }

                    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          = new Vector2(vertices[v], vertices[v + 1]);
                        itemVertices[ii].textureCoordinate = new Vector2(uvs[v], uvs[v + 1]);
                    }
                }
            }

            batcher.Draw(gl);
        }