summaryrefslogtreecommitdiff
path: root/Client/Assembly-CSharp/LightSource.cs
blob: 891fcbf98be8f83ac599d9b8ab31392d6c15456a (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Generic;
using UnityEngine;

public class LightSource : MonoBehaviour
{
	public static Dictionary<GameObject, NoShadowBehaviour> NoShadows = new Dictionary<GameObject, NoShadowBehaviour>();

	public static Dictionary<GameObject, OneWayShadows> OneWayShadows = new Dictionary<GameObject, OneWayShadows>();

	[HideInInspector]
	private GameObject child;

	[HideInInspector]
	private Vector2[] requiredDels;

	[HideInInspector]
	private Mesh myMesh;

	public int MinRays = 24;

	public float LightRadius = 3f;

	public Material Material;

	[HideInInspector]
	private List<LightSource.VertInfo> verts = new List<LightSource.VertInfo>(256);

	[HideInInspector]
	private int vertCount;

	private RaycastHit2D[] buffer = new RaycastHit2D[25];

	private Collider2D[] hits = new Collider2D[40];

	private ContactFilter2D filter;

	private Vector3[] vec;

	private Vector2[] uvs;

	private int[] triangles = new int[1200];

	public float tol = 0.05f;

	private Vector2 del;

	private Vector2 tan;

	private Vector2 side;

	private List<RaycastHit2D> lightHits = new List<RaycastHit2D>();

	private class VertInfo
	{
		public float Angle;

		public Vector3 Position;

		internal void Complete(float x, float y)
		{
			this.Position.x = x;
			this.Position.y = y;
			this.Angle = LightSource.pseudoAngle(y, x);
		}

		internal void Complete(Vector2 point)
		{
			this.Position.x = point.x;
			this.Position.y = point.y;
			this.Angle = LightSource.pseudoAngle(point.y, point.x);
		}
	}

	private class AngleComparer : IComparer<LightSource.VertInfo>
	{
		public static readonly LightSource.AngleComparer Instance = new LightSource.AngleComparer();

		public int Compare(LightSource.VertInfo x, LightSource.VertInfo y)
		{
			if (x.Angle > y.Angle)
			{
				return 1;
			}
			if (x.Angle >= y.Angle)
			{
				return 0;
			}
			return -1;
		}
	}

	private class HitDepthComparer : IComparer<RaycastHit2D>
	{
		public static readonly LightSource.HitDepthComparer Instance = new LightSource.HitDepthComparer();

		public int Compare(RaycastHit2D x, RaycastHit2D y)
		{
			if (x.fraction <= y.fraction)
			{
				return -1;
			}
			return 1;
		}
	}

	private void Start()
	{
		this.filter.useTriggers = true;
		this.filter.layerMask = Constants.ShadowMask;
		this.filter.useLayerMask = true;
		this.requiredDels = new Vector2[this.MinRays];
		for (int i = 0; i < this.requiredDels.Length; i++)
		{
			this.requiredDels[i] = Vector2.left.Rotate((float)i / (float)this.requiredDels.Length * 360f);
		}
		this.myMesh = new Mesh();
		this.myMesh.MarkDynamic();
		this.myMesh.name = "ShadowMesh";
		GameObject gameObject = new GameObject("LightChild");
		gameObject.layer = 10;
		gameObject.AddComponent<MeshFilter>().mesh = this.myMesh;
		Renderer renderer = gameObject.AddComponent<MeshRenderer>();
		this.Material = new Material(this.Material);
		renderer.sharedMaterial = this.Material;
		this.child = gameObject;
	}

	private void Update()
	{
		this.vertCount = 0;
		Vector3 position = base.transform.position;
		position.z -= 7f;
		this.child.transform.position = position;
		Vector2 vector = position;
		this.Material.SetFloat("_LightRadius", this.LightRadius);
		int num = Physics2D.OverlapCircleNonAlloc(vector, this.LightRadius, this.hits, Constants.ShadowMask);
		for (int i = 0; i < num; i++)
		{
			Collider2D collider2D = this.hits[i];
			if (!collider2D.isTrigger)
			{
				EdgeCollider2D edgeCollider2D = collider2D as EdgeCollider2D;
				if (edgeCollider2D)
				{
					Vector2[] points = edgeCollider2D.points;
					for (int j = 0; j < points.Length; j++)
					{
						Vector2 vector2 = edgeCollider2D.transform.TransformPoint(points[j]);
						this.del.x = vector2.x - vector.x;
						this.del.y = vector2.y - vector.y;
						this.TestBothSides(vector);
					}
				}
				else
				{
					PolygonCollider2D polygonCollider2D = collider2D as PolygonCollider2D;
					if (polygonCollider2D)
					{
						Vector2[] points2 = polygonCollider2D.points;
						for (int k = 0; k < points2.Length; k++)
						{
							Vector2 vector3 = polygonCollider2D.transform.TransformPoint(points2[k]);
							this.del.x = vector3.x - vector.x;
							this.del.y = vector3.y - vector.y;
							this.TestBothSides(vector);
						}
					}
					else
					{
						BoxCollider2D boxCollider2D = collider2D as BoxCollider2D;
						if (boxCollider2D)
						{
							Vector2 b = boxCollider2D.size / 2f;
							Vector2 vector4 = boxCollider2D.transform.TransformPoint(boxCollider2D.offset - b) - vector;
							Vector2 vector5 = boxCollider2D.transform.TransformPoint(boxCollider2D.offset + b) - vector;
							this.del.x = vector4.x;
							this.del.y = vector4.y;
							this.TestBothSides(vector);
							this.del.x = vector5.x;
							this.TestBothSides(vector);
							this.del.y = vector5.y;
							this.TestBothSides(vector);
							this.del.x = vector4.x;
							this.TestBothSides(vector);
						}
					}
				}
			}
		}
		float d = this.LightRadius * 1.05f;
		for (int l = 0; l < this.requiredDels.Length; l++)
		{
			Vector2 vector6 = d * this.requiredDels[l];
			this.CreateVert(vector, ref vector6);
		}
		this.verts.Sort(0, this.vertCount, LightSource.AngleComparer.Instance);
		this.myMesh.Clear();
		if (this.vec == null || this.vec.Length < this.vertCount + 1)
		{
			this.vec = new Vector3[this.vertCount + 1];
			this.uvs = new Vector2[this.vec.Length];
		}
		this.vec[0] = Vector3.zero;
		this.uvs[0] = new Vector2(this.vec[0].x, this.vec[0].y);
		for (int m = 0; m < this.vertCount; m++)
		{
			int num2 = m + 1;
			this.vec[num2] = this.verts[m].Position;
			this.uvs[num2] = new Vector2(this.vec[num2].x, this.vec[num2].y);
		}
		int num3 = this.vertCount * 3;
		if (num3 > this.triangles.Length)
		{
			this.triangles = new int[num3];
			Debug.LogWarning("Resized triangles to: " + num3);
		}
		int num4 = 0;
		for (int n = 0; n < this.triangles.Length; n += 3)
		{
			if (n < num3)
			{
				this.triangles[n] = 0;
				this.triangles[n + 1] = num4 + 1;
				if (n == num3 - 3)
				{
					this.triangles[n + 2] = 1;
				}
				else
				{
					this.triangles[n + 2] = num4 + 2;
				}
				num4++;
			}
			else
			{
				this.triangles[n] = 0;
				this.triangles[n + 1] = 0;
				this.triangles[n + 2] = 0;
			}
		}
		this.myMesh.vertices = this.vec;
		this.myMesh.uv = this.uvs;
		this.myMesh.SetIndices(this.triangles, MeshTopology.Triangles, 0);
	}

	private void TestBothSides(Vector2 myPos)
	{
		float num = LightSource.length(this.del.x, this.del.x);
		this.tan.x = -this.del.y / num * this.tol;
		this.tan.y = this.del.x / num * this.tol;
		this.side.x = this.del.x + this.tan.x;
		this.side.y = this.del.y + this.tan.y;
		this.CreateVert(myPos, ref this.side);
		this.side.x = this.del.x - this.tan.x;
		this.side.y = this.del.y - this.tan.y;
		this.CreateVert(myPos, ref this.side);
	}

	private void CreateVert(Vector2 myPos, ref Vector2 del)
	{
		float num = this.LightRadius * 1.5f;
		int num2 = Physics2D.Raycast(myPos, del, this.filter, this.buffer, num);
		if (num2 > 0)
		{
			this.lightHits.Clear();
			RaycastHit2D raycastHit2D = default(RaycastHit2D);
			Collider2D collider2D = null;
			for (int i = 0; i < num2; i++)
			{
				RaycastHit2D raycastHit2D2 = this.buffer[i];
				Collider2D collider = raycastHit2D2.collider;
				OneWayShadows oneWayShadows;
				if (!LightSource.OneWayShadows.TryGetValue(collider.gameObject, out oneWayShadows) || !oneWayShadows.IsIgnored(this))
				{
					this.lightHits.Add(raycastHit2D2);
					if (!collider.isTrigger)
					{
						raycastHit2D = raycastHit2D2;
						collider2D = collider;
						break;
					}
				}
			}
			for (int j = 0; j < this.lightHits.Count; j++)
			{
				NoShadowBehaviour noShadowBehaviour;
				if (LightSource.NoShadows.TryGetValue(this.lightHits[j].collider.gameObject, out noShadowBehaviour))
				{
					noShadowBehaviour.didHit = true;
				}
			}
			if (collider2D && !collider2D.isTrigger)
			{
				Vector2 point = raycastHit2D.point;
				this.GetEmptyVert().Complete(point.x - myPos.x, point.y - myPos.y);
				return;
			}
		}
		Vector2 normalized = del.normalized;
		this.GetEmptyVert().Complete(normalized.x * num, normalized.y * num);
	}

	private LightSource.VertInfo GetEmptyVert()
	{
		if (this.vertCount < this.verts.Count)
		{
			List<LightSource.VertInfo> list = this.verts;
			int num = this.vertCount;
			this.vertCount = num + 1;
			return list[num];
		}
		LightSource.VertInfo vertInfo = new LightSource.VertInfo();
		this.verts.Add(vertInfo);
		this.vertCount = this.verts.Count;
		return vertInfo;
	}

	private static float length(float x, float y)
	{
		return Mathf.Sqrt(x * x + y * y);
	}

	public static float pseudoAngle(float dx, float dy)
	{
		if (dx < 0f)
		{
			float num = -dx;
			float num2 = (dy > 0f) ? dy : (-dy);
			return 2f - dy / (num + num2);
		}
		float num3 = (dy > 0f) ? dy : (-dy);
		return dy / (dx + num3);
	}
}