SceneGraphUtilities.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/Matrix3x4.h>
  9. #include <AzCore/std/algorithm.h>
  10. #include <AzCore/std/typetraits/is_base_of.h>
  11. #include <SceneAPI/SceneCore/Containers/Scene.h>
  12. #include <SceneAPI/SceneCore/Containers/SceneGraph.h>
  13. #include <SceneAPI/SceneCore/Containers/Views/FilterIterator.h>
  14. #include <SceneAPI/SceneCore/Containers/Utilities/Filters.h>
  15. #include <SceneAPI/SceneCore/Containers/Utilities/SceneGraphUtilities.h>
  16. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphChildIterator.h>
  17. #include <SceneAPI/SceneCore/DataTypes/GraphData/ITransform.h>
  18. #include <SceneAPI/SceneCore/Events/GraphMetaInfoBus.h>
  19. namespace AZ
  20. {
  21. namespace SceneAPI
  22. {
  23. namespace Utilities
  24. {
  25. DataTypes::MatrixType BuildWorldTransform(const Containers::SceneGraph& graph, Containers::SceneGraph::NodeIndex nodeIndex)
  26. {
  27. DataTypes::MatrixType outTransform = DataTypes::MatrixType::Identity();
  28. while (nodeIndex.IsValid())
  29. {
  30. auto view = Containers::Views::MakeSceneGraphChildView<Containers::Views::AcceptEndPointsOnly>(graph, nodeIndex,
  31. graph.GetContentStorage().begin(), true);
  32. auto result = AZStd::find_if(view.begin(), view.end(), Containers::DerivedTypeFilter<DataTypes::ITransform>());
  33. if (result != view.end())
  34. {
  35. // Check if the node has any child transform node
  36. const DataTypes::MatrixType& azTransform = azrtti_cast<const DataTypes::ITransform*>(result->get())->GetMatrix();
  37. outTransform = azTransform * outTransform;
  38. }
  39. else
  40. {
  41. // Check if the node itself is a transform node.
  42. AZStd::shared_ptr<const DataTypes::ITransform> transformData = azrtti_cast<const DataTypes::ITransform*>(graph.GetNodeContent(nodeIndex));
  43. if (transformData)
  44. {
  45. outTransform = transformData->GetMatrix() * outTransform;
  46. }
  47. }
  48. if (graph.HasNodeParent(nodeIndex))
  49. {
  50. nodeIndex = graph.GetNodeParent(nodeIndex);
  51. }
  52. else
  53. {
  54. break;
  55. }
  56. }
  57. return outTransform;
  58. }
  59. Containers::SceneGraph::NodeIndex GetImmediateChildOfType(
  60. const Containers::SceneGraph& graph, const Containers::SceneGraph::NodeIndex& nodeIndex, const AZ::TypeId& typeId)
  61. {
  62. auto childIndex = graph.GetNodeChild(nodeIndex);
  63. while (childIndex.IsValid())
  64. {
  65. const auto nodeContent = graph.GetNodeContent(childIndex);
  66. if (nodeContent && azrtti_istypeof(typeId, nodeContent.get()))
  67. {
  68. break;
  69. }
  70. childIndex = graph.GetNodeSibling(childIndex);
  71. }
  72. return childIndex;
  73. }
  74. } // Utilities
  75. } // SceneAPI
  76. } // AZ