BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Scripts / Tweening / Curving.cs

Curving.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
Curving.cs 2.1 KiB Download Raw
1using System;
2using UnityEngine;
3
4/**
5 * Curving
6 *Created by: Tim Falken
7 *
8 *Allows you to do Bezier Curves with floats and Vector3's using an arbitrary amount of points.
9 **/
10
11public class Curving
12{
13 public static float Lerp(float[] points, float t)
14 {
15 if (points.Length == 0)
16 {
17 return 0;
18 }
19 else if (points.Length == 1)
20 {
21 return points [0];
22 }
23 else if (points.Length == 2)
24 {
25 return Mathf.Lerp (points [0], points [1], t);
26 }
27 else
28 {
29 float[] newPoints = new float[points.Length - 1];
30
31 for (int i = 0; i < newPoints.Length; i ++)
32 {
33 newPoints [i] = Mathf.Lerp (points [i], points [i + 1], t);
34 }
35
36 return Lerp (newPoints, t);
37 }
38 }
39
40 //Hetzelfde als Lerp() voor floats, maar met Vectors
41 public static Vector3 Lerp(Vector3[] points, float t)
42 {
43 if (points.Length == 0)
44 {
45 return Vector3.zero;
46 }
47 else if (points.Length == 1)
48 {
49 return points [0];
50 }
51 else if (points.Length == 2)
52 {
53 return Vector3.Lerp (points [0], points [1], t);
54 }
55 else
56 {
57 Vector3[] newPoints = new Vector3[points.Length - 1];
58
59 for (int i = 0; i < newPoints.Length; i ++)
60 {
61 newPoints [i] = Vector3.Lerp (points [i], points [i + 1], t);
62 }
63
64 return Lerp (newPoints, t);
65 }
66 }
67
68 //Hetzelfde als Lerp() voor floats, maar met elke class dir ICurvable implementeerd.
69 public static ICurvable Lerp(ICurvable[] points, float t)
70 {
71 if (points.Length == 0)
72 {
73 throw new ArgumentNullException ("Given points array is empty!");
74 }
75 else if (points.Length == 1)
76 {
77 return points [0];
78 }
79 else if (points.Length == 2)
80 {
81 return points [0].add(points [1].subtract(points[0]).multiply(t));
82 }
83 else
84 {
85 ICurvable[] newPoints = new ICurvable[points.Length - 1];
86
87 for (int i = 0; i < newPoints.Length; i ++)
88 {
89 newPoints [i] = points [i].add(points [i + 1].subtract(points[i]).multiply(t));
90 }
91
92 return Lerp (newPoints, t);
93 }
94 }
95
96 //Implementeer deze interface om een custom datatype te laten curven.
97 public interface ICurvable
98 {
99 ICurvable add(ICurvable other);
100 ICurvable subtract(ICurvable other);
101 ICurvable multiply(float factor);
102 }
103}