Skip to content

Commit fdc627f

Browse files
committed
Fixed overflowing buffer on Graphics webgl renderer
1 parent ac9d8ff commit fdc627f

6 files changed

Lines changed: 36 additions & 54 deletions

File tree

v3/src/gameobjects/graphics/Commands.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ module.exports = {
22
ARC: 0,
33
BEGIN_PATH: 1,
44
CLOSE_PATH: 2,
5-
FILL_CIRCLE: 3,
6-
FILL_RECT: 4,
7-
LINE_TO: 5,
8-
MOVE_TO: 6,
9-
LINE_STYLE: 7,
10-
STROKE_CIRCLE: 8,
11-
STROKE_RECT: 9,
12-
FILL_PATH: 10,
13-
STROKE_PATH: 11,
14-
FILL_STYLE: 12
5+
FILL_RECT: 3,
6+
LINE_TO: 4,
7+
MOVE_TO: 5,
8+
LINE_STYLE: 6,
9+
FILL_STYLE: 7,
10+
FILL_PATH: 8,
11+
STROKE_PATH: 9,
1512
};

v3/src/gameobjects/graphics/Graphics.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,13 @@ var Graphics = new Class({
103103

104104
strokeRect: function (x, y, width, height)
105105
{
106-
this.commandBuffer.push(
107-
Commands.STROKE_RECT,
108-
x, y, width, height
109-
);
106+
this.beginPath();
107+
this.moveTo(x, y);
108+
this.lineTo(x + width, y);
109+
this.lineTo(x + width, y + height);
110+
this.lineTo(x, y + height);
111+
this.lineTo(x, y);
112+
this.closePath();
110113
},
111114

112115
lineTo: function (x, y)

v3/src/gameobjects/graphics/GraphicsCanvasRenderer.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,6 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
112112
);
113113
index += 4;
114114
break;
115-
case Commands.STROKE_CIRCLE:
116-
ctx.beginPath();
117-
ctx.arc(
118-
commandBuffer[index + 1],
119-
commandBuffer[index + 2],
120-
commandBuffer[index + 3],
121-
0,
122-
PI2
123-
);
124-
ctx.stroke();
125-
ctx.closePath();
126-
index += 3;
127-
break;
128-
case Commands.STROKE_RECT:
129-
ctx.beginPath();
130-
ctx.rect(
131-
commandBuffer[index + 1],
132-
commandBuffer[index + 2],
133-
commandBuffer[index + 3],
134-
commandBuffer[index + 4]
135-
);
136-
ctx.stroke();
137-
ctx.closePath();
138-
index += 4;
139-
break;
140115
case Commands.LINE_TO:
141116
ctx.lineTo(
142117
commandBuffer[index + 1],

v3/src/gameobjects/graphics/GraphicsWebGLRenderer.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var pathArray = [];
44
var cos = Math.cos;
55
var sin = Math.sin;
66

7-
87
var Point = function (x, y)
98
{
109
this.x = x;
@@ -14,7 +13,8 @@ var Point = function (x, y)
1413
var Path = function (x, y)
1514
{
1615
this.points = [];
17-
this.points.push(new Point(x, y));
16+
this.pointsLength = 1;
17+
this.points[0] = new Point(x, y);
1818
};
1919

2020
var lerp = function (norm, min, max)
@@ -71,6 +71,7 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
7171
var path;
7272
var pathLength;
7373
var point;
74+
var maxVertices = shapeBatch.maxVertices;
7475

7576
renderer.setBatch(shapeBatch, null);
7677

@@ -127,11 +128,15 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
127128
}
128129
break;
129130
case Commands.FILL_PATH:
130-
for (var pathArrayIndex = 0, pathArrayLength = pathArray.length; pathArrayIndex < pathArrayLength; ++pathArrayIndex)
131+
for (var pathArrayIndex = 0, pathArrayLength = pathArray.length;
132+
pathArrayIndex < pathArrayLength;
133+
++pathArrayIndex)
131134
{
132135
path = pathArray[pathArrayIndex].points;
133136
pathLength = path.length;
134-
for (var pathIndex = 0; pathIndex < pathLength; ++pathIndex)
137+
for (var pathIndex = 0;
138+
pathIndex < pathLength;
139+
++pathIndex)
135140
{
136141
point = path[pathIndex];
137142
polygon.push(point.x, point.y);
@@ -142,6 +147,11 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
142147
v0 = polygonIndex[index + 0] * 2;
143148
v1 = polygonIndex[index + 1] * 2;
144149
v2 = polygonIndex[index + 2] * 2;
150+
if (vertexCount + 3 > maxVertices)
151+
{
152+
shapeBatch.flush();
153+
vertexCount = 0;
154+
}
145155
vertexOffset = vertexDataBuffer.allocate(9 * 3);
146156
vertexCount += 3;
147157

@@ -188,14 +198,18 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
188198
vertexBufferF32[vertexOffset++] = srcScaleX;
189199
vertexBufferF32[vertexOffset++] = srcScaleY;
190200
vertexBufferF32[vertexOffset++] = srcRotation;
191-
192201
}
193202
polygon.length = 0;
194203
}
195204
break;
196205
case Commands.STROKE_PATH:
197206
break;
198207
case Commands.FILL_RECT:
208+
if (vertexCount + 6 > maxVertices)
209+
{
210+
shapeBatch.flush();
211+
vertexCount = 0;
212+
}
199213
vertexOffset = vertexDataBuffer.allocate(9 * 6);
200214
vertexCount += 6;
201215

@@ -262,13 +276,6 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
262276
vertexBufferF32[vertexOffset++] = srcScaleX;
263277
vertexBufferF32[vertexOffset++] = srcScaleY;
264278
vertexBufferF32[vertexOffset++] = srcRotation;
265-
266-
cmdIndex += 4;
267-
break;
268-
case Commands.STROKE_CIRCLE:
269-
cmdIndex += 3;
270-
break;
271-
case Commands.STROKE_RECT:
272279
cmdIndex += 4;
273280
break;
274281
case Commands.LINE_TO:

v3/src/renderer/webgl/batches/shape/ShapeBatch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ var ShapeBatch = function (game, gl, manager)
4545

4646
this.manager = manager;
4747
this.dirty = false;
48-
48+
this.context = null;
4949
this.init(this.glContext);
50+
5051
};
5152

5253
ShapeBatch.prototype.constructor = ShapeBatch;
@@ -55,7 +56,6 @@ ShapeBatch.prototype = {
5556

5657
init: function (gl)
5758
{
58-
5959
var vertexDataBuffer = new Buffer32(CONST.VERTEX_SIZE * CONST.MAX_VERTICES);
6060
var vertShader = CreateShader(gl, CONST.VERTEX_SHADER_SOURCE, gl.VERTEX_SHADER);
6161
var fragShader = CreateShader(gl, CONST.FRAGMENT_SHADER_SOURCE, gl.FRAGMENT_SHADER);

v3/src/renderer/webgl/batches/shape/const.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var CONST = {
99
SHAPE_VERTEX_COMPONENT_COUNT: 9,
1010

1111
// Can't be bigger than 10,000 since index are 16-bit
12-
MAX_VERTICES: 1000000,
12+
MAX_VERTICES: 10000,
1313

1414
VERTEX_SHADER_SOURCE: VertexShader,
1515
FRAGMENT_SHADER_SOURCE: FragmentShader

0 commit comments

Comments
 (0)