Search Unity

How to mask render texture

Discussion in 'Scripting' started by donamin1, Dec 17, 2015.

  1. donamin1

    donamin1

    Joined:
    Apr 4, 2013
    Posts:
    26
    Hi
    I have a render texture assigned to a raw image in my UI.
    I'm applying a mask to this raw image but it's just not working. I'm sure that mask setup is correct.
    Can anyone tell me how to solve this issue?
    Thanks
    Amin
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you have to use a shader that does the masking
     
  3. donamin1

    donamin1

    Joined:
    Apr 4, 2013
    Posts:
    26
    I'm not familiar with shaders. Is there any suitable tutorial link for this?
    Thanks
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  5. donamin1

    donamin1

    Joined:
    Apr 4, 2013
    Posts:
    26
    Thanks.
    I searched a lot and got this shader for masking:

    Shader "MaskedTexture"
    {
    Properties
    {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Mask ("Culling Mask", 2D) = "white" {}
    }
    SubShader
    {
    Tags {"Queue"="Transparent"}
    Lighting Off
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    Pass
    {
    SetTexture [_Mask] {combine texture}
    SetTexture [_MainTex] {combine texture, previous}
    }
    }
    }

    I'm using a simple mask with a circle in the center to filer everything around the circle.

    It's strange but as you can see, in the result it seems that final material has a little transparency in the central circle. Anyone know how to solve that?
     
    sonofbryce likes this.
  6. RepoGames

    RepoGames

    Joined:
    Apr 15, 2016
    Posts:
    69
    Try turning off ZTest or change Blend property
     
  7. ShoaibAslamTintash

    ShoaibAslamTintash

    Joined:
    Sep 6, 2018
    Posts:
    11
    Thanks.
     
  8. nilupul_unity

    nilupul_unity

    Joined:
    Mar 22, 2021
    Posts:
    1
    In the render texture, try to change "Depth Buffer" to "At least 24 bits depth (with stencil)"