Skip to content
This repository was archived by the owner on Mar 30, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 209 additions & 41 deletions Source/Toolkit/SharpDX.Toolkit.Compiler/Model/ModelCompiler.cs

Large diffs are not rendered by default.

165 changes: 165 additions & 0 deletions Source/Toolkit/SharpDX.Toolkit.Game/AnimationSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using SharpDX.Toolkit.Graphics;

using System;
using System.Collections.Generic;

namespace SharpDX.Toolkit
{
/// <summary>
/// Manages and drives skeletal animations.
/// </summary>
public class AnimationSystem : GameSystem
{
private Dictionary<Model, AnimationState> animationStates;

/// <summary>
/// Initializes a new instance of the <see cref="AnimationSystem" /> class.
/// </summary>
/// <param name="game">The game.</param>
public AnimationSystem(Game game)
: base(game)
{
animationStates = new Dictionary<Model, AnimationState>();
Enabled = true;
}

/// <summary>
/// Starts a new animation.
/// </summary>
/// <param name="model">The animated model.</param>
/// <param name="animation">The animation to play.</param>
public void StartAnimation(Model model, ModelAnimation animation)
{
if (model == null)
{
throw new ArgumentNullException("model");
}

if (animation == null)
{
throw new ArgumentNullException("animation");
}

animationStates[model] = new AnimationState
{
Model = model,
Animation = animation,
ChannelMap = BuildChannelMap(model, animation)
};
}

/// <summary>
/// Stop the animation of a model.
/// </summary>
/// <param name="model">The animated model.</param>
public void StopAnimation(Model model)
{
if (model == null)
{
throw new ArgumentNullException("model");
}

animationStates.Remove(model);
}

public override void Update(GameTime gameTime)
{
foreach (var state in animationStates.Values)
{
var animation = state.Animation;

if (MathUtil.IsZero(animation.Duration))
{
state.CurrentTime = 0.0f;
}
else
{
state.CurrentTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
state.CurrentTime %= animation.Duration;
}

for (int i = 0; i < animation.Channels.Count; i++)
{
var boneIndex = state.ChannelMap[i];
if (boneIndex < 0)
{
continue;
}

var bone = state.Model.Bones[boneIndex];
var keyFrames = animation.Channels[i].KeyFrames;

int nextIndex = 0;
int lastIndex = keyFrames.Count - 1;

while (nextIndex < lastIndex && state.CurrentTime >= keyFrames[nextIndex].Time)
{
nextIndex++;
}

var nextKeyFrame = keyFrames[nextIndex];

if (nextIndex == 0)
{
bone.Transform = (Matrix)nextKeyFrame.Value;
}
else
{
var previousKeyFrame = keyFrames[nextIndex - 1];
var amount = (state.CurrentTime - previousKeyFrame.Time) / (nextKeyFrame.Time - previousKeyFrame.Time);

bone.Transform = (Matrix)CompositeTransform.Slerp(previousKeyFrame.Value, nextKeyFrame.Value, amount);
}
}
}
}

private int[] BuildChannelMap(Model model, ModelAnimation animation)
{
var channelMap = new int[animation.Channels.Count];

for (int i = 0; i < animation.Channels.Count; i++)
{
channelMap[i] = -1;
foreach (var bone in model.Bones)
{
if (bone.Name == animation.Channels[i].BoneName)
{
channelMap[i] = bone.Index;
break;
}
}
}

return channelMap;
}

private class AnimationState
{
public float CurrentTime;
public Model Model;
public ModelAnimation Animation;
public int[] ChannelMap;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<Compile Include="..\..\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="AnimationSystem.cs" />
<Compile Include="AssemblyDoc.cs" />
<Compile Include="Desktop\GameWindowDesktopWpf.cs" />
<Compile Include="Desktop\SharpDXElement.cs" />
Expand Down
48 changes: 31 additions & 17 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ namespace SharpDX.Toolkit.Graphics
[ContentReader(typeof(ModelContentReader))]
public class Model : Component
{
private static Matrix[] sharedDrawBones;

public MaterialCollection Materials;

public ModelBoneCollection Bones;

//// DISABLE_SKINNED_BONES
//public ModelBoneCollection SkinnedBones;

public ModelMeshCollection Meshes;

public ModelAnimationCollection Animations;

public PropertyCollection Properties;

/// <summary>
Expand Down Expand Up @@ -160,9 +161,13 @@ public unsafe void Draw(GraphicsDevice context, Matrix world, Matrix view, Matri
{
int count = Meshes.Count;
int boneCount = Bones.Count;
Matrix* localSharedDrawBoneMatrices = stackalloc Matrix[boneCount]; // TODO use a global cache as BoneCount could generate a StackOverflow

CopyAbsoluteBoneTransformsTo(new IntPtr(localSharedDrawBoneMatrices));
if (sharedDrawBones == null || sharedDrawBones.Length < boneCount)
{
sharedDrawBones = new Matrix[boneCount];
}

CopyAbsoluteBoneTransformsTo(sharedDrawBones);

var defaultParametersContext = default(EffectDefaultParametersContext);

Expand All @@ -174,10 +179,16 @@ public unsafe void Draw(GraphicsDevice context, Matrix world, Matrix view, Matri

if (effectOverride != null)
{
Matrix worldTranformed;
Matrix.Multiply(ref localSharedDrawBoneMatrices[index], ref world, out worldTranformed);

effectOverride.DefaultParameters.Apply(ref defaultParametersContext, ref worldTranformed, ref view, ref projection);
if (effectOverride is SkinnedEffect)
{
effectOverride.DefaultParameters.Apply(ref defaultParametersContext, ref world, ref view, ref projection);
}
else
{
Matrix worldTranformed;
Matrix.Multiply(ref sharedDrawBones[index], ref world, out worldTranformed);
effectOverride.DefaultParameters.Apply(ref defaultParametersContext, ref worldTranformed, ref view, ref projection);
}
}
else
{
Expand All @@ -190,27 +201,26 @@ public unsafe void Draw(GraphicsDevice context, Matrix world, Matrix view, Matri
}

Matrix worldTranformed;
Matrix.Multiply(ref localSharedDrawBoneMatrices[index], ref world, out worldTranformed);
Matrix.Multiply(ref sharedDrawBones[index], ref world, out worldTranformed);

var matrices = effect as IEffectMatrices;
if (matrices == null)
{
effect.DefaultParameters.Apply(ref defaultParametersContext, ref worldTranformed, ref view, ref projection);
effect.DefaultParameters.Apply(ref defaultParametersContext, ref world, ref view, ref projection);
}
else
{
matrices.World = worldTranformed;
matrices.World = effect is SkinnedEffect ? world : worldTranformed;
matrices.View = view;
matrices.Projection = projection;
}
}
}

mesh.Draw(context, effectOverride);
mesh.Draw(context, sharedDrawBones, effectOverride);
}
}


/// <summary>
/// Calculates the bounds of this model.
/// </summary>
Expand All @@ -229,16 +239,20 @@ public unsafe BoundingSphere CalculateBounds(Matrix world)
{
int count = Meshes.Count;
int boneCount = Bones.Count;
Matrix* localSharedDrawBoneMatrices = stackalloc Matrix[boneCount]; // TODO use a global cache as BoneCount could generate a StackOverflow

CopyAbsoluteBoneTransformsTo(new IntPtr(localSharedDrawBoneMatrices));
if (sharedDrawBones == null || sharedDrawBones.Length < boneCount)
{
sharedDrawBones = new Matrix[boneCount];
}

CopyAbsoluteBoneTransformsTo(sharedDrawBones);
var defaultSphere = new BoundingSphere(Vector3.Zero, 0.0f);
for (int i = 0; i < count; i++)
{
var mesh = Meshes[i];
int index = mesh.ParentBone.Index;
Matrix result;
Matrix.Multiply(ref localSharedDrawBoneMatrices[index], ref world, out result);
Matrix.Multiply(ref sharedDrawBones[index], ref world, out result);

var meshSphere = mesh.BoundingSphere;
Vector3.TransformCoordinate(ref meshSphere.Center, ref result, out meshSphere.Center);
Expand Down
39 changes: 39 additions & 0 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/ModelAnimation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;

namespace SharpDX.Toolkit.Graphics
{
public class ModelAnimation : ComponentBase
{
public string Name;

public float Duration;

public List<ModelData.AnimationChannel> Channels;

public ModelAnimation()
{
Channels = new List<ModelData.AnimationChannel>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SharpDX.Toolkit.Graphics
{
public class ModelAnimationCollection : List<ModelAnimation>
{
public ModelAnimationCollection()
{
}

public ModelAnimationCollection(int capacity)
: base(capacity)
{
}

public ModelAnimationCollection(IEnumerable<ModelAnimation> collection)
: base(collection)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ModelBoneCollection()
}

/// <summary>
/// Initializes a new instance of the <see cref="T:System.Collections.Generic.List`1" /> class that is empty and has the specified initial capacity.
/// Initializes a new instance of the <see cref="ModelBoneCollection" /> class that is empty and has the specified initial capacity.
/// </summary>
/// <param name="capacity">The number of elements that the new list can initially store.</param>
public ModelBoneCollection(int capacity)
Expand Down
Loading