outline.shader 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. Shader "Custom/outline"
  2. {
  3. Properties
  4. {
  5. _OutlineWidth("Outline Width",Float) = 7.0
  6. _OutlineZbias("Outline Zbias",Float) = -10
  7. _OutlineColor("Outline Color",Color) = (1,1,1,1)
  8. }
  9. SubShader
  10. {
  11. Tags { "RenderType"="Opaque" }
  12. LOD 200
  13. Pass
  14. {
  15. Cull Front
  16. CGPROGRAM
  17. #pragma vertex vert
  18. #pragma fragment frag
  19. #include "UnityCG.cginc"
  20. #include "AutoLight.cginc"
  21. struct appdata
  22. {
  23. float4 vertex : POSITION;
  24. float2 texcoord0 : TEXCOORD0;
  25. float3 normal : NORMAL;
  26. float4 color : COLOR;
  27. };
  28. struct v2f
  29. {
  30. float4 pos : SV_POSITION;
  31. float2 uv : TEXCOORD0;
  32. float4 vertex_color : TEXCOORD3;
  33. };
  34. float _OutlineWidth;
  35. float _OutlineZbias;
  36. float4 _OutlineColor;
  37. v2f vert(appdata v)
  38. {
  39. v2f o;
  40. float3 pos_view = UnityObjectToViewPos(v.vertex);
  41. float3 normal_world = UnityObjectToWorldNormal(v.normal);
  42. float3 outline_dir = normalize(mul((float3x3)UNITY_MATRIX_V, normal_world));
  43. outline_dir.z = _OutlineZbias * (1.0 - v.color.b);
  44. pos_view += outline_dir * _OutlineWidth * 0.001 * v.color.a;
  45. o.pos = mul(UNITY_MATRIX_P, float4(pos_view, 1.0));
  46. o.uv = v.texcoord0;
  47. o.vertex_color = v.color;
  48. return o;
  49. }
  50. half4 frag(v2f i) : SV_Target
  51. {
  52. half3 outlineColor = _OutlineColor.xyz;
  53. return float4(outlineColor, 1.0);
  54. }
  55. ENDCG
  56. }
  57. }
  58. FallBack "Diffuse"
  59. }