cocos2d-x用shader来实现阴影

不废话,直接上代码,在cocos2d-x 3.3rc0中使用,通过.



 cocos2d-X在Sprite上使用shader


auto sprite = Sprite::create("test2.png");
sprite->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height /2 );

auto shader_program = GLProgram::createWithFilenames("shadow.vsh", "shadow.fsh");
shader_program->use();
shader_program->setUniformsForBuiltins();

sprite->setGLProgram(shader_program);
this->addChild(sprite);



shadow.vsh

attribute vec4 a_position;
attribute vec2 a_texCoord;

varying vec2 v_texCoord;
void main()
{
gl_Position = CC_PMatrix * a_position;
v_texCoord = a_texCoord;
}


shadow.fsh



varying vec2 v_texCoord;

vec4 composite(vec4 over, vec4 under)
{
return over + (1.0 - over.a)*under;
}
void main(){
vec2 shadowOffset = vec2(0.03, 0.03);
vec4 textureColor = texture2D(CC_Texture0, v_texCoord + shadowOffset);
float shadowMask = texture2D(CC_Texture0, v_texCoord ).a;
const float shadowOpacity = 0.5;
vec4 shadowColor = vec4(0,0,0,shadowMask *shadowOpacity);
gl_FragColor = composite(textureColor, shadowColor);
}


你可能感兴趣的:(cpp,shadow,cocos2d-x,shader)