summaryrefslogtreecommitdiff
path: root/Runtime/Animation/MecanimUtility.h
blob: e6bfded8be1df6fd5f6dff93fe855b8f82e6a0d5 (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
119
120
121
122
123
#ifndef MECANIM_UTILITY_H
#define MECANIM_UTILITY_H

#include "Runtime/Math/Vector3.h"
#include "Runtime/Math/Quaternion.h"
#include "Runtime/Allocator/BaseAllocator.h"
#include "Runtime/Misc/AllocatorLabels.h"

#include "Runtime/mecanim/memory.h"
#include "Runtime/Math/Simd/xform.h"

#include "Runtime/Serialize/Blobification/BlobWrite.h"

#include <stack>
#include <algorithm>

std::string BuildTransitionName(std::string srcStateName, std::string dstStateName);

typedef std::map<mecanim::uint32_t, UnityStr> TOSVector;

static inline Vector3f float4ToVector3f(math::float4 const& v)
{
	ATTRIBUTE_ALIGN(ALIGN4F) float buf[4];
	math::store(v, buf);
	//return Vector3f(-buf[0],buf[1],buf[2]);
	return Vector3f(buf[0],buf[1],buf[2]);
}

static inline Quaternionf float4ToQuaternionf(math::float4 const& q)
{
	ATTRIBUTE_ALIGN(ALIGN4F) float buf[4];
	math::store(math::normalize(q), buf);
	//return Quaternionf(-buf[0],buf[1],buf[2],-buf[3]);
	return Quaternionf(buf[0],buf[1],buf[2],buf[3]);
}

static inline Quaternionf float4ToQuaternionfNoNormalize(math::float4 const& q)
{
	ATTRIBUTE_ALIGN(ALIGN4F) float buf[4];
	math::store(q, buf);
	return Quaternionf(buf[0],buf[1],buf[2],buf[3]);
}

static inline void xform2unity(math::xform const& x, Vector3f& t, Quaternionf& q, Vector3f& s)
{
	ATTRIBUTE_ALIGN(ALIGN4F) float bufS[4];	

	math::store(x.s, bufS);		

	t = float4ToVector3f(x.t);
	q = float4ToQuaternionf(x.q);
	s.Set(bufS[0], bufS[1], bufS[2]);		
}

static inline void xform2unityNoNormalize(math::xform const& x, Vector3f& t, Quaternionf& q, Vector3f& s)
{
	ATTRIBUTE_ALIGN(ALIGN4F) float bufS[4];	

	math::store(x.s, bufS);		

	t = float4ToVector3f(x.t);
	q = float4ToQuaternionfNoNormalize(x.q);
	s.Set(bufS[0], bufS[1], bufS[2]);		
}

static inline void xform2unity(math::xform const& x, Matrix4x4f& matrix)
{
	ATTRIBUTE_ALIGN(ALIGN4F) float bufS[4];	
	
	Vector3f t;
	Quaternionf q;
	Vector3f s;
	
	math::store(x.s, bufS);		
	
	t = float4ToVector3f(x.t);
	q = float4ToQuaternionf(x.q);
	s.Set(bufS[0], bufS[1], bufS[2]);
	matrix.SetTRS(t, q, s);
}


static inline math::float4 Vector3fTofloat4(Vector3f const& v, float w = 0)
{
	//ATTRIBUTE_ALIGN(ALIGN4F) float buf[4] = {-v.x, v.y, v.z, 0};
	ATTRIBUTE_ALIGN(ALIGN4F) float buf[4] = {v.x, v.y, v.z, w};
	return math::load(buf);
}

static inline math::float4 QuaternionfTofloat4(Quaternionf  const& q)
{
	//ATTRIBUTE_ALIGN(ALIGN4F) float buf[4] = {-q.x, q.y, q.z, -q.w};
	ATTRIBUTE_ALIGN(ALIGN4F) float buf[4] = {q.x, q.y, q.z, q.w};
	return math::load(buf);
}

static inline math::xform xformFromUnity(Vector3f const& t, Quaternionf const& q, Vector3f const& s)
{
	ATTRIBUTE_ALIGN(ALIGN4F) float bufS[4];

	bufS[0] = s.x; bufS[1] = s.y; bufS[2] = s.z; bufS[3] = 1.f; 
	return math::xform(Vector3fTofloat4(t), QuaternionfTofloat4(q), math::load(bufS));
}

std::string FileName(const std::string &fullpath);
std::string FileNameNoExt(const std::string &fullpath);

unsigned int ProccessString(TOSVector& tos, std::string const& str);
std::string FindString(TOSVector const& tos, unsigned int crc32);
template <typename T> inline T* CopyBlob(T const& data, mecanim::memory::Allocator& allocator, size_t& size)
{
	BlobWrite::container_type blob;
	BlobWrite blobWrite (blob, kNoTransferInstructionFlags, kBuildNoTargetPlatform);
	blobWrite.Transfer( const_cast<T&>(data), "Base");

	UInt8* ptr = reinterpret_cast<UInt8*>(allocator.Allocate(blob.size(), ALIGN_OF(T)));
	if(ptr != 0)
		memcpy(ptr, blob.begin(), blob.size());
	size = blob.size();
	return reinterpret_cast<T*>(ptr);
}

#endif