BsFBXUtility.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsFBXUtility.h"
  4. #include "Math/BsVector2.h"
  5. #include "Math/BsVector3.h"
  6. #include "Math/BsVector4.h"
  7. namespace bs
  8. {
  9. struct SmoothNormal
  10. {
  11. int group = 0;
  12. Vector3 normal = Vector3::ZERO;
  13. void addNormal(int group, const Vector3& normal)
  14. {
  15. this->group |= group;
  16. this->normal += normal;
  17. }
  18. void addNormal(const SmoothNormal& other)
  19. {
  20. this->group |= other.group;
  21. this->normal += other.normal;
  22. }
  23. void normalize()
  24. {
  25. normal.normalize();
  26. }
  27. };
  28. struct SmoothVertex
  29. {
  30. Vector<SmoothNormal> normals;
  31. void addNormal(int group, const Vector3& normal)
  32. {
  33. bool found = false;
  34. for (size_t i = 0; i < normals.size(); i++)
  35. {
  36. if ((normals[i].group & group) != 0)
  37. {
  38. bool otherGroups = (group & ~normals[i].group) != 0;
  39. if (otherGroups)
  40. {
  41. for (size_t j = i + 1; j < normals.size(); j++)
  42. {
  43. if ((normals[j].group & group) != 0)
  44. {
  45. normals[i].addNormal(normals[j]);
  46. normals.erase(normals.begin() + j);
  47. --j;
  48. }
  49. }
  50. }
  51. normals[i].addNormal(group, normal);
  52. found = true;
  53. break;;
  54. }
  55. }
  56. if (!found)
  57. {
  58. SmoothNormal smoothNormal;
  59. smoothNormal.addNormal(group, normal);
  60. normals.push_back(smoothNormal);
  61. }
  62. }
  63. Vector3 getNormal(int group) const
  64. {
  65. for (size_t i = 0; i < normals.size(); i++)
  66. {
  67. if (normals[i].group & group)
  68. return normals[i].normal;
  69. }
  70. return Vector3::ZERO;
  71. }
  72. void normalize()
  73. {
  74. for (size_t i = 0; i < normals.size(); ++i)
  75. normals[i].normalize();
  76. }
  77. };
  78. void FBXUtility::normalsFromSmoothing(const Vector<Vector3>& positions, const Vector<int>& indices,
  79. const Vector<int>& smoothing, Vector<Vector3>& normals)
  80. {
  81. std::vector<SmoothVertex> smoothNormals;
  82. smoothNormals.resize(positions.size());
  83. normals.resize(indices.size(), Vector3::ZERO);
  84. UINT32 numPolygons = (UINT32)(indices.size() / 3);
  85. int idx = 0;
  86. for (UINT32 i = 0; i < numPolygons; i++)
  87. {
  88. for (UINT32 j = 0; j < 3; j++)
  89. {
  90. int prevOffset = (j > 0 ? j - 1 : 2);
  91. int nextOffset = (j < 2 ? j + 1 : 0);
  92. int current = indices[idx + j];
  93. Vector3 v0 = (Vector3)positions[indices[idx + prevOffset]];
  94. Vector3 v1 = (Vector3)positions[current];
  95. Vector3 v2 = (Vector3)positions[indices[idx + nextOffset]];
  96. Vector3 normal = Vector3::cross(v0 - v1, v2 - v1);
  97. smoothNormals[current].addNormal(smoothing[idx + j], normal);
  98. normals[idx + j] = normal;
  99. }
  100. idx += 3;
  101. }
  102. for (size_t i = 0; i < smoothNormals.size(); ++i)
  103. smoothNormals[i].normalize();
  104. idx = 0;
  105. for (UINT32 i = 0; i < numPolygons; i++)
  106. {
  107. for (UINT32 j = 0; j < 3; j++)
  108. {
  109. if (smoothing[idx + i] != 0)
  110. {
  111. int current = indices[idx + i];
  112. normals[idx + i] = smoothNormals[current].getNormal(smoothing[idx + i]);
  113. }
  114. }
  115. idx += 3;
  116. }
  117. }
  118. void FBXUtility::splitVertices(const FBXImportMesh& source, FBXImportMesh& dest)
  119. {
  120. dest.indices = source.indices;
  121. dest.materials = source.materials;
  122. dest.positions = source.positions;
  123. dest.boneInfluences = source.boneInfluences;
  124. // Make room for minimal set of vertices
  125. UINT32 vertexCount = (UINT32)source.positions.size();
  126. if (!source.normals.empty())
  127. dest.normals.resize(vertexCount);
  128. if (!source.tangents.empty())
  129. dest.tangents.resize(vertexCount);
  130. if (!source.bitangents.empty())
  131. dest.bitangents.resize(vertexCount);
  132. if (!source.colors.empty())
  133. dest.colors.resize(vertexCount);
  134. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  135. {
  136. if (!source.UV[i].empty())
  137. dest.UV[i].resize(vertexCount);
  138. }
  139. UINT32 numBlendShapes = (UINT32)source.blendShapes.size();
  140. dest.blendShapes.resize(numBlendShapes);
  141. for (UINT32 i = 0; i < numBlendShapes; i++)
  142. {
  143. const FBXBlendShape& sourceShape = source.blendShapes[i];
  144. FBXBlendShape& destShape = dest.blendShapes[i];
  145. UINT32 numFrames = (UINT32)sourceShape.frames.size();
  146. destShape.frames.resize(numFrames);
  147. destShape.name = sourceShape.name;
  148. for (UINT32 j = 0; j < numFrames; j++)
  149. {
  150. const FBXBlendShapeFrame& sourceFrame = sourceShape.frames[j];
  151. FBXBlendShapeFrame& destFrame = destShape.frames[j];
  152. destFrame.name = sourceFrame.name;
  153. destFrame.weight = sourceFrame.weight;
  154. destFrame.positions = sourceFrame.positions;
  155. if (!sourceFrame.normals.empty())
  156. destFrame.normals.resize(vertexCount);
  157. if (!sourceFrame.tangents.empty())
  158. destFrame.tangents.resize(vertexCount);
  159. if (!sourceFrame.bitangents.empty())
  160. destFrame.bitangents.resize(vertexCount);
  161. }
  162. }
  163. Vector<Vector<int>> splitsPerVertex;
  164. splitsPerVertex.resize(source.positions.size());
  165. Vector<int>& indices = dest.indices;
  166. int indexCount = (int)dest.indices.size();
  167. for (int i = 0; i < indexCount; i++)
  168. {
  169. int srcVertIdx = indices[i];
  170. int dstVertIdx = -1;
  171. // See if we already processed this vertex and if its attributes
  172. // are close enough with the previous version
  173. Vector<int>& splits = splitsPerVertex[srcVertIdx];
  174. for (auto& splitVertIdx : splits)
  175. {
  176. if (!needsSplitAttributes(source, i, dest, splitVertIdx))
  177. dstVertIdx = splitVertIdx;
  178. }
  179. // We didn't find a close-enough match
  180. if (dstVertIdx == -1)
  181. {
  182. // First time we visited this vertex, so just copy over attributes
  183. if (splits.empty())
  184. {
  185. dstVertIdx = srcVertIdx;
  186. copyVertexAttributes(source, i, dest, dstVertIdx);
  187. }
  188. else // Split occurred, add a brand new vertex
  189. {
  190. dstVertIdx = (int)dest.positions.size();
  191. addVertex(source, i, srcVertIdx, dest);
  192. }
  193. splits.push_back(dstVertIdx);
  194. }
  195. indices[i] = dstVertIdx;
  196. }
  197. }
  198. void FBXUtility::flipWindingOrder(FBXImportMesh& input)
  199. {
  200. for (UINT32 i = 0; i < (UINT32)input.materials.size(); i += 3)
  201. {
  202. std::swap(input.materials[i + 1], input.materials[i + 2]);
  203. }
  204. for (UINT32 i = 0; i < (UINT32)input.indices.size(); i += 3)
  205. {
  206. std::swap(input.indices[i + 1], input.indices[i + 2]);
  207. }
  208. }
  209. void FBXUtility::copyVertexAttributes(const FBXImportMesh& srcMesh, int srcIdx, FBXImportMesh& destMesh, int dstIdx)
  210. {
  211. if (!srcMesh.normals.empty())
  212. destMesh.normals[dstIdx] = srcMesh.normals[srcIdx];
  213. if (!srcMesh.tangents.empty())
  214. destMesh.tangents[dstIdx] = srcMesh.tangents[srcIdx];
  215. if (!srcMesh.bitangents.empty())
  216. destMesh.bitangents[dstIdx] = srcMesh.bitangents[srcIdx];
  217. if (!srcMesh.colors.empty())
  218. destMesh.colors[dstIdx] = srcMesh.colors[srcIdx];
  219. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  220. {
  221. if (!srcMesh.UV[i].empty())
  222. destMesh.UV[i][dstIdx] = srcMesh.UV[i][srcIdx];
  223. }
  224. UINT32 numBlendShapes = (UINT32)srcMesh.blendShapes.size();
  225. for (UINT32 i = 0; i < numBlendShapes; i++)
  226. {
  227. const FBXBlendShape& sourceShape = srcMesh.blendShapes[i];
  228. FBXBlendShape& destShape = destMesh.blendShapes[i];
  229. UINT32 numFrames = (UINT32)sourceShape.frames.size();
  230. for (UINT32 j = 0; j < numFrames; j++)
  231. {
  232. const FBXBlendShapeFrame& sourceFrame = sourceShape.frames[j];
  233. FBXBlendShapeFrame& destFrame = destShape.frames[j];
  234. if (!sourceFrame.normals.empty())
  235. destFrame.normals[dstIdx] = sourceFrame.normals[srcIdx];
  236. if (!sourceFrame.tangents.empty())
  237. destFrame.tangents[dstIdx] = sourceFrame.tangents[srcIdx];
  238. if (!sourceFrame.bitangents.empty())
  239. destFrame.bitangents[dstIdx] = sourceFrame.bitangents[srcIdx];
  240. }
  241. }
  242. }
  243. void FBXUtility::addVertex(const FBXImportMesh& srcMesh, int srcIdx, int srcVertex, FBXImportMesh& destMesh)
  244. {
  245. destMesh.positions.push_back(srcMesh.positions[srcVertex]);
  246. if (!srcMesh.boneInfluences.empty())
  247. destMesh.boneInfluences.push_back(srcMesh.boneInfluences[srcVertex]);
  248. if (!srcMesh.normals.empty())
  249. destMesh.normals.push_back(srcMesh.normals[srcIdx]);
  250. if (!srcMesh.tangents.empty())
  251. destMesh.tangents.push_back(srcMesh.tangents[srcIdx]);
  252. if (!srcMesh.bitangents.empty())
  253. destMesh.bitangents.push_back(srcMesh.bitangents[srcIdx]);
  254. if (!srcMesh.colors.empty())
  255. destMesh.colors.push_back(srcMesh.colors[srcIdx]);
  256. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  257. {
  258. if (!srcMesh.UV[i].empty())
  259. destMesh.UV[i].push_back(srcMesh.UV[i][srcIdx]);
  260. }
  261. UINT32 numBlendShapes = (UINT32)srcMesh.blendShapes.size();
  262. for (UINT32 i = 0; i < numBlendShapes; i++)
  263. {
  264. const FBXBlendShape& sourceShape = srcMesh.blendShapes[i];
  265. FBXBlendShape& destShape = destMesh.blendShapes[i];
  266. UINT32 numFrames = (UINT32)sourceShape.frames.size();
  267. for (UINT32 j = 0; j < numFrames; j++)
  268. {
  269. const FBXBlendShapeFrame& sourceFrame = sourceShape.frames[j];
  270. FBXBlendShapeFrame& destFrame = destShape.frames[j];
  271. destFrame.positions.push_back(sourceFrame.positions[srcVertex]);
  272. if (!sourceFrame.normals.empty())
  273. destFrame.normals.push_back(sourceFrame.normals[srcIdx]);
  274. if (!sourceFrame.tangents.empty())
  275. destFrame.tangents.push_back(sourceFrame.tangents[srcIdx]);
  276. if (!sourceFrame.bitangents.empty())
  277. destFrame.bitangents.push_back(sourceFrame.bitangents[srcIdx]);
  278. }
  279. }
  280. }
  281. bool FBXUtility::needsSplitAttributes(const FBXImportMesh& meshA, int idxA, const FBXImportMesh& meshB, int idxB)
  282. {
  283. static const float SplitAngleCosine = Math::cos(Degree(1.0f));
  284. static const float UVEpsilon = 0.001f;
  285. if (!meshA.colors.empty())
  286. {
  287. if (meshA.colors[idxA] != meshB.colors[idxB])
  288. return true;
  289. }
  290. if (!meshA.normals.empty())
  291. {
  292. float angleCosine = meshA.normals[idxA].dot(meshB.normals[idxB]);
  293. if (angleCosine < SplitAngleCosine)
  294. return true;
  295. }
  296. if (!meshA.tangents.empty())
  297. {
  298. float angleCosine = meshA.tangents[idxA].dot(meshB.tangents[idxB]);
  299. if (angleCosine < SplitAngleCosine)
  300. return true;
  301. }
  302. if (!meshA.bitangents.empty())
  303. {
  304. float angleCosine = meshA.bitangents[idxA].dot(meshB.bitangents[idxB]);
  305. if (angleCosine < SplitAngleCosine)
  306. return true;
  307. }
  308. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  309. {
  310. if (!meshA.UV[i].empty())
  311. {
  312. if (!Math::approxEquals(meshA.UV[i][idxA], meshB.UV[i][idxB], UVEpsilon))
  313. return true;
  314. }
  315. }
  316. UINT32 numBlendShapes = (UINT32)meshA.blendShapes.size();
  317. for (UINT32 i = 0; i < numBlendShapes; i++)
  318. {
  319. const FBXBlendShape& shapeA = meshA.blendShapes[i];
  320. const FBXBlendShape& shapeB = meshB.blendShapes[i];
  321. UINT32 numFrames = (UINT32)shapeA.frames.size();
  322. for (UINT32 j = 0; j < numFrames; j++)
  323. {
  324. const FBXBlendShapeFrame& frameA = shapeA.frames[j];
  325. const FBXBlendShapeFrame& frameB = shapeB.frames[j];
  326. if (!frameA.normals.empty())
  327. {
  328. float angleCosine = frameA.normals[idxA].dot(frameB.normals[idxB]);
  329. if (angleCosine < SplitAngleCosine)
  330. return true;
  331. }
  332. if (!frameA.tangents.empty())
  333. {
  334. float angleCosine = frameA.tangents[idxA].dot(frameB.tangents[idxB]);
  335. if (angleCosine < SplitAngleCosine)
  336. return true;
  337. }
  338. if (!frameA.bitangents.empty())
  339. {
  340. float angleCosine = frameA.bitangents[idxA].dot(frameB.bitangents[idxB]);
  341. if (angleCosine < SplitAngleCosine)
  342. return true;
  343. }
  344. }
  345. }
  346. return false;
  347. }
  348. }