OpenAssetUtils.cpp 13 KB

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