< Summary

Class:TextureHelpers
Assembly:TextureHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/TextureHelpers/TextureHelpers.cs
Covered lines:18
Uncovered lines:19
Coverable lines:37
Total lines:75
Line coverage:48.6% (18 of 37)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EnsureTexture2DMaxSize(...)0%42600%
Resize(...)0%2.012088.24%
CopyTexture(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/TextureHelpers/TextureHelpers.cs

#LineLine coverage
 1using UnityEngine;
 2using UnityEngine.Rendering;
 3
 4public static class TextureHelpers
 5{
 6    public static void EnsureTexture2DMaxSize(ref Texture2D texture, int maxTextureSize)
 7    {
 08        if (texture == null)
 09            return;
 10
 011        if (texture.width == 0 || texture.height == 0)
 012            return;
 13
 014        if (Mathf.Max(texture.height, texture.width) <= maxTextureSize)
 015            return;
 16
 17        int w, h;
 018        if (texture.height > texture.width)
 19        {
 020            h = maxTextureSize;
 021            w = (int) ((texture.width / (float) texture.height) * h);
 022        }
 23        else
 24        {
 025            w = maxTextureSize;
 026            h = (int) ((texture.height / (float) texture.width) * w);
 27        }
 28
 029        var newTexture = Resize(texture, w, h);
 030        var oldTexture = texture;
 031        texture = newTexture;
 032        Object.Destroy(oldTexture);
 033    }
 34
 35    public static Texture2D Resize(Texture2D source, int newWidth, int newHeight, bool linear = false)
 36    {
 37        // RenderTexture default format is ARGB32
 12938        Texture2D nTex = new Texture2D(newWidth, newHeight, TextureFormat.ARGB32, 1, linear);
 12939        nTex.filterMode = source.filterMode;
 12940        nTex.wrapMode = source.wrapMode;
 41
 12942        RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
 12943        rt.filterMode = FilterMode.Point;
 12944        source.filterMode = FilterMode.Point;
 45
 12946        RenderTexture.active = rt;
 12947        Graphics.Blit(source, rt);
 48
 12949        bool supportsGPUTextureCopy = SystemInfo.copyTextureSupport != CopyTextureSupport.None;
 12950        if (supportsGPUTextureCopy)
 51        {
 12952            Graphics.CopyTexture(rt, nTex);
 12953        }
 54        else
 55        {
 056            nTex.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0);
 057            nTex.Apply();
 58        }
 59
 12960        RenderTexture.ReleaseTemporary(rt);
 12961        RenderTexture.active = null;
 62
 12963        return nTex;
 64    }
 65
 66    public static Texture2D CopyTexture(Texture2D sourceTexture)
 67    {
 168        Texture2D texture = new Texture2D(sourceTexture.width, sourceTexture.height, sourceTexture.format, false);
 69
 70        // Note: Surprisingly this works in WebGL here but it doesn't work in Resize()
 171        Graphics.CopyTexture(sourceTexture, texture);
 72
 173        return texture;
 74    }
 75}