From 8edf2a261f32fd19c01386d644aecdc8acef1be1 Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Wed, 20 Sep 2023 17:26:58 +0200 Subject: [PATCH] feat(Texture): restrict maximum size of textures to 128 pixels in normal variant --- readme.md | 2 +- src/Texture.ts | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index a47fbe90..99623608 100644 --- a/readme.md +++ b/readme.md @@ -34,7 +34,7 @@ Since `1.4.0` the `Settings` class has a `variant` field with 2 values: `"normal In normal mode: - jpg textures will be exported with 70% quality as opposed to 100% -- all textures except the icons (ending in `[icon].bmp`) will be resized to half the original resolution +- all textures except icons (ending in `[icon].bmp`) will be resized to have a maximum width and height of 128 pixels. ## Other docs diff --git a/src/Texture.ts b/src/Texture.ts index 461b3f5c..e06bd895 100644 --- a/src/Texture.ts +++ b/src/Texture.ts @@ -156,8 +156,26 @@ export class Texture extends ThreeJsTextue { let quality = 100 if (settings.variant !== 'premium' && !this.filename.endsWith('[icon].bmp')) { - image.resize(Math.floor(this._width / 2), Math.floor(this._height / 2), { fit: 'cover' }) quality = 70 + + let maxWidth = 128 + let maxHeight = 128 + + if (this._width > maxWidth || this._height > maxHeight) { + const isPortrait = this._width < this._height + const isLandscape = this._width > this._height + + if (isPortrait) { + maxWidth = (maxWidth / this._width) * this._height + } else if (isLandscape) { + maxHeight = (maxHeight / this._height) * this._width + } + + const newWidth = Math.max(maxWidth, this._width) + const newHeight = Math.max(maxHeight, this._height) + + image.resize(newWidth, newHeight, { fit: 'cover' }) + } } if (isBMP) { @@ -196,7 +214,7 @@ export class Texture extends ThreeJsTextue { let newSize = powerOfTwo let quality = 100 if (settings.variant !== 'premium' && !this.filename.endsWith('[icon].bmp')) { - newSize = newSize / 2 + newSize = Math.max(128, newSize) quality = 70 }