---
title: "unity3d实现描边shader"
author: "wgcode"
date: "2015-05-14T09:47:06Z"
tags: []
categories: ["移动开发"]
slug: "unity3d-shader"
draft: false
lastmod: "2015-05-14T09:47:06Z"
id: 2210795
url: "https://tts.iteye.com/blog/2210795"
---

**\[plain]** [view plain](http://blog.csdn.net/awnuxcvbn/article/details/26572249 "view plain")[copy](http://blog.csdn.net/awnuxcvbn/article/details/26572249 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/357914 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/357914/fork "派生到我的代码片")

 

01. Shader "Custom/OutLine" {Properties {
02. \_Color ("Main Color", Color) = (.5,.5,.5,1)
03. \_OutlineColor ("Outline Color", Color) = (0,0,0,1)
04. \_Outline ("Outline width", Range (0.0, 0.03)) = .005
05. \_MainTex ("Base (RGB)", 2D) = "white" { }
06. }
07. CGINCLUDE
08. #include "UnityCG.cginc"
09. struct appdata {
10. float4 vertex : POSITION;
11. float3 normal : NORMAL;
12. };
13. struct v2f {
14. float4 pos : POSITION;
15. float4 color : COLOR;
16. };
17. uniform float \_Outline;
18. uniform float4 \_OutlineColor;
19. v2f vert(appdata v) {
20. v2f o;
21. o.pos = mul(UNITY\_MATRIX\_MVP, v.vertex);
22. float3 norm   = mul ((float3x3)UNITY\_MATRIX\_IT\_MV, v.normal);
23. float2 offset = TransformViewToProjection(norm.xy);
24. o.pos.xy += offset * o.pos.z * \_Outline;
25. o.color = \_OutlineColor;
26. return o;
27. }
28. ENDCG
29. SubShader {
30. Tags { "Queue" = "Transparent" }
31. Pass {
32. Tags { "LightMode" = "Always" }
33. Cull Off
34. ZWrite Off
35. //ZTest Always//始终通过深度测试，即可以渲染
36. //ColorMask RGB // alpha not used
37. Blend SrcAlpha OneMinusSrcAlpha // Normal
38. CGPROGRAM
39. #pragma vertex vert
40. #pragma fragment frag
41. half4 frag(v2f i) :COLOR {
42. return i.color;
43. }
44. ENDCG
45. }
46. Pass {
47. Name "BASE"
48. ZWrite On
49. ZTest LEqual
50. Blend SrcAlpha OneMinusSrcAlpha
51. Material {
52. Diffuse \[\_Color]
53. Ambient \[\_Color]
54. }
55. Lighting On
56. SetTexture \[\_MainTex] {
57. ConstantColor \[\_Color]
58. Combine texture * constant
59. }
60. SetTexture \[\_MainTex] {
61. Combine previous * primary DOUBLE
62. }
63. }
64. }
65. Fallback "Diffuse"
66. }

![](http://img.blog.csdn.net/20140522113757500?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvQVdOVVhDVkJO%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA=%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)

[](http://blog.csdn.net/shengfanger/article/details/8572774)