Skip to content

Commit dd0490d

Browse files
committed
Removed batchBitmapText, tidied up and moved to its own renderer
1 parent 8b13631 commit dd0490d

2 files changed

Lines changed: 156 additions & 295 deletions

File tree

src/gameobjects/bitmaptext/static/BitmapTextWebGLRenderer.js

Lines changed: 155 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var GameObject = require('../../GameObject');
8+
var Utils = require('../../../renderer/webgl/Utils');
89

910
/**
1011
* Renders this Game Object with the WebGL Renderer to the given Camera.
@@ -16,22 +17,171 @@ var GameObject = require('../../GameObject');
1617
* @private
1718
*
1819
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
19-
* @param {Phaser.GameObjects.BitmapText} gameObject - The Game Object being rendered in this call.
20+
* @param {Phaser.GameObjects.BitmapText} src - The Game Object being rendered in this call.
2021
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
2122
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
2223
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
2324
*/
24-
var BitmapTextWebGLRenderer = function (renderer, gameObject, interpolationPercentage, camera, parentMatrix)
25+
var BitmapTextWebGLRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2526
{
26-
var text = gameObject.text;
27+
var text = src.text;
2728
var textLength = text.length;
2829

29-
if (GameObject.RENDER_MASK !== gameObject.renderFlags || textLength === 0 || (gameObject.cameraFilter > 0 && (gameObject.cameraFilter & camera._id)))
30+
if (GameObject.RENDER_MASK !== src.renderFlags || textLength === 0 || (src.cameraFilter > 0 && (src.cameraFilter & camera._id)))
3031
{
3132
return;
3233
}
3334

34-
this.pipeline.batchBitmapText(this, camera, parentMatrix);
35+
var pipeline = this.pipeline;
36+
37+
renderer.setPipeline(pipeline);
38+
39+
var camMatrix = pipeline._tempCameraMatrix;
40+
var spriteMatrix = pipeline._tempSpriteMatrix;
41+
42+
spriteMatrix.applyITRS(src.x - camera.scrollX * src.scrollFactorX, src.y - camera.scrollY * src.scrollFactorY, src.rotation, src.scaleX, src.scaleY);
43+
44+
camMatrix.copyFrom(camera.matrix);
45+
46+
var calcMatrix;
47+
48+
if (parentMatrix)
49+
{
50+
// Multiply the camera by the parent matrix
51+
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
52+
53+
// Undo the camera scroll
54+
spriteMatrix.e = src.x;
55+
spriteMatrix.f = src.y;
56+
57+
// Multiply by the Sprite matrix
58+
calcMatrix = camMatrix.multiply(spriteMatrix);
59+
}
60+
else
61+
{
62+
calcMatrix = camMatrix.multiply(spriteMatrix);
63+
}
64+
65+
var frame = src.frame;
66+
var texture = frame.glTexture;
67+
var textureX = frame.cutX;
68+
var textureY = frame.cutY;
69+
var textureWidth = texture.width;
70+
var textureHeight = texture.height;
71+
72+
var tintEffect = (src._isTinted && src.tintFill);
73+
var tintTL = Utils.getTintAppendFloatAlpha(src._tintTL, camera.alpha * src._alphaTL);
74+
var tintTR = Utils.getTintAppendFloatAlpha(src._tintTR, camera.alpha * src._alphaTR);
75+
var tintBL = Utils.getTintAppendFloatAlpha(src._tintBL, camera.alpha * src._alphaBL);
76+
var tintBR = Utils.getTintAppendFloatAlpha(src._tintBR, camera.alpha * src._alphaBR);
77+
78+
pipeline.setTexture2D(texture, 0);
79+
80+
var xAdvance = 0;
81+
var yAdvance = 0;
82+
var indexCount = 0;
83+
var charCode = 0;
84+
var lastCharCode = 0;
85+
var letterSpacing = src.letterSpacing;
86+
var glyph;
87+
var glyphX = 0;
88+
var glyphY = 0;
89+
var glyphW = 0;
90+
var glyphH = 0;
91+
var lastGlyph;
92+
93+
var fontData = src.fontData;
94+
var chars = fontData.chars;
95+
var lineHeight = fontData.lineHeight;
96+
var scale = (src.fontSize / fontData.size);
97+
98+
var roundPixels = camera.roundPixels;
99+
100+
for (var i = 0; i < textLength; i++)
101+
{
102+
charCode = text.charCodeAt(i);
103+
104+
// Carriage-return
105+
if (charCode === 10)
106+
{
107+
xAdvance = 0;
108+
indexCount = 0;
109+
yAdvance += lineHeight;
110+
lastGlyph = null;
111+
112+
continue;
113+
}
114+
115+
glyph = chars[charCode];
116+
117+
if (!glyph)
118+
{
119+
continue;
120+
}
121+
122+
glyphX = textureX + glyph.x;
123+
glyphY = textureY + glyph.y;
124+
125+
glyphW = glyph.width;
126+
glyphH = glyph.height;
127+
128+
var x = (indexCount + glyph.xOffset + xAdvance) * scale;
129+
var y = (glyph.yOffset + yAdvance) * scale;
130+
131+
if (lastGlyph !== null)
132+
{
133+
var kerningOffset = glyph.kerning[lastCharCode];
134+
x += (kerningOffset !== undefined) ? kerningOffset : 0;
135+
}
136+
137+
xAdvance += glyph.xAdvance + letterSpacing;
138+
indexCount++;
139+
lastGlyph = glyph;
140+
lastCharCode = charCode;
141+
142+
// Nothing to render or a space? Then skip to the next glyph
143+
if (glyphW === 0 || glyphH === 0 || charCode === 32)
144+
{
145+
continue;
146+
}
147+
148+
var u0 = glyphX / textureWidth;
149+
var v0 = glyphY / textureHeight;
150+
var u1 = (glyphX + glyphW) / textureWidth;
151+
var v1 = (glyphY + glyphH) / textureHeight;
152+
153+
var xw = x + glyphW * scale;
154+
var yh = y + glyphH * scale;
155+
156+
var tx0 = x * calcMatrix.a + y * calcMatrix.c + calcMatrix.e;
157+
var ty0 = x * calcMatrix.b + y * calcMatrix.d + calcMatrix.f;
158+
159+
var tx1 = x * calcMatrix.a + yh * calcMatrix.c + calcMatrix.e;
160+
var ty1 = x * calcMatrix.b + yh * calcMatrix.d + calcMatrix.f;
161+
162+
var tx2 = xw * calcMatrix.a + yh * calcMatrix.c + calcMatrix.e;
163+
var ty2 = xw * calcMatrix.b + yh * calcMatrix.d + calcMatrix.f;
164+
165+
var tx3 = xw * calcMatrix.a + y * calcMatrix.c + calcMatrix.e;
166+
var ty3 = xw * calcMatrix.b + y * calcMatrix.d + calcMatrix.f;
167+
168+
if (roundPixels)
169+
{
170+
tx0 |= 0;
171+
ty0 |= 0;
172+
173+
tx1 |= 0;
174+
ty1 |= 0;
175+
176+
tx2 |= 0;
177+
ty2 |= 0;
178+
179+
tx3 |= 0;
180+
ty3 |= 0;
181+
}
182+
183+
pipeline.batchVertices(tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3, u0, v0, u1, v1, tintTL, tintTR, tintBL, tintBR, tintEffect);
184+
}
35185
};
36186

37187
module.exports = BitmapTextWebGLRenderer;

0 commit comments

Comments
 (0)