Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AnimationClip.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Extensions;
8 using SiliconStudio.Core.Mathematics;
9 using SiliconStudio.Core.Serialization;
10 using SiliconStudio.Core.Serialization.Contents;
11 using SiliconStudio.Core.Serialization.Converters;
12 using SiliconStudio.Core.Storage;
13 using SiliconStudio.Paradox.Effects;
14 
15 namespace SiliconStudio.Paradox.DataModel
16 {
17  /// <summary>
18  /// An aggregation of <see cref="AnimationCurve"/> with their channel names.
19  /// </summary>
20  [DataContract]
21  [ContentSerializer(typeof(DataContentSerializer<AnimationClip>))]
22  [DataConverter(ContentReference = true, DataType = false)]
23  public sealed class AnimationClip
24  {
25  // If there is an evaluator, animation clip can't be changed anymore.
26  internal bool Frozen;
27 
28  /// <summary>
29  /// Gets or sets the duration of this clip.
30  /// </summary>
31  /// <value>
32  /// The duration of this clip.
33  /// </value>
34  public TimeSpan Duration { get; set; }
35 
36  /// <summary>
37  /// Gets or sets the repeat mode of the <see cref="AnimationClip"/>.
38  /// </summary>
39  public AnimationRepeatMode RepeatMode { get; set; }
40 
41  /// <summary>
42  /// Gets the channels of this clip.
43  /// </summary>
44  /// <value>
45  /// The channels of this clip.
46  /// </value>
47  public Dictionary<string, Channel> Channels = new Dictionary<string, Channel>();
48 
49  // TODO: The curve stored inside should be internal/private (it is public now to avoid implementing custom serialization before first release).
50  public List<AnimationCurve> Curves = new List<AnimationCurve>();
51 
55 
56  /// <summary>
57  /// Adds a named curve.
58  /// </summary>
59  /// <param name="propertyName">Name of the property.</param>
60  /// <param name="curve">The curve.</param>
61  public void AddCurve(string propertyName, AnimationCurve curve)
62  {
63  if (Frozen)
64  throw new InvalidOperationException("This AnimationClip is frozen");
65 
66  // Add channel
67  Channels.Add(propertyName, new Channel
68  {
69  NodeName = MeshAnimationUpdater.GetNodeName(propertyName),
70  Type = MeshAnimationUpdater.GetType(propertyName),
71  CurveIndex = Curves.Count,
72  ElementType = curve.ElementType,
73  ElementSize = curve.ElementSize,
74  });
75  Curves.Add(curve);
76  }
77 
78  /// <summary>
79  /// Optimizes data from multiple curves to a single linear data stream.
80  /// </summary>
81  public void Optimize()
82  {
83  Freeze();
84 
85  OptimizedCurvesFloat = CreateOptimizedData<float>();
86  OptimizedCurvesVector3 = CreateOptimizedData<Vector3>();
87  OptimizedCurvesQuaternion = CreateOptimizedData<Quaternion>();
88  }
89 
90  private AnimationData<T> CreateOptimizedData<T>()
91  {
92  // Find Vector3 channels
93  var curves = Channels
94  .Where(x => x.Value.CurveIndex != -1 && x.Value.ElementType == typeof(T))
95  .ToDictionary(x => x.Key, x => (AnimationCurve<T>)Curves[x.Value.CurveIndex]);
96 
97  // Update channels
98  foreach (var curve in curves)
99  {
100  var channel = Channels[curve.Key];
101 
102  // CurveIndex -1 means there is optimized data
103  if (channel.CurveIndex != -1)
104  {
105  Curves[channel.CurveIndex] = null;
106  channel.CurveIndex = -1;
107  }
108 
109  Channels[curve.Key] = channel;
110  }
111 
112  return AnimationData<T>.FromAnimationChannels(curves);
113  }
114 
115  internal void Freeze()
116  {
117  Frozen = true;
118  }
119 
120  [DataContract]
121  public struct Channel
122  {
123  public string NodeName;
125 
126  public int CurveIndex;
127  public Type ElementType;
128  public int ElementSize;
129  }
130  }
131 }
void AddCurve(string propertyName, AnimationCurve curve)
Adds a named curve.
Base class for converters to/from a data type.
Applies animation from a AnimationClip to a ModelViewHierarchyUpdater.
AnimationData< Vector3 > OptimizedCurvesVector3
void Optimize()
Optimizes data from multiple curves to a single linear data stream.
An aggregation of AnimationCurve with their channel names.
AnimationRepeatMode
Enumeration describing how an animation should be repeated.
Untyped base class for animation curves.
AnimationData< Quaternion > OptimizedCurvesQuaternion