上一篇,我们学习了怎么便捷的控制sprite的颜色,而这个都是默认一个texture的,如果要实现类似mask的效果,或者更个性化的多纹理效果,怎么实现呢?

这就是这一节需要介绍的内容。

例如上图的效果,下方2个球是原图,而上方的图就是由2个球通过某个公式合成的效果了。这里重点不是怎么合成,而是怎么把多个纹理推送到fragment shader中。

相信大家都会想到,首先需要在fragment shader中添加多一个Sample2D:

uniform sampler2D CC_Texture0
uniform sampler2D CC_Texture1

但是通过简单的绑定纹理,只能绑定到第一个sampler上:

this.tex1 = cc.textureCache.addImage("res2/item_2.png");
gl.bindTexture(gl.TEXTURE_2D, this.tex1)

那么关键点来了,我们需要利用gl的32个texture缓存。

this.tex1Location = gl.getUniformLocation(p, "CC_Texture0");
this.tex2Location = gl.getUniformLocation(p, "CC_Texture1"); gl.activeTexture(gl.TEXTURE0); //webgl中一共32个,可以看cocos2d列的常量
gl.bindTexture(gl.TEXTURE_2D, this.tex1.getName());
gl.uniform1i(this.tex1Location, 0); //把CC_Texture0指向gl.TEXTURE0
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, this.tex2.getName());
gl.uniform1i(this.tex1Location, 1);

核心是先激活某个纹理缓存,然后绑定sampler2D到对应的位置。最后,我们需要记得在draw之后清空,否则会影响下一个Node的绘制。

gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindBuffer(gl.ARRAY_BUFFER, null);

全部代码:

var MultiTexturesLayer = cc.Layer.extend({
ctor:function() {
this._super();
if( 'opengl' in cc.sys.capabilities ) {
var node1 = new cc.Sprite("res2/item_2.png");
var node2 = new cc.Sprite("res2/item_3.png");
this.addChild(node1);
this.addChild(node2);
node1.x = 500;
node2.x = 200;
node1.y = node2.y = 400;
var glnode = new cc.Node();
this.addChild(glnode,10);
this.glnode = glnode;
var winSize = cc.director.getWinSize();
glnode.x = winSize.width/2;
glnode.y = winSize.height/2;
glnode.width = 128;
glnode.height = 128;
glnode.anchorX = 0.5;
glnode.anchorY = 0.5;
var MULTI_TEXTURES_FRAGMENT_SHADER =
"precision lowp float; \n"
+ "varying vec2 v_texCoord; \n"
+ "uniform sampler2D CC_Texture0; \n"
+ "uniform sampler2D CC_Texture1; \n"
+ "void main() \n"
+ "{ \n"
+ " vec4 color1 = texture2D(CC_Texture0, v_texCoord); \n"
+ " vec4 color2 = texture2D(CC_Texture1, v_texCoord); \n"
+ " gl_FragColor = vec4(color1.r*color2.r, color1.g*color2.g, color1.b*color2.b, color1.a*color2.a); \n"
+ "}";
var DEFAULT_VERTEX_SHADER =
"attribute vec4 a_position; \n"
+ "attribute vec2 a_texCoord; \n"
+ "varying mediump vec2 v_texCoord; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
+ " v_texCoord = a_texCoord; \n"
+ "}";
this.shader = new cc.GLProgram();
this.shader.initWithVertexShaderByteArray(DEFAULT_VERTEX_SHADER, MULTI_TEXTURES_FRAGMENT_SHADER);
this.shader.addAttribute(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION);
this.shader.addAttribute(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS);
this.shader.link();
this.shader.updateUniforms(); //绑定位置,这个是cocos封装后必须做的事。详细可以看代码
this.initGL();
var p = this.shader.getProgram();
this.tex1Location = gl.getUniformLocation(p, "CC_Texture0"); //如果frag shader最终没有用某个uniform,该uniform会被优化删掉
this.tex2Location = gl.getUniformLocation(p, "CC_Texture1");
trace(this.tex1Location, this.tex2Location);
glnode.draw = function() {
this.shader.use(); //使用这个shader来绘制,封装了gl的use。跟指定glnode.shaderProgram类似
this.shader.setUniformsForBuiltins(); //设置坐标系变换
gl.activeTexture(gl.TEXTURE0); //webgl中一共32个,可以看cocos2d列的常量
gl.bindTexture(gl.TEXTURE_2D, this.tex1.getName());
gl.uniform1i(this.tex1Location, 0); //把CC_Texture0指向gl.TEXTURE0
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, this.tex2.getName());
gl.uniform1i(this.tex2Location, 1);
cc.glEnableVertexAttribs( cc.VERTEX_ATTRIB_FLAG_TEX_COORDS | cc.VERTEX_ATTRIB_FLAG_POSITION); //实际对gl的api做了封装,增加了这两个属性的位置映射。用于vertexAttribPointer
// Draw fullscreen Square
gl.bindBuffer(gl.ARRAY_BUFFER, this.squareVertexPositionBuffer);
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, this.squareVertexTextureBuffer);
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, null); //使用完必须置为空,否则影响其他node
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}.bind(this);
}
},
initGL:function() {
var tex1 = cc.textureCache.addImage("res2/item_2.png");
var tex2 = cc.textureCache.addImage("res2/item_3.png");
this.tex1 = tex1;
this.tex2 = tex2;
//
// Square
//
var squareVertexPositionBuffer = this.squareVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
var vertices = [
128, 128,
0, 128,
128, 0,
0, 0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
var squareVertexTextureBuffer = this.squareVertexTextureBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureBuffer);
var texcoords = [
0, 0,
1, 0,
0, 1,
1, 1
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texcoords), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
});
2014.11.07 补充:
由于3.1版本关于坐标系的计算发生了改变,上述例子不会正常显示。
 
