예제 #1
0
        static void SetDestination(int x, int y, float value)
        {
            var col = destination.PixelUnSafe(x, y);

            col.r = value;
            col.g = value;
            col.b = value;

            destination.SetPixelUnSafe(x, y, col);
        }
예제 #2
0
        /// <param name="maxInside">
        /// Maximum pixel distance measured inside the edge, resulting in an alpha value of 1.
        /// If set to or below 0, everything inside will have an alpha value of 1.
        /// </param>
        /// <param name="maxOutside">
        /// Maximum pixel distance measured outside the edge, resulting in an alpha value of 0.
        /// If set to or below 0, everything outside will have an alpha value of 0.
        /// </param>
        /// <param name="postProcessDistance">
        /// Pixel distance from the edge within which pixels will be post-processed using the edge gradient.
        /// </param>

        static void SetColor(int x, int y, float value, float scale)
        {
            var col = img.PixelUnSafe(x, y);

            col.r = value;
            col.g = value;
            col.b = value;

            img.SetPixelUnSafe(x, y, col);
        }
예제 #3
0
        public static IEnumerator Generate(
            TextureMeta image,
            float maxInside,
            float maxOutside,
            float postProcessDistance)
        {
            width  = image.width;
            height = image.height;

            destination = image;

            if (image.Pixels == null)
            {
                Debug.LogError("Pixels are null");
                yield break;
            }

            pixels = new Pixel[width, height];
            int   x, y;
            float scale;

            yield return(QcAsync.CallAgain("Creating pixels"));

            yield return(QcAsync.CallAfter_Thread(() => InitializePixels(), "Creating pixels coroutine"));

            yield return(QcAsync.CallAgain("Filling max Inside"));

            //INSIDE
            if (maxInside > 0f)
            {
                for (var e = ComputeEdgeGradients(); e.MoveNext();)
                {
                    yield return(e.Current);
                }

                for (var e = GenerateDistanceTransform(); e.MoveNext();)
                {
                    yield return(e.Current);
                }

                if (postProcessDistance > 0f)
                {
                    for (var e = PostProcess(postProcessDistance); e.MoveNext();)
                    {
                        yield return(e.Current);
                    }
                }


                yield return(QcAsync.CallAgain("Setting Inside Pixels"));

                scale = 1f / maxInside;
                for (y = 0; y < height; y++)
                {
                    for (x = 0; x < width; x++)
                    {
                        SetDestination(x, y, Mathf.Clamp01(pixels[x, y].distance * scale));
                    }

                    yield return(QcAsync.CallAgain("Inside Pixels {0}".F(y)));
                }
            }

            yield return(QcAsync.CallAgain("Filling max Outside"));


            //OUTSIDE
            if (maxOutside > 0f)
            {
                yield return(QcAsync.CallAfter_Thread(() => {
                    for (y = 0; y < height; y++)
                    {
                        for (x = 0; x < width; x++)
                        {
                            pixels[x, y].FlipOriginal();
                        }
                    }
                }, "Flipping pixels"));


                for (var e = ComputeEdgeGradients(); e.MoveNext();)
                {
                    yield return(e.Current);
                }

                for (var e = GenerateDistanceTransform(); e.MoveNext();)
                {
                    yield return(e.Current);
                }

                if (postProcessDistance > 0f)
                {
                    for (var e = PostProcess(postProcessDistance); e.MoveNext();)
                    {
                        yield return(e.Current);
                    }
                }

                scale = 1f / maxOutside;
                if (maxInside > 0f)
                {
                    for (y = 0; y < height; y++)
                    {
                        for (x = 0; x < width; x++)
                        {
                            float value = 0.5f + (destination.PixelUnSafe(x, y).r -
                                                  Mathf.Clamp01(pixels[x, y].distance * scale)) * 0.5f;
                            SetDestination(x, y, value);
                        }
                    }

                    yield return(QcAsync.CallAgain("Setting Outside Pixels {0}/{1}".F(y, height)));
                }
                else
                {
                    for (y = 0; y < height; y++)
                    {
                        for (x = 0; x < width; x++)
                        {
                            var value = Mathf.Clamp01(1f - pixels[x, y].distance * scale);
                            SetDestination(x, y, value);
                        }

                        yield return(QcAsync.CallAgain("Setting Outside Pixels {0}/{1}".F(y, height)));
                    }
                }
            }


            pixels = null;
        }
예제 #4
0
        public static void Generate(
            TextureMeta image,
            float maxInside,
            float maxOutside,
            float postProcessDistance)
        {
            width  = image.width;
            height = image.height;

            img = image;

            pixels = new Pixel[width, height];
            int   x, y;
            float scale;

            //Color c = Color.black;

            for (y = 0; y < height; y++)
            {
                for (x = 0; x < width; x++)
                {
                    pixels[x, y] = new Pixel();
                }
            }

            if (maxInside > 0f)
            {
                for (y = 0; y < height; y++)
                {
                    for (x = 0; x < width; x++)
                    {
                        pixels[x, y].originalValue = 1f - image.PixelUnSafe(x, y).grayscale;
                    }
                }

                ComputeEdgeGradients();
                GenerateDistanceTransform();
                if (postProcessDistance > 0f)
                {
                    PostProcess(postProcessDistance);
                }

                scale = 1f / maxInside;

                for (y = 0; y < height; y++)
                {
                    for (x = 0; x < width; x++)
                    {
                        SetColor(x, y, Mathf.Clamp01(pixels[x, y].distance * scale), scale);
                    }
                }
            }

            if (maxOutside > 0f)
            {
                for (y = 0; y < height; y++)
                {
                    for (x = 0; x < width; x++)
                    {
                        pixels[x, y].originalValue = image.PixelUnSafe(x, y).r;
                    }
                }

                ComputeEdgeGradients();
                GenerateDistanceTransform();
                if (postProcessDistance > 0f)
                {
                    PostProcess(postProcessDistance);
                }

                scale = 1f / maxOutside;
                if (maxInside > 0f)
                {
                    for (y = 0; y < height; y++)
                    {
                        for (x = 0; x < width; x++)
                        {
                            float value = 0.5f + (image.PixelUnSafe(x, y).r - Mathf.Clamp01(pixels[x, y].distance * scale)) * 0.5f;
                            SetColor(x, y, value, scale);
                        }
                    }
                }
                else
                {
                    for (y = 0; y < height; y++)
                    {
                        for (x = 0; x < width; x++)
                        {
                            var value = Mathf.Clamp01(1f - pixels[x, y].distance * scale);
                            SetColor(x, y, value, scale);
                        }
                    }
                }
            }


            pixels = null;
        }