Skip to content

Commit 7b1ad0b

Browse files
committed
transparent, clearBeforeRender and roundPixels now affect the rendering result
1 parent b889a39 commit 7b1ad0b

4 files changed

Lines changed: 198 additions & 45 deletions

File tree

src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,10 @@ var StaticTilemapLayer = new Class({
174174
texCoords = tileset.getTileTextureCoordinates(tile.index);
175175
if (texCoords === null) { continue; }
176176

177-
// Inset UV coordinates by 0.5px to prevent tile bleeding
178-
var u0 = (texCoords.x + 0.5) / width;
179-
var v0 = (texCoords.y + 0.5) / height;
180-
var u1 = (texCoords.x + tile.width - 0.5) / width;
181-
var v1 = (texCoords.y + tile.height - 0.5) / height;
177+
var u0 = texCoords.x / width;
178+
var v0 = texCoords.y / height;
179+
var u1 = (texCoords.x + tile.width) / width;
180+
var v1 = (texCoords.y + tile.height) / height;
182181

183182
var tx0 = tx;
184183
var ty0 = ty;

src/renderer/webgl/WebGLRenderer.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ var WebGLRenderer = new Class({
2121
function WebGLRenderer (game)
2222
{
2323
var renderer = this;
24-
var config = {
25-
backgroundColor: game.config.backgroundColor,
26-
contextCreation: {
27-
alpha: false,
28-
depth: false, // enable when 3D is added in the future
29-
antialias: true,
30-
premultipliedAlpha: true,
31-
stencil: true,
32-
preserveDrawingBuffer: false,
33-
failIfMajorPerformanceCaveat: false
34-
}
24+
var contextCreationConfig = {
25+
alpha: game.config.transparent,
26+
depth: false, // enable when 3D is added in the future
27+
antialias: game.config.antialias,
28+
premultipliedAlpha: game.config.transparent,
29+
stencil: true,
30+
preserveDrawingBuffer: game.config.preserveDrawingBuffer,
31+
failIfMajorPerformanceCaveat: false,
32+
powerPreference: game.config.powerPreference
3533
};
3634

35+
this.contextCreationConfig = contextCreationConfig;
3736
this.game = game;
37+
this.gameConfig = game.config;
3838
this.type = CONST.WEBGL;
3939
this.width = game.config.width * game.config.resolution;
4040
this.height = game.config.height * game.config.resolution;
@@ -88,7 +88,7 @@ var WebGLRenderer = new Class({
8888

8989
this.canvas.addEventListener('webglcontextrestored', function (event) {
9090
renderer.contextLost = false;
91-
renderer.init(config);
91+
renderer.init(renderer.gameConfig, renderer.contextCreationConfig);
9292
for (var index = 0; index < renderer.restoredContextCallbacks.length; ++index)
9393
{
9494
var callback = renderer.restoredContextCallbacks[index];
@@ -101,14 +101,14 @@ var WebGLRenderer = new Class({
101101
this.supportedExtensions = null;
102102
this.extensions = {};
103103

104-
this.init(config);
104+
this.init(this.gameConfig, this.contextCreationConfig);
105105
},
106106

107-
init: function (config)
107+
init: function (config, contextCreationConfig)
108108
{
109109
var canvas = this.canvas;
110110
var clearColor = config.backgroundColor;
111-
var gl = canvas.getContext('webgl', config.contextCreation) || canvas.getContext('experimental-webgl', config.contextCreation);
111+
var gl = canvas.getContext('webgl', contextCreationConfig) || canvas.getContext('experimental-webgl', contextCreationConfig);
112112

113113
if (!gl)
114114
{
@@ -143,7 +143,7 @@ var WebGLRenderer = new Class({
143143
this.addPipeline('Light2D', new ForwardDiffuseLightPipeline(this.game, gl, this));
144144

145145
this.setBlendMode(CONST.BlendModes.NORMAL);
146-
this.resize(this.width, this.height, this.game.config.resolution);
146+
this.resize(this.width, this.height, config.resolution);
147147

148148
return this;
149149
},
@@ -238,7 +238,7 @@ var WebGLRenderer = new Class({
238238
else console.warn('Pipeline', pipelineName, ' already exists.');
239239

240240
pipelineInstance.name = pipelineName;
241-
this.pipelines[pipelineName].resize(this.width, this.height, this.game.config.resolution);
241+
this.pipelines[pipelineName].resize(this.width, this.height, this.gameConfig.resolution);
242242

243243
return this;
244244
},
@@ -450,7 +450,7 @@ var WebGLRenderer = new Class({
450450
{
451451
filter = gl.LINEAR;
452452
}
453-
else if (scaleMode === CONST.ScaleModes.NEAREST || this.game.config.pixelArt)
453+
else if (scaleMode === CONST.ScaleModes.NEAREST || this.gameConfig.pixelArt)
454454
{
455455
filter = gl.NEAREST;
456456
}
@@ -683,12 +683,14 @@ var WebGLRenderer = new Class({
683683
if (this.contextLost) return;
684684

685685
var gl = this.gl;
686-
var color = this.game.config.backgroundColor;
686+
var color = this.gameConfig.backgroundColor;
687687
var pipelines = this.pipelines;
688688

689689
// Bind custom framebuffer here
690690
gl.clearColor(color.redGL, color.greenGL, color.blueGL, color.alphaGL);
691-
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
691+
692+
if (this.gameConfig.clearBeforeRender)
693+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
692694

693695
for (var key in pipelines)
694696
{

src/renderer/webgl/pipelines/FlatTintPipeline.js

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ var FlatTintPipeline = new Class({
9999
return this;
100100
},
101101

102-
batchFillRect: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, x, y, width, height, fillColor, fillAlpha, a1, b1, c1, d1, e1, f1, currentMatrix)
102+
batchFillRect: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, x, y, width, height, fillColor, fillAlpha, a1, b1, c1, d1, e1, f1, currentMatrix, roundPixels)
103103
{
104104
this.renderer.setPipeline(this);
105105

106106
if (this.vertexCount + 6 > this.vertexCapacity)
107107
{
108108
this.flush();
109109
}
110-
110+
111+
var renderer = this.renderer;
112+
var resolution = renderer.gameConfig.resolution;
111113
var vertexViewF32 = this.vertexViewF32;
112114
var vertexViewU32 = this.vertexViewU32;
113115
var vertexOffset = this.vertexCount * this.vertexComponentCount;
@@ -135,6 +137,18 @@ var FlatTintPipeline = new Class({
135137
var ty3 = xw * b + y * d + f;
136138
var tint = Utils.getTintAppendFloatAlphaAndSwap(fillColor, fillAlpha);
137139

140+
if (roundPixels)
141+
{
142+
tx0 = ((tx0 * resolution)|0) / resolution;
143+
ty0 = ((ty0 * resolution)|0) / resolution;
144+
tx1 = ((tx1 * resolution)|0) / resolution;
145+
ty1 = ((ty1 * resolution)|0) / resolution;
146+
tx2 = ((tx2 * resolution)|0) / resolution;
147+
ty2 = ((ty2 * resolution)|0) / resolution;
148+
tx3 = ((tx3 * resolution)|0) / resolution;
149+
ty3 = ((ty3 * resolution)|0) / resolution;
150+
}
151+
138152
vertexViewF32[vertexOffset + 0] = tx0;
139153
vertexViewF32[vertexOffset + 1] = ty0;
140154
vertexViewU32[vertexOffset + 2] = tint;
@@ -157,7 +171,7 @@ var FlatTintPipeline = new Class({
157171
this.vertexCount += 6;
158172
},
159173

160-
batchFillTriangle: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, x0, y0, x1, y1, x2, y2, fillColor, fillAlpha, a1, b1, c1, d1, e1, f1, currentMatrix)
174+
batchFillTriangle: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, x0, y0, x1, y1, x2, y2, fillColor, fillAlpha, a1, b1, c1, d1, e1, f1, currentMatrix, roundPixels)
161175
{
162176
this.renderer.setPipeline(this);
163177

@@ -166,6 +180,8 @@ var FlatTintPipeline = new Class({
166180
this.flush();
167181
}
168182

183+
var renderer = this.renderer;
184+
var resolution = renderer.gameConfig.resolution;
169185
var vertexViewF32 = this.vertexViewF32;
170186
var vertexViewU32 = this.vertexViewU32;
171187
var vertexOffset = this.vertexCount * this.vertexComponentCount;
@@ -189,6 +205,16 @@ var FlatTintPipeline = new Class({
189205
var ty2 = x2 * b + y2 * d + f;
190206
var tint = Utils.getTintAppendFloatAlphaAndSwap(fillColor, fillAlpha);
191207

208+
if (roundPixels)
209+
{
210+
tx0 = ((tx0 * resolution)|0) / resolution;
211+
ty0 = ((ty0 * resolution)|0) / resolution;
212+
tx1 = ((tx1 * resolution)|0) / resolution;
213+
ty1 = ((ty1 * resolution)|0) / resolution;
214+
tx2 = ((tx2 * resolution)|0) / resolution;
215+
ty2 = ((ty2 * resolution)|0) / resolution;
216+
}
217+
192218
vertexViewF32[vertexOffset + 0] = tx0;
193219
vertexViewF32[vertexOffset + 1] = ty0;
194220
vertexViewU32[vertexOffset + 2] = tint;
@@ -202,7 +228,7 @@ var FlatTintPipeline = new Class({
202228
this.vertexCount += 3;
203229
},
204230

205-
batchStrokeTriangle: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, x0, y0, x1, y1, x2, y2, lineWidth, lineColor, lineAlpha, a, b, c, d, e, f, currentMatrix)
231+
batchStrokeTriangle: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, x0, y0, x1, y1, x2, y2, lineWidth, lineColor, lineAlpha, a, b, c, d, e, f, currentMatrix, roundPixels)
206232
{
207233

208234
var tempTriangle = this.tempTriangle;
@@ -233,14 +259,17 @@ var FlatTintPipeline = new Class({
233259
tempTriangle, lineWidth, lineColor, lineAlpha,
234260
a, b, c, d, e, f,
235261
false,
236-
currentMatrix
262+
currentMatrix,
263+
roundPixels
237264
);
238265
},
239266

240-
batchFillPath: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, path, fillColor, fillAlpha, a1, b1, c1, d1, e1, f1, currentMatrix)
267+
batchFillPath: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, path, fillColor, fillAlpha, a1, b1, c1, d1, e1, f1, currentMatrix, roundPixels)
241268
{
242269
this.renderer.setPipeline(this);
243270

271+
var renderer = this.renderer;
272+
var resolution = renderer.gameConfig.resolution;
244273
var length = path.length;
245274
var polygonCache = this.polygonCache;
246275
var polygonIndexArray;
@@ -301,6 +330,16 @@ var FlatTintPipeline = new Class({
301330
tx2 = x2 * a + y2 * c + e;
302331
ty2 = x2 * b + y2 * d + f;
303332

333+
if (roundPixels)
334+
{
335+
tx0 = ((tx0 * resolution)|0) / resolution;
336+
ty0 = ((ty0 * resolution)|0) / resolution;
337+
tx1 = ((tx1 * resolution)|0) / resolution;
338+
ty1 = ((ty1 * resolution)|0) / resolution;
339+
tx2 = ((tx2 * resolution)|0) / resolution;
340+
ty2 = ((ty2 * resolution)|0) / resolution;
341+
}
342+
304343
vertexViewF32[vertexOffset + 0] = tx0;
305344
vertexViewF32[vertexOffset + 1] = ty0;
306345
vertexViewU32[vertexOffset + 2] = tint;
@@ -317,7 +356,7 @@ var FlatTintPipeline = new Class({
317356
polygonCache.length = 0;
318357
},
319358

320-
batchStrokePath: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, path, lineWidth, lineColor, lineAlpha, a, b, c, d, e, f, isLastPath, currentMatrix)
359+
batchStrokePath: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, path, lineWidth, lineColor, lineAlpha, a, b, c, d, e, f, isLastPath, currentMatrix, roundPixels)
321360
{
322361
this.renderer.setPipeline(this);
323362

@@ -343,7 +382,8 @@ var FlatTintPipeline = new Class({
343382
point0.width / 2, point1.width / 2,
344383
point0.rgb, point1.rgb, lineAlpha,
345384
a, b, c, d, e, f,
346-
currentMatrix
385+
currentMatrix,
386+
roundPixels
347387
);
348388

349389
polylines.push(line);
@@ -387,15 +427,17 @@ var FlatTintPipeline = new Class({
387427

388428
},
389429

390-
batchLine: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, ax, ay, bx, by, aLineWidth, bLineWidth, aLineColor, bLineColor, lineAlpha, a1, b1, c1, d1, e1, f1, currentMatrix)
430+
batchLine: function (srcX, srcY, srcScaleX, srcScaleY, srcRotation, ax, ay, bx, by, aLineWidth, bLineWidth, aLineColor, bLineColor, lineAlpha, a1, b1, c1, d1, e1, f1, currentMatrix, roundPixels)
391431
{
392432
this.renderer.setPipeline(this);
393433

394434
if (this.vertexCount + 6 > this.vertexCapacity)
395435
{
396436
this.flush();
397437
}
398-
438+
439+
var renderer = this.renderer;
440+
var resolution = renderer.gameConfig.resolution;
399441
var a0 = currentMatrix[0];
400442
var b0 = currentMatrix[1];
401443
var c0 = currentMatrix[2];
@@ -438,6 +480,18 @@ var FlatTintPipeline = new Class({
438480
var bTint = getTint(bLineColor, lineAlpha);
439481
var vertexOffset = this.vertexCount * this.vertexComponentCount;
440482

483+
if (roundPixels)
484+
{
485+
x0 = ((x0 * resolution)|0) / resolution;
486+
y0 = ((y0 * resolution)|0) / resolution;
487+
x1 = ((x1 * resolution)|0) / resolution;
488+
y1 = ((y1 * resolution)|0) / resolution;
489+
x2 = ((x2 * resolution)|0) / resolution;
490+
y2 = ((y2 * resolution)|0) / resolution;
491+
x3 = ((x3 * resolution)|0) / resolution;
492+
y3 = ((y3 * resolution)|0) / resolution;
493+
}
494+
441495
vertexViewF32[vertexOffset + 0] = x0;
442496
vertexViewF32[vertexOffset + 1] = y0;
443497
vertexViewU32[vertexOffset + 2] = bTint;
@@ -522,6 +576,7 @@ var FlatTintPipeline = new Class({
522576
var mvd = src * cmb + srd * cmd;
523577
var mve = sre * cma + srf * cmc + cme;
524578
var mvf = sre * cmb + srf * cmd + cmf;
579+
var roundPixels = camera.roundPixels;
525580

526581
pathArray.length = 0;
527582

@@ -610,7 +665,8 @@ var FlatTintPipeline = new Class({
610665
fillAlpha,
611666
/* Transform */
612667
mva, mvb, mvc, mvd, mve, mvf,
613-
currentMatrix
668+
currentMatrix,
669+
roundPixels
614670
);
615671
}
616672
break;
@@ -632,7 +688,8 @@ var FlatTintPipeline = new Class({
632688
/* Transform */
633689
mva, mvb, mvc, mvd, mve, mvf,
634690
path === this._lastPath,
635-
currentMatrix
691+
currentMatrix,
692+
roundPixels
636693
);
637694
}
638695
break;
@@ -650,7 +707,8 @@ var FlatTintPipeline = new Class({
650707
fillAlpha,
651708
/* Transform */
652709
mva, mvb, mvc, mvd, mve, mvf,
653-
currentMatrix
710+
currentMatrix,
711+
roundPixels
654712
);
655713

656714
cmdIndex += 4;
@@ -671,7 +729,8 @@ var FlatTintPipeline = new Class({
671729
fillAlpha,
672730
/* Transform */
673731
mva, mvb, mvc, mvd, mve, mvf,
674-
currentMatrix
732+
currentMatrix,
733+
roundPixels
675734
);
676735

677736
cmdIndex += 6;
@@ -693,7 +752,8 @@ var FlatTintPipeline = new Class({
693752
lineAlpha,
694753
/* Transform */
695754
mva, mvb, mvc, mvd, mve, mvf,
696-
currentMatrix
755+
currentMatrix,
756+
roundPixels
697757
);
698758

699759
cmdIndex += 6;

0 commit comments

Comments
 (0)