outlineShader.shader 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Outlined/Silhouetted Diffuse" {
  3. Properties{
  4. _Color("Main Color", Color) = (1,1,1,1)
  5. _OutlineColor("Outline Color", Color) = (1,0,0,1) //改变这个能改变轮廓边的颜色
  6. _Outline("Outline width", Range(0.0, 0.03)) = 0.0008 //改变这个能改变轮廓边的粗细
  7. _MainTex("Base (RGB)", 2D) = "white" { }
  8. }
  9. CGINCLUDE
  10. #include "UnityCG.cginc"
  11. struct appdata {
  12. float4 vertex : POSITION;
  13. float3 normal : NORMAL;
  14. };
  15. struct v2f {
  16. float4 pos : POSITION;
  17. float4 color : COLOR;
  18. };
  19. uniform float _Outline;
  20. uniform float4 _OutlineColor;
  21. v2f vert(appdata v) {
  22. // just make a copy of incoming vertex data but scaled according to normal direction
  23. v2f o;
  24. o.pos = UnityObjectToClipPos(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. ENDCG
  32. SubShader{
  33. Tags { "Queue" = "Transparent" }
  34. // note that a vertex shader is specified here but its using the one above
  35. Pass {
  36. Name "OUTLINE"
  37. Tags { "LightMode" = "Always" }
  38. Cull Off
  39. ZWrite Off
  40. ZTest Always
  41. ColorMask RGB // alpha not used
  42. // you can choose what kind of blending mode you want for the outline
  43. Blend SrcAlpha OneMinusSrcAlpha // Normal
  44. //Blend One One // Additive
  45. //Blend One OneMinusDstColor // Soft Additive
  46. //Blend DstColor Zero // Multiplicative
  47. //Blend DstColor SrcColor // 2x Multiplicative
  48. CGPROGRAM
  49. #pragma vertex vert
  50. #pragma fragment frag
  51. half4 frag(v2f i) :COLOR {
  52. return i.color;
  53. }
  54. ENDCG
  55. }
  56. Pass {
  57. Name "BASE"
  58. ZWrite On
  59. ZTest LEqual
  60. Blend SrcAlpha OneMinusSrcAlpha
  61. Material {
  62. Diffuse[_Color]
  63. Ambient[_Color]
  64. }
  65. Lighting On
  66. SetTexture[_MainTex] {
  67. ConstantColor[_Color]
  68. Combine texture * constant
  69. }
  70. SetTexture[_MainTex] {
  71. Combine previous * primary DOUBLE
  72. }
  73. }
  74. }
  75. SubShader{
  76. Tags { "Queue" = "Transparent" }
  77. Pass {
  78. Name "OUTLINE"
  79. Tags { "LightMode" = "Always" }
  80. Cull Front
  81. ZWrite Off
  82. ZTest Always
  83. ColorMask RGB
  84. // you can choose what kind of blending mode you want for the outline
  85. Blend SrcAlpha OneMinusSrcAlpha // Normal
  86. //Blend One One // Additive
  87. //Blend One OneMinusDstColor // Soft Additive
  88. //Blend DstColor Zero // Multiplicative
  89. //Blend DstColor SrcColor // 2x Multiplicative
  90. CGPROGRAM
  91. #pragma vertex vert
  92. #pragma exclude_renderers gles xbox360 ps3
  93. ENDCG
  94. SetTexture[_MainTex] { combine primary }
  95. }
  96. Pass {
  97. Name "BASE"
  98. ZWrite On
  99. ZTest LEqual
  100. Blend SrcAlpha OneMinusSrcAlpha
  101. Material {
  102. Diffuse[_Color]
  103. Ambient[_Color]
  104. }
  105. Lighting On
  106. SetTexture[_MainTex] {
  107. ConstantColor[_Color]
  108. Combine texture * constant
  109. }
  110. SetTexture[_MainTex] {
  111. Combine previous * primary DOUBLE
  112. }
  113. }
  114. }
  115. }