1 /* 2 Open Asset Import Library (assimp) 3 ---------------------------------------------------------------------- 4 5 Copyright (c) 2006-2019, assimp team 6 7 8 All rights reserved. 9 10 Redistribution and use of this software in source and binary forms, 11 with or without modification, are permitted provided that the 12 following conditions are met: 13 14 * Redistributions of source code must retain the above 15 copyright notice, this list of conditions and the 16 following disclaimer. 17 18 * Redistributions in binary form must reproduce the above 19 copyright notice, this list of conditions and the 20 following disclaimer in the documentation and/or other 21 materials provided with the distribution. 22 23 * Neither the name of the assimp team, nor the names of its 24 contributors may be used to endorse or promote products 25 derived from this software without specific prior 26 written permission of the assimp team. 27 28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 40 ---------------------------------------------------------------------- 41 */ 42 #pragma once 43 44 #include <vector> 45 #include <string> 46 #include <iostream> 47 #include <fstream> 48 #include <memory> 49 #include "MMDCpp14.h" 50 51 namespace pmx 52 { 53 class PmxSetting 54 { 55 public: PmxSetting()56 PmxSetting() 57 : encoding(0) 58 , uv(0) 59 , vertex_index_size(0) 60 , texture_index_size(0) 61 , material_index_size(0) 62 , bone_index_size(0) 63 , morph_index_size(0) 64 , rigidbody_index_size(0) 65 {} 66 67 uint8_t encoding; 68 uint8_t uv; 69 uint8_t vertex_index_size; 70 uint8_t texture_index_size; 71 uint8_t material_index_size; 72 uint8_t bone_index_size; 73 uint8_t morph_index_size; 74 uint8_t rigidbody_index_size; 75 void Read(std::istream *stream); 76 }; 77 78 enum class PmxVertexSkinningType : uint8_t 79 { 80 BDEF1 = 0, 81 BDEF2 = 1, 82 BDEF4 = 2, 83 SDEF = 3, 84 QDEF = 4, 85 }; 86 87 class PmxVertexSkinning 88 { 89 public: 90 virtual void Read(std::istream *stream, PmxSetting *setting) = 0; ~PmxVertexSkinning()91 virtual ~PmxVertexSkinning() {} 92 }; 93 94 class PmxVertexSkinningBDEF1 : public PmxVertexSkinning 95 { 96 public: PmxVertexSkinningBDEF1()97 PmxVertexSkinningBDEF1() 98 : bone_index(0) 99 {} 100 101 int bone_index; 102 void Read(std::istream *stresam, PmxSetting *setting); 103 }; 104 105 class PmxVertexSkinningBDEF2 : public PmxVertexSkinning 106 { 107 public: PmxVertexSkinningBDEF2()108 PmxVertexSkinningBDEF2() 109 : bone_index1(0) 110 , bone_index2(0) 111 , bone_weight(0.0f) 112 {} 113 114 int bone_index1; 115 int bone_index2; 116 float bone_weight; 117 void Read(std::istream *stresam, PmxSetting *setting); 118 }; 119 120 class PmxVertexSkinningBDEF4 : public PmxVertexSkinning 121 { 122 public: PmxVertexSkinningBDEF4()123 PmxVertexSkinningBDEF4() 124 : bone_index1(0) 125 , bone_index2(0) 126 , bone_index3(0) 127 , bone_index4(0) 128 , bone_weight1(0.0f) 129 , bone_weight2(0.0f) 130 , bone_weight3(0.0f) 131 , bone_weight4(0.0f) 132 {} 133 134 int bone_index1; 135 int bone_index2; 136 int bone_index3; 137 int bone_index4; 138 float bone_weight1; 139 float bone_weight2; 140 float bone_weight3; 141 float bone_weight4; 142 void Read(std::istream *stresam, PmxSetting *setting); 143 }; 144 145 class PmxVertexSkinningSDEF : public PmxVertexSkinning 146 { 147 public: PmxVertexSkinningSDEF()148 PmxVertexSkinningSDEF() 149 : bone_index1(0) 150 , bone_index2(0) 151 , bone_weight(0.0f) 152 { 153 for (int i = 0; i < 3; ++i) { 154 sdef_c[i] = 0.0f; 155 sdef_r0[i] = 0.0f; 156 sdef_r1[i] = 0.0f; 157 } 158 } 159 160 int bone_index1; 161 int bone_index2; 162 float bone_weight; 163 float sdef_c[3]; 164 float sdef_r0[3]; 165 float sdef_r1[3]; 166 void Read(std::istream *stresam, PmxSetting *setting); 167 }; 168 169 class PmxVertexSkinningQDEF : public PmxVertexSkinning 170 { 171 public: PmxVertexSkinningQDEF()172 PmxVertexSkinningQDEF() 173 : bone_index1(0) 174 , bone_index2(0) 175 , bone_index3(0) 176 , bone_index4(0) 177 , bone_weight1(0.0f) 178 , bone_weight2(0.0f) 179 , bone_weight3(0.0f) 180 , bone_weight4(0.0f) 181 {} 182 183 int bone_index1; 184 int bone_index2; 185 int bone_index3; 186 int bone_index4; 187 float bone_weight1; 188 float bone_weight2; 189 float bone_weight3; 190 float bone_weight4; 191 void Read(std::istream *stresam, PmxSetting *setting); 192 }; 193 194 class PmxVertex 195 { 196 public: PmxVertex()197 PmxVertex() 198 : edge(0.0f) 199 { 200 uv[0] = uv[1] = 0.0f; 201 for (int i = 0; i < 3; ++i) { 202 position[i] = 0.0f; 203 normal[i] = 0.0f; 204 } 205 for (int i = 0; i < 4; ++i) { 206 for (int k = 0; k < 4; ++k) { 207 uva[i][k] = 0.0f; 208 } 209 } 210 } 211 212 float position[3]; 213 float normal[3]; 214 float uv[2]; 215 float uva[4][4]; 216 PmxVertexSkinningType skinning_type; 217 std::unique_ptr<PmxVertexSkinning> skinning; 218 float edge; 219 void Read(std::istream *stream, PmxSetting *setting); 220 }; 221 222 class PmxMaterial 223 { 224 public: PmxMaterial()225 PmxMaterial() 226 : specularlity(0.0f) 227 , flag(0) 228 , edge_size(0.0f) 229 , diffuse_texture_index(0) 230 , sphere_texture_index(0) 231 , sphere_op_mode(0) 232 , common_toon_flag(0) 233 , toon_texture_index(0) 234 , index_count(0) 235 { 236 for (int i = 0; i < 3; ++i) { 237 specular[i] = 0.0f; 238 ambient[i] = 0.0f; 239 edge_color[i] = 0.0f; 240 } 241 for (int i = 0; i < 4; ++i) { 242 diffuse[i] = 0.0f; 243 } 244 } 245 246 std::string material_name; 247 std::string material_english_name; 248 float diffuse[4]; 249 float specular[3]; 250 float specularlity; 251 float ambient[3]; 252 uint8_t flag; 253 float edge_color[4]; 254 float edge_size; 255 int diffuse_texture_index; 256 int sphere_texture_index; 257 uint8_t sphere_op_mode; 258 uint8_t common_toon_flag; 259 int toon_texture_index; 260 std::string memo; 261 int index_count; 262 void Read(std::istream *stream, PmxSetting *setting); 263 }; 264 265 class PmxIkLink 266 { 267 public: PmxIkLink()268 PmxIkLink() 269 : link_target(0) 270 , angle_lock(0) 271 { 272 for (int i = 0; i < 3; ++i) { 273 max_radian[i] = 0.0f; 274 min_radian[i] = 0.0f; 275 } 276 } 277 278 int link_target; 279 uint8_t angle_lock; 280 float max_radian[3]; 281 float min_radian[3]; 282 void Read(std::istream *stream, PmxSetting *settingn); 283 }; 284 285 class PmxBone 286 { 287 public: PmxBone()288 PmxBone() 289 : parent_index(0) 290 , level(0) 291 , bone_flag(0) 292 , target_index(0) 293 , grant_parent_index(0) 294 , grant_weight(0.0f) 295 , key(0) 296 , ik_target_bone_index(0) 297 , ik_loop(0) 298 , ik_loop_angle_limit(0.0f) 299 , ik_link_count(0) 300 { 301 for (int i = 0; i < 3; ++i) { 302 position[i] = 0.0f; 303 offset[i] = 0.0f; 304 lock_axis_orientation[i] = 0.0f; 305 local_axis_x_orientation[i] = 0.0f; 306 local_axis_y_orientation[i] = 0.0f; 307 } 308 } 309 310 std::string bone_name; 311 std::string bone_english_name; 312 float position[3]; 313 int parent_index; 314 int level; 315 uint16_t bone_flag; 316 float offset[3]; 317 int target_index; 318 int grant_parent_index; 319 float grant_weight; 320 float lock_axis_orientation[3]; 321 float local_axis_x_orientation[3]; 322 float local_axis_y_orientation[3]; 323 int key; 324 int ik_target_bone_index; 325 int ik_loop; 326 float ik_loop_angle_limit; 327 int ik_link_count; 328 std::unique_ptr<PmxIkLink []> ik_links; 329 void Read(std::istream *stream, PmxSetting *setting); 330 }; 331 332 enum class MorphType : uint8_t 333 { 334 Group = 0, 335 Vertex = 1, 336 Bone = 2, 337 UV = 3, 338 AdditionalUV1 = 4, 339 AdditionalUV2 = 5, 340 AdditionalUV3 = 6, 341 AdditionalUV4 = 7, 342 Matrial = 8, 343 Flip = 9, 344 Implus = 10, 345 }; 346 347 enum class MorphCategory : uint8_t 348 { 349 ReservedCategory = 0, 350 Eyebrow = 1, 351 Eye = 2, 352 Mouth = 3, 353 Other = 4, 354 }; 355 356 class PmxMorphOffset 357 { 358 public: 359 void virtual Read(std::istream *stream, PmxSetting *setting) = 0; 360 }; 361 362 class PmxMorphVertexOffset : public PmxMorphOffset 363 { 364 public: PmxMorphVertexOffset()365 PmxMorphVertexOffset() 366 : vertex_index(0) 367 { 368 for (int i = 0; i < 3; ++i) { 369 position_offset[i] = 0.0f; 370 } 371 } 372 int vertex_index; 373 float position_offset[3]; 374 void Read(std::istream *stream, PmxSetting *setting); //override; 375 }; 376 377 class PmxMorphUVOffset : public PmxMorphOffset 378 { 379 public: PmxMorphUVOffset()380 PmxMorphUVOffset() 381 : vertex_index(0) 382 { 383 for (int i = 0; i < 4; ++i) { 384 uv_offset[i] = 0.0f; 385 } 386 } 387 int vertex_index; 388 float uv_offset[4]; 389 void Read(std::istream *stream, PmxSetting *setting); //override; 390 }; 391 392 class PmxMorphBoneOffset : public PmxMorphOffset 393 { 394 public: PmxMorphBoneOffset()395 PmxMorphBoneOffset() 396 : bone_index(0) 397 { 398 for (int i = 0; i < 3; ++i) { 399 translation[i] = 0.0f; 400 } 401 for (int i = 0; i < 4; ++i) { 402 rotation[i] = 0.0f; 403 } 404 } 405 int bone_index; 406 float translation[3]; 407 float rotation[4]; 408 void Read(std::istream *stream, PmxSetting *setting); //override; 409 }; 410 411 class PmxMorphMaterialOffset : public PmxMorphOffset 412 { 413 public: PmxMorphMaterialOffset()414 PmxMorphMaterialOffset() 415 : specularity(0.0f) 416 , edge_size(0.0f) 417 { 418 for (int i = 0; i < 3; ++i) { 419 specular[i] = 0.0f; 420 ambient[i] = 0.0f; 421 } 422 for (int i = 0; i < 4; ++i) { 423 diffuse[i] = 0.0f; 424 edge_color[i] = 0.0f; 425 texture_argb[i] = 0.0f; 426 sphere_texture_argb[i] = 0.0f; 427 toon_texture_argb[i] = 0.0f; 428 } 429 } 430 int material_index; 431 uint8_t offset_operation; 432 float diffuse[4]; 433 float specular[3]; 434 float specularity; 435 float ambient[3]; 436 float edge_color[4]; 437 float edge_size; 438 float texture_argb[4]; 439 float sphere_texture_argb[4]; 440 float toon_texture_argb[4]; 441 void Read(std::istream *stream, PmxSetting *setting); //override; 442 }; 443 444 class PmxMorphGroupOffset : public PmxMorphOffset 445 { 446 public: PmxMorphGroupOffset()447 PmxMorphGroupOffset() 448 : morph_index(0) 449 , morph_weight(0.0f) 450 {} 451 int morph_index; 452 float morph_weight; 453 void Read(std::istream *stream, PmxSetting *setting); //override; 454 }; 455 456 class PmxMorphFlipOffset : public PmxMorphOffset 457 { 458 public: PmxMorphFlipOffset()459 PmxMorphFlipOffset() 460 : morph_index(0) 461 , morph_value(0.0f) 462 {} 463 int morph_index; 464 float morph_value; 465 void Read(std::istream *stream, PmxSetting *setting); //override; 466 }; 467 468 class PmxMorphImplusOffset : public PmxMorphOffset 469 { 470 public: PmxMorphImplusOffset()471 PmxMorphImplusOffset() 472 : rigid_body_index(0) 473 , is_local(0) 474 { 475 for (int i = 0; i < 3; ++i) { 476 velocity[i] = 0.0f; 477 angular_torque[i] = 0.0f; 478 } 479 } 480 int rigid_body_index; 481 uint8_t is_local; 482 float velocity[3]; 483 float angular_torque[3]; 484 void Read(std::istream *stream, PmxSetting *setting); //override; 485 }; 486 487 class PmxMorph 488 { 489 public: PmxMorph()490 PmxMorph() 491 : offset_count(0) 492 { 493 } 494 std::string morph_name; 495 std::string morph_english_name; 496 MorphCategory category; 497 MorphType morph_type; 498 int offset_count; 499 std::unique_ptr<PmxMorphVertexOffset []> vertex_offsets; 500 std::unique_ptr<PmxMorphUVOffset []> uv_offsets; 501 std::unique_ptr<PmxMorphBoneOffset []> bone_offsets; 502 std::unique_ptr<PmxMorphMaterialOffset []> material_offsets; 503 std::unique_ptr<PmxMorphGroupOffset []> group_offsets; 504 std::unique_ptr<PmxMorphFlipOffset []> flip_offsets; 505 std::unique_ptr<PmxMorphImplusOffset []> implus_offsets; 506 void Read(std::istream *stream, PmxSetting *setting); 507 }; 508 509 class PmxFrameElement 510 { 511 public: PmxFrameElement()512 PmxFrameElement() 513 : element_target(0) 514 , index(0) 515 { 516 } 517 uint8_t element_target; 518 int index; 519 void Read(std::istream *stream, PmxSetting *setting); 520 }; 521 522 class PmxFrame 523 { 524 public: PmxFrame()525 PmxFrame() 526 : frame_flag(0) 527 , element_count(0) 528 { 529 } 530 std::string frame_name; 531 std::string frame_english_name; 532 uint8_t frame_flag; 533 int element_count; 534 std::unique_ptr<PmxFrameElement []> elements; 535 void Read(std::istream *stream, PmxSetting *setting); 536 }; 537 538 class PmxRigidBody 539 { 540 public: PmxRigidBody()541 PmxRigidBody() 542 : target_bone(0) 543 , group(0) 544 , mask(0) 545 , shape(0) 546 , mass(0.0f) 547 , move_attenuation(0.0f) 548 , rotation_attenuation(0.0f) 549 , repulsion(0.0f) 550 , friction(0.0f) 551 , physics_calc_type(0) 552 { 553 for (int i = 0; i < 3; ++i) { 554 size[i] = 0.0f; 555 position[i] = 0.0f; 556 orientation[i] = 0.0f; 557 } 558 } 559 std::string girid_body_name; 560 std::string girid_body_english_name; 561 int target_bone; 562 uint8_t group; 563 uint16_t mask; 564 uint8_t shape; 565 float size[3]; 566 float position[3]; 567 float orientation[3]; 568 float mass; 569 float move_attenuation; 570 float rotation_attenuation; 571 float repulsion; 572 float friction; 573 uint8_t physics_calc_type; 574 void Read(std::istream *stream, PmxSetting *setting); 575 }; 576 577 enum class PmxJointType : uint8_t 578 { 579 Generic6DofSpring = 0, 580 Generic6Dof = 1, 581 Point2Point = 2, 582 ConeTwist = 3, 583 Slider = 5, 584 Hinge = 6 585 }; 586 587 class PmxJointParam 588 { 589 public: PmxJointParam()590 PmxJointParam() 591 : rigid_body1(0) 592 , rigid_body2(0) 593 { 594 for (int i = 0; i < 3; ++i) { 595 position[i] = 0.0f; 596 orientaiton[i] = 0.0f; 597 move_limitation_min[i] = 0.0f; 598 move_limitation_max[i] = 0.0f; 599 rotation_limitation_min[i] = 0.0f; 600 rotation_limitation_max[i] = 0.0f; 601 spring_move_coefficient[i] = 0.0f; 602 spring_rotation_coefficient[i] = 0.0f; 603 } 604 } 605 int rigid_body1; 606 int rigid_body2; 607 float position[3]; 608 float orientaiton[3]; 609 float move_limitation_min[3]; 610 float move_limitation_max[3]; 611 float rotation_limitation_min[3]; 612 float rotation_limitation_max[3]; 613 float spring_move_coefficient[3]; 614 float spring_rotation_coefficient[3]; 615 void Read(std::istream *stream, PmxSetting *setting); 616 }; 617 618 class PmxJoint 619 { 620 public: 621 std::string joint_name; 622 std::string joint_english_name; 623 PmxJointType joint_type; 624 PmxJointParam param; 625 void Read(std::istream *stream, PmxSetting *setting); 626 }; 627 628 enum PmxSoftBodyFlag : uint8_t 629 { 630 BLink = 0x01, 631 Cluster = 0x02, 632 Link = 0x04 633 }; 634 635 class PmxAncherRigidBody 636 { 637 public: PmxAncherRigidBody()638 PmxAncherRigidBody() 639 : related_rigid_body(0) 640 , related_vertex(0) 641 , is_near(false) 642 {} 643 int related_rigid_body; 644 int related_vertex; 645 bool is_near; 646 void Read(std::istream *stream, PmxSetting *setting); 647 }; 648 649 class PmxSoftBody 650 { 651 public: PmxSoftBody()652 PmxSoftBody() 653 : shape(0) 654 , target_material(0) 655 , group(0) 656 , mask(0) 657 , blink_distance(0) 658 , cluster_count(0) 659 , mass(0.0) 660 , collisioni_margin(0.0) 661 , aero_model(0) 662 , VCF(0.0f) 663 , DP(0.0f) 664 , DG(0.0f) 665 , LF(0.0f) 666 , PR(0.0f) 667 , VC(0.0f) 668 , DF(0.0f) 669 , MT(0.0f) 670 , CHR(0.0f) 671 , KHR(0.0f) 672 , SHR(0.0f) 673 , AHR(0.0f) 674 , SRHR_CL(0.0f) 675 , SKHR_CL(0.0f) 676 , SSHR_CL(0.0f) 677 , SR_SPLT_CL(0.0f) 678 , SK_SPLT_CL(0.0f) 679 , SS_SPLT_CL(0.0f) 680 , V_IT(0) 681 , P_IT(0) 682 , D_IT(0) 683 , C_IT(0) 684 , LST(0.0f) 685 , AST(0.0f) 686 , VST(0.0f) 687 , anchor_count(0) 688 , pin_vertex_count(0) 689 {} 690 std::string soft_body_name; 691 std::string soft_body_english_name; 692 uint8_t shape; 693 int target_material; 694 uint8_t group; 695 uint16_t mask; 696 PmxSoftBodyFlag flag; 697 int blink_distance; 698 int cluster_count; 699 float mass; 700 float collisioni_margin; 701 int aero_model; 702 float VCF; 703 float DP; 704 float DG; 705 float LF; 706 float PR; 707 float VC; 708 float DF; 709 float MT; 710 float CHR; 711 float KHR; 712 float SHR; 713 float AHR; 714 float SRHR_CL; 715 float SKHR_CL; 716 float SSHR_CL; 717 float SR_SPLT_CL; 718 float SK_SPLT_CL; 719 float SS_SPLT_CL; 720 int V_IT; 721 int P_IT; 722 int D_IT; 723 int C_IT; 724 float LST; 725 float AST; 726 float VST; 727 int anchor_count; 728 std::unique_ptr<PmxAncherRigidBody []> anchers; 729 int pin_vertex_count; 730 std::unique_ptr<int []> pin_vertices; 731 void Read(std::istream *stream, PmxSetting *setting); 732 }; 733 734 class PmxModel 735 { 736 public: PmxModel()737 PmxModel() 738 : version(0.0f) 739 , vertex_count(0) 740 , index_count(0) 741 , texture_count(0) 742 , material_count(0) 743 , bone_count(0) 744 , morph_count(0) 745 , frame_count(0) 746 , rigid_body_count(0) 747 , joint_count(0) 748 , soft_body_count(0) 749 {} 750 751 float version; 752 PmxSetting setting; 753 std::string model_name; 754 std::string model_english_name; 755 std::string model_comment; 756 std::string model_english_comment; 757 int vertex_count; 758 std::unique_ptr<PmxVertex []> vertices; 759 int index_count; 760 std::unique_ptr<int []> indices; 761 int texture_count; 762 std::unique_ptr< std::string []> textures; 763 int material_count; 764 std::unique_ptr<PmxMaterial []> materials; 765 int bone_count; 766 std::unique_ptr<PmxBone []> bones; 767 int morph_count; 768 std::unique_ptr<PmxMorph []> morphs; 769 int frame_count; 770 std::unique_ptr<PmxFrame [] > frames; 771 int rigid_body_count; 772 std::unique_ptr<PmxRigidBody []> rigid_bodies; 773 int joint_count; 774 std::unique_ptr<PmxJoint []> joints; 775 int soft_body_count; 776 std::unique_ptr<PmxSoftBody []> soft_bodies; 777 void Init(); 778 void Read(std::istream *stream); 779 //static std::unique_ptr<PmxModel> ReadFromFile(const char *filename); 780 //static std::unique_ptr<PmxModel> ReadFromStream(std::istream *stream); 781 }; 782 } 783