轉寄請保持地址:http://blog.csdn.net/stalendp/article/details/23139953
這裡將介紹基於法線的邊緣檢測方法,這裡的shader是參考官方的:http://wiki.unity3d.com/index.php/Outlined_Diffuse_3;運行效果如下:
代碼如下:
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"}
原理介紹:
分兩個pass進行渲染,第一個渲染邊框,第二個渲染實物。
1)邊框的渲染
在vertex Shader階段,吧頂點按照法線的方向進行擴充, 這樣物體就比原來要膨脹(關於膨脹效果,請參考Surface Shader Example中的Normal Extrusion with Vertex Modifier)。
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; //按照法線的方向進行位移
效果如下:
邊框蓋住裡原始物體,這裡只要設定只渲染背面,就可以達到效果,在渲染邊框的Pass中設定“Cull Front”就可以了。
2)渲染實物
略
當然這個只是適用於3d物體,映像的邊緣檢測,還有canny演算法等,以後再補充,這裡給個Canny的地址:Canny Edge Detection Tutorial
另外一篇:http://en.wikipedia.org/wiki/Edge_detection; 還有: http://en.wikipedia.org/wiki/Canny_edge_detector
關於映像的邊緣檢測,可以運用在Deferred shading中的anti-aliasing中
======
相關的文章:http://unitygems.com/noobs-guide-shaders-6-toon-shader/
Silhouette-Outlined Diffuse
OutlinedDiffuse
Lighted Bumped Outline
基於法線的邊緣檢測
Code Snippet: Edge Detector/Antialiasing Shader