Skip to content

Commit 161ab76

Browse files
committed
added support for animatoin Step export
1 parent 2080f34 commit 161ab76

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

Runtime/Scripts/SceneExporter/ExporterAnimation.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,9 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
915915
foreach (KeyValuePair<string, PropertyCurve> c in curves)
916916
{
917917
var prop = c.Value;
918-
if (BakePropertyAnimation(prop, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values))
918+
if (BakePropertyAnimation(prop, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values, out var interpolationType))
919919
{
920-
AddAnimationData(prop.target, prop.propertyName, animation, times, values);
920+
AddAnimationData(prop.target, prop.propertyName, animation, times, values, interpolationType);
921921
sampledAnimationData = true;
922922
}
923923
}
@@ -932,9 +932,9 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
932932
{
933933
var trp2 = new PropertyCurve(targetTr, "translation") { propertyType = typeof(Vector3) };
934934
trp2.curve.AddRange(curve.translationCurves);
935-
if (BakePropertyAnimation(trp2, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values2))
935+
if (BakePropertyAnimation(trp2, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values2, out var interpolationType))
936936
{
937-
AddAnimationData(targetTr, trp2.propertyName, animation, times, values2);
937+
AddAnimationData(targetTr, trp2.propertyName, animation, times, values2, interpolationType);
938938
sampledAnimationData = true;
939939
}
940940
}
@@ -943,9 +943,9 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
943943
{
944944
var trp3 = new PropertyCurve(targetTr, "rotation") { propertyType = typeof(Quaternion) };
945945
trp3.curve.AddRange(curve.rotationCurves.Where(x => x != null));
946-
if (BakePropertyAnimation(trp3, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values3))
946+
if (BakePropertyAnimation(trp3, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values3, out var interpolationType))
947947
{
948-
AddAnimationData(targetTr, trp3.propertyName, animation, times, values3);
948+
AddAnimationData(targetTr, trp3.propertyName, animation, times, values3, interpolationType);
949949
sampledAnimationData = true;
950950
}
951951

@@ -955,9 +955,9 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
955955
{
956956
var trp4 = new PropertyCurve(targetTr, "scale") { propertyType = typeof(Vector3) };
957957
trp4.curve.AddRange(curve.scaleCurves);
958-
if (BakePropertyAnimation(trp4, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values4))
958+
if (BakePropertyAnimation(trp4, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values4, out var interpolationType))
959959
{
960-
AddAnimationData(targetTr, trp4.propertyName, animation, times, values4);
960+
AddAnimationData(targetTr, trp4.propertyName, animation, times, values4, interpolationType);
961961
sampledAnimationData = true;
962962
}
963963
}
@@ -966,10 +966,10 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
966966
{
967967
var trp5 = new PropertyCurve(targetTr, "weights") { propertyType = typeof(float) };
968968
trp5.curve.AddRange(curve.weightCurves.Values);
969-
if (BakePropertyAnimation(trp5, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values5))
969+
if (BakePropertyAnimation(trp5, clip.length, AnimationBakingFramerate, speedMultiplier, out times, out var values5, out var interpolationType))
970970
{
971971
var targetComponent = targetTr.GetComponent<SkinnedMeshRenderer>();
972-
AddAnimationData(targetComponent, trp5.propertyName, animation, times, values5);
972+
AddAnimationData(targetComponent, trp5.propertyName, animation, times, values5, interpolationType);
973973
sampledAnimationData = true;
974974
}
975975
}
@@ -1398,12 +1398,13 @@ private AnimationCurve CreateConstantCurve(float value, float endTime)
13981398
return curve;
13991399
}
14001400

