OpenAssetUtils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. #pragma once
  25. #include <ThirdParty/Assimp/include/assimp/cimport.h>
  26. #include <ThirdParty/Assimp/include/assimp/scene.h>
  27. #include <ThirdParty/Assimp/include/assimp/postprocess.h>
  28. #include <ThirdParty/Assimp/include/assimp/DefaultLogger.hpp>
  29. #include <cstring>
  30. #include <Atomic/Container/Str.h>
  31. #include <Atomic/Container/Sort.h>
  32. #include <Atomic/Container/Hash.h>
  33. #include <Atomic/Container/HashSet.h>
  34. #include <Atomic/Core/StringUtils.h>
  35. #include <Atomic/Math/Vector3.h>
  36. #include <Atomic/Math/Quaternion.h>
  37. #include <Atomic/Math/Matrix3x4.h>
  38. #include <Atomic/Math/BoundingBox.h>
  39. using namespace Atomic;
  40. namespace ToolCore
  41. {
  42. struct OutModel
  43. {
  44. OutModel() :
  45. rootBone_(0),
  46. totalVertices_(0),
  47. totalIndices_(0)
  48. {
  49. }
  50. String outName_;
  51. aiNode* rootNode_;
  52. HashSet<unsigned> meshIndices_;
  53. PODVector<aiMesh*> meshes_;
  54. PODVector<aiNode*> meshNodes_;
  55. PODVector<aiNode*> bones_;
  56. PODVector<aiAnimation*> animations_;
  57. PODVector<float> boneRadii_;
  58. PODVector<BoundingBox> boneHitboxes_;
  59. aiNode* rootBone_;
  60. unsigned totalVertices_;
  61. unsigned totalIndices_;
  62. };
  63. struct OutScene
  64. {
  65. String outName_;
  66. aiNode* rootNode_;
  67. Vector<OutModel> models_;
  68. PODVector<aiNode*> nodes_;
  69. PODVector<unsigned> nodeModelIndices_;
  70. };
  71. void WriteShortIndices(unsigned short*& dest, aiMesh* mesh, unsigned index, unsigned offset);
  72. void WriteLargeIndices(unsigned*& dest, aiMesh* mesh, unsigned index, unsigned offset);
  73. void WriteVertex(float*& dest, aiMesh* mesh, unsigned index, unsigned elementMask, BoundingBox& box,
  74. const Matrix3x4& vertexTransform, const Matrix3& normalTransform, Vector<PODVector<unsigned char> >& blendIndices,
  75. Vector<PODVector<float> >& blendWeights);
  76. unsigned GetElementMask(aiMesh* mesh);
  77. aiNode* GetNode(const String& name, aiNode* rootNode, bool caseSensitive = true);
  78. aiMatrix4x4 GetDerivedTransform(aiNode* node, aiNode* rootNode, bool rootInclusive = true);
  79. aiMatrix4x4 GetDerivedTransform(aiMatrix4x4 transform, aiNode* node, aiNode* rootNode, bool rootInclusive = true);
  80. aiMatrix4x4 GetMeshBakingTransform(aiNode* meshNode, aiNode* modelRootNode);
  81. void GetPosRotScale(const aiMatrix4x4& transform, Vector3& pos, Quaternion& rot, Vector3& scale);
  82. String FromAIString(const aiString& str);
  83. Vector3 ToVector3(const aiVector3D& vec);
  84. Vector2 ToVector2(const aiVector2D& vec);
  85. Quaternion ToQuaternion(const aiQuaternion& quat);
  86. Matrix3x4 ToMatrix3x4(const aiMatrix4x4& mat);
  87. String SanitateAssetName(const String& name);
  88. void GetMeshesUnderNode(const aiScene* scene, Vector<Pair<aiNode*, aiMesh*> >& dest, aiNode* node);
  89. unsigned GetMeshIndex(const aiScene* scene, aiMesh* mesh);
  90. unsigned GetBoneIndex(OutModel& model, const String& boneName);
  91. aiBone* GetMeshBone(OutModel& model, const String& boneName);
  92. Matrix3x4 GetOffsetMatrix(OutModel& model, const String& boneName);
  93. unsigned GetNumValidFaces(aiMesh* mesh);
  94. bool GetBlendData(OutModel& model, aiMesh* mesh, PODVector<unsigned>& boneMappings, Vector<PODVector<unsigned char> >&
  95. blendIndices, Vector<PODVector<float> >& blendWeights, String &errorMessage, unsigned maxBones = 64);
  96. void CollectMeshes(const aiScene* scene, OutModel& model, aiNode* node);
  97. void DumpNodes(aiNode* rootNode, unsigned level);
  98. }