/// <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); }
public static void Generate( ImageMeta 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; }