summaryrefslogtreecommitdiff
path: root/Thronefall/Ara/AraTrail.cs
blob: e5ade61b4303a9c8e5c92fe41a22cb20654be570 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Serialization;

namespace Ara;

[ExecuteInEditMode]
public class AraTrail : MonoBehaviour
{
	public enum TrailAlignment
	{
		View,
		Velocity,
		Local
	}

	public enum TrailSpace
	{
		World,
		Self,
		Custom
	}

	public enum TrailSorting
	{
		OlderOnTop,
		NewerOnTop
	}

	public enum Timescale
	{
		Normal,
		Unscaled
	}

	public enum TextureMode
	{
		Stretch,
		Tile,
		WorldTile
	}

	public struct CurveFrame
	{
		public Vector3 position;

		public Vector3 normal;

		public Vector3 bitangent;

		public Vector3 tangent;

		public CurveFrame(Vector3 position, Vector3 normal, Vector3 bitangent, Vector3 tangent)
		{
			this.position = position;
			this.normal = normal;
			this.bitangent = bitangent;
			this.tangent = tangent;
		}

		public Vector3 Transport(Vector3 newTangent, Vector3 newPosition)
		{
			Vector3 vector = newPosition - position;
			float num = Vector3.Dot(vector, vector);
			Vector3 vector2 = normal - 2f / (num + 1E-05f) * Vector3.Dot(vector, normal) * vector;
			Vector3 vector3 = tangent - 2f / (num + 1E-05f) * Vector3.Dot(vector, tangent) * vector;
			Vector3 vector4 = newTangent - vector3;
			float num2 = Vector3.Dot(vector4, vector4);
			Vector3 rhs = vector2 - 2f / (num2 + 1E-05f) * Vector3.Dot(vector4, vector2) * vector4;
			Vector3 vector5 = Vector3.Cross(newTangent, rhs);
			normal = rhs;
			bitangent = vector5;
			tangent = newTangent;
			position = newPosition;
			return normal;
		}
	}

	public struct Point
	{
		public Vector3 position;

		public Vector3 velocity;

		public Vector3 tangent;

		public Vector3 normal;

		public Color color;

		public float thickness;

		public float life;

		public float texcoord;

		public bool discontinuous;

		public Point(Vector3 position, Vector3 velocity, Vector3 tangent, Vector3 normal, Color color, float thickness, float texcoord, float lifetime)
		{
			this.position = position;
			this.velocity = velocity;
			this.tangent = tangent;
			this.normal = normal;
			this.color = color;
			this.thickness = thickness;
			life = lifetime;
			this.texcoord = texcoord;
			discontinuous = false;
		}

		public static float CatmullRom(float p0, float p1, float p2, float p3, float t)
		{
			float num = t * t;
			return 0.5f * (2f * p1 + (0f - p0 + p2) * t + (2f * p0 - 5f * p1 + 4f * p2 - p3) * num + (0f - p0 + 3f * p1 - 3f * p2 + p3) * num * t);
		}

		public static Point operator +(Point p1, Point p2)
		{
			return new Point(p1.position + p2.position, p1.velocity + p2.velocity, p1.tangent + p2.tangent, p1.normal + p2.normal, p1.color + p2.color, p1.thickness + p2.thickness, p1.texcoord + p2.texcoord, p1.life + p2.life);
		}

		public static Point operator -(Point p1, Point p2)
		{
			return new Point(p1.position - p2.position, p1.velocity - p2.velocity, p1.tangent - p2.tangent, p1.normal - p2.normal, p1.color - p2.color, p1.thickness - p2.thickness, p1.texcoord - p2.texcoord, p1.life - p2.life);
		}
	}

	public const float epsilon = 1E-05f;

	[Header("Overall")]
	[Tooltip("Trail cross-section asset, determines the shape of the emitted trail. If no asset is specified, the trail will be a simple strip.")]
	public TrailSection section;

	[Tooltip("Whether to use world or local space to generate and simulate the trail.")]
	public TrailSpace space;

	[Tooltip("Custom space to use when generating and simulating the trail")]
	public Transform customSpace;

	[Tooltip("Whether to use regular time.")]
	public Timescale timescale;

