// // RedSlotBakeModel.cpp // empty2dx-desktop // // Created by Liang zhong on 2022/11/5. // #include "RedSlotBakeModel.h" static GLubyte byteMap[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; RedSlotBakeModel::RedSlotBakeModel(std::string aSlotName,int aSlotIndex){ slotName = aSlotName; slotIndex = aSlotIndex; } void RedSlotBakeModel::finishBake(){ isSpineBakeFinish = true; if(textureArray.size()==0){ //如果没有纹理就不需要被绘制了 shoudBeDrawSlot = false; }else{ } initBakeFrameInfos(); } void RedSlotBakeModel::initBKAE_FRAME_PLAY(BKAE_FRAME_PLAY& aFrameInfo){ aFrameInfo.slotName = slotName; aFrameInfo.texture = 0; aFrameInfo.blend = 0; aFrameInfo.alpha = 0; aFrameInfo.color = 0; aFrameInfo.indices = 0; aFrameInfo.uv = 0; aFrameInfo.vert = 0; aFrameInfo.skipFrameCount = 0; aFrameInfo.hasOnceUpdate = false; aFrameInfo.hasAttachNode = false; aFrameInfo.vertMapIndex = -1; if (colorArray.size()==0) { aFrameInfo.currentFrameColor = Color4B(255,255,255,255); }else{ Color3B& color = colorArray.at(0); aFrameInfo.currentFrameColor.r = color.r; aFrameInfo.currentFrameColor.g = color.g; aFrameInfo.currentFrameColor.b = color.b; } if (alphaArray.size()!=0) { aFrameInfo.currentFrameColor.a = alphaArray.at(0); } if(uvArray.size()!=0){ std::vector ¤tFrameUV = uvArray[0]; aFrameInfo.currentFrameUV = ¤tFrameUV; } if(indicesArray.size()!=0){ std::vector& indicesVect = indicesArray.at(0); aFrameInfo.triangles.indexCount = (int)indicesVect.size(); aFrameInfo.triangles.indices = indicesVect.data(); } aFrameInfo.triangles.vertCount = 0; if (aFrameInfo.triangles.verts!=nullptr) { delete[] aFrameInfo.triangles.verts; } aFrameInfo.triangles.verts = nullptr; } bool RedSlotBakeModel::isBakeFinish(){ return isSpineBakeFinish; } bool RedSlotBakeModel::slotShoudBeDrawSlot(){ return shoudBeDrawSlot; } void RedSlotBakeModel::initBakeFrameInfos(){ for (int i =0; i0) { preInfo = bakeFrameInfos[i-1]; } GLubyte frameInit = frameChangeArr.at(i); GLubyte tmp1 = (frameInit>>frameType::frame_skip)&0x01; if (tmp1==0x01) { info.skipFrameCount = preInfo.skipFrameCount + 1; }else{ info.skipFrameCount = preInfo.skipFrameCount; } tmp1 = (frameInit>>frameType::frame_texture)&0x01; if (tmp1==0x01) { info.texture = preInfo.texture + 1; }else{ info.texture = preInfo.texture; } tmp1 = (frameInit>>frameType::frame_blend)&0x01; if (tmp1==0x01) { info.blend = preInfo.blend + 1; }else{ info.blend = preInfo.blend; } tmp1 = (frameInit>>frameType::frame_Color)&0x01; if (tmp1==0x01) { info.color = preInfo.color + 1; }else{ info.color = preInfo.color; } tmp1 = (frameInit>>frameType::frame_alpha)&0x01; if (tmp1==0x01) { info.alpha = preInfo.alpha + 1; }else{ info.alpha = preInfo.alpha; } tmp1 = (frameInit>>frameType::frame_uv)&0x01; if (tmp1==0x01) { info.uv = preInfo.uv + 1; }else{ info.uv = preInfo.uv; } tmp1 = (frameInit>>frameType::frame_indices)&0x01; if (tmp1==0x01) { info.indices = preInfo.indices + 1; }else{ info.indices = preInfo.indices; } tmp1 = (frameInit>>frameType::frame_vert)&0x01; if (tmp1==0x01) { info.vert = preInfo.vert + 1; }else{ info.vert = preInfo.vert; } bakeFrameInfos.push_back(info); } //加快性能用,这样不需要反复计算Size,同时对于始终没变的东西不需要反复更新 colorArraySize = (int)colorArray.size(); alphaArraySize = (int)alphaArray.size(); uvArraySize = (int)uvArray.size(); posArraySize = (int)posArray.size(); blendFuncArraySize = (int)blendFuncArray.size(); indicesArraySize = (int)indicesArray.size(); textureArraySize = (int)textureArray.size(); if (colorArraySize==1&&alphaArraySize==1&&uvArraySize==1&&blendFuncArraySize==1&&indicesArraySize==1) { isPosArrayChangeOnlySlot = true; } if(textureArraySize>0){ firstTexture = textureArray[0]; } if (blendFuncArraySize>0) { firstBlendFunc = blendFuncArray[0]; } } bool RedSlotBakeModel::isUVChange(int aCurrentFrame){ if (uvArraySize==1) { return false; } if (aCurrentFrame<2) { return false; } if (bakeFrameInfos[aCurrentFrame-2].uv!=bakeFrameInfos[aCurrentFrame-1].uv) { return true; } return false; } bool RedSlotBakeModel::getFrameSkip(int aCurrentFrame){ GLubyte frameInit = frameChangeArr[aCurrentFrame-1]; GLubyte tmp1 = (frameInit>>frameType::frame_skip)&0x01; if (tmp1==0x01) { return true; } return false; } void RedSlotBakeModel::setFrameSkip(int aCurrentFrame,BKAE_FRAME_PLAY& aFrameInfo){ GLubyte frameInit = frameChangeArr.at(aCurrentFrame-1); GLubyte tmp1 = (frameInit>>frameType::frame_skip)&0x01; if (tmp1==0x01) { aFrameInfo.skipFrameCount = MIN(aFrameInfo.skipFrameCount+1, frameChangeArr.size()-1); } } cocos2d::Texture2D* RedSlotBakeModel::getTexture(int aCurrentFrame){ if (textureArraySize) { return firstTexture; } cocos2d::Texture2D* aTexture = textureArray[bakeFrameInfos[aCurrentFrame-1].texture]; return aTexture; } BlendFunc& RedSlotBakeModel::getBlendFunc(int aCurrentFrame){ if (blendFuncArraySize==1) { return firstBlendFunc; } return blendFuncArray[bakeFrameInfos[aCurrentFrame-1].blend]; } void RedSlotBakeModel::getTriangles(int aCurrentFrame,BKAE_FRAME_PLAY& aFrameInfo){ BKAE_FRAME_INFO &bInfo = bakeFrameInfos[aCurrentFrame-1]; //读取烘培的顶点偏移量 std::vector &bpi_Arr = posArray[bInfo.vert]; int bpiSize = (int)bpi_Arr.size(); if (aFrameInfo.triangles.vertCount!=bpiSize) { if (aFrameInfo.triangles.verts != nullptr) { delete[] aFrameInfo.triangles.verts; } aFrameInfo.triangles.verts = new V3F_C4B_T2F[bpiSize]; aFrameInfo.triangles.vertCount = bpiSize;//改这个地方,如果顶线数遍了,再申请数据 aFrameInfo.hasOnceUpdate = false; } for (int aa = 0; aa1) { //读取烘培的UV std::vector ¤tFrameUV = uvArray[bInfo.uv]; aFrameInfo.currentFrameUV = ¤tFrameUV; // aFrameInfo.currentFrameUV = uvArray[bInfo.uv]; for (int aa = 0; aa1) { //读取烘培的Indices std::vector& indicesVect = indicesArray[bInfo.indices]; aFrameInfo.triangles.indexCount = (int)indicesVect.size(); aFrameInfo.triangles.indices = indicesVect.data(); } if (aFrameInfo.hasOnceUpdate==false||alphaArraySize>1||colorArraySize>1) { //读取烘培的Color Color3B& color = colorArray[bInfo.color]; aFrameInfo.currentFrameColor.r = color.r; aFrameInfo.currentFrameColor.g = color.g; aFrameInfo.currentFrameColor.b = color.b; //读取烘培的Aphla aFrameInfo.currentFrameColor.a = alphaArray[bInfo.alpha]; for (int aa = 0; aa posVect; for (int i = 0; i pre = posArray[posArray.size()-1]; if (_vertVectIsEqual(posVect, pre)==false) { posArray.push_back(posVect); GLubyte& lastframe = frameChangeArr.at(frameChangeArr.size()-1); lastframe = lastframe|byteMap[frameType::frame_vert]; } } //烘培颜色变化 if (colorArray.size()==0) { Color4B colors = aTriangles.verts[0].colors; colorArray.push_back(Color3B(colors.r, colors.g, colors.b)); }else{ Color4B colors = aTriangles.verts[0].colors; Color3B currentColor = Color3B(colors.r, colors.g, colors.b); Color3B lastColor = colorArray.at(colorArray.size()-1); if (lastColor.r!=currentColor.r||lastColor.g!=currentColor.g||lastColor.b!=currentColor.b) { colorArray.push_back(currentColor); GLubyte& lastframe = frameChangeArr.at(frameChangeArr.size()-1); lastframe = lastframe|byteMap[frameType::frame_Color]; } } //烘培alpha变化 if (alphaArray.size()==0) { Color4B colors = aTriangles.verts[0].colors; alphaArray.push_back(colors.a); }else{ GLubyte currentAlpha = aTriangles.verts[0].colors.a; GLubyte lastAlpha = alphaArray.at(alphaArray.size()-1); if (currentAlpha!=lastAlpha) { alphaArray.push_back(currentAlpha); GLubyte& lastframe = frameChangeArr.at(frameChangeArr.size()-1); lastframe = lastframe|byteMap[frameType::frame_alpha]; } } if (hasChangeAttachmentName) { //UV和顶点的变化必然伴随AttachmentName变化 //烘培UV变化 std::vector uvVect; for (int i = 0; i lastUV = uvArray.at(uvArray.size()-1); if (_uvVectIsEqual(uvVect, lastUV)==false) { uvArray.push_back(uvVect); GLubyte& lastframe = frameChangeArr.at(frameChangeArr.size()-1); lastframe = lastframe|byteMap[frameType::frame_uv]; } } //烘培顶点顺序数组 if (indicesArray.size()==0) { std::vector indicesVect; for (int i = 0; i lastIndices = indicesArray.at(indicesArray.size()-1); if (_indicesVectIsEqual(aTriangles.indices,aTriangles.indexCount, lastIndices)==false) { std::vector indicesVect; for (int i = 0; i a,std::vector b){ if (a.size()!=b.size()) { return false; } if (a.size()==0||b.size()==0) { if (a.size()==0&&b.size()==0) { return true; } return false; } Tex2F& a1 = a.at(0); Tex2F& b1 = b.at(0); if (a1.u!=b1.u||a1.v!=b1.v) { return false; } a1 = a.at(a.size()-1); b1 = b.at(b.size()-1); if (a1.u!=b1.u||a1.v!=b1.v) { return false; } a1 = a.at((int)(a.size()/2)); b1 = b.at((int)(b.size()/2)); if (a1.u!=b1.u||a1.v!=b1.v) { return false; } return true; } bool RedSlotBakeModel::_indicesVectIsEqual(unsigned short *a,int aCount,std::vector b){ if (aCount!=b.size()) { return false; } if (aCount==0||b.size()==0) { if (aCount==0&&b.size()==0) { return true; } return false; } short a1 = a[0]; short b1 = b.at(0); if (a1!=b1||a1!=b1) { return false; } a1 = a[aCount-1]; b1 = b.at(b.size()-1); if (a1!=b1||a1!=b1) { return false; } a1 = a[(int)(aCount/2)]; b1 = b.at((int)(b.size()/2)); if (a1!=b1||a1!=b1) { return false; } return true; } bool RedSlotBakeModel::_vertVectIsEqual(std::vector &a,std::vector &b){ if (a.size()!=b.size()) { return false; } if (a.size()==0||b.size()==0) { if (a.size()==0&&b.size()==0) { return true; } return false; } for (int i = 0; i0) { return (int)posArray[0].size(); } return 0; } int RedSlotBakeModel::getMaxXPos(){ return maxXPos; } int RedSlotBakeModel::getMaxYPos(){ return maxYPos; } int RedSlotBakeModel::getMinXPos(){ return minXPos; } int RedSlotBakeModel::getMinYPos(){ return minYPos; } void RedSlotBakeModel::setMaxXPos(int pos){ maxXPos = pos; } void RedSlotBakeModel::setMaxYPos(int pos){ maxYPos = pos; } void RedSlotBakeModel::setMinXPos(int pos){ minXPos = pos; } void RedSlotBakeModel::setMinYPos(int pos){ minYPos = pos; } const std::vector>& RedSlotBakeModel::getPosArray(){ return posArray; } void RedSlotBakeModel::addPosArray(const std::vector& aPosArray){ posArray.push_back(aPosArray); } const std::vector& RedSlotBakeModel::getBlendFuncArray(){ return blendFuncArray; } void RedSlotBakeModel::addBlendFuncToArray(const BlendFunc& blendFunc){ blendFuncArray.push_back(blendFunc); } const std::vector& RedSlotBakeModel::getColorArray(){ return colorArray; } void RedSlotBakeModel::addColorToArray(const Color3B& color){ colorArray.push_back(color); } const std::vector& RedSlotBakeModel::getAlphaArray(){ return alphaArray; } void RedSlotBakeModel::addAlphaToArray(GLubyte Alpha) { alphaArray.push_back(Alpha); } const std::vector>& RedSlotBakeModel::getUvArray(){ return uvArray; } void RedSlotBakeModel::addUvArray(const std::vector& aUvArray){ uvArray.push_back(aUvArray); } const std::vector>& RedSlotBakeModel::getIndicesArray(){ return indicesArray; } void RedSlotBakeModel::addIndicesArray(const std::vector& aIndicesArray){ indicesArray.push_back(aIndicesArray); } const std::vector& RedSlotBakeModel::getFrameChangeArr(){ return frameChangeArr; } void RedSlotBakeModel::addFrameChangeToArr(GLubyte aFrameChange){ frameChangeArr.push_back(aFrameChange); } const std::vector& RedSlotBakeModel::getTextureArray(){ return textureArray; } void RedSlotBakeModel::addTextureToArray(cocos2d::Texture2D* texture){ textureArray.push_back(texture); } void RedSlotBakeModel::getTriangles(int aCurrentFrame,BKAE_FRAME_PLAY& aFrameInfo,const Point& offSet,float dis,V3F_C4B_T2F *beginVert,bool merge){ BKAE_FRAME_INFO &bInfo = bakeFrameInfos[aCurrentFrame-1]; //读取烘培的顶点偏移量 std::vector &bpi_Arr = posArray[bInfo.vert]; int bpiSize = (int)bpi_Arr.size(); aFrameInfo.triangles.vertCount = bpiSize; if (aCurrentFrame<2||dis==0) { for (int aa = 0; aa &preBpi_Arr = posArray[prebInfo.vert]; for (int aa = 0; aa1) { //读取烘培的UV std::vector ¤tFrameUV = uvArray[bInfo.uv]; aFrameInfo.currentFrameUV = ¤tFrameUV; // aFrameInfo.currentFrameUV = uvArray[bInfo.uv]; for (int aa = 0; aa1) { //读取烘培的Indices std::vector& indicesVect = indicesArray[bInfo.indices]; aFrameInfo.triangles.indexCount = (int)indicesVect.size(); aFrameInfo.triangles.indices = indicesVect.data(); } if (aFrameInfo.hasOnceUpdate==false||alphaArraySize>1||colorArraySize>1) { //读取烘培的Color Color3B& color = colorArray[bInfo.color]; aFrameInfo.currentFrameColor.r = color.r; aFrameInfo.currentFrameColor.g = color.g; aFrameInfo.currentFrameColor.b = color.b; //读取烘培的Aphla aFrameInfo.currentFrameColor.a = alphaArray[bInfo.alpha]; for (int aa = 0; aa