Files
cocos4/cocos2d/core/layers/CCLayerWebGLRenderCmd.js
T
2016-03-14 21:46:12 +08:00

311 lines
13 KiB
JavaScript

/****************************************************************************
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.
****************************************************************************/
//-----------------------//
// 1. cc.Layer //
// 2. cc.LayerColor //
// 3. cc.LayerGradient //
//-----------------------//
/**
* cc.Layer's rendering objects of WebGL
*/
(function(){
cc.Layer.WebGLRenderCmd = function(renderable){
_ccsg.Node.WebGLRenderCmd.call(this, renderable);
};
var proto = cc.Layer.WebGLRenderCmd.prototype = Object.create(_ccsg.Node.WebGLRenderCmd.prototype);
proto.constructor = cc.Layer.WebGLRenderCmd;
proto.bake = function(){};
proto.unbake = function(){};
proto._bakeForAddChild = function(){};
})();
/**
* cc.LayerColor's rendering objects of WebGL
*/
(function(){
cc.LayerColor.WebGLRenderCmd = function(renderable){
cc.Layer.WebGLRenderCmd.call(this, renderable);
this._needDraw = true;
//
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.WebGLColor.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 = [new cc.WebGLColor(0, 0, 0, 255, locSquareColorsAB, 0),
new cc.WebGLColor(0, 0, 0, 255, locSquareColorsAB, locColorLen),
new cc.WebGLColor(0, 0, 0, 255, locSquareColorsAB, locColorLen * 2),
new cc.WebGLColor(0, 0, 0, 255, locSquareColorsAB, locColorLen * 3)];
_t._verticesFloat32Buffer = cc._renderContext.createBuffer();
_t._colorsUint8Buffer = cc._renderContext.createBuffer();
};
var proto = cc.LayerColor.WebGLRenderCmd.prototype = Object.create(cc.Layer.WebGLRenderCmd.prototype);
proto.constructor = cc.LayerColor.WebGLRenderCmd;
proto.rendering = function (ctx) {
var context = ctx || cc._renderContext;
var node = this._node;
this._shaderProgram.use();
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._stackMatrix);
cc.gl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POSITION | cc.macro.VERTEX_ATTRIB_FLAG_COLOR);
cc.gl.blendFunc(node._blendFunc.src, node._blendFunc.dst);
//
// Attributes
//
context.bindBuffer(context.ARRAY_BUFFER, this._verticesFloat32Buffer);
context.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, context.FLOAT, false, 0, 0);
context.bindBuffer(context.ARRAY_BUFFER, this._colorsUint8Buffer);
context.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, 0, 0);
context.drawArrays(context.TRIANGLE_STRIP, 0, this._squareVertices.length);
};
proto._updateSquareVertices = 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();
};
proto._updateSquareVerticesWidth = function(width){
var locSquareVertices = this._squareVertices;
locSquareVertices[1].x = width;
locSquareVertices[3].x = width;
this._bindLayerVerticesBufferData();
};
proto._updateSquareVerticesHeight = function(height){
var locSquareVertices = this._squareVertices;
locSquareVertices[2].y = height;
locSquareVertices[3].y = height;
this._bindLayerVerticesBufferData();
};
proto._updateColor = function(){
var locDisplayedColor = this._displayedColor, 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();
};
proto._bindLayerVerticesBufferData = function(){
var glContext = cc._renderContext;
glContext.bindBuffer(glContext.ARRAY_BUFFER, this._verticesFloat32Buffer);
glContext.bufferData(glContext.ARRAY_BUFFER, this._squareVerticesAB, glContext.STATIC_DRAW);
};
proto._bindLayerColorsBufferData = function(){
var glContext = cc._renderContext;
glContext.bindBuffer(glContext.ARRAY_BUFFER, this._colorsUint8Buffer);
glContext.bufferData(glContext.ARRAY_BUFFER, this._squareColorsAB, glContext.STATIC_DRAW);
};
proto.updateBlendFunc = function(blendFunc){};
})();
/**
* cc.LayerGradient's rendering objects of WebGL
*/
(function(){
cc.LayerGradient.WebGLRenderCmd = function(renderable){
cc.LayerColor.WebGLRenderCmd.call(this, renderable);
this._needDraw = true;
this._clipRect = new cc.Rect();
this._clippingRectDirty = false;
};
var proto = cc.LayerGradient.WebGLRenderCmd.prototype = Object.create(cc.LayerColor.WebGLRenderCmd.prototype);
cc.js.mixin(proto, cc.LayerGradient.RenderCmd);
proto.constructor = cc.LayerGradient.WebGLRenderCmd;
proto._syncStatus = function (parentCmd) {
var flags = _ccsg.Node._dirtyFlags, locFlag = this._dirtyFlag;
var parentNode = parentCmd ? parentCmd._node : null;
if(parentNode && parentNode._cascadeColorEnabled && (parentCmd._dirtyFlag & flags.colorDirty))
locFlag |= flags.colorDirty;
if(parentNode && parentNode._cascadeOpacityEnabled && (parentCmd._dirtyFlag & flags.opacityDirty))
locFlag |= flags.opacityDirty;
if(parentCmd && (parentCmd._dirtyFlag & flags.transformDirty))
locFlag |= flags.transformDirty;
var colorDirty = locFlag & flags.colorDirty,
opacityDirty = locFlag & flags.opacityDirty;
this._dirtyFlag = locFlag;
if (colorDirty)
this._syncDisplayColor();
if (opacityDirty)
this._syncDisplayOpacity();
//if (locFlag & flags.transformDirty) {
//update the transform
this.transform(parentCmd);
//}
if (colorDirty || opacityDirty || (locFlag & flags.gradientDirty)){
this._updateColor();
}
};
proto._updateColor = function(){
this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.gradientDirty ^ this._dirtyFlag;
var node = this._node, stops = node._colorStops;
if(!stops || stops.length < 2)
return;
this._clippingRectDirty = true;
var stopsLen = stops.length, verticesLen = stopsLen * 2, i, contentSize = node._contentSize;
this._squareVerticesAB = new ArrayBuffer(verticesLen * 8);
this._squareColorsAB = new ArrayBuffer(verticesLen * 4);
var locVertices = this._squareVertices, locColors = this._squareColors;
locVertices.length = 0;
locColors.length = 0;
var locSquareVerticesAB = this._squareVerticesAB, locSquareColorsAB = this._squareColorsAB;
var locVertex2FLen = cc.Vertex2F.BYTES_PER_ELEMENT, locColorLen = cc.WebGLColor.BYTES_PER_ELEMENT;
for(i = 0; i < verticesLen; i++){
locVertices.push(new cc.Vertex2F(0, 0, locSquareVerticesAB, locVertex2FLen * i));
locColors.push(new cc.WebGLColor(0, 0, 0, 255, locSquareColorsAB, locColorLen * i))
}
//init vertex
var angle = Math.PI + cc.pAngleSigned(cc.p(0, -1), node._alongVector), locAnchor = cc.p(contentSize.width/2, contentSize.height /2);
var degrees = Math.round(cc.radiansToDegrees(angle));
var transMat = cc.affineTransformMake(1, 0, 0, 1, locAnchor.x, locAnchor.y);
transMat = cc.affineTransformRotate(transMat, angle);
var a, b;
if(degrees < 90) {
a = cc.p(-locAnchor.x, locAnchor.y);
b = cc.p(locAnchor.x, locAnchor.y);
} else if(degrees < 180) {
a = cc.p(locAnchor.x, locAnchor.y);
b = cc.p(locAnchor.x, -locAnchor.y);
} else if(degrees < 270) {
a = cc.p(locAnchor.x, -locAnchor.y);
b = cc.p(-locAnchor.x, -locAnchor.y);
} else {
a = cc.p(-locAnchor.x, -locAnchor.y);
b = cc.p(-locAnchor.x, locAnchor.y);
}
var sin = Math.sin(angle), cos = Math.cos(angle);
var tx = Math.abs((a.x * cos - a.y * sin)/locAnchor.x), ty = Math.abs((b.x * sin + b.y * cos)/locAnchor.y);
transMat = cc.affineTransformScale(transMat, tx, ty);
for (i = 0; i < stopsLen; i++) {
var stop = stops[i], y = stop.p * contentSize.height ;
var p0 = cc.pointApplyAffineTransform(- locAnchor.x , y - locAnchor.y, transMat);
locVertices[i * 2].x = p0.x;
locVertices[i * 2].y = p0.y;
var p1 = cc.pointApplyAffineTransform(contentSize.width - locAnchor.x, y - locAnchor.y, transMat);
locVertices[i * 2 + 1].x = p1.x;
locVertices[i * 2 + 1].y = p1.y;
}
//init color
var opacityf = this._displayedOpacity / 255.0; //, displayColor = this._displayedColor;
for(i = 0; i < stopsLen; i++){
var stopColor = stops[i].color, locSquareColor0 = locColors[i * 2], locSquareColor1 = locColors[i * 2 + 1];
locSquareColor0.r = stopColor.r;
locSquareColor0.g = stopColor.g;
locSquareColor0.b = stopColor.b;
locSquareColor0.a = stopColor.a * opacityf;
locSquareColor1.r = stopColor.r;
locSquareColor1.g = stopColor.g;
locSquareColor1.b = stopColor.b;
locSquareColor1.a = stopColor.a * opacityf;
}
this._bindLayerVerticesBufferData();
this._bindLayerColorsBufferData();
};
proto.rendering = function (ctx) {
var context = ctx || cc._renderContext, node = this._node;
//it is too expensive to use stencil to clip, so it use Scissor,
//but it has a bug when layer rotated and layer's content size less than canvas's size.
var clippingRect = this._getClippingRect();
context.enable(context.SCISSOR_TEST);
cc.view.setScissorInPoints(clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
//draw gradient layer
this._shaderProgram.use();
this._shaderProgram._setUniformForMVPMatrixWithMat4(this._stackMatrix);
cc.gl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POSITION | cc.macro.VERTEX_ATTRIB_FLAG_COLOR);
cc.gl.blendFunc(node._blendFunc.src, node._blendFunc.dst);
//
// Attributes
//
context.bindBuffer(context.ARRAY_BUFFER, this._verticesFloat32Buffer);
context.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, context.FLOAT, false, 0, 0);
context.bindBuffer(context.ARRAY_BUFFER, this._colorsUint8Buffer);
context.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, 0, 0);
context.drawArrays(context.TRIANGLE_STRIP, 0, this._squareVertices.length);
context.disable(context.SCISSOR_TEST);
};
proto._getClippingRect = function(){
if(this._clippingRectDirty){
var node = this._node;
var rect = cc.rect(0, 0, node._contentSize.width, node._contentSize.height);
var trans = node.getNodeToWorldTransform();
this._clipRect = cc._rectApplyAffineTransformIn(rect, trans);
}
return this._clipRect;
};
})();