Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Shader Forge Uinty UI RectMask2D

Discussion in 'UGUI & TextMesh Pro' started by abitofjohn, Mar 9, 2018.

  1. abitofjohn

    abitofjohn

    Joined:
    Nov 6, 2012
    Posts:
    27
    Hi,

    I'm trying to create a UI shader with shader forge. I have it working for the most part but I don't know how to make it work with a RectMask2D. When the UI item with the shader is outside the RectMask2D it still renders.

    I've tried following this guide but it doesn't seem to help. Has anyone had this issue or know of a way to make a custom shader work with RectMask2D?


    Many Thanks

    JT
     
  2. alan_motionlab

    alan_motionlab

    Joined:
    Nov 27, 2014
    Posts:
    90
  3. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    151
    I can explain why, but I cannot check how to do this right now (take a look at the default UI shaders, it may use something like partial culling or alpha culling, I don't remember)

    RectMask2D does not use stencil
    Mask uses stencil


    A RectMask2D: *Only works in the 2D plane *Requires elements on the mask to be coplanar. *Does not require stencil buffer / extra draw calls *Requres fewer draw calls *Culls elements that are outside the mask area.
     
    Last edited: Mar 27, 2018
  4. mark-tt

    mark-tt

    Joined:
    Jan 17, 2018
    Posts:
    2
    Here are the steps you need to take to make a custom shader work with RectMask2D:
    • Add this variable to the SubShader's Pass clause:
    • float4 _ClipRect;
    • Note that there's no need to add a corresponding property for it
    • Add this include if you're shader doesn't already have it:
    • #include "UnityUI.cginc"
    • Make sure your vertex-shader-to-fragment-shader struct contains this field:
    • float4 worldPosition : TEXCOORD1;
    • Towards the end of the fragment shader add the following code:
    • colour.a *= UnityGet2DClipping(input.worldPosition.xy, _ClipRect);
    • Where "colour" is the float4 that'll be returned as the final pixel colour
    • And "input" is the vertex-shader-to-fragment-shader struct passed to the fragment shader
     
unityunity