summaryrefslogtreecommitdiff
path: root/Runtime/Animation/Animation.cpp
blob: a2d7cda35ac1ba6ada3df191b0438b39a452a264 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
#include "UnityPrefix.h"
#include "Animation.h"
#include "AnimationManager.h"
#include "AnimationClip.h"
#include "AnimationClipUtility.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Serialize/TransferFunctions/TransferNameConversions.h"
#include "Runtime/BaseClasses/IsPlaying.h"
#include "Runtime/Utilities/Utility.h"
#include "AnimationBinder.h"
#include "NewAnimationTrack.h"
#include "AnimationState.h"
#include "Runtime/Input/TimeManager.h"
#include "Runtime/Graphics/Transform.h"
#include "Runtime/Profiler/Profiler.h"
#include "Runtime/Shaders/Material.h"
#include "AnimationCurveUtility.h"
#include "Runtime/Serialize/TransferUtility.h"
#include "Runtime/Mono/MonoScript.h"
#include "Runtime/Filters/Renderer.h"
#include "Runtime/Utilities/dense_hash_map.h"
#include "Runtime/Utilities/Prefetch.h"
#include "Runtime/Math/Rect.h"
#include "Runtime/Animation/AnimationUtility.h"
#include "Runtime/BaseClasses/EventIDs.h"

using std::make_pair;
using std::max;

PROFILER_INFORMATION (gBuildAnimationState, "Animation.RebuildInternalState", kProfilerAnimation);
PROFILER_INFORMATION (gUpdateAnimation, "Animation.Update", kProfilerAnimation);
PROFILER_INFORMATION (gSampleAnimation, "Animation.Sample", kProfilerAnimation);
PROFILER_WARNING (gDidDestroyObjectNotification, "Animation.DestroyAnimationClip [Triggers RebuildInternalState]", kProfilerAnimation);
PROFILER_WARNING (gAddClip, "Animation.AddClip [Triggers RebuildInternalState]", kProfilerAnimation);
PROFILER_WARNING (gRemoveClip, "Animation.RemoveClip [Triggers RebuildInternalState]", kProfilerAnimation);
PROFILER_WARNING (gCloneAnimationState, "Animation.Clone [Triggers RebuildInternalState]", kProfilerAnimation);
PROFILER_WARNING (gAnimationDeactivate, "Animation.Deactivate [Triggers RebuildInternalState]", kProfilerAnimation);
PROFILER_INFORMATION (gValidate, "ValidateBoundCurves", kProfilerAnimation);

inline int CombineWrapMode (int clipWrapMode, int animationWrapMode)
{
	if (clipWrapMode != 0)
		return clipWrapMode;
	else
		return animationWrapMode;
}

Animation::Animation (MemLabelId label, ObjectCreationMode mode)
:	Super(label, mode)
,	m_AnimationManagerNode(this)
,	m_ActiveAnimationStatesSize(0)
,	m_CullingType(kCulling_AlwaysAnimate)
,   m_BoundCurves (label)
{
	m_WrapMode = 0;
	
	m_PlayAutomatically = true;
	m_AnimatePhysics = false;
	m_DirtyMask = 0;
	m_Visible = false;
	memset (m_ActiveAnimationStates, 0, sizeof(m_ActiveAnimationStates));
}

Animation::~Animation () 
{
	ClearContainedRenderers ();
	ReleaseAnimationStates ();
	CleanupBoundCurves();
}


void Animation::AwakeFromLoad (AwakeFromLoadMode awakeMode)
{
	Super::AwakeFromLoad (awakeMode);
	
#if UNITY_EDITOR
	LoadOldAnimations();
#endif
	
	// We don't know what kind of culling we used before, so we clear all culling related data
	ClearContainedRenderers();

	CheckIsCullingBasedOnBoundsDeprecated();

	if (m_CullingType == kCulling_BasedOnRenderers && !m_AnimationStates.empty())
		RecomputeContainedRenderers ();
	
	if (m_PlayAutomatically && (awakeMode & (kDidLoadFromDisk | kInstantiateOrCreateFromCodeAwakeFromLoad | kActivateAwakeFromLoad)) && IsActive () && IsWorldPlaying())
		Play (kStopAll);	
}

void Animation::Deactivate (DeactivateOperation operation)
{
	if (operation != kDeprecatedDeactivateToggleForLevelLoad)
	{
		PROFILER_AUTO(gAnimationDeactivate, this)
		//ReleaseAnimationStates ();
		Stop();
		CleanupBoundCurves();
	}
}

AnimationClip* Animation::GetClipWithNameSerialized (const std::string& name)
{
	for (Animations::iterator i=m_Animations.begin();i != m_Animations.end();i++)
	{
		AnimationClip* clip = *i;
		if (clip && clip->GetName() == name)
			return clip;
	}
	return NULL;
}

void Animation::SetClip (PPtr<AnimationClip> anim)
{
	m_Animation = anim;

	SetDirty ();
}

void Animation::SetClips (const Animations& anims)
{
	m_Animations = anims;
	
	CheckIsCullingBasedOnBoundsDeprecated();

	SetDirty ();
}

bool Animation::IsPlayingLayer (int layer)
{
	if (m_AnimationStates.empty ())
		return false;
	
	for (iterator i=begin();i != end();i++)
	{
		AnimationState& state = **i;
		if (state.GetLayer() == layer && state.GetEnabled())
			return true;
	}
	
	return false;
}


bool Animation::IsPlaying ()
{
	if (m_AnimationStates.empty ())
		return false;
	
	for (iterator i=begin();i != end();i++)
	{
		AnimationState& state = **i;
		if (state.GetEnabled())
			return true;
	}
	
	return false;
}

bool Animation::IsPlaying (const string& clip)
{
	AnimationState* state = GetState(clip);
	
	if ( state && state->GetEnabled() )
		return true;
	else // check for clones
	{
		for ( int s = 0; s < m_AnimationStates.size(); s++ )
		{
			AnimationState* st = m_AnimationStates[s];
			
			if (   st->IsClone()
				&& st->GetParentName() == clip
				&& st->GetEnabled() )
				return true;
		}
	}
	
	return false;
}



void Animation::Stop ()
{
	for (iterator i=begin();i != end();i++)
	{
		(**i).Stop();
	}
	
	// Remove all queued animations.
	m_Queued.clear();
}

void Animation::Stop( const string& name )
{
	for ( int s = 0; s < m_AnimationStates.size(); s++ )
	{
		AnimationState* state = m_AnimationStates[s];
		
		bool stopState = false;
		if ( state->IsClone() && state->GetParentName() == name )
			stopState = true;
		else if ( !state->IsClone() && state->GetName() == name )
			stopState = true;
		
		if ( stopState )
			Stop( *state );
	}
	
	// Remove any queued animations that were cloned from the named state.
	QueuedAnimations::iterator q, qnext;
	for (q=m_Queued.begin();q != m_Queued.end();q=qnext)
	{
		qnext = q;

		if ( q->state->GetParentName() != name )
			qnext++;			
		else
			m_Queued.erase( q );
	}
	
}

void Animation::Stop (AnimationState& state)
{
	state.Stop();
}

void Animation::Rewind ()
{
	for (iterator i=begin();i != end();i++)
	{
		Rewind(**i);
	}
}

void Animation::Rewind (const string& name)
{
	if (!m_AnimationStates.empty())
	{
		AnimationState* state = GetState(name);
		if (state)
			Rewind(*state);
	}
}

void Animation::Rewind (AnimationState& state)
{
	state.SetTime(0.0F);
}

void Animation::SetWrapMode (int mode)
{
	m_WrapMode = mode;
	for (iterator i=begin ();i != end ();i++)
	{
		AnimationState& state = **i;
		state.SetWrapMode(mode);
	}
	SetDirty();
}

#if UNITY_EDITOR
/// Deprecated with 1.5
void Animation::LoadOldAnimations ()
{
	// Load old animations into new animation array
	for (OldAnimations::iterator i=m_OldAnimations.begin ();i != m_OldAnimations.end ();i++)
	{
		AnimationClip* clip = i->second;
		if (clip)
		{
			AddClip (*clip, i->first, INT_MIN, INT_MAX, false);
		}
	}
	m_OldAnimations.clear();
	
	
	// Make sure we have the default animation in the animations list
	AnimationClip* defaultAnim = m_Animation;
	if (defaultAnim)
	{
		for (Animations::iterator i=m_Animations.begin();i!=m_Animations.end();i++)
		{
			if (*i == m_Animation)
				return;
		}

		AddClip (*defaultAnim, defaultAnim->GetName(), INT_MIN, INT_MAX, false);
	}
}
#endif

IMPLEMENT_CLASS_HAS_INIT (Animation)
IMPLEMENT_OBJECT_SERIALIZE (Animation)
///////////////////***************************

void Animation::AddToManager ()
{
	// This method is named AddToManager, but it is used to remove from manager too...
	m_AnimationManagerNode.RemoveFromList();
	if (IsWorldPlaying())
	{
		if (GetEnabled() && (m_Visible || m_CullingType == kCulling_AlwaysAnimate) && IsActive() && !m_AnimationStates.empty())
		{
			if (!m_AnimatePhysics)
				GetAnimationManager().AddDynamic(m_AnimationManagerNode);
			else
				GetAnimationManager().AddFixed(m_AnimationManagerNode);
		}
	}
	else
	{
		// Insert all in edit mode - for sampling animation with the timeline
		if (IsActive())
			GetAnimationManager().AddDynamic(m_AnimationManagerNode);
	}
}

void Animation::RemoveFromManager ()
{
	m_AnimationManagerNode.RemoveFromList();
}

void Animation::SetAnimatePhysics (bool anim)
{
	m_AnimatePhysics = anim;
	SetDirty();
	if (m_AnimationManagerNode.IsInList())
	{
		m_AnimationManagerNode.RemoveFromList();
		if (!m_AnimatePhysics)
			GetAnimationManager().AddDynamic(m_AnimationManagerNode);
		else
			GetAnimationManager().AddFixed(m_AnimationManagerNode);
	}
}

#define ScriptErrorStringObject ErrorStringObject

const char* kAnimationNotFoundError =
"The animation state %s could not be played because it couldn't be found!\n"
"Please attach an animation clip with the name '%s' or call this function only for existing animations.";
#define CANT_PLAY_ERROR { ScriptErrorStringObject(Format(kAnimationNotFoundError, name.c_str(), name.c_str()), this); }


const char* kWrongStateError =
"The animation state %s could not be played because it is not attached to the animation component!\n"
"You have to provide an animation state that is attached to the same animation component.";
#define WRONG_STATE_ERROR(x) { ScriptErrorStringObject(Format(kWrongStateError, x.GetName().c_str()), this); }


bool Animation::Play(const std::string& name, int playMode)
{
	// Deprecated support for play with queueing
	if (playMode == kPlayQueuedDeprecated)
	{
		QueueCrossFade(name, 0.0F, CompleteOthers, kStopSameLayer);
		return true;
	}

	CrossFade(name, 0.0F, playMode);
	return true;
}

void Animation::Play(AnimationState& fadeIn, int playMode)
{
	// Deprecated support for play with queueing
	if (playMode == kPlayQueuedDeprecated)
	{
		QueueCrossFade(fadeIn, 0.0F, CompleteOthers, kStopSameLayer);
		return;
	}
		
	CrossFade(fadeIn, 0.0F, playMode, true);
	return;
}


bool Animation::Play (int mode)
{
	AnimationClip* clip = m_Animation;
	if (clip)
	{
		AnimationState* state = GetState(clip);
		if (state)
		{
			Play(*state, mode);
			return true;
		}
		else
		{
			LogStringObject("Default clip could not be found in attached animations list.", this);
			return false;
		}
	}
	else
		return false;
}

void Animation::Blend(const std::string& name, float targetWeight, float time) {
	AnimationState* state = GetState(name);
	if (state)
		Blend(*state, targetWeight, time);
	else
		CANT_PLAY_ERROR
}

void Animation::CrossFade(const std::string& name, float time, int mode) {
	AnimationState* state = GetState(name);
	if (state)
		CrossFade(*state, time, mode, true);
	else
		CANT_PLAY_ERROR
}

AnimationState* Animation::QueueCrossFade(const std::string& name, float time, int queue, int mode) {
	AnimationState* state = GetState(name);
	if (state)
		return QueueCrossFade(*state, time, queue, mode);
	else
	{
		CANT_PLAY_ERROR
		return NULL;
	}
}

/// - Should Blend and CrossFade Rewind animations before fading in?
/// - Should CrossFade Rewind animations after they are faded out?

void Animation::Blend(AnimationState& playState, float targetWeight, float time)
{
	bool found = false;
	for (iterator i=begin();i!=end();i++)
	{
		AnimationState& state = **i;
		
		if (&playState == &state)
		{
			state.SetEnabled(true);
			state.SetWeightTarget(targetWeight, time, false);
			state.SetupFadeout(time);
			found = true;
		}
	}
	if (!found)
		WRONG_STATE_ERROR(playState)
}

void Animation::CrossFade(AnimationState& playState, float time, int mode, bool clearQueuedAnimations )
{
	bool found = false;
	for (iterator i=begin();i!=end();i++)
	{
		AnimationState& state = **i;
		
		// Don't touch animations in other layers!
		if ((mode & kStopAll) == 0 && state.GetLayer() != playState.GetLayer())
			continue;
			
		if (&playState == &state)
		{
			state.SetEnabled(true);
			if (time > kReallySmallFadeTime)
				state.SetWeightTarget(1.0F, time, false);
			else
			{
				state.SetWeightTargetImmediate(1.0F, false);
			}
			
			state.SetupFadeout(time);
			found = true;
		}
		else
		{
			if (time > kReallySmallFadeTime)
				state.SetWeightTarget(0.0F, time, true);
			else
			{
				state.Stop();
				state.SetWeight(0.0F);
			}
		}
	}
	
	if ( clearQueuedAnimations )
	{
		// Clear out queued animations on the same channel as the state
		// we are cross fading to or all queued animations if we are
		// stopping all.

		// Fixed bug: https://fogbugz.unity3d.com/default.asp?470484, on metro std vector are somehow implemented differently
		// If you remove an element from the list, you have to reinitialize all the iterators, to fix the problem, I removed iterators at all
		for (int i = 0; i < m_Queued.size();)
		{
			if ( !(mode & kStopAll) && m_Queued[i].state->GetLayer() != playState.GetLayer() )
			{		
				i++;
				continue;
			}
			
			m_Queued[i].state->Stop();
			m_Queued[i].state->ForceAutoCleanup();
			m_Queued.erase(m_Queued.begin() + i);
		}
	}

	if ( !found )
		WRONG_STATE_ERROR(playState)
}

AnimationState* Animation::QueueCrossFade(AnimationState& originalState, float time, int queueMode, int mode)
{
	AnimationState* cloned = CloneAnimation(&originalState);
	if (!cloned)
	{
		WRONG_STATE_ERROR(originalState)
		return NULL;
	}

	AnimationState& playState = *cloned;
	playState.SetAutoCleanup();
	
	// Queue the animation for real!
	if (queueMode == CompleteOthers)
	{
		QueuedAnimation queue;
		queue.mode = mode;
		queue.queue = queueMode;
		queue.fadeTime = time;
		queue.state = &playState;
		m_Queued.push_back(queue);
		return &playState;
	}
	else
	{
		CrossFade(playState, time, mode, true);
		return &playState;
	}
}

/// * All animation states have an array with pointers to the curves used when sampling. (AnimationState.m_Curves)
///   The index in the AnimationState.m_Curves is the same as the BoundCurves index.
///   If a curve is not available or excluded because of mixing the pointer is NULL.

void InsertAnimationClipCurveIDs(AnimationBinder::CurveIDLookup& curveIDLookup, AnimationClip& clip)
{
	AnimationClip::QuaternionCurves& rot = clip.GetRotationCurves();
	AnimationClip::Vector3Curves& pos = clip.GetPositionCurves();
	AnimationClip::Vector3Curves& scale = clip.GetScaleCurves();
	AnimationClip::FloatCurves& floats = clip.GetFloatCurves();
	CurveID curveID;
	
	for (AnimationClip::QuaternionCurves::iterator i=rot.begin();i != rot.end();i++)
	{
		AnimationClip::QuaternionCurve& element = *i;

		curveID = CurveID (element.path.c_str(), ClassID(Transform), NULL, "m_LocalRotation", element.hash);
		if (element.hash == 0)
		{
			curveID.CalculateHash();
			element.hash = curveID.hash;
		}
		
		AnimationBinder::InsertCurveIDIntoLookup(curveIDLookup, curveID);
	}

	for (AnimationClip::Vector3Curves::iterator i=pos.begin();i != pos.end();i++)
	{
		AnimationClip::Vector3Curve& element = *i;

		curveID = CurveID (element.path.c_str(), ClassID(Transform), NULL, "m_LocalPosition", element.hash);
		if (element.hash == 0)
		{
			curveID.CalculateHash();
			element.hash = curveID.hash;
		}
		AnimationBinder::InsertCurveIDIntoLookup(curveIDLookup, curveID);
	}

	for (AnimationClip::Vector3Curves::iterator i=scale.begin();i != scale.end();i++)
	{
		AnimationClip::Vector3Curve& element = *i;

		curveID = CurveID (element.path.c_str(), ClassID(Transform), NULL, "m_LocalScale", element.hash);
		if (element.hash == 0)
		{
			curveID.CalculateHash();
			element.hash = curveID.hash;
	}

		
		AnimationBinder::InsertCurveIDIntoLookup(curveIDLookup, curveID);
	}

	for (AnimationClip::FloatCurves::iterator i=floats.begin();i != floats.end();i++)
	{
		AnimationClip::FloatCurve& element = *i;
		curveID = CurveID (element.path.c_str(), element.classID, element.script, element.attribute.c_str(), element.hash);
		if (element.hash == 0)
		{
			curveID.CalculateHash();
			element.hash = curveID.hash;
		}
		
		AnimationBinder::InsertCurveIDIntoLookup(curveIDLookup, curveID);
	}
}

static bool IsMixedIn (const Animation::BoundCurves& boundCurves, int index, AnimationState& state)
{
	// We only mix transform animation
	if (boundCurves[index].targetType == kBindTransformRotation || boundCurves[index].targetType == kBindTransformPosition || boundCurves[index].targetType == kBindTransformScale)
	{
		DebugAssertIf(dynamic_pptr_cast<Transform*> (boundCurves[index].targetObject) == NULL);
		Transform* transform = static_cast<Transform*> (boundCurves[index].targetObject);
		
		return state.ShouldMixTransform(*transform);
	}
	return true;
}


static void AssignBoundCurve (const AnimationBinder::CurveIDLookup& curveIDLookup, const CurveID& curveID, AnimationCurveBase* curve, const Animation::BoundCurves& boundCurves, AnimationState& state)
{
	AnimationBinder::CurveIDLookup::const_iterator found;
	found = curveIDLookup.find(curveID);
	if (found == curveIDLookup.end())
		return;

	if (!IsMixedIn (boundCurves, found->second, state))
		return;
	
	AnimationState::Curves curves = state.GetCurves();
	curves[found->second] = curve;
}

static void CalculateAnimationClipCurves (const AnimationBinder::CurveIDLookup& curveIDLookup, AnimationClip& clip, const Animation::BoundCurves& boundCurves, AnimationState& state)
{
	AnimationClip::QuaternionCurves& rot = clip.GetRotationCurves();
	AnimationClip::Vector3Curves& pos = clip.GetPositionCurves();
	AnimationClip::Vector3Curves& scale = clip.GetScaleCurves();
	AnimationClip::FloatCurves& floats = clip.GetFloatCurves();

	CurveID curveID;
	
	for (AnimationClip::QuaternionCurves::iterator i=rot.begin();i != rot.end();i++)
	{
		if (!i->curve.IsValid ())
			continue;
		AssertIf(i->hash == 0);
		curveID = CurveID (i->path.c_str(), ClassID(Transform), NULL, "m_LocalRotation", i->hash);
		AssignBoundCurve(curveIDLookup, curveID, (AnimationCurveBase*)&i->curve, boundCurves, state);
	}

	for (AnimationClip::Vector3Curves::iterator i=pos.begin();i != pos.end();i++)
	{
		if (!i->curve.IsValid ())
			continue;
		
		AssertIf(i->hash == 0);
		curveID = CurveID (i->path.c_str(), ClassID(Transform), NULL, "m_LocalPosition", i->hash);
		AssignBoundCurve(curveIDLookup, curveID, (AnimationCurveBase*)&i->curve, boundCurves, state);
	}

	for (AnimationClip::Vector3Curves::iterator i=scale.begin();i != scale.end();i++)
	{
		if (!i->curve.IsValid ())
			continue;
		
		AssertIf(i->hash == 0);
		curveID = CurveID (i->path.c_str(), ClassID(Transform), NULL, "m_LocalScale", i->hash);
		AssignBoundCurve(curveIDLookup, curveID, (AnimationCurveBase*)&i->curve, boundCurves, state);
	}

	for (AnimationClip::FloatCurves::iterator i=floats.begin();i != floats.end();i++)
	{
		if (!i->curve.IsValid ())
			continue;
		
		AssertIf(i->hash == 0);
		curveID = CurveID (i->path.c_str(), i->classID, i->script, i->attribute.c_str(), i->hash);
		AssignBoundCurve(curveIDLookup, curveID, (AnimationCurveBase*)&i->curve, boundCurves, state);
	}
}

void Animation::ValidateBoundCurves ()
{
	PROFILER_AUTO(gValidate, this);
	BoundCurves::const_iterator end = m_BoundCurves.end();
	for (BoundCurves::const_iterator i=m_BoundCurves.begin(); i != end; ++i)
	{
		const BoundCurveDeprecated& bound = *i;
		
#if 0
		// We are accessing potentially deleted memory and validating it if the instanceID matches.
		// This is unsafe, but will work in almost all cases. ValidateBoundCurves does not trigger in normal circumstances.
		// Yet we have to pay a large cost for the safety provided... This seems not fair.
		if (bound.targetObject->GetInstanceID () != bound.targetInstanceID)
#else		
		if (Object::IDToPointer(bound.targetInstanceID) != bound.targetObject)
#endif			
		{
			PROFILER_AUTO(gDidDestroyObjectNotification, this);
			CleanupBoundCurves ();
			return;
		}
	}
}

void Animation::CleanupBoundCurves ()
{
	if (m_BoundCurves.empty ())
	{
		return;
	}
	
	m_BoundCurves.clear();
	m_DirtyMask |= kRebindDirtyMask;
}


void Animation::RebuildStateForEverything ()
{
	PROFILER_AUTO(gBuildAnimationState, this)
	
	//@TODO: we should make sure that the curves are aligned for better cache utilization
	// -> Simply modify curveIDLookup::iterator->second to be in the right cache order, after building the curveIDLookup!
	//    Possibly caching is already pretty good if the first curve contains all curves!

	AnimationBinder::CurveIDLookup curveIDLookup;
	AnimationBinder::InitCurveIDLookup(curveIDLookup);
	
	/// Build curveIDLookup
	/// * walk through all animation states and insert the curve into curveIDLookup
	/// * curveCount gets increased when we encounter a new curve
	CurveID curveID ("", 0, NULL, "", 0);
	AnimationState* state;
	AnimationBinder::CurveIDLookup::iterator found;
	
	string attribute;
	
	Transform* transform = QueryComponent (Transform);
		
	if (transform)
	{
		for (int s=0;s<m_AnimationStates.size();s++)
		{
			state = m_AnimationStates[s];
			AnimationClip* clip = state->m_Clip;
			if (clip == NULL)
				continue;
			
			InsertAnimationClipCurveIDs(curveIDLookup, *clip);
		}

		// Bind the global curves.
		GetAnimationBinder().BindCurves(curveIDLookup, *transform, m_BoundCurves, m_CachedAffectedSendToRootTransform, m_CachedTransformMessageMask);

		// This will compact any curves that couldn't be bound
		// - Faster at runtime (Can remove some ifs)
		// - Slower at load time
		AnimationBinder::RemoveUnboundCurves(curveIDLookup, m_BoundCurves);
		
		
		/// * Build state.m_Curves arrays
		///   - We go through all curves in the every state/clip look it up in the curve map
		for (int s=0;s<m_AnimationStates.size();s++)
		{
			state = m_AnimationStates[s];
			// Initialize all states to have no curves assigned
			// Those that aren't used or excluded for mixing will remain null after this loop
			state->CleanupCurves();
			state->AllocateCurves(curveIDLookup.size());
			
			AnimationClip* clip = state->m_Clip;
			if (clip == NULL)
				continue;
			
			CalculateAnimationClipCurves(curveIDLookup, *clip, m_BoundCurves, *state);
		}
	}
	
	// Force Bound curves mask recalculation
	m_ActiveAnimationStatesSize = 0;
	
	m_DirtyMask &= ~kRebindDirtyMask;
}
	
inline float InverseWeight(float weight)
{
	return weight > kReallySmallWeight ? (1.0F / weight) : 0.0F;
}

/***  This calculates the blend weights for one curve.

Weights are distributed so that the top layer gets everything.
If it doesn't use the full weight then the next layer gets to distribute the remaining
weights and so on. Once all weights are used by the top layers,
no weights will be available for lower layers anymore
We use fair weighting, which means if a lower layer wants 80% and 50% have already been used up, the layer will NOT use up all weights.
instead it will take up 80% of the 50%.

Example:
a upper body which is affected by wave, walk and idle
a lower body which is affected by only walk and idle

weight  name  layer lower    upper
 20%    wave   2     0%     20% 
 50%    walk   1     50%    40% 
 100%   idle   0     50%    40% 
 
- Blend weights can change per animated value because of mixing.
  Even without mixing, sometimes a curve is just not defined. Still you want the blend weights to add up to 1.
  Most of the time weights are similar between curves. So there is a lot of caching one can do.
*/

// We use fair weighting, which means if a lower layer wants 80% and 50% have already been used up, the layer will NOT use up all weights.
// Instead it will take up 80% of the 50%.
#define FAIR_DISTRIBUTION 1

template<bool optimize32States>
void CalculateWeights( AnimationState** states, int stateCount, int curveIndex, OUTPUT_OPTIONAL float* outWeights, int mask )
{
	Assert( outWeights != NULL );

	// state index -> layer index
	int*   layerIndices;
	ALLOC_TEMP(layerIndices, int, stateCount);
	// summed weights for each layer - we'll never have more layers than affectors
	float* layerSummedWeights;
	ALLOC_TEMP(layerSummedWeights, float, stateCount);

	const AnimationState* state = states[0];
	int prevLayer = state->GetLayer();
	int layerIndex = 0;

	// Clear summed layer weights
	for( int i = 0; i < stateCount; ++i )
		layerSummedWeights[i] = 0.0F;

	// sum weights for each layer
	UInt32 stateBit = 1;
	for( int i = 0; i < stateCount; ++i )
	{
		if (optimize32States)
		{
			if (mask & stateBit)
			{
				state = states[i];
				
				AssertIf( prevLayer < state->GetLayer()); // Algorithm requires sorted states by layer
				
				if( prevLayer != state->GetLayer() )
					++layerIndex;
				
				layerSummedWeights[layerIndex] += state->GetWeight();
				layerIndices[i] = layerIndex;
				outWeights[i] = state->GetWeight();
			}
			else
			{
				outWeights[i] = 0.0F;
				layerIndices[i] = 0;
			}
		}
		else
		{
			state = states[i];
			if (state->ShouldUse() && state->GetCurves()[curveIndex] != NULL && state->GetBlendMode() == AnimationState::kBlend)
			{
				AssertIf( prevLayer < state->GetLayer()); // Algorithm requires sorted states by layer
				
				if( prevLayer != state->GetLayer() )
					++layerIndex;
				
				layerSummedWeights[layerIndex] += state->GetWeight();
				layerIndices[i] = layerIndex;
				outWeights[i] = state->GetWeight();
			}
			else
			{
				outWeights[i] = 0.0F;
				layerIndices[i] = 0;
			}
		}
		stateBit <<= 1;
		prevLayer = states[i]->GetLayer();
	}
	int layerCount = layerIndex + 1;

	// Distribute weights so that the top layers get everything up to 1.
	// If they use less, the remainder goes to the lower layers.
	float* layerInvSummedWeights;
	ALLOC_TEMP(layerInvSummedWeights, float, stateCount);
	float remainderWeight = 1.0F;
	for( int i = 0; i < layerCount; ++i )
	{
		float layerWeight = max(1.0F, layerSummedWeights[i]);

		layerInvSummedWeights[i] = InverseWeight(layerWeight) * remainderWeight;

		#if FAIR_DISTRIBUTION
		remainderWeight -= layerSummedWeights[i] * remainderWeight;
		#else
		remainderWeight -= layerSummedWeights[i];
		#endif
		
		remainderWeight = max(0.0F, remainderWeight);
	}

	// - Apply the layer inverse weights
	// - Normalize the weights once again, just in case

	// @TODO: Renormalization is only necessary if the remainderWeight is larger than zero as far as i can see
	float summedWeight = 0.0F;
	for( int i = 0; i < stateCount; ++i )
	{
		outWeights[i] *= layerInvSummedWeights[layerIndices[i]];
		summedWeight += outWeights[i];
	}

	summedWeight = InverseWeight(summedWeight);
	for( int i = 0; i < stateCount; ++i )
		outWeights[i] *= summedWeight;
}