	[Tooltip("How to align the trail geometry: facing the camera (view) of using the transform's rotation (local).")]
	public TrailAlignment alignment;

	[Tooltip("Determines the order in which trail points will be rendered.")]
	public TrailSorting sorting;

	[Tooltip("Thickness multiplier, in meters.")]
	public float thickness = 0.1f;

	[Tooltip("Amount of smoothing iterations applied to the trail shape.")]
	[Range(1f, 8f)]
	public int smoothness = 1;

	[Tooltip("Calculate accurate thickness at sharp corners.")]
	public bool highQualityCorners;

	[Range(0f, 12f)]
	public int cornerRoundness = 5;

	[Header("Length")]
	[Tooltip("How should the thickness of the curve evolve over its lenght. The horizontal axis is normalized lenght (in the [0,1] range) and the vertical axis is a thickness multiplier.")]
	[FormerlySerializedAs("thicknessOverLenght")]
	public AnimationCurve thicknessOverLength = AnimationCurve.Linear(0f, 1f, 0f, 1f);

	[Tooltip("How should vertex color evolve over the trail's length.")]
	[FormerlySerializedAs("colorOverLenght")]
	public Gradient colorOverLength = new Gradient();

	[Header("Time")]
	[Tooltip("How should the thickness of the curve evolve with its lifetime. The horizontal axis is normalized lifetime (in the [0,1] range) and the vertical axis is a thickness multiplier.")]
	public AnimationCurve thicknessOverTime = AnimationCurve.Linear(0f, 1f, 0f, 1f);

	[Tooltip("How should vertex color evolve over the trail's lifetime.")]
	public Gradient colorOverTime = new Gradient();

	[Header("Emission")]
	public bool emit = true;

	[Tooltip("Initial thickness of trail points when they are first spawned.")]
	public float initialThickness = 1f;

	[Tooltip("Initial color of trail points when they are first spawned.")]
	public Color initialColor = Color.white;

	[Tooltip("Initial velocity of trail points when they are first spawned.")]
	public Vector3 initialVelocity = Vector3.zero;

	[Tooltip("Minimum amount of time (in seconds) that must pass before spawning a new point.")]
	public float timeInterval = 0.025f;

	[Tooltip("Minimum distance (in meters) that must be left between consecutive points in the trail.")]
	public float minDistance = 0.025f;

	[Tooltip("Duration of the trail (in seconds).")]
	public float time = 2f;

	[Header("Physics")]
	[Tooltip("Toggles trail physics.")]
	public bool enablePhysics;

	[Tooltip("Amount of seconds pre-simulated before the trail appears. Useful when you want a trail to be already simulating when the game starts.")]
	public float warmup;

	[Tooltip("Gravity affecting the trail.")]
	public Vector3 gravity = Vector3.zero;

	[Tooltip("Amount of speed transferred from the transform to the trail. 0 means no velocity is transferred, 1 means 100% of the velocity is transferred.")]
	[Range(0f, 1f)]
	public float inertia;

	[Tooltip("Amount of temporal smoothing applied to the velocity transferred from the transform to the trail.")]
	[Range(0f, 1f)]
	public float velocitySmoothing = 0.75f;

	[Tooltip("Amount of damping applied to the trail's velocity. Larger values will slow down the trail more as time passes.")]
	[Range(0f, 1f)]
	public float damping = 0.75f;

	[Header("Rendering")]
	public Material[] materials = new Material[1];

	public ShadowCastingMode castShadows = ShadowCastingMode.On;

	public bool receiveShadows = true;

	public bool useLightProbes = true;

	[Header("Texture")]
	[Tooltip("Quad mapping will send the shader an extra coordinate for each vertex, that can be used to correct UV distortion using tex2Dproj.")]
	public bool quadMapping;

	[Tooltip("How to apply the texture over the trail: stretch it all over its lenght, or tile it.")]
	public TextureMode textureMode;

	[Tooltip("Defines how many times are U coords repeated across the length of the trail.")]
	public float uvFactor = 1f;

	[Tooltip("Defines how many times are V coords repeated trough the width of the trail.")]
	public float uvWidthFactor = 1f;

