forked from slipster216/VertexPaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertexInstanceStream.cs
513 lines (460 loc) · 16.1 KB
/
VertexInstanceStream.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
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
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
/* Holds streams of data to override the colors or UVs on a mesh without making the mesh unique. This is more
* memory efficient than burning the color data into many copies of a mesh, and much easier to manage.
*
*/
namespace JBooth.VertexPainterPro
{
[ExecuteInEditMode]
public class VertexInstanceStream : MonoBehaviour
{
public bool keepRuntimeData = false;
[HideInInspector]
[SerializeField]
private Color[] _colors;
[HideInInspector]
[SerializeField]
private List<Vector4> _uv0;
[HideInInspector]
[SerializeField]
private List<Vector4> _uv1;
[HideInInspector]
[SerializeField]
private List<Vector4> _uv2;
[HideInInspector]
[SerializeField]
private List<Vector4> _uv3;
[HideInInspector]
[SerializeField]
private Vector3[] _positions;
[HideInInspector]
[SerializeField]
private Vector3[] _normals;
[HideInInspector]
[SerializeField]
private Vector4[] _tangents;
public Color[] colors
{
get
{
return _colors;
}
set
{
enforcedColorChannels = (! (_colors == null || (value != null && _colors.Length != value.Length)));
_colors = value;
Apply();
}
}
public List<Vector4> uv0 { get { return _uv0; } set { _uv0 = value; Apply(); } }
public List<Vector4> uv1 { get { return _uv1; } set { _uv1 = value; Apply(); } }
public List<Vector4> uv2 { get { return _uv2; } set { _uv2 = value; Apply(); } }
public List<Vector4> uv3 { get { return _uv3; } set { _uv3 = value; Apply(); } }
public Vector3[] positions { get { return _positions; } set { _positions = value; Apply(); } }
public Vector3[] normals { get { return _normals; } set { _normals = value; Apply(); } }
public Vector4[] tangents { get { return _tangents; } set { _tangents = value; Apply(); } }
#if UNITY_EDITOR
Vector3[] cachedPositions;
public Vector3 GetSafePosition(int index)
{
if (_positions != null && index < _positions.Length)
{
return _positions[index];
}
if (cachedPositions == null)
{
MeshFilter mf = GetComponent<MeshFilter>();
if (mf == null || mf.sharedMesh == null)
{
Debug.LogError("No Mesh Filter or Mesh available");
return Vector3.zero;
}
cachedPositions = mf.sharedMesh.vertices;
}
if (index < cachedPositions.Length)
{
return cachedPositions[index];
}
return Vector3.zero;
}
Vector3[] cachedNormals;
public Vector3 GetSafeNormal(int index)
{
if (_normals != null && index < _normals.Length)
{
return _normals[index];
}
if (cachedPositions == null)
{
MeshFilter mf = GetComponent<MeshFilter>();
if (mf == null || mf.sharedMesh == null)
{
Debug.LogError("No Mesh Filter or Mesh available");
return Vector3.zero;
}
cachedNormals = mf.sharedMesh.normals;
}
if (cachedNormals != null && index < cachedNormals.Length)
{
return cachedNormals[index];
}
return new Vector3(0, 0, 1);
}
Vector4[] cachedTangents;
public Vector4 GetSafeTangent(int index)
{
if (_tangents != null && index < _tangents.Length)
{
return _tangents[index];
}
if (cachedTangents == null)
{
MeshFilter mf = GetComponent<MeshFilter>();
if (mf == null || mf.sharedMesh == null)
{
Debug.LogError("No Mesh Filter or Mesh available");
return Vector3.zero;
}
cachedTangents = mf.sharedMesh.tangents;
}
if (cachedTangents != null && index < cachedTangents.Length)
{
return cachedTangents[index];
}
return new Vector4(0, 1, 0, 1);
}
#endif
#if UNITY_EDITOR
// Stored here to make copy/save behaviour work better - basically, if you copy a mesh around, you want to also
// clone the original material otherwise it may have the preview material stuck on it forever.
[HideInInspector]
public Material[] originalMaterial;
public static Material vertexShaderMat;
void Awake()
{
// restore original material if we got saved with the preview material.
// I tried to do this in a number of ways; using the pre/post serialization callbacks seemed
// like the best, but is actually not possible because they don't always both get called. In editor,
// sometimes only the pre-serialization callback gets called. WTF..
MeshRenderer mr = GetComponent<MeshRenderer>();
if (mr != null)
{
if (mr.sharedMaterials != null && mr.sharedMaterial == vertexShaderMat && originalMaterial != null
&& originalMaterial.Length == mr.sharedMaterials.Length && originalMaterial.Length > 1)
{
Material[] mats = new Material[mr.sharedMaterials.Length];
for (int i = 0; i < mr.sharedMaterials.Length; ++i)
{
if (originalMaterial[i] != null)
{
mats[i] = originalMaterial[i];
}
}
mr.sharedMaterials = mats;
}
else if (originalMaterial != null && originalMaterial.Length > 0)
{
if (originalMaterial[0] != null)
{
mr.sharedMaterial = originalMaterial[0];
}
}
}
}
#endif
void Start()
{
Apply(!keepRuntimeData);
if (keepRuntimeData)
{
var mf = GetComponent<MeshFilter>();
_positions = mf.sharedMesh.vertices;
}
}
void OnDestroy()
{
if (!Application.isPlaying)
{
MeshRenderer mr = GetComponent<MeshRenderer> ();
if ( mr != null )
mr.additionalVertexStreams = null;
}
}
bool enforcedColorChannels = false;
void EnforceOriginalMeshHasColors(Mesh stream)
{
if (enforcedColorChannels == true)
return;
enforcedColorChannels = true;
MeshFilter mf = GetComponent<MeshFilter>();
Color[] origColors = mf.sharedMesh.colors;
if (stream != null && stream.colors.Length > 0 && (origColors == null || origColors.Length == 0))
{
// workaround for unity bug; dispite docs claim, color channels must exist on the original mesh
// for the additionalVertexStream to work. Which is, sad...
mf.sharedMesh.colors = stream.colors;
}
}
#if UNITY_EDITOR
public void SetColor(Color c, int count) { _colors = new Color[count]; for (int i = 0; i < count; ++i) { _colors[i] = c; } Apply(); }
public void SetUV0(Vector4 uv, int count) { _uv0 = new List<Vector4>(count); for (int i = 0; i < count; ++i) { _uv0.Add(uv); } Apply(); }
public void SetUV1(Vector4 uv, int count) { _uv1 = new List<Vector4>(count); for (int i = 0; i < count; ++i) { _uv1.Add(uv); } Apply(); }
public void SetUV2(Vector4 uv, int count) { _uv2 = new List<Vector4>(count); for (int i = 0; i < count; ++i) { _uv2.Add(uv); } Apply(); }
public void SetUV3(Vector4 uv, int count) { _uv3 = new List<Vector4>(count); for (int i = 0; i < count; ++i) { _uv3.Add(uv); } Apply(); }
public void SetUV0_XY(Vector2 uv, int count)
{
if (_uv0 == null || _uv0.Count != count)
{
_uv0 = new List<Vector4>(count);
for (int i = 0; i < count; ++i)
{
_uv0[i] = Vector4.zero;
}
}
for (int i = 0; i < count; ++i)
{
Vector4 v = _uv0[i];
v.x = uv.x;
v.y = uv.y;
_uv0[i] = v;
}
Apply();
}
public void SetUV0_ZW(Vector2 uv, int count)
{
if (_uv0 == null || _uv0.Count != count)
{
_uv0 = new List<Vector4>(count);
for (int i = 0; i < count; ++i)
{
_uv0[i] = Vector4.zero;
}
}
for (int i = 0; i < count; ++i)
{
Vector4 v = _uv0[i];
v.z = uv.x;
v.w = uv.y;
_uv0[i] = v;
}
Apply();
}
public void SetUV1_XY(Vector2 uv, int count)
{
if (_uv1 == null || _uv1.Count != count)
{
_uv1 = new List<Vector4>(count);
for (int i = 0; i < count; ++i)
{
_uv1[i] = Vector4.zero;
}
}
for (int i = 0; i < count; ++i)
{
Vector4 v = _uv1[i];
v.x = uv.x;
v.y = uv.y;
_uv1[i] = v;
}
Apply();
}
public void SetUV1_ZW(Vector2 uv, int count)
{
if (_uv1 == null || _uv1.Count != count)
{
_uv1 = new List<Vector4>(count);
for (int i = 0; i < count; ++i)
{
_uv1[i] = Vector4.zero;
}
}
for (int i = 0; i < count; ++i)
{
Vector4 v = _uv1[i];
v.z = uv.x;
v.w = uv.y;
_uv1[i] = v;
}
Apply();
}
public void SetUV2_XY(Vector2 uv, int count)
{
if (_uv2 == null || _uv2.Count != count)
{
_uv2 = new List<Vector4>(count);
for (int i = 0; i < count; ++i)
{
_uv2[i] = Vector4.zero;
}
}
for (int i = 0; i < count; ++i)
{
Vector4 v = _uv2[i];
v.x = uv.x;
v.y = uv.y;
_uv2[i] = v;
}
Apply();
}
public void SetUV2_ZW(Vector2 uv, int count)
{
if (_uv2 == null || _uv2.Count != count)
{
_uv2 = new List<Vector4>(count);
for (int i = 0; i < count; ++i)
{
_uv2[i] = Vector4.zero;
}
}
for (int i = 0; i < count; ++i)
{
Vector4 v = _uv2[i];
v.z = uv.x;
v.w = uv.y;
_uv2[i] = v;
}
Apply();
}
public void SetUV3_XY(Vector2 uv, int count)
{
if (_uv3 == null || _uv3.Count != count)
{
_uv3 = new List<Vector4>(count);
for (int i = 0; i < count; ++i)
{
_uv3[i] = Vector4.zero;
}
}
for (int i = 0; i < count; ++i)
{
Vector4 v = _uv3[i];
v.x = uv.x;
v.y = uv.y;
_uv3[i] = v;
}
Apply();
}
public void SetUV3_ZW(Vector2 uv, int count)
{
if (_uv3 == null || _uv3.Count != count)
{
_uv3 = new List<Vector4>(count);
for (int i = 0; i < count; ++i)
{
_uv3[i] = Vector4.zero;
}
}
for (int i = 0; i < count; ++i)
{
Vector4 v = _uv3[i];
v.z = uv.x;
v.w = uv.y;
_uv3[i] = v;
}
Apply();
}
public void SetColorRG(Vector2 rg, int count)
{
if (_colors == null || _colors.Length != count)
{
_colors = new Color[count];
enforcedColorChannels = false;
}
for (int i = 0; i < count; ++i)
{
_colors[i].r = rg.x;
_colors[i].g = rg.y;
}
Apply();
}
public void SetColorBA(Vector2 ba, int count)
{
if (_colors == null || _colors.Length != count)
{
_colors = new Color[count];
enforcedColorChannels = false;
}
for (int i = 0; i < count; ++i)
{
_colors[i].r = ba.x;
_colors[i].g = ba.y;
}
Apply();
}
#endif
public Mesh Apply(bool markNoLongerReadable = true)
{
MeshRenderer mr = GetComponent<MeshRenderer>();
MeshFilter mf = GetComponent<MeshFilter>();
if (mr != null && mf != null && mf.sharedMesh != null)
{
int vertexCount = mf.sharedMesh.vertexCount;
Mesh stream = meshStream;
if (stream == null || vertexCount != stream.vertexCount)
{
if (stream != null)
{
DestroyImmediate(stream);
}
stream = new Mesh();
// even though the docs say you don't need to set the positions on your avs, you do..
stream.vertices = new Vector3[mf.sharedMesh.vertexCount];
// wtf, copy won't work?
// so, originally I did a copyTo here, but with a unity patch release the behavior changed and
// the verticies would all become 0. This seems a funny thing to change in a patch release, but
// since getting the data from the C++ side creates a new array anyway, we don't really need
// to copy them anyway since they are a unique copy already.
stream.vertices = mf.sharedMesh.vertices;
// another Unity bug, when in editor, the paint job will just disapear sometimes. So we have to re-assign
// it every update (even though this doesn't get called each frame, it appears to loose the data during
// the editor update call, which only happens occationaly.
stream.MarkDynamic();
stream.triangles = mf.sharedMesh.triangles;
meshStream = stream;
stream.hideFlags = HideFlags.HideAndDontSave;
}
if (_positions != null && _positions.Length == vertexCount) { stream.vertices = _positions; }
if (_normals != null && _normals.Length == vertexCount) { stream.normals = _normals; } else { stream.normals = null; }
if (_tangents != null && _tangents.Length == vertexCount) { stream.tangents = _tangents; } else { stream.tangents = null; }
if (_colors != null && _colors.Length == vertexCount) { stream.colors = _colors; } else { stream.colors = null; }
if (_uv0 != null && _uv0.Count == vertexCount) { stream.SetUVs(0, _uv0); } else { stream.uv = null; }
if (_uv1 != null && _uv1.Count == vertexCount) { stream.SetUVs(1, _uv1); } else { stream.uv2 = null; }
if (_uv2 != null && _uv2.Count == vertexCount) { stream.SetUVs(2, _uv2); } else { stream.uv3 = null; }
if (_uv3 != null && _uv3.Count == vertexCount) { stream.SetUVs(3, _uv3); } else { stream.uv4 = null; }
EnforceOriginalMeshHasColors(stream);
if (!Application.isPlaying || Application.isEditor)
{
// only mark no longer readable in game..
markNoLongerReadable = false;
}
stream.UploadMeshData(markNoLongerReadable);
mr.additionalVertexStreams = stream;
return stream;
}
return null;
}
// keep this around for updates..
private Mesh meshStream;
// In various versions of unity, you have to set .additionalVertexStreams every frame.
// This was broken in 5.3, then broken again in 5.5..
#if UNITY_EDITOR
public Mesh GetModifierMesh() { return meshStream; }
private MeshRenderer meshRend = null;
void Update()
{
// turns out this happens in play mode as well.. grrr..
if (meshRend == null)
{
meshRend = GetComponent<MeshRenderer>();
}
//if (!Application.isPlaying)
{
meshRend.additionalVertexStreams = meshStream;
}
}
#endif
}
}