|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Utils = require('../../../renderer/webgl/Utils'); |
| 8 | + |
| 9 | +/** |
| 10 | + * Renders this Game Object with the WebGL Renderer to the given Camera. |
| 11 | + * The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera. |
| 12 | + * This method should not be called directly. It is a utility function of the Render module. |
| 13 | + * |
| 14 | + * @method Phaser.GameObjects.Polygon#renderWebGL |
| 15 | + * @since 3.13.0 |
| 16 | + * @private |
| 17 | + * |
| 18 | + * @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer. |
| 19 | + * @param {Phaser.GameObjects.Polygon} src - The Game Object being rendered in this call. |
| 20 | + * @param {number} interpolationPercentage - Reserved for future use and custom pipelines. |
| 21 | + * @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object. |
| 22 | + * @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested |
| 23 | + */ |
| 24 | +var PolygonWebGLRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix) |
| 25 | +{ |
| 26 | + var pipeline = this.pipeline; |
| 27 | + |
| 28 | + var camMatrix = pipeline._tempMatrix1; |
| 29 | + var shapeMatrix = pipeline._tempMatrix2; |
| 30 | + var calcMatrix = pipeline._tempMatrix3; |
| 31 | + |
| 32 | + renderer.setPipeline(pipeline); |
| 33 | + |
| 34 | + shapeMatrix.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY); |
| 35 | + |
| 36 | + camMatrix.copyFrom(camera.matrix); |
| 37 | + |
| 38 | + if (parentMatrix) |
| 39 | + { |
| 40 | + // Multiply the camera by the parent matrix |
| 41 | + camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY); |
| 42 | + |
| 43 | + // Undo the camera scroll |
| 44 | + shapeMatrix.e = src.x; |
| 45 | + shapeMatrix.f = src.y; |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + shapeMatrix.e -= camera.scrollX * src.scrollFactorX; |
| 50 | + shapeMatrix.f -= camera.scrollY * src.scrollFactorY; |
| 51 | + } |
| 52 | + |
| 53 | + camMatrix.multiply(shapeMatrix, calcMatrix); |
| 54 | + |
| 55 | + var i; |
| 56 | + var dx = src._displayOriginX + src._curveBounds.x; |
| 57 | + var dy = src._displayOriginY + src._curveBounds.y; |
| 58 | + |
| 59 | + var alpha = camera.alpha * src.alpha; |
| 60 | + |
| 61 | + var path = src.pathData; |
| 62 | + |
| 63 | + if (src.isFilled) |
| 64 | + { |
| 65 | + var fillTintColor = Utils.getTintAppendFloatAlphaAndSwap(src.fillColor, src.fillAlpha * alpha); |
| 66 | + |
| 67 | + var pathIndexes = src.pathIndexes; |
| 68 | + |
| 69 | + for (i = 0; i < pathIndexes.length; i += 3) |
| 70 | + { |
| 71 | + var p0 = pathIndexes[i] * 2; |
| 72 | + var p1 = pathIndexes[i + 1] * 2; |
| 73 | + var p2 = pathIndexes[i + 2] * 2; |
| 74 | + |
| 75 | + var x0 = path[p0 + 0] - dx; |
| 76 | + var y0 = path[p0 + 1] - dy; |
| 77 | + var x1 = path[p1 + 0] - dx; |
| 78 | + var y1 = path[p1 + 1] - dy; |
| 79 | + var x2 = path[p2 + 0] - dx; |
| 80 | + var y2 = path[p2 + 1] - dy; |
| 81 | + |
| 82 | + var tx0 = calcMatrix.getX(x0, y0); |
| 83 | + var ty0 = calcMatrix.getY(x0, y0); |
| 84 | + |
| 85 | + var tx1 = calcMatrix.getX(x1, y1); |
| 86 | + var ty1 = calcMatrix.getY(x1, y1); |
| 87 | + |
| 88 | + var tx2 = calcMatrix.getX(x2, y2); |
| 89 | + var ty2 = calcMatrix.getY(x2, y2); |
| 90 | + |
| 91 | + pipeline.batchTri(tx0, ty0, tx1, ty1, tx2, ty2, 0, 0, 1, 1, fillTintColor, fillTintColor, fillTintColor, pipeline.tintEffect); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (src.isStroked) |
| 96 | + { |
| 97 | + var strokeTint = pipeline.strokeTint; |
| 98 | + var strokeTintColor = Utils.getTintAppendFloatAlphaAndSwap(src.strokeColor, src.strokeAlpha * alpha); |
| 99 | + |
| 100 | + strokeTint.TL = strokeTintColor; |
| 101 | + strokeTint.TR = strokeTintColor; |
| 102 | + strokeTint.BL = strokeTintColor; |
| 103 | + strokeTint.BR = strokeTintColor; |
| 104 | + |
| 105 | + var pathLength = path.length - 1; |
| 106 | + var lineWidth = src.lineWidth; |
| 107 | + var halfLineWidth = lineWidth / 2; |
| 108 | + |
| 109 | + var px1 = path[0] - dx; |
| 110 | + var py1 = path[1] - dy; |
| 111 | + |
| 112 | + if (!src.closePath) |
| 113 | + { |
| 114 | + pathLength -= 2; |
| 115 | + } |
| 116 | + |
| 117 | + for (i = 2; i < pathLength; i += 2) |
| 118 | + { |
| 119 | + var px2 = path[i] - dx; |
| 120 | + var py2 = path[i + 1] - dy; |
| 121 | + |
| 122 | + pipeline.batchLine( |
| 123 | + px1, |
| 124 | + py1, |
| 125 | + px2, |
| 126 | + py2, |
| 127 | + halfLineWidth, |
| 128 | + halfLineWidth, |
| 129 | + lineWidth, |
| 130 | + i - 2, |
| 131 | + (src.closePath) ? (i === pathLength - 1) : false |
| 132 | + ); |
| 133 | + |
| 134 | + px1 = px2; |
| 135 | + py1 = py2; |
| 136 | + } |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +module.exports = PolygonWebGLRenderer; |
0 commit comments