Exemplo n.º 1
0
        public static void Swirl(SS_Texture texture, int aX, int aY, int radius, float twists)
        {
            SS_Texture tmpTexture = new SS_Texture(texture.Width, texture.Height, Color.clear);

            for (int y = 0; y < texture.Height; y++)
            {
                for (int x = 0; x < texture.Width; x++)
                {
                    // compute the distance and angle from the swirl center:
                    int   pixelX        = x - aX;
                    int   pixelY        = y - aY;
                    float pixelDistance = Mathf.Sqrt((pixelX * pixelX) + (pixelY * pixelY));
                    float pixelAngle    = Mathf.Atan2(pixelY, pixelX);

                    // work out how much of a swirl to apply (1.0 in the center fading out to 0.0 at the radius):

                    float swirlAmount = 1.0f - (pixelDistance / radius);

                    if (swirlAmount > 0.0f)
                    {
                        float twistAngle = twists * swirlAmount * Mathf.PI * 2.0f;

                        // adjust the pixel angle and compute the adjusted pixel co-ordinates:
                        pixelAngle += twistAngle;
                        pixelX      = (int)(Mathf.Cos(pixelAngle) * pixelDistance);
                        pixelY      = (int)(Mathf.Sin(pixelAngle) * pixelDistance);
                    }

                    // read and write the pixel
                    tmpTexture.SetPixel(x, y, texture.GetPixel(aX + pixelX, aY + pixelY));
                }
            }

            texture.SetPixels(tmpTexture.GetPixels());
        }
Exemplo n.º 2
0
        public static void FloodFillArea(SS_Texture texture, SS_Point pt, Color aColor)
        {
            int w = texture.Width;
            int h = texture.Height;

            Color[] colors = texture.GetPixels();
            Color   refCol = colors[pt.x + pt.y * w];

            Queue <SS_Point> nodes = new Queue <SS_Point>();

            nodes.Enqueue(new SS_Point(pt.x, pt.y));

            while (nodes.Count > 0)
            {
                SS_Point current = nodes.Dequeue();

                for (int i = current.x; i < w; i++)
                {
                    Color C = colors[i + current.y * w];

                    if (C != refCol || C == aColor)
                    {
                        break;
                    }

                    colors[i + current.y * w] = aColor;

                    if (current.y + 1 < h)
                    {
                        C = colors[i + current.y * w + w];
                        if (C == refCol && C != aColor)
                        {
                            nodes.Enqueue(new SS_Point(i, current.y + 1));
                        }
                    }

                    if (current.y - 1 >= 0)
                    {
                        C = colors[i + current.y * w - w];
                        if (C == refCol && C != aColor)
                        {
                            nodes.Enqueue(new SS_Point(i, current.y - 1));
                        }
                    }
                }

                for (int i = current.x - 1; i >= 0; i--)
                {
                    Color C = colors[i + current.y * w];

                    if (C != refCol || C == aColor)
                    {
                        break;
                    }

                    colors[i + current.y * w] = aColor;

                    if (current.y + 1 < h)
                    {
                        C = colors[i + current.y * w + w];

                        if (C == refCol && C != aColor)
                        {
                            nodes.Enqueue(new SS_Point(i, current.y + 1));
                        }
                    }

                    if (current.y - 1 >= 0)
                    {
                        C = colors[i + current.y * w - w];

                        if (C == refCol && C != aColor)
                        {
                            nodes.Enqueue(new SS_Point(i, current.y - 1));
                        }
                    }
                }
            }

            texture.SetPixels(colors);
        }