Exemplo n.º 1
0
        // <Bone name="RootNode" parent="xx" orientation="0 0 0 1" position="0 0 0" scale="1 1 1"/>
        /// <summary>
        ///
        /// </summary>
        /// <param name="xBone"></param>
        /// <returns></returns>
        public static EZMBone Parse(System.Xml.Linq.XElement xElement)
        {
            EZMBone result = null;

            if (xElement.Name == "Bone")
            {
                result = new EZMBone();
                {
                    var name = xElement.Attribute("name");
                    if (name != null)
                    {
                        result.Name = name.Value;
                    }
                }
                {
                    var parent = xElement.Attribute("parent");
                    if (parent != null)
                    {
                        result.ParentName = parent.Value;
                    }
                }
                {
                    vec3 p = new vec3(); Quaternion o = new Quaternion(); vec3 s = new vec3();
                    var  orientation = xElement.Attribute("orientation");
                    if (orientation != null)
                    {
                        string[] parts = orientation.Value.Split(' ');
                        float    x     = float.Parse(parts[0]);
                        float    y     = float.Parse(parts[1]);
                        float    z     = float.Parse(parts[2]);
                        float    w     = float.Parse(parts[3]);
                        o = new Quaternion(w, x, y, z);
                    }
                    var position = xElement.Attribute("position");
                    if (position != null)
                    {
                        string[] parts = position.Value.Split(' ');
                        float    x     = float.Parse(parts[0]);
                        float    y     = float.Parse(parts[1]);
                        float    z     = float.Parse(parts[2]);
                        p = new vec3(x, y, z);
                    }
                    var scale = xElement.Attribute("scale");
                    if (scale != null)
                    {
                        string[] parts = scale.Value.Split(' ');
                        float    x     = float.Parse(parts[0]);
                        float    y     = float.Parse(parts[1]);
                        float    z     = float.Parse(parts[2]);
                        s = new vec3(x, y, z);
                    }

                    result.state         = new EZMBoneState(p, o, s);
                    result.OriginalState = result.state;
                }
            }

            return(result);
        }
Exemplo n.º 2
0
 private static void Traverse(EZMBone bone, EZMBone[] array, ref int index)
 {
     array[index++] = bone;
     foreach (var item in bone.children)
     {
         Traverse(item, array, ref index);
     }
 }
 private static void Match(AiNode aiNode, EZMBone ezmBone)
 {
     foreach (var childBone in ezmBone.children)
     {
         var childNode = Parse(childBone);
         aiNode.Children.Add(childNode);
         Match(childNode, childBone);
     }
 }
        private static AiNode Parse(EZMBone ezmBone)
        {
            var aiNode = new AiNode();

            aiNode.Name      = ezmBone.Name;
            aiNode.Transform = ezmBone.state.matrix;

            return(aiNode);
        }
