summaryrefslogtreecommitdiff
path: root/Runtime/mecanim/animation/denseclip.cpp
blob: 3c19e47c4d820b8df6f41f5dc507abdf1c9fb1aa (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
#include "UnityPrefix.h"
#include "denseclip.h"
#include "Runtime/Math/Simd/math.h"
namespace mecanim
{
namespace animation
{
		
static void BlendArray(const float* lhs, const float* rhs, size_t size, float t, float* output)
{
	for (int i=0;i<size;i++)
		output[i] = math::lerp(lhs[i], rhs[i], t); 
}
	
	
static void  PrepareBlendValues (const DenseClip& clip, float time, float*& lhs, float*& rhs, float& u)	
{
	time -= clip.m_BeginTime;
	
	float index;
	u = math::modf(time * clip.m_SampleRate, index);

	int lhsIndex = (int)index;
	int rhsIndex = lhsIndex + 1;
	lhsIndex = math::maximum(0, lhsIndex);
	lhsIndex = math::minimum(clip.m_FrameCount-1, lhsIndex);
	
	rhsIndex = math::maximum(0, rhsIndex);
	rhsIndex = math::minimum(clip.m_FrameCount-1, rhsIndex);
	
	lhs = const_cast<float*> (&clip.m_SampleArray[lhsIndex * clip.m_CurveCount]);
	rhs = const_cast<float*> (&clip.m_SampleArray[rhsIndex * clip.m_CurveCount]);
}

void  SampleClip                 (const DenseClip& clip, float time, float* output)
{
	float u;
	float* lhsPtr;
	float* rhsPtr;
	PrepareBlendValues(clip, time, lhsPtr, rhsPtr, u);
	
	BlendArray(lhsPtr, rhsPtr, clip.m_CurveCount, u, output);
}

float SampleClipAtIndex          (const DenseClip& clip, int curveIndex, float time)
{
	float u;
	float* lhsPtr;
	float* rhsPtr;
	PrepareBlendValues(clip, time, lhsPtr, rhsPtr, u);
	
	return math::lerp(lhsPtr[curveIndex], rhsPtr[curveIndex], u); 
}


// Creation & Destruction
void                DestroyDenseClip        (DenseClip& clip, memory::Allocator& alloc)
{
	alloc.Deallocate(clip.m_SampleArray);
}

}
}