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-05-08 20:13:48 +00:00
|
|
|
var matrixStack = new Float32Array(6 * 1000);
|
|
|
|
var matrixStackLength = 0;
|
|
|
|
var currentMatrix = new TransformMatrix();
|
2017-03-01 21:08:10 +00:00
|
|
|
|
2017-03-15 19:23:10 +00:00
|
|
|
var Point = function (x, y, width, rgb, alpha)
|
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-15 19:23:10 +00:00
|
|
|
this.rgb = rgb;
|
|
|
|
this.alpha = alpha;
|
2017-03-01 21:08:10 +00:00
|
|
|
};
|
|
|
|
|
2017-03-15 19:23:10 +00:00
|
|
|
var Path = function (x, y, width, rgb, alpha)
|
2017-03-01 21:08:10 +00:00
|
|
|
{
|
|
|
|
this.points = [];
|
2017-03-01 23:23:46 +00:00
|
|
|
this.pointsLength = 1;
|
2017-03-15 19:23:10 +00:00
|
|
|
this.points[0] = new Point(x, y, width, rgb, alpha);
|
2017-03-01 21:08:10 +00:00
|
|
|
};
|
|
|
|
|
2017-05-11 02:52:32 +00:00
|
|
|
var GraphicsWebGLRenderer = function (renderer, gameObject, interpolationPercentage, camera, forceRenderTarget)
|
2017-02-28 14:49:39 +00:00
|
|
|
{
|
|
|
|
if (this.renderMask !== this.renderFlags)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-03-02 02:06:37 +00:00
|
|
|
|
2017-05-11 02:52:32 +00:00
|
|
|
var renderTarget = forceRenderTarget || gameObject.renderTarget;
|
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-04-13 15:45:01 +00:00
|
|
|
const srcX = gameObject.x - cameraScrollX;
|
|
|
|
const srcY = gameObject.y - cameraScrollY;
|
|
|
|
const srcScaleX = gameObject.scaleX;
|
|
|
|
const srcScaleY = gameObject.scaleY;
|
|
|
|
const srcRotation = -gameObject.rotation;
|
|
|
|
var commandBuffer = gameObject.commandBuffer;
|
2017-03-01 21:08:10 +00:00
|
|
|
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
|
|
|
|
2017-04-13 15:45:01 +00:00
|
|
|
renderer.setRenderer(shapeBatch, null, renderTarget);
|
2017-03-01 21:08:10 +00:00
|
|
|
|
|
|
|
for (var cmdIndex = 0, cmdLength = commandBuffer.length; cmdIndex < cmdLength; ++cmdIndex)
|
|
|
|
{
|
|
|
|
var cmd = commandBuffer[cmdIndex];
|
|
|
|
|
|
|
|
switch(cmd)
|
|
|
|
{
|
|
|
|
case Commands.ARC:
|
2017-03-30 13:06:35 +00:00
|
|
|
iteration = 0;
|
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-21 20:08:43 +00:00
|
|
|
lastPath = new Path(tx, ty, lineWidth, lineColor, lineAlpha);
|
2017-03-06 18:13:39 +00:00
|
|
|
pathArray.push(lastPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-21 20:08:43 +00:00
|
|
|
lastPath.points.push(new Point(tx, ty, lineWidth, lineColor, lineAlpha));
|
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-21 20:08:43 +00:00
|
|
|
lastPath = new Path(lastPoint.x, lastPoint.y, lastPoint.width, lastPoint.rgb, lastPoint.alpha);
|
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 */
|
2017-05-08 20:13:48 +00:00
|
|
|
mva, mvb, mvc, mvd, mve, mvf,
|
|
|
|
currentMatrix
|
2017-03-06 18:13:39 +00:00
|
|
|
);
|
|
|
|
}
|
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,
|
2017-05-08 20:13:48 +00:00
|
|
|
path === this._lastPath,
|
|
|
|
currentMatrix
|
|
|
|
|
2017-03-07 04:29:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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 */
|
2017-05-08 20:13:48 +00:00
|
|
|
mva, mvb, mvc, mvd, mve, mvf,
|
|
|
|
currentMatrix
|
|
|
|
|
2017-03-06 18:13:39 +00:00
|
|
|
);
|
|
|
|
|
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 */
|
2017-05-08 20:13:48 +00:00
|
|
|
mva, mvb, mvc, mvd, mve, mvf,
|
|
|
|
currentMatrix
|
|
|
|
|
2017-03-08 00:51:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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 */
|
2017-05-08 20:13:48 +00:00
|
|
|
mva, mvb, mvc, mvd, mve, mvf,
|
|
|
|
currentMatrix
|
|
|
|
|
2017-03-08 00:51:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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-21 20:08:43 +00:00
|
|
|
lastPath.points.push(new Point(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], lineWidth, lineColor, lineAlpha));
|
2017-03-06 18:13:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-21 20:08:43 +00:00
|
|
|
lastPath = new Path(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], lineWidth, lineColor, lineAlpha);
|
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-21 20:08:43 +00:00
|
|
|
lastPath = new Path(commandBuffer[cmdIndex + 1], commandBuffer[cmdIndex + 2], lineWidth, lineColor, lineAlpha);
|
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-15 19:23:10 +00:00
|
|
|
case Commands.LINE_FX_TO:
|
2017-03-14 22:13:31 +00:00
|
|
|
if (lastPath !== null)
|
|
|
|
{
|
2017-03-15 19:23:10 +00:00
|
|
|
lastPath.points.push(new Point(
|
|
|
|
commandBuffer[cmdIndex + 1],
|
|
|
|
commandBuffer[cmdIndex + 2],
|
|
|
|
commandBuffer[cmdIndex + 3],
|
|
|
|
commandBuffer[cmdIndex + 4],
|
|
|
|
commandBuffer[cmdIndex + 5]
|
|
|
|
));
|
2017-03-14 22:13:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-15 19:23:10 +00:00
|
|
|
lastPath = new Path(
|
|
|
|
commandBuffer[cmdIndex + 1],
|
|
|
|
commandBuffer[cmdIndex + 2],
|
|
|
|
commandBuffer[cmdIndex + 3],
|
|
|
|
commandBuffer[cmdIndex + 4],
|
|
|
|
commandBuffer[cmdIndex + 5]
|
|
|
|
);
|
2017-03-14 22:13:31 +00:00
|
|
|
pathArray.push(lastPath);
|
|
|
|
}
|
2017-03-15 19:23:10 +00:00
|
|
|
cmdIndex += 5;
|
2017-03-14 22:13:31 +00:00
|
|
|
break;
|
|
|
|
|
2017-03-15 19:23:10 +00:00
|
|
|
case Commands.MOVE_FX_TO:
|
|
|
|
lastPath = new Path(
|
|
|
|
commandBuffer[cmdIndex + 1],
|
|
|
|
commandBuffer[cmdIndex + 2],
|
|
|
|
commandBuffer[cmdIndex + 3],
|
|
|
|
commandBuffer[cmdIndex + 4],
|
|
|
|
commandBuffer[cmdIndex + 5]
|
|
|
|
);
|
2017-03-14 22:13:31 +00:00
|
|
|
pathArray.push(lastPath);
|
2017-03-15 19:23:10 +00:00
|
|
|
cmdIndex += 5;
|
2017-03-14 22:13:31 +00:00
|
|
|
break;
|
|
|
|
|
2017-05-08 20:13:48 +00:00
|
|
|
case Commands.SAVE:
|
|
|
|
matrixStack[matrixStackLength + 0] = currentMatrix.matrix[0];
|
|
|
|
matrixStack[matrixStackLength + 1] = currentMatrix.matrix[1];
|
|
|
|
matrixStack[matrixStackLength + 2] = currentMatrix.matrix[2];
|
|
|
|
matrixStack[matrixStackLength + 3] = currentMatrix.matrix[3];
|
|
|
|
matrixStack[matrixStackLength + 4] = currentMatrix.matrix[4];
|
|
|
|
matrixStack[matrixStackLength + 5] = currentMatrix.matrix[5];
|
|
|
|
matrixStackLength += 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.RESTORE:
|
|
|
|
matrixStackLength -= 6;
|
|
|
|
currentMatrix.matrix[0] = matrixStack[matrixStackLength + 0];
|
|
|
|
currentMatrix.matrix[1] = matrixStack[matrixStackLength + 1];
|
|
|
|
currentMatrix.matrix[2] = matrixStack[matrixStackLength + 2];
|
|
|
|
currentMatrix.matrix[3] = matrixStack[matrixStackLength + 3];
|
|
|
|
currentMatrix.matrix[4] = matrixStack[matrixStackLength + 4];
|
|
|
|
currentMatrix.matrix[5] = matrixStack[matrixStackLength + 5];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.TRANSLATE:
|
|
|
|
currentMatrix.translate(
|
|
|
|
commandBuffer[cmdIndex + 1],
|
|
|
|
commandBuffer[cmdIndex + 2]
|
|
|
|
);
|
|
|
|
cmdIndex += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.SCALE:
|
|
|
|
currentMatrix.scale(
|
|
|
|
commandBuffer[cmdIndex + 1],
|
|
|
|
commandBuffer[cmdIndex + 2]
|
|
|
|
);
|
|
|
|
cmdIndex += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Commands.ROTATE:
|
|
|
|
currentMatrix.rotate(
|
|
|
|
-commandBuffer[cmdIndex + 1]
|
|
|
|
);
|
|
|
|
cmdIndex += 1;
|
|
|
|
break;
|
|
|
|
|
2017-03-01 21:08:10 +00:00
|
|
|
default:
|
|
|
|
console.error('Phaser: Invalid Graphics Command ID ' + cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-08 20:13:48 +00:00
|
|
|
currentMatrix.loadIdentity();
|
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;
|