模型描边

作者:追风剑情 发布于:2018-12-29 18:53 分类:Shader

示例

  1. Shader "Custom/NormalTest"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. _Cutoff ("Cut", Range(0,1)) = 0.5
  7. }
  8. SubShader
  9. {
  10. LOD 100
  11.  
  12. Pass
  13. {
  14. Tags { "RenderType"="Transparent" }
  15.  
  16. ZWrite On
  17. ZTest LEqual
  18.  
  19. CGPROGRAM
  20. #pragma vertex vert
  21. #pragma fragment frag
  22. #include "UnityCG.cginc"
  23.  
  24. struct appdata
  25. {
  26. float4 vertex : POSITION;
  27. float2 uv : TEXCOORD0;
  28. };
  29.  
  30. struct v2f
  31. {
  32. float2 uv : TEXCOORD0;
  33. float4 vertex : SV_POSITION;
  34. };
  35.  
  36. sampler2D _MainTex;
  37. float4 _MainTex_ST;
  38. v2f vert (appdata v)
  39. {
  40. v2f o;
  41. o.vertex = UnityObjectToClipPos(v.vertex);
  42. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  43. return o;
  44. }
  45. fixed4 frag (v2f i) : SV_Target
  46. {
  47. fixed4 col = tex2D(_MainTex, i.uv);
  48. return col;
  49. }
  50. ENDCG
  51. }
  52.  
  53. Pass
  54. {
  55. Tags { "RenderType"="Transparent" }
  56.  
  57. Blend SrcAlpha OneMinusSrcAlpha
  58. ZWrite On
  59. ZTest Greater
  60.  
  61. CGPROGRAM
  62. #pragma vertex vert
  63. #pragma fragment frag
  64. #include "UnityCG.cginc"
  65.  
  66. struct appdata
  67. {
  68. float4 vertex : POSITION;
  69. //模型空间法线
  70. float3 normal : NORMAL;
  71. float2 uv : TEXCOORD0;
  72. };
  73.  
  74. struct v2f
  75. {
  76. float2 uv : TEXCOORD0;
  77. float4 vertex : SV_POSITION;
  78. //世界空间法线
  79. float3 worldNormal : TEXCOORD1;
  80. //世界空间坐标
  81. float3 worldPosition : TEXCOORD2;
  82. };
  83.  
  84. sampler2D _MainTex;
  85. float4 _MainTex_ST;
  86. fixed _Cutoff;
  87. v2f vert (appdata v)
  88. {
  89. v2f o;
  90. o.vertex = UnityObjectToClipPos(v.vertex);
  91. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  92. o.worldNormal = UnityObjectToWorldNormal(v.normal);
  93. o.worldPosition = mul(unity_ObjectToWorld, v.vertex).xyz;
  94. return o;
  95. }
  96. fixed4 frag (v2f i) : SV_Target
  97. {
  98. fixed3 worldNormal = normalize(i.worldNormal);
  99. fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPosition));
  100. fixed v = saturate(1-dot(viewDir, worldNormal));
  101. clip(v - _Cutoff);
  102. return fixed4(1, 0, 0, v);
  103. }
  104. ENDCG
  105. }
  106. }
  107. }

效果

1111.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号