void Animation::SampleDefaultClip (double time)
{
	AnimationClip* clip = m_Animation;
	if (!clip)
		return;
	
	SampleAnimation(GetGameObject(), *clip, time, CombineWrapMode(clip->GetWrapMode(), m_WrapMode));
}

// Calculates a bitmask for every bound curve, containing which curve state affects it.
// - The recalculation is only necessary if a state got enabled or disabled (Either through manually disabling it or through a too low blend weight)
bool Animation::RebuildBoundStateMask()
{
	int activeStateCount = 0;
	bool requireRebuild = false;
	int s;
	for (s=0;s<m_AnimationStates.size() && activeStateCount < 32;s++)
	{
		AnimationState& state = *m_AnimationStates[s];
		if (state.ShouldUse() && state.GetBlendMode() == AnimationState::kBlend)
		{
			requireRebuild |= m_ActiveAnimationStates[activeStateCount] != &state;
			m_ActiveAnimationStates[activeStateCount] = &state;
			DebugAssertIf(activeStateCount != 0 && m_ActiveAnimationStates[activeStateCount-1]->GetLayer() < m_ActiveAnimationStates[activeStateCount]->GetLayer());
			activeStateCount++;
		}
	}
	
	// Too many active animation states
	if (s != m_AnimationStates.size())
		return false;
	
	requireRebuild |= activeStateCount != m_ActiveAnimationStatesSize;
	// early out if nothing in which animations are currently playing has changed.
	if (!requireRebuild)
		return true;
	
	m_ActiveAnimationStatesSize = activeStateCount;

	for (int i=0;i<m_BoundCurves.size();i++)
	{
		m_BoundCurves[i].affectedStateMask = 0;
		for (int s=0;s<m_ActiveAnimationStatesSize;s++)
		{
			AnimationState& state = *m_ActiveAnimationStates[s];
			if (state.m_Curves[i])
				m_BoundCurves[i].affectedStateMask |= 1 << s;
		}
	}
	
	return true;
}

inline void AwakeAndDirty (Object* o)
{
	if (o)
	{
		o->AwakeFromLoad(kDefaultAwakeFromLoad);
		o->SetDirty();
	}
}

static void UpdateLastNonTransformObject(Object*& lastNonTransformObject, Object* const targetObject)
{
	if (lastNonTransformObject != targetObject)
	{
		AwakeAndDirty(lastNonTransformObject);
		lastNonTransformObject = targetObject;
	}
}

// Optimized for 32 animation states
void Animation::BlendOptimized()
{
	AssertIf(m_BoundCurves.empty());
	
	///@TODO: Keep a cache for all curves and pass it into animationcurve.Evaluate.
	// So that many characters dont kill the cache of shared animation curves.
	
	int curveCount = m_BoundCurves.size();

	AnimationState** activeStates = m_ActiveAnimationStates;
	int stateSize = m_ActiveAnimationStatesSize;
	float* weights;
	ALLOC_TEMP(weights, float, stateSize);
	
	float weight;
	const AnimationState* state;
	
	Object* lastNonTransformObject = NULL;
	UInt32 lastAffectedStateMask = m_BoundCurves[0].affectedStateMask;
	CalculateWeights<true>(activeStates, stateSize, 0, weights, lastAffectedStateMask);
	BoundCurveDeprecated* boundCurves = &m_BoundCurves[0];
	
	for( int c = 0; c < curveCount; ++c )
	{
		BoundCurveDeprecated& bind = boundCurves[c];
	
		// Only recalculate weights if the state mask changes!
		// This happens very rarely. (If you dont use mixing it never happens)
		if (lastAffectedStateMask != bind.affectedStateMask)
		{
			lastAffectedStateMask = bind.affectedStateMask;
			CalculateWeights<true>(activeStates, stateSize, c, weights, lastAffectedStateMask);
		}
		
		if (lastAffectedStateMask == 0)
			continue;
		
		const UInt32 targetType = bind.targetType;
		
		// Sample quaternion
		if (targetType == kBindTransformRotation)
		{
			Prefetch(bind.targetPtr);
			Quaternionf& result = *(Quaternionf*)bind.targetPtr;
			result.Set(0,0,0,0);
			UInt32 stateBit = 1;
			for (int i=0;i<stateSize;i++)
			{
				if (lastAffectedStateMask & stateBit)
				{
					state = activeStates[i];
					const AnimationCurveQuat* quatCurve = reinterpret_cast<AnimationCurveQuat*>(state->GetCurves()[c]);
	
					Quaternionf sample = quatCurve->EvaluateClamp(state->m_WrappedTime);
					DebugAssertIf(!IsFinite(sample));

					/// Not necessary because we make sure when sampling that our curves are sampled and almost normalized
					/// @todo: If people animate inside unity this might be a problem.
					/// Hopefully no one does that in combination with blending.
					// sample = NormalizeFastEpsilonZero(sample);
					result += Sign(Dot (sample, result)) * sample * weights[i];
				}
				stateBit <<= 1;
			}
			
			result = NormalizeSafe(result);
			DebugAssertIf(!IsFinite(result));
		}
		// Sample vector 3
		else if (targetType == kBindTransformPosition)
		{
			Vector3f result = Vector3f(0.0F, 0.0F, 0.0F);
			UInt32 stateBit = 1;
			for (int i=0;i<stateSize;i++)
			{
				if (lastAffectedStateMask & stateBit)
				{
					state = activeStates[i];
					const AnimationCurveVec3* vec3Curve = reinterpret_cast<AnimationCurveVec3*>(state->GetCurves()[c]);
		
					Vector3f sample = vec3Curve->EvaluateClamp(state->m_WrappedTime);
					weight = weights[i];
					result += sample * weight;
				}
				stateBit <<= 1;
			}
			
			DebugAssertIf(!IsFinite(result));
			*reinterpret_cast<Vector3f*>(bind.targetPtr) = result;
		}
		else if (targetType == kBindTransformScale)
		{
			Vector3f result = Vector3f(0.0F, 0.0F, 0.0F);
			UInt32 stateBit = 1;
			for (int i=0;i<stateSize;i++)
			{
				if (lastAffectedStateMask & stateBit)
				{
					state = activeStates[i];
					const AnimationCurveVec3* vec3Curve = reinterpret_cast<AnimationCurveVec3*>(state->GetCurves()[c]);
		
					Vector3f sample = vec3Curve->EvaluateClamp(state->m_WrappedTime);
					weight = weights[i];
					result += sample * weight;
				}
				stateBit <<= 1;
			}
			
			DebugAssertIf(!IsFinite(result));
			*reinterpret_cast<Vector3f*>(bind.targetPtr) = result;
			Transform* targetTransform = reinterpret_cast<Transform*> (bind.targetObject);
			targetTransform->RecalculateTransformType ();
		}
		// Sample float
		else if (targetType > kUnbound)
		{
			float result = 0.0F;
			UInt32 stateBit = 1;
			for (int i=0;i<stateSize;i++)
			{
				if (lastAffectedStateMask & stateBit)
				{
					state = activeStates[i];
					const AnimationCurve* floatCurve = reinterpret_cast<AnimationCurve*>(state->GetCurves()[c]);
	
					float sample = floatCurve->EvaluateClamp(state->m_WrappedTime);
					weight = weights[i];
					result += sample * weight;
				}
				stateBit <<= 1;
			}

			DebugAssertIf(!IsFinite(result));
			
			AnimationBinder::SetFloatValue(bind, result);
			
			if (AnimationBinder::ShouldAwakeGeneric (bind))
				UpdateLastNonTransformObject(lastNonTransformObject, bind.targetObject);

		}
		else
		{
			#if COMPACT_UNBOUND_CURVES
			AssertString("Unbound curves should be compacted!");
			#endif
			continue;
		}

	}
	
	AwakeAndDirty(lastNonTransformObject);
}


