/// <summary> /// Serializes the bone hierarchy of a model to a binary stream. /// </summary> /// <param name="writer"> /// The binary writer to which the bone hierarchy of a model will be serialized. /// </param> /// <param name="root"> /// The bone of the hierarchy to serialize. /// </param> private void SerializeBoneHierarchy(BinaryWriter writer, IModelBone root) { writer.Write(root != null); if (root == null) { return; } writer.Write(root.ID); writer.Write(root.Name); writer.Write((uint)root.Children.Count); foreach (var kv in root.Children) { writer.Write(kv.Key); this.SerializeBoneHierarchy(writer, kv.Value); } this.SerializeMatrix(writer, root.BoneOffset); this.SerializeVector3(writer, root.DefaultTranslation); this.SerializeQuaternion(writer, root.DefaultRotation); this.SerializeVector3(writer, root.DefaultScale); }
/// <summary> /// Initializes a new instance of the <see cref="Model"/> class. /// </summary> /// <param name="availableAnimations"> /// The available animations. /// </param> /// <param name="rootBone"> /// The root bone, or null if there's no skeletal information. /// </param> /// <param name="vertexes"> /// The vertexes associated with this model. /// </param> /// <param name="indices"> /// The indices associated with the model. /// </param> public Model( IModelRenderConfiguration[] modelRenderConfigurations, IRenderBatcher renderBatcher, string name, IAnimationCollection availableAnimations, IMaterial material, IModelBone rootBone, ModelVertex[] vertexes, int[] indices) { Name = name; AvailableAnimations = availableAnimations; Root = rootBone; Vertexes = vertexes; Indices = indices; Material = material; _cachedVertexBuffers = new Dictionary <object, VertexBuffer>(); _modelRenderConfigurations = modelRenderConfigurations; _renderBatcher = renderBatcher; if (Root != null) { _flattenedBones = Root.Flatten(); Bones = _flattenedBones.ToDictionary(k => k.Name, v => v); } }
/// <summary> /// Initializes a new instance of the <see cref="Model"/> class. /// </summary> /// <param name="availableAnimations"> /// The available animations. /// </param> /// <param name="rootBone"> /// The root bone, or null if there's no skeletal information. /// </param> /// <param name="vertexes"> /// The vertexes associated with this model. /// </param> /// <param name="indices"> /// The indices associated with the model. /// </param> public Model( IModelRenderConfiguration[] modelRenderConfigurations, IRenderBatcher renderBatcher, string name, IAnimationCollection availableAnimations, IMaterial material, IModelBone rootBone, ModelVertex[] vertexes, int[] indices) { Name = name; AvailableAnimations = availableAnimations; Root = rootBone; Vertexes = vertexes; Indices = indices; Material = material; _cachedVertexBuffers = new Dictionary<object, VertexBuffer>(); _modelRenderConfigurations = modelRenderConfigurations; _renderBatcher = renderBatcher; if (Root != null) { _flattenedBones = Root.Flatten(); Bones = _flattenedBones.ToDictionary(k => k.Name, v => v); } }
/// <summary> /// Performs the actual flattening of the bone hierarchy. /// </summary> /// <param name="modelBone"> /// The current bone being flattened. /// </param> /// <returns> /// The flattened hierarchy. /// </returns> private static IEnumerable <IModelBone> PerformFlatten(IModelBone modelBone) { yield return(modelBone); foreach (var child in modelBone.Children) { foreach (var entry in PerformFlatten(child.Value)) { yield return(entry); } } }
/// <summary> /// Combines the current matrixes into a final matrix for vertex transformation. /// </summary> /// <returns> /// A matrix representing the transformation to be applied to a vertex. /// </returns> public Matrix GetFinalMatrix() { var finalMatrix = this.BoneOffset; IModelBone applicationNode = this; while (applicationNode != null) { var transform = applicationNode.GetBoneMatrix(); finalMatrix *= transform; applicationNode = applicationNode.Parent; } return(finalMatrix); }
/// <summary> /// Initializes a new instance of the <see cref="Model"/> class. /// </summary> /// <param name="availableAnimations"> /// The available animations. /// </param> /// <param name="rootBone"> /// The root bone, or null if there's no skeletal information. /// </param> /// <param name="vertexes"> /// The vertexes associated with this model. /// </param> /// <param name="indices"> /// The indices associated with the model. /// </param> public Model( IAnimationCollection availableAnimations, IModelBone rootBone, VertexPositionNormalTextureBlendable[] vertexes, int[] indices) { this.AvailableAnimations = availableAnimations; this.Root = rootBone; this.Vertexes = vertexes; this.Indices = indices; if (this.Root != null) { this.m_FlattenedBones = this.Root.Flatten(); this.Bones = this.m_FlattenedBones.ToDictionary(k => k.Name, v => v); } }
/// <summary> /// Performs the actual flattening of the bone hierarchy. /// </summary> /// <param name="modelBone"> /// The current bone being flattened. /// </param> /// <returns> /// The flattened hierarchy. /// </returns> private static IEnumerable<IModelBone> PerformFlatten(IModelBone modelBone) { yield return modelBone; foreach (var child in modelBone.Children) { foreach (var entry in PerformFlatten(child.Value)) { yield return entry; } } }