1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
using System;
using UnityEngine;
using XUtliPoolLib;
namespace XMainClient
{
[RequireComponent(typeof(Camera))]
public class XCustomShadow : MonoBehaviour
{
public int shadowChannel = 0;
public ShadowMapInfo shadowMapInfoRef = null;
public Camera m_shadowCamera = null;
private Transform m_shadowCameraTransformCache = null;
private Transform m_TargetTransformCache = null;
public Transform m_modifyEntityTransformCache = null;
public static float scale = 0.33f;
public float bias = 0.5f;
private Vector3 m_lastShadowPos = Vector3.zero;
private Transform m_lightTransformCache = null;
private Vector3 m_invLightForwardDir = new Vector3(0f, 0f, -1f);
private Vector3 m_lightRightDir = new Vector3(1f, 0f, 0f);
private Vector3 m_lightUpDir = new Vector3(0f, 1f, 0f);
public Matrix4x4 scaleBiasMatrix = Matrix4x4.identity;
private static Texture shadowMask = null;
public static XCustomShadow Create(Transform parent, ShadowMapInfo sg)
{
GameObject gameObject = new GameObject("ShadowProjector");
Camera camera = gameObject.AddComponent<Camera>();
XCustomShadow xcustomShadow = gameObject.AddComponent<XCustomShadow>();
xcustomShadow.m_shadowCamera = camera;
xcustomShadow.shadowMapInfoRef = sg;
xcustomShadow.m_shadowCameraTransformCache = gameObject.transform;
camera.useOcclusionCulling = false;
camera.clearFlags = (CameraClearFlags)2;
camera.backgroundColor = new Color(1f, 1f, 1f, 1f);
camera.farClipPlane = 5f;
camera.orthographicSize = 1.5f;
camera.orthographic = true;
camera.depth = -2f;
camera.cullingMask = 0;
camera.cullingMask |= 1 << XPlayer.PlayerLayer;
camera.targetTexture = sg.shadowMap;
camera.enabled = false;
xcustomShadow.enabled = false;
return xcustomShadow;
}
public void Begin(XGameObject target)
{
bool flag = this.OnTargetChange(target);
if (flag)
{
bool flag2 = this.m_shadowCamera != null;
if (flag2)
{
this.m_shadowCamera.orthographicSize = 1.5f;
this.scaleBiasMatrix = Matrix4x4.Scale(Vector3.one);
Shader shader = null;
int num = this.shadowChannel;
switch (num)
{
case 1:
shader = ShaderManager._maskR;
break;
case 2:
shader = ShaderManager._maskG;
break;
case 3:
break;
case 4:
shader = ShaderManager._maskB;
break;
default:
if (num == 8)
{
shader = ShaderManager._maskA;
}
break;
}
bool flag3 = shader != null;
if (flag3)
{
this.m_shadowCamera.SetReplacementShader(shader, "RenderType");
bool flag4 = XCustomShadow.shadowMask == null;
if (flag4)
{
XCustomShadow.shadowMask = XSingleton<XResourceLoaderMgr>.singleton.GetSharedResource<Texture>("Shader/Shadow/ShadowMask", ".png", true, false);
Shader.SetGlobalTexture("_ShadowMask", XCustomShadow.shadowMask);
}
}
}
}
bool flag5 = this.m_shadowCamera != null;
if (flag5)
{
this.m_shadowCamera.enabled = true;
}
base.enabled = true;
}
public void Clear(bool destroy)
{
bool flag = this.shadowMapInfoRef != null;
if (flag)
{
this.shadowMapInfoRef.FreeChannel(this.shadowChannel);
this.shadowMapInfoRef = null;
}
this.shadowChannel = 0;
this.m_lightTransformCache = null;
this.m_TargetTransformCache = null;
bool flag2 = this.m_shadowCamera != null;
if (flag2)
{
this.m_shadowCamera.enabled = false;
}
base.enabled = false;
if (destroy)
{
bool flag3 = this.m_shadowCamera != null;
if (flag3)
{
this.m_shadowCamera.targetTexture = null;
}
bool flag4 = XCustomShadow.shadowMask != null;
if (flag4)
{
XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource("Shader/Shadow/ShadowMask", ".png", XCustomShadow.shadowMask, false);
XCustomShadow.shadowMask = null;
Shader.SetGlobalTexture("_ShadowMask", null);
}
this.m_shadowCameraTransformCache = null;
UnityEngine.Object.Destroy(base.gameObject);
}
}
public bool OnTargetChange(XGameObject target)
{
Transform transform = (target != null) ? target.Find("") : null;
bool flag = this.m_TargetTransformCache != transform;
bool result;
if (flag)
{
this.m_TargetTransformCache = transform;
this.m_lightTransformCache = null;
result = true;
}
else
{
result = false;
}
return result;
}
public void SetCullLayer(bool enable)
{
bool flag = this.m_shadowCamera != null;
if (flag)
{
this.m_shadowCamera.cullingMask = 0;
if (enable)
{
this.m_shadowCamera.cullingMask |= 1 << XPlayer.PlayerLayer;
}
else
{
this.m_shadowCamera.cullingMask |= int.MinValue;
}
}
}
private void Update()
{
bool flag = this.m_lightTransformCache == null;
if (flag)
{
Light mainLight = XQualitySetting.mainLight;
bool flag2 = mainLight != null;
if (flag2)
{
this.m_lightTransformCache = mainLight.transform;
bool flag3 = this.m_lightTransformCache != null;
if (flag3)
{
this.m_invLightForwardDir = -this.m_lightTransformCache.forward;
this.m_lightRightDir = this.m_lightTransformCache.right;
this.m_lightUpDir = this.m_lightTransformCache.up;
}
}
}
bool flag4 = this.m_shadowCameraTransformCache != null && this.m_lightTransformCache != null && this.m_TargetTransformCache != null;
if (flag4)
{
Vector3 position = this.m_shadowCameraTransformCache.position;
float num = this.m_lastShadowPos.x * position.x + this.m_lastShadowPos.y * position.y + this.m_lastShadowPos.z * position.z;
float num2 = Quaternion.Angle(this.m_shadowCameraTransformCache.rotation, this.m_lightTransformCache.rotation);
bool flag5 = num2 >= 5f || num > 0.5f;
if (flag5)
{
this.m_shadowCameraTransformCache.rotation = this.m_lightTransformCache.rotation;
this.m_shadowCameraTransformCache.position = this.m_TargetTransformCache.position + this.m_invLightForwardDir * 2f + this.m_lightRightDir * 0.4f + this.m_lightUpDir * 0.2f;
this.m_lastShadowPos = this.m_shadowCameraTransformCache.position;
this.scaleBiasMatrix[0, 0] = XCustomShadow.scale;
this.scaleBiasMatrix[1, 1] = XCustomShadow.scale;
this.scaleBiasMatrix[2, 2] = XCustomShadow.scale;
this.scaleBiasMatrix[0, 3] = this.bias;
this.scaleBiasMatrix[1, 3] = this.bias;
this.scaleBiasMatrix[2, 3] = this.bias;
Matrix4x4 matrix4x = this.scaleBiasMatrix * this.m_shadowCamera.worldToCameraMatrix;
Shader.SetGlobalMatrix("custom_World2Shadow", matrix4x);
}
bool flag6 = this.m_modifyEntityTransformCache != this.m_TargetTransformCache && this.m_modifyEntityTransformCache != null;
if (flag6)
{
this.m_TargetTransformCache = this.m_modifyEntityTransformCache;
}
}
}
}
}
|