RedSlotBakeModel.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // RedSlotBakeModel.hpp
  3. // empty2dx-desktop
  4. //
  5. // Created by Liang zhong on 2022/11/5.
  6. //
  7. #ifndef RedSlotBakeModel_h
  8. #define RedSlotBakeModel_h
  9. #include <stdio.h>
  10. #include "cocos2d.h"
  11. using namespace cocos2d;
  12. struct BAKE_POS_INT {
  13. short xPos;
  14. short yPos;
  15. };
  16. struct BKAE_FRAME_INFO {
  17. unsigned char texture;
  18. unsigned char blend;
  19. short color;
  20. short alpha;
  21. unsigned char uv;
  22. unsigned char indices;
  23. short vert;
  24. short skipFrameCount; //播放到当天跳过多少帧,主要是顶点偏移数组需要用这个来维护指针
  25. };
  26. struct BKAE_FRAME_PLAY {
  27. short texture;
  28. short blend;
  29. short color;
  30. short alpha;
  31. short uv;
  32. short indices;
  33. short vert;
  34. short skipFrameCount; //播放到当天跳过多少帧,主要是顶点偏移数组需要用这个来维护指针
  35. int vertMapIndex = -1;//用于记录在BakeNode中绘制时候存储的_VertArr的什么位置
  36. bool hasAttachNode; //记录是否有AttachNode
  37. bool hasOnceUpdate; //是否更新过一次,如果跟新过一次,很多不变数据就不需要更新了,比如color,uv等
  38. std::string slotName;
  39. Color4B currentFrameColor; //临时存储当前颜色
  40. std::vector<Tex2F>* currentFrameUV;//临时存储当前UV
  41. cocos2d::TrianglesCommand::Triangles triangles;//临时存储当前Triangles
  42. // 构造函数
  43. BKAE_FRAME_PLAY() :
  44. texture(0),
  45. blend(0),
  46. color(0),
  47. alpha(0),
  48. uv(0),
  49. indices(0),
  50. vert(0),
  51. vertMapIndex(-1),
  52. hasAttachNode(false),
  53. hasOnceUpdate(false),
  54. skipFrameCount(0),
  55. slotName(""),
  56. currentFrameColor(Color4B::WHITE),
  57. currentFrameUV(),
  58. triangles()
  59. {
  60. // 初始化 triangles 结构体的成员变量
  61. triangles.verts = nullptr;
  62. triangles.indices = nullptr;
  63. triangles.vertCount = 0;
  64. triangles.indexCount = 0;
  65. }
  66. };
  67. // 定义枚举类型color_set1
  68. enum frameType {frame_skip,frame_texture,frame_blend, frame_Color, frame_alpha, frame_uv, frame_indices,frame_vert};
  69. class RedSlotBakeModel
  70. {
  71. public:
  72. RedSlotBakeModel(std::string aSlotName,int aSlotIndex);
  73. void startBakeOneFrame();
  74. void bakeFrameSkip();
  75. void bakeFrameInfo(std::string& aAttachmentName,cocos2d::Texture2D* aTexture,BlendFunc aBlendFunc,cocos2d::TrianglesCommand::Triangles aTriangles);
  76. void finishBake();
  77. bool isBakeFinish();
  78. bool slotShoudBeDrawSlot();
  79. bool isUVChange(int aCurrentFrame);
  80. bool getFrameSkip(int aCurrentFrame);
  81. void setFrameSkip(int aCurrentFrame,BKAE_FRAME_PLAY& aFrameInfo);
  82. //纹理相关
  83. cocos2d::Texture2D* getTexture(int aCurrentFrame);
  84. //混合模式相关
  85. BlendFunc& getBlendFunc(int aCurrentFrame);
  86. //顶点相关
  87. void getTriangles(int aCurrentFrame,BKAE_FRAME_PLAY& aFrameInfo);
  88. void getTriangles(int aCurrentFrame,BKAE_FRAME_PLAY& aFrameInfo,const Point& offSet,float dis,V3F_C4B_T2F *beginVert,bool merge);
  89. void initBKAE_FRAME_PLAY(BKAE_FRAME_PLAY& aFrameInfo);
  90. const std::string& getSlotName();
  91. const int getSlotIndex();
  92. void setSlotName(const std::string& aSlotName);
  93. void setSlotIndex(int aSlotIndex);
  94. int getVertCount();
  95. int getMaxXPos();
  96. int getMaxYPos();
  97. int getMinXPos();
  98. int getMinYPos();
  99. void setMaxXPos(int pos);
  100. void setMaxYPos(int pos);
  101. void setMinXPos(int pos);
  102. void setMinYPos(int pos);
  103. const std::vector<std::vector<BAKE_POS_INT>>& getPosArray();
  104. void addPosArray(const std::vector<BAKE_POS_INT>& aPosArray);
  105. const std::vector<BlendFunc>& getBlendFuncArray();
  106. void addBlendFuncToArray(const BlendFunc& blendFunc);
  107. const std::vector<Color3B>& getColorArray();
  108. void addColorToArray(const Color3B& color);
  109. const std::vector<GLubyte>& getAlphaArray();
  110. void addAlphaToArray(GLubyte Alpha);
  111. const std::vector<std::vector<Tex2F>>& getUvArray();
  112. void addUvArray(const std::vector<Tex2F>& aUvArray);
  113. const std::vector<std::vector<unsigned short>>& getIndicesArray();
  114. void addIndicesArray(const std::vector<unsigned short>& aIndicesArray);
  115. const std::vector<GLubyte>& getFrameChangeArr();
  116. void addFrameChangeToArr(GLubyte aFrameChange);
  117. const std::vector<cocos2d::Texture2D*>& getTextureArray();
  118. void addTextureToArray(cocos2d::Texture2D* texture);
  119. void initBakeFrameInfos();
  120. private:
  121. bool shoudBeDrawSlot = true;//slot是否应该被绘制,如果纹理,顶点,颜色等数组都为空,这个slot其实没有绘制的必要
  122. bool isSpineBakeFinish = false;
  123. std::vector<BKAE_FRAME_INFO> bakeFrameInfos; //将frameChangeArr扩展成程序方便读取的数据
  124. int colorArraySize; //加快性能用,这样不需要反复计算Size,同时对于始终没变的东西不需要反复更新
  125. int alphaArraySize;
  126. int uvArraySize;
  127. int posArraySize;
  128. int blendFuncArraySize;
  129. int indicesArraySize;
  130. int textureArraySize;
  131. bool isPosArrayChangeOnlySlot = false; //true代表当前角色是仅有顶点位移,这个是绝大部分Slot
  132. cocos2d::Texture2D* firstTexture = nullptr;//isPosArrayChangeOnlySlot=true的时候,每次获取纹理就返回这个就好,这样性能高
  133. BlendFunc firstBlendFunc;//isPosArrayChangeOnlySlot=true的时候,每次获取纹理就返回这个就好,这样性能高
  134. //*******************************************需要protolBuffer存**************************************
  135. std::string slotName;
  136. int slotIndex = -1;
  137. std::vector<std::vector<BAKE_POS_INT>> posArray;//记录每个顶点的偏移
  138. std::vector<BlendFunc> blendFuncArray;//记录每个blend的变化
  139. std::vector<Color3B> colorArray;//记录每个Color的变化
  140. std::vector<GLubyte> alphaArray;//记录每个alpha的变化
  141. std::vector<std::vector<Tex2F>> uvArray;//记录每个UV的变化
  142. std::vector<std::vector<unsigned short>> indicesArray;//记录每个indices的变化
  143. std::vector<GLubyte> frameChangeArr; //记录每帧哪些属性发生了改变,运行时通过这个来移动每个数组的index指针
  144. int maxXPos = 0;
  145. int maxYPos = 0;
  146. int minXPos = 0;
  147. int minYPos = 0;
  148. std::vector<cocos2d::Texture2D*> textureArray;//记录每个纹理的变化
  149. //***********************************************************************************************
  150. std::string currentBakeAttachmentName = "";
  151. bool hasChangeAttachmentName = true;
  152. void _setTexture(cocos2d::Texture2D* aTexture);
  153. void _setBlendFunc(BlendFunc aBlendFunc);
  154. void _setTriangles(cocos2d::TrianglesCommand::Triangles aTriangles);
  155. bool _uvVectIsEqual(std::vector<Tex2F> a,std::vector<Tex2F> b);
  156. bool _indicesVectIsEqual(unsigned short *a,int aCount,std::vector<unsigned short> b);
  157. bool _vertVectIsEqual(std::vector<BAKE_POS_INT> &a,std::vector<BAKE_POS_INT> &b);
  158. };
  159. #endif /* RedSlotBakeModel_h */