| | 1 | | using UnityEngine; |
| | 2 | | using UnityEngine.Rendering; |
| | 3 | |
|
| | 4 | | public static class TextureHelpers |
| | 5 | | { |
| | 6 | | public static void EnsureTexture2DMaxSize(ref Texture2D texture, int maxTextureSize) |
| | 7 | | { |
| 0 | 8 | | if (texture == null) |
| 0 | 9 | | return; |
| | 10 | |
|
| 0 | 11 | | if (texture.width == 0 || texture.height == 0) |
| 0 | 12 | | return; |
| | 13 | |
|
| 0 | 14 | | if (Mathf.Max(texture.height, texture.width) <= maxTextureSize) |
| 0 | 15 | | return; |
| | 16 | |
|
| | 17 | | int w, h; |
| 0 | 18 | | if (texture.height > texture.width) |
| | 19 | | { |
| 0 | 20 | | h = maxTextureSize; |
| 0 | 21 | | w = (int) ((texture.width / (float) texture.height) * h); |
| 0 | 22 | | } |
| | 23 | | else |
| | 24 | | { |
| 0 | 25 | | w = maxTextureSize; |
| 0 | 26 | | h = (int) ((texture.height / (float) texture.width) * w); |
| | 27 | | } |
| | 28 | |
|
| 0 | 29 | | var newTexture = Resize(texture, w, h); |
| 0 | 30 | | var oldTexture = texture; |
| 0 | 31 | | texture = newTexture; |
| 0 | 32 | | Object.Destroy(oldTexture); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public static Texture2D Resize(Texture2D source, int newWidth, int newHeight, bool linear = false) |
| | 36 | | { |
| | 37 | | // RenderTexture default format is ARGB32 |
| 128 | 38 | | Texture2D nTex = new Texture2D(newWidth, newHeight, TextureFormat.ARGB32, 1, linear); |
| 128 | 39 | | nTex.filterMode = source.filterMode; |
| 128 | 40 | | nTex.wrapMode = source.wrapMode; |
| | 41 | |
|
| 128 | 42 | | RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight); |
| 128 | 43 | | rt.filterMode = FilterMode.Point; |
| 128 | 44 | | source.filterMode = FilterMode.Point; |
| | 45 | |
|
| 128 | 46 | | RenderTexture.active = rt; |
| 128 | 47 | | Graphics.Blit(source, rt); |
| | 48 | |
|
| 128 | 49 | | bool supportsGPUTextureCopy = SystemInfo.copyTextureSupport != CopyTextureSupport.None; |
| 128 | 50 | | if (supportsGPUTextureCopy) |
| | 51 | | { |
| 128 | 52 | | Graphics.CopyTexture(rt, nTex); |
| 128 | 53 | | } |
| | 54 | | else |
| | 55 | | { |
| 0 | 56 | | nTex.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0); |
| 0 | 57 | | nTex.Apply(); |
| | 58 | | } |
| | 59 | |
|
| 128 | 60 | | RenderTexture.ReleaseTemporary(rt); |
| 128 | 61 | | RenderTexture.active = null; |
| | 62 | |
|
| 128 | 63 | | return nTex; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public static Texture2D CopyTexture(Texture2D sourceTexture) |
| | 67 | | { |
| 1 | 68 | | Texture2D texture = new Texture2D(sourceTexture.width, sourceTexture.height, sourceTexture.format, false); |
| | 69 | |
|
| 1 | 70 | | bool supportsGPUTextureCopy = SystemInfo.copyTextureSupport != CopyTextureSupport.None; |
| 1 | 71 | | if (supportsGPUTextureCopy) |
| | 72 | | { |
| 1 | 73 | | Graphics.CopyTexture(sourceTexture, texture); |
| 1 | 74 | | } |
| | 75 | | else |
| | 76 | | { |
| 0 | 77 | | RenderTexture rt = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height); |
| 0 | 78 | | rt.filterMode = FilterMode.Point; |
| 0 | 79 | | FilterMode sourceFilterMode = sourceTexture.filterMode; |
| 0 | 80 | | sourceTexture.filterMode = FilterMode.Point; |
| | 81 | |
|
| 0 | 82 | | RenderTexture.active = rt; |
| 0 | 83 | | Graphics.Blit(sourceTexture, rt); |
| | 84 | |
|
| 0 | 85 | | texture.ReadPixels(new Rect(0, 0, sourceTexture.width, sourceTexture.height), 0, 0); |
| 0 | 86 | | texture.Apply(); |
| | 87 | |
|
| 0 | 88 | | RenderTexture.ReleaseTemporary(rt); |
| 0 | 89 | | RenderTexture.active = null; |
| 0 | 90 | | sourceTexture.filterMode = sourceFilterMode; |
| | 91 | | } |
| | 92 | |
|
| 1 | 93 | | return texture; |
| | 94 | | } |
| | 95 | | } |