Search Unity

DrawTexture Alpha Question

Discussion in 'Scripting' started by RoboticDoorstop, Jan 22, 2014.

  1. RoboticDoorstop

    RoboticDoorstop

    Joined:
    Nov 6, 2012
    Posts:
    5
    I'm having issues with the results Graphics.DrawTexture that maybe someone has an answer for. Currently I'm attempting to take a texture w/ an Alpha channel and use DrawTexture to add it to a RenderTexture. The resulting RGB of the RenderTexture looks exactly as expected, the Alpha is another story. The texture I'm using is essentially a feathered brush ( a black circle with a blurred edge ). The image itself is just black square (128px on each side) with the alpha channel that is a black circle in the center of a field of white. Here's a view of the Alpha Channel:

    $Brush_Alpha.png

    When I call DrawTexture to add this to a RenderTexture (which has first been cleared and set to display as white) I end up with a render texture which contains a white alpha channel and a thin grey ring where the soft edge of my circle would be.

    $RenderTexture_Alpha.png

    The following is the script I'm using to accomplish this:
    Code (csharp):
    1. public class RenderToTexture : MonoBehaviour {
    2.  
    3.     public Texture textureStamp;
    4.     public RenderTexture renderTexture;
    5.  
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         renderTexture.format = RenderTextureFormat.ARGBFloat;
    10.         Debug.Log( "Format: "+renderTexture.format );
    11.  
    12.  
    13.         ClearTexture( renderTexture );
    14.         DrawToTexture( new Vector2( Screen.width/2, Screen.height/2 ) );
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.    
    20.     }
    21.  
    22.  
    23.     void ClearTexture( RenderTexture rt )
    24.     {
    25.         RenderTexture.active = rt;
    26.         GL.Begin( GL.QUADS );
    27.         GL.Clear ( true, true, Color.red );
    28.         GL.End();
    29.        
    30.         RenderTexture.active = null;
    31.     }
    32.  
    33.     void DrawToTexture ( Vector2 pos ) {
    34.  
    35.         RenderTexture.active = renderTexture;
    36.         GL.PushMatrix();
    37.         GL.LoadPixelMatrix(0,Screen.width,Screen.height,0);
    38.  
    39.         Rect rect = new Rect (pos.x - textureStamp.width / 2, (renderTexture.height - pos.y) - textureStamp.height / 2, textureStamp.width, textureStamp.height);
    40.         Graphics.DrawTexture (rect, textureStamp);
    41.  
    42.         GL.PopMatrix();
    43.         RenderTexture.active = null;
    44.  
    45.     }
    46. }
    Any ideas what I might be doing incorrectly?
     
    Last edited: Jan 23, 2014