ImportCM2.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using UnityEngine;
  7. public class ImportCM2 : MonoBehaviour
  8. {
  9. public static byte[] m_aniTempFile = null;
  10. private static Dictionary<string, Material> m_dicCacheMaterial = new Dictionary<string, Material>();
  11. private static Dictionary<int, KeyValuePair<string, float>> m_hashPriorityMaterials;
  12. private static byte[] m_matTempFile;
  13. private static byte[] m_skinTempFile;
  14. private static string[] properties = new string[7]
  15. {
  16. "m_LocalRotation.x", "m_LocalRotation.y", "m_LocalRotation.z", "m_LocalRotation.w", "m_LocalPosition.x", "m_LocalPosition.y",
  17. "m_LocalPosition.z"
  18. };
  19. public static GameObject LoadSkinMesh_R(string filename, string[] filename2, int layer)
  20. {
  21. try
  22. {
  23. using (AFileBase afileBase = GameUty.FileOpen(filename, null))
  24. {
  25. if (m_skinTempFile == null)
  26. m_skinTempFile = new byte[Math.Max(500000, afileBase.GetSize())];
  27. else if (m_skinTempFile.Length < afileBase.GetSize())
  28. m_skinTempFile = new byte[afileBase.GetSize()];
  29. afileBase.Read(ref m_skinTempFile, afileBase.GetSize());
  30. }
  31. }
  32. catch (Exception ex)
  33. {
  34. NDebug.Assert("ファイルが開けませんでした。" + filename + "\n" + ex.Message, false);
  35. }
  36. var r = new BinaryReader(new MemoryStream(m_skinTempFile), Encoding.UTF8);
  37. TBodySkin.OriVert oriVert = new TBodySkin.OriVert();
  38. var gameObject1 = Instantiate(Resources.Load("seed")) as GameObject;
  39. gameObject1.layer = layer;
  40. var gameObject2 = (GameObject)null;
  41. var hashtable = new Hashtable();
  42. string str1 = r.ReadString();
  43. if (str1 != "CM3D2_MESH")
  44. NDebug.Assert("LoadSkinMesh_R 例外 : ヘッダーファイルが不正です。" + str1, false);
  45. int num1 = r.ReadInt32();
  46. string str2 = r.ReadString();
  47. gameObject1.name = "_SM_" + str2;
  48. string str3 = r.ReadString();
  49. int num2 = r.ReadInt32();
  50. var gameObjectList = new List<GameObject>();
  51. for (int index = 0; index < num2; ++index)
  52. {
  53. var gameObject3 = Instantiate(Resources.Load("seed")) as GameObject;
  54. gameObject3.layer = layer;
  55. gameObject3.name = r.ReadString();
  56. gameObjectList.Add(gameObject3);
  57. if (gameObject3.name == str3)
  58. gameObject2 = gameObject3;
  59. hashtable[gameObject3.name] = gameObject3;
  60. if (r.ReadByte() != 0)
  61. {
  62. var gameObject4 = Instantiate(Resources.Load("seed")) as GameObject;
  63. gameObject4.name = gameObject3.name + "_SCL_";
  64. gameObject4.transform.parent = gameObject3.transform;
  65. hashtable[gameObject3.name + "&_SCL_"] = gameObject4;
  66. }
  67. }
  68. for (int index1 = 0; index1 < num2; ++index1)
  69. {
  70. int index2 = r.ReadInt32();
  71. gameObjectList[index1].transform.parent = index2 < 0 ? gameObject1.transform : gameObjectList[index2].transform;
  72. }
  73. for (int index = 0; index < num2; ++index)
  74. {
  75. Transform transform = gameObjectList[index].transform;
  76. float x1 = r.ReadSingle();
  77. float y1 = r.ReadSingle();
  78. float z1 = r.ReadSingle();
  79. transform.localPosition = new Vector3(x1, y1, z1);
  80. float x2 = r.ReadSingle();
  81. float y2 = r.ReadSingle();
  82. float z2 = r.ReadSingle();
  83. float w = r.ReadSingle();
  84. transform.localRotation = new Quaternion(x2, y2, z2, w);
  85. if (2001 <= num1 && r.ReadBoolean())
  86. {
  87. float x3 = r.ReadSingle();
  88. float y3 = r.ReadSingle();
  89. float z3 = r.ReadSingle();
  90. transform.localScale = new Vector3(x3, y3, z3);
  91. }
  92. }
  93. int length1 = r.ReadInt32();
  94. int length2 = r.ReadInt32();
  95. int length3 = r.ReadInt32();
  96. oriVert.VCount = length1;
  97. oriVert.nSubMeshCount = length2;
  98. gameObject2.AddComponent(typeof(SkinnedMeshRenderer));
  99. gameObject1.AddComponent(typeof(Animation));
  100. var component = gameObject2.GetComponent<Renderer>() as SkinnedMeshRenderer;
  101. component.updateWhenOffscreen = true;
  102. var transformArray = new Transform[length3];
  103. for (int index = 0; index < length3; ++index)
  104. {
  105. string str4 = r.ReadString();
  106. if (hashtable.ContainsKey(str4))
  107. {
  108. GameObject gameObject3 = !hashtable.ContainsKey(str4 + "&_SCL_")
  109. ? (GameObject)hashtable[str4]
  110. : (GameObject)hashtable[str4 + "&_SCL_"];
  111. transformArray[index] = gameObject3.transform;
  112. }
  113. }
  114. component.bones = transformArray;
  115. var mesh1 = new Mesh();
  116. component.sharedMesh = mesh1;
  117. Mesh mesh2 = mesh1;
  118. var matrix4x4Array = new Matrix4x4[length3];
  119. for (int index1 = 0; index1 < length3; ++index1)
  120. for (int index2 = 0; index2 < 16; ++index2)
  121. matrix4x4Array[index1][index2] = r.ReadSingle();
  122. mesh2.bindposes = matrix4x4Array;
  123. var vector3Array1 = new Vector3[length1];
  124. var vector3Array2 = new Vector3[length1];
  125. var vector2Array = new Vector2[length1];
  126. var boneWeightArray = new BoneWeight[length1];
  127. for (int index = 0; index < length1; ++index)
  128. {
  129. float new_x1 = r.ReadSingle();
  130. float new_y1 = r.ReadSingle();
  131. float new_z1 = r.ReadSingle();
  132. vector3Array1[index].Set(new_x1, new_y1, new_z1);
  133. float new_x2 = r.ReadSingle();
  134. float new_y2 = r.ReadSingle();
  135. float new_z2 = r.ReadSingle();
  136. vector3Array2[index].Set(new_x2, new_y2, new_z2);
  137. float newX = r.ReadSingle();
  138. float newY = r.ReadSingle();
  139. vector2Array[index].Set(newX, newY);
  140. }
  141. mesh2.vertices = vector3Array1;
  142. mesh2.normals = vector3Array2;
  143. mesh2.uv = vector2Array;
  144. oriVert.vOriVert = vector3Array1;
  145. oriVert.vOriNorm = vector3Array2;
  146. int length4 = r.ReadInt32();
  147. if (length4 > 0)
  148. {
  149. var vector4Array = new Vector4[length4];
  150. for (int index = 0; index < length4; ++index)
  151. {
  152. float x = r.ReadSingle();
  153. float y = r.ReadSingle();
  154. float z = r.ReadSingle();
  155. float w = r.ReadSingle();
  156. vector4Array[index] = new Vector4(x, y, z, w);
  157. }
  158. mesh2.tangents = vector4Array;
  159. }
  160. for (int index = 0; index < length1; ++index)
  161. {
  162. boneWeightArray[index].boneIndex0 = r.ReadUInt16();
  163. boneWeightArray[index].boneIndex1 = r.ReadUInt16();
  164. boneWeightArray[index].boneIndex2 = r.ReadUInt16();
  165. boneWeightArray[index].boneIndex3 = r.ReadUInt16();
  166. boneWeightArray[index].weight0 = r.ReadSingle();
  167. boneWeightArray[index].weight1 = r.ReadSingle();
  168. boneWeightArray[index].weight2 = r.ReadSingle();
  169. boneWeightArray[index].weight3 = r.ReadSingle();
  170. }
  171. mesh2.boneWeights = boneWeightArray;
  172. mesh2.subMeshCount = length2;
  173. oriVert.bwWeight = boneWeightArray;
  174. oriVert.nSubMeshCount = length2;
  175. oriVert.nSubMeshOriTri = new int[length2][];
  176. for (int submesh = 0; submesh < length2; ++submesh)
  177. {
  178. int length5 = r.ReadInt32();
  179. var triangles = new int[length5];
  180. for (int index = 0; index < length5; ++index)
  181. triangles[index] = r.ReadUInt16();
  182. oriVert.nSubMeshOriTri[submesh] = triangles;
  183. mesh2.SetTriangles(triangles, submesh);
  184. }
  185. int length6 = r.ReadInt32();
  186. var materialArray = new Material[length6];
  187. for (int index = 0; index < length6; ++index)
  188. {
  189. Material material = ReadMaterial(r, filename2[1 + index]);
  190. materialArray[index] = material;
  191. }
  192. component.materials = materialArray;
  193. r.Close();
  194. return gameObject1;
  195. }
  196. public static Material ReadMaterial(BinaryReader r, string filename)
  197. {
  198. if (m_hashPriorityMaterials == null)
  199. {
  200. m_hashPriorityMaterials = new Dictionary<int, KeyValuePair<string, float>>();
  201. var list = GameUty.FileSystem.GetList("prioritymaterial", AFileSystemBase.ListType.AllFile);
  202. if (list != null && 0 < list.Length)
  203. for (int index = 0; index < list.Length; ++index)
  204. if (Path.GetExtension(list[index]) == ".pmat")
  205. {
  206. string fileName = list[index];
  207. using (AFileBase afileBase = GameUty.FileOpen(fileName, null))
  208. {
  209. NDebug.Assert(afileBase.IsValid(), fileName + "を開けませんでした");
  210. using (var binaryReader = new BinaryReader(new MemoryStream(afileBase.ReadAll()), Encoding.UTF8))
  211. {
  212. NDebug.Assert(binaryReader.ReadString() == "CM3D2_PMATERIAL", "ヘッダーエラー\n" + fileName);
  213. binaryReader.ReadInt32();
  214. int key1 = binaryReader.ReadInt32();
  215. string key2 = binaryReader.ReadString();
  216. float num = binaryReader.ReadSingle();
  217. NDebug.Assert(!m_hashPriorityMaterials.ContainsKey(key1), "すでにハッシュが登録されています");
  218. m_hashPriorityMaterials.Add(key1, new KeyValuePair<string, float>(key2, num));
  219. }
  220. }
  221. }
  222. }
  223. string str1 = r.ReadString();
  224. string str2 = r.ReadString();
  225. string path1 = "DefMaterial/" + r.ReadString();
  226. Material material;
  227. var original = Resources.Load(path1, typeof(Material)) as Material;
  228. if (original == null)
  229. return original;
  230. material = Instantiate(original);
  231. material.name = str1;
  232. int hashCode1 = material.name.GetHashCode();
  233. if (m_hashPriorityMaterials != null && m_hashPriorityMaterials.ContainsKey(hashCode1))
  234. {
  235. var priorityMaterial = m_hashPriorityMaterials[hashCode1];
  236. if (priorityMaterial.Key == material.name)
  237. {
  238. material.SetFloat("_SetManualRenderQueue", priorityMaterial.Value);
  239. material.renderQueue = (int)priorityMaterial.Value;
  240. }
  241. }
  242. string str3;
  243. Vector2 vector2_1;
  244. Vector2 vector2_2;
  245. string str4;
  246. string str5;
  247. Color color;
  248. Vector4 vector4;
  249. while (true)
  250. {
  251. string str6 = r.ReadString();
  252. if (!(str6 == "end"))
  253. {
  254. string name = r.ReadString();
  255. if (str6 == "tex")
  256. {
  257. string str7 = r.ReadString();
  258. if (str7 == "null")
  259. {
  260. material.SetTexture(name, null);
  261. }
  262. else if (str7 == "tex2d")
  263. {
  264. try
  265. {
  266. string str8 = r.ReadString();
  267. str3 = r.ReadString();
  268. var data = ImportCM.LoadTexture(GameUty.FileSystem, str8 + ".tex", false).data;
  269. vector2_1.x = r.ReadSingle();
  270. vector2_1.y = r.ReadSingle();
  271. vector2_2.x = r.ReadSingle();
  272. vector2_2.y = r.ReadSingle();
  273. if (filename == null || !filename.Contains(".mate"))
  274. {
  275. var texture2D = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  276. texture2D.LoadImage(data);
  277. texture2D.name = str8;
  278. texture2D.wrapMode = TextureWrapMode.Repeat;
  279. material.SetTexture(name, texture2D);
  280. material.SetTextureOffset(name, vector2_1);
  281. material.SetTextureScale(name, vector2_2);
  282. }
  283. }
  284. catch
  285. {
  286. break;
  287. }
  288. }
  289. else if (str7 == "texRT")
  290. {
  291. str4 = r.ReadString();
  292. str5 = r.ReadString();
  293. }
  294. }
  295. else if (str6 == "col")
  296. {
  297. color.r = r.ReadSingle();
  298. color.g = r.ReadSingle();
  299. color.b = r.ReadSingle();
  300. color.a = r.ReadSingle();
  301. material.SetColor(name, color);
  302. }
  303. else if (str6 == "vec")
  304. {
  305. vector4.x = r.ReadSingle();
  306. vector4.y = r.ReadSingle();
  307. vector4.z = r.ReadSingle();
  308. vector4.w = r.ReadSingle();
  309. material.SetVector(name, vector4);
  310. }
  311. else if (str6 == "f")
  312. {
  313. float num = r.ReadSingle();
  314. material.SetFloat(name, num);
  315. }
  316. }
  317. else
  318. {
  319. break;
  320. }
  321. }
  322. if (filename != null && filename.Contains(".mate"))
  323. {
  324. try
  325. {
  326. using (AFileBase afileBase = GameUty.FileOpen(filename, null))
  327. {
  328. if (m_matTempFile == null)
  329. m_matTempFile = new byte[Math.Max(10000, afileBase.GetSize())];
  330. else if (m_matTempFile.Length < afileBase.GetSize())
  331. m_matTempFile = new byte[afileBase.GetSize()];
  332. afileBase.Read(ref m_matTempFile, afileBase.GetSize());
  333. }
  334. }
  335. catch { }
  336. var binaryReader = new BinaryReader(new MemoryStream(m_matTempFile), Encoding.UTF8);
  337. string str6 = binaryReader.ReadString();
  338. NDebug.Assert(str6 == "CM3D2_MATERIAL", "ProcScriptBin 例外 : ヘッダーファイルが不正です。" + str6);
  339. binaryReader.ReadInt32();
  340. binaryReader.ReadString();
  341. r = binaryReader;
  342. string str7 = r.ReadString();
  343. string str8 = r.ReadString();
  344. string path2 = "DefMaterial/" + r.ReadString();
  345. var original2 = Resources.Load(path2, typeof(Material)) as Material;
  346. if (original2 == null)
  347. return original2;
  348. material = Instantiate(original2);
  349. material.name = str7;
  350. int hashCode2 = material.name.GetHashCode();
  351. if (m_hashPriorityMaterials != null && m_hashPriorityMaterials.ContainsKey(hashCode2))
  352. {
  353. var priorityMaterial = m_hashPriorityMaterials[hashCode2];
  354. if (priorityMaterial.Key == material.name)
  355. {
  356. material.SetFloat("_SetManualRenderQueue", priorityMaterial.Value);
  357. material.renderQueue = (int)priorityMaterial.Value;
  358. }
  359. }
  360. while (true)
  361. {
  362. string str9 = r.ReadString();
  363. if (!(str9 == "end"))
  364. {
  365. string name = r.ReadString();
  366. if (str9 == "tex")
  367. {
  368. string str10 = r.ReadString();
  369. if (str10 == "null")
  370. {
  371. material.SetTexture(name, null);
  372. }
  373. else if (str10 == "tex2d")
  374. {
  375. try
  376. {
  377. string str11 = r.ReadString();
  378. str3 = r.ReadString();
  379. var data = ImportCM.LoadTexture(GameUty.FileSystem, str11 + ".tex", false).data;
  380. var texture2D = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  381. texture2D.LoadImage(data);
  382. texture2D.name = str11;
  383. texture2D.wrapMode = TextureWrapMode.Repeat;
  384. material.SetTexture(name, texture2D);
  385. vector2_1.x = r.ReadSingle();
  386. vector2_1.y = r.ReadSingle();
  387. material.SetTextureOffset(name, vector2_1);
  388. vector2_2.x = r.ReadSingle();
  389. vector2_2.y = r.ReadSingle();
  390. material.SetTextureScale(name, vector2_2);
  391. }
  392. catch
  393. {
  394. break;
  395. }
  396. }
  397. else if (str10 == "texRT")
  398. {
  399. str4 = r.ReadString();
  400. str5 = r.ReadString();
  401. }
  402. }
  403. else if (str9 == "col")
  404. {
  405. color.r = r.ReadSingle();
  406. color.g = r.ReadSingle();
  407. color.b = r.ReadSingle();
  408. color.a = r.ReadSingle();
  409. material.SetColor(name, color);
  410. }
  411. else if (str9 == "vec")
  412. {
  413. vector4.x = r.ReadSingle();
  414. vector4.y = r.ReadSingle();
  415. vector4.z = r.ReadSingle();
  416. vector4.w = r.ReadSingle();
  417. material.SetVector(name, vector4);
  418. }
  419. else if (str9 == "f")
  420. {
  421. float num = r.ReadSingle();
  422. material.SetFloat(name, num);
  423. }
  424. }
  425. else
  426. {
  427. break;
  428. }
  429. }
  430. }
  431. return material;
  432. }
  433. }