// Unlimited amount of animation states
void Animation::BlendGeneric()
{
	AssertIf(m_BoundCurves.empty());
	
	///@TODO: Keep a cache for all curves and pass it into animationcurve.Evaluate.
	// So that many characters dont kill the cache of shared animation curves.
	
	int curveCount = m_BoundCurves.size();

	int stateSize = m_AnimationStates.size();
	float* weights;
	ALLOC_TEMP(weights, float, stateSize);
		
	float weight;
	const AnimationState* state;
	
	Object* lastNonTransformObject = NULL;
	for( int c = 0; c < curveCount; ++c )
	{
		BoundCurveDeprecated& bind = m_BoundCurves[c];
	
		CalculateWeights<false>(&m_AnimationStates[0], stateSize, c, weights, 0);
		
		bool didSample = false;
		
		int targetType = bind.targetType;
		
		// Sample quaternion
		if (targetType == kBindTransformRotation)
		{
			Quaternionf result = Quaternionf(0.0F, 0.0F, 0.0F, 0.0F);
			
			for (int i=0;i<stateSize;i++)
			{
				state = m_AnimationStates[i];
				const AnimationCurveQuat* quatCurve = reinterpret_cast<AnimationCurveQuat*>(state->GetCurves()[c]);
				if (quatCurve && weights[i] > kReallySmallWeight)
				{
					Quaternionf sample = quatCurve->EvaluateClamp(state->m_WrappedTime);

					/// Not necessary because we make sure when sampling that our curves are sampled and almost normalized
					/// @todo: If people animate inside unity this might be a problem.
					/// Hopefully no one does that in combination with blending.

					// sample = NormalizeFastEpsilonZero(sample);
					weight = weights[i];
					if (Dot (sample, result) < 0.0F)
						weight = -weight;
					result += sample * weight;
					didSample = true;
				}
			}
			
			result = NormalizeSafe(result);
			DebugAssertIf(!IsFinite(result));
			if (didSample)
				*reinterpret_cast<Quaternionf*>(bind.targetPtr) = result;
		}
		// Sample vector 3
		else if (targetType == kBindTransformPosition)
		{
			Vector3f result = Vector3f(0.0F, 0.0F, 0.0F);
			for (int i=0;i<stateSize;i++)
			{
				state = m_AnimationStates[i];
				const AnimationCurveVec3* vec3Curve = reinterpret_cast<AnimationCurveVec3*>(state->GetCurves()[c]);
				if (vec3Curve && weights[i] > kReallySmallWeight)
				{
					Vector3f sample = vec3Curve->EvaluateClamp(state->m_WrappedTime);
					weight = weights[i];
					result += sample * weight;
					didSample = true;
				}
			}
			
			DebugAssertIf(!IsFinite(result));
			if (didSample)
			{
				*reinterpret_cast<Vector3f*>(bind.targetPtr) = result;
			}
		}
		// Sample vector 3
		else if (targetType == kBindTransformScale)
		{
			Vector3f result = Vector3f(0.0F, 0.0F, 0.0F);
			for (int i=0;i<stateSize;i++)
			{
				state = m_AnimationStates[i];
				const AnimationCurveVec3* vec3Curve = reinterpret_cast<AnimationCurveVec3*>(state->GetCurves()[c]);
				if (vec3Curve && weights[i] > kReallySmallWeight)
				{
					Vector3f sample = vec3Curve->EvaluateClamp(state->m_WrappedTime);
					weight = weights[i];
					result += sample * weight;
					didSample = true;
				}
			}
			
			DebugAssertIf(!IsFinite(result));

			if (didSample)
			{
				*reinterpret_cast<Vector3f*>(bind.targetPtr) = result;
				Transform* targetTransform = reinterpret_cast<Transform*> (bind.targetObject);
				targetTransform->RecalculateTransformType ();
			}
		}
		// Sample float
		else if (targetType > kUnbound)
		{
			float result = 0.0F;
			for (int i=0;i<stateSize;i++)
			{
				state = m_AnimationStates[i];
				const AnimationCurve* floatCurve = reinterpret_cast<AnimationCurve*>(state->GetCurves()[c]);
				if (floatCurve && weights[i] > kReallySmallWeight)
				{
					float sample = floatCurve->EvaluateClamp(state->m_WrappedTime);
					weight = weights[i];
					result += sample * weight;
					didSample = true;
				}
			}

			DebugAssertIf(!IsFinite(result));

			if (didSample)
			{
				AnimationBinder::SetFloatValue(bind, result);

				if (AnimationBinder::ShouldAwakeGeneric (bind))
					UpdateLastNonTransformObject(lastNonTransformObject, bind.targetObject);
			}
		}	
		else
		{
			#if COMPACT_UNBOUND_CURVES
			AssertString("Unbound curves should be compacted!");
			#endif
			continue;
		}

	}
	
	AwakeAndDirty(lastNonTransformObject);
}

void Animation::BlendAdditive()
{
	AssertIf(m_BoundCurves.empty());
	int stateSize = m_AnimationStates.size();
	AnimationState* state;

	AnimationState** activeStates;
	ALLOC_TEMP(activeStates, AnimationState*, stateSize);
	int activeStateSize = 0;

	///@TODO: We can also compare if the current time matches the first frame, then the animation has no effect!
	for (int i=0;i<stateSize;i++)
	{
		state = m_AnimationStates[i];
		if (state->GetBlendMode() == AnimationState::kAdditive && state->ShouldUse ())
		{
			activeStates[activeStateSize] = state;
			activeStateSize++;
		}
	}
	// early out if we have no additive animations playing
	if (activeStateSize == 0)
		return;
	
	int curveCount = m_BoundCurves.size();
	BoundCurveDeprecated* boundCurves = &m_BoundCurves[0];
//	float* weights;
//	ALLOC_TEMP(weights, float, stateSize, kMemAnimation);
//	float* time;
//	ALLOC_TEMP(time, float, stateSize, kMemAnimation);
		
	float weight;
	
	Object* lastNonTransformObject = NULL;
	for( int c = 0; c < curveCount; ++c )
	{
		BoundCurveDeprecated& bind = boundCurves[c];
		
		bool didSample = false;
		
		int targetType = bind.targetType;
		
		// Sample quaternion
		if (targetType == kBindTransformRotation)
		{
			Quaternionf result = *reinterpret_cast<Quaternionf*>(bind.targetPtr);
			
			for (int i=0;i<activeStateSize;i++)
			{
				state = activeStates[i];
				weight = clamp01 (state->GetWeight());
				const AnimationCurveQuat* quatCurve = reinterpret_cast<AnimationCurveQuat*>(state->GetCurves()[c]);
				if (quatCurve)
				{
					Quaternionf sample = Inverse(quatCurve->GetKey(0).value) * quatCurve->EvaluateClamp(state->m_WrappedTime);
					sample = Lerp(Quaternionf::identity(), sample, weight);
					result *= sample;
					didSample = true;
				}
			}
			
			result = NormalizeSafe(result);
			DebugAssertIf(!IsFinite(result));
			if (didSample)
				*reinterpret_cast<Quaternionf*>(bind.targetPtr) = result;
		}
		// Sample vector 3
		else if (targetType == kBindTransformPosition)
		{
			Vector3f result = *reinterpret_cast<Vector3f*>(bind.targetPtr);

			for (int i=0;i<activeStateSize;i++)
			{
				state = activeStates[i];
				weight = clamp01 (state->GetWeight());
				const AnimationCurveVec3* vec3Curve = reinterpret_cast<AnimationCurveVec3*>(state->GetCurves()[c]);

				if (vec3Curve)
				{
					Vector3f sample = vec3Curve->EvaluateClamp(state->m_WrappedTime) - vec3Curve->GetKey(0).value;
					result += sample * weight;
					didSample = true;
				}
			}
			DebugAssertIf(!IsFinite(result));
			if (didSample)
				*reinterpret_cast<Vector3f*>(bind.targetPtr) = result;
		}
		// Sample vector 3
		else if (targetType == kBindTransformScale)
		{
			Vector3f result = *reinterpret_cast<Vector3f*>(bind.targetPtr);

			for (int i=0;i<activeStateSize;i++)
			{
				state = activeStates[i];
				weight = clamp01 (state->GetWeight());
				const AnimationCurveVec3* vec3Curve = reinterpret_cast<AnimationCurveVec3*>(state->GetCurves()[c]);

				if (vec3Curve)
				{
					Vector3f sample = vec3Curve->EvaluateClamp(state->m_WrappedTime) - vec3Curve->GetKey(0).value;
					result += sample * weight;
					didSample = true;
				}
			}
			DebugAssertIf(!IsFinite(result));
			if (didSample)
			{
				*reinterpret_cast<Vector3f*>(bind.targetPtr) = result;
				Transform* targetTransform = reinterpret_cast<Transform*> (bind.targetObject);
				targetTransform->RecalculateTransformType ();
			}
		}
	}
	
	AwakeAndDirty(lastNonTransformObject);
}

inline Animation::CullingType RemapDeprecatedCullingType (Animation::CullingType type)
{
	if (type == Animation::kDeprecatedCulling_BasedOnClipBounds || type == Animation::kDeprecatedCulling_BasedOnUserBounds)
		return Animation::kCulling_BasedOnRenderers;
	else
		return type;
}

void Animation::SetCullingType(CullingType type)
{
	type = RemapDeprecatedCullingType(type);

	CheckIsCullingBasedOnBoundsDeprecated ();
	
	// Clearing culling related data
	if (m_CullingType == kCulling_BasedOnRenderers)
	{
		ClearContainedRenderers();
	}
	else if (m_CullingType == kCulling_AlwaysAnimate)
		RemoveFromManager();

	m_CullingType = type;

	// Building new culling data
	if (m_CullingType == kCulling_BasedOnRenderers && !m_AnimationStates.empty())
		RecomputeContainedRenderers ();
	else if (m_CullingType == kCulling_AlwaysAnimate && !m_AnimationManagerNode.IsInList())
		AddToManager();

	SetDirty();
}

void Animation::RemoveContainedRenderer (Renderer* renderer)
{
	ContainedRenderers::iterator end = m_ContainedRenderers.end();
	for (ContainedRenderers::iterator i = m_ContainedRenderers.begin();i != end;++i)
	{
		Renderer* cur = *i;
		if (cur == renderer)
		{
			*i = m_ContainedRenderers.back();
			m_ContainedRenderers.resize(m_ContainedRenderers.size() - 1);
			return;
		}
	}
}

static void AnimationVisibilityCallback (void* userData, void* senderUserData, int visibilityEvent)
{
	Animation& animation = *reinterpret_cast<Animation*> (userData);
	
	if (visibilityEvent == kBecameVisibleEvent)
		animation.SetVisibleRenderers(true);
	else if (visibilityEvent == kBecameInvisibleEvent)
		animation.CheckRendererVisibleState ();
	else if (visibilityEvent == kWillDestroyEvent)
	{
		animation.RemoveContainedRenderer(reinterpret_cast<Renderer*> (senderUserData));
		
		animation.CheckRendererVisibleState ();
	}
}

void Animation::ClearContainedRenderers ()
{
	ContainedRenderers::iterator end = m_ContainedRenderers.end();
	for (ContainedRenderers::iterator i = m_ContainedRenderers.begin();i != end;++i)
	{
		Renderer* renderer = *i;
		renderer->RemoveEvent(AnimationVisibilityCallback, this);
	}
	m_ContainedRenderers.clear();
}

void Animation::RecomputeContainedRenderers ()
{
	Assert(m_CullingType == kCulling_BasedOnRenderers);

	ClearContainedRenderers ();
		
	Transform& transform = GetComponent (Transform);
	RecomputeContainedRenderersRecurse(transform);

	Assert(m_CullingType == kCulling_BasedOnRenderers);

	CheckRendererVisibleState ();
}

void Animation::CheckRendererVisibleState ()
{
	Assert(m_CullingType == kCulling_BasedOnRenderers);

	ContainedRenderers::iterator end = m_ContainedRenderers.end();	
	for (ContainedRenderers::iterator i = m_ContainedRenderers.begin();i != end;++i)
	{
		Renderer* renderer = *i;
		Assert(renderer->HasEvent(AnimationVisibilityCallback, this));
		if (renderer->IsVisibleInScene())
		{
			SetVisibleRenderers(true);
			return;
		}
	}

	SetVisibleRenderers(false);
}

void Animation::SetVisibleInternal(bool visible)
{
	Assert(m_CullingType != kCulling_AlwaysAnimate);
	m_Visible = visible;

	if (IsWorldPlaying())
	{
		const bool wasAttached = m_AnimationManagerNode.IsInList();
		// Method is called AddToManager, but it does removal too...
		AddToManager();

		// Culling is after AnimationManager Update,
		// so when we pop into visibility we should make sure we are rendering the right frame!
		if (m_AnimationManagerNode.IsInList() && wasAttached == false)
			UpdateAnimation(GetCurTime());
	}
}


void Animation::SetVisibleRenderers(bool visible)
{
	//LogString(Format("SetVisibleRenderers %d %s", visible ? 1 : 0, GetGameObject().GetName()));

	Assert(m_CullingType == kCulling_BasedOnRenderers);
	SetVisibleInternal(visible);
}

void Animation::SetVisibleBounds(bool visible)
{
	//LogString(Format("SetVisibleBounds %d %s", visible ? 1 : 0, GetGameObject().GetName()));

	CheckIsCullingBasedOnBoundsDeprecated ();
	SetVisibleInternal(visible);
}

///@TODO: We must ensure that there are not two animation components in a hierarchy!!!. Otherwise SetAnimationPtr will break!

void Animation::RecomputeContainedRenderersRecurse (Transform& transform)
{
	Renderer* renderer = transform.QueryComponent(Renderer);
	if (renderer)
	{
		m_ContainedRenderers.push_back(renderer);
		renderer->AddEvent(AnimationVisibilityCallback, this);
	}
	Transform::iterator end = transform.end();
	for (Transform::iterator i = transform.begin();i != end;++i)
	{
		RecomputeContainedRenderersRecurse(**i);
	}
}

void Animation::SendTransformChangedToCachedTransform()
{
	int physicsMask = m_AnimatePhysics ? Transform::kAnimatePhysics : 0;
	int size = m_CachedAffectedSendToRootTransform.size();
	for (int i=0;i<size;i++)
	{
		m_CachedAffectedSendToRootTransform[i]->SendTransformChanged(m_CachedTransformMessageMask | physicsMask);
	}
}

void Animation::SyncLayerTime (int layer)
{
	float normalizedSpeed = 0.0F;
	float normalizedTime = 0.0F;
	float summedLayerWeight = 0.0F;
	
	for (AnimationStates::iterator i=m_AnimationStates.begin();i!=m_AnimationStates.end();i++)
	{
		AnimationState& state = **i;
		if (state.GetLayer() != layer || !state.GetEnabled())
			continue;
		
		float weight = max(state.GetWeight(), 0.0F);
		normalizedSpeed += state.GetNormalizedSpeed() * weight;
		normalizedTime += state.GetNormalizedTime() * weight;
		summedLayerWeight += weight;
	}
	
	if (summedLayerWeight > kReallySmallWeight)
	{
		normalizedSpeed = normalizedSpeed / summedLayerWeight;
		normalizedTime = normalizedTime / summedLayerWeight;
		
		for (AnimationStates::iterator i=m_AnimationStates.begin();i!=m_AnimationStates.end();i++)
		{
			AnimationState& state = **i;
			if (state.GetLayer() != layer || !state.GetEnabled())
				continue;
			
			state.SetNormalizedSyncedSpeed(normalizedSpeed);
			state.SetNormalizedTime(normalizedTime);
		}
	}
}

void Animation::Sample ()
{
	bool needsUpdate = false;

	// Update animation state
	for (int i=0;i<m_AnimationStates.size();i++)
	{
		AnimationState& state = *m_AnimationStates[i];
		
		// Do we actually use any of the animation states?
		if (state.ShouldUse())
			needsUpdate = true;

		m_DirtyMask |= state.GetDirtyMask();
		state.ClearDirtyMask();
	}
	
	if (needsUpdate)
		SampleInternal();
}

void Animation::SampleInternal()
{
	PROFILER_AUTO(gSampleAnimation, this)

	ValidateBoundCurves ();

	if (m_DirtyMask != 0)
	{
		if (m_DirtyMask & kRebindDirtyMask)
			RebuildStateForEverything();
		
		if (m_DirtyMask & kLayersDirtyMask)
			SortAnimationStates();
	}
	
	AssertIf(m_DirtyMask != 0);

	if (!m_BoundCurves.empty())
	{
		if (RebuildBoundStateMask())
		{
			if (m_ActiveAnimationStatesSize != 0)
				BlendOptimized();
		}
		else
		{
			/// More than 32 animation states active at the same time!
			/// @TODO: 3.0 take this out. Possibly sort animation states by maximum weight. 
			/// Or maybe supporting more than 32 states at once is really pointless
			BlendGeneric();
		}
			
		BlendAdditive();
		
		SendTransformChangedToCachedTransform();
	}
}

void Animation::UpdateAnimation (double time)
{
	if (AnimationState::UseUnity32AnimationFixes())
		UpdateAnimationInternal(time);
	else
		UpdateAnimationInternal_Before32(time);
}

// Calculates remaining play-times for all animations and for specified layer
static void GetQueueTimes(const Animation::AnimationStates& states, const int targetLayer, float& allQueueTime, float& layerQueueTime)
{
	allQueueTime = 0;
	layerQueueTime = 0;

	for (Animation::AnimationStates::const_iterator it = states.begin(), end = states.end(); it != end; ++it)
	{
		const AnimationState& state = **it;

		if (state.GetEnabled())
		{
			const int layer = state.GetLayer();

			const int wrapMode = state.GetWrapMode();
			if (wrapMode != kDefaultWrapMode && wrapMode != kClamp)
			{
				// for "infinite" animations (Loop, Repeat, ClampForever) we mark layer as "occupied"
				allQueueTime = std::numeric_limits<float>::infinity();
				if (layer == targetLayer)
					layerQueueTime = std::numeric_limits<float>::infinity();
			}
			else
			{
				const float dt = state.GetLength() - state.GetTime();
				Assert(dt >= 0);

				allQueueTime = std::max(allQueueTime, dt);
				if (layer == targetLayer)
					layerQueueTime = std::max(layerQueueTime, dt);
			}
		}
	}
}

// This function starts Queued animations (if it's already time to start).
// We always blend animations based on QueuedAnimation::fadeTime, i.e. it doesn't
// matter if currently playing animation(s) will finish in shorter time we will blend 
// in the new one in QueuedAnimation::fadeTime. We leave up to a user to specify 
// sufficient blend times.
void Animation::UpdateQueuedAnimations(bool& needsUpdate)
{
	int lastLayer = -1;
	float allQueueTime, lastLayerQueueTime;
	allQueueTime = lastLayerQueueTime = -1;

	// Update queued animations
	for (QueuedAnimations::iterator q = m_Queued.begin(); q != m_Queued.end(); )
	{
		const QueuedAnimation& qa = *q;		
		const float fadeTime = qa.fadeTime;

		const int layer = qa.state->GetLayer();

		bool startNow = false;
		if (qa.mode == kStopAll)
		{
			// queuing after all animations

			if (allQueueTime < 0)
			{
				// allQueueTime must to be recalculated
				GetQueueTimes(m_AnimationStates, layer, allQueueTime, lastLayerQueueTime);
				lastLayer = layer;
			}

			startNow = fadeTime >= allQueueTime;
		}
		else
		{
			// queuing after animations in specific layer

			if (lastLayer != layer || lastLayerQueueTime < 0)
			{
				// lastLayerQueueTime must to be recalculated
				GetQueueTimes(m_AnimationStates, layer, allQueueTime, lastLayerQueueTime);
				lastLayer = layer;
			}

			startNow = fadeTime >= lastLayerQueueTime;
		}

		if (startNow)
		{
			// This crossfade logic is framerate specific. We know when this animation had
			// to be started, so in theory we should advance time and blending value (and execute events), 
			// but we don't do that because it would be an over-complication.
			AnimationState& state = *qa.state;
			
			CrossFade(state, fadeTime, qa.mode, false);
			q = m_Queued.erase(q);
			needsUpdate = true;

			Assert(state.GetEnabled());
			// we need to recalculate queue times, because we just started an animation			
			allQueueTime = lastLayerQueueTime = -1;
		}
		else
			++q;
	}
}

void Animation::UpdateQueuedAnimations_Before34(bool& needsUpdate)
{
	// Update queued animations
	for (QueuedAnimations::iterator q = m_Queued.begin(); q != m_Queued.end(); )
	{
		const QueuedAnimation& qa = *q;
		if ((qa.mode == kStopAll && !IsPlaying()) || (qa.mode != kStopAll && !IsPlayingLayer(qa.state->GetLayer())))
		{
			CrossFade(*qa.state, qa.fadeTime, qa.mode, false);
			q = m_Queued.erase(q);
			needsUpdate = true;
		}
		else
			++q;
	}
}

void Animation::UpdateAnimationInternal(double time)
{
	PROFILER_AUTO(gUpdateAnimation, this)
	
	bool needsUpdate = false;
		
	// Sync animations
	for (SyncedLayers::iterator sync=m_SyncedLayers.begin();sync != m_SyncedLayers.end();sync++)
		SyncLayerTime(*sync);

	int stoppedAnimationCount = 0;
	AnimationState** stoppedAnimations = NULL;
	ALLOC_TEMP(stoppedAnimations, AnimationState*, m_AnimationStates.size());

	// Update animation state
	for (int i=0;i<m_AnimationStates.size();)
	{
		AnimationState& state = *m_AnimationStates[i];
			
		// Update state
		if (state.GetEnabled())
		{
			if (state.UpdateAnimationState(time, *this))
			{
				if (!state.ShouldAutoCleanupNow())
					stoppedAnimations[stoppedAnimationCount++] = &state;				
			}
		}
		
		// Do we actually use any of the animation states?
		if (state.ShouldUse())
			needsUpdate = true;
	
		m_DirtyMask |= state.GetDirtyMask();
		state.ClearDirtyMask();
		
		// Cleanup queued animations that have finished playing
		if (state.ShouldAutoCleanupNow())
		{
			delete &state;
			m_DirtyMask |= kLayersDirtyMask;
			m_AnimationStates.erase(m_AnimationStates.begin() + i);
			m_ActiveAnimationStatesSize = 0;
		}
		else
		{
			i++;
		}
	}

	if (AnimationState::UseUnity34AnimationFixes())
		UpdateQueuedAnimations(needsUpdate);
	else
		UpdateQueuedAnimations_Before34(needsUpdate);

	if (stoppedAnimationCount > 0)
	{
		for (int i = 0; i < stoppedAnimationCount; ++i)
			stoppedAnimations[i]->SetupUnstoppedState();

		needsUpdate = true;
	}

	// Only do blending if it is really necessary
	if (needsUpdate)
	{
		SampleInternal();
	}

	for (int i = 0; i < stoppedAnimationCount; ++i)
		stoppedAnimations[i]->CleanupUnstoppedState();
}

// This is for backwards compatibility. If you need to make changes, 
// then make them in UpdateAnimationInternal which is used with Unity 3.2 and later content
void Animation::UpdateAnimationInternal_Before32(double time)
{
	PROFILER_AUTO(gUpdateAnimation, this)

		bool needsUpdate = false;
	int activeLastStateCount = 0;

	// Sync animations
	for (SyncedLayers::iterator sync=m_SyncedLayers.begin();sync != m_SyncedLayers.end();sync++)
		SyncLayerTime(*sync);

	AnimationState* stoppedAnimation = NULL;

	// Update animation state
	for (int i=0;i<m_AnimationStates.size();)
	{
		AnimationState& state = *m_AnimationStates[i];

		if (state.ShouldUse())
			activeLastStateCount++;

		// Update state
		if (state.GetEnabled())
		{
			if (state.UpdateAnimationState(time, *this))
			{
				if (!state.ShouldAutoCleanupNow())
					stoppedAnimation = &state;
			}
		}

		// Do we actually use any of the animation states?
		if (state.ShouldUse())
			needsUpdate = true;

		m_DirtyMask |= state.GetDirtyMask();
		state.ClearDirtyMask();

		// Cleanup queued animations that have finished playing
		if (state.ShouldAutoCleanupNow())
		{
			delete &state;
			m_DirtyMask |= kLayersDirtyMask;
			m_AnimationStates.erase(m_AnimationStates.begin() + i);
			m_ActiveAnimationStatesSize = 0;
		}
		else
		{
			i++;
		}
	}

	UpdateQueuedAnimations_Before34(needsUpdate);	

	bool revertWrappedTimeToBeforeStop = false;
	if (activeLastStateCount == 1 && needsUpdate == false && stoppedAnimation)
	{
		stoppedAnimation->SetupUnstoppedState();
		revertWrappedTimeToBeforeStop = true;
	}

	// Only do blending if it is really necessary
	if (needsUpdate)
	{
		SampleInternal();
	}

	if (revertWrappedTimeToBeforeStop)
		stoppedAnimation->CleanupUnstoppedState();
}


struct GreaterLayer : std::binary_function<AnimationState*, AnimationState*, std::size_t>
{
	bool operator () (AnimationState* lhs, AnimationState* rhs) const
	{
		if (lhs->GetLayer() != rhs->GetLayer())
			return lhs->GetLayer() > rhs->GetLayer();
		else
			return lhs->GetName() > rhs->GetName();
	}
};

void Animation::SortAnimationStates ()
{
	sort(m_AnimationStates.begin(), m_AnimationStates.end(), GreaterLayer());
	m_DirtyMask &= ~kLayersDirtyMask;
	m_ActiveAnimationStatesSize = 0;
}

void Animation::ReleaseAnimationStates ()
{
	for (AnimationStates::iterator i=m_AnimationStates.begin();i!=m_AnimationStates.end();i++)
	{
		delete *i;
	}
	m_AnimationStates.clear();
}

AnimationClip* Animation::GetClipLegacyWarning (AnimationClip* clip)
{
	if (clip == NULL)
		return NULL;
	else
	{
		if (clip->GetAnimationType () == AnimationClip::kLegacy)
			return clip;
		else	
		{
			WarningStringObject(Format("The AnimationClip '%s' used by the Animation component '%s' must be marked as Legacy.", clip->GetName(), GetName()), clip);
			return NULL;
		}
	}
}


void Animation::BuildAnimationStates()
{
	if (!m_AnimationStates.empty())
		return;
	if (m_Animations.empty ())
		return;
	
	PROFILER_AUTO(gBuildAnimationState, this)
	
	ReleaseAnimationStates();
	
	m_AnimationStates.reserve(m_Animations.size());
	
	double time = GetCurTime();
	for (int i=0;i<m_Animations.size();i++)
	{
		AnimationClip* clip = GetClipLegacyWarning(m_Animations[i]);
		if (clip != NULL)
		{
			m_AnimationStates.push_back(new AnimationState());
			m_AnimationStates.back()->Init(clip->GetName(), clip, time, CombineWrapMode(clip->GetWrapMode(), m_WrapMode));
		}
	}

	if (m_CullingType == kCulling_BasedOnRenderers)
		RecomputeContainedRenderers();
	
	m_DirtyMask |= kRebindDirtyMask;
		
	AddToManager();
}

AnimationState* Animation::CloneAnimation (AnimationState* state)
{
	// The animation state needs to be attached to this animation
	if (GetState(state) == NULL)
		return NULL;

	PROFILER_AUTO(gCloneAnimationState, this)

	// Clone the state and reference all it's bound curves
	AnimationState* clone = new AnimationState();
	clone->Init( state->GetName() + " - Queued Clone", state->GetClip(), GetCurTime(), state->GetWrapMode(), true );
	clone->SetParentName( state->GetName() );
	clone->SetLayer(state->GetLayer());
	clone->SetClonedCurves(*state);
	clone->ClearDirtyMask();
	m_AnimationStates.push_back(clone);
	
	m_DirtyMask |= kLayersDirtyMask;
	return clone;
}

