Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Creating A Gradient Texture And Applying It At Runtime

Discussion in '2D' started by TLHPoE, Apr 10, 2016.

  1. TLHPoE

    TLHPoE

    Joined:
    Apr 9, 2016
    Posts:
    3
    Hi, I wanted to create a gradient from a transparent white to a opaque black for my quad to overlay onto a different texture. The problem is that it doesn't seem like the texture is being applied correctly to the quad.

    GradientTexture Script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GradientTexture : MonoBehaviour {
    6.     [Header("Type of Gradient")]
    7.     [Tooltip("0 = downward to black")]
    8.     public int type = 0;
    9.  
    10.     void Start () {
    11.         float width = transform.lossyScale.x;
    12.         float height = transform.lossyScale.y;
    13.  
    14.         Gradient gradient = new Gradient();
    15.         Texture2D texture = new Texture2D(Mathf.CeilToInt(width), Mathf.CeilToInt(height));
    16.         texture.alphaIsTransparency = true;
    17.  
    18.         switch(type) {
    19.         case(0): {
    20.                 gradient.SetKeys(DARKNESS_COLOR_KEY, DARKNESS_ALPHA_KEY);
    21.  
    22.                 float yStep = 1F / height;
    23.  
    24.                 print("Height: " + height);
    25.                 print("yStep: " + yStep);
    26.  
    27.                 for(int y = 0; y < Mathf.CeilToInt(height); y++) {
    28.                     Color color = gradient.Evaluate(y * yStep);
    29.                     print("Y: " + y + " | R: " + color.r + " G: " + color.g + " B: " + color.b + " A: " + color.a);
    30.  
    31.                     for(int x = 0; x < Mathf.CeilToInt(width); x++) {
    32.                         texture.SetPixel(Mathf.CeilToInt(x), Mathf.CeilToInt(y), color);
    33.                     }
    34.                 }
    35.                    
    36.                 break;
    37.             }
    38.         }
    39.  
    40.         GetComponent<Renderer>().material.mainTexture = texture;
    41.     }
    42.  
    43.     public static GradientColorKey[] DARKNESS_COLOR_KEY = {new GradientColorKey(Color.black, 0), new GradientColorKey(Color.black, 1)};
    44.     public static GradientAlphaKey[] DARKNESS_ALPHA_KEY = {new GradientAlphaKey(0, 0), new GradientAlphaKey(1, 1)};
    45.  
    46. }
    47.  

    I believe I'm creating the gradient correctly, as the print shows that as the y increases the alpha increments to 1. Before running, the quad takes on the default-checker texture from Unity. After the game finishes loading, the quad is completely invisible.

    Here is the quad object ("Darkness") and the material it uses:


    It also creates a strange rendering artifact along its diagonals during run:

    Finally, here is the desired effect:

    Sorry if I misunderstand most responses; I am entirely new to Unity, graphics, and C#, and only have experience in Java.
     
    NT_Ninetails likes this.
  2. TLHPoE

    TLHPoE

    Joined:
    Apr 9, 2016
    Posts:
    3
    I found the problem, I forgot the apply the texture (texture.Apply()).
     
  3. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Hi,
    What is the advantage of creating the texture procedurally vs using a ready made texture in your game context ?
     
  4. NerdIt-

    NerdIt-

    Joined:
    Oct 19, 2018
    Posts:
    24
    A lot
     
    skoteskote, Ne0mega and DoomDude99 like this.
  5. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521