| | 1 | | using UnityEngine; |
| | 2 | | using UnityEngine.Rendering; |
| | 3 | |
|
| | 4 | | public static class TextureHelpers |
| | 5 | | { |
| | 6 | | public static Texture2D ClampSize(Texture2D source, int maxTextureSize, bool linear = false, bool useGPUCopy = true) |
| | 7 | | { |
| 282 | 8 | | if (source.width <= maxTextureSize && source.height <= maxTextureSize) |
| 139 | 9 | | return source; |
| | 10 | |
|
| 143 | 11 | | int width = source.width; |
| 143 | 12 | | int height = source.height; |
| | 13 | |
|
| 143 | 14 | | float factor = GetScalingFactor(width, height, maxTextureSize); |
| | 15 | |
|
| 143 | 16 | | Texture2D dstTex = Resize(source, (int) (width * factor), (int) (height * factor), linear, useGPUCopy); |
| | 17 | |
|
| 143 | 18 | | if (Application.isPlaying) |
| 143 | 19 | | Object.Destroy(source); |
| | 20 | | else |
| 0 | 21 | | Object.DestroyImmediate(source); |
| | 22 | |
|
| 0 | 23 | | return dstTex; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public static float GetScalingFactor(int width, int height, int maxTextureSize) |
| | 27 | | { |
| 0 | 28 | | if (width >= height) |
| | 29 | | { |
| 0 | 30 | | return (float)maxTextureSize / width; |
| | 31 | | } |
| 0 | 32 | | return (float)maxTextureSize / height; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public static Texture2D Resize(Texture2D source, int newWidth, int newHeight, bool linear = false, bool useGPUCopy = |
| | 36 | | { |
| 143 | 37 | | newWidth = Mathf.Max(1, newWidth); |
| 143 | 38 | | newHeight = Mathf.Max(1, newHeight); |
| | 39 | |
|
| | 40 | | // RenderTexture default format is ARGB32 |
| 143 | 41 | | Texture2D nTex = new Texture2D(newWidth, newHeight, TextureFormat.ARGB32, 1, linear); |
| 143 | 42 | | nTex.filterMode = source.filterMode; |
| 143 | 43 | | nTex.wrapMode = source.wrapMode; |
| | 44 | |
|
| 143 | 45 | | RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight); |
| 143 | 46 | | rt.filterMode = FilterMode.Point; |
| 143 | 47 | | source.filterMode = FilterMode.Point; |
| | 48 | |
|
| 143 | 49 | | RenderTexture.active = rt; |
| 143 | 50 | | Graphics.Blit(source, rt); |
| | 51 | |
|
| | 52 | | // GPU Texture copy doesn't work for the Asset Bundles Converter since Application.isPlaying is false |
| 143 | 53 | | bool supportsGPUTextureCopy = Application.isPlaying && SystemInfo.copyTextureSupport != CopyTextureSupport.None; |
| 143 | 54 | | if (supportsGPUTextureCopy && useGPUCopy) |
| | 55 | | { |
| 129 | 56 | | Graphics.CopyTexture(rt, nTex); |
| 129 | 57 | | } |
| | 58 | | else |
| | 59 | | { |
| 14 | 60 | | nTex.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0); |
| 14 | 61 | | nTex.Apply(); |
| | 62 | | } |
| | 63 | |
|
| 143 | 64 | | RenderTexture.ReleaseTemporary(rt); |
| 143 | 65 | | RenderTexture.active = null; |
| | 66 | |
|
| 143 | 67 | | return nTex; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public static Texture2D CopyTexture(Texture2D sourceTexture) |
| | 71 | | { |
| 17 | 72 | | Texture2D texture = new Texture2D(sourceTexture.width, sourceTexture.height, sourceTexture.format, false); |
| | 73 | |
|
| | 74 | | // Note: Surprisingly this works in WebGL here but it doesn't work in Resize() |
| 17 | 75 | | Graphics.CopyTexture(sourceTexture, texture); |
| | 76 | |
|
| 17 | 77 | | return texture; |
| | 78 | | } |
| | 79 | | } |