	[Tooltip("When the texture mode is set to 'Tile', defines where to begin tiling from: 0 means the start of the trail, 1 means the end.")]
	[Range(0f, 1f)]
	public float tileAnchor = 1f;

	[HideInInspector]
	public ElasticArray<Point> points = new ElasticArray<Point>();

	private ElasticArray<Point> renderablePoints = new ElasticArray<Point>();

	private List<int> discontinuities = new List<int>();

	private Mesh mesh_;

	private Vector3 velocity = Vector3.zero;

	private Vector3 prevPosition;

	private float accumTime;

	private List<Vector3> vertices = new List<Vector3>();

	private List<Vector3> normals = new List<Vector3>();

	private List<Vector4> tangents = new List<Vector4>();

	private List<Vector4> uvs = new List<Vector4>();

	private List<Color> vertColors = new List<Color>();

	private List<int> tris = new List<int>();

	private Vector3 nextV = Vector3.zero;

	private Vector3 prevV = Vector3.zero;

	private Vector3 vertex = Vector3.zero;

	private Vector3 normal = Vector3.zero;

	private Vector3 bitangent = Vector3.zero;

	private Vector4 tangent = new Vector4(0f, 0f, 0f, 1f);

	private Vector4 texTangent = Vector4.zero;

	private Vector4 uv = Vector4.zero;

	private Color color;

	private Action<ScriptableRenderContext, Camera> renderCallback;

	public Vector3 Velocity => velocity;

	private float DeltaTime
	{
		get
		{
			if (timescale != Timescale.Unscaled)
			{
				return Time.deltaTime;
			}
			return Time.unscaledDeltaTime;
		}
	}

	private float FixedDeltaTime
	{
		get
		{
			if (timescale != Timescale.Unscaled)
			{
				return Time.fixedDeltaTime;
			}
			return Time.fixedUnscaledDeltaTime;
		}
	}

	public Mesh mesh => mesh_;

	public Matrix4x4 worldToTrail
	{
		get
		{
			switch (space)
			{
			case TrailSpace.World:
				return Matrix4x4.identity;
			case TrailSpace.Self:
				return base.transform.worldToLocalMatrix;
			case TrailSpace.Custom:
				if (!(customSpace != null))
				{
					return Matrix4x4.identity;
				}
				return customSpace.worldToLocalMatrix;
			default:
				return Matrix4x4.identity;
			}
		}
	}

	public event Action onUpdatePoints;

	public void OnValidate()
	{
		time = Mathf.Max(time, 1E-05f);
		warmup = Mathf.Max(0f, warmup);
	}

	public void Awake()
	{
		Warmup();
	}

	private void OnEnable()
	{
		prevPosition = base.transform.position;
		velocity = Vector3.zero;
		mesh_ = new Mesh();
		mesh_.name = "ara_trail_mesh";
		mesh_.MarkDynamic();
		AttachToCameraRendering();
	}

	private void OnDisable()
	{
		UnityEngine.Object.DestroyImmediate(mesh_);
		DetachFromCameraRendering();
	}

	private void AttachToCameraRendering()
	{
		renderCallback = delegate(ScriptableRenderContext cntxt, Camera cam)
		{
			UpdateTrailMesh(cam);
		};
		RenderPipelineManager.beginCameraRendering += renderCallback;
		Camera.onPreCull = (Camera.CameraCallback)Delegate.Combine(Camera.onPreCull, new Camera.CameraCallback(UpdateTrailMesh));
	}

	private void DetachFromCameraRendering()
	{
		RenderPipelineManager.beginCameraRendering -= renderCallback;
		Camera.onPreCull = (Camera.CameraCallback)Delegate.Remove(Camera.onPreCull, new Camera.CameraCallback(UpdateTrailMesh));
	}

	public void Clear()
	{
		points.Clear();
	}

	private void UpdateVelocity()
	{
		if (DeltaTime > 0f)
		{
			velocity = Vector3.Lerp((base.transform.position - prevPosition) / DeltaTime, velocity, velocitySmoothing);
		}
		prevPosition = base.transform.position;
	}

