BsFBXUtility.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "BsFBXUtility.h"
  2. #include "BsVector2.h"
  3. #include "BsVector3.h"
  4. #include "BsVector4.h"
  5. namespace BansheeEngine
  6. {
  7. struct SmoothNormal
  8. {
  9. int group = 0;
  10. Vector3 normal;
  11. void addNormal(int group, const Vector3& normal)
  12. {
  13. this->group |= group;
  14. this->normal += normal;
  15. }
  16. void addNormal(const SmoothNormal& other)
  17. {
  18. this->group |= other.group;
  19. this->normal += other.normal;
  20. }
  21. void normalize()
  22. {
  23. normal.normalize();
  24. }
  25. };
  26. struct SmoothVertex
  27. {
  28. Vector<SmoothNormal> normals;
  29. void addNormal(int group, const Vector3& normal)
  30. {
  31. bool found = false;
  32. for (size_t i = 0; i < normals.size(); i++)
  33. {
  34. if ((normals[i].group & group) != 0)
  35. {
  36. bool otherGroups = (group & ~normals[i].group) != 0;
  37. if (otherGroups)
  38. {
  39. for (size_t j = i + 1; j < normals.size(); j++)
  40. {
  41. if ((normals[j].group & group) != 0)
  42. {
  43. normals[i].addNormal(normals[j]);
  44. normals.erase(normals.begin() + j);
  45. --j;
  46. }
  47. }
  48. }
  49. normals[i].addNormal(group, normal);
  50. found = true;
  51. break;;
  52. }
  53. }
  54. if (!found)
  55. {
  56. SmoothNormal smoothNormal;
  57. smoothNormal.addNormal(group, normal);
  58. normals.push_back(smoothNormal);
  59. }
  60. }
  61. Vector3 getNormal(int group) const
  62. {
  63. for (size_t i = 0; i < normals.size(); i++)
  64. {
  65. if (normals[i].group & group)
  66. return normals[i].normal;
  67. }
  68. return Vector3::ZERO;
  69. }
  70. void normalize()
  71. {
  72. for (size_t i = 0; i < normals.size(); ++i)
  73. normals[i].normalize();
  74. }
  75. };
  76. void FBXUtility::normalsFromSmoothing(const Vector<Vector4>& positions, const Vector<int>& indices,
  77. const Vector<int>& smoothing, Vector<Vector4>& normals)
  78. {
  79. std::vector<SmoothVertex> smoothNormals;
  80. smoothNormals.resize(positions.size());
  81. normals.resize(indices.size(), Vector4::ZERO);
  82. UINT32 numPolygons = (UINT32)(indices.size() / 3);
  83. int idx = 0;
  84. for (UINT32 i = 0; i < numPolygons; i++)
  85. {
  86. for (UINT32 j = 0; j < 3; j++)
  87. {
  88. int prevOffset = (j > 0 ? j - 1 : 2);
  89. int nextOffset = (j < 2 ? j + 1 : 0);
  90. int current = indices[idx + j];
  91. Vector3 v0 = (Vector3)positions[indices[idx + prevOffset]];
  92. Vector3 v1 = (Vector3)positions[current];
  93. Vector3 v2 = (Vector3)positions[indices[idx + nextOffset]];
  94. Vector3 normal = Vector3::cross(v0 - v1, v2 - v1);
  95. smoothNormals[current].addNormal(smoothing[idx + j], normal);
  96. normals[idx + j] = normal;
  97. }
  98. idx += 3;
  99. }
  100. for (size_t i = 0; i < smoothNormals.size(); ++i)
  101. smoothNormals[i].normalize();
  102. idx = 0;
  103. for (UINT32 i = 0; i < numPolygons; i++)
  104. {
  105. for (UINT32 j = 0; j < 3; j++)
  106. {
  107. if (smoothing[idx + i] != 0)
  108. {
  109. int current = indices[idx + i];
  110. normals[idx + i] = (Vector4)smoothNormals[current].getNormal(smoothing[idx + i]);
  111. }
  112. }
  113. idx += 3;
  114. }
  115. }
  116. void FBXUtility::splitVertices(const FBXImportMesh& source, FBXImportMesh& dest)
  117. {
  118. dest.indices = source.indices;
  119. dest.materials = source.materials;
  120. dest.referencedBy = source.referencedBy;
  121. dest.positions = source.positions;
  122. // Make room for minimal set of vertices
  123. UINT32 vertexCount = (UINT32)source.positions.size();
  124. if (!source.normals.empty())
  125. dest.normals.resize(vertexCount);
  126. if (!source.tangents.empty())
  127. dest.tangents.resize(vertexCount);
  128. if (!source.bitangents.empty())
  129. dest.bitangents.resize(vertexCount);
  130. if (!source.colors.empty())
  131. dest.colors.resize(vertexCount);
  132. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  133. {
  134. if (!source.UV[i].empty())
  135. dest.UV[i].resize(vertexCount);
  136. }
  137. Vector<Vector<int>> splitsPerVertex;
  138. splitsPerVertex.resize(source.positions.size());
  139. Vector<int>& indices = dest.indices;
  140. int indexCount = (int)dest.indices.size();
  141. for (int i = 0; i < indexCount; i++)
  142. {
  143. int srcVertIdx = indices[i];
  144. int dstVertIdx = -1;
  145. // See if we already processed this vertex and if its attributes
  146. // are close enough with the previous version
  147. Vector<int>& splits = splitsPerVertex[srcVertIdx];
  148. for (auto& splitVertIdx : splits)
  149. {
  150. if (!needsSplitAttributes(source, i, dest, splitVertIdx))
  151. dstVertIdx = splitVertIdx;
  152. }
  153. // We didn't find a close-enough match
  154. if (dstVertIdx == -1)
  155. {
  156. // First time we visited this vertex, so just copy over attributes
  157. if (splits.empty())
  158. {
  159. dstVertIdx = srcVertIdx;
  160. copyVertexAttributes(source, i, dest, srcVertIdx);
  161. }
  162. else // Split occurred, add a brand new vertex
  163. {
  164. dstVertIdx = (int)dest.positions.size();
  165. addVertex(source, i, srcVertIdx, dest);
  166. }
  167. splits.push_back(dstVertIdx);
  168. }
  169. indices[i] = dstVertIdx;
  170. }
  171. }
  172. void FBXUtility::flipWindingOrder(FBXImportMesh& input)
  173. {
  174. for (UINT32 i = 0; i < (UINT32)input.materials.size(); i += 3)
  175. {
  176. std::swap(input.materials[i + 1], input.materials[i + 2]);
  177. }
  178. for (UINT32 i = 0; i < (UINT32)input.indices.size(); i += 3)
  179. {
  180. std::swap(input.indices[i + 1], input.indices[i + 2]);
  181. }
  182. }
  183. void FBXUtility::copyVertexAttributes(const FBXImportMesh& srcMesh, int srcIdx, FBXImportMesh& destMesh, int dstIdx)
  184. {
  185. if (!srcMesh.normals.empty())
  186. destMesh.normals[dstIdx] = srcMesh.normals[srcIdx];
  187. if (!srcMesh.tangents.empty())
  188. destMesh.tangents[dstIdx] = srcMesh.tangents[srcIdx];
  189. if (!srcMesh.bitangents.empty())
  190. destMesh.bitangents[dstIdx] = srcMesh.bitangents[srcIdx];
  191. if (!srcMesh.colors.empty())
  192. destMesh.colors[dstIdx] = srcMesh.colors[srcIdx];
  193. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  194. {
  195. if (!srcMesh.UV[i].empty())
  196. destMesh.UV[i][dstIdx] = srcMesh.UV[i][srcIdx];
  197. }
  198. }
  199. void FBXUtility::addVertex(const FBXImportMesh& srcMesh, int srcIdx, int srcVertex, FBXImportMesh& destMesh)
  200. {
  201. destMesh.positions.push_back(srcMesh.positions[srcVertex]);
  202. if (!srcMesh.normals.empty())
  203. destMesh.normals.push_back(srcMesh.normals[srcIdx]);
  204. if (!srcMesh.tangents.empty())
  205. destMesh.tangents.push_back(srcMesh.tangents[srcIdx]);
  206. if (!srcMesh.bitangents.empty())
  207. destMesh.bitangents.push_back(srcMesh.bitangents[srcIdx]);
  208. if (!srcMesh.colors.empty())
  209. destMesh.colors.push_back(srcMesh.colors[srcIdx]);
  210. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  211. {
  212. if (!srcMesh.UV[i].empty())
  213. destMesh.UV[i].push_back(srcMesh.UV[i][srcIdx]);
  214. }
  215. }
  216. bool FBXUtility::needsSplitAttributes(const FBXImportMesh& meshA, int idxA, const FBXImportMesh& meshB, int idxB)
  217. {
  218. static const float SplitAngleCosine = Math::cos(Degree(1.0f));
  219. static const float UVEpsilon = 0.001f;
  220. if (!meshA.colors.empty())
  221. {
  222. if (meshA.colors[idxA] != meshB.colors[idxB])
  223. return true;
  224. }
  225. if (!meshA.normals.empty())
  226. {
  227. float angleCosine = meshA.normals[idxA].dot(meshB.normals[idxB]);
  228. if (angleCosine < SplitAngleCosine)
  229. return true;
  230. }
  231. if (!meshA.tangents.empty())
  232. {
  233. float angleCosine = meshA.tangents[idxA].dot(meshB.tangents[idxB]);
  234. if (angleCosine < SplitAngleCosine)
  235. return true;
  236. }
  237. if (!meshA.bitangents.empty())
  238. {
  239. float angleCosine = meshA.bitangents[idxA].dot(meshB.bitangents[idxB]);
  240. if (angleCosine < SplitAngleCosine)
  241. return true;
  242. }
  243. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  244. {
  245. if (!meshA.UV[i].empty())
  246. {
  247. if (!Math::approxEquals(meshA.UV[i][idxA], meshB.UV[i][idxB], UVEpsilon))
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. }