MMDPmxParser.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include <utility>
  34. #include "MMDPmxParser.h"
  35. #include <assimp/StringUtils.h>
  36. #include "utf8.h"
  37. #include <assimp/Exceptional.h>
  38. namespace pmx
  39. {
  40. int ReadIndex(std::istream *stream, int size)
  41. {
  42. switch (size)
  43. {
  44. case 1:
  45. uint8_t tmp8;
  46. stream->read((char*) &tmp8, sizeof(uint8_t));
  47. if (255 == tmp8)
  48. {
  49. return -1;
  50. }
  51. else {
  52. return (int) tmp8;
  53. }
  54. case 2:
  55. uint16_t tmp16;
  56. stream->read((char*) &tmp16, sizeof(uint16_t));
  57. if (65535 == tmp16)
  58. {
  59. return -1;
  60. }
  61. else {
  62. return (int) tmp16;
  63. }
  64. case 4:
  65. int tmp32;
  66. stream->read((char*) &tmp32, sizeof(int));
  67. return tmp32;
  68. default:
  69. return -1;
  70. }
  71. }
  72. std::string ReadString(std::istream *stream, uint8_t encoding)
  73. {
  74. int size;
  75. stream->read((char*) &size, sizeof(int));
  76. std::vector<char> buffer;
  77. if (size == 0)
  78. {
  79. return std::string();
  80. }
  81. buffer.resize(size);
  82. stream->read((char*) buffer.data(), size);
  83. if (encoding == 0)
  84. {
  85. // UTF16 to UTF8
  86. const uint16_t* sourceStart = (uint16_t*)buffer.data();
  87. const unsigned int targetSize = size * 3; // enough to encode
  88. char *targetStart = new char[targetSize];
  89. std::memset(targetStart, 0, targetSize * sizeof(char));
  90. utf8::utf16to8( sourceStart, sourceStart + size/2, targetStart );
  91. std::string result(targetStart);
  92. delete [] targetStart;
  93. return result;
  94. }
  95. else
  96. {
  97. // the name is already UTF8
  98. return std::string((const char*)buffer.data(), size);
  99. }
  100. }
  101. void PmxSetting::Read(std::istream *stream)
  102. {
  103. uint8_t count;
  104. stream->read((char*) &count, sizeof(uint8_t));
  105. if (count < 8)
  106. {
  107. throw DeadlyImportError("MMD: invalid size");
  108. }
  109. stream->read((char*) &encoding, sizeof(uint8_t));
  110. stream->read((char*) &uv, sizeof(uint8_t));
  111. stream->read((char*) &vertex_index_size, sizeof(uint8_t));
  112. stream->read((char*) &texture_index_size, sizeof(uint8_t));
  113. stream->read((char*) &material_index_size, sizeof(uint8_t));
  114. stream->read((char*) &bone_index_size, sizeof(uint8_t));
  115. stream->read((char*) &morph_index_size, sizeof(uint8_t));
  116. stream->read((char*) &rigidbody_index_size, sizeof(uint8_t));
  117. uint8_t temp;
  118. for (int i = 8; i < count; i++)
  119. {
  120. stream->read((char*)&temp, sizeof(uint8_t));
  121. }
  122. }
  123. void PmxVertexSkinningBDEF1::Read(std::istream *stream, PmxSetting *setting)
  124. {
  125. this->bone_index = ReadIndex(stream, setting->bone_index_size);
  126. }
  127. void PmxVertexSkinningBDEF2::Read(std::istream *stream, PmxSetting *setting)
  128. {
  129. this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
  130. this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
  131. stream->read((char*) &this->bone_weight, sizeof(float));
  132. }
  133. void PmxVertexSkinningBDEF4::Read(std::istream *stream, PmxSetting *setting)
  134. {
  135. this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
  136. this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
  137. this->bone_index3 = ReadIndex(stream, setting->bone_index_size);
  138. this->bone_index4 = ReadIndex(stream, setting->bone_index_size);
  139. stream->read((char*) &this->bone_weight1, sizeof(float));
  140. stream->read((char*) &this->bone_weight2, sizeof(float));
  141. stream->read((char*) &this->bone_weight3, sizeof(float));
  142. stream->read((char*) &this->bone_weight4, sizeof(float));
  143. }
  144. void PmxVertexSkinningSDEF::Read(std::istream *stream, PmxSetting *setting)
  145. {
  146. this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
  147. this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
  148. stream->read((char*) &this->bone_weight, sizeof(float));
  149. stream->read((char*) this->sdef_c, sizeof(float) * 3);
  150. stream->read((char*) this->sdef_r0, sizeof(float) * 3);
  151. stream->read((char*) this->sdef_r1, sizeof(float) * 3);
  152. }
  153. void PmxVertexSkinningQDEF::Read(std::istream *stream, PmxSetting *setting)
  154. {
  155. this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
  156. this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
  157. this->bone_index3 = ReadIndex(stream, setting->bone_index_size);
  158. this->bone_index4 = ReadIndex(stream, setting->bone_index_size);
  159. stream->read((char*) &this->bone_weight1, sizeof(float));
  160. stream->read((char*) &this->bone_weight2, sizeof(float));
  161. stream->read((char*) &this->bone_weight3, sizeof(float));
  162. stream->read((char*) &this->bone_weight4, sizeof(float));
  163. }
  164. void PmxVertex::Read(std::istream *stream, PmxSetting *setting)
  165. {
  166. stream->read((char*) this->position, sizeof(float) * 3);
  167. stream->read((char*) this->normal, sizeof(float) * 3);
  168. stream->read((char*) this->uv, sizeof(float) * 2);
  169. for (int i = 0; i < setting->uv; ++i)
  170. {
  171. stream->read((char*) this->uva[i], sizeof(float) * 4);
  172. }
  173. stream->read((char*) &this->skinning_type, sizeof(PmxVertexSkinningType));
  174. switch (this->skinning_type)
  175. {
  176. case PmxVertexSkinningType::BDEF1:
  177. this->skinning = mmd::make_unique<PmxVertexSkinningBDEF1>();
  178. break;
  179. case PmxVertexSkinningType::BDEF2:
  180. this->skinning = mmd::make_unique<PmxVertexSkinningBDEF2>();
  181. break;
  182. case PmxVertexSkinningType::BDEF4:
  183. this->skinning = mmd::make_unique<PmxVertexSkinningBDEF4>();
  184. break;
  185. case PmxVertexSkinningType::SDEF:
  186. this->skinning = mmd::make_unique<PmxVertexSkinningSDEF>();
  187. break;
  188. case PmxVertexSkinningType::QDEF:
  189. this->skinning = mmd::make_unique<PmxVertexSkinningQDEF>();
  190. break;
  191. default:
  192. throw "invalid skinning type";
  193. }
  194. this->skinning->Read(stream, setting);
  195. stream->read((char*) &this->edge, sizeof(float));
  196. }
  197. void PmxMaterial::Read(std::istream *stream, PmxSetting *setting)
  198. {
  199. this->material_name = ReadString(stream, setting->encoding);
  200. this->material_english_name = ReadString(stream, setting->encoding);
  201. stream->read((char*) this->diffuse, sizeof(float) * 4);
  202. stream->read((char*) this->specular, sizeof(float) * 3);
  203. stream->read((char*) &this->specularlity, sizeof(float));
  204. stream->read((char*) this->ambient, sizeof(float) * 3);
  205. stream->read((char*) &this->flag, sizeof(uint8_t));
  206. stream->read((char*) this->edge_color, sizeof(float) * 4);
  207. stream->read((char*) &this->edge_size, sizeof(float));
  208. this->diffuse_texture_index = ReadIndex(stream, setting->texture_index_size);
  209. this->sphere_texture_index = ReadIndex(stream, setting->texture_index_size);
  210. stream->read((char*) &this->sphere_op_mode, sizeof(uint8_t));
  211. stream->read((char*) &this->common_toon_flag, sizeof(uint8_t));
  212. if (this->common_toon_flag)
  213. {
  214. stream->read((char*) &this->toon_texture_index, sizeof(uint8_t));
  215. }
  216. else {
  217. this->toon_texture_index = ReadIndex(stream, setting->texture_index_size);
  218. }
  219. this->memo = ReadString(stream, setting->encoding);
  220. stream->read((char*) &this->index_count, sizeof(int));
  221. }
  222. void PmxIkLink::Read(std::istream *stream, PmxSetting *setting)
  223. {
  224. this->link_target = ReadIndex(stream, setting->bone_index_size);
  225. stream->read((char*) &this->angle_lock, sizeof(uint8_t));
  226. if (angle_lock == 1)
  227. {
  228. stream->read((char*) this->max_radian, sizeof(float) * 3);
  229. stream->read((char*) this->min_radian, sizeof(float) * 3);
  230. }
  231. }
  232. void PmxBone::Read(std::istream *stream, PmxSetting *setting)
  233. {
  234. this->bone_name = ReadString(stream, setting->encoding);
  235. this->bone_english_name = ReadString(stream, setting->encoding);
  236. stream->read((char*) this->position, sizeof(float) * 3);
  237. this->parent_index = ReadIndex(stream, setting->bone_index_size);
  238. stream->read((char*) &this->level, sizeof(int));
  239. stream->read((char*) &this->bone_flag, sizeof(uint16_t));
  240. if (this->bone_flag & 0x0001) {
  241. this->target_index = ReadIndex(stream, setting->bone_index_size);
  242. }
  243. else {
  244. stream->read((char*)this->offset, sizeof(float) * 3);
  245. }
  246. if (this->bone_flag & (0x0100 | 0x0200)) {
  247. this->grant_parent_index = ReadIndex(stream, setting->bone_index_size);
  248. stream->read((char*) &this->grant_weight, sizeof(float));
  249. }
  250. if (this->bone_flag & 0x0400) {
  251. stream->read((char*)this->lock_axis_orientation, sizeof(float) * 3);
  252. }
  253. if (this->bone_flag & 0x0800) {
  254. stream->read((char*)this->local_axis_x_orientation, sizeof(float) * 3);
  255. stream->read((char*)this->local_axis_y_orientation, sizeof(float) * 3);
  256. }
  257. if (this->bone_flag & 0x2000) {
  258. stream->read((char*) &this->key, sizeof(int));
  259. }
  260. if (this->bone_flag & 0x0020) {
  261. this->ik_target_bone_index = ReadIndex(stream, setting->bone_index_size);
  262. stream->read((char*) &ik_loop, sizeof(int));
  263. stream->read((char*) &ik_loop_angle_limit, sizeof(float));
  264. stream->read((char*) &ik_link_count, sizeof(int));
  265. this->ik_links = mmd::make_unique<PmxIkLink []>(ik_link_count);
  266. for (int i = 0; i < ik_link_count; i++) {
  267. ik_links[i].Read(stream, setting);
  268. }
  269. }
  270. }
  271. void PmxMorphVertexOffset::Read(std::istream *stream, PmxSetting *setting)
  272. {
  273. this->vertex_index = ReadIndex(stream, setting->vertex_index_size);
  274. stream->read((char*)this->position_offset, sizeof(float) * 3);
  275. }
  276. void PmxMorphUVOffset::Read(std::istream *stream, PmxSetting *setting)
  277. {
  278. this->vertex_index = ReadIndex(stream, setting->vertex_index_size);
  279. stream->read((char*)this->uv_offset, sizeof(float) * 4);
  280. }
  281. void PmxMorphBoneOffset::Read(std::istream *stream, PmxSetting *setting)
  282. {
  283. this->bone_index = ReadIndex(stream, setting->bone_index_size);
  284. stream->read((char*)this->translation, sizeof(float) * 3);
  285. stream->read((char*)this->rotation, sizeof(float) * 4);
  286. }
  287. void PmxMorphMaterialOffset::Read(std::istream *stream, PmxSetting *setting)
  288. {
  289. this->material_index = ReadIndex(stream, setting->material_index_size);
  290. stream->read((char*) &this->offset_operation, sizeof(uint8_t));
  291. stream->read((char*)this->diffuse, sizeof(float) * 4);
  292. stream->read((char*)this->specular, sizeof(float) * 3);
  293. stream->read((char*) &this->specularity, sizeof(float));
  294. stream->read((char*)this->ambient, sizeof(float) * 3);
  295. stream->read((char*)this->edge_color, sizeof(float) * 4);
  296. stream->read((char*) &this->edge_size, sizeof(float));
  297. stream->read((char*)this->texture_argb, sizeof(float) * 4);
  298. stream->read((char*)this->sphere_texture_argb, sizeof(float) * 4);
  299. stream->read((char*)this->toon_texture_argb, sizeof(float) * 4);
  300. }
  301. void PmxMorphGroupOffset::Read(std::istream *stream, PmxSetting *setting)
  302. {
  303. this->morph_index = ReadIndex(stream, setting->morph_index_size);
  304. stream->read((char*) &this->morph_weight, sizeof(float));
  305. }
  306. void PmxMorphFlipOffset::Read(std::istream *stream, PmxSetting *setting)
  307. {
  308. this->morph_index = ReadIndex(stream, setting->morph_index_size);
  309. stream->read((char*) &this->morph_value, sizeof(float));
  310. }
  311. void PmxMorphImplusOffset::Read(std::istream *stream, PmxSetting *setting)
  312. {
  313. this->rigid_body_index = ReadIndex(stream, setting->rigidbody_index_size);
  314. stream->read((char*) &this->is_local, sizeof(uint8_t));
  315. stream->read((char*)this->velocity, sizeof(float) * 3);
  316. stream->read((char*)this->angular_torque, sizeof(float) * 3);
  317. }
  318. void PmxMorph::Read(std::istream *stream, PmxSetting *setting)
  319. {
  320. this->morph_name = ReadString(stream, setting->encoding);
  321. this->morph_english_name = ReadString(stream, setting->encoding);
  322. stream->read((char*) &category, sizeof(MorphCategory));
  323. stream->read((char*) &morph_type, sizeof(MorphType));
  324. stream->read((char*) &this->offset_count, sizeof(int));
  325. switch (this->morph_type)
  326. {
  327. case MorphType::Group:
  328. group_offsets = mmd::make_unique<PmxMorphGroupOffset []>(this->offset_count);
  329. for (int i = 0; i < offset_count; i++)
  330. {
  331. group_offsets[i].Read(stream, setting);
  332. }
  333. break;
  334. case MorphType::Vertex:
  335. vertex_offsets = mmd::make_unique<PmxMorphVertexOffset []>(this->offset_count);
  336. for (int i = 0; i < offset_count; i++)
  337. {
  338. vertex_offsets[i].Read(stream, setting);
  339. }
  340. break;
  341. case MorphType::Bone:
  342. bone_offsets = mmd::make_unique<PmxMorphBoneOffset []>(this->offset_count);
  343. for (int i = 0; i < offset_count; i++)
  344. {
  345. bone_offsets[i].Read(stream, setting);
  346. }
  347. break;
  348. case MorphType::Matrial:
  349. material_offsets = mmd::make_unique<PmxMorphMaterialOffset []>(this->offset_count);
  350. for (int i = 0; i < offset_count; i++)
  351. {
  352. material_offsets[i].Read(stream, setting);
  353. }
  354. break;
  355. case MorphType::UV:
  356. case MorphType::AdditionalUV1:
  357. case MorphType::AdditionalUV2:
  358. case MorphType::AdditionalUV3:
  359. case MorphType::AdditionalUV4:
  360. uv_offsets = mmd::make_unique<PmxMorphUVOffset []>(this->offset_count);
  361. for (int i = 0; i < offset_count; i++)
  362. {
  363. uv_offsets[i].Read(stream, setting);
  364. }
  365. break;
  366. default:
  367. throw DeadlyImportError("MMD: unknown morth type");
  368. }
  369. }
  370. void PmxFrameElement::Read(std::istream *stream, PmxSetting *setting)
  371. {
  372. stream->read((char*) &this->element_target, sizeof(uint8_t));
  373. if (this->element_target == 0x00)
  374. {
  375. this->index = ReadIndex(stream, setting->bone_index_size);
  376. }
  377. else {
  378. this->index = ReadIndex(stream, setting->morph_index_size);
  379. }
  380. }
  381. void PmxFrame::Read(std::istream *stream, PmxSetting *setting)
  382. {
  383. this->frame_name = ReadString(stream, setting->encoding);
  384. this->frame_english_name = ReadString(stream, setting->encoding);
  385. stream->read((char*) &this->frame_flag, sizeof(uint8_t));
  386. stream->read((char*) &this->element_count, sizeof(int));
  387. this->elements = mmd::make_unique<PmxFrameElement []>(this->element_count);
  388. for (int i = 0; i < this->element_count; i++)
  389. {
  390. this->elements[i].Read(stream, setting);
  391. }
  392. }
  393. void PmxRigidBody::Read(std::istream *stream, PmxSetting *setting)
  394. {
  395. this->girid_body_name = ReadString(stream, setting->encoding);
  396. this->girid_body_english_name = ReadString(stream, setting->encoding);
  397. this->target_bone = ReadIndex(stream, setting->bone_index_size);
  398. stream->read((char*) &this->group, sizeof(uint8_t));
  399. stream->read((char*) &this->mask, sizeof(uint16_t));
  400. stream->read((char*) &this->shape, sizeof(uint8_t));
  401. stream->read((char*) this->size, sizeof(float) * 3);
  402. stream->read((char*) this->position, sizeof(float) * 3);
  403. stream->read((char*) this->orientation, sizeof(float) * 3);
  404. stream->read((char*) &this->mass, sizeof(float));
  405. stream->read((char*) &this->move_attenuation, sizeof(float));
  406. stream->read((char*) &this->rotation_attenuation, sizeof(float));
  407. stream->read((char*) &this->repulsion, sizeof(float));
  408. stream->read((char*) &this->friction, sizeof(float));
  409. stream->read((char*) &this->physics_calc_type, sizeof(uint8_t));
  410. }
  411. void PmxJointParam::Read(std::istream *stream, PmxSetting *setting)
  412. {
  413. this->rigid_body1 = ReadIndex(stream, setting->rigidbody_index_size);
  414. this->rigid_body2 = ReadIndex(stream, setting->rigidbody_index_size);
  415. stream->read((char*) this->position, sizeof(float) * 3);
  416. stream->read((char*) this->orientaiton, sizeof(float) * 3);
  417. stream->read((char*) this->move_limitation_min, sizeof(float) * 3);
  418. stream->read((char*) this->move_limitation_max, sizeof(float) * 3);
  419. stream->read((char*) this->rotation_limitation_min, sizeof(float) * 3);
  420. stream->read((char*) this->rotation_limitation_max, sizeof(float) * 3);
  421. stream->read((char*) this->spring_move_coefficient, sizeof(float) * 3);
  422. stream->read((char*) this->spring_rotation_coefficient, sizeof(float) * 3);
  423. }
  424. void PmxJoint::Read(std::istream *stream, PmxSetting *setting)
  425. {
  426. this->joint_name = ReadString(stream, setting->encoding);
  427. this->joint_english_name = ReadString(stream, setting->encoding);
  428. stream->read((char*) &this->joint_type, sizeof(uint8_t));
  429. this->param.Read(stream, setting);
  430. }
  431. void PmxAncherRigidBody::Read(std::istream *stream, PmxSetting *setting)
  432. {
  433. this->related_rigid_body = ReadIndex(stream, setting->rigidbody_index_size);
  434. this->related_vertex = ReadIndex(stream, setting->vertex_index_size);
  435. stream->read((char*) &this->is_near, sizeof(uint8_t));
  436. }
  437. void PmxSoftBody::Read(std::istream * /*stream*/, PmxSetting * /*setting*/)
  438. {
  439. throw DeadlyImportError("MMD: Soft Body support is not implemented.");
  440. }
  441. void PmxModel::Init()
  442. {
  443. this->version = 0.0f;
  444. this->model_name.clear();
  445. this->model_english_name.clear();
  446. this->model_comment.clear();
  447. this->model_english_comment.clear();
  448. this->vertex_count = 0;
  449. this->vertices = nullptr;
  450. this->index_count = 0;
  451. this->indices = nullptr;
  452. this->texture_count = 0;
  453. this->textures = nullptr;
  454. this->material_count = 0;
  455. this->materials = nullptr;
  456. this->bone_count = 0;
  457. this->bones = nullptr;
  458. this->morph_count = 0;
  459. this->morphs = nullptr;
  460. this->frame_count = 0;
  461. this->frames = nullptr;
  462. this->rigid_body_count = 0;
  463. this->rigid_bodies = nullptr;
  464. this->joint_count = 0;
  465. this->joints = nullptr;
  466. this->soft_body_count = 0;
  467. this->soft_bodies = nullptr;
  468. }
  469. void PmxModel::Read(std::istream *stream)
  470. {
  471. char magic[4];
  472. stream->read((char*) magic, sizeof(char) * 4);
  473. if (magic[0] != 0x50 || magic[1] != 0x4d || magic[2] != 0x58 || magic[3] != 0x20)
  474. {
  475. throw DeadlyImportError("MMD: Invalid magic number.");
  476. }
  477. stream->read((char*) &version, sizeof(float));
  478. if (version != 2.0f && version != 2.1f)
  479. {
  480. throw DeadlyImportError("MMD: Unsupported version (must be 2.0 or 2.1): ", ai_to_string(version));
  481. }
  482. this->setting.Read(stream);
  483. this->model_name = ReadString(stream, setting.encoding);
  484. this->model_english_name = ReadString(stream, setting.encoding);
  485. this->model_comment = ReadString(stream, setting.encoding);
  486. this->model_english_comment = ReadString(stream, setting.encoding);
  487. // read vertices
  488. stream->read((char*) &vertex_count, sizeof(int));
  489. this->vertices = mmd::make_unique<PmxVertex []>(vertex_count);
  490. for (int i = 0; i < vertex_count; i++)
  491. {
  492. vertices[i].Read(stream, &setting);
  493. }
  494. // read indices
  495. stream->read((char*) &index_count, sizeof(int));
  496. this->indices = mmd::make_unique<int []>(index_count);
  497. for (int i = 0; i < index_count; i++)
  498. {
  499. this->indices[i] = ReadIndex(stream, setting.vertex_index_size);
  500. }
  501. // read texture names
  502. stream->read((char*) &texture_count, sizeof(int));
  503. this->textures = mmd::make_unique<std::string []>(texture_count);
  504. for (int i = 0; i < texture_count; i++)
  505. {
  506. this->textures[i] = ReadString(stream, setting.encoding);
  507. }
  508. // read materials
  509. stream->read((char*) &material_count, sizeof(int));
  510. this->materials = mmd::make_unique<PmxMaterial []>(material_count);
  511. for (int i = 0; i < material_count; i++)
  512. {
  513. this->materials[i].Read(stream, &setting);
  514. }
  515. // read bones
  516. stream->read((char*) &this->bone_count, sizeof(int));
  517. this->bones = mmd::make_unique<PmxBone []>(this->bone_count);
  518. for (int i = 0; i < this->bone_count; i++)
  519. {
  520. this->bones[i].Read(stream, &setting);
  521. }
  522. // read morphs
  523. stream->read((char*) &this->morph_count, sizeof(int));
  524. this->morphs = mmd::make_unique<PmxMorph []>(this->morph_count);
  525. for (int i = 0; i < this->morph_count; i++)
  526. {
  527. this->morphs[i].Read(stream, &setting);
  528. }
  529. // read display frames
  530. stream->read((char*) &this->frame_count, sizeof(int));
  531. this->frames = mmd::make_unique<PmxFrame []>(this->frame_count);
  532. for (int i = 0; i < this->frame_count; i++)
  533. {
  534. this->frames[i].Read(stream, &setting);
  535. }
  536. // read rigid bodies
  537. stream->read((char*) &this->rigid_body_count, sizeof(int));
  538. this->rigid_bodies = mmd::make_unique<PmxRigidBody []>(this->rigid_body_count);
  539. for (int i = 0; i < this->rigid_body_count; i++)
  540. {
  541. this->rigid_bodies[i].Read(stream, &setting);
  542. }
  543. // read joints
  544. stream->read((char*) &this->joint_count, sizeof(int));
  545. this->joints = mmd::make_unique<PmxJoint []>(this->joint_count);
  546. for (int i = 0; i < this->joint_count; i++)
  547. {
  548. this->joints[i].Read(stream, &setting);
  549. }
  550. }
  551. }