├── README.md ├── ResizeTool.cs └── img └── example.gif /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Why This Script Was Created 3 | #### This class allows you to resize an image on the GPU. 4 | ## Speeds 5 | 6 | |Methods |Speeds |Source | 7 | |:--|--:|--:| 8 | |`ResizeTool.Resize(..)`|**00:00:40.89**|This Tool **[FREE]**| 9 | |`UnityEngine.Texture2D.Resize(..)`|**01:08:08.55**|[Unity Method](https://docs.unity3d.com/ScriptReference/Texture2D.Resize.html)| 10 | |`ResizePro.Resize(..)`| **00:00:40.28**|ResizePro [\[PAID ASSET\]](https://assetstore.unity.com/packages/tools/utilities/resize-pro-61344)| 11 | 12 | ![progress bars of ResizeTool vs Texture2D.Resize](img/example.gif) 13 | 14 | ### Here is the process: 15 | 16 | 1. `RenderTexture rt = RenderTexture.GetTemporary()` create a temporary render texture with the target size 17 | 2. `RenderTexture.active` set the active [RenderTexture](https://docs.unity3d.com/ScriptReference/RenderTexture.html) to the temporary texture so we can read from it 18 | 3. `Graphics.Blit()` Copies source texture into destination render texture with a shader _(on the gpu)_ [more info](https://docs.unity3d.com/ScriptReference/Graphics.Blit.html) 19 | 4. `texture2D.Resize()` resize the texture to the target values _(this sets the pixel data as undefined)_ [more info](https://docs.unity3d.com/ScriptReference/Texture2D.Resize.html) 20 | 5. ` texture2D.ReadPixels()` reads the pixel values from the temporary [RenderTexture](https://docs.unity3d.com/ScriptReference/RenderTexture.html) onto the resized texture [more info](https://docs.unity3d.com/ScriptReference/Texture2D.ReadPixels.html) 21 | 6. ` texture2D.Apply()` actually upload the changed pixels to the graphics card [more info](https://docs.unity3d.com/ScriptReference/Texture2D.Apply.html) 22 | -------------------------------------------------------------------------------- /ResizeTool.cs: -------------------------------------------------------------------------------- 1 | /* LICENSE 2 | Copyright (c) 2019 Adrian Babilinski 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | */ 22 | 23 | // Texture2D.Resize - https://docs.unity3d.com/ScriptReference/Texture2D.Resize.html 24 | // Texture2D.Apply - https://docs.unity3d.com/ScriptReference/Texture2D.Apply.html 25 | // Texture2D.ReadPixels - https://docs.unity3d.com/ScriptReference/Texture2D.ReadPixels.html 26 | // Graphics.Blit - https://docs.unity3d.com/ScriptReference/Graphics.Blit.html 27 | 28 | /* This class allows you to resize an image on the GPU. 29 | Resizing an image from 1024px to 8196px 100 times took this method: 00:00:40.8884790 30 | Resizing an image from 1024px to 8196px 100 times with Unity.Texture2D.Resize() took: 01:08:08.55 31 | */ 32 | public class ResizeTool 33 | { 34 | 35 | 36 | public static void Resize(Texture2D texture2D, int targetX, int targetY, bool mipmap =true, FilterMode filter = FilterMode.Bilinear) 37 | { 38 | //create a temporary RenderTexture with the target size 39 | RenderTexture rt = RenderTexture.GetTemporary(targetX, targetY, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default); 40 | 41 | //set the active RenderTexture to the temporary texture so we can read from it 42 | RenderTexture.active = rt; 43 | 44 | //Copy the texture data on the GPU - this is where the magic happens [(;] 45 | Graphics.Blit(texture2D, rt); 46 | //resize the texture to the target values (this sets the pixel data as undefined) 47 | texture2D.Resize(targetX, targetY, texture2D.format, mipmap); 48 | texture2D.filterMode = filter; 49 | 50 | try 51 | { 52 | //reads the pixel values from the temporary RenderTexture onto the resized texture 53 | texture2D.ReadPixels(new Rect(0.0f, 0.0f, targetX, targetY), 0, 0); 54 | //actually upload the changed pixels to the graphics card 55 | texture2D.Apply(); 56 | } 57 | catch 58 | { 59 | Debug.LogError("Read/Write is not enabled on texture "+ texture2D.name); 60 | } 61 | 62 | 63 | RenderTexture.ReleaseTemporary(rt); 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /img/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ababilinski/unity-gpu-texture-resize/e31f3f1e8161f16a1ef8e147422807a6a0a53df4/img/example.gif --------------------------------------------------------------------------------