// <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); }
// <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); }
public static AiScene Parse(this EZMFile ezmFile) { if (ezmFile == null) { throw new ArgumentNullException(); } var aiScene = new AiScene(); aiScene.Fullname = ezmFile.Fullname; // root node. { EZMSkeleton skeleton = ezmFile.MeshSystem.Skeletons[0]; EZMBone[] bones = skeleton.Bones; aiScene.RootNode = Parse(bones[0]); Match(aiScene.RootNode, bones[0]); } // meshes. { EZMMesh[] ezmMeshes = ezmFile.MeshSystem.Meshes; var lstAiMesh = new List <AiMesh>(); for (int i = 0; i < ezmMeshes.Length; i++) { AiMesh[] aiMeshes = Parse(ezmMeshes[i]); lstAiMesh.AddRange(aiMeshes); } aiScene.Meshes = lstAiMesh.ToArray(); } // materials. { EZMMaterial[] ezmMaterials = ezmFile.MeshSystem.Materials; var aiMaterials = new AiMaterial[ezmMaterials.Length]; for (int i = 0; i < aiMaterials.Length; i++) { aiMaterials[i] = Parse(ezmMaterials[i]); } aiScene.Materials = aiMaterials; } // animations. { EZMAnimation[] ezmAnimations = ezmFile.MeshSystem.Animations; var aiAnimations = new AiAnimation[ezmAnimations.Length]; for (int i = 0; i < ezmAnimations.Length; i++) { aiAnimations[i] = Parse(ezmAnimations[i]); } aiScene.Animations = aiAnimations; } { // init material indexes in mesh. foreach (AiMesh aiMesh in aiScene.Meshes) { string name = aiMesh.materialName; for (int i = 0; i < aiScene.Materials.Length; i++) { if (aiScene.Materials[i].Name == name) { aiMesh.MaterialIndex = i; break; } } } } { // bones. EZMSkeleton skeleton = ezmFile.MeshSystem.Skeletons[0]; EZMBone[] bones = skeleton.Bones; var aiBones = new AiBone[bones.Length]; for (int i = 0; i < bones.Length; i++) { var aiBone = new AiBone(); var bone = bones[i]; //bone. aiBones[i] = aiBone; } aiScene.RootNode = Parse(bones[0]); Match(aiScene.RootNode, bones[0]); } return(aiScene); }