Skip to content

Commit 576d61b

Browse files
committed
Refactor Sprite vertices calculation and add Culling
1 parent d6f60cf commit 576d61b

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

cocos2d/core/sprites/CCSpriteWebGLRenderCmd.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
cc.Node.WebGLRenderCmd.call(this, renderable);
3030
this._needDraw = true;
3131

32-
this._uvs = [
33-
{u: 0, v: 0}, // tl
34-
{u: 0, v: 0}, // bl
35-
{u: 0, v: 0}, // tr
36-
{u: 0, v: 0} // br
32+
this._vertices = [
33+
{x: 0, y: 0, u: 0, v: 0}, // tl
34+
{x: 0, y: 0, u: 0, v: 0}, // bl
35+
{x: 0, y: 0, u: 0, v: 0}, // tr
36+
{x: 0, y: 0, u: 0, v: 0} // br
3737
];
3838
this._dirty = false;
3939
this._recursiveDirty = false;
@@ -44,12 +44,6 @@
4444
var proto = cc.Sprite.WebGLRenderCmd.prototype = Object.create(cc.Node.WebGLRenderCmd.prototype);
4545
proto.constructor = cc.Sprite.WebGLRenderCmd;
4646

47-
// The following static properties must be provided for a auto batchable command
48-
proto.vertexBytesPerUnit = cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
49-
proto.bytesPerUnit = proto.vertexBytesPerUnit;
50-
proto.indicesPerUnit = 6;
51-
proto.verticesPerUnit = 4;
52-
5347
proto.updateBlendFunc = function (blendFunc) {};
5448

5549
proto.setDirtyFlag = function(dirtyFlag){
@@ -104,7 +98,6 @@
10498
};
10599

106100
proto._textureLoadedCallback = function (sender) {
107-
var renderCmd = this._renderCmd;
108101
if (this._textureLoaded)
109102
return;
110103

@@ -137,7 +130,7 @@
137130
var node = this._node;
138131

139132
var tex = node._batchNode ? node.textureAtlas.texture : node._texture;
140-
var uvs = this._uvs;
133+
var uvs = this._vertices;
141134
if (!tex)
142135
return;
143136

@@ -280,6 +273,25 @@
280273
}
281274
};
282275

276+
proto.transform = function (parentCmd, recursive) {
277+
this.originTransform(parentCmd, recursive);
278+
279+
var node = this._node,
280+
lx = node._offsetPosition.x, rx = lx + node._rect.width,
281+
by = node._offsetPosition.y, ty = by + node._rect.height,
282+
wt = this._worldTransform;
283+
284+
var vertices = this._vertices;
285+
vertices[0].x = lx * wt.a + ty * wt.c + wt.tx; // tl
286+
vertices[0].y = lx * wt.b + ty * wt.d + wt.ty;
287+
vertices[1].x = lx * wt.a + by * wt.c + wt.tx; // bl
288+
vertices[1].y = lx * wt.b + by * wt.d + wt.ty;
289+
vertices[2].x = rx * wt.a + ty * wt.c + wt.tx; // tr
290+
vertices[2].y = rx * wt.b + ty * wt.d + wt.ty;
291+
vertices[3].x = rx * wt.a + by * wt.c + wt.tx; // br
292+
vertices[3].y = rx * wt.b + by * wt.d + wt.ty;
293+
};
294+
283295
proto.needDraw = function () {
284296
var node = this._node, locTexture = node._texture;
285297
return (this._needDraw && locTexture);
@@ -290,24 +302,18 @@
290302
if (!(locTexture && locTexture._textureLoaded && node._rect.width && node._rect.height) || !this._displayedOpacity)
291303
return false;
292304

293-
var lx = node._offsetPosition.x, rx = lx + node._rect.width,
294-
by = node._offsetPosition.y, ty = by + node._rect.height,
295-
wt = this._worldTransform;
296-
297-
offset = vertexDataOffset;
298-
299-
f32buffer[offset] = lx * wt.a + ty * wt.c + wt.tx; // tl
300-
f32buffer[offset + 1] = lx * wt.b + ty * wt.d + wt.ty;
301-
f32buffer[offset + 6] = lx * wt.a + by * wt.c + wt.tx; // bl
302-
f32buffer[offset + 7] = lx * wt.b + by * wt.d + wt.ty;
303-
f32buffer[offset + 12] = rx * wt.a + ty * wt.c + wt.tx; // tr
304-
f32buffer[offset + 13] = rx * wt.b + ty * wt.d + wt.ty;
305-
f32buffer[offset + 18] = rx * wt.a + by * wt.c + wt.tx; // br
306-
f32buffer[offset + 19] = rx * wt.b + by * wt.d + wt.ty;
305+
var vertices = this._vertices;
306+
var visibleRect = cc.view._visibleRect;
307+
var tl = vertices[0], bl = vertices[1], tr = vertices[2], br = vertices[3];
308+
if (Math.max(tl.x, bl.x, tr.x, br.x) < visibleRect.x ||
309+
Math.max(tl.y, bl.y, tr.y, br.y) < visibleRect.y ||
310+
Math.min(tl.x, bl.x, tr.x, br.x) > visibleRect.x + visibleRect.width ||
311+
Math.min(tl.y, bl.y, tr.y, br.y) > visibleRect.y + visibleRect.height) {
312+
return false;
313+
}
307314

308315
// Fill in vertex data with quad information (4 vertices for sprite)
309-
var uvs = this._uvs;
310-
var opacity = Math.floor(this._displayedOpacity);
316+
var opacity = this._displayedOpacity;
311317
var r = this._displayedColor.r,
312318
g = this._displayedColor.g,
313319
b = this._displayedColor.b;
@@ -318,15 +324,16 @@
318324
b *= a;
319325
}
320326

321-
var i, len = uvs.length, offset, uv, colorView;
327+
var i, len = vertices.length, vertex, offset = vertexDataOffset;
322328
for (i = 0; i < len; ++i) {
323329
offset = vertexDataOffset + i * 6;
324-
uv = uvs[i];
330+
vertex = vertices[i];
331+
f32buffer[offset] = vertex.x;
332+
f32buffer[offset + 1] = vertex.y;
325333
f32buffer[offset + 2] = node._vertexZ;
326-
f32buffer[offset + 4] = uv.u;
327-
f32buffer[offset + 5] = uv.v;
328-
329334
ui32buffer[offset + 3] = ((opacity<<24) | (b<<16) | (g<<8) | r);
335+
f32buffer[offset + 4] = vertex.u;
336+
f32buffer[offset + 5] = vertex.v;
330337
}
331338

332339
return true;

0 commit comments

Comments
 (0)