-
Notifications
You must be signed in to change notification settings - Fork 475
/
ExporterAnimationPointer.cs
649 lines (599 loc) · 25 KB
/
ExporterAnimationPointer.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
using System;
using System.Collections.Generic;
using System.Linq;
using GLTF.Schema;
using GLTF.Schema.KHR_lights_punctual;
using UnityEngine;
using UnityGLTF.Extensions;
using UnityGLTF.JsonPointer;
using UnityGLTF.Plugins;
using Object = UnityEngine.Object;
namespace UnityGLTF
{
public partial class GLTFSceneExporter
{
internal readonly List<IJsonPointerResolver> pointerResolvers = new List<IJsonPointerResolver>();
private readonly KHR_animation_pointer_Resolver animationPointerResolver = new KHR_animation_pointer_Resolver();
#region Shader Property Names
// ReSharper disable InconsistentNaming
private static readonly int _MainTex = Shader.PropertyToID("_MainTex");
private static readonly int _BaseMap = Shader.PropertyToID("_BaseMap");
private static readonly int _BaseColorTexture = Shader.PropertyToID("_BaseColorTexture");
private static readonly int baseColorTexture = Shader.PropertyToID("baseColorTexture");
private static readonly int _EmissionMap = Shader.PropertyToID("_EmissionMap");
private static readonly int _EmissiveTexture = Shader.PropertyToID("_EmissiveTexture");
private static readonly int emissiveTexture = Shader.PropertyToID("emissiveTexture");
private static readonly int _BumpMap = Shader.PropertyToID("_BumpMap");
private static readonly int _NormalTexture = Shader.PropertyToID("_NormalTexture");
private static readonly int normalTexture = Shader.PropertyToID("normalTexture");
private static readonly int _OcclusionMap = Shader.PropertyToID("_OcclusionMap");
private static readonly int _OcclusionTexture = Shader.PropertyToID("_OcclusionTexture");
private static readonly int occlusionTexture = Shader.PropertyToID("occlusionTexture");
// ReSharper restore InconsistentNaming
#endregion
public void RegisterResolver(IJsonPointerResolver resolver)
{
if (!pointerResolvers.Contains(resolver))
pointerResolvers.Add(resolver);
}
public void AddAnimationData(Object animatedObject, string propertyName, GLTFAnimation animation, float[] times, object[] values)
{
if (!animatedObject) return;
// TODO should skip property switches that are not supported without KHR_animation_pointer
// TODO should probably start with the transform check below and stop afterwards if KHR_animation_pointer is off
if (values.Length <= 0) return;
for (var i = 0; i < values.Length; i++)
{
if (values[i] != null) continue;
Debug.LogError(null, $"GLTFExporter error: value {i} in animated property \"{propertyName}\" is null. Skipping", animatedObject);
return;
}
var channelTargetId = GetIndex(animatedObject);
if (channelTargetId < 0)
{
Debug.LogWarning(null, $"Animation for {animatedObject.name} ({animatedObject.GetType()}) has not been exported as the object itself is not exported (disabled/EditorOnly). (InstanceID: {animatedObject.GetInstanceID()})", animatedObject);
return;
}
bool flipValueRange = false;
float? valueMultiplier = null;
bool isTextureTransform = false;
bool keepColorAlpha = true;
bool convertToLinearColor = false;
string secondPropertyName = null;
string extensionName = null;
var propertyType = values[0]?.GetType();
switch (animatedObject)
{
case Material material:
// Debug.Log("material: " + material + ", propertyName: " + propertyName);
// mapping from known Unity property names to glTF property names
switch (propertyName)
{
case "_Color":
case "_BaseColor":
case "_BaseColorFactor":
case "baseColorFactor":
propertyName = "pbrMetallicRoughness/baseColorFactor";
convertToLinearColor = true;
break;
case "_Smoothness":
case "_Glossiness":
propertyName = "pbrMetallicRoughness/roughnessFactor";
flipValueRange = true;
break;
case "_Roughness":
case "_RoughnessFactor":
case "roughnessFactor":
propertyName = "pbrMetallicRoughness/roughnessFactor";
break;
case "_Metallic":
case "_MetallicFactor":
case "metallicFactor":
propertyName = "pbrMetallicRoughness/metallicFactor";
break;
case "_MainTex_ST":
case "_BaseMap_ST":
case "_BaseColorTexture_ST":
case "baseColorTexture_ST":
if (!(material.HasProperty(_MainTex) && material.GetTexture(_MainTex)) &&
!(material.HasProperty(_BaseMap) && material.GetTexture(_BaseMap)) &&
!(material.HasProperty(_BaseColorTexture) && material.GetTexture(_BaseColorTexture)) &&
!(material.HasProperty(baseColorTexture) && material.GetTexture(baseColorTexture))) return;
propertyName = $"pbrMetallicRoughness/baseColorTexture/extensions/{ExtTextureTransformExtensionFactory.EXTENSION_NAME}/{ExtTextureTransformExtensionFactory.SCALE}";
secondPropertyName = $"pbrMetallicRoughness/baseColorTexture/extensions/{ExtTextureTransformExtensionFactory.EXTENSION_NAME}/{ExtTextureTransformExtensionFactory.OFFSET}";
isTextureTransform = true;
extensionName = ExtTextureTransformExtensionFactory.EXTENSION_NAME;
break;
case "_EmissionColor":
case "_EmissiveFactor":
case "emissiveFactor":
propertyName = "emissiveFactor";
secondPropertyName = $"extensions/{KHR_materials_emissive_strength_Factory.EXTENSION_NAME}/{nameof(KHR_materials_emissive_strength.emissiveStrength)}";
extensionName = KHR_materials_emissive_strength_Factory.EXTENSION_NAME;
keepColorAlpha = false;
convertToLinearColor = true;
break;
case "_EmissionMap_ST":
case "_EmissiveTexture_ST":
case "emissiveTexture_ST":
if (!(material.HasProperty(_EmissionMap) && material.GetTexture(_EmissionMap)) &&
!(material.HasProperty(_EmissiveTexture) && material.GetTexture(_EmissiveTexture)) &&
!(material.HasProperty(emissiveTexture) && material.GetTexture(emissiveTexture))) return;
propertyName = $"emissiveTexture/extensions/{ExtTextureTransformExtensionFactory.EXTENSION_NAME}/{ExtTextureTransformExtensionFactory.SCALE}";
secondPropertyName = $"emissiveTexture/extensions/{ExtTextureTransformExtensionFactory.EXTENSION_NAME}/{ExtTextureTransformExtensionFactory.OFFSET}";
isTextureTransform = true;
extensionName = ExtTextureTransformExtensionFactory.EXTENSION_NAME;
break;
case "_Cutoff":
case "_AlphaCutoff":
case "alphaCutoff":
propertyName = "alphaCutoff";
break;
case "_BumpScale":
case "_NormalScale":
case "normalScale":
case "normalTextureScale":
propertyName = "normalTexture/scale";
break;
case "_BumpMap_ST":
case "_NormalTexture_ST":
case "normalTexture_ST":
if (!(material.HasProperty(_BumpMap) && material.GetTexture(_BumpMap)) &&
!(material.HasProperty(_NormalTexture) && material.GetTexture(_NormalTexture)) &&
!(material.HasProperty(normalTexture) && material.GetTexture(normalTexture))) return;
propertyName = $"normalTexture/extensions/{ExtTextureTransformExtensionFactory.EXTENSION_NAME}/{ExtTextureTransformExtensionFactory.SCALE}";
secondPropertyName = $"normalTexture/extensions/{ExtTextureTransformExtensionFactory.EXTENSION_NAME}/{ExtTextureTransformExtensionFactory.OFFSET}";
isTextureTransform = true;
extensionName = ExtTextureTransformExtensionFactory.EXTENSION_NAME;
break;
case "_OcclusionStrength":
case "occlusionStrength":
case "occlusionTextureStrength":
propertyName = "occlusionTexture/strength";
break;
case "_OcclusionMap_ST":
case "_OcclusionTexture_ST":
case "occlusionTexture_ST":
if (!(material.HasProperty(_OcclusionMap) && material.GetTexture(_OcclusionMap)) &&
!(material.HasProperty(_OcclusionTexture) && material.GetTexture(_OcclusionTexture)) &&
!(material.HasProperty(occlusionTexture) && material.GetTexture(occlusionTexture))) return;
propertyName = $"occlusionTexture/extensions/{ExtTextureTransformExtensionFactory.EXTENSION_NAME}/{ExtTextureTransformExtensionFactory.SCALE}";
secondPropertyName = $"occlusionTexture/extensions/{ExtTextureTransformExtensionFactory.EXTENSION_NAME}/{ExtTextureTransformExtensionFactory.OFFSET}";
isTextureTransform = true;
extensionName = ExtTextureTransformExtensionFactory.EXTENSION_NAME;
break;
// TODO metallic/roughness _ST
// KHR_materials_transmission
case "_TransmissionFactor":
case "transmissionFactor":
propertyName = $"extensions/{KHR_materials_transmission_Factory.EXTENSION_NAME}/{nameof(KHR_materials_transmission.transmissionFactor)}";
extensionName = KHR_materials_transmission_Factory.EXTENSION_NAME;
break;
// KHR_materials_volume
case "_ThicknessFactor":
case "thicknessFactor":
propertyName = $"extensions/{KHR_materials_volume_Factory.EXTENSION_NAME}/{nameof(KHR_materials_volume.thicknessFactor)}";
extensionName = KHR_materials_volume_Factory.EXTENSION_NAME;
break;
case "_AttenuationDistance":
case "attenuationDistance":
propertyName = $"extensions/{KHR_materials_volume_Factory.EXTENSION_NAME}/{nameof(KHR_materials_volume.attenuationDistance)}";
extensionName = KHR_materials_volume_Factory.EXTENSION_NAME;
break;
case "_AttenuationColor":
case "attenuationColor":
propertyName = $"extensions/{KHR_materials_volume_Factory.EXTENSION_NAME}/{nameof(KHR_materials_volume.attenuationColor)}";
extensionName = KHR_materials_volume_Factory.EXTENSION_NAME;
keepColorAlpha = false;
break;
// KHR_materials_ior
case "_IOR":
case "ior":
propertyName = $"extensions/{KHR_materials_ior_Factory.EXTENSION_NAME}/{nameof(KHR_materials_ior.ior)}";
extensionName = KHR_materials_ior_Factory.EXTENSION_NAME;
break;
// KHR_materials_iridescence
case "_IridescenceFactor":
case "iridescenceFactor":
propertyName = $"extensions/{KHR_materials_iridescence_Factory.EXTENSION_NAME}/{nameof(KHR_materials_iridescence.iridescenceFactor)}";
extensionName = KHR_materials_iridescence_Factory.EXTENSION_NAME;
break;
case "_IridescenceIor":
case "iridescenceIor":
propertyName = $"extensions/{KHR_materials_iridescence_Factory.EXTENSION_NAME}/{nameof(KHR_materials_iridescence.iridescenceIor)}";
extensionName = KHR_materials_iridescence_Factory.EXTENSION_NAME;
break;
case "_IridescenceThicknessMinimum":
case "iridescenceThicknessMinimum":
propertyName = $"extensions/{KHR_materials_iridescence_Factory.EXTENSION_NAME}/{nameof(KHR_materials_iridescence.iridescenceThicknessMinimum)}";
extensionName = KHR_materials_iridescence_Factory.EXTENSION_NAME;
break;
case "_IridescenceThicknessMaximum":
case "iridescenceThicknessMaximum":
propertyName = $"extensions/{KHR_materials_iridescence_Factory.EXTENSION_NAME}/{nameof(KHR_materials_iridescence.iridescenceThicknessMaximum)}";
extensionName = KHR_materials_iridescence_Factory.EXTENSION_NAME;
break;
// KHR_materials_specular
case "_SpecularFactor":
case "specularFactor":
propertyName = $"extensions/{KHR_materials_specular_Factory.EXTENSION_NAME}/{nameof(KHR_materials_specular.specularFactor)}";
extensionName = KHR_materials_specular_Factory.EXTENSION_NAME;
break;
case "_SpecularColorFactor":
case "specularColorFactor":
propertyName = $"extensions/{KHR_materials_specular_Factory.EXTENSION_NAME}/{nameof(KHR_materials_specular.specularColorFactor)}";
extensionName = KHR_materials_specular_Factory.EXTENSION_NAME;
keepColorAlpha = false;
break;
// TODO KHR_materials_clearcoat
// case "_ClearcoatFactor":
// case "clearcoatFactor":
// propertyName = $"extensions/{KHR_materials_clearcoat_Factory.EXTENSION_NAME}/{nameof(KHR_materials_clearcoat.clearcoatFactor)}";
// extensionName = KHR_materials_clearcoat_Factory.EXTENSION_NAME;
// break;
// case "_ClearcoatRoughnessFactor":
// case "clearcoatRoughnessFactor":
// propertyName = $"extensions/{KHR_materials_clearcoat_Factory.EXTENSION_NAME}/{nameof(KHR_materials_clearcoat.clearcoatRoughnessFactor)}";
// extensionName = KHR_materials_clearcoat_Factory.EXTENSION_NAME;
// break;
// TODO KHR_materials_sheen
// case "_SheenColorFactor":
// case "sheenColorFactor":
// propertyName = $"extensions/{KHR_materials_sheen_Factory.EXTENSION_NAME}/{nameof(KHR_materials_sheen.sheenColorFactor)}";
// extensionName = KHR_materials_sheen_Factory.EXTENSION_NAME;
// keepColorAlpha = false;
// break;
// case "_SheenRoughnessFactor":
// case "sheenRoughnessFactor":
// propertyName = $"extensions/{KHR_materials_sheen_Factory.EXTENSION_NAME}/{nameof(KHR_materials_sheen.sheenRoughnessFactor)}";
// extensionName = KHR_materials_sheen_Factory.EXTENSION_NAME;
// break;
default:
Debug.Log(LogType.Warning, "Unknown property name on Material " + material + ": " + propertyName);
break;
}
break;
case Light light:
extensionName = KHR_lights_punctualExtensionFactory.EXTENSION_NAME;
switch (propertyName)
{
case "m_Color":
propertyName = $"color";
keepColorAlpha = false;
convertToLinearColor = true;
break;
case "m_Intensity":
valueMultiplier = Mathf.PI; // matches ExportLight
propertyName = $"intensity";
break;
case "m_SpotAngle":
valueMultiplier = Mathf.Deg2Rad / 2;
propertyName = $"spot/outerConeAngle";
break;
case "m_InnerSpotAngle":
valueMultiplier = Mathf.Deg2Rad / 2;
propertyName = $"spot/innerConeAngle";
break;
case "m_Range":
propertyName = "range";
break;
default:
extensionName = null;
Debug.Log(LogType.Warning, "Unknown property name on Light " + light + ": " + propertyName);
break;
}
break;
case Camera camera:
if(camera.orthographic)
{
switch (propertyName)
{
case "orthographic size":
// TODO conversion factor
propertyName = "orthographic/ymag";
secondPropertyName = "orthographic/xmag";
break;
case "near clip plane":
propertyName = "orthographic/znear";
break;
case "far clip plane":
propertyName = "orthographic/zfar";
break;
default:
Debug.Log(LogType.Warning, "Unknown property name on Camera " + camera + ": " + propertyName);
break;
}
}
else
{
switch (propertyName)
{
case "field of view":
// TODO conversion factor
propertyName = "perspective/yfov";
valueMultiplier = Mathf.Deg2Rad;
break;
case "near clip plane":
propertyName = "perspective/znear";
break;
case "far clip plane":
propertyName = "perspective/zfar";
break;
case "backgroundColor":
convertToLinearColor = true;
break;
default:
Debug.Log(LogType.Warning, "Unknown property name on Camera " + camera + ": " + propertyName);
break;
}
}
break;
case SkinnedMeshRenderer skinnedMesh:
// this code is adapted from SkinnedMeshRendererEditor (which calculates the right range for sliders to show)
// instead of calculating per blend shape, we're assuming all blendshapes have the same min/max here though.
var minBlendShapeFrameWeight = 0.0f;
var maxBlendShapeFrameWeight = 0.0f;
var sharedMesh = skinnedMesh.sharedMesh;
if (!sharedMesh)
{
Debug.Log(LogType.Error, "No mesh on SkinnedMeshRenderer " + skinnedMesh + ", skipping animation");
return;
}
var shapeCount = sharedMesh.blendShapeCount;
for (int index = 0; index < shapeCount; ++index)
{
var blendShapeFrameCount = sharedMesh.GetBlendShapeFrameCount(index);
for (var frameIndex = 0; frameIndex < blendShapeFrameCount; ++frameIndex)
{
var shapeFrameWeight = sharedMesh.GetBlendShapeFrameWeight(index, frameIndex);
minBlendShapeFrameWeight = Mathf.Min(shapeFrameWeight, minBlendShapeFrameWeight);
maxBlendShapeFrameWeight = Mathf.Max(shapeFrameWeight, maxBlendShapeFrameWeight);
}
}
if (maxBlendShapeFrameWeight != 0)
valueMultiplier = 1.0f / maxBlendShapeFrameWeight;
break;
case Transform _:
// generally allowed and already in the right format
break;
default:
// propertyName is exported as-is
// Debug.LogWarning($"Implicitly handling animated property \"{propertyName}\" for target {animatedObject}", animatedObject);
// filtering for what to include / what not to include based on whether its target can be resolved
if (UseAnimationPointer && animatedObject is Component _)
{
var couldResolve = false;
var prop = $"/nodes/{channelTargetId}/{propertyName}";
foreach (var res in pointerResolvers)
{
// TODO: ideally we have a new method here to just ask the resolver if it supports that type
if (res.TryResolve(animatedObject, ref prop))
{
couldResolve = true;
}
}
if (!couldResolve)
{
return;
}
// If the animated property is a color, we need to convert to linear –
// we're doing the same on regular non-animated property export.
if (propertyType == typeof(Color))
convertToLinearColor = true;
}
break;
}
var Node = new NodeId
{
Id = channelTargetId,
Root = _root
};
AccessorId timeAccessor = ExportAccessor(times);
AnimationChannel Tchannel = new AnimationChannel();
AnimationChannelTarget TchannelTarget = new AnimationChannelTarget() { Path = propertyName, Node = Node };
Tchannel.Target = TchannelTarget;
AnimationSampler Tsampler = new AnimationSampler();
Tsampler.Input = timeAccessor;
// for cases where one property needs to be split up into multiple tracks
// example: emissiveFactor * emissiveStrength
// TODO not needed when secondPropertyName==null
AnimationChannel Tchannel2 = new AnimationChannel();
AnimationChannelTarget TchannelTarget2 = new AnimationChannelTarget() { Path = secondPropertyName, Node = Node };
Tchannel2.Target = TchannelTarget2;
AnimationSampler Tsampler2 = new AnimationSampler();
Tsampler2.Input = timeAccessor;
bool actuallyNeedSecondSampler = true;
var val = values[0];
switch (val)
{
case float _:
if (flipValueRange)
{
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e => 1.0f - (float)e));
}
else if (valueMultiplier.HasValue)
{
var multiplier = valueMultiplier.Value;
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e => ((float)e) * multiplier));
}
else
{
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e => (float)e));
if (propertyName == "orthographic/ymag")
{
Tsampler2.Output = Tsampler.Output;
}
}
break;
case float[] _:
// check that all entries have the same length using a for loop
var firstLength = ((float[])values[0]).Length;
for (var i = 1; i < values.Length; i++)
{
if (((float[])values[i]).Length == firstLength) continue;
Debug.Log(LogType.Error, "When animating float arrays, all array entries must have the same length. Skipping");
return;
}
// construct a float array of all the float arrays together
var floatArray = new float[values.Length * firstLength];
// copy the individual arrays into the float array using Array.Copy
for (int i = 0; i < values.Length; i++)
Array.Copy((float[])values[i], 0, floatArray, i * firstLength, firstLength);
// glTF weights 0..1 match to Unity weights 0..100, but Unity weights can be in arbitrary ranges
if (valueMultiplier.HasValue)
{
for (var i = 0; i < floatArray.Length; i++)
floatArray[i] *= valueMultiplier.Value;
}
Tsampler.Output = ExportAccessor(floatArray);
break;
case Vector2 _:
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e => (Vector2)e));
break;
case Vector3 _:
if (propertyName == "translation")
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e =>
{
var v = (Vector3)e;
v.Scale(new Vector3(-1, 1, 1));
return v;
}));
else
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e => (Vector3)e));
break;
case Vector4 _:
if (!isTextureTransform)
{
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e => (Vector4)e));
}
else
{
var scales = new Vector2[values.Length];
var offsets = new Vector2[values.Length];
for (int i = 0; i < values.Length; i++)
{
DecomposeScaleOffset((Vector4) values[i], out var scale, out var offset);
scales[i] = scale;
offsets[i] = offset;
}
Tsampler.Output = ExportAccessor(scales);
Tsampler2.Output = ExportAccessor(offsets);
}
break;
case Quaternion _:
var animatedNode = _root.Nodes[channelTargetId];
var needsFlippedLookDirection = animatedNode.Light != null || animatedNode.Camera != null;
Tsampler.Output = ExportAccessorSwitchHandedness(Array.ConvertAll(values, e => (Quaternion)e), needsFlippedLookDirection); // Vec4 for rotations
break;
case Color _:
if (propertyName == "emissiveFactor" && secondPropertyName != null)
{
var colors = new Color[values.Length];
var strengths = new float[values.Length];
actuallyNeedSecondSampler = false;
var pluginSettings = (_plugins.FirstOrDefault(x => x is MaterialExtensions) as MaterialExtensions)?.settings;
var emissiveStrengthSupported = pluginSettings != null && pluginSettings.KHR_materials_emissive_strength;
if (emissiveStrengthSupported)
{
for (int i = 0; i < values.Length; i++)
{
DecomposeEmissionColor((Color) values[i], out var color, out var intensity);
colors[i] = color;
strengths[i] = intensity;
if (intensity > 1)
actuallyNeedSecondSampler = true;
}
}
else
{
// clamp 0..1
for (int i = 0; i < values.Length; i++)
{
var c = (Color) values[i];
if (c.r > 1) c.r = 1;
if (c.g > 1) c.g = 1;
if (c.b > 1) c.b = 1;
colors[i] = c;
}
}
Tsampler.Output = ExportAccessor(colors, false);
if (emissiveStrengthSupported)
Tsampler2.Output = ExportAccessor(strengths);
}
else
{
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e =>
{
var c = (Color) e;
if (convertToLinearColor)
c = c.linear;
return c;
}), keepColorAlpha);
}
break;
}
if (Tsampler.Output != null) Tsampler.Output.Value.BufferView.Value.ByteStride = 0;
if (Tsampler2.Output != null) Tsampler2.Output.Value.BufferView.Value.ByteStride = 0;
if (Tsampler.Output == null)
{
Debug.LogError($"GLTFExporter: Something went wrong, empty sampler output for \"{propertyName}\" in {animatedObject}", animatedObject);
return;
}
Tchannel.Sampler = new AnimationSamplerId
{
Id = animation.Samplers.Count,
GLTFAnimation = animation,
Root = _root
};
animation.Samplers.Add(Tsampler);
animation.Channels.Add(Tchannel);
if (UseAnimationPointer)
ConvertToAnimationPointer(animatedObject, propertyName, TchannelTarget);
// in some cases, extensions aren't required even when we think they might, e.g. for emission color animation.
// if all animated values are below 1, we don't need a separate channel for emissive_intensity.
if (!actuallyNeedSecondSampler)
{
extensionName = null;
secondPropertyName = null;
}
if (extensionName != null)
{
DeclareExtensionUsage(extensionName, false);
// add extension to material if needed
if(animatedObject is Material material)
{
var mat = GetMaterialId(_root, material);
if (mat.Value.Extensions == null || !mat.Value.Extensions.ContainsKey(extensionName))
{
mat.Value.AddExtension(extensionName, GLTFProperty.CreateEmptyExtension(extensionName));
}
}
}
if (secondPropertyName != null)
{
Tchannel2.Sampler = new AnimationSamplerId
{
Id = animation.Samplers.Count,
GLTFAnimation = animation,
Root = _root
};
animation.Samplers.Add(Tsampler2);
animation.Channels.Add(Tchannel2);
if (UseAnimationPointer)
ConvertToAnimationPointer(animatedObject, secondPropertyName, TchannelTarget2);
}
}
void ConvertToAnimationPointer(object animatedObject, string propertyName, AnimationChannelTarget target)
{
var ext = new KHR_animation_pointer();
ext.propertyBinding = propertyName;
ext.animatedObject = animatedObject;
ext.channel = target;
animationPointerResolver.Add(ext);
target.Node = null;
target.Path = "pointer";
target.AddExtension(KHR_animation_pointer.EXTENSION_NAME, ext);
DeclareExtensionUsage(KHR_animation_pointer.EXTENSION_NAME, false);
}
}
}