Keep forwarding address: http://blog.csdn.net/stalendp/article/details/23139953
Here we will introduce the normal-Based Edge Detection Method. Here, the shader is available at: http://wiki.unity3d.com/index.php/outlined_diffuse_3:
The Code is as follows:
Shader "Outlined/Diffuse" { // see http://wiki.unity3d.com/index.php/Outlined_Diffuse_3Properties {_Color ("Main Color", Color) = (.5,.5,.5,1)_OutlineColor ("Outline Color", Color) = (0,0,0,1)_Outline ("Outline width", Range (.002, 0.03)) = .005_MainTex ("Base (RGB)", 2D) = "white" { }} CGINCLUDE#include "UnityCG.cginc" struct appdata {float4 vertex : POSITION;float3 normal : NORMAL;}; struct v2f {float4 pos : POSITION;float4 color : COLOR;}; uniform float _Outline;uniform float4 _OutlineColor; v2f vert(appdata v) {// just make a copy of incoming vertex data but scaled according to normal directionv2f o;o.pos = mul(UNITY_MATRIX_MVP, v.vertex); float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);float2 offset = TransformViewToProjection(norm.xy);o.pos.xy += offset * o.pos.z * _Outline;// following please refer to : http://unitygems.com/noobs-guide-shaders-6-toon-shader/// o.pos = mul( UNITY_MATRIX_MVP, v.vertex + (float4(v.normal,0) * _Outline)); o.color = _OutlineColor;return o;}ENDCG SubShader {//Tags {"Queue" = "Geometry+100" }CGPROGRAM#pragma surface surf Lambert sampler2D _MainTex;fixed4 _Color; struct Input {float2 uv_MainTex;}; void surf (Input IN, inout SurfaceOutput o) {fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;o.Albedo = c.rgb;o.Alpha = c.a;}ENDCG // note that a vertex shader is specified here but its using the one abovePass {Name "OUTLINE"Tags { "LightMode" = "Always" }Cull FrontZWrite OnColorMask RGBBlend SrcAlpha OneMinusSrcAlpha//Offset 50,50 CGPROGRAM#pragma vertex vert#pragma fragment fraghalf4 frag(v2f i) :COLOR { return i.color; }ENDCG}} SubShader {CGPROGRAM#pragma surface surf Lambert sampler2D _MainTex;fixed4 _Color; struct Input {float2 uv_MainTex;}; void surf (Input IN, inout SurfaceOutput o) {fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;o.Albedo = c.rgb;o.Alpha = c.a;}ENDCG Pass {Name "OUTLINE"Tags { "LightMode" = "Always" }Cull FrontZWrite OnColorMask RGBBlend SrcAlpha OneMinusSrcAlpha CGPROGRAM#pragma vertex vert#pragma exclude_renderers gles xbox360 ps3ENDCGSetTexture [_MainTex] { combine primary }}} Fallback "Diffuse"}
Principles:
It is divided into two pass for rendering, the first rendering border, and the second rendering object.
1) Border Rendering
In the vertex Shader stage, the Vertex scales according to the Normal direction, so that the object expands more than the original one (for the expansion effect, see Normal Extrusion with vertex Modifier in Surface Shader Example ).
O. pos = mul (UNITY_MATRIX_MVP, v. vertex); float3 norm = mul (float3x3) UNITY_MATRIX_IT_MV, v. normal); float2 offset = TransformViewToProjection (norm. xy); // calculates the direction of the normal o. pos. xy + = offset * o. pos. z * _ Outline; // offset by the normal direction
The effect is as follows:
The original object in the border is covered. Here, you only need to set rendering on the back side to achieve the effect. You can set "Cull Front" in the Pass of the rendering border.
2) Rendering objects
Omitted
Of course, this is only applicable to 3d objects, image Edge Detection, and the canny algorithm. I will add it later. Here, I will give you a visit to the location of the well-being: the well-being Edge Detection Tutorial.
Another article: http://en.wikipedia.org/wiki/Edge_detection; and: http://en.wikipedia.org/wiki/Canny_edge_detector
Image edge detection can be applied to anti-aliasing in Deferred shading.
======
Article: http://unitygems.com/noobs-guide-shaders-6-toon-shader/
Silhouette-Outlined Diffuse
OutlinedDiffuse
Lighted Bumped Outline
Normal-Based Edge Detection
Code Snippet: Edge Detector/Antialiasing Shader