Search Unity

Texture with alpha pixels not working, when uv scripting

Discussion in 'Image Effects' started by MatriXz, Jan 15, 2018.

  1. MatriXz

    MatriXz

    Joined:
    Aug 17, 2013
    Posts:
    24
    Hi guys.

    Ive created a simple cube for this particular example that is supposed to use a texture that i quickly made in paint.Net for the purpose of experimentation. The image is a 80x40, with the first 40px being totally black, the other half is a black X on a transparent background, saved as png.

    Now the cube needs to have the X part of the texture on all 4 sides, and the black part on the top. and i want the sides to use the transparency component of the texture to make it "see through" on the parts where the transparent pixels are only.

    But instead of doing that, the entire image is black and made semi transparent.

    Heres my Uv mapping code:
    Code (CSharp):
    1. private void BuildRampFace(Vector3 corner, Vector3 up, Vector3 right, bool reversed, List<Vector3> verts, List<Vector2> uvs, List<int> tris, bool isTop = false, bool isRampStairs = false) {
    2.             int index = verts.Count;
    3.  
    4.             verts.Add((isTop ? corner : new Vector3(corner.x, corner.y - (isRampStairs ? 0 : 1), corner.z)));
    5.             verts.Add(corner + up);
    6.             verts.Add(corner + up + right);
    7.             verts.Add((isTop ? corner : new Vector3(corner.x, corner.y - (isRampStairs ? 0 : 1), corner.z)) + right);
    8.  
    9.             Vector2 uvWidth = new Vector2(40f, 40f);
    10.             Vector2 uvCorner = new Vector2((isTop ? 0 : 40), 0);
    11.  
    12.             uvs.Add(uvCorner);
    13.             uvs.Add(new Vector2(uvCorner.x, uvCorner.y + uvWidth.y));
    14.             uvs.Add(new Vector2(uvCorner.x + uvWidth.x, uvCorner.y + uvWidth.y));
    15.             uvs.Add(new Vector2(uvCorner.x + uvWidth.x, uvCorner.y));
    16.  
    17.             if(reversed) {
    18.                 tris.Add(index + 0);
    19.                 tris.Add(index + 1);
    20.                 tris.Add(index + 2);
    21.                 tris.Add(index + 2);
    22.                 tris.Add(index + 3);
    23.                 tris.Add(index + 0);
    24.             } else {
    25.                 tris.Add(index + 1);
    26.                 tris.Add(index + 0);
    27.                 tris.Add(index + 2);
    28.                 tris.Add(index + 3);
    29.                 tris.Add(index + 2);
    30.                 tris.Add(index + 0);
    31.             }
    32.         }
    How do i make the cube display the actual image parts i choose on the sides, and then only make the transparent pixels of the texture be transparent while the black parts are not?
     
  2. MatriXz

    MatriXz

    Joined:
    Aug 17, 2013
    Posts:
    24
    I figured it out.
    For those who stumble uppon this and have the same kind of problems:

    The uvWidth and uvCorner in must be between 0f -> 1f.. otherwise the image is repeated several times for some reason.
    So the solution is:

    Code (CSharp):
    1.  
    2. // instead of
    3. Vector2 uvWidth = new Vector2(40f, 40f);
    4. Vector2 uvCorner = new Vector2((isTop ? 0 : 40f), 0);
    5.  
    6. // we do this
    7. Vector2 uvWidth = new Vector2(0.5f, 1f);
    8. Vector2 uvCorner = new Vector2((isTop ? 0 : 0.5f), 0);
    9.  
    Remember to set the RenderType to cutout, (or one of the legacy version of the same) to not make the top semi transparent aswell.