	private void LateUpdate()
	{
		UpdateVelocity();
		EmissionStep(DeltaTime);
		SnapLastPointToTransform();
		UpdatePointsLifecycle();
		if (this.onUpdatePoints != null)
		{
			this.onUpdatePoints();
		}
	}

	private void EmissionStep(float time)
	{
		accumTime += time;
		if (accumTime >= timeInterval && emit)
		{
			Vector3 vector = worldToTrail.MultiplyPoint3x4(base.transform.position);
			if (points.Count <= 1 || Vector3.Distance(vector, points[points.Count - 2].position) >= minDistance)
			{
				EmitPoint(vector);
				accumTime = 0f;
			}
		}
	}

	private void Warmup()
	{
		if (!Application.isPlaying || !enablePhysics)
		{
			return;
		}
		for (float num = warmup; num > FixedDeltaTime; num -= FixedDeltaTime)
		{
			PhysicsStep(FixedDeltaTime);
			EmissionStep(FixedDeltaTime);
			SnapLastPointToTransform();
			UpdatePointsLifecycle();
			if (this.onUpdatePoints != null)
			{
				this.onUpdatePoints();
			}
		}
	}

	private void PhysicsStep(float timestep)
	{
		float num = Mathf.Pow(1f - Mathf.Clamp01(damping), timestep);
		for (int i = 0; i < points.Count; i++)
		{
			Point value = points[i];
			value.velocity += gravity * timestep;
			value.velocity *= num;
			value.position += value.velocity * timestep;
			points[i] = value;
		}
	}

	private void FixedUpdate()
	{
		if (enablePhysics)
		{
			PhysicsStep(FixedDeltaTime);
		}
	}

	public void EmitPoint(Vector3 position)
	{
		float texcoord = 0f;
		if (points.Count > 0)
		{
			texcoord = points[points.Count - 1].texcoord + Vector3.Distance(position, points[points.Count - 1].position);
		}
		points.Add(new Point(position, initialVelocity + velocity * inertia, base.transform.right, base.transform.forward, initialColor, initialThickness, texcoord, time));
	}

	private void SnapLastPointToTransform()
	{
		if (points.Count <= 0)
		{
			return;
		}
		Point value = points[points.Count - 1];
		if (!emit)
		{
			value.discontinuous = true;
		}
		if (!value.discontinuous)
		{
			value.position = worldToTrail.MultiplyPoint3x4(base.transform.position);
			value.normal = base.transform.forward;
			value.tangent = base.transform.right;
			if (points.Count > 1)
			{
				value.texcoord = points[points.Count - 2].texcoord + Vector3.Distance(value.position, points[points.Count - 2].position);
			}
		}
		points[points.Count - 1] = value;
	}

	private void UpdatePointsLifecycle()
	{
		for (int num = points.Count - 1; num >= 0; num--)
		{
			Point value = points[num];
			value.life -= DeltaTime;
			points[num] = value;
			if (value.life <= 0f)
			{
				if (smoothness <= 1)
				{
					points.RemoveAt(num);
				}
				else if (points[Mathf.Min(num + 1, points.Count - 1)].life <= 0f && points[Mathf.Min(num + 2, points.Count - 1)].life <= 0f)
				{
					points.RemoveAt(num);
				}
			}
		}
	}

	private void ClearMeshData()
	{
		mesh_.Clear();
		vertices.Clear();
		normals.Clear();
		tangents.Clear();
		uvs.Clear();
		vertColors.Clear();
		tris.Clear();
	}

	private void CommitMeshData()
	{
		mesh_.SetVertices(vertices);
		mesh_.SetNormals(normals);
		mesh_.SetTangents(tangents);
		mesh_.SetColors(vertColors);
		mesh_.SetUVs(0, uvs);
		mesh_.SetTriangles(tris, 0, calculateBounds: true);
	}

	private void RenderMesh(Camera cam)
	{
		Matrix4x4 inverse = worldToTrail.inverse;
		for (int i = 0; i < materials.Length; i++)
		{
			Graphics.DrawMesh(mesh_, inverse, materials[i], base.gameObject.layer, cam, 0, null, castShadows, receiveShadows, null, useLightProbes);
		}
	}

