protected override void AddedToScene()
        {
            base.AddedToScene();

            var visibleRect = VisibleBoundsWorldspace;

            var item = geoBatch.CreateGeometryInstance(3, 3);

            var vertices = item.GeometryPacket.Vertices;

            var windowSize = VisibleBoundsWorldspace.Size;
            var packet     = item.GeometryPacket;

            packet.Texture      = texture;
            item.GeometryPacket = packet;
            // Draw polygons

            vertices[0].Colors = CCColor4B.White;
            vertices[1].Colors = CCColor4B.White;
            vertices[2].Colors = CCColor4B.White;

            // Texture coordinates use a normalzed value 0 to 1
            vertices[0].TexCoords.U = 0;
            vertices[0].TexCoords.V = 0;

            vertices[1].TexCoords.U = 1;
            vertices[1].TexCoords.V = 0;

            vertices[2].TexCoords.U = 1;
            vertices[2].TexCoords.V = 1;

            vertices[0].Vertices.X = 0;
            vertices[0].Vertices.Y = 0;

            vertices[1].Vertices.X = texture.PixelsWide;
            vertices[1].Vertices.Y = 0;

            vertices[2].Vertices.X = texture.PixelsWide;
            vertices[2].Vertices.Y = texture.PixelsHigh;

            item.GeometryPacket.Indicies = new int[] { 2, 1, 0 };

            var rotation = CCAffineTransform.Identity;

            rotation.Rotation = (float)Math.PI / 4.0f;
            rotation.Tx       = windowSize.Center.X - texture.PixelsWide / 2;
            rotation.Ty       = windowSize.Center.Y - texture.PixelsHigh / 2;

            item.InstanceAttributes.AdditionalTransform = rotation;
        }
Exemple #2
0
        protected void UpdateSkeletonGeometry()
        {
            skeletonGeometry.ClearInstances();
            //defaultBlendState = PremultipliedAlpha ? BlendState.AlphaBlend : BlendState.NonPremultiplied;


            float[]     vertices = this.vertices;
            List <Slot> drawOrder = Skeleton.DrawOrder;
            float       x = Skeleton.X, y = Skeleton.Y;

            CCColor3B color3b   = Color;
            float     skeletonR = color3b.R / 255f;
            float     skeletonG = color3b.G / 255f;
            float     skeletonB = color3b.B / 255f;
            float     skeletonA = Opacity / 255f;

            for (int i = 0, n = drawOrder.Count; i < n; i++)
            {
                Slot       slot       = drawOrder[i];
                Attachment attachment = slot.Attachment;
                if (attachment is RegionAttachment)
                {
                    RegionAttachment regionAttachment = (RegionAttachment)attachment;
                    //BlendState blend = slot.Data.AdditiveBlending ? BlendState.Additive : defaultBlendState;

                    var item = skeletonGeometry.CreateGeometryInstance(4, 6);

                    //item.InstanceAttributes.BlendState = blend;
                    //item.InstanceAttributes.AdditionalTransform = AffineWorldTransform;
                    item.GeometryPacket.Indicies = quadTriangles;

                    var itemVertices = item.GeometryPacket.Vertices;

                    AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject;
                    item.GeometryPacket.Texture = (CCTexture2D)region.page.rendererObject;

                    CCColor4B color;
                    float     a = skeletonA * slot.A * regionAttachment.A;
                    if (PremultipliedAlpha)
                    {
                        color = new CCColor4B(
                            skeletonR * slot.R * regionAttachment.R * a,
                            skeletonG * slot.G * regionAttachment.G * a,
                            skeletonB * slot.B * regionAttachment.B * a, a);
                    }
                    else
                    {
                        color = new CCColor4B(
                            skeletonR * slot.R * regionAttachment.R,
                            skeletonG * slot.G * regionAttachment.G,
                            skeletonB * slot.B * regionAttachment.B, a);
                    }
                    itemVertices[TL].Colors = color;
                    itemVertices[BL].Colors = color;
                    itemVertices[BR].Colors = color;
                    itemVertices[TR].Colors = color;

                    regionAttachment.ComputeWorldVertices(x, y, slot.Bone, vertices);
                    itemVertices[TL].Vertices.X = vertices[RegionAttachment.X1];
                    itemVertices[TL].Vertices.Y = vertices[RegionAttachment.Y1];
                    itemVertices[TL].Vertices.Z = 0;
                    itemVertices[BL].Vertices.X = vertices[RegionAttachment.X2];
                    itemVertices[BL].Vertices.Y = vertices[RegionAttachment.Y2];
                    itemVertices[BL].Vertices.Z = 0;
                    itemVertices[BR].Vertices.X = vertices[RegionAttachment.X3];
                    itemVertices[BR].Vertices.Y = vertices[RegionAttachment.Y3];
                    itemVertices[BR].Vertices.Z = 0;
                    itemVertices[TR].Vertices.X = vertices[RegionAttachment.X4];
                    itemVertices[TR].Vertices.Y = vertices[RegionAttachment.Y4];
                    itemVertices[TR].Vertices.Z = 0;

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

                    int[] triangles = mesh.Triangles;
                    var   item      = skeletonGeometry.CreateGeometryInstance(vertexCount, triangles.Length);
                    //item.InstanceAttributes.AdditionalTransform = AffineWorldTransform;
                    item.GeometryPacket.Indicies = triangles;

                    AtlasRegion region = (AtlasRegion)mesh.RendererObject;
                    item.GeometryPacket.Texture = (CCTexture2D)region.page.rendererObject;

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

                    float[] uvs          = mesh.UVs;
                    var     itemVertices = item.GeometryPacket.Vertices;
                    for (int ii = 0, v = 0; v < vertexCount; ii++, v += 2)
                    {
                        itemVertices[ii].Colors      = color;
                        itemVertices[ii].Vertices.X  = vertices[v];
                        itemVertices[ii].Vertices.Y  = vertices[v + 1];
                        itemVertices[ii].Vertices.Z  = 0;
                        itemVertices[ii].TexCoords.U = uvs[v];
                        itemVertices[ii].TexCoords.V = uvs[v + 1];
                    }
                }
                else if (attachment is SkinnedMeshAttachment)
                {
                    SkinnedMeshAttachment mesh = (SkinnedMeshAttachment)attachment;
                    int vertexCount            = mesh.UVs.Length;
                    if (vertices.Length < vertexCount)
                    {
                        vertices = new float[vertexCount];
                    }
                    mesh.ComputeWorldVertices(x, y, slot, vertices);

                    int[] triangles = mesh.Triangles;
                    var   item      = skeletonGeometry.CreateGeometryInstance(vertexCount, triangles.Length);
                    item.GeometryPacket.Indicies = triangles;
                    //item.InstanceAttributes.AdditionalTransform = AffineWorldTransform;

                    AtlasRegion region = (AtlasRegion)mesh.RendererObject;
                    item.GeometryPacket.Texture = (CCTexture2D)region.page.rendererObject;

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

                    float[] uvs          = mesh.UVs;
                    var     itemVertices = item.GeometryPacket.Vertices;
                    for (int ii = 0, v = 0; v < vertexCount; ii++, v += 2)
                    {
                        itemVertices[ii].Colors      = color;
                        itemVertices[ii].Vertices.X  = vertices[v];
                        itemVertices[ii].Vertices.Y  = vertices[v + 1];
                        itemVertices[ii].Vertices.Z  = 0;
                        itemVertices[ii].TexCoords.U = uvs[v];
                        itemVertices[ii].TexCoords.V = uvs[v + 1];
                    }
                }
            }

            debugger.Clear();

            if (DebugBones || DebugSlots)
            {
                if (DebugSlots)
                {
                    for (int i = 0; i < Skeleton.Slots.Count; ++i)
                    {
                        var slot = Skeleton.Slots[i];

                        if (slot.Attachment == null)
                        {
                            continue;
                        }

                        var verticesCount = 0;
                        var worldVertices = new float[1000]; // Max number of vertices per mesh.
                        if (slot.Attachment is RegionAttachment)
                        {
                            var attachment = (RegionAttachment)slot.Attachment;
                            attachment.ComputeWorldVertices(Skeleton.X, Skeleton.Y, slot.bone, worldVertices);
                            verticesCount = 8;
                        }
                        else if (slot.Attachment is MeshAttachment)
                        {
                            var mesh = (MeshAttachment)slot.Attachment;
                            mesh.ComputeWorldVertices(Skeleton.X, Skeleton.Y, slot, worldVertices);
                            verticesCount = mesh.Vertices.Length;
                        }
                        else if (slot.Attachment is SkinnedMeshAttachment)
                        {
                            var mesh = (SkinnedMeshAttachment)slot.Attachment;
                            mesh.ComputeWorldVertices(Skeleton.X, Skeleton.Y, slot, worldVertices);
                            verticesCount = mesh.UVs.Length;
                        }
                        else
                        {
                            continue;
                        }

                        CCPoint[] slotVertices = new CCPoint[verticesCount / 2];

                        for (int ii = 0, si = 0; ii < verticesCount; ii += 2, si++)
                        {
                            slotVertices[si].X = worldVertices[ii] * ScaleX;
                            slotVertices[si].Y = worldVertices[ii + 1] * ScaleY;
                        }

                        debugger.DrawPolygon(slotVertices, verticesCount / 2, CCColor4B.Transparent, 1, DebugSlotColor);
                    }
                }

                if (DebugBones)
                {
                    // Bone lengths.
                    for (int i = 0; i < Skeleton.Bones.Count; i++)
                    {
                        Bone bone = Skeleton.Bones[i];
                        x = bone.Data.Length * bone.M00 + bone.WorldX;
                        y = bone.Data.Length * bone.M10 + bone.WorldY;

                        debugger.DrawLine(new CCPoint(bone.WorldX, bone.WorldY), new CCPoint(x, y), 1, DebugJointColor);
                    }

                    // Bone origins.
                    for (int i = 0; i < Skeleton.Bones.Count; i++)
                    {
                        Bone bone = Skeleton.Bones[i];
                        debugger.DrawDot(new CCPoint(bone.WorldX, bone.WorldY), 3, DebugBoneColor);
                    }
                }
            }
        }
        public void UpdateSkeletonGeometry()
        {
            skeletonGeometry.ClearInstances();
            BlendState blend;
            var        drawOrder = Skeleton.DrawOrder;
            var        drawOrderItems = Skeleton.DrawOrder.Items;
            float      skeletonR = Skeleton.R, skeletonG = Skeleton.G, skeletonB = Skeleton.B, skeletonA = Skeleton.A;
            Color      color;

            for (int i = 0, n = drawOrder.Count; i < n; i++)
            {
                Slot       slot       = drawOrderItems[i];
                Attachment attachment = slot.Attachment;
                blend = slot.Data.BlendMode == BlendMode.Additive ? BlendState.Additive :
                        PremultipliedAlpha ? BlendState.AlphaBlend : BlendState.NonPremultiplied;
                float       attachmentColorR, attachmentColorG, attachmentColorB, attachmentColorA;
                CCTexture2D texture       = null;
                int         verticesCount = 0;
                //float[] vertices = this.vertices;
                int     indicesCount = 0;
                int[]   indices      = null;
                float[] uvs          = null;

                if (attachment is RegionAttachment)
                {
                    RegionAttachment regionAttachment = (RegionAttachment)attachment;
                    attachmentColorR = regionAttachment.R; attachmentColorG = regionAttachment.G; attachmentColorB = regionAttachment.B; attachmentColorA = regionAttachment.A;
                    AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject;
                    texture       = (CCTexture2D)region.page.rendererObject;
                    verticesCount = 4;
                    regionAttachment.ComputeWorldVertices(slot.Bone, vertices, 0, 2);
                    indicesCount = 6;
                    indices      = quadTriangles;
                    uvs          = regionAttachment.UVs;
                }
                else if (attachment is MeshAttachment)
                {
                    MeshAttachment mesh = (MeshAttachment)attachment;
                    attachmentColorR = mesh.R; attachmentColorG = mesh.G; attachmentColorB = mesh.B; attachmentColorA = mesh.A;
                    AtlasRegion region = (AtlasRegion)mesh.RendererObject;
                    texture = (CCTexture2D)region.page.rendererObject;
                    int vertexCount = mesh.WorldVerticesLength;
                    if (vertices.Length < vertexCount)
                    {
                        vertices = new float[vertexCount];
                    }
                    verticesCount = vertexCount >> 1;
                    mesh.ComputeWorldVertices(slot, vertices);
                    indicesCount = mesh.Triangles.Length;
                    indices      = mesh.Triangles;
                    uvs          = mesh.UVs;
                }
                else if (attachment is ClippingAttachment)
                {
                    ClippingAttachment clip = (ClippingAttachment)attachment;
                    clipper.ClipStart(slot, clip);
                    continue;
                }
                else
                {
                    continue;
                }

                // calculate color
                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 darkColor = new Color();
                //if (slot.HasSecondColor)
                //{
                //      darkColor = new Color(slot.R2 * a, slot.G2 * a, slot.B2 * a);
                //}
                //darkColor.A = PremultipliedAlpha ? (byte)255 : (byte)0;

                // clip
                if (clipper.IsClipping)
                {
                    clipper.ClipTriangles(vertices, verticesCount << 1, indices, indicesCount, uvs);
                    vertices      = clipper.ClippedVertices.Items;
                    verticesCount = clipper.ClippedVertices.Count >> 1;
                    indices       = clipper.ClippedTriangles.Items;
                    indicesCount  = clipper.ClippedTriangles.Count;
                    uvs           = clipper.ClippedUVs.Items;
                }

                if (verticesCount == 0 || indicesCount == 0)
                {
                    continue;
                }

                // submit to batch
                var item = skeletonGeometry.CreateGeometryInstance(verticesCount, indicesCount);
                item.InstanceAttributes.BlendState = blend;
                item.GeometryPacket.Texture        = texture;
                for (int ii = 0, nn = indicesCount; ii < nn; ii++)
                {
                    item.GeometryPacket.Indicies[ii] = indices[ii];
                }

                var itemVertices = item.GeometryPacket.Vertices;
                for (int ii = 0, v = 0, nn = verticesCount << 1; v < nn; ii++, v += 2)
                {
                    itemVertices[ii].Colors = new CCColor4B(color.R, color.G, color.B, color.A);
                    //itemVertices[ii].Colors2 = new CCColor4B(darkColor.R, darkColor.G, darkColor.B, darkColor.A);
                    itemVertices[ii].Vertices.X  = vertices[v];
                    itemVertices[ii].Vertices.Y  = vertices[v + 1];
                    itemVertices[ii].Vertices.Z  = 0;
                    itemVertices[ii].TexCoords.U = uvs[v];
                    itemVertices[ii].TexCoords.V = uvs[v + 1];
                }

                clipper.ClipEnd(slot);
            }
            clipper.ClipEnd();
        }