//摄像机代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EdgeDectionNormalDepth: 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;
public float sampleDistance = 1.0f;
public float sensitivityDepth = 1.0f;
public float sensitivityNormals = 1.0f;
void OnEnable() {
GetComponent<Camera>().depthTextureMode |= DepthTextureMode.DepthNormals;
}
/// <summary>
/// OnRenderImage is called after all rendering is complete to render image.
/// </summary>
/// <param name="src">The source RenderTexture.</param>
/// <param name="dest">The destination RenderTexture.</param>
[ImageEffectOpaque]//希望在不透明的pass后调用该函数,render queue < 2500 的pass
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);
}
}
}
//shader代码
Shader"ShaderBook/13/EdgeDectionNormalDepth"{
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;//主纹理不需要翻转
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
uv.y = 1 - uv.y;
#endif
//其他纹理需要被翻转。
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;
}
//DecodeFloatRG 和 DecodeViewNormalStereo来解码深度+法线纹理中的深度和法线信息。
half CheckSame(half4 center,half4 sample){
//我们没有解码真正的法线值,只需要比较采样值的差异度
half2 centerNormal = center.xy;
float centerDepth = DecodeFloatRG(center.zw);
half2 sampleNormal = sample.xy;
float sampleDepth = DecodeFloatRG(sample.zw);
//difference in normals(法线的差值)*灵敏度
//当出现法线方向不同的时候肯定就是边界处
half2 diffNormal = abs(centerNormal - sampleNormal) * _Sensitivity.x;
//两个法线是否相等
int isSameNormal = (diffNormal.x + diffNormal.y) < 0.1;
//深度差值
float diffDepth = abs(centerDepth - sampleDepth) * _Sensitivity.y;
// 因为depth值很大,为了判断相等让他相差(0.1倍自身)
int isSameDepth = diffDepth < (0.05 * centerDepth);
// return:
// 1 - if normals and depth are similar enough
// 0 - otherwise
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.0f;
//返回为0时表明两点有边界
edge *= CheckSame(sample1, sample2);//右上左下
edge *= CheckSame(sample3, sample4);//左上右下
if(edge == 0){//AA
}
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
}