	public float GetLenght(ElasticArray<Point> input)
	{
		float num = 0f;
		for (int i = 0; i < input.Count - 1; i++)
		{
			num += Vector3.Distance(input[i].position, input[i + 1].position);
		}
		return num;
	}

	private ElasticArray<Point> GetRenderablePoints(int start, int end)
	{
		renderablePoints.Clear();
		if (smoothness <= 1)
		{
			for (int i = start; i <= end; i++)
			{
				renderablePoints.Add(points[i]);
			}
			return renderablePoints;
		}
		Point[] data = points.Data;
		float num = 1f / (float)smoothness;
		Point item = new Point(Vector3.zero, Vector3.zero, Vector3.zero, Vector3.zero, Color.white, 0f, 0f, 0f);
		for (int j = start; j < end; j++)
		{
			int num2 = ((j == start) ? start : (j - 1));
			int num3 = ((j == end - 1) ? end : (j + 2));
			int num4 = j + 1;
			float p = data[num2].position[0];
			float p2 = data[num2].position[1];
			float p3 = data[num2].position[2];
			float p4 = data[num2].velocity[0];
			float p5 = data[num2].velocity[1];
			float p6 = data[num2].velocity[2];
			float p7 = data[num2].tangent[0];
			float p8 = data[num2].tangent[1];
			float p9 = data[num2].tangent[2];
			float p10 = data[num2].normal[0];
			float p11 = data[num2].normal[1];
			float p12 = data[num2].normal[2];
			float p13 = data[num2].color[0];
			float p14 = data[num2].color[1];
			float p15 = data[num2].color[2];
			float p16 = data[num2].color[3];
			float p17 = data[j].position[0];
			float p18 = data[j].position[1];
			float p19 = data[j].position[2];
			float p20 = data[j].velocity[0];
			float p21 = data[j].velocity[1];
			float p22 = data[j].velocity[2];
			float p23 = data[j].tangent[0];
			float p24 = data[j].tangent[1];
			float p25 = data[j].tangent[2];
			float p26 = data[j].normal[0];
			float p27 = data[j].normal[1];
			float p28 = data[j].normal[2];
			float p29 = data[j].color[0];
			float p30 = data[j].color[1];
			float p31 = data[j].color[2];
			float p32 = data[j].color[3];
			float p33 = data[num4].position[0];
			float p34 = data[num4].position[1];
			float p35 = data[num4].position[2];
			float p36 = data[num4].velocity[0];
			float p37 = data[num4].velocity[1];
			float p38 = data[num4].velocity[2];
			float p39 = data[num4].tangent[0];
			float p40 = data[num4].tangent[1];
			float p41 = data[num4].tangent[2];
			float p42 = data[num4].normal[0];
			float p43 = data[num4].normal[1];
			float p44 = data[num4].normal[2];
			float p45 = data[num4].color[0];
			float p46 = data[num4].color[1];
			float p47 = data[num4].color[2];
			float p48 = data[num4].color[3];
			float p49 = data[num3].position[0];
			float p50 = data[num3].position[1];
			float p51 = data[num3].position[2];
			float p52 = data[num3].velocity[0];
			float p53 = data[num3].velocity[1];
			float p54 = data[num3].velocity[2];
			float p55 = data[num3].tangent[0];
			float p56 = data[num3].tangent[1];
			float p57 = data[num3].tangent[2];
			float p58 = data[num3].normal[0];
			float p59 = data[num3].normal[1];
			float p60 = data[num3].normal[2];
			float p61 = data[num3].color[0];
			float p62 = data[num3].color[1];
			float p63 = data[num3].color[2];
			float p64 = data[num3].color[3];
			for (int k = 0; k < smoothness; k++)
			{
				float t = (float)k * num;
				item.life = Point.CatmullRom(data[num2].life, data[j].life, data[num4].life, data[num3].life, t);
				if (item.life > 0f)
				{
					item.position.x = Point.CatmullRom(p, p17, p33, p49, t);
					item.position.y = Point.CatmullRom(p2, p18, p34, p50, t);
					item.position.z = Point.CatmullRom(p3, p19, p35, p51, t);
					item.velocity.x = Point.CatmullRom(p4, p20, p36, p52, t);
					item.velocity.y = Point.CatmullRom(p5, p21, p37, p53, t);
					item.velocity.z = Point.CatmullRom(p6, p22, p38, p54, t);
					item.tangent.x = Point.CatmullRom(p7, p23, p39, p55, t);
					item.tangent.y = Point.CatmullRom(p8, p24, p40, p56, t);
					item.tangent.z = Point.CatmullRom(p9, p25, p41, p57, t);
					item.normal.x = Point.CatmullRom(p10, p26, p42, p58, t);
					item.normal.y = Point.CatmullRom(p11, p27, p43, p59, t);
					item.normal.z = Point.CatmullRom(p12, p28, p44, p60, t);
					item.color.r = Point.CatmullRom(p13, p29, p45, p61, t);
					item.color.g = Point.CatmullRom(p14, p30, p46, p62, t);
					item.color.b = Point.CatmullRom(p15, p31, p47, p63, t);
					item.color.a = Point.CatmullRom(p16, p32, p48, p64, t);
					item.thickness = Point.CatmullRom(data[num2].thickness, data[j].thickness, data[num4].thickness, data[num3].thickness, t);
					item.texcoord = Point.CatmullRom(data[num2].texcoord, data[j].texcoord, data[num4].texcoord, data[num3].texcoord, t);
					renderablePoints.Add(item);
				}
			}
		}
		if (points[end].life > 0f)
		{
			renderablePoints.Add(points[end]);
		}
		return renderablePoints;
	}

	private CurveFrame InitializeCurveFrame(Vector3 point, Vector3 nextPoint)
	{
		Vector3 vector = nextPoint - point;
		if (Mathf.Approximately(Mathf.Abs(Vector3.Dot(vector.normalized, base.transform.forward)), 1f))
		{
			vector += base.transform.right * 0.01f;
		}
		return new CurveFrame(point, base.transform.forward, base.transform.up, vector);
	}

	private void UpdateTrailMesh(Camera cam)
	{
		if ((cam.cullingMask & (1 << base.gameObject.layer)) == 0)
		{
			return;
		}
		ClearMeshData();
		if (points.Count <= 1)
		{
			return;
		}
		Vector3 localCamPosition = worldToTrail.MultiplyPoint3x4(cam.transform.position);
		discontinuities.Clear();
		for (int i = 0; i < points.Count; i++)
		{
			if (points[i].discontinuous || i == points.Count - 1)
			{
				discontinuities.Add(i);
			}
		}
		int start = 0;
		for (int j = 0; j < discontinuities.Count; j++)
		{
			UpdateSegmentMesh(start, discontinuities[j], localCamPosition);
			start = discontinuities[j] + 1;
		}
		CommitMeshData();
		RenderMesh(cam);
	}

	private void UpdateSegmentMesh(int start, int end, Vector3 localCamPosition)
	{
		ElasticArray<Point> elasticArray = GetRenderablePoints(start, end);
		if (sorting == TrailSorting.NewerOnTop)
		{
			elasticArray.Reverse();
		}
		Point[] data = elasticArray.Data;
		if (elasticArray.Count <= 1)
		{
			return;
		}
		float num = Mathf.Max(GetLenght(elasticArray), 1E-05f);
		float num2 = 0f;
		float num3 = ((textureMode == TextureMode.Stretch) ? 0f : ((0f - uvFactor) * num * tileAnchor));
		if (sorting == TrailSorting.NewerOnTop)
		{
			num3 = 1f - num3;
		}
		CurveFrame frame = InitializeCurveFrame(data[elasticArray.Count - 1].position, data[elasticArray.Count - 2].position);
		int va = 1;
		int vb = 0;
		for (int num4 = elasticArray.Count - 1; num4 >= 0; num4--)
		{
			int num5 = Mathf.Max(num4 - 1, 0);
			int num6 = Mathf.Min(num4 + 1, elasticArray.Count - 1);
			nextV.x = data[num5].position.x - data[num4].position.x;
			nextV.y = data[num5].position.y - data[num4].position.y;
			nextV.z = data[num5].position.z - data[num4].position.z;
			prevV.x = data[num4].position.x - data[num6].position.x;
			prevV.y = data[num4].position.y - data[num6].position.y;
			prevV.z = data[num4].position.z - data[num6].position.z;
			float num7 = ((num5 == num4) ? prevV.magnitude : nextV.magnitude);
			nextV.Normalize();
			prevV.Normalize();
			if (alignment == TrailAlignment.Local)
			{
				tangent = data[num4].tangent.normalized;
			}
			else
			{
				tangent.x = (nextV.x + prevV.x) * 0.5f;
				tangent.y = (nextV.y + prevV.y) * 0.5f;
				tangent.z = (nextV.z + prevV.z) * 0.5f;
			}
			normal = data[num4].normal;
			if (alignment != TrailAlignment.Local)
			{
				normal = ((alignment == TrailAlignment.View) ? (localCamPosition - data[num4].position) : frame.Transport(tangent, data[num4].position));
			}
			normal.Normalize();
			if (alignment == TrailAlignment.Velocity)
			{
				bitangent = frame.bitangent;
			}
			else
			{
				bitangent.x = tangent.y * normal.z - tangent.z * normal.y;
				bitangent.y = tangent.z * normal.x - tangent.x * normal.z;
				bitangent.z = tangent.x * normal.y - tangent.y * normal.x;
			}
			bitangent.Normalize();
			float num8 = ((sorting == TrailSorting.OlderOnTop) ? (num2 / num) : ((num - num2) / num));
			float num9 = Mathf.Clamp01(1f - data[num4].life / time);
			num2 += num7;
			color = data[num4].color * colorOverTime.Evaluate(num9) * colorOverLength.Evaluate(num8);
			float sectionThickness = thickness * data[num4].thickness * thicknessOverTime.Evaluate(num9) * thicknessOverLength.Evaluate(num8);
			if (textureMode == TextureMode.WorldTile)
			{
				num3 = tileAnchor + data[num4].texcoord * uvFactor;
			}
			if (section != null)
			{
				AppendSection(data, ref frame, num4, elasticArray.Count, sectionThickness, num3);
			}
			else
			{
				AppendFlatTrail(data, ref frame, num4, elasticArray.Count, sectionThickness, num3, ref va, ref vb);
			}
			float num10 = ((textureMode == TextureMode.Stretch) ? (num7 / num) : num7);
			num3 += uvFactor * ((sorting == TrailSorting.NewerOnTop) ? (0f - num10) : num10);
		}
	}

