OpenAssetUtils.cpp 13 KB

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