Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
LightMap.cs
Go to the documentation of this file.
4
6
7public class LightMap
8{
9 private Vector3[] _colors;
10
12
14
15 private const int DEFAULT_WIDTH = 203;
16
17 private const int DEFAULT_HEIGHT = 203;
18
19 public int NonVisiblePadding { get; set; }
20
21 public int Width { get; private set; }
22
23 public int Height { get; private set; }
24
25 public float LightDecayThroughAir { get; set; }
26
27 public float LightDecayThroughSolid { get; set; }
28
29 public Vector3 LightDecayThroughWater { get; set; }
30
31 public Vector3 LightDecayThroughHoney { get; set; }
32
33 public Vector3 this[int x, int y]
34 {
35 get
36 {
37 return _colors[IndexOf(x, y)];
38 }
39 set
40 {
41 _colors[IndexOf(x, y)] = value;
42 }
43 }
44
45 public LightMap()
46 {
49 LightDecayThroughWater = new Vector3(0.88f, 0.96f, 1.015f) * 0.91f;
50 LightDecayThroughHoney = new Vector3(0.75f, 0.7f, 0.6f) * 0.91f;
51 Width = 203;
52 Height = 203;
53 _colors = new Vector3[41209];
54 _mask = new LightMaskMode[41209];
55 }
56
57 public void GetLight(int x, int y, out Vector3 color)
58 {
59 color = _colors[IndexOf(x, y)];
60 }
61
62 public LightMaskMode GetMask(int x, int y)
63 {
64 return _mask[IndexOf(x, y)];
65 }
66
67 public void Clear()
68 {
69 for (int i = 0; i < _colors.Length; i++)
70 {
71 _colors[i].X = 0f;
72 _colors[i].Y = 0f;
73 _colors[i].Z = 0f;
74 _mask[i] = LightMaskMode.None;
75 }
76 }
77
78 public void SetMaskAt(int x, int y, LightMaskMode mode)
79 {
80 _mask[IndexOf(x, y)] = mode;
81 }
82
83 public void Blur()
84 {
85 BlurPass();
86 BlurPass();
88 }
89
90 private void BlurPass()
91 {
92 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
93 //IL_0019: Expected O, but got Unknown
94 //IL_0027: Unknown result type (might be due to invalid IL or missing references)
95 //IL_0032: Expected O, but got Unknown
96 FastParallel.For(0, Width, (ParallelForAction)delegate(int start, int end, object context)
97 {
98 for (int j = start; j < end; j++)
99 {
100 BlurLine(IndexOf(j, 0), IndexOf(j, Height - 1 - NonVisiblePadding), 1);
102 }
103 }, (object)null);
104 FastParallel.For(0, Height, (ParallelForAction)delegate(int start, int end, object context)
105 {
106 for (int i = start; i < end; i++)
107 {
110 }
111 }, (object)null);
112 }
113
114 private void BlurLine(int startIndex, int endIndex, int stride)
115 {
116 Vector3 zero = Vector3.Zero;
117 bool flag = false;
118 bool flag2 = false;
119 bool flag3 = false;
120 for (int i = startIndex; i != endIndex + stride; i += stride)
121 {
122 if (zero.X < _colors[i].X)
123 {
124 zero.X = _colors[i].X;
125 flag = false;
126 }
127 else if (!flag)
128 {
129 if (zero.X < 0.0185f)
130 {
131 flag = true;
132 }
133 else
134 {
135 _colors[i].X = zero.X;
136 }
137 }
138 if (zero.Y < _colors[i].Y)
139 {
140 zero.Y = _colors[i].Y;
141 flag2 = false;
142 }
143 else if (!flag2)
144 {
145 if (zero.Y < 0.0185f)
146 {
147 flag2 = true;
148 }
149 else
150 {
151 _colors[i].Y = zero.Y;
152 }
153 }
154 if (zero.Z < _colors[i].Z)
155 {
156 zero.Z = _colors[i].Z;
157 flag3 = false;
158 }
159 else if (!flag3)
160 {
161 if (zero.Z < 0.0185f)
162 {
163 flag3 = true;
164 }
165 else
166 {
167 _colors[i].Z = zero.Z;
168 }
169 }
170 if (flag && flag3 && flag2)
171 {
172 continue;
173 }
174 switch (_mask[i])
175 {
176 case LightMaskMode.None:
177 if (!flag)
178 {
179 zero.X *= LightDecayThroughAir;
180 }
181 if (!flag2)
182 {
183 zero.Y *= LightDecayThroughAir;
184 }
185 if (!flag3)
186 {
187 zero.Z *= LightDecayThroughAir;
188 }
189 break;
190 case LightMaskMode.Solid:
191 if (!flag)
192 {
193 zero.X *= LightDecayThroughSolid;
194 }
195 if (!flag2)
196 {
197 zero.Y *= LightDecayThroughSolid;
198 }
199 if (!flag3)
200 {
201 zero.Z *= LightDecayThroughSolid;
202 }
203 break;
204 case LightMaskMode.Water:
205 {
206 float num = (float)_random.WithModifier((ulong)i).Next(98, 100) / 100f;
207 if (!flag)
208 {
209 zero.X *= LightDecayThroughWater.X * num;
210 }
211 if (!flag2)
212 {
213 zero.Y *= LightDecayThroughWater.Y * num;
214 }
215 if (!flag3)
216 {
217 zero.Z *= LightDecayThroughWater.Z * num;
218 }
219 break;
220 }
221 case LightMaskMode.Honey:
222 if (!flag)
223 {
224 zero.X *= LightDecayThroughHoney.X;
225 }
226 if (!flag2)
227 {
228 zero.Y *= LightDecayThroughHoney.Y;
229 }
230 if (!flag3)
231 {
232 zero.Z *= LightDecayThroughHoney.Z;
233 }
234 break;
235 }
236 }
237 }
238
239 private int IndexOf(int x, int y)
240 {
241 return x * Height + y;
242 }
243
244 public void SetSize(int width, int height)
245 {
246 if (width * height > _colors.Length)
247 {
248 _colors = new Vector3[width * height];
249 _mask = new LightMaskMode[width * height];
250 }
251 Width = width;
252 Height = height;
253 }
254}
static void For(int fromInclusive, int toExclusive, ParallelForAction callback, object context=null)
void GetLight(int x, int y, out Vector3 color)
Definition LightMap.cs:57
LightMaskMode GetMask(int x, int y)
Definition LightMap.cs:62
void SetMaskAt(int x, int y, LightMaskMode mode)
Definition LightMap.cs:78
void BlurLine(int startIndex, int endIndex, int stride)
Definition LightMap.cs:114
void SetSize(int width, int height)
Definition LightMap.cs:244
delegate void ParallelForAction(int fromInclusive, int toExclusive, object context)
static FastRandom CreateWithRandomSeed()
Definition FastRandom.cs:37
FastRandom WithModifier(ulong modifier)
Definition FastRandom.cs:27