/
AlembicStreamPlayer.cs
111 lines (96 loc) · 3.4 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
105
106
107
108
109
110
111
using UnityEngine;
namespace UTJ.Alembic
{
[ExecuteInEditMode]
public class AlembicStreamPlayer : MonoBehaviour
{
// "m_" prefix is intentionally missing and expose fields as public just to keep asset compatibility...
public AlembicStream abcStream;
public AlembicStreamDescriptor streamDescriptor;
public double startTime = double.MinValue;
public double endTime = double.MaxValue;
public float currentTime;
public float vertexMotionScale = 1.0f;
public bool asyncLoad = true;
float lastUpdateTime;
bool forceUpdate = false;
bool updateStarted = false;
public double duration { get { return endTime - startTime; } }
void ClampTime()
{
currentTime = Mathf.Clamp((float)currentTime, 0.0f, (float)duration);
}
public void LoadStream(bool createMissingNodes)
{
if (streamDescriptor == null)
return;
abcStream = new AlembicStream(gameObject, streamDescriptor);
abcStream.AbcLoad(createMissingNodes);
forceUpdate = true;
}
#region messages
void Start()
{
OnValidate();
}
void OnValidate()
{
if (streamDescriptor == null || abcStream == null)
return;
if (streamDescriptor.abcStartTime == double.MinValue || streamDescriptor.abcEndTime == double.MaxValue)
abcStream.GetTimeRange(ref streamDescriptor.abcStartTime, ref streamDescriptor.abcEndTime);
startTime = Mathf.Clamp((float)startTime, (float)streamDescriptor.abcStartTime, (float)streamDescriptor.abcEndTime);
endTime = Mathf.Clamp((float)endTime, (float)startTime, (float)streamDescriptor.abcEndTime);
ClampTime();
forceUpdate = true;
}
public void Update()
{
if (abcStream == null || streamDescriptor == null)
return;
ClampTime();
if (lastUpdateTime != currentTime || forceUpdate)
{
abcStream.vertexMotionScale = vertexMotionScale;
abcStream.asyncLoad = asyncLoad;
if (abcStream.AbcUpdateBegin(startTime + currentTime))
{
lastUpdateTime = currentTime;
forceUpdate = false;
updateStarted = true;
}
else
{
abcStream.Dispose();
abcStream = null;
LoadStream(false);
}
}
}
void LateUpdate()
{
// currentTime maybe updated after Update() by other GameObjects
if (!updateStarted && lastUpdateTime != currentTime)
Update();
if (!updateStarted)
return;
updateStarted = false;
abcStream.AbcUpdateEnd();
}
void OnEnable()
{
if (abcStream == null)
LoadStream(false);
}
void OnDestroy()
{
if (abcStream != null)
abcStream.Dispose();
}
void OnApplicationQuit()
{
AbcAPI.aiCleanup();
}
#endregion
}
}