UnityShader高级篇——边缘检测

1.此脚本挂在摄像机上

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

public class EdgeDetection : PostEffectsBase {

	//声明需要的Shader,并据此创建材质
    public Shader EdgeDetectShader;

    private Material _edgeDetectMaterial;

    public Material Material
    {
        get
        {
            _edgeDetectMaterial = CheckShaderAndCreateMaterial(EdgeDetectShader, _edgeDetectMaterial);
            return _edgeDetectMaterial;
        }
    }

    //提供用于调整边缘线强度、描边颜色、背景颜色的参数
    //当EdgesOnly等于0时,边缘会叠加在原图像上,等于1时,只显示边缘,不显示渲染图像
    [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);
        }
    }

}

2.此Shader赋值给脚本

Shader "Unity Shaders Book/Chapter 12/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 frag
			
			#include "UnityCG.cginc"

			sampler2D _MainTex;
		    //xxx_TexelSize可访问xxx纹理对应的每个纹素的大小
		    //由于卷积需要对相邻区域内的纹理进行采样,需要利用_MainTex_TexelSize来计算各个相邻区域的纹理坐标
		    half4 _MainTex_TexelSize;
			fixed _EdgeOnly;
			fixed4 _EdgeColor;
			fixed4 _BackgroundColor;

			struct v2f
			{
				float4 pos : SV_POSITION;
				//定义了一个维数为9的纹理数组,对应了使用Sobel算子采样时需要的9个领域纹理坐标
				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.721 * color.g;
			}

			//使用Sobel算子对原图进行边缘检测
			half Sobel(v2f i) {
				//定义Sobel卷积核的Cx和Gy值
				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 };
				//依次对9个像素进行采样,并计算亮度值
				half texColor;
				half edgeX = 0;
				half edgeY = 0;
				for (int it = 0; it < 9; it++) {
					texColor = luminance(tex2D(_MainTex, i.uv[it]));
					//与卷积核Gx和Gy中对应的权重相乘后,叠加到各自的梯度值上
					edgeX += texColor * Gx[it];
					edgeY += texColor * Gy[it];
				}
				//从1中减去水平方向和竖直方向的梯度值的绝对值,得到edge,edge值越小,表明该位置越可能是一个边缘点
				half edge = 1 - abs(edgeX) - abs(edgeY);
				return edge;
			}

			
			fixed4 frag(v2f i) : SV_Target
			{
				//调用Sobel函数计算当前像素的梯度值
				half edge = Sobel(i);
			    //利用edge计算背景为原图的颜色值
			    fixed4 withEdgeColor = lerp(_EdgeColor, tex2D(_MainTex, i.uv[4]), edge);
				//利用edge计算背景为纯色的颜色值
				fixed4 onlyEdgeColor = lerp(_EdgeColor, _BackgroundColor, edge);
				//计算二者插值,得到最终值
				return lerp(withEdgeColor, onlyEdgeColor, _EdgeOnly);
			}
			ENDCG
		}
	}
			Fallback Off
}

你可能感兴趣的:(UnityShader初级篇)