Grayscale in unity

Grayscale in unity
June 5, 2019 7 Comments Development Pushpendra Kumar

Here I am letting you know the answer of your question. how to convert normal image into black and white in unity with the help go c# coding. This is also called Grayscale in unity. Grayscale is the functionality which allow user to convert image colour into different colours. To modify colours in a more complex way. But if you are here then you do not need to worried about anything. Because we are going to let you know the basic of grayscale. Grayscale can do a simple version of color correction, i.e. remap grayscale image into arbitrary colors. This can be used for effects like heat vision.

Grayscale in unity for convert normal image into black and white

Texture2D graph;
    void ConvertToGrayscale()
    {
        Color32[] pixels = graph.GetPixels32();
        for (int x = 0; x < graph.width; x++)
        {
            for (int y = 0; y < graph.height; y++)
            {
                Color32 pixel = pixels[x + y * graph.width];
                int p = ((256 * 256 + pixel.r) * 256 + pixel.b) * 256 + pixel.g;
                int b = p % 256;
                p = Mathf.FloorToInt(p / 256);
                int g = p % 256;
                p = Mathf.FloorToInt(p / 256);
                int r = p % 256;
                float l = (0.2126f * r / 255f) + 0.7152f * (g / 255f) + 0.0722f * (b / 255f);
                Color c = new Color(l, l, l, 1);
                graph.SetPixel(x, y, c);
            }
        }
        graph.Apply(false);
        var bytes = graph.EncodeToPNG();
        System.IO.File.WriteAllBytes(Application.dataPath + "ImageSaveTest.png", bytes);
    }

Now assign graph before calling this function.

graph = sprite.texture as Texture2D;
                ConvertToGrayscale();
                image.sprite = sprite;

Quick test trying to convert this Basic256 script into Unity c# (trying to keep it close to original code..) maybe still some problems as the output seems darker than the example.

Result color = pixel’s color in the ramp image at (OriginalLuminance + RampOffset) index. For example, to invert the colors in the image you only need to flip the original color ramp horizontally (so that it goes from white to black instead of from black to white):

This is very simple solution for converting an image into black & white using c# script. In Conclusion, I hope this code helps you to finding your right answer. Thanks for visiting on this site.

More Useful links
1. http://www.wepstech.com/create-space-shooter-game/

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.
Leave Comment
  1. 1

    GreggNab

    Теперь буду знать

    Reply
    1. 1

      Pushpendra Kumar

      Спасибо

      Reply
  2. 1

    3m 8511 n95

    Found your article interesting to read. I can’t wait to see your
    post. Good Luck for the upcomingupdate. Thiss article is truly quite interesting and effective.

    King regards,
    Mead Duke

    Reply
  3. 1

    n95 8210 mask

    Thank you so much for doing the job that
    is remarkable here, everybody will like your post.

    King regards,
    Mead Dencker

    Reply
  4. 1

    The Respirator Shop

    Very good article, hi. I hope you will print again such type of post.
    Thank you!
    King regards,
    Dinesen Zacho

    Reply
  5. 1

    yatendra

    Your think to develop 2d and 3d unity game with advance functionality is very nice. this content is helpful to us and our clients. so i will share it with our client.

    Reply
  6. 1

    ปั้มไลค์

    Like!! Really appreciate you sharing this blog post.Really thank you! Keep writing.

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *