From 0a2bede95e82a4e4966d71785beb8b86cfccf4f0 Mon Sep 17 00:00:00 2001 From: Ben Richards Date: Tue, 5 Mar 2024 15:42:04 +1300 Subject: [PATCH] Limit textures probed by `TextureManager.getPixel` and `getPixelAlpha`. --- changelog/3.90/CHANGELOG-v3.90.md | 1 + src/textures/TextureManager.js | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/changelog/3.90/CHANGELOG-v3.90.md b/changelog/3.90/CHANGELOG-v3.90.md index bbee14f32..52b9cfea1 100644 --- a/changelog/3.90/CHANGELOG-v3.90.md +++ b/changelog/3.90/CHANGELOG-v3.90.md @@ -14,6 +14,7 @@ # Bug Fixes * Fix `LightPipeline` failing to render the same normal map twice in a row in a single batch, by removing `setTexture2D`. Fix #6708 (thanks @sroboubi) +* Log a warning instead of throwing an exception when calling `TextureManager.getPixel()` or `getPixelAlpha()` on textures that cannot be drawn to a canvas for sampling. Fix #6761 (thanks @yaustar) ## Examples, Documentation, Beta Testing and TypeScript diff --git a/src/textures/TextureManager.js b/src/textures/TextureManager.js index 3cb043c8b..3ceabb61b 100644 --- a/src/textures/TextureManager.js +++ b/src/textures/TextureManager.js @@ -1415,6 +1415,14 @@ var TextureManager = new Class({ if (textureFrame) { + var source = textureFrame.source; + var image = source.image; + if (!(source.isCanvas || source.isVideo || image instanceof HTMLImageElement)) + { + console.warn('TextureManager.getPixelAlpha: The texture source must be Image, Canvas, or Video'); + return null; + } + // Adjust for trim (if not trimmed x and y are just zero) x -= textureFrame.x; y -= textureFrame.y; @@ -1429,7 +1437,7 @@ var TextureManager = new Class({ var ctx = this._tempContext; ctx.clearRect(0, 0, 1, 1); - ctx.drawImage(textureFrame.source.image, x, y, 1, 1, 0, 0, 1, 1); + ctx.drawImage(image, x, y, 1, 1, 0, 0, 1, 1); var rgb = ctx.getImageData(0, 0, 1, 1); @@ -1461,6 +1469,14 @@ var TextureManager = new Class({ if (textureFrame) { + var source = textureFrame.source; + var image = source.image; + if (!(source.isCanvas || source.isVideo || image instanceof HTMLImageElement)) + { + console.warn('TextureManager.getPixelAlpha: The texture source must be Image, Canvas, or Video'); + return null; + } + // Adjust for trim (if not trimmed x and y are just zero) x -= textureFrame.x; y -= textureFrame.y; @@ -1475,7 +1491,7 @@ var TextureManager = new Class({ var ctx = this._tempContext; ctx.clearRect(0, 0, 1, 1); - ctx.drawImage(textureFrame.source.image, x, y, 1, 1, 0, 0, 1, 1); + ctx.drawImage(image, x, y, 1, 1, 0, 0, 1, 1); var rgb = ctx.getImageData(0, 0, 1, 1);