大家都知道,实时光产生阴影的性能开销很大,为了在移动端有更好的性能表现,所以在网上参考了一些方案,总结了一个阴影shader去解决实时光性能开销过大的问题。
1.阴影shader,产生阴影的物体需要使用这个shader
Shader "Custom/PlayerShadow"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ShadowInvLen ("ShadowInvLen", float) = 1.0 //0.4449261
_Power("Power", Range(0,2)) = 1.7
_Alpha("Alpha",Range(0,1)) = 1.0
}
SubShader
{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry+10" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _Power;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv) * _Power;
// apply fog
//UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Back
ColorMask RGB
Stencil
{
Ref 0
Comp Equal
WriteMask 255
ReadMask 255
//Pass IncrSat
Pass Invert
Fail Keep
ZFail Keep
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _ShadowPlane; //渲染的地面
float4 _ShadowProjDir; //渲染的光源点,这里只是记录点的位置,用于计算阴影的投影
float4 _WorldPos;
float _ShadowInvLen; //阴影的长度
float4 _ShadowFadeParams; //阴影渐变的参数
float _Alpha;
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 xlv_TEXCOORD0 : TEXCOORD0;
float3 xlv_TEXCOORD1 : TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
float3 lightdir = normalize(_ShadowProjDir); //单位化光照方向向量
float3 worldpos = mul(unity_ObjectToWorld, v.vertex).xyz; //模型坐标转成世界坐标
// _ShadowPlane.w = p0 * n // 平面的w分量就是p0 * n
float distance = (_ShadowPlane.w - dot(_ShadowPlane.xyz, worldpos)) / dot(_ShadowPlane.xyz, lightdir.xyz); //计算阴影的长度
worldpos = worldpos + distance * lightdir.xyz; //影子的投影点
o.vertex = mul(unity_MatrixVP, float4(worldpos, 1.0));
o.xlv_TEXCOORD0 = _WorldPos.xyz;
o.xlv_TEXCOORD1 = worldpos; //影子投影的点组成的纹理
return o;
}
float4 frag(v2f i) : SV_Target
{
float3 posToPlane_2 = (i.xlv_TEXCOORD0 - i.xlv_TEXCOORD1);
float4 color;
color.xyz = float3(0.0, 0.0, 0.0);
color.w = (pow((1.0 - clamp(((sqrt(dot(posToPlane_2, posToPlane_2)) * _ShadowInvLen) - _ShadowFadeParams.x), 0.0, 1.0)), _ShadowFadeParams.y) * _ShadowFadeParams.z); //透明度渐变计算
color.a = color.a*_Alpha;
return color;
}
ENDCG
}
}
}
2.控制脚本,通过获取场景中的所有运用了阴影shader的材质。需要给定一个光源位置(空物体即可,不需要light),需要一个正交相机(不是你的主相机)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Qarth;
using System;
namespace GameWish.Game
{
public class ShadowController : TMonoSingleton
{
[SerializeField]
private GameObject m_Light;//模拟光源的位置
[SerializeField]
private List m_LstMat = new List();
[SerializeField]
private Vector4 m_ShadowPlane = new Vector4(0f, 0.4f, 0.0f, 0.0f);
private void Awake()
{
RefreshRender();
EventSystem.S.Register(EventID.OnMeshChange, OnMeshChange);
}
private void OnMeshChange(int key, object[] param)
{
RefreshRender();
}
private void RefreshRender()
{
m_LstMat.Clear();
MeshRenderer[] renderlist = GetComponentsInChildren();
SkinnedMeshRenderer[] skinRenderlist = GetComponentsInChildren();
for (int i = 0; i < renderlist.Length; i++)
{
if (renderlist[i] == null)
continue;
if (renderlist[i].materials != null)
{
for (int j = 0; j < renderlist[i].materials.Length; j++)
{
if (renderlist[i].materials[j].shader.name == "Custom/PlayerShadow")
m_LstMat.Add(renderlist[i].materials[j]);
}
}
}
for (int i = 0; i < skinRenderlist.Length; i++)
{
if (skinRenderlist[i] == null)
continue;
if (skinRenderlist[i].materials != null)
{
for (int j = 0; j < skinRenderlist[i].materials.Length; j++)
{
if (skinRenderlist[i].materials[j].shader.name == "Custom/PlayerShadow")
m_LstMat.Add(skinRenderlist[i].materials[j]);
}
}
}
}
void Update()
{
UpdateShader();
}
private void UpdateShader()
{
if (m_Light == null)
return;
foreach (var mat in m_LstMat)
{
if (mat == null)
continue;
mat.SetVector("_WorldPos", transform.position);
mat.SetVector("_ShadowProjDir", m_Light.transform.forward);
mat.SetVector("_ShadowPlane", m_ShadowPlane);
mat.SetVector("_ShadowFadeParams", new Vector4(1f, 0.9f, 0.8f, 0.7f));
}
}
}
}
有问题欢迎大家提出~ 参考文献:https://blog.csdn.net/dingxiaowei2013/article/details/84237331