123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- /****************************************************************************
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2011-2012 cocos2d-x.org
- Copyright (c) 2013-2014 Chukong Technologies Inc.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- cc._tmp.LayerDefineForWebGL = function(){
- var _p = cc.Layer.prototype;
- //Layer doesn't support bake function in WebGL
- _p.bake = function(){};
- _p.unbake = function(){};
- _p.visit = cc.Node.prototype.visit;
- };
- cc._tmp.WebGLLayerColor = function () {
- //cc.LayerColor define start
- var _p = cc.LayerColor.prototype;
- _p._squareVertices = null;
- _p._squareColors = null;
- _p._verticesFloat32Buffer = null;
- _p._colorsUint8Buffer = null;
- _p._squareVerticesAB = null;
- _p._squareColorsAB = null;
- _p.ctor = function (color, width, height) {
- var _t = this;
- _t._squareVerticesAB = new ArrayBuffer(32);
- _t._squareColorsAB = new ArrayBuffer(16);
- var locSquareVerticesAB = _t._squareVerticesAB, locSquareColorsAB = _t._squareColorsAB;
- var locVertex2FLen = cc.Vertex2F.BYTES_PER_ELEMENT, locColorLen = cc.Color.BYTES_PER_ELEMENT;
- _t._squareVertices = [new cc.Vertex2F(0, 0, locSquareVerticesAB, 0),
- new cc.Vertex2F(0, 0, locSquareVerticesAB, locVertex2FLen),
- new cc.Vertex2F(0, 0, locSquareVerticesAB, locVertex2FLen * 2),
- new cc.Vertex2F(0, 0, locSquareVerticesAB, locVertex2FLen * 3)];
- _t._squareColors = [cc.color(0, 0, 0, 255, locSquareColorsAB, 0),
- cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen),
- cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen * 2),
- cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen * 3)];
- _t._verticesFloat32Buffer = cc._renderContext.createBuffer();
- _t._colorsUint8Buffer = cc._renderContext.createBuffer();
- cc.Layer.prototype.ctor.call(_t);
- _t._blendFunc = new cc.BlendFunc(cc.BLEND_SRC, cc.BLEND_DST);
- cc.LayerColor.prototype.init.call(_t, color, width, height);
- };
- _p.setContentSize = function (size, height) {
- var locSquareVertices = this._squareVertices;
- if (height === undefined) {
- locSquareVertices[1].x = size.width;
- locSquareVertices[2].y = size.height;
- locSquareVertices[3].x = size.width;
- locSquareVertices[3].y = size.height;
- } else {
- locSquareVertices[1].x = size;
- locSquareVertices[2].y = height;
- locSquareVertices[3].x = size;
- locSquareVertices[3].y = height;
- }
- this._bindLayerVerticesBufferData();
- cc.Layer.prototype.setContentSize.call(this, size, height);
- };
- _p._setWidth = function (width) {
- var locSquareVertices = this._squareVertices;
- locSquareVertices[1].x = width;
- locSquareVertices[3].x = width;
- this._bindLayerVerticesBufferData();
- cc.Layer.prototype._setWidth.call(this, width);
- };
- _p._setHeight = function (height) {
- var locSquareVertices = this._squareVertices;
- locSquareVertices[2].y = height;
- locSquareVertices[3].y = height;
- this._bindLayerVerticesBufferData();
- cc.Layer.prototype._setHeight.call(this, height);
- };
- _p._updateColor = function () {
- var locDisplayedColor = this._displayedColor;
- var locDisplayedOpacity = this._displayedOpacity, locSquareColors = this._squareColors;
- for (var i = 0; i < 4; i++) {
- locSquareColors[i].r = locDisplayedColor.r;
- locSquareColors[i].g = locDisplayedColor.g;
- locSquareColors[i].b = locDisplayedColor.b;
- locSquareColors[i].a = locDisplayedOpacity;
- }
- this._bindLayerColorsBufferData();
- };
- _p.draw = function (ctx) {
- var context = ctx || cc._renderContext;
- cc.nodeDrawSetup(this);
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR);
- //
- // Attributes
- //
- context.bindBuffer(context.ARRAY_BUFFER, this._verticesFloat32Buffer);
- context.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, context.FLOAT, false, 0, 0);
- context.bindBuffer(context.ARRAY_BUFFER, this._colorsUint8Buffer);
- context.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, 0, 0);
- cc.glBlendFunc(this._blendFunc.src, this._blendFunc.dst);
- context.drawArrays(context.TRIANGLE_STRIP, 0, 4);
- };
- _p._bindLayerVerticesBufferData = function () {
- var glContext = cc._renderContext;
- glContext.bindBuffer(glContext.ARRAY_BUFFER, this._verticesFloat32Buffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, this._squareVerticesAB, glContext.STATIC_DRAW);
- };
- _p._bindLayerColorsBufferData = function () {
- var glContext = cc._renderContext;
- glContext.bindBuffer(glContext.ARRAY_BUFFER, this._colorsUint8Buffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, this._squareColorsAB, glContext.STATIC_DRAW);
- };
- //cc.LayerColor define end
- }
- cc._tmp.WebGLLayerGradient = function () {
- //cc.LayerGradient define start
- var _p = cc.LayerGradient.prototype;
- _p.draw = cc.LayerColor.prototype.draw;
- _p._updateColor = function () {
- var _t = this;
- var locAlongVector = _t._alongVector;
- var h = cc.pLength(locAlongVector);
- if (h === 0)
- return;
- var c = Math.sqrt(2.0), u = cc.p(locAlongVector.x / h, locAlongVector.y / h);
- // Compressed Interpolation mode
- if (_t._compressedInterpolation) {
- var h2 = 1 / ( Math.abs(u.x) + Math.abs(u.y) );
- u = cc.pMult(u, h2 * c);
- }
- var opacityf = _t._displayedOpacity / 255.0;
- var locDisplayedColor = _t._displayedColor, locEndColor = _t._endColor;
- var S = { r: locDisplayedColor.r, g: locDisplayedColor.g, b: locDisplayedColor.b, a: _t._startOpacity * opacityf};
- var E = {r: locEndColor.r, g: locEndColor.g, b: locEndColor.b, a: _t._endOpacity * opacityf};
- // (-1, -1)
- var locSquareColors = _t._squareColors;
- var locSquareColor0 = locSquareColors[0], locSquareColor1 = locSquareColors[1], locSquareColor2 = locSquareColors[2], locSquareColor3 = locSquareColors[3];
- locSquareColor0.r = ((E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0 * c))));
- locSquareColor0.g = ((E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0 * c))));
- locSquareColor0.b = ((E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0 * c))));
- locSquareColor0.a = ((E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0 * c))));
- // (1, -1)
- locSquareColor1.r = ((E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0 * c))));
- locSquareColor1.g = ((E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0 * c))));
- locSquareColor1.b = ((E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0 * c))));
- locSquareColor1.a = ((E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0 * c))));
- // (-1, 1)
- locSquareColor2.r = ((E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0 * c))));
- locSquareColor2.g = ((E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0 * c))));
- locSquareColor2.b = ((E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0 * c))));
- locSquareColor2.a = ((E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0 * c))));
- // (1, 1)
- locSquareColor3.r = ((E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0 * c))));
- locSquareColor3.g = ((E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0 * c))));
- locSquareColor3.b = ((E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0 * c))));
- locSquareColor3.a = ((E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0 * c))));
- _t._bindLayerColorsBufferData();
- }
- //cc.LayerGradient define end
- }
|