cocos2d-js Shader系列3:多重紋理 multiple textures multiple samplers


上一篇,我們學習了怎么便捷的控制sprite的顏色,而這個都是默認一個texture的,如果要實現類似mask的效果,或者更個性化的多紋理效果,怎么實現呢?

這就是這一節需要介紹的內容。

Image(42)

例如上圖的效果,下方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后)



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM