/
AlembicStreamPlayer.cs
104 lines (92 loc) · 3.24 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
95
96
97
98
99
100
101
102
103
104
using UnityEngine;
namespace UTJ.Alembic
{
[ExecuteInEditMode]
public class AlembicStreamPlayer : MonoBehaviour
{
public AlembicStream m_stream;
public AlembicStreamDescriptor streamDescriptor;
[SerializeField] public double m_currentTime;
[SerializeField] public double m_startTime = double.MinValue;
[SerializeField] public double m_endTime = double.MaxValue;
[SerializeField] public float m_vertexMotionScale = 1.0f;
[SerializeField] public bool m_asyncLoad = true;
double m_lastUpdateTime;
bool m_forceUpdate = false;
bool m_updateStarted = false;
public double duration { get { return m_endTime - m_startTime; } }
void Start()
{
OnValidate();
}
void OnValidate()
{
if (streamDescriptor == null || m_stream == null)
return;
if (streamDescriptor.abcStartTime == double.MinValue)
streamDescriptor.abcStartTime = m_stream.abcTimeRange.startTime;
if (streamDescriptor.abcEndTime == double.MaxValue)
streamDescriptor.abcEndTime = m_stream.abcTimeRange.endTime;
m_startTime = Mathf.Clamp((float)m_startTime, (float)streamDescriptor.abcStartTime, (float)streamDescriptor.abcEndTime);
m_endTime = Mathf.Clamp((float)m_endTime, (float)m_startTime, (float)streamDescriptor.abcEndTime);
ClampTime();
m_forceUpdate = true;
}
void Update()
{
if (m_stream == null || streamDescriptor == null)
return;
ClampTime();
if (m_lastUpdateTime != m_currentTime || m_forceUpdate)
{
m_stream.vertexMotionScale = m_vertexMotionScale;
m_stream.asyncLoad = m_asyncLoad;
if (m_stream.AbcUpdateBegin(m_startTime + m_currentTime))
{
m_lastUpdateTime = m_currentTime;
m_forceUpdate = false;
m_updateStarted = true;
}
else
{
m_stream.Dispose();
m_stream = null;
LoadStream();
}
}
}
void LateUpdate()
{
if (!m_updateStarted)
return;
m_updateStarted = false;
m_stream.AbcUpdateEnd();
}
private void ClampTime()
{
m_currentTime = Mathf.Clamp((float)m_currentTime, 0.0f, (float)duration);
}
public void LoadStream()
{
if (streamDescriptor == null)
return;
m_stream = new AlembicStream(gameObject, streamDescriptor);
m_stream.AbcLoad();
m_forceUpdate = true;
}
void OnEnable()
{
if (m_stream == null)
LoadStream();
}
public void OnDestroy()
{
if (m_stream != null)
m_stream.Dispose();
}
public void OnApplicationQuit()
{
AbcAPI.aiCleanup();
}
}
}