Search Unity

color or color32

Discussion in 'Scripting' started by passer, Oct 19, 2016.

  1. passer

    passer

    Joined:
    Dec 1, 2013
    Posts:
    12
    I am going to write program for image manipulation. For drawing with pen and modifying images.

    So I need buffer for storing image peaces(layers for example). Later this peaces will be apply to image(just copyed or other methods(f.e. xor)). And yesterday I find out that color uses float for each color element so it takes 4 time more memory.
    1) I suppoce it also takes much more time to process color than color 32. Am I right? Or in the end unity convert color32 to color before pass to opengl?
    2) why? as I remember opengl support 4 byte color instance. So why Unity prefer floats?

    3) Is it possible some how use GL library for my targets?
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You assumed, that unity uses opengl. That's true, unless your preferred platform is windows, then it'll use directx (for some reason).

    I wouldn't suggest actually storing your layers or image pieces in an array of Color, that's exactly, why Texture2D exists.

    Opengl stores those values in floats too, because telling a led, how much dimmed it should be is way easier, than telling the cpu, that it should be 158/256 and leaving him calculate that value.
     
  3. passer

    passer

    Joined:
    Dec 1, 2013
    Posts:
    12
    Dear gorbit99, Thank you for your answer.

    Lets consider real situation.
    I have pen with next pixels alpha data.
    50, 100, 50
    100, 200, 100
    50, 100, 50


    I have to draw line with this pen on image.
    As I understand first I have to create some buffer. Find each pixel of line and put this values around that pixels. And I have to save on pixel values with bigger alpha.

    so this
    0,0,0
    0,0,0
    0,0,0
    0,0,0
    0,0,0


    will become
    50, 100, 50
    100, 200, 100
    50, 100, 50
    0, 0, 0
    0, 0, 0


    then this
    50, 100, 50
    100, 200, 100
    100, 200, 100
    50, 100, 50
    0, 0, 0

    and so on.

    And then I have to merge this values with real image pixel values. Would it looks like this with Texture2D?

    Code (CSharp):
    1. Texture2D pen;
    2. Texture2D buffer;
    3. Texture2D image;
    4. Color[] penColors=pen.GetPixels();
    5. Color[] bufferColors=buffer.GetPixels();
    6.  
    7. //copy penColors to bufferColors one by one with necessary logic.
    8. Color[] imageColors=image.GetPixels();
    9. //copy bufferColors to imageColors one by one with necessary logic.

    Or there better solution for this? for example like OpenGL shaders.
     
    Last edited: Oct 20, 2016
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Your answer is great. The reason why Unity uses Directx on Windows is because Directx is designed by Microsoft and gives better results on Windows than OpenGL. It just wouldn't make sense to use OpenGL on Windows.

    EDIT: As Erid5h5 said, if you really want to, you can use OpenGL on Windows. However, this probably will result in worse performance.
     
    Last edited: Oct 20, 2016
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Don't use Color, use Color32, it's much faster and uses 4X less memory. Don't concern yourself with implementation details of OpenGL or whatever since Unity handles it. (Also, Unity uses OpenGL on Windows if you select that in the player settings.)

    --Eric
     
    Hoorza, dman8723, sj631 and 5 others like this.
  6. Supergeek

    Supergeek

    Joined:
    Aug 13, 2010
    Posts:
    103
    Unfortunately, there doesn't seem to be a GetPixels32 equivalent of GetPixels(int x, int y, int blockWidth, int blockHeight)
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yeah, that can be annoying.

    --Eric
     
  8. The_Magical_Kiwi

    The_Magical_Kiwi

    Joined:
    Jul 16, 2012
    Posts:
    1
    I didn't know that, I've been using Color for ages. Why is Color32 less memory intensive?
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It uses 4 bytes instead of 4 floats (which are 4 bytes each).

    --Eric
     
    Hoorza, Laiken, dyupa and 2 others like this.
  10. marcospgp

    marcospgp

    Joined:
    Jun 11, 2018
    Posts:
    194
    Is there any tradeoff to keep in mind? Is there a risk of losing any information?
     
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    For most ordinary images, no. Almost all image formats only support 256 values per channel and that's also how the images are usually stored in GPU memory. However since HDR is a thing there may be cases where the underlying type has a higher precision than 8 bit per channel. In those cases you would have to use Color instead.

    edit:
    I'm not even sure which HDR image formats Unity can load. According to this wikipedia list there aren't many file formats that actually support HDR images.
     
  12. Rickmc3280

    Rickmc3280

    Joined:
    Jun 28, 2014
    Posts:
    189
    Many years later, curious, is there a way to use native array of bytes? ex.


    Code (CSharp):
    1. var layout = new[]
    2.         {
    3.             new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),          
    4.             new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.Byte,4) // <------- THIS ----------
    5.         };
    UseCase = mesh.SetVertexBufferParams(xxxx)