Skip to content

Commit d3789a3

Browse files
committed
Revamped how blend modes are handled. Added ability to swap between blendFunc and blendFunSeparate.
1 parent 2160f7a commit d3789a3

3 files changed

Lines changed: 58 additions & 16 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '67f18150-77a1-11e7-b88a-2bfabd5dc987'
2+
build: '49cc5c90-77e7-11e7-8cb3-6774896fb46c'
33
};
44
module.exports = CHECKSUM;

v3/src/gameobjects/components/BlendMode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var BlendMode = {
1717
{
1818
value | 0;
1919

20-
if (value >= 0 && value <= 16)
20+
if (value >= 0)
2121
{
2222
this._blendMode = value;
2323
}

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,21 @@ var WebGLRenderer = new Class({
103103

104104
// Map Blend Modes
105105

106-
var add = [ gl.SRC_ALPHA, gl.DST_ALPHA ];
107-
var normal = [ gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA ];
108-
var multiply = [ gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA ];
109-
var screen = [ gl.SRC_ALPHA, gl.ONE ];
110-
111-
this.blendModes = [
112-
normal, add, multiply, screen, normal,
113-
normal, normal, normal, normal,
114-
normal, normal, normal, normal,
115-
normal, normal, normal, normal
116-
];
106+
this.blendModes = [];
107+
108+
for (var i = 0; i <= 16; i++)
109+
{
110+
this.blendModes.push({ func: [ gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA ], equation: gl.FUNC_ADD });
111+
}
112+
113+
// Add
114+
this.blendModes[1].func = [ gl.SRC_ALPHA, gl.DST_ALPHA ];
115+
116+
// Multiply
117+
this.blendModes[2].func = [ gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA ];
118+
119+
// Screen
120+
this.blendModes[3].func = [ gl.SRC_ALPHA, gl.ONE ];
117121

118122
this.blendMode = -1;
119123
this.extensions = gl.getSupportedExtensions();
@@ -129,6 +133,43 @@ var WebGLRenderer = new Class({
129133
this.resize(this.width, this.height);
130134
},
131135

136+
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/blendFunc
137+
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/blendFuncSeparate
138+
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/blendEquation
139+
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/blendEquationSeparate
140+
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/blendColor
141+
addBlendMode: function (func, equation)
142+
{
143+
var index = this.blendModes.push({ func: func, equation: equation });
144+
145+
return index - 1;
146+
},
147+
148+
updateBlendMode: function (index, func, equation)
149+
{
150+
if (this.blendModes[index])
151+
{
152+
this.blendModes[index].func = func;
153+
154+
if (equation)
155+
{
156+
this.blendModes[index].equation = equation;
157+
}
158+
}
159+
160+
return this;
161+
},
162+
163+
removeBlendMode: function (index)
164+
{
165+
if (index > 16 && this.blendModes[index])
166+
{
167+
this.blendModes.splice(index, 1);
168+
}
169+
170+
return this;
171+
},
172+
132173
createTexture: function (source, width, height)
133174
{
134175
width = source ? source.width : width;
@@ -358,9 +399,10 @@ var WebGLRenderer = new Class({
358399
renderer.flush();
359400
}
360401

361-
var blend = this.blendModes[newBlendMode];
402+
var blend = this.blendModes[newBlendMode].func;
362403

363404
gl.enable(gl.BLEND);
405+
gl.blendEquation(this.blendModes[newBlendMode].equation);
364406

365407
if (blend.length > 2)
366408
{
@@ -449,7 +491,6 @@ var WebGLRenderer = new Class({
449491
{
450492
var gl = this.gl;
451493
var renderer = this.currentRenderer;
452-
var blend = null;
453494

454495
if (this.blendMode !== newBlendMode)
455496
{
@@ -458,9 +499,10 @@ var WebGLRenderer = new Class({
458499
renderer.flush();
459500
}
460501

461-
blend = this.blendModes[newBlendMode];
502+
var blend = this.blendModes[newBlendMode].func;
462503

463504
gl.enable(gl.BLEND);
505+
gl.blendEquation(this.blendModes[newBlendMode].equation);
464506

465507
if (blend.length > 2)
466508
{

0 commit comments

Comments
 (0)