Advertisement
ensiferum888

TestSnowOutline Shader

Jul 19th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. Shader "Custom/TestOulineSnow" {
  2.     Properties {
  3.         _Color ("Main Color", Color) = (1,1,1,1)
  4.         _OutlineColor ("Outline Color", Color) = (0.8,0.5,0,0.8)
  5.         _Outline ("Outline width", Range (.002, 0.03)) = .003
  6.         _MainTex ("Texture", 2D) = "white" { }
  7.         _BumpMap ("Bumpmap", 2D) = "bump" {}
  8.     }
  9.     CGINCLUDE
  10.    
  11.      #include "UnityCG.cginc"
  12.     struct appdata {
  13.         float4 vertex : POSITION;
  14.         float3 normal : NORMAL;
  15.     };
  16.     struct v2f {
  17.         float4 pos : POSITION;
  18.         float4 color : COLOR;
  19.     };
  20.     uniform float _Outline;
  21.     uniform float4 _OutlineColor;
  22.     v2f vert(appdata v) {
  23.         v2f o;
  24.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  25.         float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
  26.         float2 offset = TransformViewToProjection(norm.xy);
  27.          o.pos.xy += offset * o.pos.z * _Outline;
  28.         o.color = _OutlineColor;
  29.         return o;
  30.     }
  31.  
  32.     ENDCG
  33.     SubShader {
  34.         Tags { "Queue"="AlphaTest"}
  35.         //UsePass "Diffuse/BASE"
  36.           Pass {
  37.             Name "OUTLINE"
  38.             Tags { "LightMode" = "Always" }
  39.             Cull off
  40.             ZWrite Off
  41.             ColorMask RGB
  42.             Blend SrcAlpha OneMinusSrcAlpha
  43.             CGPROGRAM
  44.             #pragma vertex vert
  45.             #pragma fragment frag          
  46.             half4 frag(v2f i) :COLOR { return i.color; }
  47.             ENDCG
  48.        }
  49.            
  50.  
  51.             Tags { "RenderType"="Opaque" }
  52.             LOD 300
  53.             CGPROGRAM
  54.             #pragma surface surf Lambert vertex:snow addshadow
  55.             #pragma exclude_renderers flash
  56.             //#pragma target 3.0
  57.            
  58.            
  59.             sampler2D _MainTex;
  60.             sampler2D _SnowTex;
  61.             sampler2D _BumpMap;
  62.             fixed4 _Color;
  63.            
  64.             float _snowShininess;
  65.             float _SnowAmount;
  66.             float _SnowStartHeight;
  67.             sampler2D _SnowTexture;
  68.            
  69.             struct Input {
  70.                 float2 uv_MainTex;
  71.                 float2 uv_BumpMap;
  72.                 fixed4 color : COLOR;
  73.                 float3 worldPos;
  74.                 fixed3 MyWorldNormal;
  75.             };
  76.            
  77.            
  78.             void snow (inout appdata_full v, out Input o) {
  79.                 o.MyWorldNormal = normalize(mul((float3x3)_Object2World, v.normal));
  80.             }
  81.            
  82.            
  83.             void surf (Input IN, inout SurfaceOutput o) {
  84.            
  85.                 fixed4 col = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  86.                 fixed4 snow = tex2D(_SnowTex, IN.uv_MainTex);
  87.                 o.Alpha = col.a;
  88.                 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
  89.                
  90.                 // get snow texture // distribution might be stored in alpha
  91.                 half4 snowtex = tex2D( _SnowTexture, IN.uv_MainTex);
  92.                
  93.                 // clamp(IN.MyWorldNormal.y + 0.6, 0, 1) = allows snow even on orthogonal surfaces // (1-col.b) = take the blue channel to get some kind of heightmap
  94.                 float snowAmount = (_SnowAmount * (clamp(IN.MyWorldNormal.y + 0.6, 0, 1)) * (1-col.b) *.8 + clamp(o.Normal.y, 0, 1) * _SnowAmount * .25);
  95.            
  96.                 // clamp snow to _SnowStartHeight
  97.                 snowAmount = snowAmount * clamp((IN.worldPos.y - _SnowStartHeight)*.0125, 0, 1);
  98.                
  99.                 // sharpen snow mask
  100.                 snowAmount = clamp(pow(snowAmount,6)*256, 0, 1);
  101.                
  102.                 o.Alpha = 0.0; 
  103.            
  104.                 // mix
  105.                 o.Albedo = col.rgb * (1-snowAmount) + snowtex.rgb*snowAmount;
  106.                 o.Gloss = half(snowAmount*(1-snowtex.rgb));
  107.                 o.Specular = _snowShininess;
  108.                
  109.                 // smooth normal
  110.                 o.Normal = normalize(lerp(o.Normal, float3(0,0,1), snowAmount*.50));
  111.                
  112.             }
  113.             ENDCG  
  114.    }
  115.     FallBack "Diffuse"
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement