ScaleProcess.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, 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. namespace Assimp {
  37. ScaleProcess::ScaleProcess()
  38. : BaseProcess()
  39. , mScale( AI_CONFIG_GLOBAL_SCALE_FACTOR_DEFAULT ) {
  40. }
  41. ScaleProcess::~ScaleProcess() {
  42. // empty
  43. }
  44. void ScaleProcess::setScale( ai_real scale ) {
  45. mScale = scale;
  46. }
  47. ai_real ScaleProcess::getScale() const {
  48. return mScale;
  49. }
  50. bool ScaleProcess::IsActive( unsigned int pFlags ) const {
  51. return ( pFlags & aiProcess_GlobalScale ) != 0;
  52. }
  53. void ScaleProcess::SetupProperties( const Importer* pImp ) {
  54. mScale = pImp->GetPropertyFloat( AI_CONFIG_GLOBAL_SCALE_FACTOR_KEY, 1.0f );
  55. }
  56. void ScaleProcess::Execute( aiScene* pScene ) {
  57. if(mScale == 1.0f) {
  58. return; // nothing to scale
  59. }
  60. ai_assert( mScale != 0 );
  61. ai_assert( nullptr != pScene );
  62. ai_assert( nullptr != pScene->mRootNode );
  63. if ( nullptr == pScene ) {
  64. return;
  65. }
  66. if ( nullptr == pScene->mRootNode ) {
  67. return;
  68. }
  69. // Process animations and update position transform to new unit system
  70. for( unsigned int animationID = 0; animationID < pScene->mNumAnimations; animationID++ )
  71. {
  72. aiAnimation* animation = pScene->mAnimations[animationID];
  73. for( unsigned int animationChannel = 0; animationChannel < animation->mNumChannels; animationChannel++)
  74. {
  75. aiNodeAnim* anim = animation->mChannels[animationChannel];
  76. for( unsigned int posKey = 0; posKey < anim->mNumPositionKeys; posKey++)
  77. {
  78. aiVectorKey& vectorKey = anim->mPositionKeys[posKey];
  79. vectorKey.mValue *= mScale;
  80. }
  81. }
  82. }
  83. for( unsigned int meshID = 0; meshID < pScene->mNumMeshes; meshID++)
  84. {
  85. aiMesh *mesh = pScene->mMeshes[meshID];
  86. // Reconstruct mesh vertexes to the new unit system
  87. for( unsigned int vertexID = 0; vertexID < mesh->mNumVertices; vertexID++)
  88. {
  89. aiVector3D& vertex = mesh->mVertices[vertexID];
  90. vertex *= mScale;
  91. }
  92. // bone placement / scaling
  93. for( unsigned int boneID = 0; boneID < mesh->mNumBones; boneID++)
  94. {
  95. // Reconstruct matrix by transform rather than by scale
  96. // This prevent scale values being changed which can
  97. // be meaningful in some cases
  98. // like when you want the modeller to see 1:1 compatibility.
  99. aiBone* bone = mesh->mBones[boneID];
  100. aiVector3D pos, scale;
  101. aiQuaternion rotation;
  102. bone->mOffsetMatrix.Decompose( scale, rotation, pos);
  103. aiMatrix4x4 translation;
  104. aiMatrix4x4::Translation( pos * mScale, translation );
  105. aiMatrix4x4 scaling;
  106. aiMatrix4x4::Scaling( aiVector3D(scale), scaling );
  107. aiMatrix4x4 RotMatrix = aiMatrix4x4 (rotation.GetMatrix());
  108. bone->mOffsetMatrix = translation * RotMatrix * scaling;
  109. }
  110. // animation mesh processing
  111. // convert by position rather than scale.
  112. for( unsigned int animMeshID = 0; animMeshID < mesh->mNumAnimMeshes; animMeshID++)
  113. {
  114. aiAnimMesh * animMesh = mesh->mAnimMeshes[animMeshID];
  115. for( unsigned int vertexID = 0; vertexID < animMesh->mNumVertices; vertexID++)
  116. {
  117. aiVector3D& vertex = animMesh->mVertices[vertexID];
  118. vertex *= mScale;
  119. }
  120. }
  121. }
  122. traverseNodes( pScene->mRootNode );
  123. }
  124. void ScaleProcess::traverseNodes( aiNode *node, unsigned int nested_node_id ) {
  125. applyScaling( node );
  126. for( size_t i = 0; i < node->mNumChildren; i++)
  127. {
  128. // recurse into the tree until we are done!
  129. traverseNodes( node->mChildren[i], nested_node_id+1 );
  130. }
  131. }
  132. void ScaleProcess::applyScaling( aiNode *currentNode ) {
  133. if ( nullptr != currentNode ) {
  134. // Reconstruct matrix by transform rather than by scale
  135. // This prevent scale values being changed which can
  136. // be meaningful in some cases
  137. // like when you want the modeller to
  138. // see 1:1 compatibility.
  139. aiVector3D pos, scale;
  140. aiQuaternion rotation;
  141. currentNode->mTransformation.Decompose( scale, rotation, pos);
  142. aiMatrix4x4 translation;
  143. aiMatrix4x4::Translation( pos * mScale, translation );
  144. aiMatrix4x4 scaling;
  145. // note: we do not use mScale here, this is on purpose.
  146. aiMatrix4x4::Scaling( scale, scaling );
  147. aiMatrix4x4 RotMatrix = aiMatrix4x4 (rotation.GetMatrix());
  148. currentNode->mTransformation = translation * RotMatrix * scaling;
  149. }
  150. }
  151. } // Namespace Assimp