Advertisement
tonynogo

Demo 89 - Dissolve with depth value

Jul 6th, 2017
5,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/DepthDissolve"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Main Texture", 2D) = "white" {}
  6.  
  7.         [Header(Dissolution)]
  8.         _DisBegin ("Begin (The lower, the closer of the camera)", Range(0., 1.0)) = 0.
  9.         _DisEnd ("End (Should be lower than Begin value)", Range(0., 1.0)) = 0.
  10.  
  11.         [Header(Ambient)]
  12.         _Ambient ("Intensity", Range(0., 1.)) = 0.1
  13.         _AmbColor ("Color", color) = (1., 1., 1., 1.)
  14.  
  15.         [Header(Diffuse)]
  16.         _Diffuse ("Val", Range(0., 1.)) = 1.
  17.         _DifColor ("Color", color) = (1., 1., 1., 1.)
  18.  
  19.         [Header(Specular)]
  20.         [Toggle] _Spec("Enabled?", Float) = 0.
  21.         _Shininess ("Shininess", Range(0.1, 10)) = 1.
  22.         _SpecColor ("Specular color", color) = (1., 1., 1., 1.)
  23.  
  24.         [Header(Emission)]
  25.         _EmissionTex ("Emission texture", 2D) = "gray" {}
  26.         _EmiVal ("Intensity", float) = 0.
  27.         [HDR]_EmiColor ("Color", color) = (1., 1., 1., 1.)
  28.     }
  29.  
  30.     SubShader
  31.     {
  32.         Pass
  33.         {
  34.             Tags { "RenderType"="Opaque" "Queue"="Geometry" "LightMode"="ForwardBase"}
  35.  
  36.             //Blend SrcAlpha OneMinusSrcAlpha
  37.  
  38.             CGPROGRAM
  39.             #pragma vertex vert
  40.             #pragma fragment frag
  41.  
  42.             // Change "shader_feature" with "pragma_compile" if you want set this keyword from c# code
  43.             #pragma shader_feature __ _SPEC_ON
  44.  
  45.             #include "UnityCG.cginc"
  46.  
  47.             // Change path if needed
  48.             #include "Assets/Cginc/ClassicNoise3D.cginc"
  49.  
  50.             struct v2f {
  51.                 float4 pos : SV_POSITION;
  52.                 float2 uv : TEXCOORD0;
  53.                 float3 worldPos : TEXCOORD1;
  54.                 float3 worldNormal : TEXCOORD2;
  55.                 float4 projPos : TEXCOORD3;
  56.             };
  57.  
  58.             v2f vert(appdata_full v)
  59.             {
  60.                 v2f o;
  61.                 // World position
  62.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  63.  
  64.                 // Clip position
  65.                 o.pos = mul(UNITY_MATRIX_VP, float4(o.worldPos, 1.));
  66.  
  67.                 // Screen position
  68.                 o.projPos = ComputeScreenPos(o.pos);
  69.  
  70.                 // Normal in WorldSpace
  71.                 o.worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
  72.  
  73.                 o.uv = v.texcoord;
  74.  
  75.                 return o;
  76.             }
  77.  
  78.             sampler2D _MainTex;
  79.  
  80.             fixed4 _LightColor0;
  81.            
  82.             // Diffuse
  83.             fixed _Diffuse;
  84.             fixed4 _DifColor;
  85.  
  86.             //Specular
  87.             fixed _Shininess;
  88.             fixed4 _SpecColor;
  89.            
  90.             //Ambient
  91.             fixed _Ambient;
  92.             fixed4 _AmbColor;
  93.  
  94.             // Emission
  95.             sampler2D _EmissionTex;
  96.             fixed4 _EmiColor;
  97.             fixed _EmiVal;
  98.  
  99.             // Dissolution
  100.             fixed _DisBegin;
  101.             float _DisEnd;
  102.  
  103.             // Depth texture
  104.             sampler2D _CameraDepthTexture;
  105.  
  106.             fixed4 frag(v2f i) : SV_Target
  107.             {
  108.                 fixed4 c = tex2D(_MainTex, i.uv);
  109.  
  110.                 // Light direction
  111.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  112.  
  113.                 // Camera direction
  114.                 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
  115.  
  116.                 float3 worldNormal = normalize(i.worldNormal);
  117.  
  118.                 // Compute ambient lighting
  119.                 fixed4 amb = _Ambient * _AmbColor;
  120.  
  121.                 // Compute the diffuse lighting
  122.                 fixed4 NdotL = max(0., dot(worldNormal, lightDir) * _LightColor0);
  123.                 fixed4 dif = NdotL * _Diffuse * _LightColor0 * _DifColor;
  124.  
  125.                 fixed4 light = dif + amb;
  126.  
  127.                 // Compute the specular lighting
  128.                 #if _SPEC_ON
  129.                 float3 refl = normalize(reflect(-lightDir, worldNormal));
  130.                 float RdotV = max(0., dot(refl, viewDir));
  131.                 fixed4 spec = pow(RdotV, _Shininess) * _LightColor0 * ceil(NdotL) * _SpecColor;
  132.  
  133.                 light += spec;
  134.                 #endif
  135.  
  136.                 c.rgb *= light.rgb;
  137.  
  138.                 // Compute emission
  139.                 fixed4 emi = tex2D(_EmissionTex, i.uv).r * _EmiColor * _EmiVal;
  140.                 c.rgb += emi.rgb;
  141.  
  142.                 // Retrieve the depth value
  143.                 float depth = Linear01Depth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);
  144.  
  145.                 // To avoid some artefacts
  146.                 if(depth == 1.)
  147.                     discard;
  148.  
  149.                 // Establish if we display the pixel or discard it
  150.                 float ind = step(depth, _DisBegin) * (1 - (depth - _DisEnd) / (_DisBegin - _DisEnd));
  151.                 if((cnoise(i.worldPos) + 1.) / 2. <= ind)
  152.                     discard;
  153.  
  154.                 return c;
  155.             }
  156.  
  157.             ENDCG
  158.         }
  159.     }
  160.  
  161.     // !! Needed to use the depth texture !!
  162.     FallBack "Diffuse"
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement