Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How can I change the image inside rawimage to be much more bright ?

Discussion in 'Scripting' started by shamenraze1988, Jan 24, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    Just changing the rawimage material to white color not make it bright enough.
    So I added to the rawimage this script after added reference to the visual studio of the system.drawing also added copied the system.drawing to the Assets/Plugins folder.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Drawing;
    5. using System.Drawing.Imaging;
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8.  
    9. public class MouseHover : MonoBehaviour
    10. {
    11.     [Range(0, 500)]
    12.     public float bright;
    13.  
    14.     private Bitmap myImage;
    15.  
    16.     public void OnHover()
    17.     {
    18.         Debug.Log("Enter");
    19.     }
    20.     public void OnHoverExit()
    21.     {
    22.         Debug.Log("Exit");
    23.     }
    24.  
    25.     private void BrightImage()
    26.     {
    27.         myImage = new Bitmap(GetComponent<UnityEngine.UI.Image>());
    28.     }
    29.  
    30.     public static Bitmap AdjustBrightness(Bitmap Image, int Value)
    31.     {
    32.  
    33.         Bitmap TempBitmap = Image;
    34.  
    35.         Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
    36.  
    37.         System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
    38.  
    39.         float FinalValue = (float)Value / 255.0f;
    40.  
    41.         float[][] FloatColorMatrix ={
    42.  
    43.                     new float[] {1, 0, 0, 0, 0},
    44.  
    45.                     new float[] {0, 1, 0, 0, 0},
    46.  
    47.                     new float[] {0, 0, 1, 0, 0},
    48.  
    49.                     new float[] {0, 0, 0, 1, 0},
    50.  
    51.                     new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
    52.                 };
    53.  
    54.         ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
    55.  
    56.         ImageAttributes Attributes = new ImageAttributes();
    57.  
    58.         Attributes.SetColorMatrix(NewColorMatrix);
    59.  
    60.         NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
    61.  
    62.         Attributes.Dispose();
    63.  
    64.         NewGraphics.Dispose();
    65.  
    66.         return NewBitmap;
    67.     }
    68. }
    69.  
    I want to be able to use the bright variable to change the image brightness.
    but getting error on the line :

    Code (csharp):
    1.  
    2. myImage = new Bitmap(GetComponent<UnityEngine.UI.Image>());
    3.  
    Argument 1: cannot convert from 'UnityEngine.UI.Image' to 'string'

    This is how the image looks like when the game is running in the rawimage :
    The left image is too dark :



    The problem is that I'm taking screenshots from the game so the problem is not light problem or something in the scene the problem is in the image on the hard disk because when I take screenshots from the game in some cases and places the light/s in the game is not the same so the image looks like too dark but the image is fine.

    That is why I want to be able to change the image brightness and not to add lights or other stuff.

    Maybe it will help too so this is the script I'm using to take screenshots from the game and save them as images on the hard disk :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5.  
    6. public class HiResScreenshots : MonoBehaviour
    7. {
    8.     public int resWidth = 1920;
    9.     public int resHeight = 1080;
    10.     public SaveLoad saveLoad;
    11.  
    12.     public static string ScreenShotName(int width, int height)
    13.     {
    14.         return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png",
    15.                              Application.dataPath,
    16.                              width, height,
    17.                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (Input.GetKeyDown("k"))
    23.         {
    24.             var filename = ScreenShotName(resWidth, resHeight);
    25.             var directory = Path.GetDirectoryName(filename);
    26.             Directory.CreateDirectory(directory);
    27.             ScreenCapture.CaptureScreenshot(filename);
    28.  
    29.             StartCoroutine(saveLoad.SaveWithTime(directory,Path.GetFileNameWithoutExtension(filename) + ".savegame.txt"));
    30.         }
    31.     }
    32. }
    33.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    I don't think you want to do anything with System.Drawing... that won't bring you happiness in this context.

    I'm trying to understand what you mean about the image itself. Are you saying it was just screenshotted where the game is in a dark place? I mean, if you pull up the image in an image viewer, does it look too dark then too? Or is it only when it gets loaded into your RawImage Component?

    Check also you don't have some other image in front of the RawImage, and also check that the RawImage has full opacity and is solid white (the color property in the RawImage Component itself).
     
    shamenraze1988 likes this.
  3. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    Yes, If I pull the image or if I edit the image from the hard disk in paint for example it's dark too same darkness.
    You right about I'm taking the screenshots in any places I want in the game and in some places they are darker or brighter so the images on the hard disk are the same.

    That is why I need to get directly to the image it self and make it brighter.

    But this is my logic and maybe there is a better way/s to do it ? What I'm doing is taking screenshots in places in the game and also save game for the same places where I took the image screenshot. So in the game I want to make something that when you move the mouse over the images it will show them like they were selected like highlight them or make them brighter, something that the user will know he is on the image without clicking for selecting it but just moving the mouse over it.

    But I want something visual not just Debug.Log like now :

    Code (csharp):
    1.  
    2. public void OnHover()
    3.     {
    4.         Debug.Log("Enter");
    5.     }
    6.     public void OnHoverExit()
    7.     {
    8.         Debug.Log("Exit");
    9.     }
    10.  
    The way the images are no in the main menu they are not looking good some too dark and some too bright.
    I want the user to be able to save the game in any place in the game and then to load this saved games with the correct selected(highlighted, bright) image then clicking on the image will load the saved game.

    The save and load is not the problem I have this already.
    The problem is with the taken images that because in the game in some places it's more darker and some more bright.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    When you take the screenshot, do a FindObjectsOfType<Light>() and disable them all, then put in a single directional light, probably aligned the way the player is looking, take the shot, Destroy the directional light, and re-enable all the other lights (which you kept a list of from the first find).

    As for making it highlight, why not just put a white card behind it and color it black or white depending on if its selected or hovering or whatever? That's usually how I do my cursor- and mouse-selectable buttons.
     
    shamenraze1988 likes this.
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Adding more light to the game at the moment you take the screenshot might give better results. Once you've taken the picture, you no longer have 3D geometry or material information, you just have a bunch of pixels, and that limits what you can do with them (some data has been lost).

    But if you can't or don't want to do that, there are some ways you can increase the lightness of a picture after you've taken it. The simplest ways to do this would be to raise the RGB values of each pixel, perhaps by multiplying them (by some number greater than one) or adding something to them. (Note that in some cases this will give you a value higher than 100%, which means this modification is lossy.) There are probably also more-sophisticated mathematical techniques floating around on the Internet somewhere.

    A quick way to apply this concept in Unity is to use a Button component (though I believe this would require that you replace your RawImages with Images). A Button with its Transition type set to "Color Tint" lets you specify a color multiplier for each button state (normal, highlighted, pressed, etc.) Normally pure white means a multiplier of 1, and so you can't make anything brighter than it starts...however, there's an extra parameter called "Color Multiplier" that lets you multiply the whole color by an additional scalar value. This defaults to 1, but if you set it to something higher, then you can create multipliers that make the colors brighter instead of darker.

    Another way to do this would be to write a custom shader--this will give you more precise control over the exact math you're using. I haven't used shaders to make things brighter before, but I've used a custom shader to convert images to grayscale, and I can't see any reason that doing the same thing to increase brightness wouldn't work. (Note: I did this with Images; I'm not completely sure whether the equivalent thing can be done with RawImages.)

    Both of those would change how the texture is rendered. You could also change the Texture2D itself, modifying its pixel values via code (either before saving it or after loading it).
     
  6. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,098
    You could also use my asset Better UI for this (see signature).
    There is a Shader that can be assigned to images / raw images which can change the hue, saturation and brightness of the given image. The asset also allows to easily modify these values in transitions.

    The benefit is that you only need one texture and even can animate between brightness values.

    Edit: Or use the color multiplier as suggested above. That would be the cheaper but less flexible option.

    Edit2:
    No, it works on any class which derives from Graphic. That includes RawImage.
     
    Last edited: Jan 24, 2021
  7. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    692
    If you want a highlight in the actual image when hovering, add another Image (just plain white, no image file) on top of the RawImage (as opposed to behind, like your frame is). By default opacity 0, when you hover change it to 20% or so, tweak that level to suit. This will add that amount of white to the RawImage RGB values, just like turning up "Brightness".
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Adding a transparent white image on top is equivalent to blending to white, which is not visually equivalent to any of the other methods discussed so far. And in particular, it will reduce contrast within the original image (whereas e.g. multiplying will increase constrast, and adding a constant to each pixel will keep it the same, except in cases where you hit the maximum).

    Could be a good option if you're just trying to show which picture is currently selected, but a bad method of trying to make the details within the picture more visible.
     
    seejayjames likes this.
  9. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    692
    Yes, that's true, it's a somewhat different change. Adding a constant to each pixel would most likely give better visual results...just need to be careful not to change the texture asset itself, which SetPixels() will do unless you make a copy in code. (Likely there are many other ways)
     
  10. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    692
    This script lets you modify your RawImage texture by adding or subtracting a custom amount from all pixels. Colors clamp at (0,1). Original texture can be restored. No changes are made to the texture asset. Is kinda fun ;)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class forumPixelPlay : MonoBehaviour
    7. {
    8.     // Create a blank RawImage and drag this script onto it
    9.  
    10.     // NOTE: Unless you create a new texture from the image (instead of using it directly),
    11.     // changes will alter the asset itself!
    12.     // Textures must be type 2D/UI and have Read/Write enabled
    13.  
    14.     // Unity docs say GetPixels32 etc. is faster, but I wasn't able to get the modifyPixels() to work...
    15.     // colors kept wrapping around 255, even with clamps (?)
    16.  
    17.     RawImage myImage; // texture will be assigned in code, but can assign in Inspector to see in Editor
    18.  
    19.     public Texture2D origTex; // drag texture to slot in Inspector
    20.     public Texture2D newTex; // leave blank, will be created in code
    21.  
    22.     [Range(-1, 1)]
    23.     public float lightenAmount = 0.1f;
    24.  
    25.     Color[] pixels;
    26.  
    27.     void Start()
    28.     {
    29.         myImage = GetComponent<RawImage>();
    30.  
    31.         // Prints the float equivalent color at pixel (50,50). Note the texture origin is lower left!
    32.         //print("pixel 50, 50 = " + origTex.GetPixel(50, 50));
    33.         pixels = origTex.GetPixels();
    34.  
    35.         newTex = new Texture2D(origTex.width, origTex.height);
    36.  
    37.         newTex.SetPixels(pixels);
    38.         newTex.Apply();
    39.         myImage.texture = newTex;
    40.     }
    41.  
    42.     void modifyPixels() // Press "P" to change pixel colors by lightenAmount
    43.     {
    44.         for (int i = 0; i < pixels.Length; i++)
    45.         {
    46.             pixels[i].r += lightenAmount;
    47.             if (pixels[i].r > 1) pixels[i].r = 1;
    48.             if (pixels[i].r < 0) pixels[i].r = 0;
    49.             pixels[i].g += lightenAmount;
    50.             if (pixels[i].g > 1) pixels[i].g = 1;
    51.             if (pixels[i].g < 0) pixels[i].g = 0;
    52.             pixels[i].b += lightenAmount;
    53.             if (pixels[i].b > 1) pixels[i].b = 1;
    54.             if (pixels[i].b < 0) pixels[i].b = 0;
    55.         }
    56.         newTex.SetPixels(pixels);
    57.         newTex.Apply();
    58.     }
    59.     void restorePixels() // Press "O" (letter O) to restore original texture/image
    60.     {
    61.         pixels = origTex.GetPixels();
    62.         newTex.SetPixels(pixels);
    63.         newTex.Apply();
    64.     }
    65.  
    66.     void Update()
    67.     {
    68.         if (Input.GetKeyDown(KeyCode.P))
    69.         {
    70.             modifyPixels();
    71.         }
    72.         if (Input.GetKeyDown(KeyCode.O))
    73.         {
    74.             restorePixels();
    75.         }
    76.     }
    77. }
    78.  
     
    Kurt-Dekker and shamenraze1988 like this.
  11. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208

    There is a problem. When I'm attaching the script to the RawImage in runtime I can't add any texture2d to the field origTex.

    Look at this screenshot. When I'm running the game I'm using a prefab of RawImage and the RawImage source texture is a image from the hard disk. After running the game I'm dragging your script to the RawImage but the field origTex I can't assign to it anything. Not the RawImage it self and nothing else.

    I can select many other textures but they are not the one in the RawImage in the Texture field of my RawImage like in the screenshot :

     
  12. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    Found how to do it and it's working now great.

    What I changed in the script is in the Start :

    Code (csharp):
    1.  
    2. var texture = GetComponent<RawImage>().texture;
    3. origTex = texture as Texture2D;
    4.  
    Because the Texture in the RawImage is Texture and not Texture2D I just get the RawImage Texture and cast it as Texture2D.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class RawImagePixelsChange : MonoBehaviour
    8. {
    9.     // Create a blank RawImage and drag this script onto it
    10.  
    11.     // NOTE: Unless you create a new texture from the image (instead of using it directly),
    12.     // changes will alter the asset itself!
    13.     // Textures must be type 2D/UI and have Read/Write enabled
    14.  
    15.     // Unity docs say GetPixels32 etc. is faster, but I wasn't able to get the modifyPixels() to work...
    16.     // colors kept wrapping around 255, even with clamps (?)
    17.  
    18.     RawImage myImage; // texture will be assigned in code, but can assign in Inspector to see in Editor
    19.  
    20.     public Texture2D origTex; // drag texture to slot in Inspector
    21.     public Texture2D newTex; // leave blank, will be created in code
    22.  
    23.     [Range(-1, 1)]
    24.     public float lightenAmount = 0.1f;
    25.  
    26.     Color[] pixels;
    27.  
    28.     void Start()
    29.     {
    30.         var texture = GetComponent<RawImage>().texture;
    31.         origTex = texture as Texture2D;
    32.  
    33.         myImage = GetComponent<RawImage>();
    34.  
    35.         // Prints the float equivalent color at pixel (50,50). Note the texture origin is lower left!
    36.         //print("pixel 50, 50 = " + origTex.GetPixel(50, 50));
    37.         pixels = origTex.GetPixels();
    38.  
    39.         newTex = new Texture2D(origTex.width, origTex.height);
    40.  
    41.         newTex.SetPixels(pixels);
    42.         newTex.Apply();
    43.         myImage.texture = newTex;
    44.     }
    45.  
    46.     void modifyPixels() // Press "P" to change pixel colors by lightenAmount
    47.     {
    48.         for (int i = 0; i < pixels.Length; i++)
    49.         {
    50.             pixels[i].r += lightenAmount;
    51.             if (pixels[i].r > 1) pixels[i].r = 1;
    52.             if (pixels[i].r < 0) pixels[i].r = 0;
    53.             pixels[i].g += lightenAmount;
    54.             if (pixels[i].g > 1) pixels[i].g = 1;
    55.             if (pixels[i].g < 0) pixels[i].g = 0;
    56.             pixels[i].b += lightenAmount;
    57.             if (pixels[i].b > 1) pixels[i].b = 1;
    58.             if (pixels[i].b < 0) pixels[i].b = 0;
    59.         }
    60.         newTex.SetPixels(pixels);
    61.         newTex.Apply();
    62.     }
    63.     void restorePixels() // Press "O" (letter O) to restore original texture/image
    64.     {
    65.         pixels = origTex.GetPixels();
    66.         newTex.SetPixels(pixels);
    67.         newTex.Apply();
    68.     }
    69.  
    70.     void Update()
    71.     {
    72.         if (Input.GetKeyDown(KeyCode.P))
    73.         {
    74.             modifyPixels();
    75.         }
    76.         if (Input.GetKeyDown(KeyCode.O))
    77.         {
    78.             restorePixels();
    79.         }
    80.     }
    81. }
    82.  
     
  13. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    692
    Yep, it has to be the correct kind of Texture. Cool you got it working!
     
    shamenraze1988 likes this.