/
AlembicStreamPlayer.cs
94 lines (85 loc) · 2.78 KB
/
AlembicStreamPlayer.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
using UnityEngine;
namespace UTJ.Alembic
{
[ExecuteInEditMode]
public class AlembicStreamPlayer : MonoBehaviour
{
public AlembicStream Stream;
public AlembicStreamDescriptor streamDescriptor;
[SerializeField] public float currentTime;
[SerializeField] public int startFrame;
[SerializeField] public int endFrame;
[SerializeField] public float vertexMotionScale = 1.0f;
[SerializeField] public bool interpolateSamples = true;
float m_LastUpdateTime;
bool m_ForceUpdate = false;
public float Duration
{
get
{
return (endFrame- startFrame) * streamDescriptor.FrameLength;
}
}
void OnValidate()
{
if (streamDescriptor == null) return;
if (startFrame < streamDescriptor.minFrame) startFrame = streamDescriptor.minFrame;
if (startFrame > streamDescriptor.maxFrame) startFrame = streamDescriptor.maxFrame;
if (endFrame < startFrame) endFrame = startFrame;
if (endFrame > streamDescriptor.maxFrame) endFrame = streamDescriptor.maxFrame;
ClampTime();
m_ForceUpdate = true;
}
void LateUpdate()
{
if (Stream != null && streamDescriptor != null)
{
ClampTime();
if (m_LastUpdateTime != currentTime || m_ForceUpdate)
{
if (Stream.AbcUpdate(currentTime + startFrame * streamDescriptor.FrameLength + streamDescriptor.abcStartTime, vertexMotionScale, interpolateSamples))
{
m_LastUpdateTime = currentTime;
m_ForceUpdate = false;
}
else
{
Stream.Dispose();
LoadStream();
}
}
}
}
private void ClampTime()
{
float duration = Duration;
if (duration == .0f || currentTime < .0f)
currentTime = .0f;
else if (currentTime > duration)
currentTime = duration;
}
public void LoadStream()
{
if (streamDescriptor == null) return;
Stream = new AlembicStream(gameObject, streamDescriptor);
Stream.AbcLoad();
m_ForceUpdate = true;
}
void OnEnable()
{
if (Stream == null)
{
LoadStream();
}
}
public void OnDestroy()
{
if (Stream != null)
Stream.Dispose();
}
public void OnApplicationQuit()
{
AbcAPI.aiCleanup();
}
}
}