-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathCableMaker.cs
More file actions
234 lines (190 loc) · 7.09 KB
/
CableMaker.cs
File metadata and controls
234 lines (190 loc) · 7.09 KB
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
///
/// CableMaker by Nothke
///
/// Builds LineRenderer-based hanging cables using catenary formula.
///
/// Also has a static function CreateCatenary() for filling up a list of
/// catenary points so you can use it with any other rendering system.
///
/// Way it works as a component is it creates a LineRenderer on Start().
///
/// Thanks to Alan Zucconi's post for different height ends and constant length:
/// https://www.alanzucconi.com/2020/12/13/catenary-1/
///
/// ============================================================================
///
/// MIT License
///
/// Copyright(c) 2021 Ivan Notaroš
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
/// SOFTWARE.
///
/// ============================================================================
///
using UnityEngine;
using System.Collections.Generic;
namespace Nothke.Utils
{
public class CableMaker : MonoBehaviour
{
[UnityEngine.Serialization.FormerlySerializedAs("cableStart")]
public Transform start;
[UnityEngine.Serialization.FormerlySerializedAs("cableEnd")]
public Transform end;
[Min(1.0f)] public int segments = 9;
[Min(0)] public float slack = 0.2f;
public Material material;
[Min(0)] public float width = 0.03f;
LineRenderer line;
[System.NonSerialized]
public List<Vector3> linePoints = new List<Vector3>();
void Start()
{
Generate();
}
#region Public Methods
public bool BothEndsExist()
{
return start && end;
}
public void Generate()
{
if (!BothEndsExist())
return;
UpdateCatenary();
SetToLineRenderer();
}
public void UpdateCatenary()
{
if (!BothEndsExist())
return;
float length = (end.position - start.position).magnitude;
float targetLength = length + slack;
CreateCatenary(linePoints, start.position, end.position, segments, targetLength);
}
public void SetToLineRenderer()
{
line = GetComponent<LineRenderer>();
if (!line)
{
line = gameObject.AddComponent<LineRenderer>();
line.useWorldSpace = false;
line.material = material;
line.startWidth = width;
line.endWidth = width;
}
line.positionCount = linePoints.Count;
for (int i = 0; i < linePoints.Count; i++)
line.SetPosition(i, transform.InverseTransformPoint(linePoints[i]));
}
#endregion
#region Static Methods
static float Cosh(float f) => (float)System.Math.Cosh(f);
static float Sinh(float f) => (float)System.Math.Sinh(f);
static float Coth(float f) => Cosh(f) / Sinh(f);
/// <summary>
/// Fills up a list of points with catenary curve from p1 to p2.
/// If the distance between ends is greater than targetLength, returns just a p1-p2 line.
/// </summary>
/// <param name="points">List of points to fill up. Will be overwritten. Must not be null.</param>
public static void CreateCatenary(List<Vector3> points, in Vector3 p1, in Vector3 p2, int segments, float targetLength)
{
if (segments < 1)
segments = 1;
Vector3 diff = p2 - p1;
// Fully taut
if (segments == 1 || diff.magnitude > targetLength)
{
points.Clear();
points.Add(p1);
points.Add(p2);
return;
}
float yDiff = diff.y;
Vector3 planarDiff = diff;
planarDiff.y = 0;
float xDiff = planarDiff.magnitude;
float l = targetLength;
// Simple step method for finding 'a' from: https://www.alanzucconi.com/2020/12/13/catenary-2/
const float step = 0.001f;
float s = 0;
do
s += step;
while (Sinh(s) / s < Mathf.Sqrt(l * l - yDiff * yDiff) / xDiff);
float a = xDiff / s / 2.0f;
float p = (xDiff - a * Mathf.Log((l + yDiff) / (l - yDiff))) / 2.0f;
float q = (yDiff - l * Coth(s)) / 2.0f;
points.Clear();
for (int i = 0; i < segments + 1; i++)
{
float x = (float)i / segments;
// from: https://en.wikipedia.org/wiki/Catenary#Determining_parameters
float y = a * Cosh((x * xDiff - p) / a) + q;
Vector3 point = planarDiff * x;
point.y = y;
points.Add(p1 + point);
}
}
#endregion
#if UNITY_EDITOR
static readonly Vector3 gizmosCubeSize = Vector3.one * 0.1f;
private void OnValidate()
{
UpdateCatenary();
}
void DrawGizmoEnds()
{
if (!start || !end)
{
Gizmos.color = Color.red;
if (end)
Gizmos.DrawCube(end.position, gizmosCubeSize);
else if (start)
Gizmos.DrawCube(start.position, gizmosCubeSize);
}
else
{
Gizmos.color = Color.green;
Gizmos.DrawWireCube(start.position, gizmosCubeSize);
Gizmos.color = Color.cyan;
Gizmos.DrawWireCube(end.position, gizmosCubeSize);
}
}
void DrawCableGizmos()
{
if (Application.isPlaying || !BothEndsExist())
return;
for (int i = 0; i < linePoints.Count - 1; i++)
Gizmos.DrawLine(linePoints[i], linePoints[i + 1]);
}
private void OnDrawGizmos()
{
Gizmos.color = new Color(1.0f, 1.0f, 0.0f, 0.5f);
DrawCableGizmos();
}
private void OnDrawGizmosSelected()
{
UpdateCatenary();
DrawGizmoEnds();
Gizmos.color = Color.white;
DrawCableGizmos();
}
#endif
}
}