边缘检测后处理效果

使用 卷积核 对像素进行边界判断,使用 Sobel 算子 对横竖两个维度进行分别检测。

  • 脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EdgeDetection : PostEffectBase
{
    
    
    public Shader EdgeDetectionShader;
    [Range(0f, 1f)]
    public float edgesOnly;
    public Color edgeColor;
    public Color backgroundColor;

    private Material _edgeDetectionMaterial;

    private Material material
    {
    
    
        get
        {
    
    
            _edgeDetectionMaterial = CheckShaderAndCreateMaterial(EdgeDetectionShader, _edgeDetectionMaterial);
            return _edgeDetectionMaterial;
        }
    }
    
    private void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
    
    
        if (material != null)
        {
    
    
            material.SetFloat("_EdgesOnly", edgesOnly);
            material.SetColor("_EdgeColor", edgeColor);
            material.SetColor("_BackgroundColor", backgroundColor);
            
            Graphics.Blit(src, dest, material);
        }
        else Graphics.Blit(src, dest);
    }
}

  • shader
Shader "Hidden/EdgeDetection"
{
    
    
    Properties
    {
    
    
        _MainTex ("Texture", 2D) = "white" {
    
    }
        _EdgesOnly ("EdgesOnly", Float) = 1
        _EdgeColor ("EdgeColor", Color) = (1, 1, 1, 1)
        _BackgroundColor ("BackgroundColor", Color) = (0, 0, 0, 0)
    }
    SubShader
    {
    
    
        Cull Off ZWrite Off ZTest Always

        Pass
        {
    
    
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

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

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

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

            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 = dot(unity_ColorSpaceLuminance, tex2D(_MainTex, i.uv[it]));
                    edgeX += Gx[it] * texColor;
                    edgeY += Gy[it] * texColor;
                }

                return 1 - abs(edgeX) - abs(edgeY);
            }

            v2f vert (appdata v)
            {
    
    
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                half2 uv = v.uv;

                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 frag (v2f i) : SV_Target
            {
    
    
                fixed4 color = tex2D(_MainTex, i.uv[4]);

                half sobel = Sobel(i);

                fixed4 withEdgeColor = lerp(_EdgeColor, color, sobel);
                fixed4 onlyEdgeColor = lerp(_EdgeColor, _BackgroundColor, sobel);
                return lerp(withEdgeColor, onlyEdgeColor, _EdgesOnly);
            }

            ENDCG
        }
    }
    
    FallBack Off
}

猜你喜欢

转载自blog.csdn.net/m0_52361859/article/details/127984382