Skip to content

Commit 1c13008

Browse files
obiotclaude
andcommitted
WebGLRenderer: rebind shared vertex buffer on batcher switch
setBatcher() now rebinds the renderer's shared vertex buffer before activating a new batcher. This allows custom batchers that use their own GL buffers (e.g. SpineBatcher) to integrate without manual cleanup. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5207b75 commit 1c13008

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

packages/melonjs/src/video/webgl/webgl_renderer.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ export default class WebGLRenderer extends Renderer {
358358
// flush the current batcher
359359
this.currentBatcher.flush();
360360
}
361+
// rebind the renderer's shared vertex buffer (custom batchers may have bound their own)
362+
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
361363
this.currentBatcher = batcher;
362364
this.currentBatcher.bind();
363365
}
@@ -744,15 +746,19 @@ export default class WebGLRenderer extends Renderer {
744746
* and will always fall back to "normal" in WebGL. <br>
745747
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
746748
* @param {string} [mode="normal"] - blend mode
747-
* @param {WebGLRenderingContext} [gl] - a WebGL context
749+
* @param {boolean} [premultipliedAlpha=true] - whether textures use premultiplied alpha (affects source blend factor)
748750
* @returns {string} the blend mode actually applied (may differ if the requested mode is unsupported)
749751
*/
750-
setBlendMode(mode = "normal", gl = this.gl) {
752+
setBlendMode(mode = "normal", premultipliedAlpha = true) {
751753
if (this.currentBlendMode !== mode) {
754+
const gl = this.gl;
752755
this.flush();
753756
gl.enable(gl.BLEND);
754757
this.currentBlendMode = mode;
755758

759+
// source factor depends on whether textures use premultiplied alpha
760+
const srcAlpha = premultipliedAlpha ? gl.ONE : gl.SRC_ALPHA;
761+
756762
switch (mode) {
757763
case "screen":
758764
gl.blendEquation(gl.FUNC_ADD);
@@ -763,7 +769,7 @@ export default class WebGLRenderer extends Renderer {
763769
case "additive":
764770
case "add":
765771
gl.blendEquation(gl.FUNC_ADD);
766-
gl.blendFunc(gl.ONE, gl.ONE);
772+
gl.blendFunc(srcAlpha, gl.ONE);
767773
break;
768774

769775
case "multiply":
@@ -777,7 +783,7 @@ export default class WebGLRenderer extends Renderer {
777783
gl.blendFunc(gl.ONE, gl.ONE);
778784
} else {
779785
gl.blendEquation(gl.FUNC_ADD);
780-
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
786+
gl.blendFunc(srcAlpha, gl.ONE_MINUS_SRC_ALPHA);
781787
this.currentBlendMode = "normal";
782788
}
783789
break;
@@ -788,14 +794,14 @@ export default class WebGLRenderer extends Renderer {
788794
gl.blendFunc(gl.ONE, gl.ONE);
789795
} else {
790796
gl.blendEquation(gl.FUNC_ADD);
791-
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
797+
gl.blendFunc(srcAlpha, gl.ONE_MINUS_SRC_ALPHA);
792798
this.currentBlendMode = "normal";
793799
}
794800
break;
795801

796802
default:
797803
gl.blendEquation(gl.FUNC_ADD);
798-
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
804+
gl.blendFunc(srcAlpha, gl.ONE_MINUS_SRC_ALPHA);
799805
this.currentBlendMode = "normal";
800806
break;
801807
}

0 commit comments

Comments
 (0)