Skip to content

Instantly share code, notes, and snippets.

@ababilinski
Last active March 17, 2021 15:52
Show Gist options
  • Save ababilinski/ff5c8ad7491537c5de63194683d617e6 to your computer and use it in GitHub Desktop.
Save ababilinski/ff5c8ad7491537c5de63194683d617e6 to your computer and use it in GitHub Desktop.
A method that allows for fast GPU resizing in the Unity Engine.
/* LICENSE
Copyright (c) 2019 Adrian Babilinski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* This class allows you to resize an image on the GPU.
Resizing an image from 1024px to 8196px 100 times took this method: 00:00:40.8884790
Resizing an image from 1024px to 8196px 100 times with Unity.Texture2D.Resize() took: 01:08:08.55
*/
public class ResizeTool
{
public static void Resize(Texture2D texture2D, int targetX, int targetY, bool mipmap =true, FilterMode filter = FilterMode.Bilinear)
{
RenderTexture rt = RenderTexture.GetTemporary(targetX, targetY, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
RenderTexture.active = rt;
Graphics.Blit(texture2D, rt);
texture2D.Resize(targetX, targetY, texture2D.format, mipmap);
texture2D.filterMode = filter;
try
{
texture2D.ReadPixels(new Rect(0.0f, 0.0f, targetX, targetY), 0, 0);
texture2D.Apply();
}
catch
{
Debug.LogError("Read/Write is not enabled on texture "+ texture2D.name);
}
RenderTexture.ReleaseTemporary(rt);
}
}
}
@ababilinski
Copy link
Author

Why This Script Was Created

This class allows you to resize an image on the GPU.

Speeds

Methods Speeds Source
ResizeTool.Resize(..) 00:00:40.89 This Tool [FREE]
UnityEngine.Texture2D.Resize(..) 01:08:08.55 Unity Method

progress bars of ResizeTool vs Texture2D.Resize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment