Esempio n. 1
0
        void UpdateSpriteTextureQuads()
        {
            if (!Visible)
            {
                quad.BottomRight.Vertices =
                    quad.TopLeft.Vertices = quad.TopRight.Vertices = quad.BottomLeft.Vertices = new CCVertex3F(0, 0, 0);
            }
            else
            {
                CCPoint relativeOffset = unflippedOffsetPositionFromCenter;

                if (flipX)
                {
                    relativeOffset.X = -relativeOffset.X;
                }
                if (flipY)
                {
                    relativeOffset.Y = -relativeOffset.Y;
                }

                CCPoint centerPoint = UntrimmedSizeInPixels.Center + relativeOffset;
                CCPoint subRectOrigin;
                subRectOrigin.X = centerPoint.X - textureRectInPixels.Size.Width / 2.0f;
                subRectOrigin.Y = centerPoint.Y - textureRectInPixels.Size.Height / 2.0f;

                CCRect subRectRatio = CCRect.Zero;

                if (UntrimmedSizeInPixels.Width > 0 && UntrimmedSizeInPixels.Height > 0)
                {
                    subRectRatio = new CCRect(
                        subRectOrigin.X / UntrimmedSizeInPixels.Width,
                        subRectOrigin.Y / UntrimmedSizeInPixels.Height,
                        textureRectInPixels.Size.Width / UntrimmedSizeInPixels.Width,
                        textureRectInPixels.Size.Height / UntrimmedSizeInPixels.Height);
                }

                // Atlas: Vertex
                float x1 = subRectRatio.Origin.X * ContentSize.Width;
                float y1 = subRectRatio.Origin.Y * ContentSize.Height;
                float x2 = x1 + (subRectRatio.Size.Width * ContentSize.Width);
                float y2 = y1 + (subRectRatio.Size.Height * ContentSize.Height);

                // Don't set z-value: The node's transform will be set to include z offset
                quad.BottomLeft.Vertices  = new CCVertex3F(x1, y1, 0);
                quad.BottomRight.Vertices = new CCVertex3F(x2, y1, 0);
                quad.TopLeft.Vertices     = new CCVertex3F(x1, y2, 0);
                quad.TopRight.Vertices    = new CCVertex3F(x2, y2, 0);

                CCTexture2D tex = batchNode != null ? textureAtlas.Texture : texture;
                if (tex == null)
                {
                    return;
                }

                float atlasWidth  = tex.PixelsWide;
                float atlasHeight = tex.PixelsHigh;

                float left, right, top, bottom;

                if (IsTextureRectRotated)
                {
                    #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                    left   = (2 * textureRectInPixels.Origin.X + 1) / (2 * atlasWidth);
                    right  = left + (textureRectInPixels.Size.Height * 2 - 2) / (2 * atlasWidth);
                    top    = (2 * textureRectInPixels.Origin.Y + 1) / (2 * atlasHeight);
                    bottom = top + (textureRectInPixels.Size.Width * 2 - 2) / (2 * atlasHeight);
                    #else
                    left   = textureRectInPixels.Origin.X / atlasWidth;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Height) / atlasWidth;
                    top    = textureRectInPixels.Origin.Y / atlasHeight;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Width) / atlasHeight;
                    #endif

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    quad.BottomLeft.TexCoords.U  = left;
                    quad.BottomLeft.TexCoords.V  = top;
                    quad.BottomRight.TexCoords.U = left;
                    quad.BottomRight.TexCoords.V = bottom;
                    quad.TopLeft.TexCoords.U     = right;
                    quad.TopLeft.TexCoords.V     = top;
                    quad.TopRight.TexCoords.U    = right;
                    quad.TopRight.TexCoords.V    = bottom;
                }
                else
                {
                    #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                    left   = (2 * textureRectInPixels.Origin.X + 1) / (2 * atlasWidth);
                    right  = left + (textureRectInPixels.Size.Width * 2 - 2) / (2 * atlasWidth);
                    top    = (2 * textureRectInPixels.Origin.Y + 1) / (2 * atlasHeight);
                    bottom = top + (textureRectInPixels.Size.Height * 2 - 2) / (2 * atlasHeight);
                    #else
                    left   = textureRectInPixels.Origin.X / atlasWidth;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Width) / atlasWidth;
                    top    = textureRectInPixels.Origin.Y / atlasHeight;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Height) / atlasHeight;
                    #endif

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    quad.BottomLeft.TexCoords.U  = left;
                    quad.BottomLeft.TexCoords.V  = bottom;
                    quad.BottomRight.TexCoords.U = right;
                    quad.BottomRight.TexCoords.V = bottom;
                    quad.TopLeft.TexCoords.U     = left;
                    quad.TopLeft.TexCoords.V     = top;
                    quad.TopRight.TexCoords.U    = right;
                    quad.TopRight.TexCoords.V    = top;
                }
            }

            UpdateTransformedSpriteTextureQuads();
        }
Esempio n. 2
0
        void UpdateSpriteTextureQuadsCallback(ref CCV3F_C4B_T2F_Quad[] quads)
        {
            if (!Visible)
            {
                quads[0].BottomRight.Vertices             = quads[0].TopLeft.Vertices
                                                          = quads[0].TopRight.Vertices = quads[0].BottomLeft.Vertices = CCVertex3F.Zero;
            }
            else
            {
                CCPoint relativeOffset = unflippedOffsetPositionFromCenter;

                if (flipX)
                {
                    relativeOffset.X = -relativeOffset.X;
                }
                if (flipY)
                {
                    relativeOffset.Y = -relativeOffset.Y;
                }

                CCPoint centerPoint = UntrimmedSizeInPixels.Center + relativeOffset;
                CCPoint subRectOrigin;
                subRectOrigin.X = centerPoint.X - textureRectInPixels.Size.Width / 2.0f;
                subRectOrigin.Y = centerPoint.Y - textureRectInPixels.Size.Height / 2.0f;

                CCRect subRectRatio = CCRect.Zero;

                if (UntrimmedSizeInPixels.Width > 0 && UntrimmedSizeInPixels.Height > 0)
                {
                    subRectRatio = new CCRect(
                        subRectOrigin.X / UntrimmedSizeInPixels.Width,
                        subRectOrigin.Y / UntrimmedSizeInPixels.Height,
                        textureRectInPixels.Size.Width / UntrimmedSizeInPixels.Width,
                        textureRectInPixels.Size.Height / UntrimmedSizeInPixels.Height);
                }

                // Atlas: Vertex
                float x1 = subRectRatio.Origin.X * ContentSize.Width;
                float y1 = subRectRatio.Origin.Y * ContentSize.Height;
                float x2 = x1 + (subRectRatio.Size.Width * ContentSize.Width);
                float y2 = y1 + (subRectRatio.Size.Height * ContentSize.Height);

                // Don't set z-value: The node's transform will be set to include z offset
                quads[0].BottomLeft.Vertices  = new CCVertex3F(x1, y1, 0);
                quads[0].BottomRight.Vertices = new CCVertex3F(x2, y1, 0);
                quads[0].TopLeft.Vertices     = new CCVertex3F(x1, y2, 0);
                quads[0].TopRight.Vertices    = new CCVertex3F(x2, y2, 0);

                if (Texture == null)
                {
                    return;
                }

                float atlasWidth  = Texture.PixelsWide;
                float atlasHeight = Texture.PixelsHigh;

                float left, right, top, bottom;
                float offsetW = HalfTexelOffset ? 0.5f / atlasWidth : 0.0f;
                float offsetH = HalfTexelOffset ? 0.5f / atlasHeight : 0.0f;

                if (IsTextureRectRotated)
                {
                    left   = textureRectInPixels.Origin.X / atlasWidth + offsetW;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Height) / atlasWidth - offsetW;
                    top    = textureRectInPixels.Origin.Y / atlasHeight + offsetH;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Width) / atlasHeight - offsetH;

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    quads[0].BottomLeft.TexCoords.U  = left;
                    quads[0].BottomLeft.TexCoords.V  = top;
                    quads[0].BottomRight.TexCoords.U = left;
                    quads[0].BottomRight.TexCoords.V = bottom;
                    quads[0].TopLeft.TexCoords.U     = right;
                    quads[0].TopLeft.TexCoords.V     = top;
                    quads[0].TopRight.TexCoords.U    = right;
                    quads[0].TopRight.TexCoords.V    = bottom;
                }
                else
                {
                    left   = textureRectInPixels.Origin.X / atlasWidth + offsetW;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Width) / atlasWidth - offsetW;
                    top    = textureRectInPixels.Origin.Y / atlasHeight + offsetH;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Height) / atlasHeight - offsetH;

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    quads[0].BottomLeft.TexCoords.U  = left;
                    quads[0].BottomLeft.TexCoords.V  = bottom;
                    quads[0].BottomRight.TexCoords.U = right;
                    quads[0].BottomRight.TexCoords.V = bottom;
                    quads[0].TopLeft.TexCoords.U     = left;
                    quads[0].TopLeft.TexCoords.V     = top;
                    quads[0].TopRight.TexCoords.U    = right;
                    quads[0].TopRight.TexCoords.V    = top;
                }
            }
        }