tonynogo

Demo 61 - Fading when too edge

Jul 6th, 2017
6,589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/FadingWhenTooEdge"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Main Texture", 2D) = "white" {}
  6.         _Threshold ("Threshold", Range(0.0, 1.0)) = 0.0
  7.     }
  8.     SubShader
  9.     {
  10.         Tags { "RenderType"="Transparent" }
  11.         Blend SrcAlpha OneMinusSrcAlpha
  12.         Cull Off
  13.  
  14.         Pass {
  15.             CGPROGRAM
  16.             #pragma vertex vert
  17.             #pragma fragment frag
  18.             #include "UnityCG.cginc"
  19.  
  20.             struct v2f {
  21.                 float4 pos : SV_POSITION;
  22.                 half2 uv : TEXCOORD0;
  23.                 fixed val : TEXCOORD1;
  24.             };
  25.  
  26.             sampler2D _MainTex;
  27.             float4 _MainTex_ST;
  28.  
  29.             v2f vert(appdata_base v) {
  30.                 v2f o;
  31.                 float4 worldpos = mul(unity_ObjectToWorld, v.vertex);
  32.                 o.pos = mul(UNITY_MATRIX_VP, worldpos);
  33.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  34.  
  35.                 float3 worldnormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
  36.                 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - worldpos.xyz);
  37.                 o.val = abs(dot(worldnormal, viewDir));
  38.                 return o;
  39.             }
  40.  
  41.             fixed _Threshold;
  42.  
  43.             fixed4 frag(v2f i) : SV_Target {
  44.                 fixed4 col = tex2D(_MainTex, i.uv);
  45.                 col.a *= step(_Threshold + 0.01, i.val);
  46.                 return col;
  47.             }
  48.  
  49.             ENDCG
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment