位置: 编程技术 - 正文

在cocos2dx中实现水波滤镜(cocos2dx schedule)

编辑:rootadmin

推荐整理分享在cocos2dx中实现水波滤镜(cocos2dx schedule),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:cocos2dx schedule,cocos2dx运行原理,cocos2djs,cocos2djs,cocos2d schedule,cocos2dx schedule,cocos2dx schedule,cocos2dx怎么用,内容如对您有帮助,希望把文章链接给更多的朋友!

因为工作原因,开始转向cocos2d-x开发方向了。自然的,凭着之前引擎的经验偏向底层渲染研究。

在此期间看了两本书《cocos2d-x 权威指南》《cocos2d-x 高级开发教程》不得不说,第一本书完全是API介绍,也许适合新手入门,但是于我,只是烂书一本。第二本书还是不错的,最后的项目有全套源码,教程的风&#;也很有我的风&#;,哈哈,就是文章中不一定贴全代码,只贴核心,后期还要自己领会补全。

言归正传,在 《高级开发教程》 中有一篇是实现海底水纹效果的文章,里面代码不全,且代码和Shader并没有对应起来,于是我决定按照自己思路来将其补全,并贴上完整源码。

先上效果图:动态GIF图地址:

首先创建一个 ShaderNode.h 类并继承CCNode

#ifndef __FishingJoy_ShaderNode__#define __FishingJoy_ShaderNode__#include "cocos2d.h"USING_NS_CC;class ShaderNode : public CCNode{public: ShaderNode(); bool initWithVertex(const char *vert, const char *frag); void loadShaderVertex(const char *vert, const char *frag); virtual void update(float delta); virtual void setContentSize(const CCSize& var); virtual void setColor(ccColor4F newColor); virtual void draw(); static ShaderNode* shaderNodeWithVertex(const char *vert, const char *frag);private: GLuint m_uniformResolution, m_uniformTime, m_uniformTex0; GLuint m_attributeColor, m_attributePosition; float m_time; ccVertex2F m_resolution,m_center; GLuint m_texture; GLfloat color[4];};#endif

之后创建ShaderNode.cpp 并实现 ShaderNode.h 中的各方法

#include "ShaderNode.h"ShaderNode::ShaderNode(){}ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag){ ShaderNode* shader = new ShaderNode(); if(shader && shader->initWithVertex(vert,frag)) { shader->autorelease(); return shader; } CC_SAFE_DELETE(shader); return NULL;}void ShaderNode::loadShaderVertex(const char *vert, const char *frag){ CCGLProgram* shader = new CCGLProgram(); shader->initWithVertexShaderFilename(vert, frag); //载入着色器程序 //绑定attribute变量 shader->addAttribute("a_position", 0); shader->addAttribute("a_color", 1); shader->link(); //获取attribute变量标识 m_attributeColor = glGetAttribLocation(shader->getProgram(), "a_color"); m_attributePosition = glGetAttribLocation( shader->getProgram(), "a_position"); shader->updateUniforms(); //获取uniform变量标识 m_uniformResolution = glGetUniformLocation(shader->getProgram(), "resolution"); m_uniformTime = glGetUniformLocation(shader->getProgram(), "time"); m_uniformTex0 = glGetUniformLocation(shader->getProgram(), "tex0"); //使用着色器程序 this->setShaderProgram(shader); shader->release(); }void ShaderNode::setColor(ccColor4F newColor){ color[0] = newColor.r; color[1] = newColor.g; color[2] = newColor.b; color[3] = newColor.a;}bool ShaderNode::initWithVertex(const char *vert, const char *frag){ loadShaderVertex(vert,frag); m_texture = CCTextureCache::sharedTextureCache()->addImage("HelloWorld.png")->getName(); setContentSize(CCSizeMake(,)); setColor(ccc4f(0.5,0.5,1,1)); m_time = 0; scheduleUpdate(); return true;}void ShaderNode::update(float dt){ m_time &#;= dt;}void ShaderNode::setContentSize(const CCSize& var){ CCNode::setContentSize(var); m_resolution = vertex2(getContentSize().width,getContentSize().height); m_center.x = m_resolution.x/2; m_center.y = m_resolution.y/2;}void ShaderNode::draw(){ CC_NODE_DRAW_SETUP(); //传递uniform变量 CCGLProgram* shader = getShaderProgram(); shader->setUniformLocationWith2f(m_uniformResolution, m_resolution.x, m_resolution.y); shader->setUniformLocationWith1i(m_uniformTex0, 0); glUniform1f(m_uniformTime, m_time); //获取attribute变量 CCSize size = this->getContentSize(); float w = size.width; float h = size.height; ccGLBindTexture2D(m_texture); //绑定纹理到槽位 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, w, h, 0); //截取屏幕数据到纹理 glEnableVertexAttribArray(m_attributePosition); glDisableVertexAttribArray(m_attributeColor); //传递attribute变量 GLfloat vertices[] = { 0, 0, //左下0 w, 0, //右下1 w, h, //右上2 0, 0, //左下0 0, h, //左上3 w, h, //右上2 }; glVertexAttribPointer(m_attributePosition, 2, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttrib4fv(m_attributeColor, color); //绘制 glDrawArrays(GL_TRIANGLES, 0, 6); }在cocos2dx中实现水波滤镜(cocos2dx schedule)

之后在场景类中添加ShaderNode 即可

ShaderNode* shader = ShaderNode::shaderNodeWithVertex("shader.vsh","shader.fsh"); shader->setContentSize(getContentSize()); shader->setColor(ccc4f(1,1,1.0,.5)); this->addChild(shader,2);

shader.vsh 和 shader.fsh 可以从网络中得到,就是glsl文件shader.vsh 的内容为

attribute vec4 a_color; attribute vec4 a_position; varying vec4 v_color; uniform mat4 u_MVPMatrix; void main() { v_color = a_color; gl_Position = u_MVPMatrix * a_position; }

shader.fsh 的内容为

5varying vec4 v_color; uniform sampler2D tex0; precision highp float; uniform float time; uniform vec2 resolution; const float PI = 3.; const float speed = 0.2; const float speed_x = 0.3; const float speed_y = 0.3; const float intensity = 3.0; const int steps = 8; const float frequency = 4.0; const int angle = 7; const float delta = .0; const float intence = .0; const float emboss = 0.3; float col(vec2 coord) { float delta_theta = 2.0 * PI / float(angle); float col = 0.0; float theta = 0.0; for (int i = 0; i < steps; i&#;&#;) { vec2 adjc = coord; theta = delta_theta * float(i); adjc.x &#;= cos(theta)*time*speed &#; time * speed_x; adjc.y -= sin(theta)*time*speed - time * speed_y; col = col &#; cos((adjc.x*cos(theta) - adjc.y*sin(theta)) *frequency)*intensity; } return cos(col);} void main(void) { vec2 p = (gl_FragCoord.xy) / resolution.xy, c1 = p, c2 = p; float cc1 = col(c1); c2.x &#;= resolution.x/delta; float dx = emboss*(cc1-col(c2))/delta; c2.x = p.x; c2.y &#;= resolution.y/delta; float dy = emboss*(cc1-col(c2))/delta; c1.x &#;= dx; c1.y &#;= dy; float alpha = 1.&#;dot(dx,dy)*intence; gl_FragColor = texture2D(tex0,c1)*(alpha) *v_color*(alpha); }

github地址为: u_MVPMatrix我们在cocos2d-x中使用它的时候,需要把 CC_MVPMatrix 赋&#;给 u_MVPMatrix或者直接把 u_MVPMatrix 删除,替换成 CC_MVPMatrixlike this

attribute vec4 a_color; attribute vec4 a_position; varying vec4 v_color; void main() { v_color = a_color; gl_Position = CC_MVPMatrix * a_position; }

cocos2d-x 在创建 GLProgram 的时候,会自动在 GLSL的文件之前加上 cocos2d-x 自身需要的参数

const GLchar *sources[] = {#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN && CC_TARGET_PLATFORM != CC_PLATFORM_LINUX && CC_TARGET_PLATFORM != CC_PLATFORM_MAC) (type == GL_VERTEX_SHADER ? "precision highp float;n" : "precision mediump float;n"),#endif "uniform mat4 CC_PMatrix;n" "uniform mat4 CC_MVMatrix;n" "uniform mat4 CC_MVPMatrix;n" "uniform vec4 CC_Time;n" "uniform vec4 CC_SinTime;n" "uniform vec4 CC_CosTime;n" "uniform vec4 CC_Random;n" "//CC INCLUDES ENDnn", source, };

that’s all.本文对于原书教程补充的地方如下修改 shader.vsh 替换 u_MVPMatrix 为 CC_MVPMatrix删除 ShaderNode.cpp 中全部的 m_uniformCenter 的相关代码, m_uniformCenter 在Shader中并没有该字段,没有补完的意义添加 ShaderNode.cpp 构造函数及SetColor方法修改 ShaderNode.h 中 update 的参数

上文来自:

cocos2dx3.6 弹出对话框的实现 头文件:////PopAlertDialog.h//macstudycocos2dx////CreatedbyWangWeion/6/8.////#ifndef__macstudycocos2dx__PopAlertDialog__#define__macstudycocos2dx__PopAlertDialog__#pragmaonce#includecocos2d.h#in

设置glblendfunc出现黑框 发现用粒子编辑器设置粒子效果的混合模式,放到程序里会出现偏差,比如黑框等。搜了下,发现原因是:DestBlendFunc只接受以下8个参数:GL_ZEROGL_ONEGL_SR

cocos2dx多个精灵调用runAction()方法执行组合动作,只有最后一个精灵有效的问题 问题代码如下//将MoveTo和RotateTo两个动作转换成FiniteTimeAction类型FiniteTimeAction*MVto=(FiniteTimeAction*)(MoveTo::create(2.0,Vec2(,)));FiniteTimeAction*ROto=(FiniteTimeAction*

标签: cocos2dx schedule

本文链接地址:https://www.jiuchutong.com/biancheng/368365.html 转载请保留说明!

上一篇:Cocos2d-3.x_在Lua中使用cjson库解析json(cocoscreator lua)

下一篇:cocos2dx3.6 弹出对话框的实现(cocos2djs)

  • 报税残疾人保障费怎么算
  • 小规模纳税人销售已使用固定资产
  • 小规模纳税人零申报逾期未申报
  • 所得税费用影响净利润吗
  • 要补交税怎么办
  • 营业费用和营业收入的关系
  • 零售盘点窍门
  • 取得失控发票
  • 生产型企业付的消防年检费记什么科目
  • 收保险赔偿款如何处理?
  • 没有实收资本是负债吗
  • 减半征收政策
  • 增值税减少在借方还是贷方
  • 旅游业是一个怎样的产业
  • 出租不动产什么时候交税
  • 发票开了款没付
  • 公司发手机奖励合法吗
  • 超额用电罚款应由谁缴纳
  • 驾校如何用完工百分比法确认收入?
  • windows在cmd设置变量
  • 职工福利费扣除率是多少
  • 税控服务费政策
  • 我想测试一下网速
  • 法人把资产转移到国外有没有限制
  • 电脑开不了机一直重启
  • vantUI van-picker中的column使用,picker选择器展示对象数组里面的属性,自定义展示数据
  • 梅德威海滩棕榈树间散步的冲浪者,印尼巴厘岛 (© helivideo/GettyImages)
  • 属于工业企业期间费用的是
  • 应退税款抵扣欠税款的账务处理
  • adamax优化器
  • 搭建自己的php mvc框架
  • netty websocket心跳
  • 又拍云使用教程
  • 手把手教你如何套路男神
  • 百旺开票软件状态查询在哪里
  • 职工食堂费用计入什么科目
  • 缴纳增值税会计科目
  • 律师费开票税费谁承担
  • cmd 访问mysql
  • 经营范围没有的项目开票了会怎么样
  • 未缴增值税的贷方
  • 增值税发票是记账联还是抵扣联
  • linux大版本升级
  • 不动产租赁服务包括哪些
  • 以前年度多计管理费用怎么调整
  • 主营业务成本借贷方向增减
  • 冲去年管理费用
  • 净资产利润率的公式是什么
  • 医保账户收入金额是怎么算的
  • 电子承兑汇票的风险有哪些
  • 首先要知道什么英语
  • windows2000修改ip
  • windows2003企业版sp2密钥
  • WINDOWS7系统安装包
  • ubuntu虚拟机怎么联网
  • ·exe病毒
  • xp查看用户名和密码
  • spyagent4.exe - spyagent4是什么进程
  • win7启动应用程序的方式
  • centos7安装软件包命令
  • Win7 64位纯净版无法安装第三方主题的可行破解方法
  • win10如何创建一个新的用户
  • win8 开始
  • 项目部不足和改进
  • 在android的activity活动中
  • 行为怪异的人有问题吗
  • python os模块
  • node.js java 性能
  • shell脚本监听端口
  • python计算文件大小
  • js设置标签内容
  • javascript消息对话框
  • jquery悬浮显示
  • android设计模式与最佳实践 电子版
  • 企业所得税纳税申报表A类
  • 深圳税务 qzzn
  • 个税申报表明细怎么打印
  • 关于新时代新征程
  • 国税增值税普通发票
  • 非中山户籍学生可以自己买医保吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设