Exemple #1
0
        /// <summary>
        /// Patterns the paint of texture.
        /// </summary>
        /// <returns>The paint.</returns>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <param name="rad">RAD.</param>
        /// <param name="hardness">Hardness.</param>
        /// <param name="tex">Tex.</param>
        /// <param name="pattern">Pattern.</param>
        public static Texture2D PatternPaint(Vector2 from, Vector2 to, float rad, float hardness, Texture2D tex, Texture2D pattern)
        {
            float extentX = (float)(pattern.width / 2);
            float extentY = (float)(pattern.height / 2);

            float stY  = Mathf.Clamp(Mathf.Min(from.y, to.y) - extentY, 0, tex.height);
            float stX  = Mathf.Clamp(Mathf.Min(from.x, to.x) - extentX, 0, tex.width);
            float endY = Mathf.Clamp(Mathf.Max(from.y, to.y) + extentY, 0, tex.height);
            float endX = Mathf.Clamp(Mathf.Max(from.x, to.x) + extentX, 0, tex.width);

            int lengthX = Mathf.RoundToInt(endX - stX);
            int lengthY = Mathf.RoundToInt(endY - stY);

            if (lengthX > pattern.width)
            {
                lengthX = pattern.width;
            }
            if (lengthY > pattern.height)
            {
                lengthY = pattern.height;
            }

            float sqrRad2 = (extentX + 1) * (extentY + 1);

            Color[] pixels    = tex.GetPixels((int)stX, (int)stY, (int)lengthX, (int)lengthY, 0);
            Color[] patPixels = pattern.GetPixels(0, 0, lengthX, lengthY);

            var start = new Vector2(stX, stY);

            for (int y = 0; y < (int)lengthY; y++)
            {
                for (int x = 0; x < (int)lengthX; x++)
                {
                    var   p      = new Vector2(x, y) + start;
                    var   center = p + new Vector2(0.5f, 0.5f);
                    float dist   = (center - PaintMathfx.NearestPointStrict(from, to, center)).sqrMagnitude;
                    if (dist > sqrRad2)
                    {
                        continue;
                    }
                    dist = PaintMathfx.GaussFalloff(Mathf.Sqrt(dist), rad) * hardness;

                    if (patPixels[y * (int)lengthX + x].a > 0.25f)
                    {
                        pixels[y * (int)lengthX + x] = patPixels[y * (int)lengthX + x];
                    }
                }
            }

            tex.SetPixels((int)start.x, (int)start.y, (int)lengthX, (int)lengthY, pixels, 0);
            return(tex);
        }
Exemple #2
0
        /// <summary>
        /// Paint the specified Texture with given color.
        /// Can ignore alpha value, if alpha value is 0 then color wan't affect.
        /// </summary>
        /// <param name="from">Vector2 Start position.</param>
        /// <param name="to">Vector2 End position.</param>
        /// <param name="rad">float Radius area, brush size.</param>
        /// <param name="col">Color to replace the texture with.</param>
        /// <param name="hardness">float opacity of texture color.</param>
        /// <param name="tex">Texture2D texture where color to be paint.</param>
        /// <param name="ignoreAlpha">If set to <c>true</c> ignore the alpha 0 part.</param>
        public static Texture2D Paint(Vector2 from, Vector2 to, float rad, Color col, float hardness, Texture2D tex, bool ignoreAlpha = true)
        {
            var extent = rad;
            var stY    = Mathf.Clamp(Mathf.Min(from.y, to.y) - extent, 0, tex.height);
            var stX    = Mathf.Clamp(Mathf.Min(from.x, to.x) - extent, 0, tex.width);
            var endY   = Mathf.Clamp(Mathf.Max(from.y, to.y) + extent, 0, tex.height);
            var endX   = Mathf.Clamp(Mathf.Max(from.x, to.x) + extent, 0, tex.width);

            var lengthX = endX - stX;
            var lengthY = endY - stY;

            var sqrRad2 = (rad + 1) * (rad + 1);

            Color[] pixels = tex.GetPixels((int)stX, (int)stY, (int)lengthX, (int)lengthY, 0);
            var     start  = new Vector2(stX, stY);

            for (int y = 0; y < (int)lengthY; y++)
            {
                for (int x = 0; x < (int)lengthX; x++)
                {
                    var   p      = new Vector2(x, y) + start;
                    var   center = p + new Vector2(0.5f, 0.5f);
                    float dist   = (center - PaintMathfx.NearestPointStrict(from, to, center)).sqrMagnitude;
                    if (dist > sqrRad2)
                    {
                        continue;
                    }
                    dist = PaintMathfx.GaussFalloff(Mathf.Sqrt(dist), rad) * hardness;

                    Color c;
                    if (dist > 0)
                    {
                        c = Color.Lerp(pixels[y * (int)lengthX + x], col, dist);
                    }
                    else
                    {
                        c = pixels[y * (int)lengthX + x];
                    }

                    if (ignoreAlpha == false)
                    {
                        c.a = pixels[y * (int)lengthX + x].a;
                    }

                    pixels[y * (int)lengthX + x] = c;
                }
            }

            tex.SetPixels((int)start.x, (int)start.y, (int)lengthX, (int)lengthY, pixels, 0);
            return(tex);
        }