public static Texture2D SetPixelsForChannel(Texture2D texture, float[] pixels, WpColorChannels channel) { Color[] srcPixels = texture.GetPixels(); for (int i = 0; i < texture.width * texture.height; i++) { switch (channel) { default: srcPixels[i].r = pixels[i]; break; case WpColorChannels.G: srcPixels[i].g = pixels[i]; break; case WpColorChannels.B: srcPixels[i].b = pixels[i]; break; case WpColorChannels.A: srcPixels[i].a = pixels[i]; break; } } Texture2D resultTexture = texture; resultTexture.SetPixels(srcPixels); resultTexture.Apply(); return(resultTexture); }
public WpGrayscaleImage(Texture2D texture, WpColorChannels channel) { Width = texture.width; Height = texture.height; m_Pixels = new byte[Width * Height]; Color[] srcPixels = texture.GetPixels(); for (int i = 0; i < Width * Height; i++) { switch (channel) { default: m_Pixels[i] = (byte)(srcPixels[i].r * 255.0f); break; case WpColorChannels.G: m_Pixels[i] = (byte)(srcPixels[i].g * 255.0f); break; case WpColorChannels.B: m_Pixels[i] = (byte)(srcPixels[i].b * 255.0f); break; case WpColorChannels.A: m_Pixels[i] = (byte)(srcPixels[i].a * 255.0f); break; } } }
public static Texture2D FillChannelWithValue(Texture2D texture, WpColorChannels channel, float value) { Color[] srcPixels = texture.GetPixels(); for (int i = 0; i < texture.width * texture.height; i++) { switch (channel) { case WpColorChannels.R: srcPixels[i].r = value; break; case WpColorChannels.G: srcPixels[i].g = value; break; case WpColorChannels.B: srcPixels[i].b = value; break; default: srcPixels[i].a = value; break; case WpColorChannels.Rgb: srcPixels[i].r = value; srcPixels[i].g = value; srcPixels[i].b = value; break; } } Texture2D resultsTexture = new Texture2D(texture.width, texture.height, texture.format, false); resultsTexture.SetPixels(srcPixels); resultsTexture.Apply(); return(resultsTexture); }
public static Vector2 CalculateGeometricalCenter(Texture2D texture, Color lookupColor, float tolerance, WpColorChannels channel) { int totalX = 0, totalY = 0, totalPoints = 0; Color[] srcPixels = texture.GetPixels(); for (int x = 0; x < texture.width; x++) { for (int y = 0; y < texture.height; y++) { Color pixelColor = srcPixels[y * texture.width + x]; switch (channel) { case WpColorChannels.R: if (Mathf.Abs(pixelColor.r - lookupColor.r) <= tolerance) { totalX += x; totalY += y; totalPoints++; } break; case WpColorChannels.G: if (Mathf.Abs(pixelColor.g - lookupColor.g) <= tolerance) { totalX += x; totalY += y; totalPoints++; } break; case WpColorChannels.B: if (Mathf.Abs(pixelColor.b - lookupColor.b) <= tolerance) { totalX += x; totalY += y; totalPoints++; } break; default: if (Mathf.Abs(pixelColor.a - lookupColor.a) <= tolerance) { totalX += x; totalY += y; totalPoints++; } break; case WpColorChannels.Rgb: if (Mathf.Abs(pixelColor.r - lookupColor.r) <= tolerance && Mathf.Abs(pixelColor.g - lookupColor.g) <= tolerance && Mathf.Abs(pixelColor.b - lookupColor.b) <= tolerance) { totalX += x; totalY += y; totalPoints++; } break; } } } if (totalPoints <= 0) { return(new Vector2(texture.width / 2f, texture.height / 2f)); } totalX /= totalPoints; totalY /= totalPoints; Debug.Log("Found geometrical center at" + DimensionsString(totalX, totalY)); return(new Vector2(totalX, totalY)); }