void Animation::AddClip (AnimationClip& clip, const std::string& newName, int firstFrame, int lastFrame, bool loop)
{
	PROFILER_AUTO (gAddClip, this)

	if (GetClipLegacyWarning (&clip) == NULL)
		return;
	
	AnimationClip* newClip = &clip;
	// Do we really need to create a duplicate clip?
	if (loop || firstFrame != INT_MIN || lastFrame != INT_MAX || newName != clip.GetName())
	{
		newClip = NEW_OBJECT (AnimationClip);

		CopySerialized(clip, *newClip);
		newClip->SetName(newName.c_str());
		
		if (loop || firstFrame != INT_MIN || lastFrame != INT_MAX)
		{
			// [case 504486] need to clear curve because function ClipAnimation() will add all these clipped curves from source clip.
			newClip->ClearCurves();
			ClipAnimation(clip, *newClip, FrameToTime(firstFrame, clip.GetSampleRate()), FrameToTime(lastFrame, clip.GetSampleRate()), loop);
		}
	}
	
	
	// Replace clips with duplicate names
	Animations::iterator i;
	for (i=m_Animations.begin();i != m_Animations.end();i++)
	{
		AnimationClip* cur = *i;
		if (cur && cur->GetName() == newName)
			break;
	}
	if (i == m_Animations.end())
		m_Animations.push_back(newClip);
	else
		*i = newClip;

	if (!m_AnimationStates.empty())
	{
		m_DirtyMask |= kRebindDirtyMask;

		// Remove states with duplicate names
		for (AnimationStates::iterator s=begin();s != end();s++)
		{
			if ((**s).GetName() == newName)
			{
				delete *s;
				m_AnimationStates.erase(s);
				break;			
			}
		}

		m_AnimationStates.push_back(new AnimationState());
		m_AnimationStates.back()->Init(newName, newClip, GetCurTime(), CombineWrapMode(newClip->GetWrapMode(), m_WrapMode));		
	}

	CheckIsCullingBasedOnBoundsDeprecated();
	
	SetDirty();
}

void Animation::AddClip (AnimationClip& clip)
{
	AddClip(clip, clip.GetName(), std::numeric_limits<int>::min(), std::numeric_limits<int>::max(), false);
}

void Animation::RemoveClip (AnimationClip& clip)
{
	PROFILER_AUTO(gRemoveClip, this)
	
	// Find the clip to remove
	// We might have the same clip multiple times in the animation list.
	// We are removing elements, so we iterate over it backwards.
	{
		bool found = false;
		int i = m_Animations.size();
		while (i--)
		{
			AnimationClip* cur = m_Animations[i];
			if (cur && cur == &clip) 
			{
				found = true;
				Animations::iterator j = m_Animations.begin() + i;
				m_Animations.erase(j);
			}
		}
		if (!found) 
		{
			AssertStringObject (Format ("Unable to remove Animation Clip '%s' - clip not found in animation list", clip.GetName()), this);
			return;
		}
	}
	
	{
		// Find the animation state(s) to remove
		// We are removing elements, so we iterate over it backwards.
		int i = m_AnimationStates.size();
		while (i--)
		{
			if (m_AnimationStates[i]->m_Clip == &clip) 
			{
				delete m_AnimationStates[i];
				iterator j = m_AnimationStates.begin() + i;
				m_AnimationStates.erase(j);
			}
		}
	}
	
	CheckIsCullingBasedOnBoundsDeprecated ();

	m_DirtyMask |= kRebindDirtyMask;
}

void Animation::RemoveClip (const std::string &clipName)
{
	PROFILER_AUTO(gRemoveClip, this)

	// Find the clip to remove
	// We might have the same clip multiple times in the animation list.
	// We are removing elements, so we iterate over it backwards.
	{
		bool found = false;
		int i = m_Animations.size();
		while (i--)
		{
			AnimationClip* cur = m_Animations[i];
			if (cur && cur->GetName() == clipName) 
			{
				found = true;
				Animations::iterator j = m_Animations.begin() + i;
				m_Animations.erase(j);
			}
		}
		if (!found) 
		{
			AssertStringObject (Format ("Unable to remove Animation Clip '%s' - clip not found in animation list", clipName.c_str()), this);
			return;
		}
	}
	
	{
		// Find the animation state(s) to remove
		// We are removing elements, so we iterate over it backwards.
		int i = m_AnimationStates.size();
		while (i--)
		{
			AnimationState *cur = m_AnimationStates[i];
			if (cur && cur->GetName() == clipName) 
			{
				delete cur;
				iterator j = m_AnimationStates.begin() + i;
				m_AnimationStates.erase(j);
			}
		}
	}	

	CheckIsCullingBasedOnBoundsDeprecated();

	m_DirtyMask |= kRebindDirtyMask;
}

int Animation::GetClipCount () const {
	return m_Animations.size ();
}

AnimationState* Animation::GetState(const string& name)
{
	BuildAnimationStates();

	for (AnimationStates::iterator i=m_AnimationStates.begin();i!=m_AnimationStates.end();i++)
	{
		AnimationState& state = **i;
		if (state.m_Name == name)
			return &state;
	}
	return NULL;
}

AnimationState* Animation::GetState(AnimationClip* clip)
{
	BuildAnimationStates();
	for (iterator i=begin();i != end();i++)
	{
		
		if ((**i).GetClip() == clip)
			return *i;
	}
	return NULL;
}

AnimationState* Animation::GetState(AnimationState* state)
{
	BuildAnimationStates();
	for (iterator i=begin();i != end();i++)
	{
		if (*i == state)
			return state;
	}
	return NULL;
}

void Animation::InitializeClass ()
{
	AnimationState::InitializeClass();
	AnimationManager::InitializeClass();
	
	RegisterAllowNameConversion("Animation", "m_PlayFixedFrameRate", "m_AnimatePhysics");
	RegisterAllowNameConversion("Animation", "m_AnimateIfVisible", "m_AnimateOnlyIfVisible");
}

void Animation::CleanupClass ()
{
	AnimationState::CleanupClass();
	AnimationManager::CleanupClass();
}


template<class TransferFunction>
void Animation::Transfer (TransferFunction& transfer)
{
	Super::Transfer (transfer);
	
	transfer.SetVersion (3);
	
#if UNITY_EDITOR
	if (transfer.IsOldVersion(1))
	{
		TRANSFER_SIMPLE (m_Animation);
		transfer.Transfer (m_OldAnimations, "m_Animations");
		TRANSFER_SIMPLE (m_WrapMode);
		TRANSFER_SIMPLE (m_PlayAutomatically);
		transfer.Transfer (m_AnimatePhysics, "m_PlayFixedFrameRate");
		return;
	}
#endif
	
	
	TRANSFER_SIMPLE (m_Animation);
	TRANSFER_SIMPLE (m_Animations);
	
	// Hide the wrapmode in the inspector if the user has not changed it already... We are in the process of deprecating it
#if UNITY_EDITOR
	bool hide = (transfer.GetFlags () & kSerializeForInspector) != 0 && (transfer.GetFlags () & kSerializeDebugProperties) == 0 && m_WrapMode == 0;
	transfer.Transfer (m_WrapMode, "m_WrapMode", hide ? kHideInEditorMask : kNoTransferFlags);
#else
	transfer.Transfer (m_WrapMode, "m_WrapMode", kNoTransferFlags);
#endif	
	
	// In Unity 3.4 we switched to the m_CullingType enum. Previously we serialized animateOnlyIfVisible.
	if (transfer.IsOldVersion(2))
	{
		bool animateOnlyIfVisible = false;
		transfer.Transfer(animateOnlyIfVisible, "m_AnimateOnlyIfVisible");
		m_CullingType = animateOnlyIfVisible ? kCulling_BasedOnRenderers : kCulling_AlwaysAnimate;
	}
	
	TRANSFER_SIMPLE (m_PlayAutomatically);
	TRANSFER (m_AnimatePhysics);

	transfer.Align();
	
	// We hide these two fields here, because they are displayed by custom inspector (AnimationEditor)
	TRANSFER_ENUM(m_CullingType);
	if (transfer.IsReading())
		m_CullingType = RemapDeprecatedCullingType(m_CullingType);
	
	
	TRANSFER_DEBUG(m_AnimationStates);
}

/*

 TODO:
 
 * Allow for reimport animations while in playmode!
 
- Make a list of all mesh users and invalidate them when the mesh changes!
- implement all missing functions
- Implement mixing properly (Based on which curves have changed)
- support delay cross fade (Starts time advancing only when the animation has faded in completely )
- Don't always add animation component to all game objects! But what if people want to animate a prefab???

- Make the animation system store amount of loops and not one huge float for time?
- Keep a cache for all curves and pass it into animationcurve.Evaluate.
   So that many characters dont kill the cache of shared animation curves.

 - automatically Reduce curves that dont affect anything!
	1. If all animation clips change that curve to the same value and all curves dont actually modify the value!
	2. The current state when loaded is the same as in all curves
    3. Only do it for animation clips that got imported from a fbx file
      - Unity made animations are more likely to be modified by scripting as well 
         so it should not play any tricks on the user
    4. Have an option to turn the optimization off. eg someone wants to do IK etc.

---
- Handle bake simulation better when using clipped animations!
- auto cleanup of queued animations	
- search for !IsWorldPlaying () those are hacks to get the timeline working
- timeline doesnt work anymore!

 - Store optimized animation curves in animation clip instead of animation state.
 - CRASHBUG: Check when animated objects are removed -> rebuild 
 - CRASHBUG: Check when animations change -> rebuild

- Remove normalize fast before blending, not necessary for sampled animations!
  But what about unity made animations?


///@TODO: EXPOSE AnimationState too!(cspreprocess fucks up at the moment)
- implement manual clipping of animations from scripts
- Handle stopping and fading out of animations!
- Add named clip will fail when called at runtime

*/