ImportCM2.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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, string slotname, TBodySkin bodyskin, 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 = bodyskin.m_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, bodyskin, null, 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, TBodySkin bodyskin, Material existmat, string filename)
  197. {
  198. bodyskin = null;
  199. existmat = null;
  200. if (m_hashPriorityMaterials == null)
  201. {
  202. m_hashPriorityMaterials = new Dictionary<int, KeyValuePair<string, float>>();
  203. var list = GameUty.FileSystem.GetList("prioritymaterial", AFileSystemBase.ListType.AllFile);
  204. if (list != null && 0 < list.Length)
  205. for (int index = 0; index < list.Length; ++index)
  206. if (Path.GetExtension(list[index]) == ".pmat")
  207. {
  208. string fileName = list[index];
  209. using (AFileBase afileBase = GameUty.FileOpen(fileName, null))
  210. {
  211. NDebug.Assert(afileBase.IsValid(), fileName + "を開けませんでした");
  212. using (var binaryReader = new BinaryReader(new MemoryStream(afileBase.ReadAll()), Encoding.UTF8))
  213. {
  214. NDebug.Assert(binaryReader.ReadString() == "CM3D2_PMATERIAL", "ヘッダーエラー\n" + fileName);
  215. binaryReader.ReadInt32();
  216. int key1 = binaryReader.ReadInt32();
  217. string key2 = binaryReader.ReadString();
  218. float num = binaryReader.ReadSingle();
  219. NDebug.Assert(!m_hashPriorityMaterials.ContainsKey(key1), "すでにハッシュが登録されています");
  220. m_hashPriorityMaterials.Add(key1, new KeyValuePair<string, float>(key2, num));
  221. }
  222. }
  223. }
  224. }
  225. string str1 = r.ReadString();
  226. string str2 = r.ReadString();
  227. string path1 = "DefMaterial/" + r.ReadString();
  228. Material material;
  229. if (existmat == null)
  230. {
  231. var original = Resources.Load(path1, typeof(Material)) as Material;
  232. if (original == null)
  233. return original;
  234. material = Instantiate(original);
  235. }
  236. else
  237. {
  238. material = existmat;
  239. NDebug.Assert(material.shader.name == str2, "マテリアル入れ替えエラー。違うシェーダーに入れようとしました。 " + str2 + " -> " + material.shader.name);
  240. }
  241. material.name = str1;
  242. int hashCode1 = material.name.GetHashCode();
  243. if (m_hashPriorityMaterials != null && m_hashPriorityMaterials.ContainsKey(hashCode1))
  244. {
  245. var priorityMaterial = m_hashPriorityMaterials[hashCode1];
  246. if (priorityMaterial.Key == material.name)
  247. {
  248. material.SetFloat("_SetManualRenderQueue", priorityMaterial.Value);
  249. material.renderQueue = (int) priorityMaterial.Value;
  250. }
  251. }
  252. string str3;
  253. Vector2 vector2_1;
  254. Vector2 vector2_2;
  255. string str4;
  256. string str5;
  257. Color color;
  258. Vector4 vector4;
  259. bool flag;
  260. while (true)
  261. {
  262. flag = true;
  263. string str6 = r.ReadString();
  264. if (!(str6 == "end"))
  265. {
  266. string name = r.ReadString();
  267. if (str6 == "tex")
  268. {
  269. string str7 = r.ReadString();
  270. if (str7 == "null")
  271. {
  272. material.SetTexture(name, null);
  273. }
  274. else if (str7 == "tex2d")
  275. {
  276. try
  277. {
  278. string str8 = r.ReadString();
  279. str3 = r.ReadString();
  280. var data = ImportCM.LoadTexture(GameUty.FileSystem, str8 + ".tex", false).data;
  281. vector2_1.x = r.ReadSingle();
  282. vector2_1.y = r.ReadSingle();
  283. vector2_2.x = r.ReadSingle();
  284. vector2_2.y = r.ReadSingle();
  285. if (filename == null || !filename.Contains(".mate"))
  286. {
  287. var texture2D = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  288. texture2D.LoadImage(data);
  289. texture2D.name = str8;
  290. texture2D.wrapMode = TextureWrapMode.Clamp;
  291. material.SetTexture(name, texture2D);
  292. material.SetTextureOffset(name, vector2_1);
  293. material.SetTextureScale(name, vector2_2);
  294. }
  295. }
  296. catch
  297. {
  298. break;
  299. }
  300. }
  301. else if (str7 == "texRT")
  302. {
  303. str4 = r.ReadString();
  304. str5 = r.ReadString();
  305. }
  306. }
  307. else if (str6 == "col")
  308. {
  309. color.r = r.ReadSingle();
  310. color.g = r.ReadSingle();
  311. color.b = r.ReadSingle();
  312. color.a = r.ReadSingle();
  313. material.SetColor(name, color);
  314. }
  315. else if (str6 == "vec")
  316. {
  317. vector4.x = r.ReadSingle();
  318. vector4.y = r.ReadSingle();
  319. vector4.z = r.ReadSingle();
  320. vector4.w = r.ReadSingle();
  321. material.SetVector(name, vector4);
  322. }
  323. else if (str6 == "f")
  324. {
  325. float num = r.ReadSingle();
  326. material.SetFloat(name, num);
  327. }
  328. }
  329. else
  330. {
  331. break;
  332. }
  333. }
  334. if (filename != null && filename.Contains(".mate"))
  335. {
  336. try
  337. {
  338. using (AFileBase afileBase = GameUty.FileOpen(filename, null))
  339. {
  340. if (m_matTempFile == null)
  341. m_matTempFile = new byte[Math.Max(10000, afileBase.GetSize())];
  342. else if (m_matTempFile.Length < afileBase.GetSize())
  343. m_matTempFile = new byte[afileBase.GetSize()];
  344. afileBase.Read(ref m_matTempFile, afileBase.GetSize());
  345. }
  346. }
  347. catch { }
  348. var binaryReader = new BinaryReader(new MemoryStream(m_matTempFile), Encoding.UTF8);
  349. string str6 = binaryReader.ReadString();
  350. NDebug.Assert(str6 == "CM3D2_MATERIAL", "ProcScriptBin 例外 : ヘッダーファイルが不正です。" + str6);
  351. binaryReader.ReadInt32();
  352. binaryReader.ReadString();
  353. r = binaryReader;
  354. string str7 = r.ReadString();
  355. string str8 = r.ReadString();
  356. string path2 = "DefMaterial/" + r.ReadString();
  357. if (existmat == null)
  358. {
  359. var original = Resources.Load(path2, typeof(Material)) as Material;
  360. if (original == null)
  361. return original;
  362. material = Instantiate(original);
  363. }
  364. else
  365. {
  366. material = existmat;
  367. NDebug.Assert(material.shader.name == str8, "マテリアル入れ替えエラー。違うシェーダーに入れようとしました。 " + str8 + " -> " + material.shader.name);
  368. }
  369. material.name = str7;
  370. int hashCode2 = material.name.GetHashCode();
  371. if (m_hashPriorityMaterials != null && m_hashPriorityMaterials.ContainsKey(hashCode2))
  372. {
  373. var priorityMaterial = m_hashPriorityMaterials[hashCode2];
  374. if (priorityMaterial.Key == material.name)
  375. {
  376. material.SetFloat("_SetManualRenderQueue", priorityMaterial.Value);
  377. material.renderQueue = (int) priorityMaterial.Value;
  378. }
  379. }
  380. while (true)
  381. {
  382. flag = true;
  383. string str9 = r.ReadString();
  384. if (!(str9 == "end"))
  385. {
  386. string name = r.ReadString();
  387. if (str9 == "tex")
  388. {
  389. string str10 = r.ReadString();
  390. if (str10 == "null")
  391. {
  392. material.SetTexture(name, null);
  393. }
  394. else if (str10 == "tex2d")
  395. {
  396. try
  397. {
  398. string str11 = r.ReadString();
  399. str3 = r.ReadString();
  400. var data = ImportCM.LoadTexture(GameUty.FileSystem, str11 + ".tex", false).data;
  401. var texture2D = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  402. texture2D.LoadImage(data);
  403. texture2D.name = str11;
  404. texture2D.wrapMode = TextureWrapMode.Clamp;
  405. material.SetTexture(name, texture2D);
  406. vector2_1.x = r.ReadSingle();
  407. vector2_1.y = r.ReadSingle();
  408. material.SetTextureOffset(name, vector2_1);
  409. vector2_2.x = r.ReadSingle();
  410. vector2_2.y = r.ReadSingle();
  411. material.SetTextureScale(name, vector2_2);
  412. }
  413. catch
  414. {
  415. break;
  416. }
  417. }
  418. else if (str10 == "texRT")
  419. {
  420. str4 = r.ReadString();
  421. str5 = r.ReadString();
  422. }
  423. }
  424. else if (str9 == "col")
  425. {
  426. color.r = r.ReadSingle();
  427. color.g = r.ReadSingle();
  428. color.b = r.ReadSingle();
  429. color.a = r.ReadSingle();
  430. material.SetColor(name, color);
  431. }
  432. else if (str9 == "vec")
  433. {
  434. vector4.x = r.ReadSingle();
  435. vector4.y = r.ReadSingle();
  436. vector4.z = r.ReadSingle();
  437. vector4.w = r.ReadSingle();
  438. material.SetVector(name, vector4);
  439. }
  440. else if (str9 == "f")
  441. {
  442. float num = r.ReadSingle();
  443. material.SetFloat(name, num);
  444. }
  445. }
  446. else
  447. {
  448. break;
  449. }
  450. }
  451. }
  452. return material;
  453. }
  454. }