2 using System.Collections.Generic;
10 public class MqxReader
13 //todo: rename to MqoPath
14 public string MqoFile;
18 return Path.ChangeExtension(MqoFile, ".mqx");
22 public MqoBone[] bones;
25 public MqoWeit[] weits;
27 public void CreateWeits()
29 weits = new MqoWeit[4];
30 for (int i = 0; i < 4; ++i)
32 weits[i] = new MqoWeit();
37 public bool Load(string mqo_file)
40 string mqx_path = GetMqxPath();
42 if (! File.Exists(mqx_path))
45 XmlReader reader = XmlReader.Create(mqx_path);
52 Dictionary<int, MqoBone> bone_idmap;
58 bone_idmap = new Dictionary<int, MqoBone>();
59 foreach (MqoBone bone in bones)
61 bone_idmap[bone.id] = bone;
67 foreach (MqoBone bone in bones)
71 bone.parent = bone_idmap[bone.pid];
77 foreach (MqoBone bone in bones)
79 if (bone.parent != null)
81 bone.path = bone.parent.path + "|" + bone.name;
84 bone.path = "|" + bone.name;
86 // assign node.local_position
87 foreach (MqoBone bone in bones)
89 if (bone.parent != null)
91 bone.local_position = bone.world_position - bone.parent.world_position;
94 bone.local_position = bone.world_position;
98 public void Read(XmlReader reader)
102 reader.ReadStartElement("MetasequoiaDocument");
104 reader.ReadStartElement("IncludedBy");
105 string mqo_file = reader.ReadString();
106 Console.WriteLine(mqo_file);
107 reader.ReadEndElement();//IncludedBy
109 reader.ReadStartElement("Plugin.56A31D20.71F282AB");
110 reader.ReadStartElement("BoneSet");
112 bones = new MqoBone[len];
114 while (reader.IsStartElement("Bone"))
116 MqoBone bone = new MqoBone(i);
118 this.bones[i++] = bone;
120 reader.ReadEndElement();//BoneSet
123 Array.Resize(ref bones, len);
128 while (reader.IsStartElement("Obj"))
130 //Console.WriteLine("Obj");
131 //Console.WriteLine(" id:{0}", reader.GetAttribute("id"));
135 while (reader.IsStartElement("Poses"))
137 //Console.WriteLine("Poses");
138 //Console.WriteLine(" isExist:{0}", reader.GetAttribute("isExist"));
139 bool empty = reader.IsEmptyElement;
140 reader.Read();//Poses
143 while (reader.IsStartElement("Pose"))
145 //Console.WriteLine("Pose");
146 //Console.WriteLine(" id:{0}", reader.GetAttribute("id"));
149 reader.ReadEndElement();//Poses
151 reader.ReadEndElement();//Plugin.56A31D20.71F282AB
152 reader.ReadEndElement();//MetasequoiaDocument
155 List<Dictionary<int, List<MqoWeit>>> weitmap;
157 public void CreateWeitMap()
159 int maxobjects = 255;
160 weitmap = new List<Dictionary<int, List<MqoWeit>>>(maxobjects);
161 for (int i = 0; i < maxobjects; i++)
163 weitmap.Add(new Dictionary<int, List<MqoWeit>>(2048));
165 foreach (MqoBone bone in bones)
167 foreach (MqoWeit weit in bone.weits)
169 Dictionary<int, List<MqoWeit>> map = weitmap[weit.object_id];
171 if (! map.TryGetValue(weit.vertex_id, out weits))
173 weits = map[weit.vertex_id] = new List<MqoWeit>(4);
180 public void UpdateWeits(int object_id, int vertex_id)
182 List<MqoWeit> weits = weitmap[object_id][vertex_id];
183 int len = weits.Count;
189 for (int i = 0; i < len; ++i)
191 this.weits[i].node_id = weits[i].node_id;
192 this.weits[i].weit = weits[i].weit;
194 for (int i = len; i < 4; ++i)
196 this.weits[i].node_id = 0;
197 this.weits[i].weit = 0.0f;