Search Unity

Color alpha doesn't set to transparent?

Discussion in 'Scripting' started by TheOtherUserName, Feb 23, 2021.

  1. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I made an Script generating stacked Perlin noise and wanted to paint a plane via material to make clouds. For that I first made an RawImage to show me the result first but I only get full Pixles. Am I overseeing something here?
    Code (CSharp):
    1. tex = new Texture2D((int)dimensions.x, (int)dimensions.y);
    2.  
    3.         noise = new float[(int)dimensions.x, (int)dimensions.y];
    4.         noise = GenerateNoise(seed, (int)dimensions.x, (int)dimensions.y, layers, frequency, amplitude);
    5.  
    6.         for(int y = 0; y < (int)dimensions.y; y++)
    7.         {
    8.             for(int x = 0; x < dimensions.x; x++)
    9.             {
    10.                 tex.SetPixel(x, y, new Color(noise[x, y], noise[x, y], noise[x, y], 1f - noise[x, y]));
    11.             }
    12.         }
    13.  
    14.         tex.Apply();
    15.         img.texture = tex;
    This is all my Operating code used to set the color
    Here is my code for the Noise(for people who want to use it) note that you need very small numbers for the freqency I am using 0.01:
    Code (CSharp):
    1. float[,] GenerateNoise(float seed, int width, int height, int layers, float frequency, float amplitude)
    2.     {
    3.         float[,] noise = new float[width, height];
    4.  
    5.         for (int i = 0; i < layers; i++)
    6.         {
    7.             for (int y = 0; y < height; y++)
    8.             {
    9.                 for (int x = 0; x < width; x++)
    10.                 {
    11.                     noise[y, x] += Mathf.PerlinNoise((x + seed) * frequency, (y + seed) * frequency) * amplitude;
    12.                 }
    13.             }
    14.  
    15.             frequency *= 2f;
    16.             amplitude *= .5f;
    17.         }
    18.  
    19.         float heighest = 0f;
    20.  
    21.         for (int y = 0; y < height; y++)
    22.         {
    23.             for (int x = 0; x < width; x++)
    24.             {
    25.                 if(noise[y, x] > heighest)
    26.                 {
    27.                     heighest = noise[y, x];
    28.                 }
    29.             }
    30.         }
    31.  
    32.         for (int y = 0; y < height; y++)
    33.         {
    34.             for (int x = 0; x < width; x++)
    35.             {
    36.                 noise[y, x] = noise[y, x] / heighest;
    37.             }
    38.         }
    39.  
    40.         return noise;
    41.     }
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    It's hard to say just from code but some ideas for you to check:
    Do you use a shader that supports transparency?
    What happens when you export the image as png?
    Have you verified that noise is within 0-1 range? Print the noise values to the Console.
    If all else fails I would print the resulting colors in the console and see if they are correct (for a small texture though).
    Note that Color32 uses less space.

    this disturbs me:
    Code (csharp):
    1.  
    2.         noise = new float[(int)dimensions.x, (int)dimensions.y];
    3.         noise = GenerateNoise(seed, (int)dimensions.x, (int)dimensions.y, layers, frequency, amplitude);
    4.  
    You create an array for the noise but in the method you create a new one which is returned.
    Code (csharp):
    1.  
    2. float[,] GenerateNoise(float seed, int width, int height, int layers, float frequency, float amplitude)
    3.     {
    4.         float[,] noise = new float[width, height];
    5.  
    So the first array is not used at all and just wastes memory.
     
  3. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I also noticed that I use an unnecessary array for my floats and optimized it after posting but thanks for the note. I have normalized my values between (not all the way to)0 and 1 bc I divide all of them through the highest. I dont know though what you meant with the shader supporting transparency. A RawImage is able to show transparency thus it should display my color I think. My only other thought was maybe the Texture2D does not support it but after putting the default downarrow as an Image I could see that in fact this is also no problem. I will try though exporting my noise as an png and look if it helps in anyway. Btw I subtract the noise value from one to get the inverse value(1 at places with 0 to be transparent there else it would be black)

    EDIT:
    I saved it as an Png and could see that my transparency is in fact saved. Meaning the problem is in Unity not my code. Prob. need to change some setting my declaring the Texture
     
    Last edited: Feb 23, 2021
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    So you are talking about displaying that cloud image in the UI? The manual also has a field for the material to render that image. So I would verify that a shader with transparency is used. You could also just put the texture on a regular quad and see if it works there. I have never used RawImages so I have no ideas beside those if the error lies within them.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Is it intentional that every pixel will be somewhere on a spectrum between transparent white and opaque black? (Since you are using the same random number for every channel, except that alpha is inverted.)
     
  6. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Yes I want to create a dark theme
     
  7. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I have put it on a quad but even after changing the material to Transparent it wont show the Transparency. I have even tried cutout but not even that worked. I really run out of ideas

    EDIT:
    Alright guys I just solved it. I feel really dumb right now but I was changing the texture position to move my clouds and didnt add the alpha when assigning it in Update.
    But thanks for the help!
     
    Last edited: Feb 23, 2021