OpenAssetUtils.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. //
  8. // Copyright (c) 2008-2015 the Urho3D project.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. //
  28. #include <Atomic/Core/ProcessUtils.h>
  29. #include <Atomic/Graphics/Geometry.h>
  30. #include "OpenAssetUtils.h"
  31. namespace ToolCore
  32. {
  33. void CollectMeshes(const aiScene* scene, OutModel& model, aiNode* node)
  34. {
  35. // skip LOD for now
  36. String name = node->mName.C_Str();
  37. if (name.Find("LOD1") != String::NPOS || name.Find("LOD2") != String::NPOS)
  38. {
  39. return;
  40. }
  41. for (unsigned i = 0; i < node->mNumMeshes; ++i)
  42. {
  43. aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
  44. for (unsigned j = 0; j < model.meshes_.Size(); ++j)
  45. {
  46. if (mesh == model.meshes_[j])
  47. {
  48. PrintLine("Warning: same mesh found multiple times");
  49. break;
  50. }
  51. }
  52. model.meshIndices_.Insert(node->mMeshes[i]);
  53. model.meshes_.Push(mesh);
  54. model.meshNodes_.Push(node);
  55. model.totalVertices_ += mesh->mNumVertices;
  56. model.totalIndices_ += GetNumValidFaces(mesh) * 3;
  57. }
  58. for (unsigned i = 0; i < node->mNumChildren; ++i)
  59. CollectMeshes(scene, model, node->mChildren[i]);
  60. }
  61. void GetMeshesUnderNode(const aiScene* scene, Vector<Pair<aiNode*, aiMesh*> >& dest, aiNode* node)
  62. {
  63. for (unsigned i = 0; i < node->mNumMeshes; ++i)
  64. dest.Push(MakePair(node, scene->mMeshes[node->mMeshes[i]]));
  65. }
  66. unsigned GetMeshIndex(const aiScene* scene, aiMesh* mesh)
  67. {
  68. for (unsigned i = 0; i < scene->mNumMeshes; ++i)
  69. {
  70. if (scene->mMeshes[i] == mesh)
  71. return i;
  72. }
  73. return M_MAX_UNSIGNED;
  74. }
  75. unsigned GetBoneIndex(OutModel& model, const String& boneName)
  76. {
  77. for (unsigned i = 0; i < model.bones_.Size(); ++i)
  78. {
  79. if (boneName == model.bones_[i]->mName.data)
  80. return i;
  81. }
  82. return M_MAX_UNSIGNED;
  83. }
  84. aiBone* GetMeshBone(OutModel& model, const String& boneName)
  85. {
  86. for (unsigned i = 0; i < model.meshes_.Size(); ++i)
  87. {
  88. aiMesh* mesh = model.meshes_[i];
  89. for (unsigned j = 0; j < mesh->mNumBones; ++j)
  90. {
  91. aiBone* bone = mesh->mBones[j];
  92. if (boneName == bone->mName.data)
  93. return bone;
  94. }
  95. }
  96. return 0;
  97. }
  98. Matrix3x4 GetOffsetMatrix(OutModel& model, const String& boneName)
  99. {
  100. for (unsigned i = 0; i < model.meshes_.Size(); ++i)
  101. {
  102. aiMesh* mesh = model.meshes_[i];
  103. aiNode* node = model.meshNodes_[i];
  104. for (unsigned j = 0; j < mesh->mNumBones; ++j)
  105. {
  106. aiBone* bone = mesh->mBones[j];
  107. if (boneName == bone->mName.data)
  108. {
  109. aiMatrix4x4 offset = bone->mOffsetMatrix;
  110. aiMatrix4x4 nodeDerivedInverse = GetMeshBakingTransform(node, model.rootNode_);
  111. nodeDerivedInverse.Inverse();
  112. offset *= nodeDerivedInverse;
  113. return ToMatrix3x4(offset);
  114. }
  115. }
  116. }
  117. return Matrix3x4::IDENTITY;
  118. }
  119. unsigned GetNumValidFaces(aiMesh* mesh)
  120. {
  121. unsigned ret = 0;
  122. for (unsigned j = 0; j < mesh->mNumFaces; ++j)
  123. {
  124. if (mesh->mFaces[j].mNumIndices == 3)
  125. ++ret;
  126. }
  127. return ret;
  128. }
  129. void WriteShortIndices(unsigned short*& dest, aiMesh* mesh, unsigned index, unsigned offset)
  130. {
  131. if (mesh->mFaces[index].mNumIndices == 3)
  132. {
  133. *dest++ = mesh->mFaces[index].mIndices[0] + offset;
  134. *dest++ = mesh->mFaces[index].mIndices[1] + offset;
  135. *dest++ = mesh->mFaces[index].mIndices[2] + offset;
  136. }
  137. }
  138. void WriteLargeIndices(unsigned*& dest, aiMesh* mesh, unsigned index, unsigned offset)
  139. {
  140. if (mesh->mFaces[index].mNumIndices == 3)
  141. {
  142. *dest++ = mesh->mFaces[index].mIndices[0] + offset;
  143. *dest++ = mesh->mFaces[index].mIndices[1] + offset;
  144. *dest++ = mesh->mFaces[index].mIndices[2] + offset;
  145. }
  146. }
  147. void WriteVertex(float*& dest, aiMesh* mesh, unsigned index, unsigned elementMask, BoundingBox& box,
  148. const Matrix3x4& vertexTransform, const Matrix3& normalTransform, Vector<PODVector<unsigned char> >& blendIndices,
  149. Vector<PODVector<float> >& blendWeights)
  150. {
  151. Vector3 vertex = vertexTransform * ToVector3(mesh->mVertices[index]);
  152. box.Merge(vertex);
  153. *dest++ = vertex.x_;
  154. *dest++ = vertex.y_;
  155. *dest++ = vertex.z_;
  156. if (elementMask & MASK_NORMAL)
  157. {
  158. Vector3 normal = normalTransform * ToVector3(mesh->mNormals[index]);
  159. *dest++ = normal.x_;
  160. *dest++ = normal.y_;
  161. *dest++ = normal.z_;
  162. }
  163. if (elementMask & MASK_COLOR)
  164. {
  165. *((unsigned*)dest) = Color(mesh->mColors[0][index].r, mesh->mColors[0][index].g, mesh->mColors[0][index].b,
  166. mesh->mColors[0][index].a).ToUInt();
  167. ++dest;
  168. }
  169. if (elementMask & MASK_TEXCOORD1)
  170. {
  171. Vector3 texCoord = ToVector3(mesh->mTextureCoords[0][index]);
  172. *dest++ = texCoord.x_;
  173. *dest++ = texCoord.y_;
  174. }
  175. if (elementMask & MASK_TEXCOORD2)
  176. {
  177. Vector3 texCoord = ToVector3(mesh->mTextureCoords[1][index]);
  178. *dest++ = texCoord.x_;
  179. *dest++ = texCoord.y_;
  180. }
  181. if (elementMask & MASK_TANGENT)
  182. {
  183. Vector3 tangent = normalTransform * ToVector3(mesh->mTangents[index]);
  184. Vector3 normal = normalTransform * ToVector3(mesh->mNormals[index]);
  185. Vector3 bitangent = normalTransform * ToVector3(mesh->mBitangents[index]);
  186. // Check handedness
  187. float w = 1.0f;
  188. if ((tangent.CrossProduct(normal)).DotProduct(bitangent) < 0.5f)
  189. w = -1.0f;
  190. *dest++ = tangent.x_;
  191. *dest++ = tangent.y_;
  192. *dest++ = tangent.z_;
  193. *dest++ = w;
  194. }
  195. if (elementMask & MASK_BLENDWEIGHTS)
  196. {
  197. for (unsigned i = 0; i < 4; ++i)
  198. {
  199. if (i < blendWeights[index].Size())
  200. *dest++ = blendWeights[index][i];
  201. else
  202. *dest++ = 0.0f;
  203. }
  204. }
  205. if (elementMask & MASK_BLENDINDICES)
  206. {
  207. unsigned char* destBytes = (unsigned char*)dest;
  208. ++dest;
  209. for (unsigned i = 0; i < 4; ++i)
  210. {
  211. if (i < blendIndices[index].Size())
  212. *destBytes++ = blendIndices[index][i];
  213. else
  214. *destBytes++ = 0;
  215. }
  216. }
  217. }
  218. unsigned GetElementMask(aiMesh* mesh)
  219. {
  220. unsigned elementMask = MASK_POSITION;
  221. if (mesh->HasNormals())
  222. elementMask |= MASK_NORMAL;
  223. if (mesh->HasTangentsAndBitangents())
  224. elementMask |= MASK_TANGENT;
  225. if (mesh->GetNumColorChannels() > 0)
  226. elementMask |= MASK_COLOR;
  227. if (mesh->GetNumUVChannels() > 0)
  228. elementMask |= MASK_TEXCOORD1;
  229. if (mesh->GetNumUVChannels() > 1)
  230. elementMask |= MASK_TEXCOORD2;
  231. if (mesh->HasBones())
  232. elementMask |= (MASK_BLENDWEIGHTS | MASK_BLENDINDICES);
  233. return elementMask;
  234. }
  235. aiNode* GetNode(const String& name, aiNode* rootNode, bool caseSensitive)
  236. {
  237. if (!rootNode)
  238. return 0;
  239. if (!name.Compare(rootNode->mName.data, caseSensitive))
  240. return rootNode;
  241. for (unsigned i = 0; i < rootNode->mNumChildren; ++i)
  242. {
  243. aiNode* found = GetNode(name, rootNode->mChildren[i], caseSensitive);
  244. if (found)
  245. return found;
  246. }
  247. return 0;
  248. }
  249. aiMatrix4x4 GetDerivedTransform(aiNode* node, aiNode* rootNode, bool rootInclusive)
  250. {
  251. return GetDerivedTransform(node->mTransformation, node, rootNode, rootInclusive);
  252. }
  253. aiMatrix4x4 GetDerivedTransform(aiMatrix4x4 transform, aiNode* node, aiNode* rootNode, bool rootInclusive)
  254. {
  255. // If basenode is defined, go only up to it in the parent chain
  256. while (node && node != rootNode)
  257. {
  258. node = node->mParent;
  259. if (!rootInclusive && node == rootNode)
  260. break;
  261. if (node)
  262. transform = node->mTransformation * transform;
  263. }
  264. return transform;
  265. }
  266. aiMatrix4x4 GetMeshBakingTransform(aiNode* meshNode, aiNode* modelRootNode)
  267. {
  268. if (meshNode == modelRootNode)
  269. return aiMatrix4x4();
  270. else
  271. return GetDerivedTransform(meshNode, modelRootNode);
  272. }
  273. void GetPosRotScale(const aiMatrix4x4& transform, Vector3& pos, Quaternion& rot, Vector3& scale)
  274. {
  275. aiVector3D aiPos;
  276. aiQuaternion aiRot;
  277. aiVector3D aiScale;
  278. transform.Decompose(aiScale, aiRot, aiPos);
  279. pos = ToVector3(aiPos);
  280. rot = ToQuaternion(aiRot);
  281. scale = ToVector3(aiScale);
  282. }
  283. bool GetBlendData(OutModel& model, aiMesh* mesh, PODVector<unsigned>& boneMappings, Vector<PODVector<unsigned char> >&
  284. blendIndices, Vector<PODVector<float> >& blendWeights, String& errorMessage, unsigned maxBones)
  285. {
  286. blendIndices.Resize(mesh->mNumVertices);
  287. blendWeights.Resize(mesh->mNumVertices);
  288. boneMappings.Clear();
  289. // If model has more bones than can fit vertex shader parameters, write the per-geometry mappings
  290. if (model.bones_.Size() > maxBones)
  291. {
  292. if (mesh->mNumBones > maxBones)
  293. {
  294. errorMessage = "Geometry (submesh) has over " + String(maxBones) + " bone influences. Try splitting to more submeshes\n"
  295. "that each stay at " + String(maxBones) + " bones or below.";
  296. return false;
  297. }
  298. boneMappings.Resize(mesh->mNumBones);
  299. for (unsigned i = 0; i < mesh->mNumBones; ++i)
  300. {
  301. aiBone* bone = mesh->mBones[i];
  302. String boneName = FromAIString(bone->mName);
  303. unsigned globalIndex = GetBoneIndex(model, boneName);
  304. if (globalIndex == M_MAX_UNSIGNED)
  305. {
  306. errorMessage = "Bone " + boneName + " not found";
  307. return false;
  308. }
  309. boneMappings[i] = globalIndex;
  310. for (unsigned j = 0; j < bone->mNumWeights; ++j)
  311. {
  312. unsigned vertex = bone->mWeights[j].mVertexId;
  313. blendIndices[vertex].Push(i);
  314. blendWeights[vertex].Push(bone->mWeights[j].mWeight);
  315. }
  316. }
  317. }
  318. else
  319. {
  320. for (unsigned i = 0; i < mesh->mNumBones; ++i)
  321. {
  322. aiBone* bone = mesh->mBones[i];
  323. String boneName = FromAIString(bone->mName);
  324. unsigned globalIndex = GetBoneIndex(model, boneName);
  325. if (globalIndex == M_MAX_UNSIGNED)
  326. {
  327. errorMessage = "Bone " + boneName + " not found";
  328. return false;
  329. }
  330. for (unsigned j = 0; j < bone->mNumWeights; ++j)
  331. {
  332. unsigned vertex = bone->mWeights[j].mVertexId;
  333. blendIndices[vertex].Push(globalIndex);
  334. blendWeights[vertex].Push(bone->mWeights[j].mWeight);
  335. }
  336. }
  337. }
  338. // Normalize weights now if necessary, also remove too many influences
  339. for (unsigned i = 0; i < blendWeights.Size(); ++i)
  340. {
  341. if (blendWeights[i].Size() > 4)
  342. {
  343. PrintLine("Warning: more than 4 bone influences in vertex " + String(i));
  344. while (blendWeights[i].Size() > 4)
  345. {
  346. unsigned lowestIndex = 0;
  347. float lowest = M_INFINITY;
  348. for (unsigned j = 0; j < blendWeights[i].Size(); ++j)
  349. {
  350. if (blendWeights[i][j] < lowest)
  351. {
  352. lowest = blendWeights[i][j];
  353. lowestIndex = j;
  354. }
  355. }
  356. blendWeights[i].Erase(lowestIndex);
  357. blendIndices[i].Erase(lowestIndex);
  358. }
  359. }
  360. float sum = 0.0f;
  361. for (unsigned j = 0; j < blendWeights[i].Size(); ++j)
  362. sum += blendWeights[i][j];
  363. if (sum != 1.0f && sum != 0.0f)
  364. {
  365. for (unsigned j = 0; j < blendWeights[i].Size(); ++j)
  366. blendWeights[i][j] /= sum;
  367. }
  368. }
  369. return true;
  370. }
  371. String FromAIString(const aiString& str)
  372. {
  373. return String(str.data);
  374. }
  375. Vector3 ToVector3(const aiVector3D& vec)
  376. {
  377. return Vector3(vec.x, vec.y, vec.z);
  378. }
  379. Vector2 ToVector2(const aiVector2D& vec)
  380. {
  381. return Vector2(vec.x, vec.y);
  382. }
  383. Quaternion ToQuaternion(const aiQuaternion& quat)
  384. {
  385. return Quaternion(quat.w, quat.x, quat.y, quat.z);
  386. }
  387. Matrix3x4 ToMatrix3x4(const aiMatrix4x4& mat)
  388. {
  389. Matrix3x4 ret;
  390. memcpy(&ret.m00_, &mat.a1, sizeof(Matrix3x4));
  391. return ret;
  392. }
  393. String SanitateAssetName(const String& name)
  394. {
  395. String fixedName = name;
  396. fixedName.Replace("<", "");
  397. fixedName.Replace(">", "");
  398. fixedName.Replace("?", "");
  399. fixedName.Replace("*", "");
  400. fixedName.Replace(":", "");
  401. fixedName.Replace("\"", "");
  402. fixedName.Replace("/", "");
  403. fixedName.Replace("\\", "");
  404. fixedName.Replace("|", "");
  405. return fixedName;
  406. }
  407. }