CMMImporter.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class CMMImporter
  6. {
  7. public static bool ImportOnlyBone(string fileName, GameObject parent)
  8. {
  9. CMMImporter.CMModel cmmodel = new CMMImporter.CMModel();
  10. cmmodel.Deserialize(fileName);
  11. foreach (CMMImporter.CMModel.CMObject cmobject in cmmodel.bones)
  12. {
  13. Transform transform = parent.transform.Find(cmobject.path);
  14. if (transform == null)
  15. {
  16. Debug.Log("skip !! " + cmobject.path);
  17. }
  18. else
  19. {
  20. transform.localPosition = cmobject.localPos;
  21. transform.localRotation = cmobject.localRot;
  22. transform.localScale = cmobject.localScale;
  23. }
  24. }
  25. return true;
  26. }
  27. public static bool ImportModel(string fileName, GameObject parent, TMorph tgtMorph)
  28. {
  29. CMMImporter.CMModel cmmodel = new CMMImporter.CMModel();
  30. cmmodel.Deserialize(fileName);
  31. foreach (CMMImporter.CMModel.CMObject cmobject in cmmodel.bones)
  32. {
  33. Transform transform = parent.transform.Find(cmobject.path);
  34. if (transform == null)
  35. {
  36. Debug.Log("skip !! " + cmobject.path);
  37. }
  38. else
  39. {
  40. transform.localPosition = cmobject.localPos;
  41. transform.localRotation = cmobject.localRot;
  42. transform.localScale = cmobject.localScale;
  43. }
  44. }
  45. foreach (CMMImporter.CMModel.CMMorph cmmorph in cmmodel.morphs)
  46. {
  47. if (!tgtMorph.hash.Contains(cmmorph.name))
  48. {
  49. Debug.LogError("target morph name is not found. " + cmmorph.name);
  50. }
  51. else
  52. {
  53. tgtMorph.SetBlendValues(cmmorph.idx, cmmorph.value);
  54. }
  55. }
  56. tgtMorph.FixBlendValues();
  57. return true;
  58. }
  59. private class CMModel
  60. {
  61. public bool Serialize(string fileName)
  62. {
  63. FileStream fileStream = new FileStream(fileName, FileMode.CreateNew);
  64. BinaryWriter binaryWriter = new BinaryWriter(fileStream);
  65. binaryWriter.Write("CMMODEL");
  66. binaryWriter.Write(100);
  67. binaryWriter.Write(this.bones.Count);
  68. for (int i = 0; i < this.bones.Count; i++)
  69. {
  70. this.bones[i].Serialize(binaryWriter);
  71. }
  72. binaryWriter.Write(this.morphs.Count);
  73. for (int j = 0; j < this.morphs.Count; j++)
  74. {
  75. this.morphs[j].Serialize(binaryWriter);
  76. }
  77. fileStream.Close();
  78. binaryWriter.Close();
  79. return true;
  80. }
  81. public bool Deserialize(string fileName)
  82. {
  83. this.bones.Clear();
  84. this.morphs.Clear();
  85. FileStream fileStream = new FileStream(fileName, FileMode.Open);
  86. BinaryReader binaryReader = new BinaryReader(fileStream);
  87. string text = binaryReader.ReadString();
  88. int num = binaryReader.ReadInt32();
  89. int num2 = binaryReader.ReadInt32();
  90. for (int i = 0; i < num2; i++)
  91. {
  92. this.bones.Add(new CMMImporter.CMModel.CMObject(binaryReader));
  93. }
  94. int num3 = binaryReader.ReadInt32();
  95. for (int j = 0; j < num3; j++)
  96. {
  97. this.morphs.Add(new CMMImporter.CMModel.CMMorph(binaryReader));
  98. }
  99. fileStream.Close();
  100. binaryReader.Close();
  101. return true;
  102. }
  103. private const int CMMODEL_VER = 100;
  104. public List<CMMImporter.CMModel.CMObject> bones = new List<CMMImporter.CMModel.CMObject>();
  105. public List<CMMImporter.CMModel.CMMorph> morphs = new List<CMMImporter.CMModel.CMMorph>();
  106. public class CMObject
  107. {
  108. public CMObject(Transform tr, string parentPath)
  109. {
  110. this.name = tr.name;
  111. this.parentName = ((!(tr.parent != null)) ? null : tr.parent.name);
  112. this.path = this.name + parentPath;
  113. this.localPos = tr.localPosition;
  114. this.localRot = tr.localRotation;
  115. this.localScale = tr.localScale;
  116. }
  117. public CMObject(BinaryReader br)
  118. {
  119. this.name = br.ReadString();
  120. this.parentName = br.ReadString();
  121. this.path = br.ReadString();
  122. this.localPos.x = br.ReadSingle();
  123. this.localPos.y = br.ReadSingle();
  124. this.localPos.z = br.ReadSingle();
  125. this.localRot.x = br.ReadSingle();
  126. this.localRot.y = br.ReadSingle();
  127. this.localRot.z = br.ReadSingle();
  128. this.localRot.w = br.ReadSingle();
  129. this.localScale.x = br.ReadSingle();
  130. this.localScale.y = br.ReadSingle();
  131. this.localScale.z = br.ReadSingle();
  132. }
  133. public bool Serialize(BinaryWriter bw)
  134. {
  135. bw.Write(this.name);
  136. bw.Write((this.parentName != null) ? this.parentName : string.Empty);
  137. bw.Write(this.path);
  138. bw.Write(this.localPos.x);
  139. bw.Write(this.localPos.y);
  140. bw.Write(this.localPos.z);
  141. bw.Write(this.localRot.x);
  142. bw.Write(this.localRot.y);
  143. bw.Write(this.localRot.z);
  144. bw.Write(this.localRot.w);
  145. bw.Write(this.localScale.x);
  146. bw.Write(this.localScale.y);
  147. bw.Write(this.localScale.z);
  148. return true;
  149. }
  150. public string name;
  151. public string parentName;
  152. public string path;
  153. public Vector3 localPos;
  154. public Quaternion localRot;
  155. public Vector3 localScale;
  156. }
  157. public class CMMorph
  158. {
  159. public CMMorph(string name, int idx, float value)
  160. {
  161. this.name = name;
  162. this.idx = idx;
  163. this.value = value;
  164. }
  165. public CMMorph(BinaryReader br)
  166. {
  167. this.name = br.ReadString();
  168. this.idx = br.ReadInt32();
  169. this.value = br.ReadSingle();
  170. }
  171. public bool Serialize(BinaryWriter bw)
  172. {
  173. bw.Write(this.name);
  174. bw.Write(this.idx);
  175. bw.Write(this.value);
  176. return true;
  177. }
  178. public string name;
  179. public int idx;
  180. public float value;
  181. }
  182. }
  183. }