cocos2d-x 3.0 版本 骨骼动画设置shader

时间:2021-01-16 00:53:14

因为骨骼动画是由多个sprite组成, 所以需要遍历每个sprite 才能修改整体, 开头这样设置,在游戏中发现走路状态没问题,攻击状态就有部分sprite没效果

    for (auto & object : armObj->getBoneDic())
{
if (cocostudio::Bone *bone = dynamic_cast<cocostudio::Bone *>(object.second))
{
Node
*node = bone->getDisplayRenderNode();

if (nullptr == node)
continue;
cocostudio::Skin
*skin = static_cast<cocostudio::Skin *>(node);

skin
->setGLProgramState(shader);
}
}

 查下底层代码,必须从DecorativeDisplayList中取得renderNode

for (auto & object : armObj->getBoneDic())
{
if (cocostudio::Bone *bone = dynamic_cast<cocostudio::Bone *>(object.second))
{
       
const cocos2d::Vector<cocostudio::DecorativeDisplay*>& list = bone->getDisplayManager()->getDecorativeDisplayList();
for (auto & display : list)
{
Node
*node = display->getDisplay();

node
->setGLProgramState(glProgramState);
}
}
}

 另外设置了新shader后会导致cocos2d-x 渲染时DP不合并,查下了底层代码,发现QuadCommond generateMaterialID 该函数引起。

当ProgrameState 的uniformCount等于0 时, 会把matrierialID 设置为0。  (不明白cocos2d-x 底层为啥要这样做)....

用以下方法,即可让DP合并,目前测试游戏跑下来没啥大问题

void QuadCommand::generateMaterialID()
{

if(_glProgramState->getUniformCount() > 0)
{
// _materialID = QuadCommand::MATERIAL_ID_DO_NOT_BATCH;
int glProgram = (int)_glProgramState->getGLProgram()->getProgram();
int intArray[4] = { glProgram, (int)_textureID, (int)_blendType.src, (int)_blendType.dst };

_materialID
= XXH32((const void*)intArray, sizeof(intArray), 0);
}
else
{
int glProgram = (int)_glProgramState->getGLProgram()->getProgram();
int intArray[4] = { glProgram, (int)_textureID, (int)_blendType.src, (int)_blendType.dst};

_materialID
= XXH32((const void*)intArray, sizeof(intArray), 0);
}
}