ShadowMap是比較流行的實(shí)時(shí)陰影實(shí)現(xiàn)方案,原理比較簡單,但真正實(shí)現(xiàn)起來還是會(huì)遇到很多問題的,我這里主要記錄下實(shí)現(xiàn)方式
先看效果
凹凸地形上也有陰影
實(shí)現(xiàn)原理
ShadowMap技術(shù)是從燈光空間用相機(jī)渲染一張RenderTexture,把深度值寫入其中所以稱之為深度圖,在把接受陰影的物體從模型空間轉(zhuǎn)換到燈光空間中,獲取深度圖里的深度進(jìn)行比較,如果深度值比深度圖中取出的值大就說明該點(diǎn)為陰影。
《Cg教程_可編程實(shí)時(shí)圖形權(quán)威指南》書上說的原理
陰影映射是一個(gè)雙過程的技術(shù):
1、 首先,場景以光源的位置為視點(diǎn)被渲染。每個(gè)渲染圖像的像素的深度被記錄在一個(gè)“深度紋理”中(這個(gè)紋理通常被稱為陰影貼圖)。
2、 然后,場景從眼睛的位置渲染,但是用標(biāo)準(zhǔn)的陰影紋理把陰影貼圖從燈的位置投影到場景中。在每個(gè)像素,深度采樣(從被投影的陰影貼圖紋理)與片段到燈的距離進(jìn)行比較。如果后者大,該像素就不是最靠近燈源的表面。這意味著這個(gè)片段是陰影,它在著色過程中不應(yīng)該接受光照。
第一步:生成深度圖shader####
把視點(diǎn)空間的Z值深度傳入片段找色器里除以w轉(zhuǎn)換為其次坐標(biāo),為啥要傳入片段找色器處理呢?因?yàn)镚PU會(huì)對(duì)片段找色器傳入的參數(shù)進(jìn)行插值計(jì)算,這樣才能更精確的計(jì)算出深度。
計(jì)算出深度之后,要轉(zhuǎn)換到一張圖片里存儲(chǔ)起來,如何把一個(gè)float存入圖片中呢?
float是4個(gè)字節(jié)的,剛好可以對(duì)應(yīng)RGBA4個(gè)分量,把一個(gè)float轉(zhuǎn)換成顏色值就可以存為圖片了,Unity中提供了一個(gè)內(nèi)置函數(shù):EncodeFloatRGBA幫助我們轉(zhuǎn)換
Shader "lijia/DeapthTextureShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float2 depth : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.depth = o.vertex.zw;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float depth = i.depth.x/i.depth.y;
fixed4 col = EncodeFloatRGBA(depth);
return col;
}
ENDCG
}
}
}
第二步:接受陰影的Shader
假如有一塊地板作為接受陰影的物體,在這個(gè)物體上運(yùn)行該Shader
- 把深度值傳入片段著色器里,在片段著色器中除以w轉(zhuǎn)換為其次坐標(biāo)的深度值(跟生成深度圖的Shader一樣處理)
- 把頂點(diǎn)轉(zhuǎn)換到燈光的視點(diǎn)空間,這里是傳入一個(gè)lijia_ProjectionMatrix 矩陣計(jì)算的
- 取出該像素對(duì)應(yīng)深度圖上的顏色值,轉(zhuǎn)換成深度值
- 把該像素的深度值跟深度圖里取出來的值進(jìn)行比較,如果比深度圖里的大,該點(diǎn)就為陰影
Shader "swan/ShadowMap/ShadowMapNormal"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
// sampler2D unity_Lightmap;//若開啟光照貼圖,系統(tǒng)默認(rèn)填值
// float4 unity_LightmapST;//與上unity_Lightmap同理
struct v2f {
float4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
float2 uv2:TEXCOORD1;
UNITY_FOG_COORDS(2)
float4 proj : TEXCOORD3;
float2 depth : TEXCOORD4;
};
float4x4 lijia_ProjectionMatrix;
sampler2D lijia_DepthTexture;
v2f vert(appdata_full v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//動(dòng)態(tài)陰影
o.depth = o.pos.zw;
lijia_ProjectionMatrix = mul(lijia_ProjectionMatrix, unity_ObjectToWorld);
o.proj = mul(lijia_ProjectionMatrix, v.vertex);
//--------------------------------------------------
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
fixed4 frag(v2f v) : COLOR
{
//解密光照貼圖計(jì)算公式
float3 lightmapColor = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap,v.uv2));
fixed4 col = tex2D(_MainTex, v.uv);
col.rgb *= lightmapColor;
UNITY_APPLY_FOG(v.fogCoord, col);
float depth = v.depth.x / v.depth.y;
fixed4 dcol = tex2Dproj(lijia_DepthTexture, v.proj);
float d = DecodeFloatRGBA(dcol);
float shadowScale = 1;
if(depth > d)
{
shadowScale = 0.55;
}
return col*shadowScale;
}
ENDCG
}
}
}
第三步:寫一個(gè)腳本調(diào)用上面的2個(gè)Shader####
上面我們已經(jīng)創(chuàng)建好了2個(gè)Shader
- 生成深度圖的DeapthTextureShader.shader
- 接受陰影的ShadowMapNormal(我這里把T4M跟接收光照貼圖的處理都寫進(jìn)去了)
腳本主要做的事情
- 創(chuàng)建一個(gè)相機(jī)把角度設(shè)置的跟燈光一樣,渲染出一張深度圖傳入給接受陰影的Shader(像素越高陰影的精度越高,但是消耗也就越大)
- 計(jì)算好頂點(diǎn)轉(zhuǎn)換到燈光空間的矩陣傳入給接受陰影的shader
using UnityEngine;
using System.Collections;
namespace SwanEngine.Core
{
/// <summary>
/// 創(chuàng)建depth相機(jī)
/// by lijia
/// </summary>
public class DepthTextureCamera : MonoBehaviour
{
Camera _camera;
RenderTexture _rt;
/// <summary>
/// 光照的角度
/// </summary>
public Transform lightTrans;
Matrix4x4 sm = new Matrix4x4();
void Start()
{
_camera = new GameObject().AddComponent<Camera>();
_camera.name = "DepthCamera";
_camera.depth = 2;
_camera.clearFlags = CameraClearFlags.SolidColor;
_camera.backgroundColor = new Color(1, 1, 1, 0);
_camera.cullingMask = LayerMask.GetMask("Player");
_camera.aspect = 1;
_camera.transform.position = this.transform.position;
_camera.transform.rotation = this.transform.rotation;
_camera.transform.parent = this.transform;
_camera.orthographic = true;
_camera.orthographicSize = 10;
sm.m00 = 0.5f;
sm.m11 = 0.5f;
sm.m22 = 0.5f;
sm.m03 = 0.5f;
sm.m13 = 0.5f;
sm.m23 = 0.5f;
sm.m33 = 1;
_rt = new RenderTexture(1024, 1024, 0);
_rt.wrapMode = TextureWrapMode.Clamp;
_camera.targetTexture = _rt;
_camera.SetReplacementShader(Shader.Find("lijia/DeapthTextureShader"), "RenderType");
}
void Update()
{
this.transform.eulerAngles = new Vector3(37.2f, -46.109f, -90.489f);
_camera.Render();
Matrix4x4 tm = GL.GetGPUProjectionMatrix(_camera.projectionMatrix, false) * _camera.worldToCameraMatrix;
tm = sm * tm;
Shader.SetGlobalMatrix("lijia_ProjectionMatrix", tm);
Shader.SetGlobalTexture("lijia_DepthTexture", _rt);
}
}
}
后面的話
幾乎所有的代碼我都貼上來了,有一些Shader基礎(chǔ)的話應(yīng)該是可以實(shí)現(xiàn)出效果的,希望能夠幫助到你們理解ShadowMap
在Unity里動(dòng)態(tài)陰影的實(shí)現(xiàn)方式還有很多,這里有個(gè)大合集
https://blog.uwa4d.com/archives/sparkle_shadow.html