1401-
private bool BakePropertyAnimation(PropertyCurve prop, float length, float bakingFramerate, float speedMultiplier, out float[] times, out object[] values)
1401+
private bool BakePropertyAnimation(PropertyCurve prop, float length, float bakingFramerate, float speedMultiplier, out float[] times, out object[] values, out InterpolationType interpolationType)
14021402
{
14031403
var isReverse = speedMultiplier < 0;
14041404
speedMultiplier = Mathf.Abs(speedMultiplier);
14051405
times = null;
14061406
values = null;
1407+
interpolationType = InterpolationType.LINEAR;
14071408

14081409
prop.SortCurves();
14091410
if (!prop.Validate()) return false;
@@ -1418,6 +1419,20 @@ private bool BakePropertyAnimation(PropertyCurve prop, float length, float bakin
14181419
var keyframes = prop.curve.Select(x => x.keys).ToArray();
14191420
var keyframeIndex = new int[curveCount];
14201421

1422+
// Check if all samples are constant
1423+
var allConstant = true;
1424+
for (int i = 0; i < keyframes.Length; i++)
1425+
{
1426+
var kf = keyframes[i];
1427+
for (var k = 0; k < kf.Length; k++)
1428+
allConstant |= float.IsInfinity(kf[k].inTangent);
1429+
1430+
if (!allConstant) break;
1431+
}
1432+
1433+
if (allConstant)
1434+
interpolationType = InterpolationType.STEP;
1435+
14211436
// Assuming all the curves exist now
14221437
for (var i = 0; i < nbSamples; ++i)
14231438
{
@@ -1429,8 +1444,11 @@ private bool BakePropertyAnimation(PropertyCurve prop, float length, float bakin
14291444
keyframeIndex[k]++;
14301445

14311446
var isConstant = false;
1432-
for (var k = 0; k < curveCount; k++)
1433-
isConstant |= float.IsInfinity(keyframes[k][keyframeIndex[k]].inTangent);
1447+
if (interpolationType != InterpolationType.STEP)
1448+
{
1449+
for (var k = 0; k < curveCount; k++)
1450+
isConstant |= float.IsInfinity(keyframes[k][keyframeIndex[k]].inTangent);
1451+
}
14341452

14351453
if (isConstant && _times.Count > 0)
14361454
{

Runtime/Scripts/SceneExporter/ExporterAnimationPointer.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public bool ShouldMaterialPropertiesRemapped(Material material)
6262
/// _exporter.GetRoot().Animations.Add(_animationA);
6363
/// };
6464
/// </code></example>
65-
public void AddAnimationData(Object animatedObject, string propertyName, GLTFAnimation animation, float[] times, object[] values)
65+
public void AddAnimationData(Object animatedObject, string propertyName, GLTFAnimation animation, float[] times, object[] values, InterpolationType interpolation = InterpolationType.LINEAR)
6666
{
6767
if (!animatedObject) return;
6868

@@ -111,9 +111,12 @@ public void AddAnimationData(Object animatedObject, string propertyName, GLTFAni
111111
bool isBoolean = propertyType == typeof(bool);
112112
if (TryGetCurrentValue(animatedObject, propertyName, out var currentValue))
113113
{
114-
// For bool, we always want to export as byte (0/1). Unity is using float for animation curves of bool properties.
114+
// For bool, we always want to export as byte (0,255). Unity is using float for animation curves of bool properties.
115115
if (currentValue is bool)
116+
{
116117
isBoolean = true;
118+
interpolation = InterpolationType.STEP;
119+
}
117120
}
118121

119122
var animationPointerExportContext = _plugins.FirstOrDefault(x => x is AnimationPointerExportContext) as AnimationPointerExportContext;
@@ -331,6 +334,7 @@ public void AddAnimationData(Object animatedObject, string propertyName, GLTFAni
331334

332335
AnimationSampler Tsampler = new AnimationSampler();
333336
Tsampler.Input = timeAccessor;
337+
Tsampler.Interpolation = interpolation;
334338

335339
// for cases where one property needs to be split up into multiple tracks
336340
// example: emissiveFactor * emissiveStrength
@@ -340,12 +344,14 @@ public void AddAnimationData(Object animatedObject, string propertyName, GLTFAni
340344
Tchannel2.Target = TchannelTarget2;
341345
AnimationSampler Tsampler2 = new AnimationSampler();
342346
Tsampler2.Input = timeAccessor;
347+
Tsampler2.Interpolation = interpolation;
348+
343349
bool actuallyNeedSecondSampler = true;
344350

345351
var val = values[0];
346352
if (isBoolean)
347353
{
348-
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e => (float)e >= 0.5f ? Byte.MaxValue : Byte.MinValue));
354+
Tsampler.Output = ExportAccessor(Array.ConvertAll(values, e => (float)e >= 0.5f ? (byte)1 : Byte.MinValue));
349355
}
350356
else
351357
{

0 commit comments

Comments
 (0)