	private void AppendSection(Point[] data, ref CurveFrame frame, int i, int count, float sectionThickness, float vCoord)
	{
		int segments = section.Segments;
		int num = segments + 1;
		for (int j = 0; j <= segments; j++)
		{
			normal.x = (section.vertices[j].x * bitangent.x + section.vertices[j].y * tangent.x) * sectionThickness;
			normal.y = (section.vertices[j].x * bitangent.y + section.vertices[j].y * tangent.y) * sectionThickness;
			normal.z = (section.vertices[j].x * bitangent.z + section.vertices[j].y * tangent.z) * sectionThickness;
			vertex.x = data[i].position.x + normal.x;
			vertex.y = data[i].position.y + normal.y;
			vertex.z = data[i].position.z + normal.z;
			texTangent.x = 0f - (normal.y * frame.tangent.z - normal.z * frame.tangent.y);
			texTangent.y = 0f - (normal.z * frame.tangent.x - normal.x * frame.tangent.z);
			texTangent.z = 0f - (normal.x * frame.tangent.y - normal.y * frame.tangent.x);
			texTangent.w = 1f;
			uv.x = (float)j / (float)segments * uvWidthFactor;
			uv.y = vCoord;
			uv.z = 0f;
			uv.w = 1f;
			vertices.Add(vertex);
			normals.Add(normal);
			tangents.Add(texTangent);
			uvs.Add(uv);
			vertColors.Add(color);
			if (j < segments && i < count - 1)
			{
				tris.Add(i * num + j);
				tris.Add(i * num + (j + 1));
				tris.Add((i + 1) * num + j);
				tris.Add(i * num + (j + 1));
				tris.Add((i + 1) * num + (j + 1));
				tris.Add((i + 1) * num + j);
			}
		}
	}

