Unity 描邊效果


第一種實現可以利用模板緩沖。

第二種實現可以使用邊緣檢測。邊緣檢測的原理就是利用一些邊緣檢測算子對圖像進行卷積操作。

卷積指的就是用一個卷積核kernel對一張圖像中的每個像素進行一系列操作。卷積核通常是一個四方形網格結構,該區域內每個方格都有一個權重值,當對圖像中的某個像素進行卷積時,我們會把卷積核的中心放在該像素上,翻轉核之后再一次計算核中每個元素和其覆蓋的圖像像素值的乘積並求和,得到的結果就是該位置的新像素值。例如,如果要對圖像進行均值模糊,可以使用一個3x3的卷積核,核內每個元素的值均為1/9.

Shader如下:

Shader "Unlit/EdgeDetection"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _EdgeOnly("Edge Only",Float) = 1.0
        _EdgeColor("Edge Color",Color) = (0,0,0,1)
        _BackgroundColor("BackgroundColor",Color) = (1,1,1,1)
    }
        SubShader
        {
            ZTest Always Cull off ZWrite Off
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"

                sampler2D _MainTex;
                half4 _MainTex_TexelSize;
                fixed _EdgeOnly;
                fixed4 _EdgeColor;
                fixed4 _BackgroundColor;

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                half2 uv[9] : TEXCOORD0;
            };

            
            v2f vert (appdata v)
            {
                v2f o;
                
                o.vertex = UnityObjectToClipPos(v.vertex);
                
                //該像素周圍的九個像素的uv坐標
                o.uv[0] = v.uv + _MainTex_TexelSize.xy * half2(-1, -1);
                o.uv[1] = v.uv + _MainTex_TexelSize.xy * half2(0, -1);
                o.uv[2] = v.uv + _MainTex_TexelSize.xy * half2(1, -1);
                o.uv[3] = v.uv + _MainTex_TexelSize.xy * half2(-1, 0);
                o.uv[4] = v.uv + _MainTex_TexelSize.xy * half2(0, 0);
                o.uv[5] = v.uv + _MainTex_TexelSize.xy * half2(1, 0);
                o.uv[6] = v.uv + _MainTex_TexelSize.xy * half2(-1, 1);
                o.uv[7] = v.uv + _MainTex_TexelSize.xy * half2(0, 1);
                o.uv[8] = v.uv + _MainTex_TexelSize.xy * half2(1, 1);


                return o;
            }
            
            //計算亮度值
            fixed luminace(fixed4 color)
            {
                return 0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b;
            }

            half Sobel(v2f i)
            {
                const half Gx[9] = {-1,-2,-1,
                                    0,0,0,
                                    1,2,1};
                const half Gy[9] = { -1,0,1,
                                    -2,0,2,
                                    -1,0,1 };

                half texColor;
                half edgeX = 0;
                half edgeY = 0;

                for (int it = 0; it < 9; it++)
                {
                    texColor = luminace(tex2D(_MainTex, i.uv[it]));
                    edgeX += texColor * Gx[it];
                    edgeY += texColor * Gy[it];
                }

                half edge = 1 - abs(edgeX) - abs(edgeY);

                return edge;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                half edge = Sobel(i);

                fixed4 result = lerp(_EdgeColor, tex2D(_MainTex, i.uv[4]), edge);

                return result;
            }
            ENDCG
        }
    }
}

測試效果:

應用shader后                                                      

  應用shader前


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM