OutlineFill.shader 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // OutlineFill.shader
  3. // QuickOutline
  4. //
  5. // Created by Chris Nolet on 2/21/18.
  6. // Copyright © 2018 Chris Nolet. All rights reserved.
  7. //
  8. Shader "Custom/Outline Fill" {
  9. Properties {
  10. [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0
  11. _OutlineColor("Outline Color", Color) = (1, 1, 1, 1)
  12. _OutlineWidth("Outline Width", Range(0, 10)) = 2
  13. }
  14. SubShader {
  15. Tags {
  16. "Queue" = "Transparent+110"
  17. "RenderType" = "Transparent"
  18. "DisableBatching" = "True"
  19. }
  20. Pass {
  21. Name "Fill"
  22. Cull Off
  23. ZTest [_ZTest]
  24. ZWrite Off
  25. Blend SrcAlpha OneMinusSrcAlpha
  26. ColorMask RGB
  27. Stencil {
  28. Ref 1
  29. Comp NotEqual
  30. }
  31. CGPROGRAM
  32. #include "UnityCG.cginc"
  33. #pragma vertex vert
  34. #pragma fragment frag
  35. struct appdata {
  36. float4 vertex : POSITION;
  37. float3 normal : NORMAL;
  38. float3 smoothNormal : TEXCOORD3;
  39. UNITY_VERTEX_INPUT_INSTANCE_ID
  40. };
  41. struct v2f {
  42. float4 position : SV_POSITION;
  43. fixed4 color : COLOR;
  44. UNITY_VERTEX_OUTPUT_STEREO
  45. };
  46. uniform fixed4 _OutlineColor;
  47. uniform float _OutlineWidth;
  48. v2f vert(appdata input) {
  49. v2f output;
  50. UNITY_SETUP_INSTANCE_ID(input);
  51. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  52. float3 normal = any(input.smoothNormal) ? input.smoothNormal : input.normal;
  53. float3 viewPosition = UnityObjectToViewPos(input.vertex);
  54. float3 viewNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, normal));
  55. output.position = UnityViewToClipPos(viewPosition + viewNormal * -viewPosition.z * _OutlineWidth / 1000.0);
  56. output.color = _OutlineColor;
  57. return output;
  58. }
  59. fixed4 frag(v2f input) : SV_Target {
  60. return input.color;
  61. }
  62. ENDCG
  63. }
  64. }
  65. }