ScaleProcess.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "ScaleProcess.h"
  34. #include <assimp/scene.h>
  35. #include <assimp/postprocess.h>
  36. #include <assimp/BaseImporter.h>
  37. namespace Assimp {
  38. // ------------------------------------------------------------------------------------------------
  39. ScaleProcess::ScaleProcess() : BaseProcess(), mScale( AI_CONFIG_GLOBAL_SCALE_FACTOR_DEFAULT ) {
  40. // empty
  41. }
  42. // ------------------------------------------------------------------------------------------------
  43. void ScaleProcess::setScale( ai_real scale ) {
  44. mScale = scale;
  45. }
  46. // ------------------------------------------------------------------------------------------------
  47. ai_real ScaleProcess::getScale() const {
  48. return mScale;
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. bool ScaleProcess::IsActive( unsigned int pFlags ) const {
  52. return ( pFlags & aiProcess_GlobalScale ) != 0;
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. void ScaleProcess::SetupProperties( const Importer* pImp ) {
  56. // User scaling
  57. mScale = pImp->GetPropertyFloat( AI_CONFIG_GLOBAL_SCALE_FACTOR_KEY, 1.0f );
  58. // File scaling * Application Scaling
  59. float importerScale = pImp->GetPropertyFloat( AI_CONFIG_APP_SCALE_KEY, 1.0f );
  60. // apply scale to the scale
  61. // helps prevent bugs with backward compatibility for anyone using normal scaling.
  62. mScale *= importerScale;
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. void ScaleProcess::Execute( aiScene* pScene ) {
  66. if(mScale == 1.0f) {
  67. return; // nothing to scale
  68. }
  69. ai_assert(mScale != 0 );
  70. ai_assert(nullptr != pScene );
  71. ai_assert(nullptr != pScene->mRootNode );
  72. if ( nullptr == pScene ) {
  73. return;
  74. }
  75. if ( nullptr == pScene->mRootNode ) {
  76. return;
  77. }
  78. // Process animations and update position transform to new unit system
  79. for( unsigned int animationID = 0; animationID < pScene->mNumAnimations; animationID++ ) {
  80. aiAnimation* animation = pScene->mAnimations[animationID];
  81. for( unsigned int animationChannel = 0; animationChannel < animation->mNumChannels; animationChannel++) {
  82. aiNodeAnim* anim = animation->mChannels[animationChannel];
  83. for( unsigned int posKey = 0; posKey < anim->mNumPositionKeys; posKey++) {
  84. aiVectorKey& vectorKey = anim->mPositionKeys[posKey];
  85. vectorKey.mValue *= mScale;
  86. }
  87. }
  88. }
  89. for( unsigned int meshID = 0; meshID < pScene->mNumMeshes; meshID++) {
  90. aiMesh *mesh = pScene->mMeshes[meshID];
  91. // Reconstruct mesh vertices to the new unit system
  92. for( unsigned int vertexID = 0; vertexID < mesh->mNumVertices; vertexID++) {
  93. aiVector3D& vertex = mesh->mVertices[vertexID];
  94. vertex *= mScale;
  95. }
  96. // bone placement / scaling
  97. for( unsigned int boneID = 0; boneID < mesh->mNumBones; boneID++) {
  98. // Reconstruct matrix by transform rather than by scale
  99. // This prevent scale values being changed which can
  100. // be meaningful in some cases
  101. // like when you want the modeller to see 1:1 compatibility.
  102. aiBone* bone = mesh->mBones[boneID];
  103. aiVector3D pos, scale;
  104. aiQuaternion rotation;
  105. bone->mOffsetMatrix.Decompose( scale, rotation, pos);
  106. aiMatrix4x4 translation;
  107. aiMatrix4x4::Translation( pos * mScale, translation );
  108. aiMatrix4x4 scaling;
  109. aiMatrix4x4::Scaling( aiVector3D(scale), scaling );
  110. const aiMatrix4x4 RotMatrix = aiMatrix4x4(rotation.GetMatrix());
  111. bone->mOffsetMatrix = translation * RotMatrix * scaling;
  112. }
  113. // animation mesh processing
  114. // convert by position rather than scale.
  115. for( unsigned int animMeshID = 0; animMeshID < mesh->mNumAnimMeshes; animMeshID++) {
  116. aiAnimMesh * animMesh = mesh->mAnimMeshes[animMeshID];
  117. for( unsigned int vertexID = 0; vertexID < animMesh->mNumVertices; vertexID++) {
  118. aiVector3D& vertex = animMesh->mVertices[vertexID];
  119. vertex *= mScale;
  120. }
  121. }
  122. }
  123. traverseNodes( pScene->mRootNode );
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. void ScaleProcess::traverseNodes( aiNode *node, unsigned int nested_node_id ) {
  127. applyScaling( node );
  128. for( size_t i = 0; i < node->mNumChildren; i++) {
  129. // recurse into the tree until we are done!
  130. traverseNodes( node->mChildren[i], nested_node_id+1 );
  131. }
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. void ScaleProcess::applyScaling( aiNode *currentNode ) {
  135. if ( nullptr != currentNode ) {
  136. // Reconstruct matrix by transform rather than by scale
  137. // This prevent scale values being changed which can
  138. // be meaningful in some cases
  139. // like when you want the modeller to
  140. // see 1:1 compatibility.
  141. aiVector3D pos, scale;
  142. aiQuaternion rotation;
  143. currentNode->mTransformation.Decompose( scale, rotation, pos);
  144. aiMatrix4x4 translation;
  145. aiMatrix4x4::Translation( pos * mScale, translation );
  146. aiMatrix4x4 scaling;
  147. // note: we do not use mScale here, this is on purpose.
  148. aiMatrix4x4::Scaling( scale, scaling );
  149. aiMatrix4x4 RotMatrix = aiMatrix4x4 (rotation.GetMatrix());
  150. currentNode->mTransformation = translation * RotMatrix * scaling;
  151. }
  152. }
  153. } // Namespace Assimp