需要扩展cc.Node为cc.GLNode。
 
cc.GLNode = cc.GLNode || cc.Node.extend({
ctor:function(){
this._super();
this.init();
},
_initRendererCmd:function(){
this._rendererCmd = new cc.CustomRenderCmdWebGL(this, function(){
cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
cc.kmGLPushMatrix();
cc.kmGLLoadMatrix(this._stackMatrix); this.draw(); cc.kmGLPopMatrix();
});
}
});
另外,在new cc.GLProgram后retain一下就可以兼容jsb了(cocos2d-js 3.1后)

cocos2d-js Shader系列3:多重纹理 multiple textures multiple samplers的更多相关文章

  1. JS组件系列——BootstrapTable 行内编辑解决方案:x-editable

    前言:之前介绍bootstrapTable组件的时候有提到它的行内编辑功能,只不过为了展示功能,将此一笔带过了,罪过罪过!最近项目里面还是打算将行内编辑用起来,于是再次研究了下x-editable组件 ...

  2. JS组件系列——Bootstrap Select2组件使用小结

    前言:在介绍select组件的时候,博主之前分享过一篇JS组件系列——两种bootstrap multiselect组件大比拼,这两个组件的功能确实很强大,只可惜没有图文结合的效果(也就是将图片放入到 ...

  3. Shader Model 3.0:Using Vertex Textures SM3:使用顶点纹理 (NVIDIA spec, 6800支持使用D3DFMT_R32F and D3DFMT_A32B32G32R32F的纹理格式实现Vertex Texture。)

    翻译者 周波 zhoubo22@hotmail.com 版权所有 Philipp Gerasimov Randima (Randy) Fernando Simon Green NVIDIA Corpo ...

  4. JS组件系列——又一款MVVM组件:Vue(二:构建自己的Vue组件)

    前言:转眼距离上篇 JS组件系列——又一款MVVM组件:Vue(一:30分钟搞定前端增删改查) 已有好几个月了,今天打算将它捡起来,发现好久不用,Vue相关技术点都生疏不少.经过这几个月的时间,Vue ...

  5. JS组件系列——再推荐一款好用的bootstrap-select组件,亲测还不错

    前言:之前分享过两篇bootstrap下拉框的组件:JS组件系列——两种bootstrap multiselect组件大比拼  和 JS组件系列——Bootstrap Select2组件使用小结 ,收 ...

  6. cocos2d-js Shader系列1:cocos2d-js Shader和OpenGL ES2.0

    cocos2d的Shader也就是差不多直接跟GPU打交道了,跟Flash的Stage3D(AGAL)类似,不过没有AGAL这么恶心,不需要直接编写汇编语言.而Fragment Shader又跟Fla ...

  7. [转]JS组件系列——BootstrapTable 行内编辑解决方案:x-editable

    本文转自:http://www.cnblogs.com/landeanfen/p/5821192.html 阅读目录 一.x-editable组件介绍 二.bootstrapTable行内编辑初始方案 ...

  8. JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(一)

    前言:出于某种原因,需要学习下Knockout.js,这个组件很早前听说过,但一直没尝试使用,这两天学习了下,觉得它真心不错,双向绑定的机制简直太爽了.今天打算结合bootstrapTable和Kno ...

  9. JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(四):自定义T4模板快速生成页面

    前言:上篇介绍了下ko增删改查的封装,确实节省了大量的js代码.博主是一个喜欢偷懒的人,总觉得这些基础的增删改查效果能不能通过一个什么工具直接生成页面效果,啥代码都不用写了,那该多爽.于是研究了下T4 ...

随机推荐

  1. cat、tac、rev、nl命令

    当日志文件log.log很长,但又要按内容从后往前查看时,可以使用如下命令: tac log.log | more cat     由第一行开始显示内容,并将所有内容输出    tac     从最后 ...

  2. .AVLFile Extension

    .AVLFile Extension File Type 1ArcView Legend File   Developer ESRI Popularity           4.1 (7 Votes ...

  3. PHP导出大数据

    保存到本地 <?php // a db link for queries $lh = mysql_connect( '127.0.0.1', 'root', '' ); // and a con ...

  4. 5句mysql语句

    显示表的结构: mysql> DESCRIBE MYTABLE; 往表中加入记录 mysql> insert into MYTABLE values ("hyq",&q ...

  5. Leap Motion 上手体验

    很早之前就关注了Leap Motion这个颠覆性的体感操作设备,如今7月22日上市至今已经一个月左右,淘宝的价格也已经降到650元,虽说相对国外还是偏贵,但是已经忍不住尝尝鲜了. Leap Motio ...

  6. Windbg命令学习15(bp bm bu bl bc ba断点)

    以下以skinhgy为例,windbg附加运行 1. bp 命令是在某个地址下断点, 可以 bp 0x7783FEB 也可以 bp MyApp!SomeFunction . 对于后者,WinDBG 会 ...

  7. CListCtrl 之右键菜单

    在使用CListCtrl时要为它添加一个右键菜单,步骤如下: 1. 响应CListCtrl的NM_RCLICK消息. 2. 添加一个菜单资源,在菜单资源中插入要添加到菜单内容.   一般存在两种方法: ...

  8. JQuery Ajax 在asp.net中使用总结

    自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...

  9. Python Numpy模块函数np.c_和np.r_

    np.r_:是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等,类似于pandas中的concat(). np.c_:是按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等,类似于pandas中的 ...

  10. [转]Python机器学习工具箱

    原文在这里  Python在科学计算领域,有两个重要的扩展模块:Numpy和Scipy.其中Numpy是一个用python实现的科学计算包.包括: 一个强大的N维数组对象Array: 比较成熟的(广播 ...