Skip to content

Commit 1fa0ac4

Browse files
committed
Improve renderer webgl to support mesh rendering
1 parent 15704c3 commit 1fa0ac4

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

cocos2d/core/platform/CCMacro.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,13 @@ cc.ORIENTATION_AUTO = 3;
499499
*/
500500
cc.CONCURRENCY_HTTP_REQUEST_COUNT = cc.sys.isMobile ? 20 : 0;
501501

502+
/**
503+
* The maximum vertex count for a single batched draw call.
504+
* @constant
505+
* @type Number
506+
*/
507+
cc.BATCH_VERTEX_COUNT = 2000;
508+
502509

503510
// ------------------- vertex attrib flags -----------------------------
504511
/**

cocos2d/core/renderer/RendererWebGL.js

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var _batchedInfo = {
3333
blendSrc: null,
3434
// The batched blend destination, all batching element should have the same blend destination
3535
blendDst: null,
36-
// The batched glProgramState, all batching element should have the same glProgramState
36+
// The batched gl program state, all batching element should have the same state
3737
glProgramState: null
3838
},
3939

@@ -129,7 +129,7 @@ return {
129129
_cacheToBufferCmds: {}, // an array saves the renderer commands need for cache to other canvas
130130
_cacheInstanceIds: [],
131131
_currentID: 0,
132-
_clearColor: cc.color(), //background color,default BLACK
132+
_clearColor: cc.color(0, 0, 0, 255), //background color,default BLACK
133133

134134
init: function () {
135135
var gl = cc._renderContext;
@@ -138,7 +138,7 @@ return {
138138

139139
this.mat4Identity = new cc.math.Matrix4();
140140
this.mat4Identity.identity();
141-
initQuadBuffer(2000);
141+
initQuadBuffer(cc.BATCH_VERTEX_COUNT);
142142
if (cc.sys.os === cc.sys.OS_IOS) {
143143
_IS_IOS = true;
144144
}
@@ -313,18 +313,23 @@ return {
313313
_batchingSize += increment;
314314
},
315315

316-
_updateBatchedInfo: function (texture, blendFunc, shaderProgram) {
317-
if (texture) {
316+
_updateBatchedInfo: function (texture, blendFunc, glProgramState) {
317+
if (texture !== _batchedInfo.texture ||
318+
blendFunc.src !== _batchedInfo.blendSrc ||
319+
blendFunc.dst !== _batchedInfo.blendDst ||
320+
glProgramState !== _batchedInfo.glProgramState) {
321+
// Draw batched elements
322+
this._batchRendering();
323+
// Update _batchedInfo
318324
_batchedInfo.texture = texture;
319-
}
320-
321-
if (blendFunc) {
322325
_batchedInfo.blendSrc = blendFunc.src;
323326
_batchedInfo.blendDst = blendFunc.dst;
324-
}
327+
_batchedInfo.glProgramState = glProgramState;
325328

326-
if (shaderProgram) {
327-
_batchedInfo.shader = shaderProgram;
329+
return true;
330+
}
331+
else {
332+
return false;
328333
}
329334
},
330335

@@ -339,7 +344,7 @@ return {
339344

340345
// Check batching
341346
var node = cmd._node;
342-
var texture = node._texture || (node._spriteFrame ? node._spriteFrame._texture : null);
347+
var texture = node._texture || (node._spriteFrame && node._spriteFrame._texture);
343348
var blendSrc = node._blendFunc.src;
344349
var blendDst = node._blendFunc.dst;
345350
var glProgramState = cmd._glProgramState;
@@ -361,38 +366,7 @@ return {
361366
// Upload vertex data
362367
var len = cmd.uploadData(_vertexDataF32, _vertexDataUI32, _batchingSize * _sizePerVertex);
363368
if (len > 0) {
364-
var i, curr, type = cmd.vertexType || VertexType.QUAD;
365-
switch (type) {
366-
case VertexType.QUAD:
367-
for (i = 0; i < len; i += 4) {
368-
curr = _batchingSize + i;
369-
_indexData[_indexSize++] = curr + 0;
370-
_indexData[_indexSize++] = curr + 1;
371-
_indexData[_indexSize++] = curr + 2;
372-
_indexData[_indexSize++] = curr + 1;
373-
_indexData[_indexSize++] = curr + 2;
374-
_indexData[_indexSize++] = curr + 3;
375-
}
376-
break;
377-
case VertexType.TRIANGLE:
378-
_pureQuad = false;
379-
for (i = 0; i < len; i += 3) {
380-
curr = _batchingSize + i;
381-
_indexData[_indexSize++] = curr + 0;
382-
_indexData[_indexSize++] = curr + 1;
383-
_indexData[_indexSize++] = curr + 2;
384-
}
385-
break;
386-
case VertexType.CUSTOM:
387-
_pureQuad = false;
388-
if (cmd.uploadIndexData) {
389-
_indexSize += cmd.uploadIndexData(_indexData, _indexSize, _batchingSize);
390-
}
391-
break;
392-
default:
393-
return;
394-
}
395-
_batchingSize += len;
369+
this._increaseBatchingSize(len, cmd.vertexType);
396370
}
397371
},
398372

0 commit comments

Comments
 (0)