CCLayerWebGL.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. cc._tmp.LayerDefineForWebGL = function(){
  23. var _p = cc.Layer.prototype;
  24. //Layer doesn't support bake function in WebGL
  25. _p.bake = function(){};
  26. _p.unbake = function(){};
  27. _p.visit = cc.Node.prototype.visit;
  28. };
  29. cc._tmp.WebGLLayerColor = function () {
  30. //cc.LayerColor define start
  31. var _p = cc.LayerColor.prototype;
  32. _p._squareVertices = null;
  33. _p._squareColors = null;
  34. _p._verticesFloat32Buffer = null;
  35. _p._colorsUint8Buffer = null;
  36. _p._squareVerticesAB = null;
  37. _p._squareColorsAB = null;
  38. _p.ctor = function (color, width, height) {
  39. var _t = this;
  40. _t._squareVerticesAB = new ArrayBuffer(32);
  41. _t._squareColorsAB = new ArrayBuffer(16);
  42. var locSquareVerticesAB = _t._squareVerticesAB, locSquareColorsAB = _t._squareColorsAB;
  43. var locVertex2FLen = cc.Vertex2F.BYTES_PER_ELEMENT, locColorLen = cc.Color.BYTES_PER_ELEMENT;
  44. _t._squareVertices = [new cc.Vertex2F(0, 0, locSquareVerticesAB, 0),
  45. new cc.Vertex2F(0, 0, locSquareVerticesAB, locVertex2FLen),
  46. new cc.Vertex2F(0, 0, locSquareVerticesAB, locVertex2FLen * 2),
  47. new cc.Vertex2F(0, 0, locSquareVerticesAB, locVertex2FLen * 3)];
  48. _t._squareColors = [cc.color(0, 0, 0, 255, locSquareColorsAB, 0),
  49. cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen),
  50. cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen * 2),
  51. cc.color(0, 0, 0, 255, locSquareColorsAB, locColorLen * 3)];
  52. _t._verticesFloat32Buffer = cc._renderContext.createBuffer();
  53. _t._colorsUint8Buffer = cc._renderContext.createBuffer();
  54. cc.Layer.prototype.ctor.call(_t);
  55. _t._blendFunc = new cc.BlendFunc(cc.BLEND_SRC, cc.BLEND_DST);
  56. cc.LayerColor.prototype.init.call(_t, color, width, height);
  57. };
  58. _p.setContentSize = function (size, height) {
  59. var locSquareVertices = this._squareVertices;
  60. if (height === undefined) {
  61. locSquareVertices[1].x = size.width;
  62. locSquareVertices[2].y = size.height;
  63. locSquareVertices[3].x = size.width;
  64. locSquareVertices[3].y = size.height;
  65. } else {
  66. locSquareVertices[1].x = size;
  67. locSquareVertices[2].y = height;
  68. locSquareVertices[3].x = size;
  69. locSquareVertices[3].y = height;
  70. }
  71. this._bindLayerVerticesBufferData();
  72. cc.Layer.prototype.setContentSize.call(this, size, height);
  73. };
  74. _p._setWidth = function (width) {
  75. var locSquareVertices = this._squareVertices;
  76. locSquareVertices[1].x = width;
  77. locSquareVertices[3].x = width;
  78. this._bindLayerVerticesBufferData();
  79. cc.Layer.prototype._setWidth.call(this, width);
  80. };
  81. _p._setHeight = function (height) {
  82. var locSquareVertices = this._squareVertices;
  83. locSquareVertices[2].y = height;
  84. locSquareVertices[3].y = height;
  85. this._bindLayerVerticesBufferData();
  86. cc.Layer.prototype._setHeight.call(this, height);
  87. };
  88. _p._updateColor = function () {
  89. var locDisplayedColor = this._displayedColor;
  90. var locDisplayedOpacity = this._displayedOpacity, locSquareColors = this._squareColors;
  91. for (var i = 0; i < 4; i++) {
  92. locSquareColors[i].r = locDisplayedColor.r;
  93. locSquareColors[i].g = locDisplayedColor.g;
  94. locSquareColors[i].b = locDisplayedColor.b;
  95. locSquareColors[i].a = locDisplayedOpacity;
  96. }
  97. this._bindLayerColorsBufferData();
  98. };
  99. _p.draw = function (ctx) {
  100. var context = ctx || cc._renderContext;
  101. cc.nodeDrawSetup(this);
  102. cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR);
  103. //
  104. // Attributes
  105. //
  106. context.bindBuffer(context.ARRAY_BUFFER, this._verticesFloat32Buffer);
  107. context.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, context.FLOAT, false, 0, 0);
  108. context.bindBuffer(context.ARRAY_BUFFER, this._colorsUint8Buffer);
  109. context.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, 0, 0);
  110. cc.glBlendFunc(this._blendFunc.src, this._blendFunc.dst);
  111. context.drawArrays(context.TRIANGLE_STRIP, 0, 4);
  112. };
  113. _p._bindLayerVerticesBufferData = function () {
  114. var glContext = cc._renderContext;
  115. glContext.bindBuffer(glContext.ARRAY_BUFFER, this._verticesFloat32Buffer);
  116. glContext.bufferData(glContext.ARRAY_BUFFER, this._squareVerticesAB, glContext.STATIC_DRAW);
  117. };
  118. _p._bindLayerColorsBufferData = function () {
  119. var glContext = cc._renderContext;
  120. glContext.bindBuffer(glContext.ARRAY_BUFFER, this._colorsUint8Buffer);
  121. glContext.bufferData(glContext.ARRAY_BUFFER, this._squareColorsAB, glContext.STATIC_DRAW);
  122. };
  123. //cc.LayerColor define end
  124. }
  125. cc._tmp.WebGLLayerGradient = function () {
  126. //cc.LayerGradient define start
  127. var _p = cc.LayerGradient.prototype;
  128. _p.draw = cc.LayerColor.prototype.draw;
  129. _p._updateColor = function () {
  130. var _t = this;
  131. var locAlongVector = _t._alongVector;
  132. var h = cc.pLength(locAlongVector);
  133. if (h === 0)
  134. return;
  135. var c = Math.sqrt(2.0), u = cc.p(locAlongVector.x / h, locAlongVector.y / h);
  136. // Compressed Interpolation mode
  137. if (_t._compressedInterpolation) {
  138. var h2 = 1 / ( Math.abs(u.x) + Math.abs(u.y) );
  139. u = cc.pMult(u, h2 * c);
  140. }
  141. var opacityf = _t._displayedOpacity / 255.0;
  142. var locDisplayedColor = _t._displayedColor, locEndColor = _t._endColor;
  143. var S = { r: locDisplayedColor.r, g: locDisplayedColor.g, b: locDisplayedColor.b, a: _t._startOpacity * opacityf};
  144. var E = {r: locEndColor.r, g: locEndColor.g, b: locEndColor.b, a: _t._endOpacity * opacityf};
  145. // (-1, -1)
  146. var locSquareColors = _t._squareColors;
  147. var locSquareColor0 = locSquareColors[0], locSquareColor1 = locSquareColors[1], locSquareColor2 = locSquareColors[2], locSquareColor3 = locSquareColors[3];
  148. locSquareColor0.r = ((E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0 * c))));
  149. locSquareColor0.g = ((E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0 * c))));
  150. locSquareColor0.b = ((E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0 * c))));
  151. locSquareColor0.a = ((E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0 * c))));
  152. // (1, -1)
  153. locSquareColor1.r = ((E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0 * c))));
  154. locSquareColor1.g = ((E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0 * c))));
  155. locSquareColor1.b = ((E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0 * c))));
  156. locSquareColor1.a = ((E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0 * c))));
  157. // (-1, 1)
  158. locSquareColor2.r = ((E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0 * c))));
  159. locSquareColor2.g = ((E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0 * c))));
  160. locSquareColor2.b = ((E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0 * c))));
  161. locSquareColor2.a = ((E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0 * c))));
  162. // (1, 1)
  163. locSquareColor3.r = ((E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0 * c))));
  164. locSquareColor3.g = ((E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0 * c))));
  165. locSquareColor3.b = ((E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0 * c))));
  166. locSquareColor3.a = ((E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0 * c))));
  167. _t._bindLayerColorsBufferData();
  168. }
  169. //cc.LayerGradient define end
  170. }