2017-03-01 21:08:10 +00:00
|
|
|
var Commands = require('./Commands');
|
2017-03-06 18:13:39 +00:00
|
|
|
var TransformMatrix = require('../../components/TransformMatrix');
|
2017-03-01 21:08:10 +00:00
|
|
|
var pathArray = [];
|
|
|
|
var cos = Math.cos;
|
|
|
|
var sin = Math.sin;
|
2017-03-02 00:40:03 +00:00
|
|
|
var sqrt = Math.sqrt;
|
2017-03-06 18:13:39 +00:00
|
|
|
var tempMatrix = new TransformMatrix();
|
2017-03-01 21:08:10 +00:00
|
|
|
|
2017-03-14 22:13:31 +00:00
|
|
|
var Point = function (x, y, width)
|
2017-03-01 21:08:10 +00:00
|
|
|
{
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2017-03-14 22:13:31 +00:00
|
|
|
this.width = width;
|
2017-03-01 21:08:10 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 22:13:31 +00:00
|
|
|
var Path = function (x, y, width)
|
2017-03-01 21:08:10 +00:00
|
|
|
{
|
|
|
|
this.points = [];
|
2017-03-01 23:23:46 +00:00
|
|
|
this.pointsLength = 1;
|
2017-03-14 22:13:31 +00:00
|
|
|
this.points[0] = new Point(x, y, width);
|
2017-03-01 21:08:10 +00:00
|
|
|
};
|
|
|
|
|
2017-02-28 14:49:39 +00:00
|
|
|
var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, camera)
|
|
|
|
{
|
|
|
|
if (this.renderMask !== this.renderFlags)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
var shapeBatch = renderer.shapeBatch;
|
|
|
|
var vertexDataBuffer = shapeBatch.vertexDataBuffer;
|
|
|
|
var vertexBufferF32 = vertexDataBuffer.floatView;
|
|
|
|
var vertexBufferU32 = vertexDataBuffer.uintView;
|
|
|
|
var vertexOffset = 0;
|
|
|
|
var cameraScrollX = camera.scrollX;
|
|
|
|
var cameraScrollY = camera.scrollY;
|
2017-03-06 18:13:39 +00:00
|
|
|
const srcX = src.x - cameraScrollX;
|
|
|
|
const srcY = src.y - cameraScrollY;
|
2017-03-03 21:28:04 +00:00
|
|
|
const srcScaleX = src.scaleX;
|
|
|
|
const srcScaleY = src.scaleY;
|
2017-03-06 18:13:39 +00:00
|
|
|
const srcRotation = -src.rotation;
|
2017-03-01 21:08:10 +00:00
|
|
|
var commandBuffer = src.commandBuffer;
|
|
|
|
var value;
|
|
|
|
var lineAlpha = 1.0;
|
|
|
|
var fillAlpha = 1.0;
|
|
|
|
var lineColor = 0;
|
|
|
|
var fillColor = 0;
|
|
|
|
var lineWidth = 1.0;
|
|
|
|
var cameraMatrix = camera.matrix.matrix;
|
|
|
|
var lastPath = null;
|
|
|
|
var iteration = 0;
|
|
|
|
var iterStep = 0.01;
|
|
|
|
var tx = 0;
|
|
|
|
var ty = 0;
|
|
|
|
var ta = 0;
|
|
|
|
var x, y, radius, startAngle, endAngle, anticlockwise;
|
|
|
|
var width, height, txw, tyh;
|
|
|
|
var vertexCount = shapeBatch.vertexCount;
|
|
|
|
var polygon = [];
|
|
|
|
var x0, y0, x1, y1, x2, y2;
|
|
|
|
var tx0, ty0, tx1, ty1, tx2, ty2;
|
|
|
|
var v0, v1, v2;
|
|
|
|
var polygonIndex;
|
|
|
|
var path;
|
|
|
|
var pathLength;
|
|
|
|
var point;
|
2017-03-01 23:23:46 +00:00
|
|
|
var maxVertices = shapeBatch.maxVertices;
|
2017-03-03 21:28:04 +00:00
|
|
|
var translateX, translateY;
|
2017-03-06 18:13:39 +00:00
|
|
|
var tempMatrixMatrix = tempMatrix.matrix;
|
|
|
|
var sra, srb, src, srd, sre, srf, cma, cmb, cmc, cmd, cme, cmf;
|
|
|
|
var mva, mvb, mvc, mvd, mve, mvf;
|
|
|
|
|
|
|
|
tempMatrix.applyITRS(srcX, srcY, srcRotation, srcScaleX, srcScaleY);
|
|
|
|
|
|
|
|
sra = tempMatrixMatrix[0];
|
|
|
|
srb = tempMatrixMatrix[1];
|
|
|
|
src = tempMatrixMatrix[2];
|
|
|
|
srd = tempMatrixMatrix[3];
|
|
|
|
sre = tempMatrixMatrix[4];
|
|
|
|
srf = tempMatrixMatrix[5];
|
|
|
|
|
|
|
|
cma = cameraMatrix[0];
|
|
|
|
cmb = cameraMatrix[1];
|
|
|
|
cmc = cameraMatrix[2];
|
|
|
|
cmd = cameraMatrix[3];
|
|
|
|
cme = cameraMatrix[4];
|
|
|
|
cmf = cameraMatrix[5];
|
|
|
|
|
|
|
|
mva = sra * cma + srb * cmc;
|
|
|
|
mvb = sra * cmb + srb * cmd;
|
|
|
|
mvc = src * cma + srd * cmc;
|
|
|
|
mvd = src * cmb + srd * cmd;
|
|
|
|
mve = sre * cma + srf * cmc + cme;
|
|
|
|
mvf = sre * cmb + srf * cmd + cmf;
|
2017-03-01 21:08:10 +00:00
|
|
|
|
|
|
|
renderer.setBatch(shapeBatch, null);
|
|
|
|
|
|
|
|
for (var cmdIndex = 0, cmdLength = commandBuffer.length; cmdIndex < cmdLength; ++cmdIndex)
|
|
|
|
{
|
|
|
|
var cmd = commandBuffer[cmdIndex];
|
|
|
|
|
|
|
|
switch(cmd)
|
|
|
|
{
|
|
|
|
case Commands.ARC:
|
2017-03-06 18:13:39 +00:00
|
|
|
x = commandBuffer[cmdIndex + 1];
|
|
|
|
y = commandBuffer[cmdIndex + 2];
|
|
|
|
radius = commandBuffer[cmdIndex + 3];
|
|
|
|
startAngle = commandBuffer[cmdIndex + 4];
|
|
|
|
endAngle = commandBuffer[cmdIndex + 5];
|
|
|
|
anticlockwise = commandBuffer[cmdIndex + 6];
|
|
|
|
|
|
|
|
while (iteration < 1)
|
|
|
|
{
|
2017-03-07 04:29:51 +00:00
|
|
|
ta = (endAngle - startAngle) * iteration + startAngle;
|
2017-03-06 18:13:39 +00:00
|
|
|
tx = x + cos(ta) * radius;
|
|
|
|
ty = y + sin(ta) * radius;
|
|
|
|
|
|
|
|
if (iteration === 0)
|
|
|
|
{
|
2017-03-14 22:13:31 +00:00
|
|
|
lastPath = new Path(tx, ty, lineWidth);
|
2017-03-06 18:13:39 +00:00
|
|
|
pathArray.push(lastPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-14 22:13:31 +00:00
|
|
|
lastPath.points.push(new Point(tx, ty, lineWidth));
|
2017-03-06 18:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iteration += iterStep;
|
|
|
|
}
|
2017-03-01 21:08:10 +00:00
|
|
|
cmdIndex += 6;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.LINE_STYLE:
|
|
|
|
lineWidth = commandBuffer[cmdIndex + 1];
|
2017-03-02 02:06:37 +00:00
|
|
|
lineColor = commandBuffer[cmdIndex + 2];
|
2017-03-01 21:08:10 +00:00
|
|
|
lineAlpha = commandBuffer[cmdIndex + 3];
|
|
|
|
cmdIndex += 3;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.FILL_STYLE:
|
|
|
|
fillColor = commandBuffer[cmdIndex + 1];
|
|
|
|
fillAlpha = commandBuffer[cmdIndex + 2];
|
|
|
|
cmdIndex += 2;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.BEGIN_PATH:
|
2017-03-06 18:13:39 +00:00
|
|
|
pathArray.length = 0;
|
2017-03-01 21:08:10 +00:00
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.CLOSE_PATH:
|
2017-03-06 18:13:39 +00:00
|
|
|
if (lastPath !== null && lastPath.points.length > 0)
|
|
|
|
{
|
|
|
|
var firstPoint = lastPath.points[0];
|
2017-03-14 22:13:31 +00:00
|
|
|
var lastPoint = lastPath.points[lastPath.points.length - 1];
|
2017-03-06 18:13:39 +00:00
|
|
|
lastPath.points.push(firstPoint);
|
2017-03-14 22:13:31 +00:00
|
|
|
lastPath = new Path(lastPoint.x, lastPoint.y, lineWidth);
|
2017-03-06 18:13:39 +00:00
|
|
|
pathArray.push(lastPath);
|
|
|
|
}
|
2017-03-01 21:08:10 +00:00
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.FILL_PATH:
|
2017-03-06 18:13:39 +00:00
|
|
|
for (var pathArrayIndex = 0, pathArrayLength = pathArray.length;
|
|
|
|
pathArrayIndex < pathArrayLength;
|
|
|
|
++pathArrayIndex)
|
|
|
|
{
|
|
|
|
shapeBatch.addFillPath(
|
|
|
|
/* Graphics Game Object Properties */
|
|
|
|
srcX, srcY, srcScaleX, srcScaleY, srcRotation,
|
|
|
|
/* Rectangle properties */
|
|
|
|
pathArray[pathArrayIndex].points,
|
|
|
|
fillColor,
|
|
|
|
fillAlpha,
|
|
|
|
/* Transform */
|
|
|
|
mva, mvb, mvc, mvd, mve, mvf
|
|
|
|
);
|
|
|
|
}
|
2017-03-01 21:08:10 +00:00
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.STROKE_PATH:
|
2017-03-07 04:29:51 +00:00
|
|
|
for (var pathArrayIndex = 0, pathArrayLength = pathArray.length;
|
|
|
|
pathArrayIndex < pathArrayLength;
|
|
|
|
++pathArrayIndex)
|
|
|
|
{
|
|
|
|
var path = pathArray[pathArrayIndex];
|
|
|
|
shapeBatch.addStrokePath(
|
|
|
|
/* Graphics Game Object Properties */
|
|
|
|
srcX, srcY, srcScaleX, srcScaleY, srcRotation,
|
|
|
|
/* Rectangle properties */
|
|
|
|
path.points,
|
|
|
|
lineWidth,
|
|
|
|
lineColor,
|
|
|
|
lineAlpha,
|
|
|
|
/* Transform */
|
|
|
|
mva, mvb, mvc, mvd, mve, mvf,
|
|
|
|
path === this._lastPath
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.FILL_RECT:
|
2017-03-06 18:13:39 +00:00
|
|
|
shapeBatch.addFillRect(
|
|
|
|
/* Graphics Game Object Properties */
|
|
|
|
srcX, srcY, srcScaleX, srcScaleY, srcRotation,
|
|
|
|
/* Rectangle properties */
|
|
|
|
commandBuffer[cmdIndex + 1] - cameraScrollX,
|
|
|
|
commandBuffer[cmdIndex + 2] - cameraScrollY,
|
|
|
|
commandBuffer[cmdIndex + 3],
|
|
|
|
commandBuffer[cmdIndex + 4],
|
|
|
|
fillColor,
|
|
|
|
fillAlpha,
|
|
|
|
/* Transform */
|
|
|
|
mva, mvb, mvc, mvd, mve, mvf
|
|
|
|
);
|
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
cmdIndex += 4;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-08 00:51:09 +00:00
|
|
|
case Commands.FILL_TRIANGLE:
|
|
|
|
shapeBatch.addFillTriangle(
|
|
|
|
/* Graphics Game Object Properties */
|
|
|
|
srcX, srcY, srcScaleX, srcScaleY, srcRotation,
|
|
|
|
/* Triangle properties */
|
|
|
|
commandBuffer[cmdIndex + 1] - cameraScrollX,
|
|
|
|
commandBuffer[cmdIndex + 2] - cameraScrollY,
|
|
|
|
commandBuffer[cmdIndex + 3] - cameraScrollX,
|
|
|
|
commandBuffer[cmdIndex + 4] - cameraScrollY,
|
|
|
|
commandBuffer[cmdIndex + 5] - cameraScrollX,
|
|
|
|
commandBuffer[cmdIndex + 6] - cameraScrollY,
|
|
|
|
fillColor,
|
|
|
|
fillAlpha,
|
|
|
|
/* Transform */
|
|
|
|
mva, mvb, mvc, mvd, mve, mvf
|
|
|
|
);
|
|
|
|
|
|
|
|
cmdIndex += 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.STROKE_TRIANGLE:
|
|
|
|
shapeBatch.addStrokeTriangle(
|
|
|
|
/* Graphics Game Object Properties */
|
|
|
|
srcX, srcY, srcScaleX, srcScaleY, srcRotation,
|
|
|
|
/* Triangle properties */
|
|
|
|
commandBuffer[cmdIndex + 1] - cameraScrollX,
|
|
|
|
commandBuffer[cmdIndex + 2] - cameraScrollY,
|
|
|
|
commandBuffer[cmdIndex + 3] - cameraScrollX,
|
|
|
|
commandBuffer[cmdIndex + 4] - cameraScrollY,
|
|
|
|
commandBuffer[cmdIndex + 5] - cameraScrollX,
|
|
|
|
commandBuffer[cmdIndex + 6] - cameraScrollY,
|
|
|
|
lineWidth,
|
|
|
|
lineColor,
|
|
|
|
lineAlpha,
|
|
|
|
/* Transform */
|
|
|
|
mva, mvb, mvc, mvd, mve, mvf
|
|
|
|
);
|
|
|
|
|
|
|
|
cmdIndex += 6;
|
|
|
|
break
|
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.LINE_TO:
|
2017-03-06 18:13:39 +00:00
|
|
|
if (lastPath !== null)
|
|
|
|
{
|
2017-03-14 22:13:31 +00:00
|
|
|
lastPath.points.push(new Point(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], lineWidth));
|
2017-03-06 18:13:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-14 22:13:31 +00:00
|
|
|
lastPath = new Path(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], lineWidth);
|
2017-03-06 18:13:39 +00:00
|
|
|
pathArray.push(lastPath);
|
|
|
|
}
|
2017-03-01 21:08:10 +00:00
|
|
|
cmdIndex += 2;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
case Commands.MOVE_TO:
|
2017-03-14 22:13:31 +00:00
|
|
|
lastPath = new Path(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], lineWidth);
|
2017-03-06 18:13:39 +00:00
|
|
|
pathArray.push(lastPath);
|
2017-03-01 21:08:10 +00:00
|
|
|
cmdIndex += 2;
|
|
|
|
break;
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-14 22:13:31 +00:00
|
|
|
case Commands.LINE_WIDTH_TO:
|
|
|
|
if (lastPath !== null)
|
|
|
|
{
|
|
|
|
lastPath.points.push(new Point(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], commandBuffer[cmdIndex + 3]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastPath = new Path(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], commandBuffer[cmdIndex + 3]);
|
|
|
|
pathArray.push(lastPath);
|
|
|
|
}
|
|
|
|
cmdIndex += 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.MOVE_WIDTH_TO:
|
|
|
|
lastPath = new Path(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], commandBuffer[cmdIndex + 3]);
|
|
|
|
pathArray.push(lastPath);
|
|
|
|
cmdIndex += 3;
|
|
|
|
break;
|
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
default:
|
|
|
|
console.error('Phaser: Invalid Graphics Command ID ' + cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
pathArray.length = 0;
|
2017-03-02 02:06:37 +00:00
|
|
|
};
|
2017-02-28 14:49:39 +00:00
|
|
|
|
|
|
|
module.exports = GraphicsWebGLRenderer;
|