-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathBaseMeshSync.Modifiers.cs
More file actions
522 lines (413 loc) · 19.2 KB
/
BaseMeshSync.Modifiers.cs
File metadata and controls
522 lines (413 loc) · 19.2 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
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
514
515
516
517
518
519
520
521
522
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Unity.MeshSync;
using UnityEngine;
#if AT_USE_SPLINES
using UnityEngine.Splines;
using Unity.Mathematics;
#endif
namespace Unity.MeshSync {
internal class PropertyInfoDataWrapperComparer : IComparer<PropertyInfoDataWrapper> {
public int Compare(PropertyInfoDataWrapper x, PropertyInfoDataWrapper y) {
if (x.path == y.path) return x.name.CompareTo(y.name);
return x.path.CompareTo(y.path);
}
}
/// <summary>
/// Wrapper with additional data on PropertyInfoData
/// that cannot be stored in PropertyInfoData:
/// </summary>
[Serializable]
internal class PropertyInfoDataWrapper : ISerializationCallbackReceiver {
[DllImport(Lib.name)]
private static extern void msPropertyInfoCopyData(IntPtr self, ref int dst);
[DllImport(Lib.name)]
private static extern void msPropertyInfoCopyData(IntPtr self, ref float dst);
[DllImport(Lib.name)]
private static extern void msPropertyInfoCopyData(IntPtr self, StringBuilder dst);
[DllImport(Lib.name)]
private static extern void msPropertyInfoCopyData(IntPtr self, float[] dst);
[DllImport(Lib.name)]
private static extern void msPropertyInfoCopyData(IntPtr self, int[] dst);
[DllImport(Lib.name)]
private static extern int msPropertyInfoGetArrayLength(IntPtr self);
public static object PropertyUpdateLock = new object();
#region Serialization constants
private const char SERIALIZED_NULL = 'n';
private const char SERIALIZED_INT = 'i';
private const char SERIALIZED_FLOAT = 'f';
private const char SERIALIZED_STRING = 's';
private const char SERIALIZED_INT_ARRAY = 'a';
private const char SERIALIZED_FLOAT_ARRAY = 'b';
#endregion
internal PropertyInfoDataWrapper(PropertyInfoData propertyInfoData) {
type = propertyInfoData.type;
sourceType = propertyInfoData.sourceType;
name = propertyInfoData.name;
min = propertyInfoData.min;
max = propertyInfoData.max;
path = propertyInfoData.path;
modifierName = propertyInfoData.modifierName;
propertyName = propertyInfoData.propertyName;
arrayLength = msPropertyInfoGetArrayLength(propertyInfoData.self);
switch (type) {
case PropertyInfoDataType.Int: {
int r = 0;
msPropertyInfoCopyData(propertyInfoData.self, ref r);
propertyValue = r;
break;
}
case PropertyInfoDataType.Float: {
float r = 0;
msPropertyInfoCopyData(propertyInfoData.self, ref r);
propertyValue = r;
break;
}
case PropertyInfoDataType.IntArray: {
int[] r = new int[arrayLength];
msPropertyInfoCopyData(propertyInfoData.self, r);
propertyValue = r;
break;
}
case PropertyInfoDataType.FloatArray: {
float[] r = new float[arrayLength];
msPropertyInfoCopyData(propertyInfoData.self, r);
propertyValue = r;
break;
}
case PropertyInfoDataType.String: {
StringBuilder s = new StringBuilder(arrayLength + 1); // +1 for string terminator
msPropertyInfoCopyData(propertyInfoData.self, s);
propertyValue = s.ToString();
break;
}
default:
Debug.LogError($"Type {propertyInfoData.type} not implemented");
break;
}
}
public PropertyInfoDataType type;
public PropertyInfoDataSourceType sourceType;
[field: SerializeField] public string name { get; private set; }
[field: SerializeField] public float min { get; private set; }
[field: SerializeField] public float max { get; private set; }
[field: SerializeField] public string path { get; private set; }
[field: SerializeField] public string modifierName { get; private set; }
[field: SerializeField] public string propertyName { get; private set; }
[field: SerializeField] public int arrayLength { get; private set; }
// This is the new value set in Unity, to be sent back to the DCC tool:
private object newValue;
// This is the value we get from the DCC tool:
public object propertyValue;
// object cannot be serialized by unity, save a string representation of it:
[SerializeField] [HideInInspector] private string propertyValueSerialized;
public string ID {
get { return $"{path}_{name}"; }
}
public bool Matches(PropertyInfoDataWrapper other) {
return
name == other.name &&
type == other.type &&
sourceType == other.sourceType &&
propertyName == other.propertyName &&
arrayLength == other.arrayLength;
}
private static T[] ParseArray<T>(string propertyValueSerialized, Func<string, T> parse) {
string[] v = propertyValueSerialized.Substring(2).Split('|');
T[] array = new T[v.Length];
for (int i = 0; i < v.Length; i++) array[i] = parse(v[i]);
return array;
}
private static string SaveArray<T>(string prefix, T[] array) {
StringBuilder sb = new StringBuilder(prefix);
for (int i = 0; i < array.Length; i++) {
if (i < array.Length) sb.Append("|");
sb.Append(array[i]);
}
return sb.ToString();
}
public string GetSerializedValue(bool useNewValues = false) {
object valueToSerialize = propertyValue;
if (useNewValues && newValue != null) valueToSerialize = newValue;
if (valueToSerialize == null) return $"{SERIALIZED_NULL}";
if (valueToSerialize is int)
return $"{SERIALIZED_INT}{valueToSerialize}";
if (valueToSerialize is float)
return $"{SERIALIZED_FLOAT}{valueToSerialize}";
if (valueToSerialize is string)
return $"{SERIALIZED_STRING}{valueToSerialize}";
if (valueToSerialize is int[] intArray)
return SaveArray($"{SERIALIZED_INT_ARRAY}", intArray);
if (valueToSerialize is float[] floatArray)
return SaveArray($"{SERIALIZED_FLOAT_ARRAY}", floatArray);
throw new NotImplementedException($"propertyValue: {valueToSerialize.GetType()} cannot be serialized!");
}
private static object DeserializeString(string serializedValue) {
if (serializedValue.Length == 0)
return null;
char type = serializedValue[0];
if (type == SERIALIZED_NULL)
return null;
if (type == SERIALIZED_INT)
return int.Parse(serializedValue.Substring(1));
if (type == SERIALIZED_FLOAT)
return float.Parse(serializedValue.Substring(1));
if (type == SERIALIZED_STRING)
return serializedValue.Substring(1);
if (type == SERIALIZED_INT_ARRAY)
return ParseArray(serializedValue, int.Parse);
if (type == SERIALIZED_FLOAT_ARRAY)
return ParseArray(serializedValue, float.Parse);
throw new NotImplementedException($"propertyValue: {type} cannot be deserialized!");
}
public void SetSerializedValue(string serializedValue) {
NewValue = DeserializeString(serializedValue);
}
public void OnBeforeSerialize() {
propertyValueSerialized = GetSerializedValue();
}
public void OnAfterDeserialize() {
propertyValue = DeserializeString(propertyValueSerialized);
}
public object NewValue {
get { return newValue; }
set {
switch (type) {
case PropertyInfoDataType.IntArray: {
int[] newValueAsArray = new int[arrayLength];
if (value is Vector2Int newValueAsVector2Int) {
newValueAsArray[0] = Mathf.Clamp(newValueAsVector2Int.x, (int)min, (int)max);
newValueAsArray[1] = Mathf.Clamp(newValueAsVector2Int.y, (int)min, (int)max);
value = newValueAsArray;
}
else if (value is Vector3Int newValueAsVector3Int) {
newValueAsArray[0] = Mathf.Clamp(newValueAsVector3Int.x, (int)min, (int)max);
newValueAsArray[1] = Mathf.Clamp(newValueAsVector3Int.y, (int)min, (int)max);
newValueAsArray[2] = Mathf.Clamp(newValueAsVector3Int.z, (int)min, (int)max);
value = newValueAsArray;
}
else if (value is Vector2 newValueAsVector2) {
newValueAsArray[0] = Mathf.Clamp((int)newValueAsVector2.x, (int)min, (int)max);
newValueAsArray[1] = Mathf.Clamp((int)newValueAsVector2.y, (int)min, (int)max);
value = newValueAsArray;
}
else if (value is Vector3 newValueAsVector3) {
newValueAsArray[0] = Mathf.Clamp((int)newValueAsVector3.x, (int)min, (int)max);
newValueAsArray[1] = Mathf.Clamp((int)newValueAsVector3.y, (int)min, (int)max);
newValueAsArray[2] = Mathf.Clamp((int)newValueAsVector3.z, (int)min, (int)max);
value = newValueAsArray;
}
else if (value is Vector4 newValueAsVector4) {
newValueAsArray[0] = Mathf.Clamp((int)newValueAsVector4.x, (int)min, (int)max);
newValueAsArray[1] = Mathf.Clamp((int)newValueAsVector4.y, (int)min, (int)max);
newValueAsArray[2] = Mathf.Clamp((int)newValueAsVector4.z, (int)min, (int)max);
newValueAsArray[3] = Mathf.Clamp((int)newValueAsVector4.w, (int)min, (int)max);
value = newValueAsArray;
}
else {
int[] array = value as int[];
for (int i = 0; i < array.Length; i++) array[i] = Mathf.Clamp(array[i], (int)min, (int)max);
}
break;
}
case PropertyInfoDataType.FloatArray: {
float[] newValueAsArray = new float[arrayLength];
if (value is Vector2Int newValueAsVector2Int) {
newValueAsArray[0] = Mathf.Clamp(newValueAsVector2Int.x, min, max);
newValueAsArray[1] = Mathf.Clamp(newValueAsVector2Int.y, min, max);
value = newValueAsArray;
}
else if (value is Vector3Int newValueAsVector3Int) {
newValueAsArray[0] = Mathf.Clamp(newValueAsVector3Int.x, min, max);
newValueAsArray[1] = Mathf.Clamp(newValueAsVector3Int.y, min, max);
newValueAsArray[2] = Mathf.Clamp(newValueAsVector3Int.z, min, max);
value = newValueAsArray;
}
else if (value is Vector2 newValueAsVector2) {
newValueAsArray[0] = Mathf.Clamp(newValueAsVector2.x, min, max);
newValueAsArray[1] = Mathf.Clamp(newValueAsVector2.y, min, max);
value = newValueAsArray;
}
else if (value is Vector3 newValueAsVector3) {
newValueAsArray[0] = Mathf.Clamp(newValueAsVector3.x, min, max);
newValueAsArray[1] = Mathf.Clamp(newValueAsVector3.y, min, max);
newValueAsArray[2] = Mathf.Clamp(newValueAsVector3.z, min, max);
value = newValueAsArray;
}
else if (value is Vector4 newValueAsVector4) {
newValueAsArray[0] = Mathf.Clamp(newValueAsVector4.x, min, max);
newValueAsArray[1] = Mathf.Clamp(newValueAsVector4.y, min, max);
newValueAsArray[2] = Mathf.Clamp(newValueAsVector4.z, min, max);
newValueAsArray[3] = Mathf.Clamp(newValueAsVector4.w, min, max);
value = newValueAsArray;
}
else {
float[] array = value as float[];
for (int i = 0; i < array.Length; i++) array[i] = Mathf.Clamp(array[i], min, max);
}
break;
}
}
if (newValue != null) {
if (newValue.Equals(value)) return;
}
else if (propertyValue != null && propertyValue.Equals(value)) {
return;
}
newValue = value;
IsDirty = true;
}
}
public bool IsDirty { get; set; }
public T GetValue<T>() {
if (NewValue is T x) return x;
if (propertyValue == null) return default;
return (T)propertyValue;
}
public override string ToString() {
return $"PropertyInfoDataWrapper: {name}: {GetValue<object>()}";
}
}
// Partial class for now to make merging code easier later.
[Serializable]
partial class BaseMeshSync {
internal enum InstanceHandlingType {
InstanceRenderer,
Copies,
Prefabs
}
[SerializeField] private InstanceHandlingType instanceHandling = InstanceHandlingType.InstanceRenderer;
private int numberOfPropertiesReceived;
internal List<PropertyInfoDataWrapper> propertyInfos {
get {
MeshSyncServerLiveEditProperties propertyComponent = Misc.GetOrAddComponent<MeshSyncServerLiveEditProperties>(gameObject);
return propertyComponent.propertyInfos;
}
}
internal virtual InstanceHandlingType InstanceHandling {
get => instanceHandling;
#if UNITY_EDITOR
set => instanceHandling = value;
#endif
}
#if AT_USE_PROBUILDER
[SerializeField] private bool useProBuilder;
internal virtual bool UseProBuilder {
get { return useProBuilder; }
set { useProBuilder = value; }
}
#endif
private void UpdateProperties(SceneData scene) {
// handle properties
Try(() => {
int numProperties = scene.numPropertyInfos;
if (numProperties == 0) return;
numberOfPropertiesReceived += numProperties;
lock (PropertyInfoDataWrapper.PropertyUpdateLock) {
List<PropertyInfoDataWrapper> pendingProps = null;
// If there are dirty properties, make sure we set any updated values on the new ones:
foreach (PropertyInfoDataWrapper prop in propertyInfos)
if (prop.IsDirty) {
if (pendingProps == null) pendingProps = new List<PropertyInfoDataWrapper>();
pendingProps.Add(prop);
}
propertyInfos.Clear();
for (int i = 0; i < numProperties; ++i) {
PropertyInfoData data = scene.GetPropertyInfo(i);
propertyInfos.Add(new PropertyInfoDataWrapper(data));
}
propertyInfos.Sort(new PropertyInfoDataWrapperComparer());
if (pendingProps == null) return;
foreach (PropertyInfoDataWrapper pendingProp in pendingProps)
foreach (PropertyInfoDataWrapper prop in propertyInfos)
if (prop.Matches(pendingProp))
prop.NewValue = pendingProp.NewValue;
}
});
}
private object splineLock = new object();
private EntityRecord UpdateCurveEntity(CurvesData data, MeshSyncPlayerConfig config) {
#if AT_USE_SPLINES
lock (splineLock) {
TransformData dtrans = data.transform;
EntityRecord rec = UpdateTransformEntity(dtrans, config);
GameObject go = rec.go;
Transform trans = go.transform;
SplineContainer splineContainer = rec.splineContainer;
if (splineContainer == null) {
splineContainer = rec.splineContainer = Misc.GetOrAddComponent<SplineContainer>(trans.gameObject);
}
float3[] cos = null;
float3[] handles_left = null;
float3[] handles_right = null;
var numSplines = data.numSplines;
Spline.Changed -= SplineChanged;
// Try to reuse the same splines if the number of splines and knots has not changed:
bool useNewSplines = splineContainer.Splines.Count != numSplines;
if (!useNewSplines) {
for (int index = 0; index < numSplines; index++) {
if (splineContainer.Splines[index].Count != data.GetNumSplinePoints(index)) {
useNewSplines = true;
break;
}
}
}
var newSplines = new List<Spline>();
for (int index = 0; index < numSplines; index++) {
Spline spline;
if (useNewSplines) {
spline = new Spline();
newSplines.Add(spline);
}
else {
spline = splineContainer.Splines[index];
}
// Need to be in this mode to be consistent with blender:
spline.SetTangentMode(TangentMode.Broken);
spline.Closed = data.IsSplineClosed(index);
var numPoints = data.GetNumSplinePoints(index);
m_tmpFloat3.Resize(numPoints);
data.ReadSplineCos(index, m_tmpFloat3);
m_tmpFloat3.CopyTo(ref cos);
data.ReadSplineHandlesLeft(index, m_tmpFloat3);
m_tmpFloat3.CopyTo(ref handles_left);
data.ReadSplineHandlesRight(index, m_tmpFloat3);
m_tmpFloat3.CopyTo(ref handles_right);
for (int pointIndex = 0; pointIndex < cos.Length; pointIndex++) {
var co = cos[pointIndex];
var knot = new BezierKnot(co, handles_left[pointIndex] - co, handles_right[pointIndex] - co,
Quaternion.identity);
if (useNewSplines) {
spline.Add(knot);
}
else {
spline.SetKnot(pointIndex, knot);
}
}
}
if (useNewSplines) {
splineContainer.Splines = newSplines;
}
Spline.Changed += SplineChanged;
return rec;
}
#else
// If the curve was exported as a mesh before, delete this now, as it's handled as a curve:
TransformData dtrans = data.transform;
EntityRecord rec = UpdateTransformEntity(dtrans, config);
if (rec != null) rec.DestroyMeshRendererAndFilter();
return null;
#endif
}
#if AT_USE_SPLINES
protected virtual void SplineChanged(Spline spline, int arg2, SplineModification arg3)
{
// Overriden in Server.
}
#endif
}
}