Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Lighting.cs
Go to the documentation of this file.
1using System;
5using Terraria.ID;
7
8namespace Terraria;
9
10public class Lighting
11{
12 private const float DEFAULT_GLOBAL_BRIGHTNESS = 1.2f;
13
14 private const float BLIND_GLOBAL_BRIGHTNESS = 1f;
15
16 [Old]
17 public static int OffScreenTiles = 45;
18
19 private static LightMode _mode = LightMode.Color;
20
21 private static readonly LightingEngine NewEngine = new LightingEngine();
22
23 private static readonly LegacyLighting LegacyEngine = new LegacyLighting(Main.Camera);
24
26
27 public static float GlobalBrightness { get; set; }
28
29 public static LightMode Mode
30 {
31 get
32 {
33 return _mode;
34 }
35 set
36 {
37 _mode = value;
38 switch (_mode)
39 {
40 case LightMode.Color:
42 LegacyEngine.Mode = 0;
43 OffScreenTiles = 35;
44 break;
45 case LightMode.White:
47 LegacyEngine.Mode = 1;
48 break;
49 case LightMode.Retro:
51 LegacyEngine.Mode = 2;
52 break;
53 case LightMode.Trippy:
55 LegacyEngine.Mode = 3;
56 break;
57 }
58 Main.renderCount = 0;
59 Main.renderNow = false;
60 }
61 }
62
63 public static bool NotRetro
64 {
65 get
66 {
67 if (Mode != LightMode.Retro)
68 {
69 return Mode != LightMode.Trippy;
70 }
71 return false;
72 }
73 }
74
75 public static bool UsingNewLighting => Mode == LightMode.Color;
76
77 public static bool UpdateEveryFrame
78 {
79 get
80 {
82 {
83 return !NotRetro;
84 }
85 return false;
86 }
87 }
88
89 public static void Initialize()
90 {
91 GlobalBrightness = 1.2f;
92 NewEngine.Rebuild();
93 LegacyEngine.Rebuild();
94 if (_activeEngine == null)
95 {
96 Mode = LightMode.Color;
97 }
98 }
99
100 public static void LightTiles(int firstX, int lastX, int firstY, int lastY)
101 {
102 Main.render = true;
104 _activeEngine.ProcessArea(new Rectangle(firstX, firstY, lastX - firstX, lastY - firstY));
105 }
106
107 private static void UpdateGlobalBrightness()
108 {
109 GlobalBrightness = 1.2f;
110 if (Main.player[Main.myPlayer].blind)
111 {
112 GlobalBrightness = 1f;
113 }
114 }
115
116 public static float Brightness(int x, int y)
117 {
118 Vector3 color = _activeEngine.GetColor(x, y);
119 return GlobalBrightness * (color.X + color.Y + color.Z) / 3f;
120 }
121
122 public static Vector3 GetSubLight(Vector2 position)
123 {
124 Vector2 vector = position / 16f - new Vector2(0.5f, 0.5f);
125 Vector2 vector2 = new Vector2(vector.X % 1f, vector.Y % 1f);
126 int num = (int)vector.X;
127 int num2 = (int)vector.Y;
128 Vector3 color = _activeEngine.GetColor(num, num2);
129 Vector3 color2 = _activeEngine.GetColor(num + 1, num2);
130 Vector3 color3 = _activeEngine.GetColor(num, num2 + 1);
131 Vector3 color4 = _activeEngine.GetColor(num + 1, num2 + 1);
132 Vector3 value = Vector3.Lerp(color, color2, vector2.X);
133 Vector3 value2 = Vector3.Lerp(color3, color4, vector2.X);
134 return Vector3.Lerp(value, value2, vector2.Y);
135 }
136
137 public static void AddLight(Vector2 position, Vector3 rgb)
138 {
139 AddLight((int)(position.X / 16f), (int)(position.Y / 16f), rgb.X, rgb.Y, rgb.Z);
140 }
141
142 public static void AddLight(Vector2 position, float r, float g, float b)
143 {
144 AddLight((int)(position.X / 16f), (int)(position.Y / 16f), r, g, b);
145 }
146
147 public static void AddLight(int i, int j, int torchID, float lightAmount)
148 {
149 TorchID.TorchColor(torchID, out var R, out var G, out var B);
150 _activeEngine.AddLight(i, j, new Vector3(R * lightAmount, G * lightAmount, B * lightAmount));
151 }
152
153 public static void AddLight(Vector2 position, int torchID)
154 {
155 TorchID.TorchColor(torchID, out var R, out var G, out var B);
156 AddLight((int)position.X / 16, (int)position.Y / 16, R, G, B);
157 }
158
159 public static void AddLight(int i, int j, float r, float g, float b)
160 {
161 if (!Main.gamePaused && Main.netMode != 2)
162 {
163 _activeEngine.AddLight(i, j, new Vector3(r, g, b));
164 }
165 }
166
167 public static void NextLightMode()
168 {
169 Mode++;
170 if (!Enum.IsDefined(typeof(LightMode), Mode))
171 {
172 Mode = LightMode.White;
173 }
174 Clear();
175 }
176
177 public static void Clear()
178 {
179 _activeEngine.Clear();
180 }
181
182 public static Color GetColor(Point tileCoords)
183 {
184 if (Main.gameMenu)
185 {
186 return Color.White;
187 }
188 return new Color(_activeEngine.GetColor(tileCoords.X, tileCoords.Y) * GlobalBrightness);
189 }
190
191 public static Color GetColor(Point tileCoords, Color originalColor)
192 {
193 if (Main.gameMenu)
194 {
195 return originalColor;
196 }
197 return new Color(_activeEngine.GetColor(tileCoords.X, tileCoords.Y) * originalColor.ToVector3());
198 }
199
200 public static Color GetColor(int x, int y, Color oldColor)
201 {
202 if (Main.gameMenu)
203 {
204 return oldColor;
205 }
206 return new Color(_activeEngine.GetColor(x, y) * oldColor.ToVector3());
207 }
208
209 public static Color GetColorClamped(int x, int y, Color oldColor)
210 {
211 if (Main.gameMenu)
212 {
213 return oldColor;
214 }
215 Vector3 color = _activeEngine.GetColor(x, y);
216 color = Vector3.Min(Vector3.One, color);
217 return new Color(color * oldColor.ToVector3());
218 }
219
220 public static Color GetColor(int x, int y)
221 {
222 if (Main.gameMenu)
223 {
224 return Color.White;
225 }
226 Color result = default(Color);
227 Vector3 color = _activeEngine.GetColor(x, y);
228 float num = GlobalBrightness * 255f;
229 int num2 = (int)(color.X * num);
230 int num3 = (int)(color.Y * num);
231 int num4 = (int)(color.Z * num);
232 if (num2 > 255)
233 {
234 num2 = 255;
235 }
236 if (num3 > 255)
237 {
238 num3 = 255;
239 }
240 if (num4 > 255)
241 {
242 num4 = 255;
243 }
244 num4 <<= 16;
245 num3 <<= 8;
246 result.PackedValue = (uint)(num2 | num3 | num4) | 0xFF000000u;
247 return result;
248 }
249
250 public static void GetColor9Slice(int centerX, int centerY, ref Color[] slices)
251 {
252 int num = 0;
253 for (int i = centerX - 1; i <= centerX + 1; i++)
254 {
255 for (int j = centerY - 1; j <= centerY + 1; j++)
256 {
257 Vector3 color = _activeEngine.GetColor(i, j);
258 int num2 = (int)(255f * color.X * GlobalBrightness);
259 int num3 = (int)(255f * color.Y * GlobalBrightness);
260 int num4 = (int)(255f * color.Z * GlobalBrightness);
261 if (num2 > 255)
262 {
263 num2 = 255;
264 }
265 if (num3 > 255)
266 {
267 num3 = 255;
268 }
269 if (num4 > 255)
270 {
271 num4 = 255;
272 }
273 num4 <<= 16;
274 num3 <<= 8;
275 slices[num].PackedValue = (uint)(num2 | num3 | num4) | 0xFF000000u;
276 num += 3;
277 }
278 num -= 8;
279 }
280 }
281
282 public static void GetColor9Slice(int x, int y, ref Vector3[] slices)
283 {
284 slices[0] = _activeEngine.GetColor(x - 1, y - 1) * GlobalBrightness;
285 slices[3] = _activeEngine.GetColor(x - 1, y) * GlobalBrightness;
286 slices[6] = _activeEngine.GetColor(x - 1, y + 1) * GlobalBrightness;
287 slices[1] = _activeEngine.GetColor(x, y - 1) * GlobalBrightness;
288 slices[4] = _activeEngine.GetColor(x, y) * GlobalBrightness;
289 slices[7] = _activeEngine.GetColor(x, y + 1) * GlobalBrightness;
290 slices[2] = _activeEngine.GetColor(x + 1, y - 1) * GlobalBrightness;
291 slices[5] = _activeEngine.GetColor(x + 1, y) * GlobalBrightness;
292 slices[8] = _activeEngine.GetColor(x + 1, y + 1) * GlobalBrightness;
293 }
294
295 public static void GetCornerColors(int centerX, int centerY, out VertexColors vertices, float scale = 1f)
296 {
297 vertices = default(VertexColors);
298 Vector3 color = _activeEngine.GetColor(centerX, centerY);
299 Vector3 color2 = _activeEngine.GetColor(centerX, centerY - 1);
300 Vector3 color3 = _activeEngine.GetColor(centerX, centerY + 1);
301 Vector3 color4 = _activeEngine.GetColor(centerX - 1, centerY);
302 Vector3 color5 = _activeEngine.GetColor(centerX + 1, centerY);
303 Vector3 color6 = _activeEngine.GetColor(centerX - 1, centerY - 1);
304 Vector3 color7 = _activeEngine.GetColor(centerX + 1, centerY - 1);
305 Vector3 color8 = _activeEngine.GetColor(centerX - 1, centerY + 1);
306 Vector3 color9 = _activeEngine.GetColor(centerX + 1, centerY + 1);
307 float num = GlobalBrightness * scale * 63.75f;
308 int num2 = (int)((color2.X + color6.X + color4.X + color.X) * num);
309 int num3 = (int)((color2.Y + color6.Y + color4.Y + color.Y) * num);
310 int num4 = (int)((color2.Z + color6.Z + color4.Z + color.Z) * num);
311 if (num2 > 255)
312 {
313 num2 = 255;
314 }
315 if (num3 > 255)
316 {
317 num3 = 255;
318 }
319 if (num4 > 255)
320 {
321 num4 = 255;
322 }
323 num3 <<= 8;
324 num4 <<= 16;
325 vertices.TopLeftColor.PackedValue = (uint)(num2 | num3 | num4) | 0xFF000000u;
326 num2 = (int)((color2.X + color7.X + color5.X + color.X) * num);
327 num3 = (int)((color2.Y + color7.Y + color5.Y + color.Y) * num);
328 num4 = (int)((color2.Z + color7.Z + color5.Z + color.Z) * num);
329 if (num2 > 255)
330 {
331 num2 = 255;
332 }
333 if (num3 > 255)
334 {
335 num3 = 255;
336 }
337 if (num4 > 255)
338 {
339 num4 = 255;
340 }
341 num3 <<= 8;
342 num4 <<= 16;
343 vertices.TopRightColor.PackedValue = (uint)(num2 | num3 | num4) | 0xFF000000u;
344 num2 = (int)((color3.X + color8.X + color4.X + color.X) * num);
345 num3 = (int)((color3.Y + color8.Y + color4.Y + color.Y) * num);
346 num4 = (int)((color3.Z + color8.Z + color4.Z + color.Z) * num);
347 if (num2 > 255)
348 {
349 num2 = 255;
350 }
351 if (num3 > 255)
352 {
353 num3 = 255;
354 }
355 if (num4 > 255)
356 {
357 num4 = 255;
358 }
359 num3 <<= 8;
360 num4 <<= 16;
361 vertices.BottomLeftColor.PackedValue = (uint)(num2 | num3 | num4) | 0xFF000000u;
362 num2 = (int)((color3.X + color9.X + color5.X + color.X) * num);
363 num3 = (int)((color3.Y + color9.Y + color5.Y + color.Y) * num);
364 num4 = (int)((color3.Z + color9.Z + color5.Z + color.Z) * num);
365 if (num2 > 255)
366 {
367 num2 = 255;
368 }
369 if (num3 > 255)
370 {
371 num3 = 255;
372 }
373 if (num4 > 255)
374 {
375 num4 = 255;
376 }
377 num3 <<= 8;
378 num4 <<= 16;
379 vertices.BottomRightColor.PackedValue = (uint)(num2 | num3 | num4) | 0xFF000000u;
380 }
381
382 public static void GetColor4Slice(int centerX, int centerY, ref Color[] slices)
383 {
384 Vector3 color = _activeEngine.GetColor(centerX, centerY - 1);
385 Vector3 color2 = _activeEngine.GetColor(centerX, centerY + 1);
386 Vector3 color3 = _activeEngine.GetColor(centerX - 1, centerY);
387 Vector3 color4 = _activeEngine.GetColor(centerX + 1, centerY);
388 float num = color.X + color.Y + color.Z;
389 float num2 = color2.X + color2.Y + color2.Z;
390 float num3 = color4.X + color4.Y + color4.Z;
391 float num4 = color3.X + color3.Y + color3.Z;
392 if (num >= num4)
393 {
394 int num5 = (int)(255f * color3.X * GlobalBrightness);
395 int num6 = (int)(255f * color3.Y * GlobalBrightness);
396 int num7 = (int)(255f * color3.Z * GlobalBrightness);
397 if (num5 > 255)
398 {
399 num5 = 255;
400 }
401 if (num6 > 255)
402 {
403 num6 = 255;
404 }
405 if (num7 > 255)
406 {
407 num7 = 255;
408 }
409 slices[0] = new Color((byte)num5, (byte)num6, (byte)num7, 255);
410 }
411 else
412 {
413 int num8 = (int)(255f * color.X * GlobalBrightness);
414 int num9 = (int)(255f * color.Y * GlobalBrightness);
415 int num10 = (int)(255f * color.Z * GlobalBrightness);
416 if (num8 > 255)
417 {
418 num8 = 255;
419 }
420 if (num9 > 255)
421 {
422 num9 = 255;
423 }
424 if (num10 > 255)
425 {
426 num10 = 255;
427 }
428 slices[0] = new Color((byte)num8, (byte)num9, (byte)num10, 255);
429 }
430 if (num >= num3)
431 {
432 int num11 = (int)(255f * color4.X * GlobalBrightness);
433 int num12 = (int)(255f * color4.Y * GlobalBrightness);
434 int num13 = (int)(255f * color4.Z * GlobalBrightness);
435 if (num11 > 255)
436 {
437 num11 = 255;
438 }
439 if (num12 > 255)
440 {
441 num12 = 255;
442 }
443 if (num13 > 255)
444 {
445 num13 = 255;
446 }
447 slices[1] = new Color((byte)num11, (byte)num12, (byte)num13, 255);
448 }
449 else
450 {
451 int num14 = (int)(255f * color.X * GlobalBrightness);
452 int num15 = (int)(255f * color.Y * GlobalBrightness);
453 int num16 = (int)(255f * color.Z * GlobalBrightness);
454 if (num14 > 255)
455 {
456 num14 = 255;
457 }
458 if (num15 > 255)
459 {
460 num15 = 255;
461 }
462 if (num16 > 255)
463 {
464 num16 = 255;
465 }
466 slices[1] = new Color((byte)num14, (byte)num15, (byte)num16, 255);
467 }
468 if (num2 >= num4)
469 {
470 int num17 = (int)(255f * color3.X * GlobalBrightness);
471 int num18 = (int)(255f * color3.Y * GlobalBrightness);
472 int num19 = (int)(255f * color3.Z * GlobalBrightness);
473 if (num17 > 255)
474 {
475 num17 = 255;
476 }
477 if (num18 > 255)
478 {
479 num18 = 255;
480 }
481 if (num19 > 255)
482 {
483 num19 = 255;
484 }
485 slices[2] = new Color((byte)num17, (byte)num18, (byte)num19, 255);
486 }
487 else
488 {
489 int num20 = (int)(255f * color2.X * GlobalBrightness);
490 int num21 = (int)(255f * color2.Y * GlobalBrightness);
491 int num22 = (int)(255f * color2.Z * GlobalBrightness);
492 if (num20 > 255)
493 {
494 num20 = 255;
495 }
496 if (num21 > 255)
497 {
498 num21 = 255;
499 }
500 if (num22 > 255)
501 {
502 num22 = 255;
503 }
504 slices[2] = new Color((byte)num20, (byte)num21, (byte)num22, 255);
505 }
506 if (num2 >= num3)
507 {
508 int num23 = (int)(255f * color4.X * GlobalBrightness);
509 int num24 = (int)(255f * color4.Y * GlobalBrightness);
510 int num25 = (int)(255f * color4.Z * GlobalBrightness);
511 if (num23 > 255)
512 {
513 num23 = 255;
514 }
515 if (num24 > 255)
516 {
517 num24 = 255;
518 }
519 if (num25 > 255)
520 {
521 num25 = 255;
522 }
523 slices[3] = new Color((byte)num23, (byte)num24, (byte)num25, 255);
524 }
525 else
526 {
527 int num26 = (int)(255f * color2.X * GlobalBrightness);
528 int num27 = (int)(255f * color2.Y * GlobalBrightness);
529 int num28 = (int)(255f * color2.Z * GlobalBrightness);
530 if (num26 > 255)
531 {
532 num26 = 255;
533 }
534 if (num27 > 255)
535 {
536 num27 = 255;
537 }
538 if (num28 > 255)
539 {
540 num28 = 255;
541 }
542 slices[3] = new Color((byte)num26, (byte)num27, (byte)num28, 255);
543 }
544 }
545
546 public static void GetColor4Slice(int x, int y, ref Vector3[] slices)
547 {
548 Vector3 color = _activeEngine.GetColor(x, y - 1);
549 Vector3 color2 = _activeEngine.GetColor(x, y + 1);
550 Vector3 color3 = _activeEngine.GetColor(x - 1, y);
551 Vector3 color4 = _activeEngine.GetColor(x + 1, y);
552 float num = color.X + color.Y + color.Z;
553 float num2 = color2.X + color2.Y + color2.Z;
554 float num3 = color4.X + color4.Y + color4.Z;
555 float num4 = color3.X + color3.Y + color3.Z;
556 if (num >= num4)
557 {
558 slices[0] = color3 * GlobalBrightness;
559 }
560 else
561 {
562 slices[0] = color * GlobalBrightness;
563 }
564 if (num >= num3)
565 {
566 slices[1] = color4 * GlobalBrightness;
567 }
568 else
569 {
570 slices[1] = color * GlobalBrightness;
571 }
572 if (num2 >= num4)
573 {
574 slices[2] = color3 * GlobalBrightness;
575 }
576 else
577 {
578 slices[2] = color2 * GlobalBrightness;
579 }
580 if (num2 >= num3)
581 {
582 slices[3] = color4 * GlobalBrightness;
583 }
584 else
585 {
586 slices[3] = color2 * GlobalBrightness;
587 }
588 }
589}
static bool IsDefined(Type enumType, object value)
Definition Enum.cs:359
static void TorchColor(int torchID, out float R, out float G, out float B)
Definition TorchID.cs:164
static void GetColor9Slice(int x, int y, ref Vector3[] slices)
Definition Lighting.cs:282
const float BLIND_GLOBAL_BRIGHTNESS
Definition Lighting.cs:14
static void GetCornerColors(int centerX, int centerY, out VertexColors vertices, float scale=1f)
Definition Lighting.cs:295
static ILightingEngine _activeEngine
Definition Lighting.cs:25
static void AddLight(Vector2 position, float r, float g, float b)
Definition Lighting.cs:142
static Color GetColor(Point tileCoords)
Definition Lighting.cs:182
static void Initialize()
Definition Lighting.cs:89
static void NextLightMode()
Definition Lighting.cs:167
static void AddLight(int i, int j, float r, float g, float b)
Definition Lighting.cs:159
static void GetColor9Slice(int centerX, int centerY, ref Color[] slices)
Definition Lighting.cs:250
static void GetColor4Slice(int x, int y, ref Vector3[] slices)
Definition Lighting.cs:546
static bool UsingNewLighting
Definition Lighting.cs:75
static Color GetColor(Point tileCoords, Color originalColor)
Definition Lighting.cs:191
static Color GetColorClamped(int x, int y, Color oldColor)
Definition Lighting.cs:209
static bool NotRetro
Definition Lighting.cs:64
static void Clear()
Definition Lighting.cs:177
static void GetColor4Slice(int centerX, int centerY, ref Color[] slices)
Definition Lighting.cs:382
static void LightTiles(int firstX, int lastX, int firstY, int lastY)
Definition Lighting.cs:100
static Vector3 GetSubLight(Vector2 position)
Definition Lighting.cs:122
static bool UpdateEveryFrame
Definition Lighting.cs:78
static readonly LegacyLighting LegacyEngine
Definition Lighting.cs:23
static Color GetColor(int x, int y, Color oldColor)
Definition Lighting.cs:200
static float Brightness(int x, int y)
Definition Lighting.cs:116
const float DEFAULT_GLOBAL_BRIGHTNESS
Definition Lighting.cs:12
static float GlobalBrightness
Definition Lighting.cs:27
static void UpdateGlobalBrightness()
Definition Lighting.cs:107
static void AddLight(Vector2 position, int torchID)
Definition Lighting.cs:153
static int OffScreenTiles
Definition Lighting.cs:17
static LightMode _mode
Definition Lighting.cs:19
static void AddLight(Vector2 position, Vector3 rgb)
Definition Lighting.cs:137
static Color GetColor(int x, int y)
Definition Lighting.cs:220
static void AddLight(int i, int j, int torchID, float lightAmount)
Definition Lighting.cs:147
static readonly LightingEngine NewEngine
Definition Lighting.cs:21
static int myPlayer
Definition Main.cs:1801
static int netMode
Definition Main.cs:2095
static Camera Camera
Definition Main.cs:289
static bool RenderTargetsRequired
Definition Main.cs:2653
static bool LightingEveryFrame
Definition Main.cs:241
static bool gameMenu
Definition Main.cs:1926
static bool gamePaused
Definition Main.cs:1072
static Player[] player
Definition Main.cs:1803
static Vector3 Min(Vector3 value1, Vector3 value2)
Definition Vector3.cs:241
static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount)
Definition Vector3.cs:307