Exemplo n.º 5
0
        // <Skeleton name="skeleton" count="60">
        /// <summary>
        /// A collection of <see cref="EZMBone"/>s.
        /// </summary>
        /// <param name="xSkeletion"></param>
        /// <returns></returns>
        public static EZMSkeleton Parse(System.Xml.Linq.XElement xElement)
        {
            EZMSkeleton result = null;

            if (xElement.Name == "Skeleton")
            {
                result = new EZMSkeleton();
                {
                    var attr = xElement.Attribute("name");
                    if (attr != null)
                    {
                        result.Name = attr.Value;
                    }
                }
                {
                    var xBones = xElement.Elements("Bone");
                    {
                        var bones = new EZMBone[xBones.Count()];
                        var dict  = new Dictionary <string, EZMBone>();
                        {
                            int index = 0;
                            foreach (var xBone in xBones)
                            {
                                var bone = EZMBone.Parse(xBone);
                                dict.Add(bone.Name, bone);
                                bones[index++] = bone;
                            }
                        }
                        result.nameBoneDict = dict;
                        result.Bones        = bones;
                    }
                    {
                        // form a tree.
                        List <EZMBone> rootBones = new List <EZMBone>();
                        foreach (var item in result.Bones)
                        {
                            string  parentName = item.ParentName;
                            EZMBone parent;
                            if ((parentName != null) && result.nameBoneDict.TryGetValue(parentName, out parent))
                            {
                                item.Parent = parent;
                                parent.children.Add(item);
                            }
                            else
                            {
                                rootBones.Add(item);
                            }
                        }

                        var orderedBones = new EZMBone[xBones.Count()];
                        {
                            // make sure bones are in 'parent - child' order.
                            int index = 0;
                            foreach (var item in rootBones)
                            {
                                Traverse(item, orderedBones, ref index);
                            }
                        }
                        // update absolute bone matrix.
                        EZMBone rootBone = orderedBones[0];
                        mat4    inverse  = glm.inverse(rootBone.OriginalState.matrix);
                        for (int i = 0; i < orderedBones.Length; i++)
                        {
                            EZMBone bone = orderedBones[i];
                            //bone.State.UpdateCache();
                            EZMBone parent = bone.Parent;
                            if (parent == null)
                            {
                                bone.combinedMat = bone.state.matrix;
                            }
                            else
                            {
                                bone.combinedMat = parent.combinedMat * bone.state.matrix;
                            }

                            bone.offsetMat = glm.inverse(bone.combinedMat);
                        }
                        result.OrderedBones = orderedBones;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        // <MeshSystem asset_name="dude.fbx" asset_info="null" mesh_system_version="1" mesh_system_asset_version="0">
        /// <summary>
        ///
        /// </summary>
        /// <param name="xElement"></param>
        /// <returns></returns>
        public static EZMMeshSystem Parse(System.Xml.Linq.XElement xElement)
        {
            EZMMeshSystem result = null;

            if (xElement.Name == "MeshSystem")
            {
                result = new EZMMeshSystem();
                {
                    var attr = xElement.Attribute("asset_name");
                    if (attr != null)
                    {
                        result.AssetName = attr.Value;
                    }
                }
                {
                    var attr = xElement.Attribute("asset_info");
                    if (attr != null)
                    {
                        result.AssetInfo = attr.Value;
                    }
                }
                {
                    var attr = xElement.Attribute("mesh_system_version");
                    if (attr != null)
                    {
                        result.Version = attr.Value;
                    }
                }
                {
                    var attr = xElement.Attribute("mesh_system_asset_version");
                    if (attr != null)
                    {
                        result.AssetVersion = attr.Value;
                    }
                }
                {
                    var skeletionRoot = xElement.Element("Skeletons");
                    if (skeletionRoot != null)
                    {
                        var xSkeletons = skeletionRoot.Elements("Skeleton");
                        var skeletons  = new EZMSkeleton[xSkeletons.Count()];
                        int index      = 0;
                        foreach (var xSkeleton in xSkeletons)
                        {
                            skeletons[index++] = EZMSkeleton.Parse(xSkeleton);
                        }
                        result.Skeletons = skeletons;
                    }
                }
                {
                    var animationRoot = xElement.Element("Animations");
                    if (animationRoot != null)
                    {
                        var xAnimations = animationRoot.Elements("Animation");
                        var animations  = new EZMAnimation[xAnimations.Count()];
                        int index       = 0;
                        foreach (var xAnimation in xAnimations)
                        {
                            var animation = EZMAnimation.Parse(xAnimation);
                            foreach (var animTrack in animation.AnimTracks)
                            {
                                string name = animTrack.BoneName;
                                if (name != null)
                                {
                                    foreach (var skeleton in result.Skeletons)
                                    {
                                        EZMBone bone = null;
                                        if (skeleton.nameBoneDict.TryGetValue(name, out bone))
                                        {
                                            if (animTrack.Bone != null)
                                            {
                                                throw new Exception("More than 1 bone for the animTrack!");
                                            }

                                            animTrack.Bone = bone;
                                            //break;
                                        }
                                    }
                                }
                            }
                            animations[index++] = animation;
                        }
                        result.Animations = animations;
                    }
                }

                var nameMaterialDict = new Dictionary <string, EZMMaterial>();
                {
                    var materialsRoot = xElement.Element("Materials");
                    if (materialsRoot != null)
                    {
                        var xMaterials = materialsRoot.Elements("Material");
                        var materials  = new EZMMaterial[xMaterials.Count()];
                        int index      = 0;
                        foreach (var xMaterial in xMaterials)
                        {
                            var material = EZMMaterial.Parse(xMaterial);
                            nameMaterialDict.Add(material.Name, material);
                            materials[index++] = material;
                        }
                        result.Materials = materials;
                    }
                }
                {
                    var meshesRoot = xElement.Element("Meshes");
                    if (meshesRoot != null)
                    {
                        var xMeshes = meshesRoot.Elements("Mesh");
                        var meshes  = new EZMMesh[xMeshes.Count()];
                        int index   = 0;
                        foreach (var xMesh in xMeshes)
                        {
                            EZMMesh mesh = EZMMesh.Parse(xMesh);
                            foreach (var meshSection in mesh.MeshSections)
                            {
                                string      name     = meshSection.MaterialName;
                                EZMMaterial material = null;
                                if (nameMaterialDict.TryGetValue(name, out material))
                                {
                                    meshSection.Material = material;
                                }
                            }
                            EZMSkeleton[] skeletons = result.Skeletons;
                            if (skeletons.Length > 0)
                            {
                                if (string.IsNullOrEmpty(mesh.SkeletonName) ||
                                    mesh.SkeletonName == "null")
                                {
                                    mesh.Skeleton = skeletons[0];
                                }
                                else
                                {
                                    foreach (var skeleton in skeletons)
                                    {
                                        if (mesh.SkeletonName == skeleton.Name)
                                        {
                                            mesh.Skeleton = skeleton;
                                            break;
                                        }
                                    }
                                }
                            }
                            meshes[index++] = mesh;
                        }
                        result.Meshes = meshes;
                    }
                }
            }

            return(result);
        }