VerticesOutline_Always.shader 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. Shader "ModelEffect/VerticsOutline_Always"
  2. {
  3. Properties {
  4. _MainTex("Base (RGB)", 2D) = "white" {}
  5. _OutlineFactor("Outline Factor", Range(0, 1)) = 0.5
  6. _OutlineColor("Outline Color", Color) = (1,1,1,1)
  7. _OutlineWidth("Outline Width", Range(0, 10)) = 0.002
  8. _BodyAlpha("Body Alpha", Range(0, 1)) = 1
  9. _Stencil("Stencil ID", Int) = 16
  10. [HideInInspector] _StencilWriteMask("Stencil Write Mask", Float) = 255
  11. [HideInInspector] _StencilReadMask("Stencil Read Mask", Float) = 255
  12. }
  13. SubShader {
  14. Tags {
  15. "RenderType" = "Transparent"
  16. "Queue" = "Transparent"
  17. "IgnoreProjector" = "True"
  18. }
  19. LOD 200
  20. Pass {
  21. Name "VerticsOutline_Outline_Stencil"
  22. Cull Off
  23. ZWrite Off
  24. ZTest Always
  25. ColorMask 0
  26. Stencil {
  27. Ref [_Stencil]
  28. Comp Always
  29. Pass Replace
  30. ZFail Replace
  31. ReadMask [_StencilReadMask]
  32. WriteMask [_StencilWriteMask]
  33. }
  34. CGPROGRAM
  35. #pragma vertex vert
  36. #pragma fragment frag
  37. #pragma target 2.0
  38. #pragma multi_compile_fog
  39. #include "UnityCG.cginc"
  40. struct appdata_t {
  41. float4 vertex : POSITION;
  42. float2 texcoord : TEXCOORD0;
  43. float3 normal : NORMAL;
  44. UNITY_VERTEX_INPUT_INSTANCE_ID
  45. };
  46. struct v2f {
  47. float4 vertex : SV_POSITION;
  48. float2 texcoord : TEXCOORD0;
  49. UNITY_FOG_COORDS(1)
  50. UNITY_VERTEX_OUTPUT_STEREO
  51. };
  52. sampler2D _MainTex;
  53. float4 _MainTex_ST;
  54. v2f vert(appdata_t v)
  55. {
  56. v2f o;
  57. UNITY_SETUP_INSTANCE_ID(v);
  58. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  59. o.vertex = UnityObjectToClipPos(v.vertex);
  60. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  61. UNITY_TRANSFER_FOG(o, o.vertex);
  62. return o;
  63. }
  64. fixed4 frag(v2f i) : SV_Target
  65. {
  66. fixed4 col = tex2D(_MainTex, i.texcoord);
  67. UNITY_APPLY_FOG(i.fogCoord, col);
  68. UNITY_OPAQUE_ALPHA(col.a);
  69. return col;
  70. }
  71. ENDCG
  72. }
  73. Pass {
  74. Name "VerticsOutline_Outline_Face1"
  75. Cull Off
  76. ZWrite On
  77. ZTest Always
  78. ColorMask RGBA
  79. Stencil {
  80. Ref [_Stencil]
  81. Comp NotEqual
  82. Pass Keep
  83. ZFail Keep
  84. ReadMask[_StencilReadMask]
  85. WriteMask[_StencilWriteMask]
  86. }
  87. CGPROGRAM
  88. #pragma vertex vert
  89. #pragma fragment frag
  90. #pragma target 2.0
  91. #pragma multi_compile_fog
  92. #include "UnityCG.cginc"
  93. struct appdata_t {
  94. float4 vertex : POSITION;
  95. float2 texcoord : TEXCOORD0;
  96. float3 normal : NORMAL;
  97. UNITY_VERTEX_INPUT_INSTANCE_ID
  98. };
  99. struct v2f {
  100. float4 vertex : SV_POSITION;
  101. float2 texcoord : TEXCOORD0;
  102. UNITY_FOG_COORDS(1)
  103. UNITY_VERTEX_OUTPUT_STEREO
  104. };
  105. sampler2D _MainTex;
  106. float4 _MainTex_ST;
  107. uniform fixed4 _OutlineColor;
  108. uniform float _OutlineWidth;
  109. uniform float _OutlineFactor;
  110. v2f vert(appdata_t v)
  111. {
  112. v2f o;
  113. UNITY_SETUP_INSTANCE_ID(v);
  114. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  115. float3 caculateVec = lerp(normalize(v.vertex.xyz), normalize(v.normal), _OutlineFactor);
  116. o.vertex = UnityObjectToClipPos(v.vertex);
  117. float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, caculateVec);
  118. float2 offset = TransformViewToProjection(norm.xy);
  119. o.vertex.xy += offset * o.vertex.z * _OutlineWidth;
  120. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  121. UNITY_TRANSFER_FOG(o,o.vertex);
  122. return o;
  123. }
  124. fixed4 frag(v2f i) : SV_Target
  125. {
  126. fixed4 col = _OutlineColor;
  127. UNITY_APPLY_FOG(i.fogCoord, col);
  128. UNITY_OPAQUE_ALPHA(col.a);
  129. return col;
  130. }
  131. ENDCG
  132. }
  133. Pass {
  134. Name "VerticsOutline_Outline_Face2"
  135. Cull Off
  136. ZWrite On
  137. ZTest Always
  138. Blend SrcAlpha OneMinusSrcAlpha
  139. Stencil {
  140. Ref [_Stencil]
  141. Comp NotEqual
  142. Pass Keep
  143. ZFail Keep
  144. ReadMask[_StencilReadMask]
  145. WriteMask[_StencilWriteMask]
  146. }
  147. CGPROGRAM
  148. #pragma vertex vert
  149. #pragma fragment frag
  150. #pragma target 2.0
  151. #pragma multi_compile_fog
  152. #include "UnityCG.cginc"
  153. struct appdata_t {
  154. float4 vertex : POSITION;
  155. float2 texcoord : TEXCOORD0;
  156. float3 normal : NORMAL;
  157. UNITY_VERTEX_INPUT_INSTANCE_ID
  158. };
  159. struct v2f {
  160. float4 vertex : SV_POSITION;
  161. float2 texcoord : TEXCOORD0;
  162. UNITY_FOG_COORDS(1)
  163. UNITY_VERTEX_OUTPUT_STEREO
  164. };
  165. sampler2D _MainTex;
  166. float4 _MainTex_ST;
  167. uniform fixed4 _OutlineColor;
  168. uniform float _OutlineWidth;
  169. uniform float _OutlineFactor;
  170. v2f vert(appdata_t v)
  171. {
  172. v2f o;
  173. UNITY_SETUP_INSTANCE_ID(v);
  174. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  175. float3 caculateVec = -lerp(normalize(v.vertex.xyz), normalize(v.normal), _OutlineFactor);
  176. o.vertex = UnityObjectToClipPos(v.vertex);
  177. float3 norm = mul((float3x3)UNITY_MATRIX_IT_MV, caculateVec);
  178. float2 offset = TransformViewToProjection(norm.xy);
  179. o.vertex.xy += offset * o.vertex.z * _OutlineWidth;
  180. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  181. UNITY_TRANSFER_FOG(o,o.vertex);
  182. return o;
  183. }
  184. fixed4 frag(v2f i) : SV_Target
  185. {
  186. fixed4 col = _OutlineColor;
  187. UNITY_APPLY_FOG(i.fogCoord, col);
  188. UNITY_OPAQUE_ALPHA(col.a);
  189. return col;
  190. }
  191. ENDCG
  192. }
  193. Pass {
  194. Name "VerticsOutline_Body"
  195. Cull Back
  196. ZWrite On
  197. Blend SrcAlpha OneMinusSrcAlpha
  198. ColorMask RGBA
  199. Stencil {
  200. Ref[_Stencil]
  201. Comp Always
  202. Pass Replace
  203. ZFail Replace
  204. ReadMask[_StencilReadMask]
  205. WriteMask[_StencilWriteMask]
  206. }
  207. CGPROGRAM
  208. #pragma vertex vert
  209. #pragma fragment frag
  210. #pragma target 2.0
  211. #pragma multi_compile_fog
  212. #include "UnityCG.cginc"
  213. struct appdata_t {
  214. float4 vertex : POSITION;
  215. float2 texcoord : TEXCOORD0;
  216. UNITY_VERTEX_INPUT_INSTANCE_ID
  217. };
  218. struct v2f {
  219. float4 vertex : SV_POSITION;
  220. float2 texcoord : TEXCOORD0;
  221. UNITY_FOG_COORDS(1)
  222. UNITY_VERTEX_OUTPUT_STEREO
  223. };
  224. sampler2D _MainTex;
  225. float4 _MainTex_ST;
  226. uniform float _BodyAlpha;
  227. v2f vert(appdata_t v)
  228. {
  229. v2f o;
  230. UNITY_SETUP_INSTANCE_ID(v);
  231. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  232. o.vertex = UnityObjectToClipPos(v.vertex);
  233. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  234. UNITY_TRANSFER_FOG(o,o.vertex);
  235. return o;
  236. }
  237. fixed4 frag(v2f i) : SV_Target
  238. {
  239. fixed4 col = tex2D(_MainTex, i.texcoord);
  240. UNITY_APPLY_FOG(i.fogCoord, col);
  241. UNITY_OPAQUE_ALPHA(col.a);
  242. return fixed4(col.rgb, _BodyAlpha);
  243. }
  244. ENDCG
  245. }
  246. }
  247. }