MMDPmdParser.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. #pragma once
  34. #include <vector>
  35. #include <string>
  36. #include <memory>
  37. #include <iostream>
  38. #include <fstream>
  39. #include "MMDCpp14.h"
  40. namespace pmd {
  41. struct PmdHeader {
  42. std::string name;
  43. std::string name_english;
  44. std::string comment;
  45. std::string comment_english;
  46. PmdHeader() = default;
  47. ~PmdHeader() = default;
  48. bool Read(std::ifstream *stream) {
  49. if (stream == nullptr) {
  50. return false;
  51. }
  52. char buffer[256] = {};
  53. stream->read(buffer, 20);
  54. name = std::string(buffer);
  55. stream->read(buffer, 256);
  56. comment = std::string(buffer);
  57. return true;
  58. }
  59. bool ReadExtension(std::ifstream *stream) {
  60. if (stream == nullptr) {
  61. return false;
  62. }
  63. char buffer[256] = {};
  64. stream->read(buffer, 20);
  65. name_english = std::string(buffer);
  66. stream->read(buffer, 256);
  67. comment_english = std::string(buffer);
  68. return true;
  69. }
  70. };
  71. struct PmdVertex {
  72. float position[3];
  73. float normal[3];
  74. float uv[2];
  75. uint16_t bone_index[2];
  76. uint8_t bone_weight;
  77. bool edge_invisible;
  78. PmdVertex() :
  79. position{ 0.0f }, normal{ 0.0f }, uv{ 0.0f }, bone_index{ 0 }, bone_weight(0), edge_invisible(false) {}
  80. ~PmdVertex() = default;
  81. bool Read(std::ifstream *stream) {
  82. if (stream == nullptr) {
  83. return false;
  84. }
  85. stream->read((char *)position, sizeof(float) * 3);
  86. stream->read((char *)normal, sizeof(float) * 3);
  87. stream->read((char *)uv, sizeof(float) * 2);
  88. stream->read((char *)bone_index, sizeof(uint16_t) * 2);
  89. stream->read((char *)&bone_weight, sizeof(uint8_t));
  90. stream->read((char *)&edge_invisible, sizeof(uint8_t));
  91. return true;
  92. }
  93. };
  94. struct PmdMaterial {
  95. float diffuse[4];
  96. float power;
  97. float specular[3];
  98. float ambient[3];
  99. uint8_t toon_index;
  100. uint8_t edge_flag;
  101. uint32_t index_count;
  102. std::string texture_filename;
  103. std::string sphere_filename;
  104. PmdMaterial() :
  105. diffuse{ 0.0f }, power(0.0f), specular{ 0.0f }, ambient{ 0.0f }, toon_index(0), edge_flag(0), index_count(0), texture_filename(), sphere_filename() {}
  106. ~PmdMaterial() = default;
  107. bool Read(std::ifstream *stream) {
  108. if (stream == nullptr) {
  109. return false;
  110. }
  111. constexpr size_t BufferSize = 20;
  112. char buffer[BufferSize] = {};
  113. stream->read((char *)&diffuse, sizeof(float) * 4);
  114. stream->read((char *)&power, sizeof(float));
  115. stream->read((char *)&specular, sizeof(float) * 3);
  116. stream->read((char *)&ambient, sizeof(float) * 3);
  117. stream->read((char *)&toon_index, sizeof(uint8_t));
  118. stream->read((char *)&edge_flag, sizeof(uint8_t));
  119. stream->read((char *)&index_count, sizeof(uint32_t));
  120. stream->read((char *)&buffer, sizeof(char) * BufferSize);
  121. char *pstar = strchr(buffer, '*');
  122. if (nullptr == pstar) {
  123. texture_filename = std::string(buffer);
  124. sphere_filename.clear();
  125. } else {
  126. *pstar = 0;
  127. texture_filename = std::string(buffer);
  128. sphere_filename = std::string(pstar + 1);
  129. }
  130. return true;
  131. }
  132. };
  133. enum class BoneType : uint8_t {
  134. Rotation,
  135. RotationAndMove,
  136. IkEffector,
  137. Unknown,
  138. IkEffectable,
  139. RotationEffectable,
  140. IkTarget,
  141. Invisible,
  142. Twist,
  143. RotationMovement
  144. };
  145. struct PmdBone {
  146. std::string name;
  147. std::string name_english;
  148. uint16_t parent_bone_index;
  149. uint16_t tail_pos_bone_index;
  150. BoneType bone_type;
  151. uint16_t ik_parent_bone_index;
  152. float bone_head_pos[3];
  153. PmdBone() :
  154. name(), name_english(), parent_bone_index(0), tail_pos_bone_index(0), bone_type(BoneType::Unknown), ik_parent_bone_index(0), bone_head_pos{ 0.0f } {}
  155. ~PmdBone() = default;
  156. void Read(std::istream *stream) {
  157. if (stream == nullptr) {
  158. return;
  159. }
  160. constexpr size_t BufferSize = 20;
  161. char buffer[BufferSize] = {};
  162. stream->read(buffer, BufferSize);
  163. name = std::string(buffer);
  164. stream->read((char *)&parent_bone_index, sizeof(uint16_t));
  165. stream->read((char *)&tail_pos_bone_index, sizeof(uint16_t));
  166. stream->read((char *)&bone_type, sizeof(uint8_t));
  167. stream->read((char *)&ik_parent_bone_index, sizeof(uint16_t));
  168. stream->read((char *)&bone_head_pos, sizeof(float) * 3);
  169. }
  170. void ReadExpantion(std::istream *stream) {
  171. if (stream == nullptr) {
  172. return;
  173. }
  174. constexpr size_t BufferSize = 20;
  175. char buffer[BufferSize] = {};
  176. stream->read(buffer, BufferSize);
  177. name_english = std::string(buffer);
  178. }
  179. };
  180. struct PmdIk {
  181. uint16_t ik_bone_index;
  182. uint16_t target_bone_index;
  183. uint16_t iterations;
  184. float angle_limit;
  185. std::vector<uint16_t> ik_child_bone_index;
  186. PmdIk() : ik_bone_index(0), target_bone_index(0), iterations(0), angle_limit(0.0f) {}
  187. ~PmdIk() = default;
  188. void Read(std::istream *stream) {
  189. if (stream == nullptr) {
  190. return;
  191. }
  192. stream->read((char *)&ik_bone_index, sizeof(uint16_t));
  193. stream->read((char *)&target_bone_index, sizeof(uint16_t));
  194. uint8_t ik_chain_length;
  195. stream->read((char *)&ik_chain_length, sizeof(uint8_t));
  196. stream->read((char *)&iterations, sizeof(uint16_t));
  197. stream->read((char *)&angle_limit, sizeof(float));
  198. ik_child_bone_index.resize(ik_chain_length);
  199. for (int i = 0; i < ik_chain_length; i++) {
  200. stream->read((char *)&ik_child_bone_index[i], sizeof(uint16_t));
  201. }
  202. }
  203. };
  204. struct PmdFaceVertex {
  205. int vertex_index;
  206. float position[3];
  207. PmdFaceVertex() :
  208. vertex_index(0), position{ 0.0f } {}
  209. ~PmdFaceVertex() = default;
  210. void Read(std::istream *stream) {
  211. if (stream == nullptr) {
  212. return;
  213. }
  214. stream->read((char *)&vertex_index, sizeof(int));
  215. stream->read((char *)position, sizeof(float) * 3);
  216. }
  217. };
  218. enum class FaceCategory : uint8_t {
  219. Base,
  220. Eyebrow,
  221. Eye,
  222. Mouth,
  223. Other
  224. };
  225. struct PmdFace {
  226. std::string name;
  227. FaceCategory type;
  228. std::vector<PmdFaceVertex> vertices;
  229. std::string name_english;
  230. PmdFace() :
  231. name(), type(FaceCategory::Other), vertices(), name_english() {}
  232. ~PmdFace() = default;
  233. void Read(std::istream *stream) {
  234. if (stream == nullptr) {
  235. return;
  236. }
  237. constexpr size_t BufferSize = 20;
  238. char buffer[BufferSize];
  239. stream->read(buffer, BufferSize);
  240. name = std::string(buffer);
  241. int vertex_count;
  242. stream->read((char *)&vertex_count, sizeof(int));
  243. stream->read((char *)&type, sizeof(uint8_t));
  244. vertices.resize(vertex_count);
  245. for (int i = 0; i < vertex_count; i++) {
  246. vertices[i].Read(stream);
  247. }
  248. }
  249. void ReadExpantion(std::istream *stream) {
  250. if (stream == nullptr) {
  251. return;
  252. }
  253. char buffer[20];
  254. stream->read(buffer, 20);
  255. name_english = std::string(buffer);
  256. }
  257. };
  258. struct PmdBoneDispName {
  259. std::string bone_disp_name;
  260. std::string bone_disp_name_english;
  261. PmdBoneDispName() = default;
  262. ~PmdBoneDispName() = default;
  263. void Read(std::istream *stream) {
  264. if (stream == nullptr) {
  265. return;
  266. }
  267. char buffer[50];
  268. stream->read(buffer, 50);
  269. bone_disp_name = std::string(buffer);
  270. bone_disp_name_english.clear();
  271. }
  272. void ReadExpantion(std::istream *stream) {
  273. if (stream == nullptr) {
  274. return;
  275. }
  276. char buffer[50];
  277. stream->read(buffer, 50);
  278. bone_disp_name_english = std::string(buffer);
  279. }
  280. };
  281. struct PmdBoneDisp {
  282. uint16_t bone_index;
  283. uint8_t bone_disp_index;
  284. PmdBoneDisp() :
  285. bone_index(0), bone_disp_index(0) {}
  286. ~PmdBoneDisp() = default;
  287. void Read(std::istream *stream) {
  288. if (stream == nullptr) {
  289. return;
  290. }
  291. stream->read((char *)&bone_index, sizeof(uint16_t));
  292. stream->read((char *)&bone_disp_index, sizeof(uint8_t));
  293. }
  294. };
  295. enum class RigidBodyShape : uint8_t {
  296. Sphere = 0,
  297. Box = 1,
  298. Cpusel = 2
  299. };
  300. enum class RigidBodyType : uint8_t {
  301. BoneConnected = 0,
  302. Physics = 1,
  303. ConnectedPhysics = 2
  304. };
  305. struct PmdRigidBody {
  306. std::string name;
  307. uint16_t related_bone_index;
  308. uint8_t group_index;
  309. uint16_t mask;
  310. RigidBodyShape shape;
  311. float size[3];
  312. float position[3];
  313. float orientation[3];
  314. float weight;
  315. float linear_damping;
  316. float anglar_damping;
  317. float restitution;
  318. float friction;
  319. RigidBodyType rigid_type;
  320. PmdRigidBody() :
  321. name(), related_bone_index(0), group_index(0), mask(0), shape(RigidBodyShape::Box), size{ 0.0f }, position{ 0.0f }, weight(0.0f), linear_damping(0.0f), anglar_damping(0.0f), restitution(0.0f), friction(0.0f), rigid_type(RigidBodyType::BoneConnected) {}
  322. ~PmdRigidBody() = default;
  323. void Read(std::istream *stream) {
  324. if (stream == nullptr) {
  325. return;
  326. }
  327. char buffer[20];
  328. stream->read(buffer, sizeof(char) * 20);
  329. name = (std::string(buffer));
  330. stream->read((char *)&related_bone_index, sizeof(uint16_t));
  331. stream->read((char *)&group_index, sizeof(uint8_t));
  332. stream->read((char *)&mask, sizeof(uint16_t));
  333. stream->read((char *)&shape, sizeof(uint8_t));
  334. stream->read((char *)size, sizeof(float) * 3);
  335. stream->read((char *)position, sizeof(float) * 3);
  336. stream->read((char *)orientation, sizeof(float) * 3);
  337. stream->read((char *)&weight, sizeof(float));
  338. stream->read((char *)&linear_damping, sizeof(float));
  339. stream->read((char *)&anglar_damping, sizeof(float));
  340. stream->read((char *)&restitution, sizeof(float));
  341. stream->read((char *)&friction, sizeof(float));
  342. stream->read((char *)&rigid_type, sizeof(char));
  343. }
  344. };
  345. struct PmdConstraint {
  346. std::string name;
  347. uint32_t rigid_body_index_a;
  348. uint32_t rigid_body_index_b;
  349. float position[3];
  350. float orientation[3];
  351. float linear_lower_limit[3];
  352. float linear_upper_limit[3];
  353. float angular_lower_limit[3];
  354. float angular_upper_limit[3];
  355. float linear_stiffness[3];
  356. float angular_stiffness[3];
  357. PmdConstraint() :
  358. name(), rigid_body_index_a(0), rigid_body_index_b(0), position{ 0.0f }, orientation{ 0.0f }, linear_lower_limit{ 0.0f }, linear_upper_limit{ 0.0f }, angular_lower_limit{ 0.0f }, angular_upper_limit{ 0.0f }, linear_stiffness{ 0.0f }, angular_stiffness{ 0.0f } {}
  359. ~PmdConstraint() = default;
  360. void Read(std::istream *stream) {
  361. if (stream == nullptr) {
  362. return;
  363. }
  364. char buffer[20];
  365. stream->read(buffer, 20);
  366. name = std::string(buffer);
  367. stream->read((char *)&rigid_body_index_a, sizeof(uint32_t));
  368. stream->read((char *)&rigid_body_index_b, sizeof(uint32_t));
  369. stream->read((char *)position, sizeof(float) * 3);
  370. stream->read((char *)orientation, sizeof(float) * 3);
  371. stream->read((char *)linear_lower_limit, sizeof(float) * 3);
  372. stream->read((char *)linear_upper_limit, sizeof(float) * 3);
  373. stream->read((char *)angular_lower_limit, sizeof(float) * 3);
  374. stream->read((char *)angular_upper_limit, sizeof(float) * 3);
  375. stream->read((char *)linear_stiffness, sizeof(float) * 3);
  376. stream->read((char *)angular_stiffness, sizeof(float) * 3);
  377. }
  378. };
  379. struct PmdModel {
  380. float version;
  381. PmdHeader header;
  382. std::vector<PmdVertex> vertices;
  383. std::vector<uint16_t> indices;
  384. std::vector<PmdMaterial> materials;
  385. std::vector<PmdBone> bones;
  386. std::vector<PmdIk> iks;
  387. std::vector<PmdFace> faces;
  388. std::vector<uint16_t> faces_indices;
  389. std::vector<PmdBoneDispName> bone_disp_name;
  390. std::vector<PmdBoneDisp> bone_disp;
  391. std::vector<std::string> toon_filenames;
  392. std::vector<PmdRigidBody> rigid_bodies;
  393. std::vector<PmdConstraint> constraints;
  394. PmdModel() :
  395. version(0.0f) {}
  396. ~PmdModel() = default;
  397. static std::unique_ptr<PmdModel> LoadFromFile(const char *filename) {
  398. if (filename == nullptr) {
  399. return nullptr;
  400. }
  401. std::ifstream stream(filename, std::ios::binary);
  402. if (stream.fail()) {
  403. std::cerr << "could not open \"" << filename << "\"" << std::endl;
  404. return nullptr;
  405. }
  406. auto result = LoadFromStream(&stream);
  407. stream.close();
  408. return result;
  409. }
  410. static std::unique_ptr<PmdModel> LoadFromStream(std::ifstream *stream) {
  411. auto result = mmd::make_unique<PmdModel>();
  412. char buffer[100];
  413. // magic
  414. char magic[3];
  415. stream->read(magic, 3);
  416. if (magic[0] != 'P' || magic[1] != 'm' || magic[2] != 'd') {
  417. std::cerr << "invalid file" << std::endl;
  418. return nullptr;
  419. }
  420. // version
  421. stream->read((char *)&(result->version), sizeof(float));
  422. if (result->version != 1.0f) {
  423. std::cerr << "invalid version" << std::endl;
  424. return nullptr;
  425. }
  426. // header
  427. result->header.Read(stream);
  428. // vertices
  429. uint32_t vertex_num;
  430. stream->read((char *)&vertex_num, sizeof(uint32_t));
  431. result->vertices.resize(vertex_num);
  432. for (uint32_t i = 0; i < vertex_num; i++) {
  433. result->vertices[i].Read(stream);
  434. }
  435. // indices
  436. uint32_t index_num;
  437. stream->read((char *)&index_num, sizeof(uint32_t));
  438. result->indices.resize(index_num);
  439. for (uint32_t i = 0; i < index_num; i++) {
  440. stream->read((char *)&result->indices[i], sizeof(uint16_t));
  441. }
  442. // materials
  443. uint32_t material_num;
  444. stream->read((char *)&material_num, sizeof(uint32_t));
  445. result->materials.resize(material_num);
  446. for (uint32_t i = 0; i < material_num; i++) {
  447. result->materials[i].Read(stream);
  448. }
  449. // bones
  450. uint16_t bone_num;
  451. stream->read((char *)&bone_num, sizeof(uint16_t));
  452. result->bones.resize(bone_num);
  453. for (uint32_t i = 0; i < bone_num; i++) {
  454. result->bones[i].Read(stream);
  455. }
  456. // iks
  457. uint16_t ik_num;
  458. stream->read((char *)&ik_num, sizeof(uint16_t));
  459. result->iks.resize(ik_num);
  460. for (uint32_t i = 0; i < ik_num; i++) {
  461. result->iks[i].Read(stream);
  462. }
  463. // faces
  464. uint16_t face_num;
  465. stream->read((char *)&face_num, sizeof(uint16_t));
  466. result->faces.resize(face_num);
  467. for (uint32_t i = 0; i < face_num; i++) {
  468. result->faces[i].Read(stream);
  469. }
  470. // face frames
  471. uint8_t face_frame_num;
  472. stream->read((char *)&face_frame_num, sizeof(uint8_t));
  473. result->faces_indices.resize(face_frame_num);
  474. for (uint32_t i = 0; i < face_frame_num; i++) {
  475. stream->read((char *)&result->faces_indices[i], sizeof(uint16_t));
  476. }
  477. // bone names
  478. uint8_t bone_disp_num;
  479. stream->read((char *)&bone_disp_num, sizeof(uint8_t));
  480. result->bone_disp_name.resize(bone_disp_num);
  481. for (uint32_t i = 0; i < bone_disp_num; i++) {
  482. result->bone_disp_name[i].Read(stream);
  483. }
  484. // bone frame
  485. uint32_t bone_frame_num;
  486. stream->read((char *)&bone_frame_num, sizeof(uint32_t));
  487. result->bone_disp.resize(bone_frame_num);
  488. for (uint32_t i = 0; i < bone_frame_num; i++) {
  489. result->bone_disp[i].Read(stream);
  490. }
  491. // english name
  492. bool english;
  493. stream->read((char *)&english, sizeof(char));
  494. if (english) {
  495. result->header.ReadExtension(stream);
  496. for (uint32_t i = 0; i < bone_num; i++) {
  497. result->bones[i].ReadExpantion(stream);
  498. }
  499. for (uint32_t i = 0; i < face_num; i++) {
  500. if (result->faces[i].type == pmd::FaceCategory::Base) {
  501. continue;
  502. }
  503. result->faces[i].ReadExpantion(stream);
  504. }
  505. for (uint32_t i = 0; i < result->bone_disp_name.size(); i++) {
  506. result->bone_disp_name[i].ReadExpantion(stream);
  507. }
  508. }
  509. // toon textures
  510. if (stream->peek() == std::ios::traits_type::eof()) {
  511. result->toon_filenames.clear();
  512. } else {
  513. result->toon_filenames.resize(10);
  514. for (uint32_t i = 0; i < 10; i++) {
  515. stream->read(buffer, 100);
  516. result->toon_filenames[i] = std::string(buffer);
  517. }
  518. }
  519. // physics
  520. if (stream->peek() == std::ios::traits_type::eof()) {
  521. result->rigid_bodies.clear();
  522. result->constraints.clear();
  523. } else {
  524. uint32_t rigid_body_num;
  525. stream->read((char *)&rigid_body_num, sizeof(uint32_t));
  526. result->rigid_bodies.resize(rigid_body_num);
  527. for (uint32_t i = 0; i < rigid_body_num; i++) {
  528. result->rigid_bodies[i].Read(stream);
  529. }
  530. uint32_t constraint_num;
  531. stream->read((char *)&constraint_num, sizeof(uint32_t));
  532. result->constraints.resize(constraint_num);
  533. for (uint32_t i = 0; i < constraint_num; i++) {
  534. result->constraints[i].Read(stream);
  535. }
  536. }
  537. if (stream->peek() != std::ios::traits_type::eof()) {
  538. std::cerr << "there is unknown data" << std::endl;
  539. }
  540. return result;
  541. }
  542. };
  543. } // namespace pmd