Limit textures probed by TextureManager.getPixel and getPixelAlpha.

This commit is contained in:
Ben Richards 2024-03-05 15:42:04 +13:00
parent 3c7bee4e08
commit 0a2bede95e
2 changed files with 19 additions and 2 deletions

View file

@ -14,6 +14,7 @@
# Bug Fixes # 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) * 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 ## Examples, Documentation, Beta Testing and TypeScript

View file

@ -1415,6 +1415,14 @@ var TextureManager = new Class({
if (textureFrame) 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) // Adjust for trim (if not trimmed x and y are just zero)
x -= textureFrame.x; x -= textureFrame.x;
y -= textureFrame.y; y -= textureFrame.y;
@ -1429,7 +1437,7 @@ var TextureManager = new Class({
var ctx = this._tempContext; var ctx = this._tempContext;
ctx.clearRect(0, 0, 1, 1); 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); var rgb = ctx.getImageData(0, 0, 1, 1);
@ -1461,6 +1469,14 @@ var TextureManager = new Class({
if (textureFrame) 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) // Adjust for trim (if not trimmed x and y are just zero)
x -= textureFrame.x; x -= textureFrame.x;
y -= textureFrame.y; y -= textureFrame.y;
@ -1475,7 +1491,7 @@ var TextureManager = new Class({
var ctx = this._tempContext; var ctx = this._tempContext;
ctx.clearRect(0, 0, 1, 1); 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); var rgb = ctx.getImageData(0, 0, 1, 1);