OpenAssetUtils.cpp 16 KB

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