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

Linear gradient in Texture2d run-time

Discussion in 'Scripting' started by ex_mi, Mar 25, 2016.

  1. ex_mi

    ex_mi

    Joined:
    Jan 25, 2016
    Posts:
    11
    Good afternoon!

    I genrerate (setPixel) Texture2D (100, 100). I want to draw on triangle gradient texture with known coordinates v1, v2, v3 - corners and corners colors color1, color2, color3. Sample - a white texture with a linear triangle gradient (v2 - Green; v1, v3 - Red):




    Nobody will tell the script for such a thing?
     
    Last edited: Mar 25, 2016
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Have you considered skipping textures all together and just using vertex coloring?
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    And here is how to make 'a white texture with a linear triangle gradient'

    Code (CSharp):
    1.     Texture2D tex;
    2.     Renderer rend;
    3.     // Use this for initialization
    4.     void Start () {
    5.         rend = GetComponent<Renderer>();
    6.         tex = new Texture2D( width, height );
    7.     }
    8.  
    9.  
    10.  
    11.     //Texture size
    12.     public int width, height;
    13.     //x and y coordinates of 3 vertex positions (in texture space 0->width)
    14.     public int v1x, v1y, v2x, v2y, v3x, v3y;
    15.     //Corner colors
    16.     public Color color1, color2, color3;
    17.  
    18.     void Update () {
    19.         for (int x = 0; x < width; x++) {
    20.             for (int y = 0; y < height; y++) {
    21.  
    22.                 tex.SetPixel( x, y, PointColor( x, y ) );
    23.  
    24.             }
    25.         }
    26.         tex.Apply();
    27.         rend.material.mainTexture = tex;
    28.     }
    29.  
    30.     Color PointColor ( int x, int y ) {
    31.         //Calculate barycentric co-efficients a,b,c
    32.         float d = (v2y - v3y) * (v1x - v3x) + (v3x - v2x) * (v1y - v3y);
    33.         float a = ((v2y - v3y) * (x - v3x) + (v3x - v2x) * (y - v3y)) / d;
    34.         float b = ((v3y - v1y) * (x - v3x) + (v1x - v3x) * (y - v3y)) / d;
    35.         float c = 1 - a - b;
    36.         if (0 <= a && a <= 1 && 0 <= b && b <= 1 && 0 <= c && c <= 1) {
    37.             return color1 * a + color2 * b + color3 * c;
    38.         } else {
    39.             return Color.white;
    40.  
    41.         }
    42.     }
     
  4. ex_mi

    ex_mi

    Joined:
    Jan 25, 2016
    Posts:
    11

    Thanks it works for 1 triangle, but if I need to be on the texture draw N triangles. That would not want to run through the loop the entire length of the texture.
     
    Last edited: Mar 25, 2016
  5. ex_mi

    ex_mi

    Joined:
    Jan 25, 2016
    Posts:
    11
    if you use the vertex color, is it possible to add noise to the texture blending mode for mesh?
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You can use the vertex colors to tint a bitmap (eg. noise) that is applied in the normal fashion