Advertisement
Guest User

SmoothOutline.shader

a guest
Oct 23rd, 2017
9,893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. Shader "Toon/Smooth Outline" {
  2. Properties{
  3. _Color("Main Color", Color) = (.5,.5,.5,1)
  4. _OutlineColor("Outline Color", Color) = (0,0,0,1)
  5. _Outline("Outline width", Range(.002, 0.2)) = .005
  6. _OutlineZ("Outline Z", Range(-.002, 0)) = -.001// outline z offset
  7. _MainTex("Base (RGB)", 2D) = "white" { }
  8. _Offset("Outline Noise Offset", Range(0.5, 10)) = .005// noise offset
  9. _NoiseTex("Noise (RGB)", 2D) = "white" { }// noise texture
  10. _Ramp("Toon Ramp (RGB)", 2D) = "gray" {}
  11.  
  12. [Toggle(NOISE)] _NOISE("Enable Noise?", Float) = 0
  13. }
  14.  
  15. CGINCLUDE
  16. #include "UnityCG.cginc"
  17. #pragma shader_feature NOISE
  18. struct appdata {
  19. float4 vertex : POSITION;
  20. float3 normal : NORMAL;
  21. float4 texcoord : TEXCOORD0;// texture coordinates
  22. };
  23.  
  24. struct v2f {
  25. float4 pos : SV_POSITION;
  26. UNITY_FOG_COORDS(0)
  27. fixed4 color : COLOR;
  28. };
  29.  
  30. uniform float _Outline;
  31. uniform float _OutlineZ;// outline z offset
  32. uniform float4 _OutlineColor;
  33. sampler2D _NoiseTex;// noise texture
  34. float _Offset; // noise offset
  35.  
  36. v2f vert(appdata v) {
  37. v2f o;
  38. o.pos = UnityObjectToClipPos(v.vertex);
  39.  
  40. float3 norm = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normal));
  41. float2 offset = TransformViewToProjection(norm.xy);
  42.  
  43. float4 tex = tex2Dlod(_NoiseTex, float4(v.texcoord.xy, 0, 0) * _Offset);// noise texture based on texture coordinates and offset
  44.  
  45. #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE //to handle recent standard asset package on older version of unity (before 5.5)
  46. #if NOISE // switch for noise
  47. o.pos.xy += offset * _Outline * (tex.r);// add noise
  48. #else
  49. o.pos.xy += offset * _Outline;// or not
  50. #endif
  51. o.pos.z += _OutlineZ;// push away from camera
  52. #else
  53. o.pos.xy += offset * o.pos.z * _Outline;
  54. #endif
  55. o.color = _OutlineColor;
  56. UNITY_TRANSFER_FOG(o, o.pos);
  57. return o;
  58. }
  59. ENDCG
  60.  
  61. SubShader{
  62. Tags{ "RenderType" = "Opaque" }
  63. UsePass "Toon/Lit/FORWARD"
  64. Pass{
  65. Name "OUTLINE"
  66. Tags{ "LightMode" = "Always" }
  67. Cull Off// we dont want to cull
  68. ZWrite On
  69. ColorMask RGB
  70.  
  71.  
  72. CGPROGRAM
  73. #pragma vertex vert
  74. #pragma fragment frag
  75. #pragma multi_compile_fog
  76. fixed4 frag(v2f i) : SV_Target
  77. {
  78. UNITY_APPLY_FOG(i.fogCoord, i.color);
  79. return i.color;
  80. }
  81. ENDCG
  82. }
  83. }
  84.  
  85. Fallback "Toon/Basic"
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement