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

Most efficient way to "erase" some parts of a texture?

Discussion in 'General Graphics' started by Waliactico, Jan 17, 2015.

  1. Waliactico

    Waliactico

    Joined:
    Jan 16, 2013
    Posts:
    91
    Hello, I'm developing a game for smartphones where you can "erase" a texture of a 2d plane by touching the screen; that is, making it invisible in the area the finger touched. My concern with this is performance, so the game can run on most devices.
    [ Note that the game won't be just a paint app, it will be a full 2D game with its playable character, enemies and stuff, so the performance of the "texture eraser" is a secondary mechanic, that's why it should not have an important impact of performance ]

    So, what's the most efficient technique to accomplish that?
    - Should I use the standard Texture2D.SetPixel()?
    - Or would it be better a shader that modifies the alfa of certain pixels on its fragment shader?
    - Maybe something regarding RenderToTexture?
    - Any other idea other than these, that would be better?

    I'm hearding your comments, and thanks for the help, graphics gurus!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    I'd use Vectrosity with a separate camera and a depth mask shader for the line, demonstrated here. (That's a minor modification of the line drawing demo, which uses 1) a depth-only camera to draw the black rectangle, 2) a depth mask shader for the line material, and 3) the drawing script is slightly modified to use Draw3D with world-space coords using ScreenToWorldPoint.) I think that's about as fast as you can get; much faster than SetPixel and uploading textures.

    --Eric
     
    theANMATOR2b likes this.
  3. Waliactico

    Waliactico

    Joined:
    Jan 16, 2013
    Posts:
    91
    Hi Eric, thanks for your answer, it does seems like a powerful tool. Just have some questions:
    1. Using vectrosity, could I replace that black rectangle with another image texture, so erasing this one would reveal the background?
    2. And also, does it has support for modifying alpha more softly? I mean, without those hard borders, smoothing the alpha from the edges to the center of the line
    3. Can it work in some other ways than lines? For example, erasing the texture in a circle where touched

    Thanks for your time!
     
    jarensasaint_unity likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    1. The black rectangle isn't related to Vectrosity, it's just something I threw in there because it took 2 seconds.
    2. Could possibly modify the depth mask shader somehow.
    3. Vectrosity only draws lines.

    --Eric
     
  5. Emak-Bakia

    Emak-Bakia

    Joined:
    Oct 20, 2013
    Posts:
    4
    I've had a similar problem and came up with this solution:
    1. Have 2 render textures (let's call them A and B)
    2. Draw from A to B using Graphics.Blit and your drawing shader.
    3.Display B as your output texture.
    3. Swap A and B.
     
  6. Waliactico

    Waliactico

    Joined:
    Jan 16, 2013
    Posts:
    91
    Hi Emak-Bakia, thanks for your answer.
    I don't quite understand the 2nd step. What do you mean by draw from A to B?
    Also, does Graphics.Blit has good performance on mobile platforms?
    Thank you for your time!
     
  7. Emak-Bakia

    Emak-Bakia

    Joined:
    Oct 20, 2013
    Posts:
    4
    I ment this:
    Code (CSharp):
    1. Graphics.Blit (A, B, material);
    where material has your drawing shader.
    As for performance issues, I haven't ecnountered any, but I was targeting middle-to-high-end devices, so I'm afraid you'll have to check performance yourself.
     
  8. RavenTravelStudios

    RavenTravelStudios

    Joined:
    Oct 15, 2015
    Posts:
    99
    Hi, can you explain more about this?
    I have set the Depth mask as the line shader and i have my separate camera with depth only prop.

    Now i'd like to know:
    - Where to put the texture (i suppose on the separate cam)
    - How to modify the script to use "Draw3D" methods...

    If you could provide some code behind the demo you posted that would be great.

    Thank you.