Advertisement
Guest User

DitherFade.shader

a guest
Dec 23rd, 2021
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/DitherFade"
  2. {
  3.     Properties
  4.     {
  5.         _Color ("Color", Color) = (1,1,1,1)        
  6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
  7.         _Noise ("Noise (RGB)", 2D) = "white" {}
  8.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
  9.         _Metallic ("Metallic", Range(0,1)) = 0.0
  10.         _PosSlider ("Cutoff Slider", float) = 0.0
  11.         _Dither("Dither", float) = 0.0
  12.         _AlphaThreshold("Alphacutoff", Range(0,1)) = 0.0
  13.         _Fade("Fade", Range(0,10)) = 5
  14.  
  15.     }
  16.     SubShader
  17.     {
  18.         Tags { "RenderType"="Opaque" }
  19.         LOD 200
  20.         //  Blend SrcAlpha OneMinusSrcAlpha
  21.         Cull Off
  22.  
  23.         CGPROGRAM
  24.         // Physically based Standard lighting model, and enable shadows on all light types
  25.         #pragma surface surf Standard vertex:vert addshadow
  26.  
  27.         // Use shader model 3.0 target, to get nicer looking lighting
  28.         #pragma target 4.0
  29.  
  30.         sampler2D _MainTex, _Noise;
  31.  
  32.         struct Input
  33.         {
  34.             float2 uv_MainTex;
  35.             float3 localPos;
  36.             float vFace : VFACE;
  37.             float4 screenPos;
  38.         };
  39.  
  40.         half _Glossiness;
  41.         half _Metallic;
  42.         fixed4 _Color;
  43.         half _PosSlider;
  44.         float _Dither;
  45.         float _AlphaThreshold,_Fade;
  46.        
  47.         void vert (inout appdata_full v, out Input o) {
  48.             UNITY_INITIALIZE_OUTPUT(Input,o);        
  49.             o.localPos = v.vertex.xyz;
  50.         }
  51.  
  52.         float4 Unity_Dither_float4(float4 In, float4 ScreenPosition)
  53.         {
  54.             float2 coords = ScreenPosition.xy / ScreenPosition.w;
  55.             float2 uv = coords * _ScreenParams.xy;
  56.             float DITHER_THRESHOLDS[16] =
  57.             {
  58.                 1.0 / 17.0,  9.0 / 17.0,  3.0 / 17.0, 11.0 / 17.0,
  59.                 13.0 / 17.0,  5.0 / 17.0, 15.0 / 17.0,  7.0 / 17.0,
  60.                 4.0 / 17.0, 12.0 / 17.0,  2.0 / 17.0, 10.0 / 17.0,
  61.                 16.0 / 17.0,  8.0 / 17.0, 14.0 / 17.0,  6.0 / 17.0
  62.             };
  63.             uint index = (uint(uv.x) % 4) * 4 + uint(uv.y) % 4;
  64.             return In - DITHER_THRESHOLDS[index];
  65.         }
  66.  
  67.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
  68.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
  69.         // #pragma instancing_options assumeuniformscaling
  70.         UNITY_INSTANCING_BUFFER_START(Props)
  71.         // put more per-instance properties here
  72.         UNITY_INSTANCING_BUFFER_END(Props)
  73.  
  74.         void surf (Input IN, inout SurfaceOutputStandard o)
  75.         {
  76.             // Albedo comes from a texture tinted by color
  77.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
  78.             o.Albedo = c;
  79.              //  the object position and a slider that moves over the object
  80.             float objectPosSliding = IN.localPos.y  + _PosSlider;
  81.             // the dither effect, adding in the cutoff
  82.             float ditheredCutoff = Unity_Dither_float4(_Dither, IN.screenPos).r + (1-(saturate(objectPosSliding)) * _Fade) ;
  83.             // discard pixels based on dither
  84.             clip(ditheredCutoff - _AlphaThreshold);
  85.             // Metallic and smoothness come from slider variables
  86.             o.Metallic = _Metallic;
  87.             o.Smoothness = _Glossiness;
  88.         }
  89.         ENDCG
  90.     }
  91.     FallBack "Diffuse"
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement