summaryrefslogtreecommitdiff
path: root/Runtime/Animation/AvatarPlayback.cpp
blob: 1fd09af2b87f56aaa0a6f53668ad7af6777b55be (plain)
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
112
113
114
115
116
117
118
#include "UnityPrefix.h"

#include "AvatarPlayback.h"

#include "Runtime/mecanim/animation/avatar.h"
#include "Runtime/Serialize/Blobification/BlobWrite.h"
#include "Runtime/Animation/MecanimUtility.h"
#include "Runtime/Utilities/LogAssert.h"

enum  { kMaxFrameCount = 10000};

AvatarPlayback::AvatarPlayback(MemLabelId label) 
:	m_Alloc(label),
	m_FrameCount(-1),
	m_CursorIndex(-1),
	m_StartIndex(-1),
	m_StopIndex(-1)
{
}

void AvatarPlayback::Init(int frameCount)
{
	Clear();
	if(frameCount > 0 )
	{
		if(frameCount > kMaxFrameCount )
			WarningString("Could not allocate requested frameCount for Animator Recording. 10000 frames where allocated.");
		
		m_Frames.resize(min<unsigned int>(frameCount,kMaxFrameCount));	
		m_FrameCount = m_Frames.size();		
	}
	else 
	{
		m_FrameCount = 0 ;
	}
	
	m_CursorIndex = -1;
	m_StartIndex = -1 ;
	m_StopIndex = -1;
}

void AvatarPlayback::Clear()
{
	for(int i = 0 ; i < m_Frames.size() ; i++)
	{
		mecanim::animation::DestroyAvatarMemory(m_Frames[i].m_AvatarMemory, m_Alloc);
	}
	m_Frames.clear();
}

int AvatarPlayback::NextIndex(int index)
{
	return m_FrameCount > 0 ? (index+1)%m_FrameCount : index+1;
}

mecanim::animation::AvatarMemory* AvatarPlayback::PlayFrame(float time, float &effectiveTime)
{	
	int frameIndex = m_StopIndex;
	bool  found = false;

	if(m_StartIndex == -1)
		return 0;

	int i = m_StartIndex;
	int prevIndex = i;

	int endIndex = NextIndex(m_StopIndex);
	do // at this point, we have a least one frame recorded
	{
		if(m_Frames[i].m_CurrentTime > time)
		{
			frameIndex = prevIndex;
			found = true;
		}

		prevIndex = i;
		i = NextIndex(i);
	} while(i != endIndex && !found);
	

	effectiveTime = m_Frames[frameIndex].m_CurrentTime;
	m_CursorIndex = frameIndex;
	
	return m_Frames[frameIndex].m_AvatarMemory;	
}

void AvatarPlayback::RecordFrame(float deltaTime, const mecanim::animation::AvatarMemory* srcMemory)
{
	if(m_FrameCount == -1)
	{
		WarningString("Could not record Animator. Frame allocation has failed.");
		return;
	}

	AvatarFrame newFrame;
	if(m_StartIndex != -1)
		newFrame.m_CurrentTime = m_Frames[m_CursorIndex].m_CurrentTime + deltaTime;

	size_t size=0;
	newFrame.m_AvatarMemory = CopyBlob( *srcMemory, m_Alloc, size);
	
	m_CursorIndex = NextIndex(m_CursorIndex);
	if(m_StartIndex == m_CursorIndex|| m_StartIndex == -1) // increment startIndex when the cursor is writing on it (the buffer is full)
	{
		if(m_StartIndex != -1)
			mecanim::animation::DestroyAvatarMemory(m_Frames[m_CursorIndex].m_AvatarMemory, m_Alloc);
		m_StartIndex = NextIndex(m_StartIndex);
	}
	m_StopIndex = m_CursorIndex;
	if(m_FrameCount > 0)
		m_Frames[m_CursorIndex] = newFrame;
	else
		m_Frames.push_back(newFrame);
}

float AvatarPlayback::CursorTime() {return m_CursorIndex != -1 ? m_Frames[m_CursorIndex].m_CurrentTime : -1;}
float AvatarPlayback::StartTime() {return m_CursorIndex != -1 ? m_Frames[m_StartIndex].m_CurrentTime  : -1;}
float AvatarPlayback::StopTime() {return m_CursorIndex != -1 ? m_Frames[m_StopIndex].m_CurrentTime  : -1;}