Orihaus

Texture-Sampled Vertex Distortion - v0.5

Nov 18th, 2013
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.08 KB | None | 0 0
  1. Shader "Base_Artifact"
  2. {
  3.     Properties  
  4.     {
  5.         _DataTex ( "Data Artifact", 2D ) = "black" {}
  6.         _Rate ( "Artifact Rate", float ) = 2.0
  7.         _Screeny_rate ( "Screen Rate", float ) = 6.0
  8.         _WarpScale ( "Warp Scale", range( -2, 2 ) ) = 0.5
  9.         _WarpOffset ( "Warp Offset", range( 0, 0.5 ) ) = 0.5
  10.        
  11.         _FresnelEmitColor ( "Fresnel Emit Color", Color ) = ( 0.89, 0.945, 1.0, 0.0 )
  12.         _FresnelEmitPrimarySecondary ( "Primary/Secondary Emit Fresnel Degree", Range( 0.0, 1.0 ) ) = 1.0
  13.         _FresnelEmitPower ( "Fresnel Emit Power", Range( 0.0, 16.0 ) ) = 8.0
  14.    
  15.         _DiffuseAmount ( "Diffuse Amount", Range( 0.0, 1.0 ) ) = 0.25
  16.         _TexAmount ( "Texture Amount", Range( 0.0, 1.0 ) ) = 0.25
  17.         _SpecAmount ( "Specular Amount", Range( 0.0, 16.0 ) ) = 1.0
  18.         _SpecTexAmount ( "Spec Texture Amount", Range( 0.0, 1.0 ) ) = 0.25
  19.        
  20.         _Normalmap ( "Normalmap", 2D ) = "normal" {}
  21.         _Specmap ( "Specmap", 2D ) = "spec" {}
  22.        
  23.         _Gloss ( "Gloss", Range( 0.001, 1.0 ) ) = 1.0
  24.         _SRNI ( "Specular Reflectance at Normal Incidence", Range( 0.0, 1.0 ) ) = 0.075
  25.     }
  26.        
  27.     SubShader
  28.     {
  29.         Tags
  30.         {
  31.           "Queue"="Geometry+0"
  32.           "IgnoreProjector"="False"
  33.           "RenderType"="Opaque"
  34.         }
  35.  
  36.         Cull Back
  37.         ZWrite On
  38.         ZTest LEqual
  39.  
  40.         CGPROGRAM
  41.         #pragma target 3.0
  42.         #pragma surface surf SimpleSpecular vertex:vert novertexlights
  43.  
  44.         sampler2D _DataTex;
  45.         float4 _DataTex_ST;
  46.        
  47.         float _WarpScale;
  48.         float _WarpOffset;
  49.         float _Rate;
  50.         float _Screeny_rate;
  51.        
  52.         struct Input
  53.         {
  54.             float2 uv_Normalmap;
  55.             float2 uv_Specmap;
  56.             float2 uv_DataTex;
  57.            
  58.             float4 pos : POSITION;
  59.             float4 dataUV : TEXCOORD1;
  60.             float3 viewDir;
  61.         };
  62.  
  63.         void vert ( inout appdata_full v, out Input o )
  64.         {
  65.             float4 pos = mul( UNITY_MATRIX_MVP, v.vertex );
  66.             float2 screenuv = pos.xy / pos.w;
  67.             screenuv.y += _Time.x * _Screeny_rate;
  68.            
  69.             o.dataUV = float4( screenuv.x, screenuv.y, 0, 0 );
  70.             o.dataUV.xy = TRANSFORM_TEX( o.dataUV.xy, _DataTex );
  71.             float4 tex = ( tex2Dlod( _DataTex, o.dataUV ) * 0.5 ) + 0.0;
  72.            
  73.             float3 warp = float3(
  74.                 sin( v.normal.x*tex.r), //*v.vertex.x
  75.                 sin( v.normal.y*tex.g),
  76.                 sin( v.normal.z*tex.b)
  77.             );
  78.            
  79.             float dist = distance( v.vertex.xyz, v.vertex.xyz + warp );
  80.             v.vertex.xyz = lerp( v.vertex.xyz + warp * _WarpScale, v.vertex.xyz, dist );
  81.         }
  82.        
  83.         float _Gloss;
  84.         fixed _SpecAmount;
  85.         float _SRNI;
  86.        
  87.         float CalculateGGX( float alpha, float cosThetaM )
  88.         {
  89.             float roughness = alpha * alpha;
  90.             roughness *= roughness;
  91.            
  92.             float CosSquared = cosThetaM * cosThetaM;
  93.             float TanSquared = ( 1.0 - CosSquared ) + 1.0;
  94.             float GGX = ( CosSquared * ( roughness - 1.0 ) ) + 1.0;
  95.             GGX = 0.31831 * GGX * GGX;
  96.             return roughness / GGX;
  97.         }
  98.        
  99.         float CalculateGeometric( float n_dot_v, float roughness )
  100.         {
  101.             return n_dot_v / ( n_dot_v * ( 1.0 - roughness ) + roughness );
  102.         }
  103.        
  104.         float CalculateGeometricAtten( float n_dot_l, float n_dot_v, float roughness )
  105.         {
  106.             float roughnessRemap = roughness + 1.0;
  107.             roughnessRemap *= roughnessRemap;
  108.             roughnessRemap *= 0.125;
  109.            
  110.             float atten = CalculateGeometric( n_dot_v, roughnessRemap ) * CalculateGeometric( n_dot_l, roughnessRemap );
  111.             return atten;
  112.         }
  113.  
  114.         float CalculateSpecular( fixed3 lDir, fixed3 vDir, fixed3 norm, float lightSize, float gloss, float n_dot_l )
  115.         {    
  116.             float3 halfVector = normalize( lDir + vDir );
  117.        
  118.             float n_dot_v = saturate( dot( norm, vDir ) );
  119.             float h_dot_n = saturate( dot( halfVector, norm ) );
  120.             float h_dot_v = saturate( dot( halfVector, vDir ) );
  121.            
  122.             float fresnel = pow( 1.0 - h_dot_v, 5.0 );
  123.             fresnel = _SRNI + ( 1.0 - _SRNI ) * fresnel;
  124.  
  125.             float geoAtten = CalculateGeometricAtten( n_dot_l, h_dot_v, gloss );
  126.             float distribution = CalculateGGX( saturate( gloss + lightSize * _WorldSpaceLightPos0.w ), h_dot_n );
  127.            
  128.             float BRDF = distribution * fresnel * geoAtten * saturate( h_dot_v / ( h_dot_n * n_dot_v ) );
  129.             return BRDF;
  130.         }
  131.        
  132.         float3 AreaLight( float3 light, float3 reflection, float size )
  133.         {
  134.             float3 centerToRay = light - dot( light, reflection ) * reflection;
  135.             return light + centerToRay * saturate( size / centerToRay );
  136.         }
  137.        
  138.         struct SurfaceOutputMod
  139.         {
  140.             half3 Albedo;
  141.             half3 Normal;
  142.             half3 Emission;
  143.             half Specular;
  144.             half Gloss;
  145.             half Alpha;
  146.             float3 position;
  147.         };
  148.        
  149.         fixed4 LightingSimpleSpecular( SurfaceOutputMod s, fixed3 lightDir, fixed3 viewDir, fixed atten )
  150.         {
  151.             fixed lightSize = _LightColor0.w * 4.0;
  152.            
  153.             float n_dot_l = saturate( dot( s.Normal, lightDir ) );
  154.             fixed spec = CalculateSpecular( lightDir, viewDir, s.Normal, lightSize, s.Gloss, n_dot_l ) * 0.005 * _SpecAmount;
  155.             fixed3 diff = n_dot_l * s.Albedo * 0.31831;
  156.            
  157.             diff = lerp( diff, 0.0, spec );
  158.            
  159.             fixed4 c;
  160.             c.rgb = diff * _LightColor0.rgb;
  161.             c.rgb += _LightColor0.rgb * spec;
  162.             c.rgb *= atten * 2.0;
  163.             c.a = s.Alpha;
  164.            
  165.             return c;
  166.         }
  167.    
  168.         sampler2D _Normalmap;
  169.         sampler2D _Specmap;
  170.  
  171.         fixed _SpecTexAmount;
  172.         fixed _TexAmount;
  173.         fixed _DiffuseAmount;
  174.        
  175.             float4 _FresnelEmitColor;
  176.             float _FresnelEmitPower;
  177.             float _FresnelEmitPrimarySecondary;
  178.  
  179.         void surf( Input IN, inout SurfaceOutputMod o )
  180.         {
  181.             float spec = tex2D( _Specmap, IN.uv_Specmap ).r;
  182.            
  183.             o.Normal = UnpackNormal( tex2D( _Normalmap, IN.uv_Normalmap ) );
  184.            
  185.                 float fresnelDot = saturate( 1.0 - dot( normalize( IN.viewDir ), o.Normal ) );
  186.                 float emitPrimaryBlob = pow( fresnelDot, _FresnelEmitPower * 2.0 );
  187.                 float emitSecondaryBlob = pow( fresnelDot, _FresnelEmitPower );
  188.                 float emitFresnel = lerp( emitPrimaryBlob, emitSecondaryBlob, _FresnelEmitPrimarySecondary );
  189.                  o.Emission = emitFresnel * _FresnelEmitColor.rgb * ( _FresnelEmitColor.a * 16.0f ) * spec;
  190.  
  191.             o.Gloss = lerp( spec * 2.0, 0.5, _SpecTexAmount ) * _Gloss;
  192.             o.Albedo = _DiffuseAmount * lerp( spec, 1.0, _TexAmount );
  193.             o.position = IN.pos;
  194.         }
  195.    
  196.         ENDCG
  197.     }
  198.        
  199.     Fallback "Diffuse"
  200. }
Advertisement
Add Comment
Please, Sign In to add comment