Search Unity

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:
    99
  3. paradizIsCool

    paradizIsCool

    Joined:
    Jul 10, 2014
    Posts:
    178
    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:
    3
    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
     
  5. LiChenGuang

    LiChenGuang

    Joined:
    Aug 7, 2019
    Posts:
    2
    • How is "worldPosition" assigned
     
  6. LiChenGuang

    LiChenGuang

    Joined:
    Aug 7, 2019
    Posts:
    2
    I found a way,
    Add the follwing code to the vert function:
    o.worldPosition = v.vertex;
    "v" is vertex-shader struct, "o" is vertex-shader-to-fragment-shader struct
    Add the follwing code to the frag function:
    clip(col.a - 0.001);
    Where "colour" is the float4 that'll be returned as the final pixel colour