< Summary

Class:TextureHelpers
Assembly:TextureHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/TextureHelpers/TextureHelpers.cs
Covered lines:14
Uncovered lines:17
Coverable lines:31
Total lines:60
Line coverage:45.1% (14 of 31)
Covered branches:0
Total branches:0

Metrics

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

File(s)

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

#LineLine coverage
 1using UnityEngine;
 2
 3public static class TextureHelpers
 4{
 5    public static void EnsureTexture2DMaxSize(ref Texture2D texture, int maxTextureSize)
 6    {
 07        if (texture == null)
 08            return;
 9
 010        if (texture.width == 0 || texture.height == 0)
 011            return;
 12
 013        if (Mathf.Max(texture.height, texture.width) <= maxTextureSize)
 014            return;
 15
 16        int w, h;
 017        if (texture.height > texture.width)
 18        {
 019            h = maxTextureSize;
 020            w = (int) ((texture.width / (float) texture.height) * h);
 021        }
 22        else
 23        {
 024            w = maxTextureSize;
 025            h = (int) ((texture.height / (float) texture.width) * w);
 26        }
 27
 028        var newTexture = Resize(texture, w, h);
 029        var oldTexture = texture;
 030        texture = newTexture;
 031        Object.Destroy(oldTexture);
 032    }
 33
 34    public static Texture2D Resize(Texture2D source, int newWidth, int newHeight)
 35    {
 8936        source.filterMode = FilterMode.Point;
 37
 8938        RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
 8939        rt.filterMode = FilterMode.Point;
 40
 8941        RenderTexture.active = rt;
 8942        Graphics.Blit(source, rt);
 43
 8944        Texture2D nTex = new Texture2D(newWidth, newHeight);
 8945        nTex.ReadPixels(new Rect(0, 0, newWidth, newWidth), 0, 0);
 8946        nTex.Apply();
 47
 8948        RenderTexture.ReleaseTemporary(rt);
 8949        RenderTexture.active = null;
 50
 8951        return nTex;
 52    }
 53
 54    public static Texture2D CopyTexture(Texture2D sourceTexture)
 55    {
 156        Texture2D texture = new Texture2D(sourceTexture.width, sourceTexture.height, sourceTexture.format, false);
 157        Graphics.CopyTexture(sourceTexture, texture); // TODO: does this work in WebGL?
 158        return texture;
 59    }
 60}