Manual: TextureUtils Pixel Operations

Documentation and discussion for Draconus Texture Utils available in the Unity Asset Store.
snlehton
Crew
Crew
Posts: 16
Joined: Tue Nov 04, 2014 1:59 pm

Manual: TextureUtils Pixel Operations

Postby snlehton » Tue Nov 11, 2014 2:35 pm

Manual: TextureUtils Pixel Operations
TextureUtils: Slicing | Pixel Operations |Normal Maps | Misc
AtlasGenerator | PSDReader |API Scripting Reference

ReplaceColor (API ➔)

ReplaceColor can be used to replace one particular color with another color. The source color (or key color) is a 32-bit exact color instead of the regular Color which has floating-point accuracy.

Unlike most of the DTU API calls, this function has an option to modify the given texture instead of creating a new texture. This function also has a version which takes a set of texture (IEnumeration<Texture2D>) which is handy for texture collections such as bitmap font letters or slices.

Code: Select all

// color keying, make all magenta (pink) pixels transparent
TextureUtils.ReplaceColor(texture, new Color32(1,0,1,1), new Color(0,0,0,0));


BlendColor (API ➔)

BlendColor can be used to blend all the pixels in a texture towards a given color. Color blending is mostly useful for debugging texture operations such as atlas generation. For example, you can use it to display boundaries of otherwise alpha blended textures in atlases.

Unlike most of the DTU API calls, this function has an option to modify the given texture instead of creating a new texture. This function also has a version which takes a set of texture (IEnumeration<Texture2D>) which is handy for texture collections such as bitmap font letters or slices.

Code: Select all

// make a texture 25% opaque magenta (pink)
TextureUtils.BlendColor(texture, new Color32(1,0,1,1), 0.25f);


MixColorChannels (API ➔)

MixColorChannels is used to transfer channels in one texture (source) to some channels in other texture (destination). Possible channel types are: Red, Green, Blue, Alpha, RGB and RGBA.

When reading a pixel from a pixel from the input texture, channels other than the ones in the input channel are synthesized to form a full RGBA pixel. For example if the input channel is RGB, then value for the alpha channel is average of the RGB values (alpha = (red + green + blue) / 3)

The output channel then is simply a mask that tells which channels to write to the output texture.

This is how you can set a grayscale RGB texture as an alpha channel to another color RGB texture. In here, the new alpha channel is first synthesized from the input RGB channels and then written to the alpha channel of the output texture:

Code: Select all

Texture2D colorTexture; // RGB color texture without alpha channel
Texture2D alphaTexture; // Grayscale RGB texture to be used as mask
Texture2D combinedTexture = TextureUtils.MixColorChannels(colorTexture,  TextureUtils.ChannelType.Alpha, alphaTexture, TextureUtils.ChannelType.RGB);


mixcolorchannels.png
mixcolorchannels.png (222.11 KiB) Viewed 680 times


ModifyColor (API ➔)

ModifyColor is the most versatile of the color operations. It works by applying a supplied modifier function for each pixel in the texture. The modifier is a ColorModifier delegate:

Code: Select all

public delegate Color ColorModifier(Color inputColor, Texture2D inputTexture, int x, int y);


Unlike most of the DTU API calls, this function has an option to modify the given texture instead of creating a new texture. This function also has a version which takes a set of texture (IEnumeration<Texture2D>) which is handy for texture collections such as bitmap font letters or slices.

Code: Select all

// change the colors of a texture to a gradient, red to blue (bottom-up pixel space!)
Texture2D gradientTexture = TextureUtils.ModifyColor(texture, (c, i, x, y) =>
{
   float t = y / (float)i.height;
   Color l = Color.Lerp(Color.red, Color.blue, t);
   l.a = c.a;
   return l;
});

Code: Select all

// replace color of a texture with a color from another texture
Texture2D replacedColorTexture = TextureUtils.ModifyColor(texture, (c, i, x, y) =>
      {
         float u = x/(float)i.width;
         float v = y/(float)i.height;
         Color diffuse = anotherTexture.GetPixelBilinear(u, v);
         diffuse.a = c.a;
         return diffuse;
      });
      


modifycolor.jpg
modifycolor.jpg (95.69 KiB) Viewed 704 times



====================================================================================
TextureUtils: Slicing | Pixel Operations |Normal Maps | Misc
AtlasGenerator | PSDReader |API Scripting Reference

Return to “DTU - Draconus Texture Utils”

Who is online

Users browsing this forum: No registered users and 1 guest