SceneUtilities.cpp 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/Containers/Views/SceneGraphUpwardsIterator.h>
  18. #include <SceneAPI/SceneCore/DataTypes/GraphData/ITransform.h>
  19. #include <SceneAPI/SceneCore/DataTypes/Rules/ICoordinateSystemRule.h>
  20. #include <SceneAPI/SceneCore/Containers/RuleContainer.h>
  21. namespace AZ
  22. {
  23. namespace SceneAPI
  24. {
  25. namespace Utilities
  26. {
  27. DataTypes::MatrixType ConcatenateMatricesUpwards(const Containers::SceneGraph& graph, Containers::SceneGraph::NodeIndex nodeIndex)
  28. {
  29. DataTypes::MatrixType outTransform = DataTypes::MatrixType::Identity();
  30. while (nodeIndex.IsValid())
  31. {
  32. auto view = Containers::Views::MakeSceneGraphChildView<Containers::Views::AcceptEndPointsOnly>(graph, nodeIndex, graph.GetContentStorage().begin(), true);
  33. auto result = AZStd::find_if(view.begin(), view.end(), Containers::DerivedTypeFilter<DataTypes::ITransform>());
  34. if (result != view.end())
  35. {
  36. // Check if the node has any child transform node
  37. const DataTypes::MatrixType& azTransform = azrtti_cast<const DataTypes::ITransform*>(result->get())->GetMatrix();
  38. outTransform = azTransform * outTransform;
  39. }
  40. else
  41. {
  42. // Check if the node itself is a transform node.
  43. AZStd::shared_ptr<const DataTypes::ITransform> transformData = azrtti_cast<const DataTypes::ITransform*>(graph.GetNodeContent(nodeIndex));
  44. if (transformData)
  45. {
  46. outTransform = transformData->GetMatrix() * outTransform;
  47. }
  48. }
  49. if (graph.HasNodeParent(nodeIndex))
  50. {
  51. nodeIndex = graph.GetNodeParent(nodeIndex);
  52. }
  53. else
  54. {
  55. break;
  56. }
  57. }
  58. return outTransform;
  59. }
  60. SCENE_CORE_API DataTypes::MatrixType DetermineWorldTransform(const Containers::Scene& scene, const Containers::SceneGraph::NodeIndex nodeIndex, const Containers::RuleContainer& ruleContainer)
  61. {
  62. auto coordinateSystemRule = ruleContainer.FindFirstByType<DataTypes::ICoordinateSystemRule>();
  63. if (coordinateSystemRule && coordinateSystemRule->GetUseAdvancedData())
  64. {
  65. SceneAPI::DataTypes::MatrixType matrix = SceneAPI::DataTypes::MatrixType::CreateIdentity();
  66. if (coordinateSystemRule->GetTranslation() != Vector3(0.0f, 0.0f, 0.0f) || !coordinateSystemRule->GetRotation().IsIdentity())
  67. {
  68. matrix = DataTypes::MatrixType::CreateFromQuaternionAndTranslation(coordinateSystemRule->GetRotation(), coordinateSystemRule->GetTranslation());
  69. }
  70. if (coordinateSystemRule->GetScale() != 1.0f)
  71. {
  72. float scale = coordinateSystemRule->GetScale();
  73. matrix.MultiplyByScale(Vector3(scale));
  74. }
  75. if (!coordinateSystemRule->GetOriginNodeName().empty())
  76. {
  77. auto rootIndex = scene.GetGraph().Find(coordinateSystemRule->GetOriginNodeName());
  78. if (rootIndex.IsValid())
  79. {
  80. auto worldMatrix = ConcatenateMatricesUpwards(scene.GetGraph(), rootIndex);
  81. worldMatrix.InvertFull();
  82. matrix *= worldMatrix;
  83. }
  84. }
  85. return matrix;
  86. }
  87. return ConcatenateMatricesUpwards(scene.GetGraph(), nodeIndex);
  88. }
  89. } // Utilities
  90. } // SceneAPI
  91. } // AZ