OpenAssetUtils.cpp 12 KB

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