123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AzCore/Math/Transform.h>
- #include <AzToolsFramework/Debug/TraceContext.h>
- #include <SceneAPI/SceneBuilder/ImportContexts/AssImpImportContexts.h>
- #include <SceneAPI/SceneBuilder/Importers/ImporterUtilities.h>
- #include <SceneAPI/SceneCore/Containers/Scene.h>
- #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
- #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
- #include <SceneAPI/SceneData/GraphData/AnimationData.h>
- #include <SceneAPI/SceneData/GraphData/BoneData.h>
- #include <SceneAPI/SceneData/GraphData/MaterialData.h>
- #include <SceneAPI/SceneData/GraphData/MeshData.h>
- #include <SceneAPI/SceneData/GraphData/MeshVertexColorData.h>
- #include <SceneAPI/SceneData/GraphData/MeshVertexUVData.h>
- #include <SceneAPI/SceneData/GraphData/SkinWeightData.h>
- #include <SceneAPI/SceneData/GraphData/TransformData.h>
- namespace AZ
- {
- namespace SceneAPI
- {
- namespace SceneBuilder
- {
- static const float g_sceneUtilityEqualityEpsilon = 0.001f;
- CoreProcessingResult AddDataNodeWithContexts(SceneDataPopulatedContextBase& dataPopulated)
- {
- AZ_TraceContext("Node Name", dataPopulated.m_dataName);
- const char* nodeTypeName = dataPopulated.m_graphData ? dataPopulated.m_graphData->RTTI_GetTypeName() : "Null";
- AZ_TraceContext("Node Type", (!nodeTypeName || nodeTypeName[0] == '\0' ? "Null" : nodeTypeName));
-
- Events::ProcessingResultCombiner nodeResults;
- nodeResults += Events::Process(dataPopulated);
- dataPopulated.m_scene.GetGraph().SetContent(dataPopulated.m_currentGraphPosition,
- AZStd::move(dataPopulated.m_graphData));
- if (azrtti_istypeof<AssImpSceneDataPopulatedContext>(dataPopulated))
- {
- AssImpSceneDataPopulatedContext* dataPopulatedContext = azrtti_cast<AssImpSceneDataPopulatedContext*>(&dataPopulated);
- AssImpSceneNodeAppendedContext nodeAppended(*dataPopulatedContext, dataPopulated.m_currentGraphPosition);
- nodeResults += Events::Process(nodeAppended);
- AssImpSceneNodeAddedAttributesContext addedAttributes(nodeAppended);
- nodeResults += Events::Process(addedAttributes);
- AssImpSceneNodeFinalizeContext finalizeNode(addedAttributes);
- nodeResults += Events::Process(finalizeNode);
- }
- return nodeResults.GetResult();
- }
- CoreProcessingResult AddAttributeDataNodeWithContexts(SceneAttributeDataPopulatedContextBase& dataPopulated)
- {
- AZ_TraceContext("Node Name", dataPopulated.m_dataName);
- const char* nodeTypeName = dataPopulated.m_graphData ? dataPopulated.m_graphData->RTTI_GetTypeName() : "Null";
- AZ_TraceContext("Node Type", (!nodeTypeName || nodeTypeName[0] == '\0' ? "Null" : nodeTypeName));
- Events::ProcessingResultCombiner nodeResults;
- nodeResults += Events::Process(dataPopulated);
- dataPopulated.m_scene.GetGraph().MakeEndPoint(dataPopulated.m_currentGraphPosition);
- dataPopulated.m_scene.GetGraph().SetContent(dataPopulated.m_currentGraphPosition,
- AZStd::move(dataPopulated.m_graphData));
- if (azrtti_istypeof<AssImpSceneAttributeDataPopulatedContext>(dataPopulated))
- {
- AssImpSceneAttributeDataPopulatedContext* dataPopulatedContext = azrtti_cast<AssImpSceneAttributeDataPopulatedContext*>(&dataPopulated);
- AssImpSceneAttributeNodeAppendedContext nodeAppended(*dataPopulatedContext, dataPopulated.m_currentGraphPosition);
- nodeResults += Events::Process(nodeAppended);
- }
- return nodeResults.GetResult();
- }
- bool AreSceneGraphsEqual(const CoreSceneGraph& lhsGraph, const CoreSceneGraph& rhsGraph)
- {
- auto lhsContentStorage = lhsGraph.GetContentStorage();
- auto lhsNameStorage = lhsGraph.GetNameStorage();
-
- auto lhsNameContentView = Containers::Views::MakePairView(lhsNameStorage, lhsContentStorage);
- Containers::SceneGraph::NodeIndex lhsRootIndex = lhsGraph.GetRoot();
- auto lhsDownwardView =
- Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(lhsGraph, lhsRootIndex,
- lhsNameContentView.begin(), true);
- auto rhsContentStorage = rhsGraph.GetContentStorage();
- auto rhsNameStorage = rhsGraph.GetNameStorage();
-
- auto rhsNameContentView = Containers::Views::MakePairView(rhsNameStorage, rhsContentStorage);
- Containers::SceneGraph::NodeIndex rhsRootIndex = rhsGraph.GetRoot();
- auto rhsDownwardView =
- Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(rhsGraph, rhsRootIndex,
- rhsNameContentView.begin(), true);
- auto lhsIt = lhsDownwardView.begin();
- auto rhsIt = rhsDownwardView.begin();
- while (lhsIt != lhsDownwardView.end() && rhsIt != rhsDownwardView.end())
- {
- if (!IsGraphDataEqual(lhsIt->second, rhsIt->second))
- {
- return false;
- }
- if (lhsIt->first != rhsIt->first)
- {
- return false;
- }
- ++lhsIt;
- ++rhsIt;
- }
- return (lhsIt == lhsDownwardView.end() && rhsIt == rhsDownwardView.end());
- }
-
- bool operator==(const SceneData::GraphData::MeshData& lhs,
- const SceneData::GraphData::MeshData& rhs)
- {
- if (lhs.GetVertexCount() != rhs.GetVertexCount())
- {
- return false;
- }
- if (lhs.HasNormalData() != rhs.HasNormalData())
- {
- return false;
- }
- if (lhs.GetFaceCount() != rhs.GetFaceCount())
- {
- return false;
- }
- bool hasNormals = lhs.HasNormalData();
- unsigned int vertexCount = lhs.GetVertexCount();
- for (unsigned int vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
- {
- if (lhs.GetPosition(vertexIndex) != rhs.GetPosition(vertexIndex))
- {
- return false;
- }
- if (hasNormals && (lhs.GetNormal(vertexIndex) != rhs.GetNormal(vertexIndex)))
- {
- return false;
- }
- }
- unsigned int faceCount = lhs.GetFaceCount();
- for (unsigned int faceIndex = 0; faceIndex < faceCount; ++faceIndex)
- {
- if (lhs.GetFaceMaterialId(faceIndex) != rhs.GetFaceMaterialId(faceIndex))
- {
- return false;
- }
- if (lhs.GetFaceInfo(faceIndex) != rhs.GetFaceInfo(faceIndex))
- {
- return false;
- }
- }
- return true;
- }
- bool operator==(const SceneData::GraphData::SkinWeightData& lhs,
- const SceneData::GraphData::SkinWeightData& rhs)
- {
- if (lhs.GetVertexCount() != rhs.GetVertexCount())
- {
- return false;
- }
- if (lhs.GetBoneCount() != rhs.GetBoneCount())
- {
- return false;
- }
- size_t vertexCount = lhs.GetVertexCount();
- for (size_t vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex)
- {
- if (lhs.GetLinkCount(vertexIndex) != rhs.GetLinkCount(vertexIndex))
- {
- return false;
- }
- size_t linkCount = lhs.GetLinkCount(vertexIndex);
- for (size_t linkIndex = 0; linkIndex < linkCount; ++linkIndex)
- {
- const DataTypes::ISkinWeightData::Link lhsLink = lhs.GetLink(vertexIndex, linkIndex);
- const DataTypes::ISkinWeightData::Link rhsLink = rhs.GetLink(vertexIndex, linkIndex);
- if (lhsLink.boneId != rhsLink.boneId || !IsClose(lhsLink.weight, rhsLink.weight, g_sceneUtilityEqualityEpsilon))
- {
- return false;
- }
- if (lhs.GetBoneName(lhsLink.boneId) != rhs.GetBoneName(rhsLink.boneId))
- {
- return false;
- }
- }
- }
- return true;
- }
- bool operator==(const SceneData::GraphData::BoneData& lhs,
- const SceneData::GraphData::BoneData& rhs)
- {
- return (lhs.GetWorldTransform() == rhs.GetWorldTransform());
- }
- bool operator==(const DataTypes::Color& lhs, const DataTypes::Color& rhs)
- {
- if (!IsClose(lhs.alpha, rhs.alpha, g_sceneUtilityEqualityEpsilon) ||
- !IsClose(lhs.blue, rhs.blue, g_sceneUtilityEqualityEpsilon) ||
- !IsClose(lhs.green, rhs.green, g_sceneUtilityEqualityEpsilon) ||
- !IsClose(lhs.red, rhs.red, g_sceneUtilityEqualityEpsilon))
- {
- return false;
- }
- return true;
- }
- bool operator!=(const DataTypes::Color& lhs, const DataTypes::Color& rhs)
- {
- return !(lhs == rhs);
- }
- bool operator==(const SceneData::GraphData::MeshVertexColorData& lhs,
- const SceneData::GraphData::MeshVertexColorData& rhs)
- {
- if (lhs.GetCount() != rhs.GetCount())
- {
- return false;
- }
- size_t colorCount = lhs.GetCount();
- for (size_t colorIndex = 0; colorIndex < colorCount; ++colorIndex)
- {
- if (lhs.GetColor(colorIndex) != rhs.GetColor(colorIndex))
- {
- return false;
- }
- }
- return true;
- }
- bool operator==(const SceneData::GraphData::MeshVertexUVData& lhs,
- const SceneData::GraphData::MeshVertexUVData& rhs)
- {
- if (lhs.GetCount() != rhs.GetCount())
- {
- return false;
- }
- size_t uvCount = lhs.GetCount();
- for (size_t uvIndex = 0; uvIndex < uvCount; ++uvIndex)
- {
- if (lhs.GetUV(uvIndex) != rhs.GetUV(uvIndex))
- {
- return false;
- }
- }
- return true;
- }
- bool operator==(const SceneData::GraphData::MaterialData& lhs,
- const SceneData::GraphData::MaterialData& rhs)
- {
- if (lhs.IsNoDraw() != rhs.IsNoDraw())
- {
- return false;
- }
- if (lhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Diffuse) !=
- rhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Diffuse))
- {
- return false;
- }
- if (lhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Specular) !=
- rhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Specular))
- {
- return false;
- }
- if (lhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Bump) !=
- rhs.GetTexture(DataTypes::IMaterialData::TextureMapType::Bump))
- {
- return false;
- }
- return true;
- }
- bool operator==(const SceneData::GraphData::TransformData& lhs,
- const SceneData::GraphData::TransformData& rhs)
- {
- return lhs.GetMatrix() == rhs.GetMatrix();
- }
- bool operator==(const SceneData::GraphData::AnimationData& lhs,
- const SceneData::GraphData::AnimationData& rhs)
- {
- if (lhs.GetKeyFrameCount() != rhs.GetKeyFrameCount())
- {
- return false;
- }
- size_t keyFrameCount = lhs.GetKeyFrameCount();
-
- for (size_t keyFrameIndex = 0; keyFrameIndex < keyFrameCount; ++keyFrameIndex)
- {
- if (lhs.GetKeyFrame(keyFrameIndex) != rhs.GetKeyFrame(keyFrameIndex))
- {
- return false;
- }
- }
- return true;
- }
- bool IsGraphDataEqual(const AZStd::shared_ptr<const DataTypes::IGraphObject>& lhs,
- const AZStd::shared_ptr<const DataTypes::IGraphObject>& rhs)
- {
- // If both are null, they are considered equal
- if (!lhs && !rhs)
- {
- return true;
- }
- // If only one is null, they are considered not equal
- if (!lhs || !rhs)
- {
- return false;
- }
- // If they have disparate types they are considered not equal
- if (lhs->RTTI_GetType() != rhs->RTTI_GetType())
- {
- return false;
- }
- if (lhs->RTTI_IsTypeOf(SceneData::GraphData::BoneData::TYPEINFO_Uuid()))
- {
- const SceneData::GraphData::BoneData* lhsBone =
- azrtti_cast<const SceneData::GraphData::BoneData*>(lhs.get());
- const SceneData::GraphData::BoneData* rhsBone =
- azrtti_cast<const SceneData::GraphData::BoneData*>(rhs.get());
- return (*lhsBone == *rhsBone);
- }
- else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::MeshData::TYPEINFO_Uuid()))
- {
- const SceneData::GraphData::MeshData* lhsMesh =
- azrtti_cast<const SceneData::GraphData::MeshData*>(lhs.get());
- const SceneData::GraphData::MeshData* rhsMesh =
- azrtti_cast<const SceneData::GraphData::MeshData*>(rhs.get());
- return (*lhsMesh == *rhsMesh);
- }
- else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::SkinWeightData::TYPEINFO_Uuid()))
- {
- const SceneData::GraphData::SkinWeightData* lhsSkinWeights =
- azrtti_cast<const SceneData::GraphData::SkinWeightData*>(lhs.get());
- const SceneData::GraphData::SkinWeightData* rhsSkinWeights =
- azrtti_cast<const SceneData::GraphData::SkinWeightData*>(rhs.get());
- return (*lhsSkinWeights == *rhsSkinWeights);
- }
- else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::MeshVertexColorData::TYPEINFO_Uuid()))
- {
- const SceneData::GraphData::MeshVertexColorData* lhsColorData =
- azrtti_cast<const SceneData::GraphData::MeshVertexColorData*>(lhs.get());
- const SceneData::GraphData::MeshVertexColorData* rhsColorData =
- azrtti_cast<const SceneData::GraphData::MeshVertexColorData*>(rhs.get());
- return (*lhsColorData == *rhsColorData);
- }
- else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::MeshVertexUVData::TYPEINFO_Uuid()))
- {
- const SceneData::GraphData::MeshVertexUVData* lhsUVData =
- azrtti_cast<const SceneData::GraphData::MeshVertexUVData*>(lhs.get());
- const SceneData::GraphData::MeshVertexUVData* rhsUVData =
- azrtti_cast<const SceneData::GraphData::MeshVertexUVData*>(rhs.get());
- return (*lhsUVData == *rhsUVData);
- }
- else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::MaterialData::TYPEINFO_Uuid()))
- {
- const SceneData::GraphData::MaterialData* lhsMaterialData =
- azrtti_cast<const SceneData::GraphData::MaterialData*>(lhs.get());
- const SceneData::GraphData::MaterialData* rhsMaterialData =
- azrtti_cast<const SceneData::GraphData::MaterialData*>(rhs.get());
- return (*lhsMaterialData == *rhsMaterialData);
- }
- else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::TransformData::TYPEINFO_Uuid()))
- {
- const SceneData::GraphData::TransformData* lhsTransform =
- azrtti_cast<const SceneData::GraphData::TransformData*>(lhs.get());
- const SceneData::GraphData::TransformData* rhsTransform =
- azrtti_cast<const SceneData::GraphData::TransformData*>(rhs.get());
- return (*lhsTransform == *rhsTransform);
- }
- else if (lhs->RTTI_IsTypeOf(SceneData::GraphData::AnimationData::TYPEINFO_Uuid()))
- {
- const SceneData::GraphData::AnimationData* lhsAnimation =
- azrtti_cast<const SceneData::GraphData::AnimationData*>(lhs.get());
- const SceneData::GraphData::AnimationData* rhsAnimation =
- azrtti_cast<const SceneData::GraphData::AnimationData*>(rhs.get());
- return (*lhsAnimation == *rhsAnimation);
- }
- return true;
- }
- } // namespace SceneBuilder
- } // namespace SceneAPI
- } // namespace AZ
|