Unity Shader学习笔记:描边

来源:互联网 发布:淘宝推广软件哪个好 编辑:程序博客网 时间:2024/05/17 02:10

边缘检测(描边效果)
边缘检测:是描边效果的一种实现方法。边缘检测的原理是利用一些边缘检测算子对图像进行卷积操作。
卷积操作:使用一个卷积核对一张图像中的每个像素进行一系列操作。卷积核通常是一个四方形网格结构(例如2x2、3x3的方形区域),该区域内每个方格都有一个权重值。当对图像中的某个像素进行卷积时,我们会把卷积核的中心放置于该像素上。翻转核之后再依次计算核中每个元素和其覆盖的图像像素值的乘积并求和,得到的结果就是该位置的新像素值。
可实现的图像效果:图像模糊,边缘检测等如果想要对图像进行均值模糊,可以使用一个3x3的卷积核,核内每个元素的值均为1/9.
边缘检测算子:
边:如果相邻像素之间存在差别明显的颜色、亮度、纹理等属性,我们就会认为它们之间应该有一条边界。这种相邻像素之间的差值可以用梯度来表示,可以想象得到,边缘处的梯度绝对值会比较大
梯度计算:G = 根号(Gx^2 + Gy^2)或G=|Gx|+|Gy|
三种常见的边缘检测算子:

Roberts: Gx: -1 0     Gy:0 -1    0 1          1  0Prewitt:Gx:-1 0 1  Gy: -1 -1 -1    -1 0 1       0  0  0    -1 0 1       1  1  1Sobel:Gx:-1 0 1   Gy : -1  -2  -1     -2 0 2        0   0   0    -1 0 1        1   2    1
using UnityEngine;public class OwnEdgeDetection : OwnPostEffectsBase {    public  Shader edgeDetectShader;    private Material edgeDetectMaterial = null;    public Material material    {        get        {            edgeDetectMaterial = CheckShaderAndCreateMaterial(edgeDetectShader, edgeDetectMaterial);            return edgeDetectMaterial;        }    }    //用于调整边缘线强度,当edgesOnly值为0时,边缘将会叠加在原渲染图像上,当edgesOnly值为1时,则只会显示边缘,不显示原渲染图像    [Range(0.0f,1.0f)]    public float edgesOnly = 0.0f;    //描边颜色          public Color edgeColor = Color.black;    //背景颜色    public Color backgroundColor = Color.black;    private void OnRenderImage(RenderTexture source, RenderTexture destination)    {        if(material != null)        {            material.SetFloat("_EdgeOnly", edgesOnly);            material.SetColor("_EdgeColor", edgeColor);            material.SetColor("_BackgroundColor", backgroundColor);            Graphics.Blit(source, destination, material);        }        else        {            Graphics.Blit(source, destination);        }    }}
Shader "Own/Chapter12-EdgeDetection"{    Properties    {        //输入的渲染纹理        _MainTex("Base (RGB)",2D) = "white"{}        _EdgeOnly("Edge Only",Float) = 1.0        _EdgeColor("Edge Color",Color) = (0,0,0,1)        _BackgroundColor("Background Color",Color) = (1,1,1,1)    }    SubShader    {        Pass        {            ZTest Always Cull Off ZWrite Off            CGPROGRAM            #pragma vertex vert            #pragma fragment fragSobel            #include "UnityCG.cginc"            sampler2D _MainTex;            //Unity提供的访问纹理对应的每个纹素的大小。            //是一个Vector4(1/width,1/height,width,height)            uniform half4 _MainTex_TexelSize;            fixed _EdgeOnly;            fixed4 _EdgeColor;            fixed4 _BackgroundColor;            struct v2f{                float4 pos : SV_POSITION;                half2 uv[9] : TEXCOORD0;            };            v2f vert(appdata_img v){                v2f o;                o.pos = mul(UNITY_MATRIX_MVP,v.vertex);                half2 uv = v.texcoord;                o.uv[0] = uv + _MainTex_TexelSize.xy * half2(-1,1);                o.uv[1] = uv + _MainTex_TexelSize.xy * half2(0,-1);                o.uv[2] = uv + _MainTex_TexelSize.xy * half2(1,-1);                o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1,0);                o.uv[4] = uv + _MainTex_TexelSize.xy * half2(0,0);                o.uv[5] = uv + _MainTex_TexelSize.xy * half2(1,0);                o.uv[6] = uv + _MainTex_TexelSize.xy * half2(-1,1);                o.uv[7] = uv + _MainTex_TexelSize.xy * half2(0,1);                o.uv[8] = uv + _MainTex_TexelSize.xy * half2(1,1);                return o;            }            fixed luminance(fixed4 color){                return 0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b;            }            half Sobel(v2f i){                const half Gx[9] = {-1,0,1,                                    -2,0,2,                                    -1,0,1};                const half Gy[9] = {-1,-2,-1,                                    0,0,0,                                    1,2,1};                half texColor;                half edgeX = 0;                half edgeY = 0;                for(int it = 0; it < 9; it++){                    texColor = luminance(tex2D(_MainTex,i.uv[it]));                    edgeX += texColor * Gx[it];                    edgeY += texColor * Gy[it];                }                half edge = 1 - abs(edgeX) - abs(edgeY);                return edge;            }            fixed4 fragSobel(v2f i):SV_Target{                half edge = Sobel(i);                fixed4 withEdgeColor = lerp(_EdgeColor,tex2D(_MainTex,i.uv[4]),edge);                fixed4 onlyEdgeColor = lerp(_EdgeColor,_BackgroundColor,edge);                return lerp(withEdgeColor,onlyEdgeColor,_EdgeOnly);            }            ENDCG        }    }    FallBack Off}

扩展:
图像卷积和滤波:http://blog.csdn.net/zouxy09/article/details/49080029

原创粉丝点击