forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampledAnimationClipPlaybackSystem.cs
More file actions
56 lines (48 loc) · 2.69 KB
/
Copy pathSampledAnimationClipPlaybackSystem.cs
File metadata and controls
56 lines (48 loc) · 2.69 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
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
namespace Samples.Boids
{
public class SampledAnimationClipPlaybackSystem : JobComponentSystem
{
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var deltaTime = math.min(0.05f,Time.DeltaTime);
var transformJobHandle = Entities.ForEach((ref Translation translation, ref Rotation rotation, in SampledAnimationClip sampledAnimationClip) =>
{
var frameIndex = sampledAnimationClip.FrameIndex;
var timeOffset = sampledAnimationClip.TimeOffset;
// Be careful not to cache Value (or any field in Value like Samples) inside of blob asset.
var prevTranslation = sampledAnimationClip.TransformSamplesBlob.Value.TranslationSamples[frameIndex];
var nextTranslation = sampledAnimationClip.TransformSamplesBlob.Value.TranslationSamples[frameIndex+1];
var prevRotation = sampledAnimationClip.TransformSamplesBlob.Value.RotationSamples[frameIndex];
var nextRotation = sampledAnimationClip.TransformSamplesBlob.Value.RotationSamples[frameIndex+1];
translation.Value = math.lerp(prevTranslation, nextTranslation, timeOffset);
rotation.Value = math.slerp(prevRotation, nextRotation, timeOffset);
}).Schedule(inputDeps);
var clipJobHandle = Entities.ForEach((ref SampledAnimationClip sampledAnimationClip) =>
{
var currentTime = sampledAnimationClip.CurrentTime + deltaTime;
var sampleRate = sampledAnimationClip.SampleRate;
var frameIndex = (int)(currentTime / sampledAnimationClip.SampleRate);
var timeOffset = (currentTime - (frameIndex * sampleRate)) * (1.0f / sampleRate);
// Just restart loop when over end:
// - Don't interpolate between last and first frame.
// - Don't worry about interpolating time into the start of the loop.
// - Don't worry too much about exactly what the last frame even means.
if (frameIndex >= (sampledAnimationClip.FrameCount - 2))
{
currentTime = 0.0f;
frameIndex = 0;
timeOffset = 0.0f;
}
sampledAnimationClip.CurrentTime = currentTime;
sampledAnimationClip.FrameIndex = frameIndex;
sampledAnimationClip.TimeOffset = timeOffset;
}).Schedule(transformJobHandle);
return clipJobHandle;
}
}
}