Unity卡通渲染之描边处理

做游戏时候可能经常需要对物体轮廓进行描边处理,使用该Shader即可,但可能有个缺点,正方体类型的描边并不会准确。


效果图如下:


Unity卡通渲染之描边处理_第1张图片


//Unity描边处理
//@author mingming [email protected]
//@time 2017-05-10 18:06:28
Shader "Toon Shader/Toon Outline" {
	Properties {
		_Color ("Color Tint", Color) = (1, 1, 1, 1)
		_MainTex ("Main Tex", 2D) = "white" {}
		_Outline ("Outline", Range(0, 1)) = 0.1
		_OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
	}
    SubShader {
		Tags { "RenderType"="Opaque" "Queue"="Geometry"}
		//

		Pass {
			NAME "OUTLINE"
			
			Cull Front
			
			CGPROGRAM
			
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"
			
			float _Outline;
			fixed4 _OutlineColor;
			
			struct a2v {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			}; 
			
			struct v2f {
			    float4 pos : SV_POSITION;
			};
			
			v2f vert (a2v v) {
				v2f o;
				
				float4 pos = mul(UNITY_MATRIX_MV, v.vertex); 
				float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);  
				normal.z = -0.5;
				pos = pos + float4(normalize(normal), 0) * _Outline;
				o.pos = mul(UNITY_MATRIX_P, pos);
				
				return o;
			}
			
			float4 frag(v2f i) : SV_Target { 
				return float4(_OutlineColor.rgb, 1);               
			}
			
			ENDCG
		}
		
		Pass {
			Tags { "LightMode"="ForwardBase" }
			
			Cull Back
		
			CGPROGRAM
		
			#pragma vertex vert
			#pragma fragment frag
			
			#pragma multi_compile_fwdbase
		
			#include "UnityCG.cginc"

			fixed4 _Color;
			sampler2D _MainTex;
			float4 _MainTex_ST;
		
			struct a2v {
				float4 pos : POSITION;
				float2 texcoord : TEXCOORD0;
			}; 
		
			struct v2f {
				float4 pos : SV_POSITION;
				half2 texcoord : TEXCOORD0;
			};
			
			v2f vert (a2v v) {
				v2f o;
				o.pos = mul( UNITY_MATRIX_MVP, v.pos);
				o.texcoord = TRANSFORM_TEX (v.texcoord, _MainTex);
				return o;
			}
			fixed4 frag(v2f i) : SV_Target {
				fixed4 col = tex2D (_MainTex, i.texcoord);
				col *= _Color;
                return col;
            }
		
			ENDCG
		}
	}
	FallBack Off
}

你可能感兴趣的:(Unity效果开发)