TextureTransform.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. /** @file Definition of a helper step that processes texture transformations */
  34. #ifndef AI_TEXTURE_TRANSFORM_H_INCLUDED
  35. #define AI_TEXTURE_TRANSFORM_H_INCLUDED
  36. #include <assimp/BaseImporter.h>
  37. #include "Common/BaseProcess.h"
  38. #include <assimp/material.h>
  39. #include <list>
  40. struct aiNode;
  41. struct aiMaterial;
  42. namespace Assimp {
  43. #define AI_TT_UV_IDX_LOCK_TBD 0xffffffff
  44. #define AI_TT_UV_IDX_LOCK_NONE 0xeeeeeeee
  45. #define AI_TT_ROTATION_EPSILON ((float)AI_DEG_TO_RAD(0.5))
  46. // ---------------------------------------------------------------------------
  47. /** Small helper structure representing a shortcut into the material list
  48. * to be able to update some values quickly.
  49. */
  50. struct TTUpdateInfo {
  51. TTUpdateInfo() AI_NO_EXCEPT
  52. : directShortcut(nullptr)
  53. , mat(nullptr)
  54. , semantic(0)
  55. , index(0) {
  56. // empty
  57. }
  58. //! Direct shortcut, if available
  59. unsigned int* directShortcut;
  60. //! Material
  61. aiMaterial *mat;
  62. //! Texture type and index
  63. unsigned int semantic, index;
  64. };
  65. // ---------------------------------------------------------------------------
  66. /** Helper class representing texture coordinate transformations
  67. */
  68. struct STransformVecInfo : public aiUVTransform {
  69. STransformVecInfo() AI_NO_EXCEPT
  70. : uvIndex(0)
  71. , mapU(aiTextureMapMode_Wrap)
  72. , mapV(aiTextureMapMode_Wrap)
  73. , lockedPos(AI_TT_UV_IDX_LOCK_NONE) {
  74. // empty
  75. }
  76. //! Source texture coordinate index
  77. unsigned int uvIndex;
  78. //! Texture mapping mode in the u, v direction
  79. aiTextureMapMode mapU,mapV;
  80. //! Locked destination UV index
  81. //! AI_TT_UV_IDX_LOCK_TBD - to be determined
  82. //! AI_TT_UV_IDX_LOCK_NONE - none (default)
  83. unsigned int lockedPos;
  84. //! Update info - shortcuts into all materials
  85. //! that are referencing this transform setup
  86. std::list<TTUpdateInfo> updateList;
  87. // -------------------------------------------------------------------
  88. /** Compare two transform setups
  89. */
  90. inline bool operator== (const STransformVecInfo& other) const
  91. {
  92. // We use a small epsilon here
  93. const static float epsilon = 0.05f;
  94. if (std::fabs( mTranslation.x - other.mTranslation.x ) > epsilon ||
  95. std::fabs( mTranslation.y - other.mTranslation.y ) > epsilon)
  96. {
  97. return false;
  98. }
  99. if (std::fabs( mScaling.x - other.mScaling.x ) > epsilon ||
  100. std::fabs( mScaling.y - other.mScaling.y ) > epsilon)
  101. {
  102. return false;
  103. }
  104. if (std::fabs( mRotation - other.mRotation) > epsilon)
  105. {
  106. return false;
  107. }
  108. return true;
  109. }
  110. inline bool operator!= (const STransformVecInfo& other) const
  111. {
  112. return !(*this == other);
  113. }
  114. // -------------------------------------------------------------------
  115. /** Returns whether this is an untransformed texture coordinate set
  116. */
  117. inline bool IsUntransformed() const
  118. {
  119. return (1.0f == mScaling.x && 1.f == mScaling.y &&
  120. !mTranslation.x && !mTranslation.y &&
  121. mRotation < AI_TT_ROTATION_EPSILON);
  122. }
  123. // -------------------------------------------------------------------
  124. /** Build a 3x3 matrix from the transformations
  125. */
  126. inline void GetMatrix(aiMatrix3x3& mOut)
  127. {
  128. mOut = aiMatrix3x3();
  129. if (1.0f != mScaling.x || 1.0f != mScaling.y)
  130. {
  131. aiMatrix3x3 mScale;
  132. mScale.a1 = mScaling.x;
  133. mScale.b2 = mScaling.y;
  134. mOut = mScale;
  135. }
  136. if (mRotation)
  137. {
  138. aiMatrix3x3 mRot;
  139. mRot.a1 = mRot.b2 = std::cos(mRotation);
  140. mRot.a2 = mRot.b1 = std::sin(mRotation);
  141. mRot.a2 = -mRot.a2;
  142. mOut *= mRot;
  143. }
  144. if (mTranslation.x || mTranslation.y)
  145. {
  146. aiMatrix3x3 mTrans;
  147. mTrans.a3 = mTranslation.x;
  148. mTrans.b3 = mTranslation.y;
  149. mOut *= mTrans;
  150. }
  151. }
  152. };
  153. // ---------------------------------------------------------------------------
  154. /** Helper step to compute final UV coordinate sets if there are scalings
  155. * or rotations in the original data read from the file.
  156. */
  157. class TextureTransformStep : public BaseProcess {
  158. public:
  159. // -------------------------------------------------------------------
  160. /// The default class constructor / destructor.
  161. TextureTransformStep();
  162. ~TextureTransformStep() override = default;
  163. // -------------------------------------------------------------------
  164. bool IsActive( unsigned int pFlags) const override;
  165. // -------------------------------------------------------------------
  166. void Execute( aiScene* pScene) override;
  167. // -------------------------------------------------------------------
  168. void SetupProperties(const Importer* pImp) override;
  169. protected:
  170. // -------------------------------------------------------------------
  171. /** Preprocess a specific UV transformation setup
  172. *
  173. * @param info Transformation setup to be preprocessed.
  174. */
  175. void PreProcessUVTransform(STransformVecInfo& info);
  176. private:
  177. unsigned int configFlags;
  178. };
  179. } // namespace Assimp
  180. #endif //! AI_TEXTURE_TRANSFORM_H_INCLUDED