Unity Shader系列文章:Unity Shader目錄-初級篇
Unity Shader系列文章:Unity Shader目錄-中級篇
效果:
左圖:在原圖上描邊的效果卒落。右圖:只顯示描邊的效果
左圖:原效果趟薄。右圖:直接對顏色圖像進行邊緣檢測的結(jié)果
原理:
使用Roberts 算子進行邊緣檢測,Roberts 算子的本質(zhì)就是計算左上角和右下角的差值,乘以右上角和左下角的差值资柔,作為評估邊緣的依據(jù)。按這樣的方式,取對角方向的深度或法線值壶栋,比較它們之間的差值结胀,如果超過某個闕值(可由參數(shù)控制)赞咙,就認為它們之間存在一條邊。
Roberts 算子
ScreenPostEffectsBase基類代碼:
using UnityEngine;
/// <summary>
/// 屏幕后處理效果基類
/// </summary>
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ScreenPostEffectsBase : MonoBehaviour
{
public Shader Shader;
public Material Material
{
get
{
return CheckAndCreateMaterial();
}
}
private Material _material;
protected void Start()
{
CheckResources();
}
/// <summary>
/// 檢查資源
/// </summary>
protected void CheckResources()
{
if (!CheckSupport())
{
NotSupported();
}
}
/// <summary>
/// 檢查支持
/// </summary>
/// <returns></returns>
protected bool CheckSupport()
{
bool isSupported = SystemInfo.supportsImageEffects;
return isSupported;
}
/// <summary>
/// 不支持
/// </summary>
protected void NotSupported()
{
enabled = false;
}
/// <summary>
/// 檢查和創(chuàng)建Material
/// </summary>
/// <returns></returns>
protected Material CheckAndCreateMaterial()
{
if (!Shader || !Shader.isSupported)
{
return null;
}
if (_material && _material.shader == Shader)
{
return _material;
}
_material = new Material(Shader);
_material.hideFlags = HideFlags.DontSave;
return _material;
}
}
ScreenEdgeDetectNormalsAndDepth派生類代碼:
using UnityEngine;
/// <summary>
// 屏幕后處理-基于法線和深度紋理的邊緣檢測
/// </summary>
public class ScreenEdgeDetectNormalsAndDepth : ScreenPostEffectsBase
{
[Range(0.0f, 1.0f)]
public float EdgesOnly = 0.0f;
public Color EdgeColor = Color.black; // 邊緣顏色
public Color BackgroundColor = Color.white; // 背景顏色
public float SampleDistance = 1.0f; // 控制對深度+法線紋理采樣時 糟港,使用的采樣距離攀操。值越大,描邊越寬
public float SensitivityDepth = 1.0f; // 深度敏感度着逐,影響當鄰域的深度值相差多少時崔赌,會被認為存在一條邊界
public float SensitivityNormals = 1.0f; // 法線敏感度意蛀,影響當鄰域的法線值相差多少時耸别,會被認為存在一條邊界
private void OnEnable()
{
// 要獲取攝像機的深度+法線紋理
GetComponent<Camera>().depthTextureMode |= DepthTextureMode.DepthNormals;
}
// ImageEffectOpaque含義:
// 在默認情況下 OnRenderlmage 函數(shù)會 所有的不透明和透明的 Pass 執(zhí)行完畢后被調(diào)用 ,
// 前添加 ImageEffectOpaque 屬性后县钥,可以在不透明 Pass (即渲染隊列小于等于 500 Pass, 內(nèi)置的 Background Geometry AlphaTest 渲染隊列均在此范圍內(nèi))
// 執(zhí)行完畢后立即調(diào)用該函數(shù)秀姐,而不對透明物體(渲染隊列為 Transparent Pass 產(chǎn)生影響,
[ImageEffectOpaque]
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (Material != null)
{
Material.SetFloat("_EdgeOnly", EdgesOnly);
Material.SetColor("_EdgeColor", EdgeColor);
Material.SetColor("_BackgroundColor", BackgroundColor);
Material.SetFloat("_SampleDistance", SampleDistance);
Material.SetVector("_Sensitivity", new Vector4(SensitivityNormals, SensitivityDepth, 0.0f, 0.0f));
Graphics.Blit(src, dest, Material);
}
else
{
Graphics.Blit(src, dest);
}
}
}
shander代碼:
// 屏幕后處理-基于法線和深度紋理的邊緣檢測
Shader "Custom/EdgeDetectNormalsAndDepth"
{
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)
_SampleDistance ("Sample Distance", Float) = 1.0
_Sensitivity ("Sensitivity", Vector) = (1, 1, 1, 1)
}
SubShader
{
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
half4 _MainTex_TexelSize;
fixed _EdgeOnly;
fixed4 _EdgeColor;
fixed4 _BackgroundColor;
float _SampleDistance;
half4 _Sensitivity;
sampler2D _CameraDepthNormalsTexture; // 深度+法線紋理
struct v2f
{
float4 pos: SV_POSITION;
half2 uv[5]: TEXCOORD0;
};
v2f vert(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.texcoord;
o.uv[0] = uv;
// 下面要對這個紋理坐標進行平臺差異化處理若贮,因為OpenGL省有,(0, 0)點對應(yīng)了屏幕的左下角,DirectX中對應(yīng)了屏幕左上角
// 大多數(shù)時候這都無關(guān)緊要谴麦,除了渲染到渲染紋理時蠢沿。在此情況下,Unity 渲染到 Direct3D 上的紋理時匾效,會自動在內(nèi)部翻轉(zhuǎn)渲染舷蟀,以便平臺之間的慣例匹配。
// 如果我們做的屏幕后期特效簡單(一次處理一個紋理)面哼,這無關(guān)緊要野宜,因為 Graphics.Blit 方法會自動進行處理。
// 然而魔策,如果在屏幕后期特效中同時處理一個以上的 RenderTexture匈子,它們很可能會在不同的垂直方向出現(xiàn)(僅在類似 Direct3D 的平臺上,并且僅在使用抗鋸齒選項時)
// UNITY_UV_STARTS_AT_TOP闯袒,紋理的坐標系原點在紋理頂部的平臺上值:Direct3D類似平臺是1虎敦;OpenGL類似平臺是0
#if UNITY_UV_STARTS_AT_TOP
// 在Direct3D平臺下,如果我們開啟了抗鋸齒政敢,則xxx_TexelSize.y 會變成負值其徙,好讓我們能夠正確的進行采樣。
// 所以if (_MainTex_TexelSize.y < 0)的作用就是判斷我們當前是否開啟了抗鋸齒堕仔。
if (_MainTex_TexelSize.y < 0)
uv.y = 1 - uv.y;
#endif
// 存儲了使用 Roberts 算子時需要采樣的紋理坐標
o.uv[1] = uv + _MainTex_TexelSize.xy * half2(1, 1) * _SampleDistance;
o.uv[2] = uv + _MainTex_TexelSize.xy * half2(-1, -1) * _SampleDistance;
o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1, 1) * _SampleDistance;
o.uv[4] = uv + _MainTex_TexelSize.xy * half2(1, -1) * _SampleDistance;
return o;
}
// 計算對角線上兩個紋理值的差值擂橘,返回0表明這兩點之間存在一條邊界,反之則返回1
half CheckSame(half4 center, half4 sample)
{
// 獲取兩個采樣點的法線和深度值
// 這里并沒有解碼得到真正的法線值 而是直接使用了 xy 分量摩骨,是因為只需要比較兩個采樣值之間的差異度通贞,
// 而并不需要知道它們真正的法線值朗若。
half2 centerNormal = center.xy;
float centerDepth = DecodeFloatRG(center.zw);
half2 sampleNormal = sample.xy;
float sampleDepth = DecodeFloatRG(sample.zw);
// 把兩個采樣點的對應(yīng)值相減并取絕對值,再乘以靈敏度參數(shù)昌罩,把差異值的每個分量相加再和一個闕值比較哭懈,
// 如果它們的和小于閾值, 則返回1, 說明差異不明顯茎用,不存在一條邊界遣总;否則返回0,說明存在一條邊界轨功。
half2 diffNormal = abs(centerNormal - sampleNormal) * _Sensitivity.x;
int isSameNormal = (diffNormal.x + diffNormal.y) < 0.1;
float diffDepth = abs(centerDepth - sampleDepth) * _Sensitivity.y;
int isSameDepth = diffDepth < 0.1 * centerDepth;
// 最后旭斥, 把法線和深度的檢查結(jié)果相乘,作為組合后的返回值古涧。
return isSameNormal * isSameDepth ? 1.0: 0.0;
}
fixed4 fragRobertsCrossDepthAndNormal(v2f i): SV_Target
{
half4 sample1 = tex2D(_CameraDepthNormalsTexture, i.uv[1]);
half4 sample2 = tex2D(_CameraDepthNormalsTexture, i.uv[2]);
half4 sample3 = tex2D(_CameraDepthNormalsTexture, i.uv[3]);
half4 sample4 = tex2D(_CameraDepthNormalsTexture, i.uv[4]);
half edge = 1.0;
edge *= CheckSame(sample1, sample2);
edge *= CheckSame(sample3, sample4);
fixed4 withEdgeColor = lerp(_EdgeColor, tex2D(_MainTex, i.uv[0]), edge);
fixed4 onlyEdgeColor = lerp(_EdgeColor, _BackgroundColor, edge);
return lerp(withEdgeColor, onlyEdgeColor, _EdgeOnly);
}
ENDCG
Pass
{
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment fragRobertsCrossDepthAndNormal
ENDCG
}
}
Fallback Off
}