-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmoothWeaponTrail.cs
433 lines (361 loc) · 14 KB
/
SmoothWeaponTrail.cs
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
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Smooth Weapon Trails by Jan Forberg
/// </summary>
public class SmoothWeaponTrail : MonoBehaviour
{
/*
* | head tail |
*
* | 1 | 2 | 3 | 4 | <--- segments
*
* x -- o -- x -- o -- x -- o -- x -- o -- x
* | | | | | | | | |
* | | | | | | | | |
* x -- o -- x -- o -- x -- o -- x -- o -- x
* | | | | | | | | |
* | | | | | | | | |
* x -- o -- x -- o -- x -- o -- x -- o -- x <--- line
*
* |-------------lineVertexCount-----------|
*
* Example:
* nodeCount = 3
* segments = 4
* subdivisions = 1
* lineVertexCount = 9
* segmentVertexCount = 6
* vertexCount = 27
*
* Approach:
*
* - Generate a tringale mesh
* - Always move the head vertices with the transforms specified in "nodes"
* - Move all segment vertices (x's) to its headward neighbor along each line n times per second
* - Recalculate the position of all interpolation vertices (o's) based on hermite interpolation of the neighboring
* segment vertices (x's)
* - Determine the world space length of each line (keep all segments lengths in a helper array to avoid
* costly distance calculations - only the segment lengths of the head segment changes)
* - Calculate the UV coordinate for each vertex (x's AND o's) based on the line length and its position on
* the line from tail to head
*/
/// <summary>
/// The number of segments of the trail mesh
/// </summary>
[SerializeField]
private int segments = 10;
/// <summary>
/// The number of vertical
/// </summary>
[SerializeField]
private int subdivisions = 1;
[SerializeField]
private bool useHermiteInterpolation = false;
[SerializeField]
private float subdivisionHermiteTension = 0;
[SerializeField]
private float subdivisionHermiteBias = 0;
[SerializeField]
private float maxSegmentLength = 0.05f;
[SerializeField]
private Material material;
[SerializeField]
private bool disabledByDefault = true;
[SerializeField]
private Transform[] nodes;
private GameObject trailObject;
private MeshRenderer meshRenderer;
private Vector3[] positions;
private Color[] colors;
private Vector3[] uvs;
private float[,] segmentLengths;
private Mesh mesh;
private int lineVertexCount;
private int segmentVertexCount;
private int vertexCount;
private bool frozen;
private void OnDestroy()
{
Destroy(trailObject);
}
private void OnEnable()
{
CancelInvoke("Disable");
frozen = false;
for (int i = lineVertexCount - 1; i >= 0; i--)
{
for (int j = 0; j < nodes.Length; j++)
{
positions[i * nodes.Length + j] = nodes[j].position;
}
}
mesh.vertices = positions;
mesh.SetUVs(0, uvs);
mesh.RecalculateBounds();
trailObject.SetActive(true);
}
private void OnDisable()
{
trailObject.SetActive(false);
}
//public void DisableDeferred(float delay)
//{
// frozen = true;
// Invoke("Disable", delay);
//}
private void Disable()
{
enabled = false;
frozen = false;
}
/// <summary>
/// Mesh creation
/// </summary>
private void Awake()
{
lineVertexCount = segments * (subdivisions + 1) + 1;
segmentVertexCount = (subdivisions + 1) * nodes.Length;
vertexCount = nodes.Length * lineVertexCount;
positions = new Vector3[vertexCount];
colors = new Color[vertexCount];
uvs = new Vector3[vertexCount];
segmentLengths = new float[segments, nodes.Length];
List<int> indices = new List<int>();
int k = 0;
for (int j = 0; j < lineVertexCount; j++)
{
for (int i = 0; i < nodes.Length; i++)
{
positions[k] = new Vector3(i, 0, j);
uvs[k] = new Vector2(0, 0);
if (i < nodes.Length - 1 && j < lineVertexCount - 1)
{
indices.Add(k);
indices.Add(k + nodes.Length);
indices.Add(k + nodes.Length + 1);
indices.Add(k);
indices.Add(k + nodes.Length + 1);
indices.Add(k + 1);
}
k++;
}
}
// Create trail mesh
mesh = new Mesh();
mesh.vertices = positions;
mesh.colors = colors;
mesh.SetUVs(0, uvs);
mesh.SetIndices(indices.ToArray(), MeshTopology.Triangles, 0);
mesh.RecalculateBounds();
// Create a trail object in the scene to render the mesh
trailObject = new GameObject("Trail");
meshRenderer = trailObject.AddComponent<MeshRenderer>();
meshRenderer.material = material;
meshRenderer.receiveShadows = true;
meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
MeshFilter filter = trailObject.AddComponent<MeshFilter>();
filter.mesh = mesh;
if (disabledByDefault)
{
enabled = false;
}
}
private void Update()
{
// Update the length array at the first column.
// Distances are the distance of the tracers to the vertices right behind the trail head
bool updateMesh = false;
for (int j = 0; j < nodes.Length; j++)
{
segmentLengths[0, j] = Vector3.Distance(nodes[j].position, positions[(subdivisions + 1) * nodes.Length + j]);
if (segmentLengths[0, j] > maxSegmentLength)
{
updateMesh = true;
}
}
// Only do this x times per second...
if (updateMesh)
{
// Update positions of all vertices
for (int i = segments - 1; i >= 1; i--)
{
for (int j = 0; j < segmentVertexCount; j++)
{
positions[i * segmentVertexCount + j] = positions[(i - 1) * segmentVertexCount + j];
colors[i * segmentVertexCount + j] = colors[(i - 1) * segmentVertexCount + j];
}
}
for (int i = 0; i < nodes.Length; i++)
{
positions[segments * segmentVertexCount + i] = positions[(segments - 1) * segmentVertexCount + i];
}
// Update segment lengths
for (int i = segments - 1; i >= 1; i--)
{
for (int j = 0; j < nodes.Length; j++)
{
segmentLengths[i, j] = segmentLengths[i - 1, j];
}
}
// Update UVs for all vertices
for (int j = 0; j < nodes.Length; j++)
{
float lineLength = 0;
float u = j * (1.0f / (nodes.Length - 1));
for (int i = 0; i < segments; i++)
{
lineLength += segmentLengths[i, j];
}
float progressAlongLine = 0.0f;
// Set the uvs of the tail of the line
uvs[segments * segmentVertexCount + j] = new Vector2(u, 0);
//// Set the uvs of the head of the line
//uvs[j] = new Vector2(u, 1);
if (lineLength == 0.0f)
{
// If the entire line has length 0, distribute the v coordinate evenly
for (int i = lineVertexCount - 1; i >= 0; i--)
{
uvs[i * nodes.Length + j] = new Vector2(u, 1.0f / lineVertexCount);
}
}
else
{
// Track the progress along each line
float previousV = 0;
for (int i = segments - 1; i >= 0; i--)
{
// Update the progress along the line
progressAlongLine += segmentLengths[i, j];
float segmentV = progressAlongLine / lineLength;
for (int s = 0; s <= subdivisions; s++)
{
float subdivisionLerp = (float)s / (subdivisions + 1);
int subdivisionColumnIndex = i * segmentVertexCount + nodes.Length * s;
float v = Mathf.Lerp(segmentV, previousV, subdivisionLerp);
uvs[subdivisionColumnIndex + j] = new Vector2(u, v);
}
previousV = segmentV;
}
}
}
}
// Debug Draw
for (int i = 0; i < segments; i++)
{
for (int j = 0; j < nodes.Length - 1; j++)
{
Debug.DrawLine(positions[i * segmentVertexCount + j], positions[i * segmentVertexCount + j + 1], Color.red);
}
}
}
private void LateUpdate()
{
if (!frozen)
{
// Update the head vertices
for (int j = 0; j < nodes.Length; j++)
{
positions[j] = nodes[j].position;
Vector3 end = positions[segmentVertexCount + j];
Vector3 start = positions[j];
for (int s = 1; s <= subdivisions; s++)
{
float subdivisionLerp = (float)s / (subdivisions + 1);
int subdivisionColumnIndex = nodes.Length * s;
positions[subdivisionColumnIndex + j] = Vector3.Lerp(start, end, subdivisionLerp);
}
}
// Update the position of head subdivision vertices
for (int i = 1; i >= 0; i--)
{
// For each segment along each line, find 4 subsequent segment vertices along a line
// Perform hermite interpolation for all the interpolation vertices
for (int j = 0; j < nodes.Length; j++)
{
// Start and endpoint for hermite interpolation
Vector3 end = positions[(i + 1) * segmentVertexCount + j];
Vector3 start = positions[i * segmentVertexCount + j];
// Additional previous and next point for hermite interpolation
Vector3 prev = i == 0 ? 2 * start - end : positions[(i - 1) * segmentVertexCount + j];
Vector3 next;
if (i < segments - 1)
{
next = positions[(i + 2) * segmentVertexCount + j];
}
else
{
// Create an artificial point if we are at the end of the line
next = 2 * end - start;
}
// Perform interpolation for all subdivision vertices
for (int s = 1; s <= subdivisions; s++)
{
float subdivisionLerp = (float)s / (subdivisions + 1);
int subdivisionColumnIndex = i * segmentVertexCount + nodes.Length * s;
if(useHermiteInterpolation)
{
positions[subdivisionColumnIndex + j] = HermiteInterpolate(prev, start, end, next, subdivisionLerp);
}
else
{
positions[subdivisionColumnIndex + j] = CatmullRom(prev, start, end, next, subdivisionLerp);
}
}
}
}
}
mesh.vertices = positions;
mesh.colors = colors;
mesh.SetUVs(0, uvs);
mesh.RecalculateBounds();
}
/// <summary>
/// CatmullRom taken and adjusted from http://paulbourke.net/miscellaneous/interpolation/
/// </summary>
/// <param name="previous"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="next"></param>
/// <param name="progress"></param>
/// <returns></returns>
private Vector3 CatmullRom(Vector3 previous, Vector3 start, Vector3 end, Vector3 next, float progress)
{
float progressSqr = progress * progress;
float progressCbe = progressSqr * progress;
return previous * (-0.5f * progressCbe + progressSqr - 0.5f * progress) +
start * (1.5f * progressCbe + -2.5f * progressSqr + 1.0f) +
end * (-1.5f * progressCbe + 2.0f * progressSqr + 0.5f * progress) +
next * (0.5f * progressCbe - 0.5f * progressSqr);
}
/// <summary>
/// Hermite interpolation taken and adjusted from http://paulbourke.net/miscellaneous/interpolation/
/// </summary>
/// <param name="previous"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="next"></param>
/// <param name="progress"></param>
/// <returns></returns>
private Vector3 HermiteInterpolate(
Vector3 previous, Vector3 start,
Vector3 end, Vector3 next,
float progress)
{
Vector3 m0, m1;
float mu2, mu3;
float a0, a1, a2, a3;
mu2 = progress * progress;
mu3 = mu2 * progress;
m0 = (start - previous) * (1 + subdivisionHermiteBias) * (1 - subdivisionHermiteTension) / 2;
m0 += (end - start) * (1 - subdivisionHermiteBias) * (1 - subdivisionHermiteTension) / 2;
m1 = (end - start) * (1 + subdivisionHermiteBias) * (1 - subdivisionHermiteTension) / 2;
m1 += (next - end) * (1 - subdivisionHermiteBias) * (1 - subdivisionHermiteTension) / 2;
a0 = 2 * mu3 - 3 * mu2 + 1;
a1 = mu3 - 2 * mu2 + progress;
a2 = mu3 - mu2;
a3 = -2 * mu3 + 3 * mu2;
return a0 * start + a1 * m0 + a2 * m1 + a3 * end;
}
}