/
GridWebGLRenderer.js
195 lines (153 loc) · 5.24 KB
/
GridWebGLRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var GetCalcMatrix = require('../../GetCalcMatrix');
var Utils = require('../../../renderer/webgl/Utils');
/**
* Renders this Game Object with the WebGL Renderer to the given Camera.
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
* This method should not be called directly. It is a utility function of the Render module.
*
* @method Phaser.GameObjects.Grid#renderWebGL
* @since 3.13.0
* @private
*
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
* @param {Phaser.GameObjects.Grid} src - The Game Object being rendered in this call.
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
*/
var GridWebGLRenderer = function (renderer, src, camera, parentMatrix)
{
camera.addToRenderList(src);
var pipeline = renderer.pipelines.set(src.pipeline);
var result = GetCalcMatrix(src, camera, parentMatrix);
var calcMatrix = pipeline.calcMatrix.copyFrom(result.calc);
calcMatrix.translate(-src._displayOriginX, -src._displayOriginY);
var alpha = camera.alpha * src.alpha;
// Work out the grid size
var width = src.width;
var height = src.height;
var cellWidth = src.cellWidth;
var cellHeight = src.cellHeight;
var gridWidth = Math.ceil(width / cellWidth);
var gridHeight = Math.ceil(height / cellHeight);
var cellWidthA = cellWidth;
var cellHeightA = cellHeight;
var cellWidthB = cellWidth - ((gridWidth * cellWidth) - width);
var cellHeightB = cellHeight - ((gridHeight * cellHeight) - height);
var fillTint;
var fillTintColor;
var showCells = src.showCells;
var showAltCells = src.showAltCells;
var showOutline = src.showOutline;
var x = 0;
var y = 0;
var r = 0;
var cw = 0;
var ch = 0;
if (showOutline)
{
// To make room for the grid lines (in case alpha < 1)
cellWidthA--;
cellHeightA--;
if (cellWidthB === cellWidth)
{
cellWidthB--;
}
if (cellHeightB === cellHeight)
{
cellHeightB--;
}
}
renderer.pipelines.preBatch(src);
if (showCells && src.fillAlpha > 0)
{
fillTint = pipeline.fillTint;
fillTintColor = Utils.getTintAppendFloatAlpha(src.fillColor, src.fillAlpha * alpha);
fillTint.TL = fillTintColor;
fillTint.TR = fillTintColor;
fillTint.BL = fillTintColor;
fillTint.BR = fillTintColor;
for (y = 0; y < gridHeight; y++)
{
if (showAltCells)
{
r = y % 2;
}
for (x = 0; x < gridWidth; x++)
{
if (showAltCells && r)
{
r = 0;
continue;
}
r++;
cw = (x < gridWidth - 1) ? cellWidthA : cellWidthB;
ch = (y < gridHeight - 1) ? cellHeightA : cellHeightB;
pipeline.batchFillRect(
x * cellWidth,
y * cellHeight,
cw,
ch
);
}
}
}
if (showAltCells && src.altFillAlpha > 0)
{
fillTint = pipeline.fillTint;
fillTintColor = Utils.getTintAppendFloatAlpha(src.altFillColor, src.altFillAlpha * alpha);
fillTint.TL = fillTintColor;
fillTint.TR = fillTintColor;
fillTint.BL = fillTintColor;
fillTint.BR = fillTintColor;
for (y = 0; y < gridHeight; y++)
{
if (showAltCells)
{
r = y % 2;
}
for (x = 0; x < gridWidth; x++)
{
if (showAltCells && !r)
{
r = 1;
continue;
}
r = 0;
cw = (x < gridWidth - 1) ? cellWidthA : cellWidthB;
ch = (y < gridHeight - 1) ? cellHeightA : cellHeightB;
pipeline.batchFillRect(
x * cellWidth,
y * cellHeight,
cw,
ch
);
}
}
}
if (showOutline && src.outlineFillAlpha > 0)
{
var strokeTint = pipeline.strokeTint;
var color = Utils.getTintAppendFloatAlpha(src.outlineFillColor, src.outlineFillAlpha * alpha);
strokeTint.TL = color;
strokeTint.TR = color;
strokeTint.BL = color;
strokeTint.BR = color;
for (x = 1; x < gridWidth; x++)
{
var x1 = x * cellWidth;
pipeline.batchLine(x1, 0, x1, height, 1, 1, 1, 0, false);
}
for (y = 1; y < gridHeight; y++)
{
var y1 = y * cellHeight;
pipeline.batchLine(0, y1, width, y1, 1, 1, 1, 0, false);
}
}
renderer.pipelines.postBatch(src);
};
module.exports = GridWebGLRenderer;