Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
BevelledShapeRenderer.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
5 public static class BevelledShapeRenderer {
6 public struct Point : IEquatable<Point> {
8
9 public float RoundingRadius;
10
11 public int RoundingCount;
12
13 public bool Equals(Point other) {
14 if (Position == other.Position
15 && RoundingRadius == other.RoundingRadius) {
16 return RoundingCount == other.RoundingCount;
17 }
18 return false;
19 }
20 }
21
22 static FlatBatch2D TmpBatch = new();
23
24 static DynamicArray<Point> TmpQuadPoints = new();
25
26 static DynamicArray<Point> TmpPoints = new();
27
28 static DynamicArray<Vector2> TmpPositions = new();
29
30 static DynamicArray<Vector2> TmpPositions2 = new();
31
32 static DynamicArray<Vector2> TmpNormals = new();
33
34 static DynamicArray<int> TmpIndices = new();
35
36 static DynamicArray<int> TmpIndicesTriangulation = new();
37
38 static DynamicArray<PathRenderer.Point> TmpPathPoints = new();
39
40 public static void QueueShape(FlatBatch2D batch,
41 IEnumerable<Point> points,
42 float pixelsPerUnit,
43 float antialiasSize,
44 float bevelSize,
45 bool flatShading,
46 Color centerColor,
47 Color bevelColor,
48 float directional,
49 float ambient) {
50 if ((bevelColor == Color.Transparent || bevelSize == 0f)
51 && centerColor == Color.Transparent) {
52 return;
53 }
54 TmpPoints.Count = 0;
55 TmpPoints.AddRange(points);
56 TmpPositions.Count = 0;
57 RoundCorners(TmpPoints, true, pixelsPerUnit, MathF.Abs(bevelSize), TmpPositions);
59 TmpNormals.Count = 0;
61 if (bevelColor != Color.Transparent
62 && bevelSize != 0f) {
63 float num = MathF.Abs(bevelSize);
64 Vector2 directionToLight = Vector2.Normalize(bevelSize > 0f ? new Vector2(-1f, -2f) : -new Vector2(-1f, -2f));
65 TmpPathPoints.Count = 0;
68 0f,
69 0f,
70 0f,
71 num,
72 bevelColor,
73 bevelColor,
74 bevelColor,
75 bevelColor,
76 0f,
77 float.PositiveInfinity,
79 );
80 LightPoints(TmpPathPoints, true, flatShading, directionToLight, directional, ambient);
81 PathRenderer.QueuePath(batch, TmpPathPoints, TmpNormals, null, true, flatShading);
82 TmpPositions2.Count = 0;
83 for (int i = 0; i < TmpNormals.Count; i++) {
85 }
86 TmpIndices.Count = 0;
88 int count = batch.TriangleVertices.Count;
89 for (int j = 0; j < TmpPositions2.Count; j++) {
90 batch.TriangleVertices.Add(new VertexPositionColor(new Vector3(TmpPositions2[j], 0f), centerColor));
91 }
92 for (int k = 0; k < TmpIndices.Count; k++) {
93 batch.TriangleIndices.Add(TmpIndices[k] + count);
94 }
95 if (antialiasSize > 0f) {
96 TmpPathPoints.Count = 0;
97 float outerRadiusR = num + antialiasSize;
100 0f,
101 num,
102 0f,
103 outerRadiusR,
104 bevelColor,
105 bevelColor,
108 0f,
109 float.PositiveInfinity,
111 );
112 LightPoints(TmpPathPoints, true, flatShading, directionToLight, directional, ambient);
113 PathRenderer.QueuePath(batch, TmpPathPoints, TmpNormals, null, true, flatShading);
114 TmpPathPoints.Count = 0;
117 0f,
118 0f,
119 antialiasSize,
120 0f,
121 bevelColor,
122 bevelColor,
125 0f,
126 float.PositiveInfinity,
128 );
129 LightPoints(TmpPathPoints, true, flatShading, directionToLight, directional, ambient);
130 PathRenderer.QueuePath(batch, TmpPathPoints, TmpNormals, null, true, flatShading);
131 }
132 }
133 else if (centerColor != Color.Transparent) {
134 TmpIndices.Count = 0;
136 int count2 = batch.TriangleVertices.Count;
137 for (int l = 0; l < TmpPositions.Count; l++) {
138 batch.TriangleVertices.Add(new VertexPositionColor(new Vector3(TmpPositions[l], 0f), centerColor));
139 }
140 for (int m = 0; m < TmpIndices.Count; m++) {
141 batch.TriangleIndices.Add(TmpIndices[m] + count2);
142 }
143 if (antialiasSize > 0f) {
144 TmpPathPoints.Count = 0;
147 0f,
148 0f,
149 antialiasSize,
150 0f,
151 centerColor,
152 centerColor,
155 0f,
156 float.PositiveInfinity,
158 );
159 PathRenderer.QueuePath(batch, TmpPathPoints, TmpNormals, null, true, flatShading);
160 }
161 }
162 }
163
164 public static void QueueShapeShadow(FlatBatch2D batch, IEnumerable<Point> points, float pixelsPerUnit, float shadowSize, Color shadowColor) {
165 TmpPoints.Count = 0;
166 TmpPoints.AddRange(points);
167 TmpPositions.Count = 0;
168 RoundCorners(TmpPoints, true, pixelsPerUnit, shadowSize, TmpPositions);
170 TmpNormals.Count = 0;
172 TmpPathPoints.Count = 0;
175 0f,
176 0f,
177 shadowSize,
178 0f,
179 shadowColor,
183 0f,
184 float.PositiveInfinity,
186 );
187 PathRenderer.QueuePath(batch, TmpPathPoints, TmpNormals, null, true, true);
188 TmpIndices.Count = 0;
190 int count = batch.TriangleVertices.Count;
191 for (int i = 0; i < TmpPositions.Count; i++) {
192 batch.TriangleVertices.Add(new VertexPositionColor(new Vector3(TmpPositions[i], 0f), shadowColor));
193 }
194 for (int j = 0; j < TmpIndices.Count; j++) {
195 batch.TriangleIndices.Add(TmpIndices[j] + count);
196 }
197 }
198
199 public static void QueueShape(TexturedBatch2D batch,
200 IEnumerable<Point> points,
201 Vector2 textureScale,
202 Vector2 textureOffset,
203 float pixelsPerUnit,
204 float antialiasSize,
205 float bevelSize,
206 bool flatShading,
207 Color centerColor,
208 Color bevelColor,
209 float directional,
210 float ambient) {
211 TmpBatch.Clear();
213 TmpBatch,
214 points,
215 pixelsPerUnit,
216 antialiasSize,
217 bevelSize,
218 flatShading,
219 centerColor,
220 bevelColor,
221 directional,
222 ambient
223 );
224 Vector2 vector = 1f / (textureScale * new Vector2(batch.Texture.Width, batch.Texture.Height));
225 int count = batch.TriangleVertices.Count;
226 foreach (VertexPositionColor triangleVertex in TmpBatch.TriangleVertices) {
227 DynamicArray<VertexPositionColorTexture> triangleVertices = batch.TriangleVertices;
228 VertexPositionColorTexture item = new() { Position = triangleVertex.Position, Color = triangleVertex.Color };
229 Vector3 position = triangleVertex.Position;
230 item.TexCoord = position.XY * vector + textureOffset;
231 triangleVertices.Add(item);
232 }
233 foreach (int triangleIndex in TmpBatch.TriangleIndices) {
234 batch.TriangleIndices.Add(triangleIndex + count);
235 }
236 }
237
238 public static void QueueQuad(FlatBatch2D batch,
239 Vector2 p1,
240 Vector2 p2,
241 float pixelsPerUnit,
242 float antialiasSize,
243 float bevelSize,
244 float roundingRadius,
245 int roundingCount,
246 bool flatShading,
247 Color centerColor,
248 Color bevelColor,
249 float directional,
250 float ambient) {
251 TmpQuadPoints.Count = 0;
252 TmpQuadPoints.Add(new Point { Position = new Vector2(p1.X, p1.Y), RoundingRadius = roundingRadius, RoundingCount = roundingCount });
253 TmpQuadPoints.Add(new Point { Position = new Vector2(p2.X, p1.Y), RoundingRadius = roundingRadius, RoundingCount = roundingCount });
254 TmpQuadPoints.Add(new Point { Position = new Vector2(p2.X, p2.Y), RoundingRadius = roundingRadius, RoundingCount = roundingCount });
255 TmpQuadPoints.Add(new Point { Position = new Vector2(p1.X, p2.Y), RoundingRadius = roundingRadius, RoundingCount = roundingCount });
257 batch,
259 pixelsPerUnit,
260 antialiasSize,
261 bevelSize,
262 flatShading,
263 centerColor,
264 bevelColor,
265 directional,
266 ambient
267 );
268 }
269
270 public static void QueueQuad(TexturedBatch2D batch,
271 Vector2 p1,
272 Vector2 p2,
273 Vector2 textureScale,
274 Vector2 textureOffset,
275 float pixelsPerUnit,
276 float antialiasSize,
277 float bevelSize,
278 float roundingRadius,
279 int roundingCount,
280 bool flatShading,
281 Color centerColor,
282 Color bevelColor,
283 float directional,
284 float ambient) {
285 TmpQuadPoints.Count = 0;
286 TmpQuadPoints.Add(new Point { Position = new Vector2(p1.X, p1.Y), RoundingRadius = roundingRadius, RoundingCount = roundingCount });
287 TmpQuadPoints.Add(new Point { Position = new Vector2(p2.X, p1.Y), RoundingRadius = roundingRadius, RoundingCount = roundingCount });
288 TmpQuadPoints.Add(new Point { Position = new Vector2(p2.X, p2.Y), RoundingRadius = roundingRadius, RoundingCount = roundingCount });
289 TmpQuadPoints.Add(new Point { Position = new Vector2(p1.X, p2.Y), RoundingRadius = roundingRadius, RoundingCount = roundingCount });
291 batch,
293 textureScale,
294 textureOffset,
295 pixelsPerUnit,
296 antialiasSize,
297 bevelSize,
298 flatShading,
299 centerColor,
300 bevelColor,
301 directional,
302 ambient
303 );
304 }
305
306 static void RemoveDuplicates(DynamicArray<Vector2> positions) {
307 int count = 0;
308 Vector2? vector = null;
309 for (int i = 0; i < positions.Count; i++) {
310 Vector2 value = positions[i];
311 if (value != vector) {
312 positions[count++] = value;
313 vector = value;
314 }
315 }
316 positions.Count = count;
317 }
318
319 static void Triangulate(DynamicArray<Vector2> source, DynamicArray<int> destination) {
320 TmpIndicesTriangulation.Count = source.Count;
321 for (int i = 0; i < source.Count; i++) {
322 TmpIndicesTriangulation.Array[i] = i;
323 }
324 while (true) {
325 int num = TmpIndicesTriangulation.Count - 1;
326 int num2;
327 int num3;
328 int num4;
329 while (true) {
330 if (num >= 3) {
332 num3 = TmpIndicesTriangulation[num];
333 num4 = TmpIndicesTriangulation[(num + 1) % TmpIndicesTriangulation.Count];
334 Vector2 vector = source[num2];
335 Vector2 vector2 = source[num3];
336 Vector2 vector3 = source[num4];
337 if (Vector2.Cross(vector2 - vector, vector3 - vector2) >= 0f) {
338 break;
339 }
340 num--;
341 continue;
342 }
343 if (TmpIndicesTriangulation.Count == 3) {
345 }
346 return;
347 }
348 destination.Add(num2);
349 destination.Add(num3);
350 destination.Add(num4);
351 TmpIndicesTriangulation.RemoveAt(num);
352 }
353 }
354
355 static void LightPoints(DynamicArray<PathRenderer.Point> points,
356 bool loop,
357 bool flatShading,
358 Vector2 directionToLight,
359 float directional,
360 float ambient) {
361 if (flatShading) {
362 for (int i = 0; i < points.Count; i++) {
363 int index = i;
364 int index2 = loop ? (i + 1) % points.Count : MathUtils.Min(i + 1, points.Count - 1);
365 PathRenderer.Point value = points[index];
366 Vector2 v = -Vector2.Perpendicular(Vector2.Normalize(points[index2].Position - value.Position));
367 float num = directional * Vector2.Dot(v, directionToLight);
368 Color color = new(new Vector3(num + ambient));
369 value.InnerColorL *= color;
370 value.InnerColorR *= color;
371 value.OuterColorL *= color;
372 value.OuterColorR *= color;
373 points[index] = value;
374 }
375 return;
376 }
377 for (int j = 0; j < points.Count; j++) {
378 int index3 = loop ? (j - 1 + points.Count) % points.Count : MathUtils.Max(j - 1, 0);
379 int index4 = j;
380 int index5 = loop ? (j + 1) % points.Count : MathUtils.Min(j + 1, points.Count - 1);
381 PathRenderer.Point point = points[index3];
382 PathRenderer.Point value2 = points[index4];
383 PathRenderer.Point point2 = points[index5];
384 Vector2 obj = value2.Position != point.Position ? Vector2.Normalize(value2.Position - point.Position) : Vector2.Zero;
385 Vector2 vector = point2.Position != value2.Position ? Vector2.Normalize(point2.Position - value2.Position) : Vector2.Zero;
386 Vector2 v2 = -Vector2.Perpendicular(Vector2.Normalize(obj + vector));
387 Color color2 = new(new Vector3(directional * Vector2.Dot(v2, directionToLight) + ambient));
388 value2.InnerColorL *= color2;
389 value2.InnerColorR *= color2;
390 value2.OuterColorL *= color2;
391 value2.OuterColorR *= color2;
392 points[index4] = value2;
393 }
394 }
395
396 static void GeneratePathPoints(DynamicArray<Vector2> positions,
397 float innerRadiusL,
398 float innerRadiusR,
399 float outerRadiusL,
400 float outerRadiusR,
401 Color innerColorL,
402 Color innerColorR,
403 Color outerColorL,
404 Color outerColorR,
405 float lengthScale,
406 float miterLimit,
407 DynamicArray<PathRenderer.Point> result) {
408 foreach (Vector2 position in positions) {
409 result.Add(
410 new PathRenderer.Point {
411 Position = position,
412 InnerRadiusL = innerRadiusL,
413 InnerRadiusR = innerRadiusR,
414 OuterRadiusL = outerRadiusL,
415 OuterRadiusR = outerRadiusR,
416 InnerColorL = innerColorL,
417 InnerColorR = innerColorR,
418 OuterColorL = outerColorL,
419 OuterColorR = outerColorR,
420 LengthScale = lengthScale,
421 MiterLimit = miterLimit
422 }
423 );
424 }
425 }
426
427 static void RoundCorners(DynamicArray<Point> points, bool loop, float pixelsPerUnit, float bevelSize, DynamicArray<Vector2> result) {
428 int num = loop ? points.Count : points.Count - 2;
429 for (int i = 0; i < num; i++) {
430 Point point = points[i];
431 Point point2 = points[(i + 1) % points.Count];
432 Point point3 = points[(i + 2) % points.Count];
434 point.Position,
435 point2.Position,
436 point3.Position,
437 point2.RoundingRadius,
438 point2.RoundingCount,
439 pixelsPerUnit,
440 bevelSize,
441 result
442 );
443 }
444 }
445
446 static void RoundCorner(Vector2 p0,
447 Vector2 p1,
448 Vector2 p2,
449 float radius,
450 int count,
451 float pixelsPerUnit,
452 float bevelSize,
453 DynamicArray<Vector2> result) {
454 Vector2 vector = p0 - p1;
455 Vector2 vector2 = p2 - p1;
456 float num = vector.Length();
457 float num2 = vector2.Length();
458 float num3 = MathUtils.Min(num * 0.49f, num2 * 0.49f, radius);
459 if (num3 > 0f) {
460 Vector2 vector3 = vector / num;
461 Vector2 vector4 = vector2 / num2;
462 Vector2 vector5 = p1 + vector3 * num3;
463 Vector2 vector6 = p1 + vector4 * num3;
464 Vector2 v = p1 - vector5;
465 Vector2 v2 = p1 - vector6;
466 Line2 l = new(vector5, vector5 + Vector2.Perpendicular(v));
467 Line2 l2 = new(vector6, vector6 + Vector2.Perpendicular(v2));
468 Vector2? vector7 = Line2.Intersection(l, l2);
469 if (vector7.HasValue) {
470 float num4 = Vector2.Distance(vector7.Value, vector5);
471 if (count < 0) {
472 float num5 = 0.25f;
473 float num6 = (num4 + bevelSize) * pixelsPerUnit;
474 float num7 = MathF.Acos(1f - num5 / num6);
475 count = !float.IsNaN(num7) ? (int)Math.Clamp(MathF.Ceiling(((float)Math.PI / num7 - 4f) / 4f), 0f, 50f) : 0;
476 }
477 float num8 = Vector2.Angle(vector5 - vector7.Value, Vector2.UnitY);
478 float num9 = MathUtils.NormalizeAngle(Vector2.Angle(vector6 - vector7.Value, Vector2.UnitY) - num8);
479 result.Add(vector5);
480 for (int i = 1; i <= count; i++) {
481 float x = num8 + num9 * i / (count + 1);
482 Vector2 item = vector7.Value + new Vector2(num4 * MathF.Sin(x), num4 * MathF.Cos(x));
483 result.Add(item);
484 }
485 result.Add(vector6);
486 return;
487 }
488 }
489 result.Add(p1);
490 }
491 }
492}
Engine.Vector3 Vector3
void AddRange(IEnumerable< T > items)
readonly DynamicArray< int > TriangleIndices
readonly DynamicArray< VertexPositionColor > TriangleVertices
readonly DynamicArray< VertexPositionColorTexture > TriangleVertices
static int Min(int x1, int x2)
static float NormalizeAngle(float angle)
static int Max(int x1, int x2)
static void GeneratePathPoints(DynamicArray< Vector2 > positions, float innerRadiusL, float innerRadiusR, float outerRadiusL, float outerRadiusR, Color innerColorL, Color innerColorR, Color outerColorL, Color outerColorR, float lengthScale, float miterLimit, DynamicArray< PathRenderer.Point > result)
static void LightPoints(DynamicArray< PathRenderer.Point > points, bool loop, bool flatShading, Vector2 directionToLight, float directional, float ambient)
static void QueueQuad(FlatBatch2D batch, Vector2 p1, Vector2 p2, float pixelsPerUnit, float antialiasSize, float bevelSize, float roundingRadius, int roundingCount, bool flatShading, Color centerColor, Color bevelColor, float directional, float ambient)
static void QueueShapeShadow(FlatBatch2D batch, IEnumerable< Point > points, float pixelsPerUnit, float shadowSize, Color shadowColor)
static DynamicArray< Vector2 > TmpPositions
static void Triangulate(DynamicArray< Vector2 > source, DynamicArray< int > destination)
static void QueueShape(FlatBatch2D batch, IEnumerable< Point > points, float pixelsPerUnit, float antialiasSize, float bevelSize, bool flatShading, Color centerColor, Color bevelColor, float directional, float ambient)
static DynamicArray< Vector2 > TmpNormals
static void RoundCorners(DynamicArray< Point > points, bool loop, float pixelsPerUnit, float bevelSize, DynamicArray< Vector2 > result)
static DynamicArray< Point > TmpPoints
static void QueueShape(TexturedBatch2D batch, IEnumerable< Point > points, Vector2 textureScale, Vector2 textureOffset, float pixelsPerUnit, float antialiasSize, float bevelSize, bool flatShading, Color centerColor, Color bevelColor, float directional, float ambient)
static DynamicArray< int > TmpIndicesTriangulation
static DynamicArray< int > TmpIndices
static DynamicArray< PathRenderer.Point > TmpPathPoints
static void RemoveDuplicates(DynamicArray< Vector2 > positions)
static DynamicArray< Point > TmpQuadPoints
static void RoundCorner(Vector2 p0, Vector2 p1, Vector2 p2, float radius, int count, float pixelsPerUnit, float bevelSize, DynamicArray< Vector2 > result)
static DynamicArray< Vector2 > TmpPositions2
static void QueueQuad(TexturedBatch2D batch, Vector2 p1, Vector2 p2, Vector2 textureScale, Vector2 textureOffset, float pixelsPerUnit, float antialiasSize, float bevelSize, float roundingRadius, int roundingCount, bool flatShading, Color centerColor, Color bevelColor, float directional, float ambient)
static void GeneratePathNormals(DynamicArray< Vector2 > positions, DynamicArray< bool > visibility, bool loop, DynamicArray< Vector2 > normals)
static void QueuePath(FlatBatch2D batch, DynamicArray< Point > points, DynamicArray< bool > visibility, bool loop, bool flatShading, float depth=0f)
static Color Transparent
定义 Color.cs:5
static ? Vector2 Intersection(Line2 l1, Line2 l2)
static float Dot(Vector2 v1, Vector2 v2)
static float Cross(Vector2 v1, Vector2 v2)
static readonly Vector2 Zero
static readonly Vector2 UnitY
static Vector2 Normalize(Vector2 v)
static float Angle(Vector2 v1, Vector2 v2)
static float Distance(Vector2 v1, Vector2 v2)
static Vector2 Perpendicular(Vector2 v)