|
| 1 | +/**************************************************************************** |
| 2 | + Copyright (c) 2013-2014 Chukong Technologies Inc. |
| 3 | +
|
| 4 | + http://www.cocos2d-x.org |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + THE SOFTWARE. |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +//Layer's canvas render command |
| 26 | +cc.Layer.CanvasRenderCmd = function(renderable){ |
| 27 | + cc.Node.CanvasRenderCmd.call(this, renderable); |
| 28 | +}; |
| 29 | + |
| 30 | +cc.Layer.CanvasRenderCmd.prototype = Object.create(cc.Node.CanvasRenderCmd.prototype); |
| 31 | + |
| 32 | +//Layer's WebGL render command |
| 33 | +cc.Layer.WebGLRenderCmd = function(renderable){ |
| 34 | + cc.Node.WebGLRenderCmd.call(this, renderable); |
| 35 | +}; |
| 36 | + |
| 37 | +cc.Layer.WebGLRenderCmd.prototype = Object.create(cc.Node.WebGLRenderCmd.prototype); |
| 38 | + |
| 39 | +//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 40 | + |
| 41 | +//LayerColor's canvas render command |
| 42 | +cc.LayerColor.CanvasRenderCmd = function(renderable){ |
| 43 | + cc.Layer.CanvasRenderCmd.call(this, renderable); |
| 44 | + this._needDraw = true; |
| 45 | +}; |
| 46 | +cc.LayerColor.CanvasRenderCmd.prototype = Object.create(cc.Layer.CanvasRenderCmd.prototype); |
| 47 | + |
| 48 | +cc.LayerColor.CanvasRenderCmd.prototype.rendering = function (ctx, scaleX, scaleY) { |
| 49 | + var context = ctx || cc._renderContext, |
| 50 | + node = this._node, |
| 51 | + t = node._transformWorld, |
| 52 | + curColor = node._displayedColor, |
| 53 | + opacity = node._displayedOpacity / 255, |
| 54 | + locWidth = node._contentSize.width, |
| 55 | + locHeight = node._contentSize.height; |
| 56 | + |
| 57 | + if (opacity === 0) |
| 58 | + return; |
| 59 | + |
| 60 | + var needTransform = (t.a !== 1 || t.b !== 0 || t.c !== 0 || t.d !== 1); |
| 61 | + var needRestore = (node._blendFuncStr !== "source-over") || needTransform; |
| 62 | + |
| 63 | + if (needRestore) { |
| 64 | + context.save(); |
| 65 | + context.globalCompositeOperation = node._blendFuncStr; |
| 66 | + } |
| 67 | + context.globalAlpha = opacity; |
| 68 | + context.fillStyle = "rgba(" + (0 | curColor.r) + "," + (0 | curColor.g) + "," |
| 69 | + + (0 | curColor.b) + ", 1)"; |
| 70 | + if (needTransform) { |
| 71 | + context.transform(t.a, t.c, t.b, t.d, t.tx * scaleX, -t.ty * scaleY); |
| 72 | + context.fillRect(0, 0, locWidth * scaleX, -locHeight * scaleY); |
| 73 | + } else { |
| 74 | + context.fillRect(t.tx * scaleX, -t.ty * scaleY, locWidth * scaleX, -locHeight * scaleY); |
| 75 | + } |
| 76 | + if (needRestore) |
| 77 | + context.restore(); |
| 78 | + cc.g_NumberOfDraws++; |
| 79 | +}; |
| 80 | + |
| 81 | +//LayerColor's WebGL render command |
| 82 | +cc.LayerColor.WebGLRenderCmd = function(renderable){ |
| 83 | + cc.Layer.WebGLRenderCmd.call(this, renderable); |
| 84 | + this._needDraw = true; |
| 85 | +}; |
| 86 | +cc.LayerColor.WebGLRenderCmd.prototype = Object.create(cc.Layer.WebGLRenderCmd.prototype); |
| 87 | + |
| 88 | +cc.LayerColor.WebGLRenderCmd.prototype.rendering = function (ctx) { |
| 89 | + var context = ctx || cc._renderContext; |
| 90 | + var node = this._node; |
| 91 | + |
| 92 | + node._shaderProgram.use(); |
| 93 | + node._shaderProgram._setUniformForMVPMatrixWithMat4(node._stackMatrix); |
| 94 | + cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR); |
| 95 | + |
| 96 | + // |
| 97 | + // Attributes |
| 98 | + // |
| 99 | + context.bindBuffer(context.ARRAY_BUFFER, node._verticesFloat32Buffer); |
| 100 | + context.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, context.FLOAT, false, 0, 0); |
| 101 | + |
| 102 | + context.bindBuffer(context.ARRAY_BUFFER, node._colorsUint8Buffer); |
| 103 | + context.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, 0, 0); |
| 104 | + |
| 105 | + cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst); |
| 106 | + context.drawArrays(context.TRIANGLE_STRIP, 0, 4); |
| 107 | +}; |
| 108 | + |
| 109 | +//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 110 | +//LayerGradient's Canvas render command |
| 111 | +cc.LayerGradient.CanvasRenderCmd = function(renderable){ |
| 112 | + cc.LayerColor.CanvasRenderCmd.call(this, renderable); |
| 113 | + this._needDraw = true; |
| 114 | + this._startPoint = cc.p(0, 0); |
| 115 | + this._endPoint = cc.p(0, 0); |
| 116 | + this._startStopStr = null; |
| 117 | + this._endStopStr = null; |
| 118 | +}; |
| 119 | +cc.LayerGradient.CanvasRenderCmd.prototype = Object.create(cc.LayerColor.CanvasRenderCmd.prototype); |
| 120 | + |
| 121 | +cc.LayerGradient.CanvasRenderCmd.prototype.rendering = function (ctx, scaleX, scaleY) { |
| 122 | + var context = ctx || cc._renderContext, |
| 123 | + self = this, |
| 124 | + node = self._node, |
| 125 | + opacity = node._displayedOpacity / 255, |
| 126 | + t = node._transformWorld; |
| 127 | + |
| 128 | + if (opacity === 0) |
| 129 | + return; |
| 130 | + |
| 131 | + var needTransform = (t.a !== 1 || t.b !== 0 || t.c !== 0 || t.d !== 1); |
| 132 | + var needRestore = (node._blendFuncStr !== "source-over") || needTransform; |
| 133 | + if (needRestore) { |
| 134 | + context.save(); |
| 135 | + context.globalCompositeOperation = node._blendFuncStr; |
| 136 | + } |
| 137 | + context.globalAlpha = opacity; |
| 138 | + var locWidth = node._contentSize.width, locHeight = node._contentSize.height; |
| 139 | + |
| 140 | + var gradient = context.createLinearGradient(self._startPoint.x, self._startPoint.y, self._endPoint.x, self._endPoint.y); |
| 141 | + gradient.addColorStop(0, this._startStopStr); |
| 142 | + gradient.addColorStop(1, this._endStopStr); |
| 143 | + context.fillStyle = gradient; |
| 144 | + |
| 145 | + if (needTransform) { |
| 146 | + context.transform(t.a, t.c, t.b, t.d, t.tx * scaleX, -t.ty * scaleY); |
| 147 | + context.fillRect(0, 0, locWidth * scaleX, -locHeight * scaleY); |
| 148 | + } else |
| 149 | + context.fillRect(t.tx * scaleX, -t.ty * scaleY, locWidth * scaleX, -locHeight * scaleY); |
| 150 | + |
| 151 | + if (needRestore) |
| 152 | + context.restore(); |
| 153 | + cc.g_NumberOfDraws++; |
| 154 | +}; |
| 155 | + |
| 156 | +//LayerColor's WebGL render command |
| 157 | +cc.LayerGradient.WebGLRenderCmd = function(renderable){ |
| 158 | + cc.LayerColor.WebGLRenderCmd.call(this, renderable); |
| 159 | + this._needDraw = true; |
| 160 | +}; |
| 161 | +cc.LayerGradient.WebGLRenderCmd.prototype = Object.create(cc.LayerColor.WebGLRenderCmd.prototype); |
| 162 | + |
| 163 | + |
| 164 | + |
0 commit comments