BsMorphShapes.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsIReflectable.h"
  6. #include "BsVector3.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Animation-Internal
  10. * @{
  11. */
  12. /** A single vertex used for morph target animation. Contains a difference between base and target shape. */
  13. struct BS_CORE_EXPORT MorphVertex
  14. {
  15. MorphVertex() { }
  16. MorphVertex(const Vector3& deltaPosition, const Vector3& deltaNormal, UINT32 sourceIdx)
  17. :deltaPosition(deltaPosition), deltaNormal(deltaNormal), sourceIdx(sourceIdx)
  18. { }
  19. Vector3 deltaPosition;
  20. Vector3 deltaNormal;
  21. UINT32 sourceIdx;
  22. };
  23. /**
  24. * A set of vertices representing a single shape in a morph target animation. Vertices are represented as a difference
  25. * between base and target shape.
  26. */
  27. class BS_CORE_EXPORT MorphShape : public IReflectable
  28. {
  29. public:
  30. MorphShape(const String& name, float weight, const Vector<MorphVertex>& vertices);
  31. /** Returns the name of the shape. */
  32. const String& getName() const { return mName; }
  33. /** Returns the weight of the shape, determining how are different shapes within a channel blended. */
  34. float getWeight() const { return mWeight; }
  35. /** Returns a reference to all of the shape's vertices. Contains only vertices that differ from the base. */
  36. const Vector<MorphVertex>& getVertices() const { return mVertices; }
  37. /**
  38. * Creates a new morph shape from the provided set of vertices.
  39. *
  40. * @param[in] name Name of the frame. Must be unique within a morph channel.
  41. * @param[in] weight Weight in range [0, 1]. Determines how are sequential shapes animated between within a
  42. * morph channel. e.g. if there is a shape A with weight 0.3 and shape B with weight 0.8
  43. * then shape A will be displayed first and then be blended towards shape B as time passes.
  44. * @param[in] vertices Vertices of the base mesh modified by the shape.
  45. */
  46. static SPtr<MorphShape> create(const String& name, float weight, const Vector<MorphVertex>& vertices);
  47. private:
  48. String mName;
  49. float mWeight;
  50. Vector<MorphVertex> mVertices;
  51. /************************************************************************/
  52. /* SERIALIZATION */
  53. /************************************************************************/
  54. public:
  55. friend class MorphShapeRTTI;
  56. static RTTITypeBase* getRTTIStatic();
  57. RTTITypeBase* getRTTI() const override;
  58. MorphShape(); // Serialization only
  59. };
  60. /**
  61. * A collection of morph shapes that are sequentially blended together. Each shape has a weight in range [0, 1] which
  62. * determines at what point is that shape blended. As the channel percent moves from 0 to 1, different shapes will be
  63. * blended with those before or after them, depending on their weight.
  64. */
  65. class BS_CORE_EXPORT MorphChannel : public IReflectable
  66. {
  67. public:
  68. /** Returns the name of the channel. */
  69. const String& getName() const { return mName; }
  70. /** Returns the number of available morph shapes. */
  71. UINT32 getNumShapes() const { return (UINT32)mShapes.size(); }
  72. /** Returns the morph shape at the specified index. */
  73. SPtr<MorphShape> getShape(UINT32 idx) const { return mShapes[idx]; }
  74. /** Creates a new channel from a set of morph shapes. */
  75. static SPtr<MorphChannel> create(const String& name, const Vector<SPtr<MorphShape>>& shapes);
  76. private:
  77. MorphChannel();
  78. MorphChannel(const String& name, const Vector<SPtr<MorphShape>>& shapes);
  79. String mName;
  80. Vector<SPtr<MorphShape>> mShapes;
  81. /************************************************************************/
  82. /* SERIALIZATION */
  83. /************************************************************************/
  84. public:
  85. friend class MorphChannelRTTI;
  86. static RTTITypeBase* getRTTIStatic();
  87. RTTITypeBase* getRTTI() const override;
  88. /**
  89. * Creates MorphShapes with no data. You must populate its data manually.
  90. *
  91. * @note For serialization use only.
  92. */
  93. static SPtr<MorphChannel> createEmpty();
  94. };
  95. /**
  96. * Contains a set of morph channels used for morph target animation. Each morph channel contains one or multiple shapes
  97. * which are blended together depending on frame animation. Each channel is then additively blended together depending
  98. * on some weight.
  99. */
  100. class BS_CORE_EXPORT MorphShapes : public IReflectable // Note: Must be immutable in order to be usable on multiple threads
  101. {
  102. public:
  103. /** Returns the number of available morph channels. */
  104. UINT32 getNumChannels() const { return (UINT32)mChannels.size(); }
  105. /** Returns the morph channel at the specified index. */
  106. SPtr<MorphChannel> getChannel(UINT32 idx) const { return mChannels[idx]; }
  107. /** Returns the number of vertices per morph shape. */
  108. UINT32 getNumVertices() const { return mNumVertices; }
  109. /** Creates a new set of morph shapes. */
  110. static SPtr<MorphShapes> create(const Vector<SPtr<MorphChannel>>& channels, UINT32 numVertices);
  111. private:
  112. MorphShapes();
  113. MorphShapes(const Vector<SPtr<MorphChannel>>& channels, UINT32 numVertices);
  114. Vector<SPtr<MorphChannel>> mChannels;
  115. UINT32 mNumVertices;
  116. /************************************************************************/
  117. /* SERIALIZATION */
  118. /************************************************************************/
  119. public:
  120. friend class MorphShapesRTTI;
  121. static RTTITypeBase* getRTTIStatic();
  122. RTTITypeBase* getRTTI() const override;
  123. /**
  124. * Creates MorphShapes with no data. You must populate its data manually.
  125. *
  126. * @note For serialization use only.
  127. */
  128. static SPtr<MorphShapes> createEmpty();
  129. };
  130. /** @} */
  131. }