BsMorphShapes.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "Reflection/BsIReflectable.h"
  6. #include "Math/BsVector3.h"
  7. namespace bs
  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. * @native
  25. * A set of vertices representing a single shape in a morph target animation. Vertices are represented as a difference
  26. * between base and target shape.
  27. * @endnative
  28. * @script
  29. * Name and weight of a single shape in a morph target animation. Each shape internally represents a set of vertices
  30. * that describe the morph shape.
  31. * @endscript
  32. */
  33. class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Animation) MorphShape : public IReflectable
  34. {
  35. public:
  36. MorphShape(const String& name, float weight, const Vector<MorphVertex>& vertices);
  37. /** Returns the name of the shape. */
  38. BS_SCRIPT_EXPORT(pr:getter,n:Name)
  39. const String& getName() const { return mName; }
  40. /** Returns the weight of the shape, determining how are different shapes within a channel blended. */
  41. BS_SCRIPT_EXPORT(pr:getter,n:Weight)
  42. float getWeight() const { return mWeight; }
  43. /** Returns a reference to all of the shape's vertices. Contains only vertices that differ from the base. */
  44. const Vector<MorphVertex>& getVertices() const { return mVertices; }
  45. /**
  46. * Creates a new morph shape from the provided set of vertices.
  47. *
  48. * @param[in] name Name of the frame. Must be unique within a morph channel.
  49. * @param[in] weight Weight in range [0, 1]. Determines how are sequential shapes animated between within a
  50. * morph channel. e.g. if there is a shape A with weight 0.3 and shape B with weight 0.8
  51. * then shape A will be displayed first and then be blended towards shape B as time passes.
  52. * @param[in] vertices Vertices of the base mesh modified by the shape.
  53. */
  54. static SPtr<MorphShape> create(const String& name, float weight, const Vector<MorphVertex>& vertices);
  55. private:
  56. String mName;
  57. float mWeight;
  58. Vector<MorphVertex> mVertices;
  59. /************************************************************************/
  60. /* SERIALIZATION */
  61. /************************************************************************/
  62. public:
  63. friend class MorphShapeRTTI;
  64. static RTTITypeBase* getRTTIStatic();
  65. RTTITypeBase* getRTTI() const override;
  66. MorphShape(); // Serialization only
  67. };
  68. /**
  69. * A collection of morph shapes that are sequentially blended together. Each shape has a weight in range [0, 1] which
  70. * determines at what point is that shape blended. As the channel percent moves from 0 to 1, different shapes will be
  71. * blended with those before or after them, depending on their weight.
  72. */
  73. class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Animation) MorphChannel : public IReflectable
  74. {
  75. public:
  76. /** Returns the unique name of the channel. */
  77. BS_SCRIPT_EXPORT(pr:getter,n:Name)
  78. const String& getName() const { return mName; }
  79. /** Returns the number of available morph shapes. */
  80. UINT32 getNumShapes() const { return (UINT32)mShapes.size(); }
  81. /** Returns the morph shape at the specified index. */
  82. SPtr<MorphShape> getShape(UINT32 idx) const { return mShapes[idx]; }
  83. /** Returns all morph shapes within this channel, in order from lowest to highest. */
  84. BS_SCRIPT_EXPORT(pr:getter,n:Shapes)
  85. const Vector<SPtr<MorphShape>>& getShapes() const { return mShapes; }
  86. /** Creates a new channel from a set of morph shapes. */
  87. static SPtr<MorphChannel> create(const String& name, const Vector<SPtr<MorphShape>>& shapes);
  88. private:
  89. MorphChannel();
  90. MorphChannel(const String& name, const Vector<SPtr<MorphShape>>& shapes);
  91. String mName;
  92. Vector<SPtr<MorphShape>> mShapes;
  93. /************************************************************************/
  94. /* SERIALIZATION */
  95. /************************************************************************/
  96. public:
  97. friend class MorphChannelRTTI;
  98. static RTTITypeBase* getRTTIStatic();
  99. RTTITypeBase* getRTTI() const override;
  100. /**
  101. * Creates MorphShapes with no data. You must populate its data manually.
  102. *
  103. * @note For serialization use only.
  104. */
  105. static SPtr<MorphChannel> createEmpty();
  106. };
  107. /**
  108. * Contains a set of morph channels used for morph target animation. Each morph channel contains one or multiple shapes
  109. * which are blended together depending on frame animation. Each channel is then additively blended together depending
  110. * on some weight.
  111. */
  112. class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Animation) MorphShapes : public IReflectable // Note: Must be immutable in order to be usable on multiple threads
  113. {
  114. public:
  115. /** Returns the number of available morph channels. */
  116. UINT32 getNumChannels() const { return (UINT32)mChannels.size(); }
  117. /** Returns the morph channel at the specified index. */
  118. SPtr<MorphChannel> getChannel(UINT32 idx) const { return mChannels[idx]; }
  119. /** Returns a list of all morph channels in the morph animation. */
  120. BS_SCRIPT_EXPORT(pr:getter,n:Channels)
  121. const Vector<SPtr<MorphChannel>>& getChannels() const { return mChannels; }
  122. /** Returns the number of vertices per morph shape. */
  123. UINT32 getNumVertices() const { return mNumVertices; }
  124. /** Creates a new set of morph shapes. */
  125. static SPtr<MorphShapes> create(const Vector<SPtr<MorphChannel>>& channels, UINT32 numVertices);
  126. private:
  127. MorphShapes();
  128. MorphShapes(const Vector<SPtr<MorphChannel>>& channels, UINT32 numVertices);
  129. Vector<SPtr<MorphChannel>> mChannels;
  130. UINT32 mNumVertices;
  131. /************************************************************************/
  132. /* SERIALIZATION */
  133. /************************************************************************/
  134. public:
  135. friend class MorphShapesRTTI;
  136. static RTTITypeBase* getRTTIStatic();
  137. RTTITypeBase* getRTTI() const override;
  138. /**
  139. * Creates MorphShapes with no data. You must populate its data manually.
  140. *
  141. * @note For serialization use only.
  142. */
  143. static SPtr<MorphShapes> createEmpty();
  144. };
  145. /** @} */
  146. }