Resize Textures with MSPaint Algorithm via Postprocess

Example (2048 -> 1024)

Unity Built-In (Unity自带) MSPaint Kernel(后处理加入画图锐化)
-

MSPaint provides better resize results as stated in Which interpolation algorithm does MS Paint on Windows 7 use for image rescaling?

Source: skenera’s answer

MSPaint uses bilinear interpolation then sharpens the image with a convolution kernel:

1
2
3
4
5
0.0, -0.125, 0.0

-0.125, 1.5, -0.125

0.0, -0.125, 0.0

For our current project we need to batch resize using this algorithm.

Implementation

AssetPostprocessor.OnPostprocessTexture(Texture2D)

The actual kernel used:

new float[3]{ 0.0f, -0.25f, 0.0f },
new float[3]{ -0.25f, 2f, -0.25f },
new float[3]{ 0.0f, -0.25f, 0.0f }

ImageMagick

An alternative approach would be compressing source texture files with ImageMagick.
However it is only useful in some cases and is not recommended, compare to postprocess approach.

ImageMagick Command:

1
magick image.png -scale 512 -morphology Convolve "3x3: 0.0, -0.125, 0.0 -0.125, 1.5, -0.125 0.0, -0.125, 0.0" image_resized.png

Download ImageMagick

分享