基本思路:unity有一种很容易实现光影效果的神器,那就是unity自带的projector。
首先需要用到两张贴图:
一张投影需要的光圈贴图,如下:

一张根据远近距离显示暗淡的贴图(左至右,白渐变黑),如下:

主要用到两个参数:_Projector 和 _ProjectorClip
mul(_Projector, vertex):用于计算要投影的图片正确的显示到场景上接受投影的位置
mul(_ProjectorClip, vertex)://用于计算投影位置和投影器之间的距离
1 Shader "xj/ModelEffect/ProjectorLight" {
2 Properties {
3 _Color ("Main Color", Color) = (1,1,1,1)
4 _ShadowTex ("Cookie", 2D) = "" {}
5 _FalloffTex ("FallOff", 2D) = "" {}
6 _intensity ("Intensity", float) = 1.0
7 }
8
9 Subshader {
10 Tags {"Queue"="Transparent"}
11 Fog {Mode Off}
12
13 Pass {
14 ZWrite Off
15 //Fog { Color (0, 0, 0) }
16 ColorMask RGB
17 Blend DstColor One
18 Offset -1, -1
19
20 CGPROGRAM
21 #pragma vertex vert
22 #pragma fragment frag
23 #include "UnityCG.cginc"
24
25 struct v2f {
26 half4 uvShadow : TEXCOORD0;
27 half4 uvFalloff : TEXCOORD1;
28 float4 pos : SV_POSITION;
29 };
30
31 float4x4 _Projector;
32 float4x4 _ProjectorClip;
33
34 v2f vert (float4 vertex : POSITION)
35 {
36 v2f o;
37 o.pos = mul(UNITY_MATRIX_MVP,vertex);
38 //用于计算要投影的图片正确的显示到场景上接受投影的位置
39 o.uvShadow = mul(_Projector, vertex);
40 //用于计算投影位置和投影器之间的距离
41 o.uvFalloff = mul(_ProjectorClip, vertex);
42 return o;
43 }
44
45 fixed4 _Color;
46 sampler2D _ShadowTex;
47 sampler2D _FalloffTex;
48 fixed _intensity;
49
50 fixed4 frag (v2f i) : SV_Target
51 {
52 fixed4 texS = tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
53 texS.rgb *= _Color.rgb * _intensity;
54 texS.a = 1.0-texS.a;
55
56 fixed4 texF = tex2Dproj (_FalloffTex, UNITY_PROJ_COORD(i.uvFalloff));
57 fixed4 res = texS * texF.a;
58 return res;
59 }
60 ENDCG
61 }
62 }
63 }
来源:https://www.cnblogs.com/kane0526/p/10122849.html