cocos2dx中毒冰冻shader

中毒效果

中毒

#ifdef GL_ES   
precision mediump float;   
#endif   
uniform sampler2D u_texture;   
varying vec2 v_texCoord;   
varying vec4 v_fragmentColor;   
void main(void)   
{   
    gl_FragColor = texture2D(u_texture, v_texCoord) * v_fragmentColor;  
    gl_FragColor.r *= 0.8;  
    gl_FragColor.r += 0.08 * gl_FragColor.a;  
    gl_FragColor.g *= 0.8;  
    gl_FragColor.b *= 0.8;  
    gl_FragColor.g += 0.2 * gl_FragColor.a;  
   //gl_FragColor= vec4(color.r,color.g, color.b,color.a) ;  
}  
冰冻效果

冰冻

#ifdef GL_ES   
precision mediump float;   
#endif   
uniform sampler2D u_texture;   
varying vec2 v_texCoord;   
varying vec4 v_fragmentColor;   
void main(void)   
{   
    vec4 normalColor = v_fragmentColor * texture2D(u_texture, v_texCoord);  
    normalColor *= vec4(0.8, 0.8, 0.8, 1);  
    normalColor.b += normalColor.a * 0.2;  
    gl_FragColor = normalColor;  
}  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Cocos 刮刮卡 shader 可以实现在图片或者精灵上进行刮开效果的特效。具体实现方法如下: 1. 用 cocos2d-x 创建一个新的项目。 2. 在项目中创建一个精灵,加载一张背景图片。 3. 创建一个 Shader 文件,命名为 ScratchCard.fsh。 4. 在 ScratchCard.fsh 中编写 shader 代码,实现刮开效果,代码如下: ``` varying vec2 v_texCoord; uniform sampler2D CC_Texture0; uniform sampler2D brushTexture; uniform float u_threshold; void main() { vec4 color = texture2D(CC_Texture0, v_texCoord); vec4 brush = texture2D(brushTexture, v_texCoord); float luminance = dot(color.rgb, vec3(0.299, 0.587, 0.114)); float mask = step(luminance, u_threshold); gl_FragColor = mix(color, vec4(0, 0, 0, 0), mask * brush.a); } ``` 5. 在 C++ 代码中加载 Shader 文件,将 Shader 绑定到精灵上。 ``` auto shader = GLProgram::createWithFilenames("ScratchCard.vsh", "ScratchCard.fsh"); auto sprite = Sprite::create("background.png"); sprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); sprite->setGLProgram(shader); this->addChild(sprite); ``` 6. 创建一个 Brush 精灵,用于擦除操作。 ``` auto brush = Sprite::create("brush.png"); brush->setVisible(false); this->addChild(brush); ``` 7. 在触摸事件中实现刮开效果,将 Brush 精灵的位置设置为触摸点的位置,并且将 Brush 精灵显示出来。 ``` bool HelloWorld::onTouchBegan(Touch* touch, Event* event) { brush->setPosition(touch->getLocation()); brush->setVisible(true); return true; } void HelloWorld::onTouchMoved(Touch* touch, Event* event) { brush->setPosition(touch->getLocation()); } void HelloWorld::onTouchEnded(Touch* touch, Event* event) { brush->setVisible(false); } ``` 8. 在每帧更新中将 Brush 精灵的位置传递给 Shader,用于在精灵上实现擦除效果。 ``` void HelloWorld::update(float delta) { auto shader = sprite->getGLProgram(); shader->use(); shader->setUniformTexture("brushTexture", brush->getTexture()); shader->setUniformLocationWith1f(shader->getUniformLocation("u_threshold"), 0.7f); shader->setUniformLocationWith2f(shader->getUniformLocation("u_brushPosition"), brush->getPositionX(), brush->getPositionY()); } ``` 这样就实现了 cocos 刮刮卡 shader 效果
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值