Skip to content

Commit b1e4905

Browse files
committed
Fixed alpha blending issue with textured renderer
1 parent 3a0c276 commit b1e4905

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ var WebGLRenderer = new Class({
8080
preserveDrawingBuffer: false,
8181

8282
WebGLContextOptions: {
83-
alpha: false,
83+
alpha: true,
8484
antialias: true,
85-
premultipliedAlpha: false,
85+
premultipliedAlpha: true,
8686
stencil: true,
8787
preserveDrawingBuffer: false
8888
}
@@ -143,9 +143,8 @@ var WebGLRenderer = new Class({
143143
gl.enable(gl.BLEND);
144144
gl.clearColor(color.redGL, color.greenGL, color.blueGL, color.alphaGL);
145145
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
146-
146+
147147
// Map Blend Modes
148-
149148
this.blendModes = [];
150149

151150
for (var i = 0; i <= 16; i++)
@@ -154,13 +153,13 @@ var WebGLRenderer = new Class({
154153
}
155154

156155
// Add
157-
this.blendModes[1].func = [ gl.SRC_ALPHA, gl.DST_ALPHA ];
156+
this.blendModes[1].func = [ gl.ONE, gl.DST_ALPHA ];
158157

159158
// Multiply
160159
this.blendModes[2].func = [ gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA ];
161160

162161
// Screen
163-
this.blendModes[3].func = [ gl.SRC_ALPHA, gl.ONE ];
162+
this.blendModes[3].func = [ gl.ONE, gl.ONE_MINUS_SRC_COLOR ];
164163

165164
this.blendMode = -1;
166165
this.extensions = gl.getSupportedExtensions();
@@ -550,12 +549,22 @@ var WebGLRenderer = new Class({
550549

551550
setBlendMode: function (newBlendMode)
552551
{
552+
var gl = this.gl;
553+
553554
if (newBlendMode === BlendModes.SKIP_CHECK)
554555
{
555556
return;
556557
}
557558

558-
var gl = this.gl;
559+
if (newBlendMode === BlendModes.NORMAL)
560+
{
561+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
562+
}
563+
else
564+
{
565+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
566+
}
567+
559568
var renderer = this.currentRenderer;
560569

561570
if (this.blendMode !== newBlendMode)

v3/src/renderer/webgl/shaders/TexturedAndAlphaShader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
'varying float v_alpha;',
2020
'void main() {',
2121
' vec4 output_color = texture2D(u_sampler2D, v_tex_coord);',
22-
' gl_FragColor = vec4(output_color.rgb * output_color.a, v_alpha * output_color.a);',
22+
' gl_FragColor = vec4(output_color.rgb * v_alpha * output_color.a, v_alpha * output_color.a);',
2323
'}'
2424
].join('\n')
2525
};

v3/src/renderer/webgl/shaders/TexturedAndNormalizedTintedShader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = {
2323
'varying float v_alpha;',
2424
'void main() {',
2525
' vec4 sample_color = texture2D(u_sampler2D, v_tex_coord);',
26-
' gl_FragColor = vec4((sample_color.rgb * v_color.rgb) * sample_color.a, sample_color.a * v_alpha);',
26+
' gl_FragColor = vec4(sample_color.rgb * v_alpha * sample_color.a, v_alpha * sample_color.a);',
2727
'}'
2828
].join('\n')
2929
};

0 commit comments

Comments
 (0)