summaryrefslogtreecommitdiff
path: root/ch.sycoforge.Decal.Demo/BezierUtil.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-03-13 11:00:58 +0800
committerchai <215380520@qq.com>2024-03-13 11:00:58 +0800
commit6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch)
treeb38119d2acf0a982cb67e381f146924b9bfc3b3f /ch.sycoforge.Decal.Demo/BezierUtil.cs
+init
Diffstat (limited to 'ch.sycoforge.Decal.Demo/BezierUtil.cs')
-rw-r--r--ch.sycoforge.Decal.Demo/BezierUtil.cs77
1 files changed, 77 insertions, 0 deletions
diff --git a/ch.sycoforge.Decal.Demo/BezierUtil.cs b/ch.sycoforge.Decal.Demo/BezierUtil.cs
new file mode 100644
index 0000000..4784ce7
--- /dev/null
+++ b/ch.sycoforge.Decal.Demo/BezierUtil.cs
@@ -0,0 +1,77 @@
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace ch.sycoforge.Decal.Demo;
+
+public static class BezierUtil
+{
+ public static List<Vector3> InterpolatePath(List<Vector3> path, int segments, float radius, float angleThreshold)
+ {
+ if (path.Count >= 3)
+ {
+ List<Vector3> list = new List<Vector3>();
+ int num = path.Count - 1;
+ list.Add(path[0]);
+ int num2 = 0;
+ for (int i = 2; i < path.Count; i++)
+ {
+ Vector3 vector = path[i - 2];
+ Vector3 vector2 = path[i - 1];
+ Vector3 vector3 = path[i];
+ Vector3 vector4 = vector2 - vector;
+ Vector3 vector5 = vector3 - vector2;
+ float num3 = Mathf.Abs(Vector3.Angle(vector4, vector5));
+ if (!(num3 <= angleThreshold))
+ {
+ float magnitude = vector4.magnitude;
+ float magnitude2 = vector5.magnitude;
+ vector4.Normalize();
+ vector5.Normalize();
+ magnitude = Mathf.Min(magnitude * 0.5f, radius);
+ magnitude2 = Mathf.Min(magnitude2 * 0.5f, radius);
+ Vector3 vector6 = vector2 - vector4 * magnitude;
+ Vector3 vector7 = vector2;
+ Vector3 vector8 = vector2 + vector5 * magnitude2;
+ for (int j = 0; j < segments; j++)
+ {
+ float num4 = (float)j / ((float)segments - 1f);
+ float num5 = 1f - num4;
+ Vector3 item = num5 * num5 * vector6 + 2f * num5 * num4 * vector7 + num4 * num4 * vector8;
+ list.Add(item);
+ }
+ num2 = i;
+ }
+ }
+ if (num2 <= num)
+ {
+ list.Add(path[num]);
+ }
+ return list;
+ }
+ return path;
+ }
+
+ public static Vector3[] GetBezierApproximation(Vector3[] controlPoints, int outputSegmentCount)
+ {
+ Vector3[] array = new Vector3[outputSegmentCount + 1];
+ for (int i = 0; i < outputSegmentCount; i++)
+ {
+ float t = (float)i / (float)outputSegmentCount;
+ ref Vector3 reference = ref array[i];
+ reference = GetBezierPoint(t, controlPoints, 0, controlPoints.Length);
+ }
+ return array;
+ }
+
+ public static Vector3 GetBezierPoint(float t, Vector3[] controlPoints, int index, int count)
+ {
+ if (count == 1)
+ {
+ return controlPoints[index];
+ }
+ Vector3 bezierPoint = GetBezierPoint(t, controlPoints, index - 1, count - 1);
+ Vector3 bezierPoint2 = GetBezierPoint(t, controlPoints, index, count - 1);
+ Vector3 bezierPoint3 = GetBezierPoint(t, controlPoints, index + 1, count - 1);
+ return (1f - t) * (1f - t) * bezierPoint + 2f * (1f - t) * t * bezierPoint2 + t * t * bezierPoint3;
+ }
+}