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