UnityShader屏幕后处理-边缘检测,经典的插值lerp

边缘检测原理。通过每个色块的周围色块计算该色块的卷积。图片中各个边缘的卷积不同于其他像素块的,至于大于或者小于由算法决定。

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;
}

调用Sobel()就可以求得edge系数,edge越小越接近边界。
采集边界在vert中进行

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

v2f vert(appdata_img v){
    v2f o;
    o.pos = UnityObjectToClipPos(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;
}

在片段处理器中,求得边界卷积之后

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

                //边缘颜色与图片颜色 - 越靠近边缘越接近于设置的边缘颜色否则为图片原来的颜色
                fixed4 withEdgeColor = lerp(_EdgeColor, tex2D(_MainTex, i.uv[4]), edge * 0.5 + 0.7);
                //边缘颜色与背景颜色 - 越靠近边缘越接近于设置的边缘颜色否则为设置的背景颜色
                fixed4 onlyEdgeColor = lerp(_EdgeColor, _BackgroundColor, edge);
                //图片颜色于背景颜色
                return lerp(withEdgeColor, onlyEdgeColor, _EdgeOnly);
            }

代码

EdgeDetection.shader

Shader "Custom/EdgeDetection"{
    Properties{
        _MainTex("MainTex", 2D) = ""{}
        _EdgeOnly("EdgeOnly", Float) = 1.0
        _EdgeColor("EdgeColor", Color) = (0,0,0,1)
        _BackgroundColor("BackgroundColor", Color) = (1,1,1,1)
    }

    SubShader{
        ZWrite Off
        ZTest Always Cull Off

        Pass{
            CGPROGRAM

            #include "UnityCG.cginc"

            #pragma vertex vert  
            #pragma fragment fragSobel

            sampler2D _MainTex;
            float4 _MainTex_ST;
            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 = UnityObjectToClipPos(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 * 0.5 + 0.7);
                //边缘颜色与背景颜色 - 越靠近边缘越接近于设置的边缘颜色否则为设置的背景颜色
                fixed4 onlyEdgeColor = lerp(_EdgeColor, _BackgroundColor, edge);
                //图片颜色于背景颜色
                return lerp(withEdgeColor, onlyEdgeColor, _EdgeOnly);
            }

            ENDCG
        }
    }
    FallBack Off
}

EdgeDetection.cs

using UnityEngine;
using System.Collections;

public class EdgeDetection : PostEffectsBase {

    public Shader edgeDetectShader;
    private Material edgeDetectMaterial = null;
    public Material material {  
        get {
            edgeDetectMaterial = CheckShaderAndCreateMaterial(edgeDetectShader, edgeDetectMaterial);
            return edgeDetectMaterial;
        }  
    }

    [Range(0.0f, 1.0f)]
    public float edgesOnly = 0.0f;

    public Color edgeColor = Color.black;

    public Color backgroundColor = Color.white;

    void OnRenderImage (RenderTexture src, RenderTexture dest) {
        if (material != null) {
            material.SetFloat("_EdgeOnly", edgesOnly);
            material.SetColor("_EdgeColor", edgeColor);
            material.SetColor("_BackgroundColor", backgroundColor);

            Graphics.Blit(src, dest, material);
        } else {
            Graphics.Blit(src, dest);
        }
    }
}

PostEffectsBase.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class PostEffectsBase : MonoBehaviour {

    protected bool CheckSupport() {
        if (SystemInfo.supportsImageEffects == false) {
            Debug.LogWarning("This platform does not support image effects.");
            return false;
        }

        return true;
    }

    protected void NotSupported() {
        enabled = false;
    }

    protected void CheckResources() {
        bool isSupported = CheckSupport();

        if (isSupported == false) {
            NotSupported();
        }
    }

    protected void Start() {
        CheckResources();
    }

    // Called when need to create the material used by this effect
    protected Material CheckShaderAndCreateMaterial(Shader shader, Material material) {
        if (shader == null) {
            return null;
        }

        if (shader.isSupported && material && material.shader == shader)
            return material;

        if (!shader.isSupported) {
            return null;
        }
        else {
            material = new Material(shader);
            material.hideFlags = HideFlags.DontSave;
            if (material)
                return material;
            else 
                return null;
        }
    }
}

你可能感兴趣的:(Unity,shader)