OpenAssetUtils.h 4.3 KB

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