转载自:http://blog.sina.com.cn/s/blog_89d90b7c0102vo5q.html
玩过摄影的人都会知道“景深”这个词,简单来说就是焦点与前景,背景存在一定距离时,前后成像会比较模糊,因为焦点很清晰必然会成为照片中的重点。
在游戏中这种应用也是非常常见,印象深刻的比如网页版仙剑,在与NPC对话时,摄像机会变换到NPC正前方,而后面的背景会变的模糊,从而达到比较真实的空间感,让人身临其境。
效果如下:
因为没有真实场景,所以后面只用了一张图片而已。
原理在代码中,就直接上代码了。
shader代码:
- Shader
"Tut/Effects/Dof" { -
Properties { -
_MainTex ("Base (RGB)", 2D) = "" {} -
} -
Subshader { -
Pass { -
ZTest Always Cull Off ZWrite Off -
Fog { Mode off } -
CGPROGRAM -
#pragma vertex vert -
#pragma fragment frag -
#include "UnityCG.cginc" -
struct v2f { -
float4 pos : POSITION; -
float2 uv : TEXCOORD0; -
}; -
// 摄像机所见纹理 -
sampler2D _MainTex; -
// 摄像机深度纹理 -
sampler2D _CameraDepthTexture; -
// 模糊纹理 -
sampler2D _BlurTex; -
v2f vert (appdata_img v) { -
v2f o; -
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); -
o.uv.xy = v.texcoord.xy; -
return o; -
} -
half4 frag (v2f i) : COLOR { -
// 摄像机纹理 -
half4 ori = tex2D(_MainTex,i.uv); -
// 模糊纹理 -
half4 blur = tex2D(_BlurTex,i.uv); -
// 获取摄像机深度值颜色值,取rgb都可以 -
float dep = tex2D(_CameraDepthTexture,i.uv).r; -
// 深度Z缓存,从摄像机到最远平截面[0,1] -
dep = Linear01Depth(dep); -
// (1-dep) * ori + dep * blur -
// 所以靠近摄像机的不模糊,越远越模糊 -
return lerp(ori,blur,dep); -
} -
ENDCG -
} -
} -
Fallback off - }
-
- //inline
float Linear01Depth( float z ) - //{
- //
return 1.0 / (_ZBufferParams.x * z + _ZBufferParams.y); - //}
C#代码【挂在摄像机上】:
- using
UnityEngine; - using
System.Collections; -
- public
class _DepthOfField_2 : MonoBehaviour - {
-
// 景深shader -
public Shader dofShader; -
private Material dofMat = null; -
-
public Shader blurShader ; -
private Material blurMat = null; -
-
private float onePixelWidth = 1.0f / 512.0f; -
private float onePixelHeight = 1.0f / 512.0f; -
-
void Start () -
{ -
blurMat = new Material(blurShader); -
dofMat = new Material(dofShader); -
} -
-
void OnRenderImage(RenderTexture source, RenderTexture destination) -
{ -
// 创建【临时的屏幕纹理】 -
RenderTexture blurRT = RenderTexture.GetTemporary(source.width, source.height, 16); -
// 将【临时的屏幕纹理】通过【模糊shader】变换为【模糊纹理】 -
blurMat.SetVector("offsets", new Vector4(onePixelWidth, onePixelWidth, 0.0f, 0.0f)); -
Graphics.Blit(source, blurRT, blurMat); -
blurMat.SetVector("offsets", new Vector4(onePixelHeight, 0.0f, 0.0f, 0.0f)); -
Graphics.Blit(blurRT, blurRT, blurMat); -
-
// 将【模糊纹理】的值传到【景深shader】中进行处理 -
dofMat.SetTexture("_BlurTex", blurRT); -
// 将【屏幕纹理】通过【景深shader】变换为【最终纹理】 -
Graphics.Blit(source, destination, dofMat); -
// 释放临时纹理 -
RenderTexture.ReleaseTemporary(blurRT); -
} - }
学习来源:shaderlab实例教程