< Summary

Class:TextureHelpers
Assembly:TextureHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/TextureHelpers/TextureHelpers.cs
Covered lines:21
Uncovered lines:30
Coverable lines:51
Total lines:95
Line coverage:41.1% (21 of 51)
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%3.082035.29%

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
 12838        Texture2D nTex = new Texture2D(newWidth, newHeight, TextureFormat.ARGB32, 1, linear);
 12839        nTex.filterMode = source.filterMode;
 12840        nTex.wrapMode = source.wrapMode;
 41
 12842        RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
 12843        rt.filterMode = FilterMode.Point;
 12844        source.filterMode = FilterMode.Point;
 45
 12846        RenderTexture.active = rt;
 12847        Graphics.Blit(source, rt);
 48
 12849        bool supportsGPUTextureCopy = SystemInfo.copyTextureSupport != CopyTextureSupport.None;
 12850        if (supportsGPUTextureCopy)
 51        {
 12852            Graphics.CopyTexture(rt, nTex);
 12853        }
 54        else
 55        {
 056            nTex.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0);
 057            nTex.Apply();
 58        }
 59
 12860        RenderTexture.ReleaseTemporary(rt);
 12861        RenderTexture.active = null;
 62
 12863        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
 170        bool supportsGPUTextureCopy = SystemInfo.copyTextureSupport != CopyTextureSupport.None;
 171        if (supportsGPUTextureCopy)
 72        {
 173            Graphics.CopyTexture(sourceTexture, texture);
 174        }
 75        else
 76        {
 077            RenderTexture rt = RenderTexture.GetTemporary(sourceTexture.width, sourceTexture.height);
 078            rt.filterMode = FilterMode.Point;
 079            FilterMode sourceFilterMode = sourceTexture.filterMode;
 080            sourceTexture.filterMode = FilterMode.Point;
 81
 082            RenderTexture.active = rt;
 083            Graphics.Blit(sourceTexture, rt);
 84
 085            texture.ReadPixels(new Rect(0, 0, sourceTexture.width, sourceTexture.height), 0, 0);
 086            texture.Apply();
 87
 088            RenderTexture.ReleaseTemporary(rt);
 089            RenderTexture.active = null;
 090            sourceTexture.filterMode = sourceFilterMode;
 91        }
 92
 193        return texture;
 94    }
 95}