AssImpSceneWrapper.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <AzCore/Debug/Trace.h>
  13. #include <AzToolsFramework/Debug/TraceContext.h>
  14. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  15. #include <SceneAPI/SDKWrapper/AssImpSceneWrapper.h>
  16. #include <SceneAPI/SDKWrapper/AssImpNodeWrapper.h>
  17. #include <assimp/scene.h>
  18. #include <assimp/postprocess.h>
  19. #if AZ_TRAIT_COMPILER_SUPPORT_CSIGNAL
  20. #include <csignal>
  21. #include <cstdlib>
  22. #include <iostream>
  23. #include <stdlib.h>
  24. #endif // AZ_TRAIT_COMPILER_SUPPORT_CSIGNAL
  25. namespace AZ
  26. {
  27. namespace AssImpSDKWrapper
  28. {
  29. AssImpSceneWrapper::AssImpSceneWrapper()
  30. : SDKScene::SceneWrapperBase()
  31. {
  32. }
  33. AssImpSceneWrapper::AssImpSceneWrapper(aiScene* aiScene)
  34. : SDKScene::SceneWrapperBase(aiScene)
  35. {
  36. }
  37. AssImpSceneWrapper::~AssImpSceneWrapper()
  38. {
  39. }
  40. #if AZ_TRAIT_COMPILER_SUPPORT_CSIGNAL
  41. void signal_handler(int signal)
  42. {
  43. AZ_TracePrintf(
  44. SceneAPI::Utilities::ErrorWindow,
  45. "Failed to import scene with Asset Importer library. An %s has occured in the library, this scene file cannot be parsed by the library.",
  46. signal == SIGABRT ? "assert" : "unknown error");
  47. }
  48. #endif // AZ_TRAIT_COMPILER_SUPPORT_CSIGNAL
  49. bool AssImpSceneWrapper::LoadSceneFromFile(const char* fileName)
  50. {
  51. AZ_TracePrintf(SceneAPI::Utilities::LogWindow, "AssImpSceneWrapper::LoadSceneFromFile %s", fileName);
  52. AZ_TraceContext("Filename", fileName);
  53. #if AZ_TRAIT_COMPILER_SUPPORT_CSIGNAL
  54. // Turn off the abort popup because it can disrupt automation.
  55. // AssImp calls abort when asserts are enabled, and an assert is encountered.
  56. #ifdef _WRITE_ABORT_MSG
  57. _set_abort_behavior(0, _WRITE_ABORT_MSG);
  58. #endif // #ifdef _WRITE_ABORT_MSG
  59. // Instead, capture any calls to abort with a signal handler, and report them.
  60. auto previous_handler = std::signal(SIGABRT, signal_handler);
  61. #endif // AZ_TRAIT_COMPILER_SUPPORT_CSIGNAL
  62. // aiProcess_JoinIdenticalVertices is not enabled because O3DE has a mesh optimizer that also does this,
  63. // this flag is disabled to keep AssImp output similar to FBX SDK to reduce downstream bugs for the initial AssImp release.
  64. // There's currently a minimum of properties and flags set to maximize compatibility with the existing node graph.
  65. m_importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, false);
  66. m_importer.SetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, false);
  67. m_sceneFileName = fileName;
  68. m_assImpScene = m_importer.ReadFile(fileName,
  69. aiProcess_Triangulate //Triangulates all faces of all meshes
  70. | aiProcess_LimitBoneWeights //Limits the number of bones that can affect a vertex to a maximum value
  71. //dropping the least important and re-normalizing
  72. | aiProcess_GenNormals); //Generate normals for meshes
  73. #if AZ_TRAIT_COMPILER_SUPPORT_CSIGNAL
  74. // Reset abort behavior for anything else that may call abort.
  75. std::signal(SIGABRT, previous_handler);
  76. #ifdef _WRITE_ABORT_MSG
  77. _set_abort_behavior(1, _WRITE_ABORT_MSG);
  78. #endif // #ifdef _WRITE_ABORT_MSG
  79. #endif // AZ_TRAIT_COMPILER_SUPPORT_CSIGNAL
  80. if (!m_assImpScene)
  81. {
  82. AZ_TracePrintf(SceneAPI::Utilities::ErrorWindow, "Failed to import Asset Importer Scene. Error returned: %s", m_importer.GetErrorString());
  83. return false;
  84. }
  85. return true;
  86. }
  87. bool AssImpSceneWrapper::LoadSceneFromFile(const AZStd::string& fileName)
  88. {
  89. return LoadSceneFromFile(fileName.c_str());
  90. }
  91. const std::shared_ptr<SDKNode::NodeWrapper> AssImpSceneWrapper::GetRootNode() const
  92. {
  93. return std::shared_ptr<SDKNode::NodeWrapper>(new AssImpNodeWrapper(m_assImpScene->mRootNode));
  94. }
  95. std::shared_ptr<SDKNode::NodeWrapper> AssImpSceneWrapper::GetRootNode()
  96. {
  97. return std::shared_ptr<SDKNode::NodeWrapper>(new AssImpNodeWrapper(m_assImpScene->mRootNode));
  98. }
  99. void AssImpSceneWrapper::Clear()
  100. {
  101. m_importer.FreeScene();
  102. }
  103. AZStd::pair<AssImpSceneWrapper::AxisVector, int32_t> AssImpSceneWrapper::GetUpVectorAndSign() const
  104. {
  105. AZStd::pair<AssImpSceneWrapper::AxisVector, int32_t> result(AxisVector::Z, 1);
  106. int32_t upVectorRead(static_cast<int32_t>(result.first));
  107. m_assImpScene->mMetaData->Get("UpAxis", upVectorRead);
  108. m_assImpScene->mMetaData->Get("UpAxisSign", result.second);
  109. result.first = static_cast<AssImpSceneWrapper::AxisVector>(upVectorRead);
  110. return result;
  111. }
  112. AZStd::pair<AssImpSceneWrapper::AxisVector, int32_t> AssImpSceneWrapper::GetFrontVectorAndSign() const
  113. {
  114. AZStd::pair<AssImpSceneWrapper::AxisVector, int32_t> result(AxisVector::Y, 1);
  115. int32_t frontVectorRead(static_cast<int32_t>(result.first));
  116. m_assImpScene->mMetaData->Get("FrontAxis", frontVectorRead);
  117. m_assImpScene->mMetaData->Get("FrontAxisSign", result.second);
  118. result.first = static_cast<AssImpSceneWrapper::AxisVector>(frontVectorRead);
  119. return result;
  120. }
  121. }//namespace AssImpSDKWrapper
  122. } // namespace AZ