Advertisement
tonynogo

Demo 87 - Dissolve 3D

Jul 6th, 2017
8,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Dissolve3D"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Main Texture", 2D) = "white" {}
  6.  
  7.         [Header(Dissolution)]
  8.         _DisVal ("Threshold", Range(0., 1.01)) = 0.
  9.  
  10.         [Header(Ambient)]
  11.         _Ambient ("Intensity", Range(0., 1.)) = 0.1
  12.         _AmbColor ("Color", color) = (1., 1., 1., 1.)
  13.  
  14.         [Header(Diffuse)]
  15.         _Diffuse ("Val", Range(0., 1.)) = 1.
  16.         _DifColor ("Color", color) = (1., 1., 1., 1.)
  17.  
  18.         [Header(Specular)]
  19.         [Toggle] _Spec("Enabled?", Float) = 0.
  20.         _Shininess ("Shininess", Range(0.1, 10)) = 1.
  21.         _SpecColor ("Specular color", color) = (1., 1., 1., 1.)
  22.  
  23.         [Header(Emission)]
  24.         _EmissionTex ("Emission texture", 2D) = "gray" {}
  25.         _EmiVal ("Intensity", float) = 0.
  26.         [HDR]_EmiColor ("Color", color) = (1., 1., 1., 1.)
  27.     }
  28.  
  29.     SubShader
  30.     {
  31.         Pass
  32.         {
  33.             Tags { "RenderType"="Transparent" "Queue"="Transparent" "LightMode"="ForwardBase" }
  34.  
  35.             Blend SrcAlpha OneMinusSrcAlpha
  36.             Cull Off
  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.             // Change path if needed
  47.             #include "Assets/Cginc/ClassicNoise3D.cginc"
  48.  
  49.             struct v2f {
  50.                 float4 pos : SV_POSITION;
  51.                 float2 uv : TEXCOORD0;
  52.                 float3 worldPos : TEXCOORD1;
  53.                 float3 worldNormal : TEXCOORD2;
  54.             };
  55.  
  56.             v2f vert(appdata_base v)
  57.             {
  58.                 v2f o;
  59.                 // World position
  60.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  61.  
  62.                 // Clip position
  63.                 o.pos = mul(UNITY_MATRIX_VP, float4(o.worldPos, 1.));
  64.  
  65.                 // Normal in WorldSpace
  66.                 o.worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
  67.  
  68.                 o.uv = v.texcoord;
  69.  
  70.                 return o;
  71.             }
  72.  
  73.             sampler2D _MainTex;
  74.  
  75.             fixed4 _LightColor0;
  76.            
  77.             // Diffuse
  78.             fixed _Diffuse;
  79.             fixed4 _DifColor;
  80.  
  81.             //Specular
  82.             fixed _Shininess;
  83.             fixed4 _SpecColor;
  84.            
  85.             //Ambient
  86.             fixed _Ambient;
  87.             fixed4 _AmbColor;
  88.  
  89.             // Emission
  90.             sampler2D _EmissionTex;
  91.             fixed4 _EmiColor;
  92.             fixed _EmiVal;
  93.  
  94.             // Dissolution
  95.             fixed _DisVal;
  96.  
  97.             fixed4 frag(v2f i) : SV_Target
  98.             {
  99.                 fixed4 c = tex2D(_MainTex, i.uv);
  100.  
  101.                 // Light direction
  102.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  103.  
  104.                 // Camera direction
  105.                 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
  106.  
  107.                 float3 worldNormal = normalize(i.worldNormal);
  108.  
  109.                 // Compute ambient lighting
  110.                 fixed4 amb = _Ambient * _AmbColor;
  111.  
  112.                 // Compute the diffuse lighting
  113.                 fixed4 NdotL = max(0., dot(worldNormal, lightDir) * _LightColor0);
  114.                 fixed4 dif = NdotL * _Diffuse * _LightColor0 * _DifColor;
  115.  
  116.                 fixed4 light = dif + amb;
  117.  
  118.                 // Compute the specular lighting
  119.                 #if _SPEC_ON
  120.                 float3 refl = normalize(reflect(-lightDir, worldNormal));
  121.                 float RdotV = max(0., dot(refl, viewDir));
  122.                 fixed4 spec = pow(RdotV, _Shininess) * _LightColor0 * ceil(NdotL) * _SpecColor;
  123.  
  124.                 light += spec;
  125.                 #endif
  126.  
  127.                 c.rgb *= light.rgb;
  128.  
  129.                 // Compute emission
  130.                 fixed4 emi = tex2D(_EmissionTex, i.uv).r * _EmiColor * _EmiVal;
  131.                 c.rgb += emi.rgb;
  132.  
  133.                 if((cnoise(i.worldPos) + 1.) / 2. < _DisVal)
  134.                     discard;
  135.  
  136.                 return c;
  137.             }
  138.  
  139.             ENDCG
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement