Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Generating a dynamic alpha mask texture in code

Discussion in 'General Graphics' started by natehirsch, Mar 10, 2015.

  1. natehirsch

    natehirsch

    Joined:
    Sep 29, 2013
    Posts:
    2
    Hi, so I have only a small experience level with shaders in any language, and none in Unity. What I am trying to do is turn a 2D array (of booleans or floats) into the Alpha Mask Texture for a shader that already uses a static alpha mask.

    Here is the high level overview of what I'm trying to do: programmatically create a Alpha Mask Texture that will change over time (as the user touches the texture, it modifies the alpha in that area), and apply that alpha mask that sprites shader.

    So here's where I am now: I have my 2D array of booleans with the same width and height as my texture.
    I think I can do something like this to set the alpha mask dynamically:

    mySprite.renderer.material.SetTexture("_AlphaTex", myAlphaRenderTexture);

    but I need help creating the RenderTexture myAlphaRenderTexture object...Some but not all of the pixels in this texture will be modified every frame, so I'd like the texture to persist and easily and arbitrarily access any pixel in the texture, and I don't want to have to recreate the texture each time I want to change the mask.
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    you won't have to recreate the texture that you keep uploading to - see SetPixels() and Apply()... but nevertheless the whole texture still has to be uploaded to the graphics card every frame. Slow.
     
  3. natehirsch

    natehirsch

    Joined:
    Sep 29, 2013
    Posts:
    2
    I don't see a SetPixels function on RenderTexture. Could you step me through creating the RenderTexture object, changing an arbitrary pixel, and then setting it to the material?

    Also, you mentioned uploading the texture to the graphics card every frame will be slow. Do you have a suggestion for an alternate method of achieving my requirements that would have better performance? I am not committed to doing it this way especially since great performance is a requirement of my project.