Search Unity

Question 3d scrollrect mask for unlit/texture shader

Discussion in 'Shader Graph' started by mige5, Mar 8, 2020.

  1. mige5

    mige5

    Joined:
    Oct 20, 2017
    Posts:
    16
    Hey

    so I have this shader which supports supports masking 3d objects in scrollRect, but it makes my models look different, so I would need the feature transferred over to the default Unlit/texture shader, but I got no idea how to do that:
    Code (csharp):
    1.  
    2. Shader "Custom/NewSurfaceShader" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    7.         _Metallic ("Metallic", Range(0,1)) = 0.0
    8.  
    9.  
    10.         _StencilComp("Stencil Comparison", Float) = 8
    11.         _Stencil("Stencil ID", Float) = 0
    12.         _StencilOp("Stencil Operation", Float) = 0
    13.         _StencilWriteMask("Stencil Write Mask", Float) = 255
    14.         _StencilReadMask("Stencil Read Mask", Float) = 255
    15.  
    16.         _ColorMask("Color Mask", Float) = 15
    17.  
    18.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
    19.     }
    20.  
    21.     SubShader
    22.     {
    23.         Tags { "RenderType"="UI""Queue"="Transparent" }
    24.         LOD 200
    25.         ZWrite Off
    26.         ZTest[unity_GUIZTestMode]
    27.         Blend SrcAlpha OneMinusSrcAlpha
    28.         ColorMask[_ColorMask]
    29.        
    30.         Stencil
    31.         {
    32.             Ref 1
    33.             Comp LEqual
    34.             Pass Keep
    35.             ReadMask [_StencilReadMask]
    36.             WriteMask [_StencilWriteMask]
    37.         }
    38.  
    39.         CGPROGRAM
    40.         // Physically based Standard lighting model, and enable shadows on all light types
    41.         #pragma surface surf Standard vertex:vert fullforwardshadows
    42.  
    43.         // Use shader model 3.0 target, to get nicer looking lighting
    44.         #pragma target 3.0
    45.  
    46.         #include "UnityUI.cginc"
    47.            
    48.         struct Input
    49.         {
    50.             float2 uv_MainTex;
    51.             float4 worldPosition;
    52.         };
    53.  
    54.         void vert(inout appdata_full v, out Input o)
    55.         {
    56.             UNITY_INITIALIZE_OUTPUT(Input, o);
    57.             o.worldPosition = v.vertex;
    58.         }
    59.  
    60.         sampler2D _MainTex;
    61.         half _Glossiness;
    62.         half _Metallic;
    63.         fixed4 _Color;
    64.         float4 _ClipRect;
    65.  
    66.         void surf (Input IN, inout SurfaceOutputStandard o) {
    67.             // Albedo comes from a texture tinted by color
    68.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    69.             o.Albedo = c.rgb;
    70.             // Metallic and smoothness come from slider variables
    71.             o.Metallic = _Metallic;
    72.             o.Smoothness = _Glossiness;
    73.             o.Alpha = c.a;
    74.             o.Alpha *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    75.            
    76. #ifdef UNITY_UI_ALPHACLIP
    77.             clip(o.Alpha - 0.001);
    78.             #endif
    79.         }
    80.         ENDCG
    81.     }
    82.     FallBack "Diffuse"
    83. }
    84.  
    Default Unlit/Texture shader:
    Code (csharp):
    1.  
    2. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    3.  
    4. // Unlit shader. Simplest possible textured shader.
    5. // - no lighting
    6. // - no lightmap support
    7. // - no per-material color
    8.  
    9. Shader "Unlit/Texture2" {
    10. Properties {
    11.     _MainTex ("Base (RGB)", 2D) = "white" {}
    12. }
    13.  
    14. SubShader {
    15.     Tags { "RenderType"="Opaque" }
    16.     LOD 100
    17.  
    18.     Pass {
    19.         CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma target 2.0
    23.             #pragma multi_compile_fog
    24.  
    25.             #include "UnityCG.cginc"
    26.  
    27.             struct appdata_t {
    28.                 float4 vertex : POSITION;
    29.                 float2 texcoord : TEXCOORD0;
    30.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    31.             };
    32.  
    33.             struct v2f {
    34.                 float4 vertex : SV_POSITION;
    35.                 float2 texcoord : TEXCOORD0;
    36.                 UNITY_FOG_COORDS(1)
    37.                 UNITY_VERTEX_OUTPUT_STEREO
    38.             };
    39.  
    40.             sampler2D _MainTex;
    41.             float4 _MainTex_ST;
    42.  
    43.             v2f vert (appdata_t v)
    44.             {
    45.                 v2f o;
    46.                 UNITY_SETUP_INSTANCE_ID(v);
    47.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);
    49.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    50.                 UNITY_TRANSFER_FOG(o,o.vertex);
    51.                 return o;
    52.             }
    53.  
    54.             fixed4 frag (v2f i) : SV_Target
    55.             {
    56.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    57.                 UNITY_APPLY_FOG(i.fogCoord, col);
    58.                 UNITY_OPAQUE_ALPHA(col.a);
    59.                 return col;
    60.             }
    61.         ENDCG
    62.     }
    63. }
    64.  
    65. }
    66.