Ouline.shader 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 
  2. Shader "Unlit/Outline"
  3. {
  4. Properties{
  5. _Color("Main Tint", Color) = (1, 1, 1, 1)
  6. _MainTex("Texture", 2D) = "white" {}
  7. _OutlineWidth("OutlineWidth", Range(0, 1)) = 0.1
  8. _OutlineColor("OutlineColor", Color) = (1, 1, 1, 1)
  9. _AlphaBlend("AlphaBlend", Range(0, 1)) = 0.8 // 用于在透明纹理的基础上控制整体的透明度
  10. _AlphaTest("AlphaTest", Range(0, 1)) = 0.5
  11. }
  12. SubShader
  13. {
  14. // RenderType标签可以让Unity把这个Shader归入到提前定义的组(Transparent)用于指明该Shader是一个使用了透明度混合的Shader
  15. // IgnoreProjector=True这意味着该Shader不会受到投影器(Projectors)的影响
  16. // 为了使用透明度混合的Shader一般都应该在SubShader中设置这三个标签
  17. Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
  18. //第一个Pass,开启深度写入,不输出颜色,处理AlphaTest部分
  19. Pass{
  20. // 仅仅是把模型的深度信息写入深度缓冲中从而剔除模型中被自身遮挡的片元
  21. ZWrite On
  22. // 用于设置颜色通道的写掩码(Wirte Mask)ColorMask RGB|A|0|(R/G/B/A组合)
  23. // 当为0时意味着该Pass不写入任何颜色通道,也就不会输出任何颜色
  24. //也可以不用,这样就能刻意控制AlphaTest导致的发丝边缘的颜色
  25. ColorMask 0
  26. CGPROGRAM
  27. #pragma vertex vert
  28. #pragma fragment frag
  29. #include "Lighting.cginc"
  30. sampler2D _MainTex;
  31. float4 _MainTex_ST;
  32. fixed _AlphaTest;
  33. struct a2v {
  34. float4 vertex : POSITION;
  35. float4 texcoord : TEXCOORD0;
  36. };
  37. struct v2f {
  38. float4 pos : SV_POSITION;
  39. float2 uv : TEXCOORD2;
  40. };
  41. v2f vert(a2v v) {
  42. v2f o;
  43. o.pos = UnityObjectToClipPos(v.vertex);
  44. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  45. return o;
  46. }
  47. fixed4 frag(v2f i) : SV_Target{
  48. fixed4 texColor = tex2D(_MainTex, i.uv);
  49. clip(texColor.a - _AlphaTest); //进行AlphaTest //clip函数,但参数为负数则舍弃该片元输出
  50. return fixed4(0,0,0,1);
  51. }
  52. ENDCG
  53. }
  54. //第二个Pass,关闭深度写入,处理AlphaBlend半透明,处理光照等部分
  55. Pass{
  56. // 向前渲染路径的方式
  57. Tags{ "LightMode" = "ForwardBase" }
  58. Cull Off //关闭剔除,让头发双面显示
  59. ZWrite Off //关闭深度写入
  60. //透明度混合需要关闭深度写入
  61. //Orgb = SrcAlpha * Srgb + OneMinusSrcAlpha * Drgb
  62. //Oa = SrcAlpha * Sa + OneMinusSrcAlpha * Da
  63. //将本Shader计算出的颜色值(源颜色值) * 源Alpha值 + 目标颜色值(可以理解为背景色) * (1-源Alpha值),从而让源物体展示出了(1-alpha)的透明度。
  64. Blend SrcAlpha OneMinusSrcAlpha
  65. CGPROGRAM
  66. #pragma vertex vert
  67. #pragma fragment frag
  68. #include "Lighting.cginc"
  69. fixed4 _Color;
  70. sampler2D _MainTex;
  71. float4 _MainTex_ST;
  72. fixed _AlphaBlend;
  73. struct a2v {
  74. float4 vertex : POSITION;
  75. float3 normal : NORMAL;
  76. float4 texcoord : TEXCOORD0;
  77. };
  78. struct v2f {
  79. float4 pos : SV_POSITION;
  80. float3 worldNormal : TEXCOORD0;
  81. float3 worldPos : TEXCOORD1;
  82. float2 uv : TEXCOORD2;
  83. };
  84. v2f vert(a2v v) {
  85. v2f o;
  86. o.pos = UnityObjectToClipPos(v.vertex);
  87. o.worldNormal = UnityObjectToWorldNormal(v.normal);
  88. o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  89. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  90. return o;
  91. }
  92. fixed4 frag(v2f i) : SV_Target{
  93. fixed3 worldNormal = normalize(i.worldNormal);
  94. fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
  95. fixed4 texColor = tex2D(_MainTex, i.uv);
  96. fixed3 albedo = texColor.rgb * _Color;
  97. fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
  98. fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(worldNormal, worldLightDir));
  99. // 设置透明度通道的值
  100. return fixed4(ambient + diffuse, texColor.a * _AlphaBlend);
  101. }
  102. ENDCG
  103. }
  104. //第三个Pass,处理描边
  105. Pass{
  106. Cull Front //剔除正面
  107. CGPROGRAM
  108. #pragma vertex vert
  109. #pragma fragment frag
  110. #include "UnityCG.cginc"
  111. sampler2D _MainTex;
  112. float4 _MainTex_ST;
  113. fixed _AlphaTest;
  114. float _OutlineWidth;
  115. float4 _OutlineColor;
  116. struct a2v
  117. {
  118. float4 vertex : POSITION;
  119. float4 normal : NORMAL;
  120. float4 texcoord : TEXCOORD0;
  121. };
  122. struct v2f
  123. {
  124. float4 pos : SV_POSITION;
  125. float2 uv : TEXCOORD2;
  126. };
  127. v2f vert(a2v v)
  128. {
  129. v2f o;
  130. //o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  131. //float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
  132. //float2 offset = TransformViewToProjection(normal.xy);
  133. //o.pos.xy += offset *o.pos.z * _OutlineWidth;
  134. //方法二:
  135. //o.pos = UnityObjectToClipPos(v.vertex);
  136. //float3 normal = UnityObjectToWorldNormal(v.normal);
  137. //normal = mul(UNITY_MATRIX_VP, normal);
  138. //o.pos.xyz += normal * _Outline;
  139. float4 pos = mul(UNITY_MATRIX_MV, v.vertex); // viewPos,顶点变换到视角空间
  140. float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal); // viewNormal,UNITY_MATRIX_IT_MV这个针对方向向量,法线变换
  141. normal.z = -0.5; // viewNormal 对顶点法线的z分量进行处理,使他们等于一个定值
  142. float4 newNoraml = float4(normalize(normal), 0); //归一化后的noraml
  143. pos = pos + newNoraml * _OutlineWidth; // viewPos 沿法线方向对顶点进行扩大
  144. o.pos = mul(UNITY_MATRIX_P, pos);
  145. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  146. return o;
  147. }
  148. fixed4 frag(v2f i) : SV_Target
  149. {
  150. fixed4 texColor = tex2D(_MainTex, i.uv);
  151. //AlphaTest为了让描边也能根据透贴部分来勾边,也可以不加
  152. clip(texColor.a - _AlphaTest);
  153. return _OutlineColor;
  154. }
  155. ENDCG
  156. }
  157. }
  158. FallBack "Diffuse"
  159. }