我从cocos2dx 3.x开始研究的,发现网上有很多在2.x变灰的例子,甚至需要改源码,而我个人偏向于静态方法的封装的用法,所以整合总结并测试了如下方法。在cocos2dx 3.4版本通过,关于偏移问题确实恶心,查阅很多博文已解决,有了这个方法我就准备在下一篇实现一个技能CD效果的通用方案。
static voID SwitchGray (Sprite *sp,bool isGray) { GLchar* grayvsh = "attribute vec4 a_position; \n \ attribute vec2 a_texCoord; \n \ attribute vec4 a_color; \n \ varying vec4 v_fragmentcolor;\n \ varying vec2 v_texCoord;\n \ voID main(voID) \n \ { \n \ gl_position = CC_PMatrix * a_position;\n \ v_fragmentcolor = a_color;\n \ v_texCoord = a_texCoord;\n \ }"; if(isGray) { GLchar* grayfsh= "varying vec4 v_fragmentcolor;\n \ varying vec2 v_texCoord;\n \ voID main()\n \ {\n \ vec4 v_orcolor = v_fragmentcolor * texture2D(CC_Texture0,v_texCoord);\n \ float gray = dot(v_orcolor.rgb,vec3(0.299,0.587,0.114));\n \ gl_Fragcolor = vec4(gray,gray,v_orcolor.a);\n \ }"; GLProgram* pProgram = new GLProgram(); pProgram->initWithVertexShaderByteArray(grayvsh,grayfsh); sp->setShaderProgram(pProgram); pProgram->release(); CHECK_GL_ERROR_DEBUG(); } else { GLProgram* pProgram = new GLProgram(); //pProgram->initWithfilenames("gray.vsh","color.fsh"); pProgram->initWithVertexShaderByteArray(grayvsh,ccpositionTexturecolor_frag); sp->setShaderProgram(pProgram); pProgram->release(); CHECK_GL_ERROR_DEBUG(); } sp->getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_name_position,kCCVertexAttribFlag_position); sp->getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_name_position,kCCVertexAttrib_position); sp->getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_name_color,kCCVertexAttrib_color); sp->getShaderProgram()->addAttribute(GLProgram::ATTRIBUTE_name_TEX_COORD,kCCVertexAttrib_TexCoords); CHECK_GL_ERROR_DEBUG(); sp->getShaderProgram()->link(); CHECK_GL_ERROR_DEBUG(); sp->getShaderProgram()->updateUniforms(); CHECK_GL_ERROR_DEBUG();}总结
以上是内存溢出为你收集整理的cocos2dx 3.x版本 Sprite 灰度和彩色切换,无偏移全部内容,希望文章能够帮你解决cocos2dx 3.x版本 Sprite 灰度和彩色切换,无偏移所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)