Ejemplo n.º 1
0
        private void UpdateTextureColor(Texture2D original, Texture2D modified, int offsetX, int offsetY, int realWidth, int realHeight, int alphaOffsetX, int alphaOffsetY, float offsetMultiplier)
        {
            if (original == null || modified == null)
            {
                return;
            }

            Color[] pixels    = original.GetPixels();
            Color[] newPixels = modified.GetPixels();

            int width  = (int)Vector2.Distance(new Vector2(alphaOffsetX, alphaOffsetY), new Vector2(alphaOffsetX + realHeight, alphaOffsetY + realHeight));
            int center = width / 2;

            float angle = 0.0F;

            if (styleProperties.colorType == ColorType.AxialGradient)
            {
                angle = styleProperties.angle;
            }

            float xStep = Mathf.Cos(Mathf.Deg2Rad * angle);
            float yStep = Mathf.Sin(Mathf.Deg2Rad * angle);

            Vector2 start = new Vector2(-xStep * realWidth, -yStep * realHeight);
            Vector2 end   = new Vector2(xStep * realWidth, yStep * realHeight);
            Vector2 dir   = end - start;

            Vector2Int offset = Vector2Int.zero;

            if (styleProperties.colorType != ColorType.SolidColor)
            {
                offset = styleProperties.offset;
            }

            for (int i = 0; i < pixels.Length; i++)
            {
                Color col = styleColor;

                if (styleProperties.colorType != ColorType.SolidColor)
                {
                    int   x = i % original.width + offsetX;
                    int   y = i / original.width + offsetY;
                    float gradientPercent;

                    if (styleProperties.colorType == ColorType.RadialGradient)
                    {
                        x -= (int)(offset.x * offsetMultiplier);
                        y -= (int)(offset.y * offsetMultiplier);
                        int distance = (x - center) * (x - center) + (y - center) * (y - center);
                        gradientPercent = Mathf.Sqrt(distance) / (float)center;
                    }
                    else                     // Gradient interface
                    {
                        x -= (int)(offset.x * offsetMultiplier);
                        y -= (int)(offset.y * offsetMultiplier);

                        Vector2 pix = new Vector2(x - center, y - center) * 2 - start;
                        float   dot = Vector2.Dot(pix, dir);
                        dot            *= 1 / dir.sqrMagnitude;
                        gradientPercent = dot;
                    }
                    col = styleProperties.gradient.Evaluate(Mathf.PingPong(gradientPercent, 1.0F));
                }

                newPixels[i].r = ColorUtils.OverlayBlend(pixels[i].r, col.r);
                newPixels[i].g = ColorUtils.OverlayBlend(pixels[i].g, col.g);
                newPixels[i].b = ColorUtils.OverlayBlend(pixels[i].b, col.b);
            }

            modified.SetPixels(newPixels);
            modified.Apply();
        }