BsFBXUtility.cpp 11 KB

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