	private void AppendFlatTrail(Point[] data, ref CurveFrame frame, int i, int count, float sectionThickness, float vCoord, ref int va, ref int vb)
	{
		bool num = highQualityCorners && alignment != TrailAlignment.Local;
		Quaternion quaternion = Quaternion.identity;
		Vector3 vector = Vector3.zero;
		float num2 = 0f;
		float num3 = sectionThickness;
		Vector3 vector2 = bitangent;
		if (num)
		{
			Vector3 vector3 = ((i == 0) ? bitangent : Vector3.Cross(nextV, Vector3.Cross(bitangent, tangent)).normalized);
			if (cornerRoundness > 0)
			{
				vector2 = ((i == count - 1) ? (-bitangent) : Vector3.Cross(prevV, Vector3.Cross(bitangent, tangent)).normalized);
				num2 = ((i == 0 || i == count - 1) ? 1f : Mathf.Sign(Vector3.Dot(nextV, -vector2)));
				float num4 = ((i == 0 || i == count - 1) ? MathF.PI : Mathf.Acos(Mathf.Clamp(Vector3.Dot(vector3, vector2), -1f, 1f)));
				quaternion = Quaternion.AngleAxis(57.29578f * num4 / (float)cornerRoundness, normal * num2);
				vector = vector2 * sectionThickness * num2;
			}
			if (vector3.sqrMagnitude > 0.1f)
			{
				num3 = sectionThickness / Mathf.Max(Vector3.Dot(bitangent, vector3), 0.15f);
			}
		}
		if (num && cornerRoundness > 0)
		{
			if (num2 > 0f)
			{
				vertices.Add(data[i].position + vector2 * sectionThickness);
				vertices.Add(data[i].position - bitangent * num3);
			}
			else
			{
				vertices.Add(data[i].position + bitangent * num3);
				vertices.Add(data[i].position - vector2 * sectionThickness);
			}
		}
		else
		{
			vertices.Add(data[i].position + bitangent * num3);
			vertices.Add(data[i].position - bitangent * num3);
		}
		normals.Add(normal);
		normals.Add(normal);
		tangents.Add(tangent);
		tangents.Add(tangent);
		vertColors.Add(color);
		vertColors.Add(color);
		if (quadMapping)
		{
			uv.Set(vCoord * sectionThickness, (sorting == TrailSorting.NewerOnTop) ? (uvWidthFactor * sectionThickness) : 0f, 0f, sectionThickness);
			uvs.Add(uv);
			uv.Set(vCoord * sectionThickness, (sorting == TrailSorting.NewerOnTop) ? 0f : (uvWidthFactor * sectionThickness), 0f, sectionThickness);
			uvs.Add(uv);
		}
		else
		{
			uv.Set(vCoord, (sorting == TrailSorting.NewerOnTop) ? uvWidthFactor : 0f, 0f, 1f);
			uvs.Add(uv);
			uv.Set(vCoord, (sorting == TrailSorting.NewerOnTop) ? 0f : uvWidthFactor, 0f, 1f);
			uvs.Add(uv);
		}
		if (i < count - 1)
		{
			int num5 = vertices.Count - 1;
			tris.Add(num5);
			tris.Add(va);
			tris.Add(vb);
			tris.Add(vb);
			tris.Add(num5 - 1);
			tris.Add(num5);
		}
		va = vertices.Count - 1;
		vb = vertices.Count - 2;
		if (!num || cornerRoundness <= 0)
		{
			return;
		}
		for (int j = 0; j <= cornerRoundness; j++)
		{
			vertices.Add(data[i].position + vector);
			normals.Add(normal);
			tangents.Add(tangent);
			vertColors.Add(color);
			uv.Set(vCoord, (!(num2 > 0f)) ? 1 : 0, 0f, 1f);
			uvs.Add(uv);
			int num6 = vertices.Count - 1;
			tris.Add(num6);
			tris.Add(va);
			tris.Add(vb);
			if (num2 > 0f)
			{
				vb = num6;
			}
			else
			{
				va = num6;
			}
			vector = quaternion * vector;
		}
	}
}