【OpenGL】Shader執行個體分析(五)- 邊緣檢測

來源:互聯網
上載者:User

轉寄請保持地址: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.