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

Question How to draw a mask?

Discussion in '2D' started by lfirek, Jun 29, 2023.

  1. lfirek

    lfirek

    Joined:
    Aug 23, 2022
    Posts:
    1
    Hi there,

    I am prototyping mobile game where you can draw by using line renderer but in developing some features I had problem.

    The feature is that you draw with specific texture but not in normal way, texture should be static. It's similar to scraping.

    Example:
    example.png

    The problem is that I can not find easy solution for masking. Line renderer can not be sprite mask but that might have been the easiest way.
    The easiest way is to instantiate some mask pointer (sprite mask) in every move but for me it's not complex solution because we get too many objects.

    How can I get result like in example in the simplest and performance way?
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    You could just paint a texture with C#, it is not that complex. For most performance you should use native pixel data. Also for a simple mask just a single channel texture will work. Lower resolution textures will work faster

    For more performance, you can use an additional non-native pixel array to modify its values only if those pixels are not painted; then copy the data to the native pixel array periodically or something; test this yourself. I do this to skip complex paint logic. Periodically updating data instead of every frame might also help with reducing cpu-gpu bus usage

    Here is some example code https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Texture2D.GetPixelData.html
    Note that this example uses mipmaps, you probably won't want to use them. Also the example uses a 4 channel texture, hence why it does this:
    Code (CSharp):
    1. m_Texture2D.GetPixelData<Color32>(1);
    You'll probably want to do it like:
    Code (CSharp):
    1. m_Texture2D.GetPixelData<byte>(0);
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563