Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
PathRenderer.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
5 static class PathRenderer {
6 public struct Point : IEquatable<Point> {
8
9 public float InnerRadiusL;
10
11 public float InnerRadiusR;
12
13 public float OuterRadiusL;
14
15 public float OuterRadiusR;
16
18
20
22
24
25 public float LengthScale;
26
27 public float MiterLimit;
28
29 public float InnerRadius {
30 set => InnerRadiusL = InnerRadiusR = value;
31 }
32
33 public float OuterRadius {
34 set => OuterRadiusL = OuterRadiusR = value;
35 }
36
38 set => InnerColorL = InnerColorR = value;
39 }
40
42 set => OuterColorL = OuterColorR = value;
43 }
44
45 public Color Color {
46 set {
47 Color innerColor = OuterColor = value;
48 InnerColor = innerColor;
49 }
50 }
51
52 bool IEquatable<Point>.Equals(Point other) {
53 if (Position == other.Position
54 && InnerRadiusL == other.InnerRadiusL
55 && InnerRadiusR == other.InnerRadiusR
56 && OuterRadiusL == other.OuterRadiusL
57 && OuterRadiusR == other.OuterRadiusR
58 && InnerColorL == other.InnerColorL
59 && InnerColorR == other.InnerColorR
60 && OuterColorL == other.OuterColorL
61 && OuterColorR == other.OuterColorR
62 && LengthScale == other.LengthScale) {
63 return MiterLimit == other.MiterLimit;
64 }
65 return false;
66 }
67 }
68
69 static DynamicArray<Point> TmpPoints = new();
70
71 static DynamicArray<Vector2> TmpPositions = new();
72
73 static DynamicArray<Vector2> TmpNormals = new();
74
75 public static int TrimPathStart(DynamicArray<Vector2> positions, BoundingRectangle rectangle) {
76 int count = positions.Count;
77 int num = -1;
78 for (int i = 0; i < positions.Count; i++) {
79 if (!rectangle.Contains(positions[i])) {
80 num = i;
81 break;
82 }
83 }
84 if (num < 0) {
85 positions.Count = 0;
86 }
87 else if (num > 0) {
88 positions.RemoveRange(0, num - 1);
89 Ray2 ray = new(positions[1], Vector2.Normalize(positions[0] - positions[1]));
90 float num2 = ray.Intersection(rectangle) ?? 0f;
91 positions[0] = ray.Position + ray.Direction * num2;
92 }
93 return count - positions.Count;
94 }
95
96 public static int TrimPathEnd(DynamicArray<Vector2> positions, BoundingRectangle rectangle) {
97 positions.Reverse();
98 int result = TrimPathStart(positions, rectangle);
99 positions.Reverse();
100 return result;
101 }
102
103 public static int TrimPathStart(DynamicArray<Vector2> positions, BoundingCircle circle) {
104 int count = positions.Count;
105 int num = -1;
106 for (int i = 0; i < positions.Count; i++) {
107 if (!circle.Contains(positions[i])) {
108 num = i;
109 break;
110 }
111 }
112 if (num < 0) {
113 positions.Count = 0;
114 }
115 else if (num > 0) {
116 positions.RemoveRange(0, num - 1);
117 Ray2 ray = new(positions[1], Vector2.Normalize(positions[0] - positions[1]));
118 float num2 = ray.Intersection(circle) ?? 0f;
119 positions[0] = ray.Position + ray.Direction * num2;
120 }
121 return count - positions.Count;
122 }
123
124 public static int TrimPathEnd(DynamicArray<Vector2> positions, BoundingCircle circle) {
125 positions.Reverse();
126 int result = TrimPathStart(positions, circle);
127 positions.Reverse();
128 return result;
129 }
130
131 public static void GeneratePathNormals(DynamicArray<Vector2> positions,
132 DynamicArray<bool> visibility,
133 bool loop,
134 DynamicArray<Vector2> normals) {
135 if (positions.Count == 0) {
136 return;
137 }
138 Vector2[] array = positions.Array;
139 bool[] array2 = visibility?.Array;
140 if (array2 != null) {
141 if (loop) {
142 for (int i = 0; i < positions.Count; i++) {
143 int num = (i - 1 + positions.Count) % positions.Count;
144 int num2 = i;
145 int num3 = (i + 1) % positions.Count;
146 normals.Add(array2[num] || array2[num2] ? Normal2(array[num2] - array[num], array[num3] - array[num2]) : default);
147 }
148 return;
149 }
150 normals.Add(array2[0] ? Normal1(array[1] - array[0]) : default);
151 for (int j = 1; j < positions.Count - 1; j++) {
152 normals.Add(array2[j - 1] || array2[j] ? Normal2(array[j] - array[j - 1], array[j + 1] - array[j]) : default);
153 }
154 if (positions.Count >= 2) {
155 normals.Add(array2[positions.Count - 2] ? Normal1(array[positions.Count - 1] - array[positions.Count - 2]) : default);
156 }
157 }
158 else if (loop) {
159 for (int k = 0; k < positions.Count; k++) {
160 int num4 = (k - 1 + positions.Count) % positions.Count;
161 int num5 = k;
162 int num6 = (k + 1) % positions.Count;
163 normals.Add(Normal2(array[num5] - array[num4], array[num6] - array[num5]));
164 }
165 }
166 else {
167 normals.Add(Normal1(array[1] - array[0]));
168 for (int l = 1; l < positions.Count - 1; l++) {
169 normals.Add(Normal2(array[l] - array[l - 1], array[l + 1] - array[l]));
170 }
171 if (positions.Count >= 2) {
172 normals.Add(Normal1(array[positions.Count - 1] - array[positions.Count - 2]));
173 }
174 }
175 }
176
177 public static void QueuePath(FlatBatch2D batch,
178 DynamicArray<Point> points,
179 DynamicArray<bool> visibility,
180 bool loop,
181 bool flatShading,
182 float depth = 0f) {
183 TmpPositions.Count = 0;
184 foreach (Point point in points) {
185 TmpPositions.Add(point.Position);
186 }
187 TmpNormals.Count = 0;
188 GeneratePathNormals(TmpPositions, visibility, loop, TmpNormals);
190 batch,
191 points,
193 visibility,
194 loop,
195 flatShading,
196 depth
197 );
198 }
199
200 public static void QueuePath(FlatBatch2D batch,
201 DynamicArray<Point> points,
202 DynamicArray<Vector2> normals,
203 DynamicArray<bool> visibility,
204 bool loop,
205 bool flatShading,
206 float depth = 0f) {
208 batch,
209 points,
210 normals,
211 visibility,
212 loop,
213 flatShading,
214 depth
215 );
216 }
217
218 public static void QueuePath(FlatBatch2D batch,
219 DynamicArray<Vector2> positions,
220 DynamicArray<bool> visibility,
221 bool loop,
222 bool flatShading,
223 float innerRadius,
224 float outerRadius,
225 Color innerColor,
226 Color outerColor,
227 float miterLimit,
228 float depth = 0f) {
229 QueuePath(
230 batch,
231 positions,
232 visibility,
233 loop,
234 flatShading,
235 innerRadius,
236 innerRadius,
237 outerRadius,
238 outerRadius,
239 innerColor,
240 innerColor,
241 outerColor,
242 outerColor,
243 miterLimit,
244 depth
245 );
246 }
247
248 public static void QueuePath(FlatBatch2D batch,
249 DynamicArray<Vector2> positions,
250 DynamicArray<bool> visibility,
251 bool loop,
252 bool flatShading,
253 float innerRadiusL,
254 float innerRadiusR,
255 float outerRadiusL,
256 float outerRadiusR,
257 Color innerColorL,
258 Color innerColorR,
259 Color outerColorL,
260 Color outerColorR,
261 float miterLimit,
262 float depth = 0f) {
263 Point point = default;
264 point.InnerRadiusL = innerRadiusL;
265 point.InnerRadiusR = innerRadiusR;
266 point.OuterRadiusL = outerRadiusL;
267 point.OuterRadiusR = outerRadiusR;
268 point.InnerColorL = innerColorL;
269 point.InnerColorR = innerColorR;
270 point.OuterColorL = outerColorL;
271 point.OuterColorR = outerColorR;
272 point.MiterLimit = miterLimit;
273 Point value = point;
274 TmpPoints.Count = positions.Count;
275 for (int i = 0; i < positions.Count; i++) {
276 value.Position = positions[i];
277 TmpPoints[i] = value;
278 }
279 TmpNormals.Count = 0;
280 GeneratePathNormals(positions, visibility, loop, TmpNormals);
282 batch,
283 TmpPoints,
285 visibility,
286 loop,
287 flatShading,
288 depth
289 );
290 }
291
292 public static void QueuePath(TexturedBatch2D batch,
293 DynamicArray<Point> points,
294 DynamicArray<bool> visibility,
295 bool loop,
296 bool flatShading,
297 float lengthOffset,
298 float depth = 0f) {
299 TmpPositions.Count = 0;
300 foreach (Point point in points) {
301 TmpPositions.Add(point.Position);
302 }
303 TmpNormals.Count = 0;
304 GeneratePathNormals(TmpPositions, visibility, loop, TmpNormals);
306 batch,
307 points,
309 visibility,
310 loop,
311 flatShading,
312 lengthOffset,
313 depth
314 );
315 }
316
317 public static void QueuePath(TexturedBatch2D batch,
318 DynamicArray<Point> points,
319 DynamicArray<Vector2> normals,
320 DynamicArray<bool> visibility,
321 bool loop,
322 bool flatShading,
323 float lengthOffset,
324 float depth = 0f) {
326 batch,
327 points,
328 normals,
329 visibility,
330 loop,
331 flatShading,
332 lengthOffset,
333 depth
334 );
335 }
336
337 public static void QueuePath(TexturedBatch2D batch,
338 DynamicArray<Vector2> positions,
339 DynamicArray<bool> visibility,
340 bool loop,
341 bool flatShading,
342 float innerRadius,
343 float outerRadius,
344 Color innerColor,
345 Color outerColor,
346 float lengthScale,
347 float lengthOffset,
348 float miterLimit,
349 float depth = 0f) {
350 QueuePath(
351 batch,
352 positions,
353 visibility,
354 loop,
355 flatShading,
356 innerRadius,
357 innerRadius,
358 outerRadius,
359 outerRadius,
360 innerColor,
361 innerColor,
362 outerColor,
363 outerColor,
364 lengthScale,
365 lengthOffset,
366 miterLimit,
367 depth
368 );
369 }
370
371 public static void QueuePath(TexturedBatch2D batch,
372 DynamicArray<Vector2> positions,
373 DynamicArray<bool> visibility,
374 bool loop,
375 bool flatShading,
376 float innerRadiusL,
377 float innerRadiusR,
378 float outerRadiusL,
379 float outerRadiusR,
380 Color innerColorL,
381 Color innerColorR,
382 Color outerColorL,
383 Color outerColorR,
384 float lengthScale,
385 float lengthOffset,
386 float miterLimit,
387 float depth = 0f) {
388 Point point = default;
389 point.InnerRadiusL = innerRadiusL;
390 point.InnerRadiusR = innerRadiusR;
391 point.OuterRadiusL = outerRadiusL;
392 point.OuterRadiusR = outerRadiusR;
393 point.InnerColorL = innerColorL;
394 point.InnerColorR = innerColorR;
395 point.OuterColorL = outerColorL;
396 point.OuterColorR = outerColorR;
397 point.LengthScale = lengthScale;
398 point.MiterLimit = miterLimit;
399 Point value = point;
400 TmpPoints.Count = positions.Count;
401 for (int i = 0; i < positions.Count; i++) {
402 value.Position = positions[i];
403 TmpPoints[i] = value;
404 }
405 TmpNormals.Count = 0;
406 GeneratePathNormals(positions, visibility, loop, TmpNormals);
408 batch,
409 TmpPoints,
411 visibility,
412 loop,
413 flatShading,
414 lengthOffset,
415 depth
416 );
417 }
418
419 // ReSharper disable UnusedParameter.Local
421 DynamicArray<Point> points,
422 DynamicArray<Vector2> normals,
423 DynamicArray<bool> visibility,
424 bool loop,
425 bool flatShading,
426 float depth)
427 // ReSharper restore UnusedParameter.Local
428 {
429 int num = loop ? normals.Count : normals.Count - 1;
430 for (int i = 0; i < num; i++) {
431 if (visibility != null
432 && !visibility[i]) {
433 continue;
434 }
435 int index = i;
436 int index2 = (i + 1) % normals.Count;
437 Point point = points[index];
438 Point point2 = points[index2];
439 Vector2 position = point.Position;
440 Vector2 position2 = point2.Position;
441 Vector2 vector = normals[index];
442 Vector2 vector2 = normals[index2];
443 if (vector.LengthSquared() > point.MiterLimit) {
444 vector = Normal1(position2 - position);
445 }
446 if (vector2.LengthSquared() > point2.MiterLimit) {
447 vector2 = Normal1(position2 - position);
448 }
449 if (point.OuterRadiusL != 0f
450 || point2.OuterRadiusL != 0f) {
451 Vector2 vector3 = vector * (0f - point.InnerRadiusL);
452 Vector2 vector4 = vector * (0f - point.OuterRadiusL);
453 Vector2 vector5 = vector2 * (0f - point2.InnerRadiusL);
454 Vector2 vector6 = vector2 * (0f - point2.OuterRadiusL);
455 if (flatShading) {
456 batch.QueueQuad(
457 position + vector4,
458 position + vector3,
459 position2 + vector5,
460 position2 + vector6,
461 0f,
462 point.OuterColorR,
463 point.InnerColorR,
464 point.InnerColorR,
465 point.OuterColorR
466 );
467 }
468 else {
469 batch.QueueQuad(
470 position + vector4,
471 position + vector3,
472 position2 + vector5,
473 position2 + vector6,
474 0f,
475 point.OuterColorR,
476 point.InnerColorR,
477 point2.InnerColorR,
478 point2.OuterColorR
479 );
480 }
481 }
482 if (point.OuterRadiusR != 0f
483 || point2.OuterRadiusR != 0f) {
484 Vector2 vector7 = vector * point.InnerRadiusR;
485 Vector2 vector8 = vector * point.OuterRadiusR;
486 Vector2 vector9 = vector2 * point2.InnerRadiusR;
487 Vector2 vector10 = vector2 * point2.OuterRadiusR;
488 if (flatShading) {
489 batch.QueueQuad(
490 position + vector8,
491 position + vector7,
492 position2 + vector9,
493 position2 + vector10,
494 0f,
495 point.OuterColorR,
496 point.InnerColorR,
497 point.InnerColorR,
498 point.OuterColorR
499 );
500 }
501 else {
502 batch.QueueQuad(
503 position + vector8,
504 position + vector7,
505 position2 + vector9,
506 position2 + vector10,
507 0f,
508 point.OuterColorR,
509 point.InnerColorR,
510 point2.InnerColorR,
511 point2.OuterColorR
512 );
513 }
514 }
515 }
516 }
517
518 // ReSharper disable UnusedParameter.Local
520 DynamicArray<Point> points,
521 DynamicArray<Vector2> normals,
522 DynamicArray<bool> visibility,
523 bool loop,
524 bool flatShading,
525 float lengthOffset,
526 float depth)
527 // ReSharper restore UnusedParameter.Local
528 {
529 float num = lengthOffset;
530 int num2 = loop ? normals.Count : normals.Count - 1;
531 for (int i = 0; i < num2; i++) {
532 if (visibility != null
533 && !visibility[i]) {
534 continue;
535 }
536 int index = i;
537 int index2 = (i + 1) % normals.Count;
538 Point point = points[index];
539 Point point2 = points[index2];
540 Vector2 position = point.Position;
541 Vector2 position2 = point2.Position;
542 float num3 = 1f / point.LengthScale;
543 Vector2 vector = position2 - position;
544 float num4 = vector.Length();
545 Vector2 v = vector / num4 * num3;
546 Vector2 vector2 = normals[index];
547 Vector2 vector3 = normals[index2];
548 if (vector2.LengthSquared() > point.MiterLimit) {
549 vector2 = Normal1(position2 - position);
550 }
551 if (vector3.LengthSquared() > point2.MiterLimit) {
552 vector3 = Normal1(position2 - position);
553 }
554 Vector2 vector4 = new(num, 0.5f);
555 num += num4 * num3;
556 Vector2 vector5 = new(num, 0.5f);
557 if (point.OuterRadiusL > 0f
558 || point2.OuterRadiusL > 0f) {
559 Vector2 vector6 = vector2 * (0f - point.InnerRadiusL);
560 Vector2 vector7 = vector2 * (0f - point.OuterRadiusL);
561 Vector2 vector8 = vector3 * (0f - point2.InnerRadiusL);
562 Vector2 vector9 = vector3 * (0f - point2.OuterRadiusL);
563 Vector2 vector10 = new(Vector2.Dot(vector6, v), -0.5f * point.InnerRadiusL / point.OuterRadiusL);
564 Vector2 vector11 = new(Vector2.Dot(vector7, v), -0.5f);
565 Vector2 vector12 = new(Vector2.Dot(vector8, v), -0.5f * point2.InnerRadiusL / point2.OuterRadiusL);
566 Vector2 vector13 = new(Vector2.Dot(vector9, v), -0.5f);
567 if (flatShading) {
568 batch.QueueQuad(
569 position + vector7,
570 position + vector6,
571 position2 + vector8,
572 position2 + vector9,
573 0f,
574 vector4 + vector11,
575 vector4 + vector10,
576 vector5 + vector12,
577 vector5 + vector13,
578 point.OuterColorR,
579 point.InnerColorR,
580 point.InnerColorR,
581 point.OuterColorR
582 );
583 }
584 else {
585 batch.QueueQuad(
586 position + vector7,
587 position + vector6,
588 position2 + vector8,
589 position2 + vector9,
590 0f,
591 vector4 + vector11,
592 vector4 + vector10,
593 vector5 + vector12,
594 vector5 + vector13,
595 point.OuterColorR,
596 point.InnerColorR,
597 point2.InnerColorR,
598 point2.OuterColorR
599 );
600 }
601 }
602 if (point.OuterRadiusR != 0f
603 || point2.OuterRadiusR != 0f) {
604 Vector2 vector14 = vector2 * point.InnerRadiusR;
605 Vector2 vector15 = vector2 * point.OuterRadiusR;
606 Vector2 vector16 = vector3 * point2.InnerRadiusR;
607 Vector2 vector17 = vector3 * point2.OuterRadiusR;
608 Vector2 vector18 = new(Vector2.Dot(vector14, v), 0.5f * point.InnerRadiusR / point.OuterRadiusR);
609 Vector2 vector19 = new(Vector2.Dot(vector15, v), 0.5f);
610 Vector2 vector20 = new(Vector2.Dot(vector16, v), 0.5f * point2.InnerRadiusR / point2.OuterRadiusR);
611 Vector2 vector21 = new(Vector2.Dot(vector17, v), 0.5f);
612 if (flatShading) {
613 batch.QueueQuad(
614 position + vector15,
615 position + vector14,
616 position2 + vector16,
617 position2 + vector17,
618 0f,
619 vector4 + vector19,
620 vector4 + vector18,
621 vector5 + vector20,
622 vector5 + vector21,
623 point.OuterColorR,
624 point.InnerColorR,
625 point.InnerColorR,
626 point.OuterColorR
627 );
628 }
629 else {
630 batch.QueueQuad(
631 position + vector15,
632 position + vector14,
633 position2 + vector16,
634 position2 + vector17,
635 0f,
636 vector4 + vector19,
637 vector4 + vector18,
638 vector5 + vector20,
639 vector5 + vector21,
640 point.OuterColorR,
641 point.InnerColorR,
642 point2.InnerColorR,
643 point2.OuterColorR
644 );
645 }
646 }
647 }
648 }
649
651
653 d1 = Vector2.Normalize(d1);
654 float num = MathF.Tan(((float)Math.PI - Vector2.Angle(d1, d2)) / 2f);
655 return Vector2.Perpendicular(d1) - d1 / num;
656 }
657 }
658}
void RemoveRange(int index, int count)
void QueueQuad(Vector2 corner1, Vector2 corner2, float depth, Color color)
void QueueQuad(Vector2 corner1, Vector2 corner2, float depth, Vector2 texCoord1, Vector2 texCoord2, Color color)
static void QueuePath(FlatBatch2D batch, DynamicArray< Vector2 > positions, DynamicArray< bool > visibility, bool loop, bool flatShading, float innerRadiusL, float innerRadiusR, float outerRadiusL, float outerRadiusR, Color innerColorL, Color innerColorR, Color outerColorL, Color outerColorR, float miterLimit, float depth=0f)
static int TrimPathStart(DynamicArray< Vector2 > positions, BoundingCircle circle)
static DynamicArray< Vector2 > TmpNormals
static int TrimPathEnd(DynamicArray< Vector2 > positions, BoundingCircle circle)
static void QueuePath(FlatBatch2D batch, DynamicArray< Vector2 > positions, DynamicArray< bool > visibility, bool loop, bool flatShading, float innerRadius, float outerRadius, Color innerColor, Color outerColor, float miterLimit, float depth=0f)
static int TrimPathStart(DynamicArray< Vector2 > positions, BoundingRectangle rectangle)
static void QueuePathInternal(TexturedBatch2D batch, DynamicArray< Point > points, DynamicArray< Vector2 > normals, DynamicArray< bool > visibility, bool loop, bool flatShading, float lengthOffset, float depth)
static DynamicArray< Vector2 > TmpPositions
static void QueuePath(TexturedBatch2D batch, DynamicArray< Vector2 > positions, DynamicArray< bool > visibility, bool loop, bool flatShading, float innerRadius, float outerRadius, Color innerColor, Color outerColor, float lengthScale, float lengthOffset, float miterLimit, float depth=0f)
static void GeneratePathNormals(DynamicArray< Vector2 > positions, DynamicArray< bool > visibility, bool loop, DynamicArray< Vector2 > normals)
static Vector2 Normal2(Vector2 d1, Vector2 d2)
static DynamicArray< Point > TmpPoints
static void QueuePathInternal(FlatBatch2D batch, DynamicArray< Point > points, DynamicArray< Vector2 > normals, DynamicArray< bool > visibility, bool loop, bool flatShading, float depth)
static void QueuePath(FlatBatch2D batch, DynamicArray< Point > points, DynamicArray< bool > visibility, bool loop, bool flatShading, float depth=0f)
static void QueuePath(FlatBatch2D batch, DynamicArray< Point > points, DynamicArray< Vector2 > normals, DynamicArray< bool > visibility, bool loop, bool flatShading, float depth=0f)
static void QueuePath(TexturedBatch2D batch, DynamicArray< Point > points, DynamicArray< Vector2 > normals, DynamicArray< bool > visibility, bool loop, bool flatShading, float lengthOffset, float depth=0f)
static void QueuePath(TexturedBatch2D batch, DynamicArray< Point > points, DynamicArray< bool > visibility, bool loop, bool flatShading, float lengthOffset, float depth=0f)
static int TrimPathEnd(DynamicArray< Vector2 > positions, BoundingRectangle rectangle)
static void QueuePath(TexturedBatch2D batch, DynamicArray< Vector2 > positions, DynamicArray< bool > visibility, bool loop, bool flatShading, float innerRadiusL, float innerRadiusR, float outerRadiusL, float outerRadiusR, Color innerColorL, Color innerColorR, Color outerColorL, Color outerColorR, float lengthScale, float lengthOffset, float miterLimit, float depth=0f)
static Vector2 Normal1(Vector2 d)
float? Intersection(BoundingRectangle rectangle)
定义 Ray2.cs:20
static float Dot(Vector2 v1, Vector2 v2)
static Vector2 Normalize(Vector2 v)
static float Angle(Vector2 v1, Vector2 v2)
float LengthSquared()
static Vector2 Perpendicular(Vector2 v)