/
DynamicBitmapTextWebGLRenderer.js
282 lines (220 loc) · 8.03 KB
/
DynamicBitmapTextWebGLRenderer.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @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 TransformMatrix = require('../../components/TransformMatrix');
var Utils = require('../../../renderer/webgl/Utils');
var tempMatrix = new TransformMatrix();
/**
* 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.DynamicBitmapText#renderWebGL
* @since 3.0.0
* @private
*
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
* @param {Phaser.GameObjects.DynamicBitmapText} 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 DynamicBitmapTextWebGLRenderer = function (renderer, src, camera, parentMatrix)
{
var text = src.text;
var textLength = text.length;
if (textLength === 0)
{
return;
}
camera.addToRenderList(src);
var pipeline = renderer.pipelines.set(src.pipeline, src);
var result = GetCalcMatrix(src, camera, parentMatrix);
var spriteMatrix = result.sprite;
var calcMatrix = result.calc;
var fontMatrix = tempMatrix;
var crop = (src.cropWidth > 0 || src.cropHeight > 0);
if (crop)
{
pipeline.flush();
renderer.pushScissor(
calcMatrix.tx,
calcMatrix.ty,
src.cropWidth * calcMatrix.scaleX,
src.cropHeight * calcMatrix.scaleY
);
}
var frame = src.frame;
var texture = frame.glTexture;
var tintEffect = src.tintFill;
var tintTL = Utils.getTintAppendFloatAlpha(src.tintTopLeft, camera.alpha * src._alphaTL);
var tintTR = Utils.getTintAppendFloatAlpha(src.tintTopRight, camera.alpha * src._alphaTR);
var tintBL = Utils.getTintAppendFloatAlpha(src.tintBottomLeft, camera.alpha * src._alphaBL);
var tintBR = Utils.getTintAppendFloatAlpha(src.tintBottomRight, camera.alpha * src._alphaBR);
var textureUnit = pipeline.setGameObject(src);
var xAdvance = 0;
var yAdvance = 0;
var charCode = 0;
var lastCharCode = 0;
var letterSpacing = src.letterSpacing;
var glyph;
var glyphW = 0;
var glyphH = 0;
var lastGlyph;
var scrollX = src.scrollX;
var scrollY = src.scrollY;
var fontData = src.fontData;
var chars = fontData.chars;
var lineHeight = fontData.lineHeight;
var scale = (src.fontSize / fontData.size);
var rotation = 0;
var align = src._align;
var currentLine = 0;
var lineOffsetX = 0;
// Update the bounds - skipped internally if not dirty
var bounds = src.getTextBounds(false);
// In case the method above changed it (word wrapping)
if (src.maxWidth > 0)
{
text = bounds.wrappedText;
textLength = text.length;
}
var lineData = src._bounds.lines;
if (align === 1)
{
lineOffsetX = (lineData.longest - lineData.lengths[0]) / 2;
}
else if (align === 2)
{
lineOffsetX = (lineData.longest - lineData.lengths[0]);
}
var roundPixels = camera.roundPixels;
var displayCallback = src.displayCallback;
var callbackData = src.callbackData;
renderer.pipelines.preBatch(src);
for (var i = 0; i < textLength; i++)
{
charCode = text.charCodeAt(i);
// Carriage-return
if (charCode === 10)
{
currentLine++;
if (align === 1)
{
lineOffsetX = (lineData.longest - lineData.lengths[currentLine]) / 2;
}
else if (align === 2)
{
lineOffsetX = (lineData.longest - lineData.lengths[currentLine]);
}
xAdvance = 0;
yAdvance += lineHeight;
lastGlyph = null;
continue;
}
glyph = chars[charCode];
if (!glyph)
{
continue;
}
glyphW = glyph.width;
glyphH = glyph.height;
var x = (glyph.xOffset + xAdvance) - scrollX;
var y = (glyph.yOffset + yAdvance) - scrollY;
if (lastGlyph !== null)
{
var kerningOffset = glyph.kerning[lastCharCode];
x += (kerningOffset !== undefined) ? kerningOffset : 0;
}
xAdvance += glyph.xAdvance + letterSpacing;
lastGlyph = glyph;
lastCharCode = charCode;
// Nothing to render or a space? Then skip to the next glyph
if (glyphW === 0 || glyphH === 0 || charCode === 32)
{
continue;
}
scale = (src.fontSize / src.fontData.size);
rotation = 0;
if (displayCallback)
{
callbackData.color = 0;
callbackData.tint.topLeft = tintTL;
callbackData.tint.topRight = tintTR;
callbackData.tint.bottomLeft = tintBL;
callbackData.tint.bottomRight = tintBR;
callbackData.index = i;
callbackData.charCode = charCode;
callbackData.x = x;
callbackData.y = y;
callbackData.scale = scale;
callbackData.rotation = rotation;
callbackData.data = glyph.data;
var output = displayCallback(callbackData);
x = output.x;
y = output.y;
scale = output.scale;
rotation = output.rotation;
if (output.color)
{
tintTL = output.color;
tintTR = output.color;
tintBL = output.color;
tintBR = output.color;
}
else
{
tintTL = output.tint.topLeft;
tintTR = output.tint.topRight;
tintBL = output.tint.bottomLeft;
tintBR = output.tint.bottomRight;
}
tintTL = Utils.getTintAppendFloatAlpha(tintTL, camera.alpha * src._alphaTL);
tintTR = Utils.getTintAppendFloatAlpha(tintTR, camera.alpha * src._alphaTR);
tintBL = Utils.getTintAppendFloatAlpha(tintBL, camera.alpha * src._alphaBL);
tintBR = Utils.getTintAppendFloatAlpha(tintBR, camera.alpha * src._alphaBR);
}
x *= scale;
y *= scale;
x -= src.displayOriginX;
y -= src.displayOriginY;
x += lineOffsetX;
fontMatrix.applyITRS(x, y, rotation, scale, scale);
calcMatrix.multiply(fontMatrix, spriteMatrix);
var u0 = glyph.u0;
var v0 = glyph.v0;
var u1 = glyph.u1;
var v1 = glyph.v1;
var xw = glyphW;
var yh = glyphH;
var tx0 = spriteMatrix.e;
var ty0 = spriteMatrix.f;
var tx1 = yh * spriteMatrix.c + spriteMatrix.e;
var ty1 = yh * spriteMatrix.d + spriteMatrix.f;
var tx2 = xw * spriteMatrix.a + yh * spriteMatrix.c + spriteMatrix.e;
var ty2 = xw * spriteMatrix.b + yh * spriteMatrix.d + spriteMatrix.f;
var tx3 = xw * spriteMatrix.a + spriteMatrix.e;
var ty3 = xw * spriteMatrix.b + spriteMatrix.f;
if (roundPixels)
{
tx0 = Math.round(tx0);
ty0 = Math.round(ty0);
tx1 = Math.round(tx1);
ty1 = Math.round(ty1);
tx2 = Math.round(tx2);
ty2 = Math.round(ty2);
tx3 = Math.round(tx3);
ty3 = Math.round(ty3);
}
pipeline.batchQuad(src, tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3, u0, v0, u1, v1, tintTL, tintTR, tintBL, tintBR, tintEffect, texture, textureUnit);
}
if (crop)
{
pipeline.flush();
renderer.popScissor();
}
renderer.pipelines.postBatch(src);
};
module.exports = DynamicBitmapTextWebGLRenderer;