Search Unity

Render Texture & Sprite Masks

Discussion in 'General Graphics' started by jlanisdev, Aug 14, 2019.

  1. jlanisdev

    jlanisdev

    Joined:
    Jan 18, 2016
    Posts:
    76
    I'm trying to create a new sprite dynamically from two other sprites (using the built in sprite mask). However the mask is not getting applied when using RenderTexture, hence the final generated sprite is exactly the same as the original without the mask applied. Using 2017.4.29. Is there anyway to use the built in sprite mask functionality with RenderTexture, and if not - why?
     
    Last edited: Aug 15, 2019
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Sprite masks make use of the stencil buffer, so you will need to make sure your RenderTexture's "Depth Buffer" setting is set to include a stencil buffer.
     
  3. jlanisdev

    jlanisdev

    Joined:
    Jan 18, 2016
    Posts:
    76
    I have the depth parameter set to 32 when creating the RenderTexture; is that what you mean?

    Code (CSharp):
    1.  
    2. cam.targetTexture = new RenderTexture(width, height, 32, RenderTextureFormat.ARGB32);//RenderTexture.GetTemporary(width, height, 24);
    3.  
    4. cam.Render();
    5.  
    6.  // Activate the temporary render texture
    7. RenderTexture previouslyActiveRenderTexture = RenderTexture.active;
    8. RenderTexture.active = cam.targetTexture;
    9.  
    10. Texture2D texture = new Texture2D(cam.targetTexture.width, cam.targetTexture.height, TextureFormat.ARGB32, false);
    11. texture.ReadPixels(new Rect(0, 0, cam.targetTexture.width, cam.targetTexture.height), 0, 0);
    It's still not working. Is there something else I'm supposed to be setting?
     
    Last edited: Aug 15, 2019
  4. jlanisdev

    jlanisdev

    Joined:
    Jan 18, 2016
    Posts:
    76
    Not exactly sure why, but I ended up cloning my existing camera instead of creating a new one when rendering the texture and that solved the issue