2
0

ScaleProcess.cpp 6.7 KB

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