前端之家收集整理的这篇文章主要介绍了
cocos2d-x用shader来实现阴影,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
不废话,直接上代码,在cocos2d-x 3.3rc0中使用,通过.
cocos2d-X在Sprite上使用shader
@H_
502_21@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
@H_
502_21@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
@H_
502_21@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,shadowMask *shadowOpacity);
gl_FragColor = composite(textureColor,shadowColor);
}
原文链接:https://www.f2er.com/cocos2dx/345753.html