Exemple #1
0
        /// <summary>
        ///     Levanta la informacion de una animacion a partir del XML
        /// </summary>
        /// <param name="xmlString">Contenido que el XML</param>
        /// <returns>Información parseada</returns>
        public TgcSkeletalAnimationData parseAnimationFromString(string xmlString)
        {
            var dom = new XmlDocument();

            dom.LoadXml(xmlString);
            var root = dom.DocumentElement;

            var animation = new TgcSkeletalAnimationData();

            //Parsear informacion general de animation
            var animationNode = (XmlElement)root.GetElementsByTagName("animation")[0];

            animation.name        = animationNode.Attributes["name"].InnerText;
            animation.bonesCount  = int.Parse(animationNode.Attributes["bonesCount"].InnerText);
            animation.framesCount = int.Parse(animationNode.Attributes["framesCount"].InnerText);
            animation.frameRate   = int.Parse(animationNode.Attributes["frameRate"].InnerText);
            animation.startFrame  = int.Parse(animationNode.Attributes["startFrame"].InnerText);
            animation.endFrame    = int.Parse(animationNode.Attributes["endFrame"].InnerText);

            //Parsear boundingBox, si esta
            var boundingBoxNodes = animationNode.GetElementsByTagName("boundingBox");

            if (boundingBoxNodes != null && boundingBoxNodes.Count == 1)
            {
                var boundingBoxNode = boundingBoxNodes[0];
                animation.pMin = TgcParserUtils.parseFloat3Array(boundingBoxNode.Attributes["min"].InnerText);
                animation.pMax = TgcParserUtils.parseFloat3Array(boundingBoxNode.Attributes["max"].InnerText);
            }

            //Parsear bones
            var boneNodes = animationNode.GetElementsByTagName("bone");

            animation.bonesFrames = new TgcSkeletalAnimationBoneData[boneNodes.Count];
            var boneIdx = 0;

            foreach (XmlElement boneNode in boneNodes)
            {
                var boneData = new TgcSkeletalAnimationBoneData();
                boneData.id             = TgcParserUtils.parseInt(boneNode.Attributes["id"].InnerText);
                boneData.keyFramesCount = TgcParserUtils.parseInt(boneNode.Attributes["keyFramesCount"].InnerText);
                boneData.keyFrames      = new TgcSkeletalAnimationBoneFrameData[boneData.keyFramesCount];

                //Parsear frames
                var frames = 0;
                foreach (XmlElement frameNode in boneNode.ChildNodes)
                {
                    var frameData = new TgcSkeletalAnimationBoneFrameData();
                    frameData.frame    = TgcParserUtils.parseInt(frameNode.Attributes["n"].InnerText);
                    frameData.position = TgcParserUtils.parseFloat3Array(frameNode.Attributes["pos"].InnerText);
                    frameData.rotation = TgcParserUtils.parseFloat4Array(frameNode.Attributes["rotQuat"].InnerText);

                    boneData.keyFrames[frames++] = frameData;
                }

                animation.bonesFrames[boneIdx++] = boneData;
            }

            return(animation);
        }
Exemple #2
0
        /// <summary>
        ///     Cargar estructura de animacion
        /// </summary>
        private TgcSkeletalAnimation loadAnimation(TgcSkeletalMesh mesh, TgcSkeletalAnimationData animationData)
        {
            //Crear array para todos los huesos, tengan o no keyFrames
            var boneFrames = new List <TgcSkeletalAnimationFrame> [mesh.Bones.Length];

            //Cargar los frames para los huesos que si tienen
            for (var i = 0; i < animationData.bonesFrames.Length; i++)
            {
                var boneData = animationData.bonesFrames[i];

                //Crear frames
                for (var j = 0; j < boneData.keyFrames.Length; j++)
                {
                    var frameData = boneData.keyFrames[j];

                    var frame = new TgcSkeletalAnimationFrame(
                        frameData.frame,
                        new TGCVector3(frameData.position[0], frameData.position[1], frameData.position[2]),
                        new TGCQuaternion(frameData.rotation[0], frameData.rotation[1], frameData.rotation[2],
                                          frameData.rotation[3])
                        );

                    //Agregar a lista de frames del hueso
                    if (boneFrames[boneData.id] == null)
                    {
                        boneFrames[boneData.id] = new List <TgcSkeletalAnimationFrame>();
                    }
                    boneFrames[boneData.id].Add(frame);
                }
            }

            //BoundingBox de la animación, aprovechar lo que viene en el XML o utilizar el de la malla estática
            TgcBoundingAxisAlignBox boundingBox = null;

            if (animationData.pMin != null && animationData.pMax != null)
            {
                boundingBox = new TgcBoundingAxisAlignBox(
                    TGCVector3.Float3ArrayToVector3(animationData.pMin),
                    TGCVector3.Float3ArrayToVector3(animationData.pMax));
            }
            else
            {
                boundingBox = mesh.BoundingBox;
            }

            //Crear animacion
            var animation = new TgcSkeletalAnimation(animationData.name, animationData.frameRate,
                                                     animationData.framesCount, boneFrames, boundingBox);

            return(animation);
        }