Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Resolved TilemapRenderer custom shader with 2D Renderer in URP (7.3.1)

Discussion in '2D Experimental Preview' started by Djayp, Aug 1, 2020.

  1. Djayp

    Djayp

    Joined:
    Feb 16, 2015
    Posts:
    114
    Hello ! Using the stencil buffer, I'm able to mask things outside of the renderer bounds, but it doesn't care about the alpha inside of the bounds. Any idea about how to proceed ?

    (Left : Tilemap ; Right : Blood) upload_2020-8-1_1-44-45.png

    Code (CSharp):
    1. Shader "Custom/Custom Stencil"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData]_MainTex("Diffuse", 2D) = "white" {}
    6.         [PerRendererData]_MaskTex("Mask", 2D) = "white" {}
    7.         [PerRendererData]_NormalMap("Normal Map", 2D) = "bump" {}
    8.  
    9.         [Header(Option)]
    10.         [Enum(UnityEngine.Rendering.BlendOp)]  _BlendOp("BlendOp", Float) = 0
    11.         [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("SrcBlend", Float) = 1
    12.         [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("DstBlend", Float) = 0
    13.         [Enum(Off, 0, On, 1)]_ZWriteMode("ZWriteMode", float) = 1
    14.         [Enum(UnityEngine.Rendering.CullMode)]_CullMode("CullMode", float) = 2
    15.         [Enum(UnityEngine.Rendering.CompareFunction)]_ZTestMode("ZTestMode", Float) = 4
    16.         [Enum(UnityEngine.Rendering.ColorWriteMask)]_ColorMask("ColorMask", Float) = 15
    17.  
    18.         [Header(Stencil)]
    19.         [Enum(UnityEngine.Rendering.CompareFunction)]_StencilComp("Stencil Comparison", Float) = 8
    20.         [IntRange]_StencilWriteMask("Stencil Write Mask", Range(0,255)) = 255
    21.         [IntRange]_StencilReadMask("Stencil Read Mask", Range(0,255)) = 255
    22.         [IntRange]_Stencil("Stencil ID", Range(0,255)) = 0
    23.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilPass("Stencil Pass", Float) = 0
    24.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilFail("Stencil Fail", Float) = 0
    25.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilZFail("Stencil ZFail", Float) = 0
    26.  
    27.         // Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
    28.         [HideInInspector] _Color("Tint", Color) = (1,1,1,1)
    29.         [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
    30.         [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
    31.         [HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
    32.         [HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
    33.     }
    34.  
    35.     HLSLINCLUDE
    36.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    37.     ENDHLSL
    38.  
    39.     SubShader
    40.     {
    41.         Tags {
    42.             "Queue" = "Transparent"
    43.             "RenderType" = "Transparent"
    44.             "RenderPipeline" = "UniversalPipeline"
    45.             "IgnoreProjector" = "True"
    46.             "CanUseSpriteAtlas" = "True"
    47.         }
    48.  
    49.         BlendOp[_BlendOp]
    50.         Blend[_SrcBlend][_DstBlend]
    51.         ZWrite[_ZWriteMode]
    52.         ZTest[_ZTestMode]
    53.         Cull[_CullMode]
    54.         ColorMask[_ColorMask]
    55.  
    56.         Stencil
    57.         {
    58.             Ref[_Stencil]
    59.             Comp[_StencilComp]
    60.             ReadMask[_StencilReadMask]
    61.             WriteMask[_StencilWriteMask]
    62.             Pass[_StencilPass]
    63.             Fail[_StencilFail]
    64.             ZFail[_StencilZFail]
    65.         }
    66.  
    67.         Pass
    68.         {
    69.             Tags { "LightMode" = "Universal2D" }
    70.             HLSLPROGRAM
    71.             #pragma prefer_hlslcc gles
    72.             #pragma vertex CombinedShapeLightVertex
    73.             #pragma fragment CombinedShapeLightFragment
    74.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
    75.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
    76.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
    77.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
    78.  
    79.             struct Attributes
    80.             {
    81.                 float3 positionOS   : POSITION;
    82.                 float4 color        : COLOR;
    83.                 float2  uv           : TEXCOORD0;
    84.             };
    85.  
    86.             struct Varyings
    87.             {
    88.                 float4  positionCS  : SV_POSITION;
    89.                 float4  color       : COLOR;
    90.                 float2    uv          : TEXCOORD0;
    91.                 float2    lightingUV  : TEXCOORD1;
    92.             };
    93.  
    94.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
    95.  
    96.             TEXTURE2D(_MainTex);
    97.             SAMPLER(sampler_MainTex);
    98.             TEXTURE2D(_MaskTex);
    99.             SAMPLER(sampler_MaskTex);
    100.             TEXTURE2D(_NormalMap);
    101.             SAMPLER(sampler_NormalMap);
    102.             half4 _MainTex_ST;
    103.             half4 _NormalMap_ST;
    104.  
    105.             #if USE_SHAPE_LIGHT_TYPE_0
    106.             SHAPE_LIGHT(0)
    107.             #endif
    108.  
    109.             #if USE_SHAPE_LIGHT_TYPE_1
    110.             SHAPE_LIGHT(1)
    111.             #endif
    112.  
    113.             #if USE_SHAPE_LIGHT_TYPE_2
    114.             SHAPE_LIGHT(2)
    115.             #endif
    116.  
    117.             #if USE_SHAPE_LIGHT_TYPE_3
    118.             SHAPE_LIGHT(3)
    119.             #endif
    120.  
    121.             Varyings CombinedShapeLightVertex(Attributes v)
    122.             {
    123.                 Varyings o = (Varyings)0;
    124.  
    125.                 o.positionCS = TransformObjectToHClip(v.positionOS);
    126.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    127.                 float4 clipVertex = o.positionCS / o.positionCS.w;
    128.                 o.lightingUV = ComputeScreenPos(clipVertex).xy;
    129.                 if (v.color.a < .9) o.color = 0;
    130.                 else o.color = v.color;
    131.  
    132.                 return o;
    133.             }
    134.  
    135.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
    136.  
    137.             half4 CombinedShapeLightFragment(Varyings i) : SV_Target
    138.             {
    139.                 half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    140.  
    141.                 if (main.z < .0078431)
    142.                 {
    143.                     discard;
    144.                 }
    145.  
    146.                 half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
    147.                 return CombinedShapeLightShared(main, mask, i.lightingUV);
    148.             }
    149.             ENDHLSL
    150.         }
    151.  
    152.         Pass
    153.         {
    154.             Tags { "LightMode" = "NormalsRendering"}
    155.             HLSLPROGRAM
    156.             #pragma prefer_hlslcc gles
    157.             #pragma vertex NormalsRenderingVertex
    158.             #pragma fragment NormalsRenderingFragment
    159.  
    160.             struct Attributes
    161.             {
    162.                 float3 positionOS   : POSITION;
    163.                 float4 color        : COLOR;
    164.                 float2 uv            : TEXCOORD0;
    165.                 float4 tangent      : TANGENT;
    166.             };
    167.  
    168.             struct Varyings
    169.             {
    170.                 float4  positionCS        : SV_POSITION;
    171.                 float4  color            : COLOR;
    172.                 float2    uv                : TEXCOORD0;
    173.                 float3  normalWS        : TEXCOORD1;
    174.                 float3  tangentWS        : TEXCOORD2;
    175.                 float3  bitangentWS        : TEXCOORD3;
    176.             };
    177.  
    178.             TEXTURE2D(_MainTex);
    179.             SAMPLER(sampler_MainTex);
    180.             TEXTURE2D(_NormalMap);
    181.             SAMPLER(sampler_NormalMap);
    182.             float4 _NormalMap_ST;  // Is this the right way to do this?
    183.  
    184.             Varyings NormalsRenderingVertex(Attributes attributes)
    185.             {
    186.                 Varyings o = (Varyings)0;
    187.  
    188.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    189.                 o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
    190.                 o.uv = attributes.uv;
    191.                 if (attributes.color.a < .9) o.color = 0;
    192.                 else o.color = attributes.color;
    193.                 o.normalWS = TransformObjectToWorldDir(float3(0, 0, -1));
    194.                 o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
    195.                 o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
    196.                 return o;
    197.             }
    198.  
    199.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
    200.  
    201.             float4 NormalsRenderingFragment(Varyings i) : SV_Target
    202.             {
    203.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    204.  
    205.                 if (mainTex.z < 1)
    206.                 {
    207.                     discard;
    208.                 }
    209.  
    210.                 float3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
    211.                 return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
    212.             }
    213.             ENDHLSL
    214.         }
    215.  
    216.         Stencil
    217.         {
    218.             Ref 0
    219.             Comp never
    220.         }
    221.  
    222.         Pass
    223.         {
    224.             Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
    225.  
    226.             HLSLPROGRAM
    227.             #pragma prefer_hlslcc gles
    228.             #pragma vertex UnlitVertex
    229.             #pragma fragment UnlitFragment
    230.  
    231.             struct Attributes
    232.             {
    233.                 float3 positionOS   : POSITION;
    234.                 float4 color        : COLOR;
    235.                 float2 uv            : TEXCOORD0;
    236.             };
    237.  
    238.             struct Varyings
    239.             {
    240.                 float4  positionCS        : SV_POSITION;
    241.                 float4  color            : COLOR;
    242.                 float2    uv                : TEXCOORD0;
    243.             };
    244.  
    245.             TEXTURE2D(_MainTex);
    246.             SAMPLER(sampler_MainTex);
    247.             float4 _MainTex_ST;
    248.  
    249.             Varyings UnlitVertex(Attributes attributes)
    250.             {
    251.                 Varyings o = (Varyings)0;
    252.  
    253.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    254.                 o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
    255.                 o.uv = attributes.uv;
    256.                 o.color = attributes.color;
    257.                 return o;
    258.             }
    259.  
    260.             float4 UnlitFragment(Varyings i) : SV_Target
    261.             {
    262.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    263.  
    264.                 if (mainTex.z < 1)
    265.                 {
    266.                     discard;
    267.                 }
    268.  
    269.                 return mainTex;
    270.             }
    271.             ENDHLSL
    272.         }
    273.  
    274.     }
    275. }
     
    Last edited: Aug 1, 2020
  2. Djayp

    Djayp

    Joined:
    Feb 16, 2015
    Posts:
    114
    Ok I found my mistake. I had to clip the transparent pixels from the tilemap :
    upload_2020-8-2_16-7-52.png
    Tilemap | Blood​
    upload_2020-8-2_16-9-25.png upload_2020-8-2_16-10-24.png
    Code (CSharp):
    1. Shader "DayDream/Custom Stencil"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData]_MainTex("Diffuse", 2D) = "white" {}
    6.         [PerRendererData]_MaskTex("Mask", 2D) = "white" {}
    7.         [PerRendererData]_NormalMap("Normal Map", 2D) = "bump" {}
    8.         [Enum(Off, 0, On, 1)]_CutTransparent("Cut", float) = 1
    9.  
    10.         [Header(Option)]
    11.         [Enum(UnityEngine.Rendering.BlendOp)]  _BlendOp("BlendOp", Float) = 0
    12.         [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("SrcBlend", Float) = 1
    13.         [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("DstBlend", Float) = 0
    14.         [Enum(Off, 0, On, 1)]_ZWriteMode("ZWriteMode", float) = 1
    15.         [Enum(UnityEngine.Rendering.CullMode)]_CullMode("CullMode", float) = 2
    16.         [Enum(UnityEngine.Rendering.CompareFunction)]_ZTestMode("ZTestMode", Float) = 4
    17.         [Enum(None,0,Alpha,1,Red,8,Green,4,Blue,2,RGB,14,RGBA,15)]_ColorMask("ColorMask", Float) = 15
    18.  
    19.         [Header(Stencil)]
    20.         [Enum(UnityEngine.Rendering.CompareFunction)]_StencilComp("Stencil Comparison", Float) = 8
    21.         [IntRange]_StencilWriteMask("Stencil Write Mask", Range(0,255)) = 255
    22.         [IntRange]_StencilReadMask("Stencil Read Mask", Range(0,255)) = 255
    23.         [IntRange]_Stencil("Stencil ID", Range(0,255)) = 0
    24.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilPass("Stencil Pass", Float) = 0
    25.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilFail("Stencil Fail", Float) = 0
    26.         [Enum(UnityEngine.Rendering.StencilOp)]_StencilZFail("Stencil ZFail", Float) = 0
    27.  
    28.         // Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
    29.         [HideInInspector] _Color("Tint", Color) = (1,1,1,1)
    30.         [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
    31.         [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
    32.         [HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
    33.         [HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
    34.     }
    35.  
    36.     HLSLINCLUDE
    37.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    38.     ENDHLSL
    39.  
    40.     SubShader
    41.     {
    42.         Tags {
    43.             "Queue" = "Transparent"
    44.             "RenderType" = "Transparent"
    45.             "RenderPipeline" = "UniversalPipeline"
    46.             "IgnoreProjector" = "True"
    47.             "CanUseSpriteAtlas" = "True"
    48.         }
    49.  
    50.         BlendOp[_BlendOp]
    51.         Blend[_SrcBlend][_DstBlend]
    52.         ZWrite[_ZWriteMode]
    53.         ZTest[_ZTestMode]
    54.         Cull[_CullMode]
    55.         ColorMask[_ColorMask]
    56.  
    57.         Stencil
    58.         {
    59.             Ref[_Stencil]
    60.             Comp[_StencilComp]
    61.             ReadMask[_StencilReadMask]
    62.             WriteMask[_StencilWriteMask]
    63.             Pass[_StencilPass]
    64.             Fail[_StencilFail]
    65.             ZFail[_StencilZFail]
    66.         }
    67.  
    68.         Pass
    69.         {
    70.             Tags { "LightMode" = "Universal2D" }
    71.             HLSLPROGRAM
    72.             #pragma prefer_hlslcc gles
    73.             #pragma vertex CombinedShapeLightVertex
    74.             #pragma fragment CombinedShapeLightFragment
    75.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
    76.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
    77.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
    78.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
    79.  
    80.             struct Attributes
    81.             {
    82.                 float3 positionOS   : POSITION;
    83.                 float4 color        : COLOR;
    84.                 float2  uv           : TEXCOORD0;
    85.             };
    86.  
    87.             struct Varyings
    88.             {
    89.                 float4  positionCS  : SV_POSITION;
    90.                 float4  color       : COLOR;
    91.                 float2    uv          : TEXCOORD0;
    92.                 float2    lightingUV  : TEXCOORD1;
    93.             };
    94.  
    95.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
    96.  
    97.             TEXTURE2D(_MainTex);
    98.             SAMPLER(sampler_MainTex);
    99.             TEXTURE2D(_MaskTex);
    100.             SAMPLER(sampler_MaskTex);
    101.             TEXTURE2D(_NormalMap);
    102.             SAMPLER(sampler_NormalMap);
    103.             half4 _MainTex_ST;
    104.             half4 _NormalMap_ST;
    105.             float _CutTransparent;
    106.  
    107.             #if USE_SHAPE_LIGHT_TYPE_0
    108.             SHAPE_LIGHT(0)
    109.             #endif
    110.  
    111.             #if USE_SHAPE_LIGHT_TYPE_1
    112.             SHAPE_LIGHT(1)
    113.             #endif
    114.  
    115.             #if USE_SHAPE_LIGHT_TYPE_2
    116.             SHAPE_LIGHT(2)
    117.             #endif
    118.  
    119.             #if USE_SHAPE_LIGHT_TYPE_3
    120.             SHAPE_LIGHT(3)
    121.             #endif
    122.  
    123.             Varyings CombinedShapeLightVertex(Attributes v)
    124.             {
    125.                 Varyings o = (Varyings)0;
    126.  
    127.                 o.positionCS = TransformObjectToHClip(v.positionOS);
    128.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    129.                 float4 clipVertex = o.positionCS / o.positionCS.w;
    130.                 o.lightingUV = ComputeScreenPos(clipVertex).xy;
    131.                 if (v.color.a < .9) o.color = 0;
    132.                 else o.color = v.color;
    133.  
    134.                 return o;
    135.             }
    136.  
    137.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
    138.  
    139.             half4 CombinedShapeLightFragment(Varyings i) : SV_Target
    140.             {
    141.                 half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    142.              
    143.                 if (_CutTransparent == 1) clip(main.a - 0.1);
    144.  
    145.                 half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
    146.                 return CombinedShapeLightShared(main, mask, i.lightingUV);
    147.             }
    148.             ENDHLSL
    149.         }
    150.  
    151.         Pass
    152.         {
    153.             Tags { "LightMode" = "NormalsRendering"}
    154.             HLSLPROGRAM
    155.             #pragma prefer_hlslcc gles
    156.             #pragma vertex NormalsRenderingVertex
    157.             #pragma fragment NormalsRenderingFragment
    158.  
    159.             struct Attributes
    160.             {
    161.                 float3 positionOS   : POSITION;
    162.                 float4 color        : COLOR;
    163.                 float2 uv            : TEXCOORD0;
    164.                 float4 tangent      : TANGENT;
    165.             };
    166.  
    167.             struct Varyings
    168.             {
    169.                 float4  positionCS        : SV_POSITION;
    170.                 float4  color            : COLOR;
    171.                 float2    uv                : TEXCOORD0;
    172.                 float3  normalWS        : TEXCOORD1;
    173.                 float3  tangentWS        : TEXCOORD2;
    174.                 float3  bitangentWS        : TEXCOORD3;
    175.             };
    176.  
    177.             TEXTURE2D(_MainTex);
    178.             SAMPLER(sampler_MainTex);
    179.             TEXTURE2D(_NormalMap);
    180.             SAMPLER(sampler_NormalMap);
    181.             float4 _NormalMap_ST;  // Is this the right way to do this?
    182.  
    183.             Varyings NormalsRenderingVertex(Attributes attributes)
    184.             {
    185.                 Varyings o = (Varyings)0;
    186.  
    187.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    188.                 o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
    189.                 o.uv = attributes.uv;
    190.                 o.color = attributes.color;
    191.                 o.normalWS = TransformObjectToWorldDir(float3(0, 0, -1));
    192.                 o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
    193.                 o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
    194.                 return o;
    195.             }
    196.  
    197.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
    198.  
    199.             float4 NormalsRenderingFragment(Varyings i) : SV_Target
    200.             {
    201.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    202.  
    203.                 float3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
    204.                 return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
    205.             }
    206.             ENDHLSL
    207.         }
    208.  
    209.         Stencil
    210.         {
    211.             Ref 0
    212.             Comp never
    213.         }
    214.  
    215.         Pass
    216.         {
    217.             Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
    218.  
    219.             HLSLPROGRAM
    220.             #pragma prefer_hlslcc gles
    221.             #pragma vertex UnlitVertex
    222.             #pragma fragment UnlitFragment
    223.  
    224.             struct Attributes
    225.             {
    226.                 float3 positionOS   : POSITION;
    227.                 float4 color        : COLOR;
    228.                 float2 uv            : TEXCOORD0;
    229.             };
    230.  
    231.             struct Varyings
    232.             {
    233.                 float4  positionCS        : SV_POSITION;
    234.                 float4  color            : COLOR;
    235.                 float2    uv                : TEXCOORD0;
    236.             };
    237.  
    238.             TEXTURE2D(_MainTex);
    239.             SAMPLER(sampler_MainTex);
    240.             float4 _MainTex_ST;
    241.  
    242.             Varyings UnlitVertex(Attributes attributes)
    243.             {
    244.                 Varyings o = (Varyings)0;
    245.  
    246.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    247.                 o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
    248.                 o.uv = attributes.uv;
    249.                 o.color = attributes.color;
    250.                 return o;
    251.             }
    252.  
    253.             float4 UnlitFragment(Varyings i) : SV_Target
    254.             {
    255.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    256.  
    257.                 return mainTex;
    258.             }
    259.             ENDHLSL
    260.         }
    261.  
    262.     }
    263.  
    264.     Fallback "Sprites/Default"
    265. }

    Resource : https://nielson.dev/2015/12/splatter-effects-in-unity-using-the-stencil-buffer