-
-
Notifications
You must be signed in to change notification settings - Fork 660
Fix renderer batcher API, setBlendMode PMA, and plugin registration #1332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1c13008
5a828f9
3d515d3
9a4a7e2
b485a09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -73,6 +73,12 @@ export default class Renderer { | |||||
| */ | ||||||
| this.depthTest = "sorting"; | ||||||
|
|
||||||
| /** | ||||||
| * The GPU renderer string (WebGL only, undefined for Canvas) | ||||||
| * @type {string|undefined} | ||||||
| */ | ||||||
| this.GPURenderer = undefined; | ||||||
|
|
||||||
| /** | ||||||
| * The Path2D instance used by the renderer to draw primitives | ||||||
| * @type {Path2D} | ||||||
|
|
@@ -240,6 +246,16 @@ export default class Renderer { | |||||
| return this.currentBlendMode; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * set the current blend mode. | ||||||
| * Subclasses (CanvasRenderer, WebGLRenderer) implement the actual GL/Canvas logic. | ||||||
| * @param {string} [mode="normal"] - blend mode | ||||||
| * @param {boolean} [premultipliedAlpha=true] - whether textures use premultiplied alpha (WebGL only) | ||||||
| */ | ||||||
| setBlendMode(mode = "normal") { | ||||||
|
||||||
| setBlendMode(mode = "normal") { | |
| setBlendMode(mode = "normal", premultipliedAlpha = true) { // premultipliedAlpha is unused in the base renderer |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -358,6 +358,8 @@ export default class WebGLRenderer extends Renderer { | |
| // flush the current batcher | ||
| this.currentBatcher.flush(); | ||
| } | ||
| // rebind the renderer's shared vertex buffer (custom batchers may have bound their own) | ||
| this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer); | ||
| this.currentBatcher = batcher; | ||
| this.currentBatcher.bind(); | ||
| } | ||
|
|
@@ -724,7 +726,7 @@ export default class WebGLRenderer extends Renderer { | |
| } | ||
|
|
||
| /** | ||
| * set a blend mode for the given context. <br> | ||
| * set the current blend mode for this renderer. <br> | ||
| * All renderers support: <br> | ||
| * - "normal" : draws new content on top of the existing content <br> | ||
| * <img src="../images/normal-blendmode.png" width="180"/> <br> | ||
|
|
@@ -744,15 +746,19 @@ export default class WebGLRenderer extends Renderer { | |
| * and will always fall back to "normal" in WebGL. <br> | ||
| * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation | ||
| * @param {string} [mode="normal"] - blend mode | ||
| * @param {WebGLRenderingContext} [gl] - a WebGL context | ||
| * @param {boolean} [premultipliedAlpha=true] - whether textures use premultiplied alpha (affects source blend factor) | ||
| * @returns {string} the blend mode actually applied (may differ if the requested mode is unsupported) | ||
| */ | ||
| setBlendMode(mode = "normal", gl = this.gl) { | ||
| setBlendMode(mode = "normal", premultipliedAlpha = true) { | ||
| if (this.currentBlendMode !== mode) { | ||
| const gl = this.gl; | ||
| this.flush(); | ||
| gl.enable(gl.BLEND); | ||
| this.currentBlendMode = mode; | ||
|
|
||
| // source factor depends on whether textures use premultiplied alpha | ||
| const srcAlpha = premultipliedAlpha ? gl.ONE : gl.SRC_ALPHA; | ||
|
Comment on lines
+752
to
+760
|
||
|
|
||
| switch (mode) { | ||
| case "screen": | ||
| gl.blendEquation(gl.FUNC_ADD); | ||
|
|
@@ -763,7 +769,7 @@ export default class WebGLRenderer extends Renderer { | |
| case "additive": | ||
| case "add": | ||
| gl.blendEquation(gl.FUNC_ADD); | ||
| gl.blendFunc(gl.ONE, gl.ONE); | ||
| gl.blendFunc(srcAlpha, gl.ONE); | ||
| break; | ||
|
|
||
| case "multiply": | ||
|
|
@@ -777,7 +783,7 @@ export default class WebGLRenderer extends Renderer { | |
| gl.blendFunc(gl.ONE, gl.ONE); | ||
| } else { | ||
| gl.blendEquation(gl.FUNC_ADD); | ||
| gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | ||
| gl.blendFunc(srcAlpha, gl.ONE_MINUS_SRC_ALPHA); | ||
| this.currentBlendMode = "normal"; | ||
| } | ||
| break; | ||
|
|
@@ -788,14 +794,14 @@ export default class WebGLRenderer extends Renderer { | |
| gl.blendFunc(gl.ONE, gl.ONE); | ||
| } else { | ||
| gl.blendEquation(gl.FUNC_ADD); | ||
| gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | ||
| gl.blendFunc(srcAlpha, gl.ONE_MINUS_SRC_ALPHA); | ||
| this.currentBlendMode = "normal"; | ||
| } | ||
| break; | ||
|
|
||
| default: | ||
| gl.blendEquation(gl.FUNC_ADD); | ||
| gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | ||
| gl.blendFunc(srcAlpha, gl.ONE_MINUS_SRC_ALPHA); | ||
| this.currentBlendMode = "normal"; | ||
| break; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pluginNamederivation can still produce incorrect names for anonymous classes. For example,plugin.register(class extends BasePlugin {})yieldspluginClass.name === ""and the regex ontoString()will typically match "extends", causing collisions like "plugin extends already registered". Consider validating the derived name (and/or guarding against regex match failure) and throwing a clear error requiring an explicitnamewhen it can’t be reliably derived.