ImporterUtilities.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Math/Transform.h>
  9. #include <AzToolsFramework/Debug/TraceContext.h>
  10. #include <SceneAPI/SceneBuilder/ImportContexts/AssImpImportContexts.h>
  11. #include <SceneAPI/SceneBuilder/Importers/ImporterUtilities.h>
  12. #include <SceneAPI/SceneCore/Containers/Scene.h>
  13. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  14. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
  15. #include <SceneAPI/SceneData/GraphData/AnimationData.h>
  16. #include <SceneAPI/SceneData/GraphData/BoneData.h>
  17. #include <SceneAPI/SceneData/GraphData/MaterialData.h>
  18. #include <SceneAPI/SceneData/GraphData/MeshData.h>
  19. #include <SceneAPI/SceneData/GraphData/MeshVertexColorData.h>
  20. #include <SceneAPI/SceneData/GraphData/MeshVertexUVData.h>
  21. #include <SceneAPI/SceneData/GraphData/SkinWeightData.h>
  22. #include <SceneAPI/SceneData/GraphData/TransformData.h>
  23. namespace AZ
  24. {
  25. namespace SceneAPI
  26. {
  27. namespace SceneBuilder
  28. {
  29. static const float g_sceneUtilityEqualityEpsilon = 0.001f;
  30. CoreProcessingResult AddDataNodeWithContexts(SceneDataPopulatedContextBase& dataPopulated)
  31. {
  32. AZ_TraceContext("Node Name", dataPopulated.m_dataName);
  33. const char* nodeTypeName = dataPopulated.m_graphData ? dataPopulated.m_graphData->RTTI_GetTypeName() : "Null";
  34. AZ_TraceContext("Node Type", (!nodeTypeName || nodeTypeName[0] == '\0' ? "Null" : nodeTypeName));
  35. Events::ProcessingResultCombiner nodeResults;
  36. nodeResults += Events::Process(dataPopulated);
  37. dataPopulated.m_scene.GetGraph().SetContent(dataPopulated.m_currentGraphPosition,
  38. AZStd::move(dataPopulated.m_graphData));
  39. if (azrtti_istypeof<AssImpSceneDataPopulatedContext>(dataPopulated))
  40. {
  41. AssImpSceneDataPopulatedContext* dataPopulatedContext = azrtti_cast<AssImpSceneDataPopulatedContext*>(&dataPopulated);
  42. AssImpSceneNodeAppendedContext nodeAppended(*dataPopulatedContext, dataPopulated.m_currentGraphPosition);
  43. nodeResults += Events::Process(nodeAppended);
  44. AssImpSceneNodeAddedAttributesContext addedAttributes(nodeAppended);
  45. nodeResults += Events::Process(addedAttributes);
  46. AssImpSceneNodeFinalizeContext finalizeNode(addedAttributes);
  47. nodeResults += Events::Process(finalizeNode);
  48. }
  49. return nodeResults.GetResult();
  50. }
  51. CoreProcessingResult AddAttributeDataNodeWithContexts(SceneAttributeDataPopulatedContextBase& dataPopulated)
  52. {
  53. AZ_TraceContext("Node Name", dataPopulated.m_dataName);
  54. const char* nodeTypeName = dataPopulated.m_graphData ? dataPopulated.m_graphData->RTTI_GetTypeName() : "Null";
  55. AZ_TraceContext("Node Type", (!nodeTypeName || nodeTypeName[0] == '\0' ? "Null" : nodeTypeName));
  56. Events::ProcessingResultCombiner nodeResults;
  57. nodeResults += Events::Process(dataPopulated);
  58. dataPopulated.m_scene.GetGraph().MakeEndPoint(dataPopulated.m_currentGraphPosition);
  59. dataPopulated.m_scene.GetGraph().SetContent(dataPopulated.m_currentGraphPosition,
  60. AZStd::move(dataPopulated.m_graphData));
  61. if (azrtti_istypeof<AssImpSceneAttributeDataPopulatedContext>(dataPopulated))
  62. {
  63. AssImpSceneAttributeDataPopulatedContext* dataPopulatedContext = azrtti_cast<AssImpSceneAttributeDataPopulatedContext*>(&dataPopulated);
  64. AssImpSceneAttributeNodeAppendedContext nodeAppended(*dataPopulatedContext, dataPopulated.m_currentGraphPosition);
  65. nodeResults += Events::Process(nodeAppended);
  66. }
  67. return nodeResults.GetResult();
  68. }
  69. bool AreSceneGraphsEqual(const CoreSceneGraph& lhsGraph, const CoreSceneGraph& rhsGraph)
  70. {
  71. auto lhsContentStorage = lhsGraph.GetContentStorage();
  72. auto lhsNameStorage = lhsGraph.GetNameStorage();
  73. auto lhsNameContentView = Containers::Views::MakePairView(lhsNameStorage, lhsContentStorage);
  74. Containers::SceneGraph::NodeIndex lhsRootIndex = lhsGraph.GetRoot();
  75. auto lhsDownwardView =
  76. Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(lhsGraph, lhsRootIndex,
  77. lhsNameContentView.begin(), true);
  78. auto rhsContentStorage = rhsGraph.GetContentStorage();
  79. auto rhsNameStorage = rhsGraph.GetNameStorage();
  80. auto rhsNameContentView = Containers::Views::MakePairView(rhsNameStorage, rhsContentStorage);
  81. Containers::SceneGraph::NodeIndex rhsRootIndex = rhsGraph.GetRoot();
  82. auto rhsDownwardView =
  83. Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(rhsGraph, rhsRootIndex,
  84. rhsNameContentView.begin(), true);
  85. auto lhsIt = lhsDownwardView.begin();
  86. auto rhsIt = rhsDownwardView.begin();
  87. while (lhsIt != lhsDownwardView.end() && rhsIt != rhsDownwardView.end())
  88. {
  89. if (!IsGraphDataEqual(lhsIt->second, rhsIt->second))
  90. {
  91. return false;
  92. }
  93. if (lhsIt->first != rhsIt->first)
  94. {
  95. return false;
  96. }
  97. ++lhsIt;
  98. ++rhsIt;
  99. }
  100. return (lhsIt == lhsDownwardView.end() && rhsIt == rhsDownwardView.end());
  101. }
  102. bool operator==(const SceneData::GraphData::MeshData& lhs,
  103. const SceneData::GraphData::MeshData& rhs)
  104. {
  105. if (lhs.GetVertexCount() != rhs.GetVertexCount())
  106. {
  107. return false;
  108. }
  109. if (lhs.HasNormalData() != rhs.HasNormalData())
  110. {
  111. return false;
  112. }
  113. if (lhs.GetFaceCount() != rhs.GetFaceCount())
  114. {
  115. return false;
  116. }
  117. bool hasNormals = lhs.HasNormalData();
  118. unsigned int vertexCount = lhs.GetVertexCount();
  119. for (unsigned int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  120. {
  121. if (lhs.GetPosition(vertexIndex) != rhs.GetPosition(vertexIndex))
  122. {
  123. return false;
  124. }
  125. if (hasNormals && (lhs.GetNormal(vertexIndex) != rhs.GetNormal(vertexIndex)))
  126. {
  127. return false;
  128. }
  129. }
  130. unsigned int faceCount = lhs.GetFaceCount();
  131. for (unsigned int faceIndex = 0; faceIndex < faceCount; ++faceIndex)
  132. {
  133. if (lhs.GetFaceMaterialId(faceIndex) != rhs.GetFaceMaterialId(faceIndex))
  134. {
  135. return false;
  136. }
  137. if (lhs.GetFaceInfo(faceIndex) != rhs.GetFaceInfo(faceIndex))
  138. {
  139. return false;
  140. }
  141. }
  142. return true;
  143. }
  144. bool operator==(const SceneData::GraphData::SkinWeightData& lhs,
  145. const SceneData::GraphData::SkinWeightData& rhs)
  146. {
  147. if (lhs.GetVertexCount() != rhs.GetVertexCount())
  148. {
  149. return false;
  150. }
  151. if (lhs.GetBoneCount() != rhs.GetBoneCount())
  152. {
  153. return false;
  154. }
  155. size_t vertexCount = lhs.GetVertexCount();
  156. for (size_t vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
  157. {
  158. if (lhs.GetLinkCount(vertexIndex) != rhs.GetLinkCount(vertexIndex))
  159. {
  160. return false;
  161. }
  162. size_t linkCount = lhs.GetLinkCount(vertexIndex);
  163. for (size_t linkIndex = 0; linkIndex < linkCount; ++linkIndex)
  164. {
  165. const DataTypes::ISkinWeightData::Link lhsLink = lhs.GetLink(vertexIndex, linkIndex);
  166. const DataTypes::ISkinWeightData::Link rhsLink = rhs.GetLink(vertexIndex, linkIndex);
  167. if (lhsLink.boneId != rhsLink.boneId || !IsClose(lhsLink.weight, rhsLink.weight, g_sceneUtilityEqualityEpsilon))
  168. {
  169. return false;
  170. }
  171. if (lhs.GetBoneName(lhsLink.boneId) != rhs.GetBoneName(rhsLink.boneId))
  172. {
  173. return false;
  174. }
  175. }
  176. }
  177. return true;
  178. }
  179. bool operator==(const SceneData::GraphData::BoneData& lhs,
  180. const SceneData::GraphData::BoneData& rhs)
  181. {
  182. return (lhs.GetWorldTransform() == rhs.GetWorldTransform());
  183. }
  184. bool operator==(const DataTypes::Color& lhs, const DataTypes::Color& rhs)
  185. {
  186. if (!IsClose(lhs.alpha, rhs.alpha, g_sceneUtilityEqualityEpsilon) ||
  187. !IsClose(lhs.blue, rhs.blue, g_sceneUtilityEqualityEpsilon) ||
  188. !IsClose(lhs.green, rhs.green, g_sceneUtilityEqualityEpsilon) ||
  189. !IsClose(lhs.red, rhs.red, g_sceneUtilityEqualityEpsilon))
  190. {
  191. return false;
  192. }
  193. return true;
  194. }
  195. bool operator!=(const DataTypes::Color& lhs, const DataTypes::Color& rhs)
  196. {
  197. return !(lhs == rhs);
  198. }
  199. bool operator==(const SceneData::GraphData::MeshVertexColorData& lhs,
  200. const SceneData::GraphData::MeshVertexColorData& rhs)
  201. {
  202. if (lhs.GetCount() != rhs.GetCount())
  203. {
  204. return false;
  205. }
  206. size_t colorCount = lhs.GetCount();
  207. for (size_t colorIndex = 0; colorIndex < colorCount; ++colorIndex)
  208. {
  209. if (lhs.GetColor(colorIndex) != rhs.GetColor(colorIndex))
  210. {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }
  216. bool operator==(const SceneData::GraphData::MeshVertexUVData& lhs,
  217. const SceneData::GraphData::MeshVertexUVData& rhs)
  218. {
  219. if (lhs.GetCount() != rhs.GetCount())
  220. {
  221. return false;
  222. }
  223. size_t uvCount = lhs.GetCount();
  224. for (size_t uvIndex = 0; uvIndex < uvCount; ++uvIndex)
  225. {
  226. if (lhs.GetUV(uvIndex) != rhs.GetUV(uvIndex))
  227. {
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. bool operator==(const SceneData::GraphData::MaterialData& lhs,
  234. const SceneData::GraphData::MaterialData& rhs)
  235. {
  236. if (lhs.IsNoDraw() != rhs.IsNoDraw())
  237. {
  238. return false;
  239. }
  240. if (lhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Diffuse) !=
  241. rhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Diffuse))
  242. {
  243. return false;
  244. }
  245. if (lhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Specular) !=
  246. rhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Specular))
  247. {
  248. return false;
  249. }
  250. if (lhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Bump) !=
  251. rhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Bump))
  252. {
  253. return false;
  254. }
  255. return true;
  256. }
  257. bool operator==(const SceneData::GraphData::TransformData& lhs,
  258. const SceneData::GraphData::TransformData& rhs)
  259. {
  260. return lhs.GetMatrix() == rhs.GetMatrix();
  261. }
  262. bool operator==(const SceneData::GraphData::AnimationData& lhs,
  263. const SceneData::GraphData::AnimationData& rhs)
  264. {
  265. if (lhs.GetKeyFrameCount() != rhs.GetKeyFrameCount())
  266. {
  267. return false;
  268. }
  269. size_t keyFrameCount = lhs.GetKeyFrameCount();
  270. for (size_t keyFrameIndex = 0; keyFrameIndex < keyFrameCount; ++keyFrameIndex)
  271. {
  272. if (lhs.GetKeyFrame(keyFrameIndex) != rhs.GetKeyFrame(keyFrameIndex))
  273. {
  274. return false;
  275. }
  276. }
  277. return true;
  278. }
  279. bool IsGraphDataEqual(const AZStd::shared_ptr<const DataTypes::IGraphObject>& lhs,
  280. const AZStd::shared_ptr<const DataTypes::IGraphObject>& rhs)
  281. {
  282. // If both are null, they are considered equal
  283. if (!lhs && !rhs)
  284. {
  285. return true;
  286. }
  287. // If only one is null, they are considered not equal
  288. if (!lhs || !rhs)
  289. {
  290. return false;
  291. }
  292. // If they have disparate types they are considered not equal
  293. if (lhs->RTTI_GetType() != rhs->RTTI_GetType())
  294. {
  295. return false;
  296. }
  297. if (lhs->RTTI_IsTypeOf(SceneData::GraphData::BoneData::TYPEINFO_Uuid()))
  298. {
  299. const SceneData::GraphData::BoneData* lhsBone =
  300. azrtti_cast<const SceneData::GraphData::BoneData*>(lhs.get());
  301. const SceneData::GraphData::BoneData* rhsBone =
  302. azrtti_cast<const SceneData::GraphData::BoneData*>(rhs.get());
  303. return (*lhsBone == *rhsBone);
  304. }
  305. else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::MeshData::TYPEINFO_Uuid()))
  306. {
  307. const SceneData::GraphData::MeshData* lhsMesh =
  308. azrtti_cast<const SceneData::GraphData::MeshData*>(lhs.get());
  309. const SceneData::GraphData::MeshData* rhsMesh =
  310. azrtti_cast<const SceneData::GraphData::MeshData*>(rhs.get());
  311. return (*lhsMesh == *rhsMesh);
  312. }
  313. else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::SkinWeightData::TYPEINFO_Uuid()))
  314. {
  315. const SceneData::GraphData::SkinWeightData* lhsSkinWeights =
  316. azrtti_cast<const SceneData::GraphData::SkinWeightData*>(lhs.get());
  317. const SceneData::GraphData::SkinWeightData* rhsSkinWeights =
  318. azrtti_cast<const SceneData::GraphData::SkinWeightData*>(rhs.get());
  319. return (*lhsSkinWeights == *rhsSkinWeights);
  320. }
  321. else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::MeshVertexColorData::TYPEINFO_Uuid()))
  322. {
  323. const SceneData::GraphData::MeshVertexColorData* lhsColorData =
  324. azrtti_cast<const SceneData::GraphData::MeshVertexColorData*>(lhs.get());
  325. const SceneData::GraphData::MeshVertexColorData* rhsColorData =
  326. azrtti_cast<const SceneData::GraphData::MeshVertexColorData*>(rhs.get());
  327. return (*lhsColorData == *rhsColorData);
  328. }
  329. else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::MeshVertexUVData::TYPEINFO_Uuid()))
  330. {
  331. const SceneData::GraphData::MeshVertexUVData* lhsUVData =
  332. azrtti_cast<const SceneData::GraphData::MeshVertexUVData*>(lhs.get());
  333. const SceneData::GraphData::MeshVertexUVData* rhsUVData =
  334. azrtti_cast<const SceneData::GraphData::MeshVertexUVData*>(rhs.get());
  335. return (*lhsUVData == *rhsUVData);
  336. }
  337. else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::MaterialData::TYPEINFO_Uuid()))
  338. {
  339. const SceneData::GraphData::MaterialData* lhsMaterialData =
  340. azrtti_cast<const SceneData::GraphData::MaterialData*>(lhs.get());
  341. const SceneData::GraphData::MaterialData* rhsMaterialData =
  342. azrtti_cast<const SceneData::GraphData::MaterialData*>(rhs.get());
  343. return (*lhsMaterialData == *rhsMaterialData);
  344. }
  345. else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::TransformData::TYPEINFO_Uuid()))
  346. {
  347. const SceneData::GraphData::TransformData* lhsTransform =
  348. azrtti_cast<const SceneData::GraphData::TransformData*>(lhs.get());
  349. const SceneData::GraphData::TransformData* rhsTransform =
  350. azrtti_cast<const SceneData::GraphData::TransformData*>(rhs.get());
  351. return (*lhsTransform == *rhsTransform);
  352. }
  353. else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::AnimationData::TYPEINFO_Uuid()))
  354. {
  355. const SceneData::GraphData::AnimationData* lhsAnimation =
  356. azrtti_cast<const SceneData::GraphData::AnimationData*>(lhs.get());
  357. const SceneData::GraphData::AnimationData* rhsAnimation =
  358. azrtti_cast<const SceneData::GraphData::AnimationData*>(rhs.get());
  359. return (*lhsAnimation == *rhsAnimation);
  360. }
  361. return true;
  362. }
  363. } // namespace SceneBuilder
  364. } // namespace SceneAPI
  365. } // namespace AZ