BsFBXUtility.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsFBXUtility.h"
  4. #include "BsVector2.h"
  5. #include "BsVector3.h"
  6. #include "BsVector4.h"
  7. namespace BansheeEngine
  8. {
  9. struct SmoothNormal
  10. {
  11. int group = 0;
  12. Vector3 normal;
  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.boneInfluences = source.boneInfluences;
  123. dest.positions = source.positions;
  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.weight = sourceFrame.weight;
  153. destFrame.positions = sourceFrame.positions;
  154. if (!sourceFrame.normals.empty())
  155. destFrame.normals.resize(vertexCount);
  156. if (!sourceFrame.tangents.empty())
  157. destFrame.tangents.resize(vertexCount);
  158. if (!sourceFrame.bitangents.empty())
  159. destFrame.bitangents.resize(vertexCount);
  160. }
  161. }
  162. Vector<Vector<int>> splitsPerVertex;
  163. splitsPerVertex.resize(source.positions.size());
  164. Vector<int>& indices = dest.indices;
  165. int indexCount = (int)dest.indices.size();
  166. for (int i = 0; i < indexCount; i++)
  167. {
  168. int srcVertIdx = indices[i];
  169. int dstVertIdx = -1;
  170. // See if we already processed this vertex and if its attributes
  171. // are close enough with the previous version
  172. Vector<int>& splits = splitsPerVertex[srcVertIdx];
  173. for (auto& splitVertIdx : splits)
  174. {
  175. if (!needsSplitAttributes(source, i, dest, splitVertIdx))
  176. dstVertIdx = splitVertIdx;
  177. }
  178. // We didn't find a close-enough match
  179. if (dstVertIdx == -1)
  180. {
  181. // First time we visited this vertex, so just copy over attributes
  182. if (splits.empty())
  183. {
  184. dstVertIdx = srcVertIdx;
  185. copyVertexAttributes(source, i, dest, srcVertIdx);
  186. }
  187. else // Split occurred, add a brand new vertex
  188. {
  189. dstVertIdx = (int)dest.positions.size();
  190. addVertex(source, i, srcVertIdx, dest);
  191. }
  192. splits.push_back(dstVertIdx);
  193. }
  194. indices[i] = dstVertIdx;
  195. }
  196. }
  197. void FBXUtility::flipWindingOrder(FBXImportMesh& input)
  198. {
  199. for (UINT32 i = 0; i < (UINT32)input.materials.size(); i += 3)
  200. {
  201. std::swap(input.materials[i + 1], input.materials[i + 2]);
  202. }
  203. for (UINT32 i = 0; i < (UINT32)input.indices.size(); i += 3)
  204. {
  205. std::swap(input.indices[i + 1], input.indices[i + 2]);
  206. }
  207. }
  208. void FBXUtility::copyVertexAttributes(const FBXImportMesh& srcMesh, int srcIdx, FBXImportMesh& destMesh, int dstIdx)
  209. {
  210. if (!srcMesh.normals.empty())
  211. destMesh.normals[dstIdx] = srcMesh.normals[srcIdx];
  212. if (!srcMesh.tangents.empty())
  213. destMesh.tangents[dstIdx] = srcMesh.tangents[srcIdx];
  214. if (!srcMesh.bitangents.empty())
  215. destMesh.bitangents[dstIdx] = srcMesh.bitangents[srcIdx];
  216. if (!srcMesh.colors.empty())
  217. destMesh.colors[dstIdx] = srcMesh.colors[srcIdx];
  218. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  219. {
  220. if (!srcMesh.UV[i].empty())
  221. destMesh.UV[i][dstIdx] = srcMesh.UV[i][srcIdx];
  222. }
  223. UINT32 numBlendShapes = (UINT32)srcMesh.blendShapes.size();
  224. for (UINT32 i = 0; i < numBlendShapes; i++)
  225. {
  226. const FBXBlendShape& sourceShape = srcMesh.blendShapes[i];
  227. FBXBlendShape& destShape = destMesh.blendShapes[i];
  228. UINT32 numFrames = (UINT32)sourceShape.frames.size();
  229. for (UINT32 j = 0; j < numFrames; j++)
  230. {
  231. const FBXBlendShapeFrame& sourceFrame = sourceShape.frames[j];
  232. FBXBlendShapeFrame& destFrame = destShape.frames[j];
  233. if (!sourceFrame.normals.empty())
  234. destFrame.normals[dstIdx] = sourceFrame.normals[srcIdx];
  235. if (!sourceFrame.tangents.empty())
  236. destFrame.tangents[dstIdx] = sourceFrame.tangents[srcIdx];
  237. if (!sourceFrame.bitangents.empty())
  238. destFrame.bitangents[dstIdx] = sourceFrame.bitangents[srcIdx];
  239. }
  240. }
  241. }
  242. void FBXUtility::addVertex(const FBXImportMesh& srcMesh, int srcIdx, int srcVertex, FBXImportMesh& destMesh)
  243. {
  244. destMesh.positions.push_back(srcMesh.positions[srcVertex]);
  245. if (!srcMesh.normals.empty())
  246. destMesh.normals.push_back(srcMesh.normals[srcIdx]);
  247. if (!srcMesh.tangents.empty())
  248. destMesh.tangents.push_back(srcMesh.tangents[srcIdx]);
  249. if (!srcMesh.bitangents.empty())
  250. destMesh.bitangents.push_back(srcMesh.bitangents[srcIdx]);
  251. if (!srcMesh.colors.empty())
  252. destMesh.colors.push_back(srcMesh.colors[srcIdx]);
  253. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  254. {
  255. if (!srcMesh.UV[i].empty())
  256. destMesh.UV[i].push_back(srcMesh.UV[i][srcIdx]);
  257. }
  258. UINT32 numBlendShapes = (UINT32)srcMesh.blendShapes.size();
  259. for (UINT32 i = 0; i < numBlendShapes; i++)
  260. {
  261. const FBXBlendShape& sourceShape = srcMesh.blendShapes[i];
  262. FBXBlendShape& destShape = destMesh.blendShapes[i];
  263. UINT32 numFrames = (UINT32)sourceShape.frames.size();
  264. for (UINT32 j = 0; j < numFrames; j++)
  265. {
  266. const FBXBlendShapeFrame& sourceFrame = sourceShape.frames[j];
  267. FBXBlendShapeFrame& destFrame = destShape.frames[j];
  268. destFrame.positions.push_back(sourceFrame.positions[srcVertex]);
  269. if (!sourceFrame.normals.empty())
  270. destFrame.normals.push_back(sourceFrame.normals[srcIdx]);
  271. if (!sourceFrame.tangents.empty())
  272. destFrame.tangents.push_back(sourceFrame.tangents[srcIdx]);
  273. if (!sourceFrame.bitangents.empty())
  274. destFrame.bitangents.push_back(sourceFrame.bitangents[srcIdx]);
  275. }
  276. }
  277. }
  278. bool FBXUtility::needsSplitAttributes(const FBXImportMesh& meshA, int idxA, const FBXImportMesh& meshB, int idxB)
  279. {
  280. static const float SplitAngleCosine = Math::cos(Degree(1.0f));
  281. static const float UVEpsilon = 0.001f;
  282. if (!meshA.colors.empty())
  283. {
  284. if (meshA.colors[idxA] != meshB.colors[idxB])
  285. return true;
  286. }
  287. if (!meshA.normals.empty())
  288. {
  289. float angleCosine = meshA.normals[idxA].dot(meshB.normals[idxB]);
  290. if (angleCosine < SplitAngleCosine)
  291. return true;
  292. }
  293. if (!meshA.tangents.empty())
  294. {
  295. float angleCosine = meshA.tangents[idxA].dot(meshB.tangents[idxB]);
  296. if (angleCosine < SplitAngleCosine)
  297. return true;
  298. }
  299. if (!meshA.bitangents.empty())
  300. {
  301. float angleCosine = meshA.bitangents[idxA].dot(meshB.bitangents[idxB]);
  302. if (angleCosine < SplitAngleCosine)
  303. return true;
  304. }
  305. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  306. {
  307. if (!meshA.UV[i].empty())
  308. {
  309. if (!Math::approxEquals(meshA.UV[i][idxA], meshB.UV[i][idxB], UVEpsilon))
  310. return true;
  311. }
  312. }
  313. UINT32 numBlendShapes = (UINT32)meshA.blendShapes.size();
  314. for (UINT32 i = 0; i < numBlendShapes; i++)
  315. {
  316. const FBXBlendShape& shapeA = meshA.blendShapes[i];
  317. const FBXBlendShape& shapeB = meshB.blendShapes[i];
  318. UINT32 numFrames = (UINT32)shapeA.frames.size();
  319. for (UINT32 j = 0; j < numFrames; j++)
  320. {
  321. const FBXBlendShapeFrame& frameA = shapeA.frames[j];
  322. const FBXBlendShapeFrame& frameB = shapeB.frames[j];
  323. if (!frameA.normals.empty())
  324. {
  325. float angleCosine = frameA.normals[idxA].dot(frameB.normals[idxB]);
  326. if (angleCosine < SplitAngleCosine)
  327. return true;
  328. }
  329. if (!frameA.tangents.empty())
  330. {
  331. float angleCosine = frameA.tangents[idxA].dot(frameB.tangents[idxB]);
  332. if (angleCosine < SplitAngleCosine)
  333. return true;
  334. }
  335. if (!frameA.bitangents.empty())
  336. {
  337. float angleCosine = frameA.bitangents[idxA].dot(frameB.bitangents[idxB]);
  338. if (angleCosine < SplitAngleCosine)
  339. return true;
  340. }
  341. }
  342. }
  343. return false;
  344. }
  345. }