< Summary

Class:TextureHelpers
Assembly:TextureHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/TextureHelpers/TextureHelpers.cs
Covered lines:30
Uncovered lines:5
Coverable lines:35
Total lines:79
Line coverage:85.7% (30 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ClampSize(...)0%4.134080%
GetScalingFactor(...)0%6200%
Resize(...)0%330100%
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 Texture2D ClampSize(Texture2D source, int maxTextureSize, bool linear = false, bool useGPUCopy = true)
 7    {
 2828        if (source.width <= maxTextureSize && source.height <= maxTextureSize)
 1399            return source;
 10
 14311        int width = source.width;
 14312        int height = source.height;
 13
 14314        float factor = GetScalingFactor(width, height, maxTextureSize);
 15
 14316        Texture2D dstTex = Resize(source, (int) (width * factor), (int) (height * factor), linear, useGPUCopy);
 17
 14318        if (Application.isPlaying)
 14319            Object.Destroy(source);
 20        else
 021            Object.DestroyImmediate(source);
 22
 023        return dstTex;
 24    }
 25
 26    public static float GetScalingFactor(int width, int height, int maxTextureSize)
 27    {
 028        if (width >= height)
 29        {
 030            return (float)maxTextureSize / width;
 31        }
 032        return (float)maxTextureSize / height;
 33    }
 34
 35    public static Texture2D Resize(Texture2D source, int newWidth, int newHeight, bool linear = false, bool useGPUCopy =
 36    {
 14337        newWidth = Mathf.Max(1, newWidth);
 14338        newHeight = Mathf.Max(1, newHeight);
 39
 40        // RenderTexture default format is ARGB32
 14341        Texture2D nTex = new Texture2D(newWidth, newHeight, TextureFormat.ARGB32, 1, linear);
 14342        nTex.filterMode = source.filterMode;
 14343        nTex.wrapMode = source.wrapMode;
 44
 14345        RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
 14346        rt.filterMode = FilterMode.Point;
 14347        source.filterMode = FilterMode.Point;
 48
 14349        RenderTexture.active = rt;
 14350        Graphics.Blit(source, rt);
 51
 52        // GPU Texture copy doesn't work for the Asset Bundles Converter since Application.isPlaying is false
 14353        bool supportsGPUTextureCopy = Application.isPlaying && SystemInfo.copyTextureSupport != CopyTextureSupport.None;
 14354        if (supportsGPUTextureCopy && useGPUCopy)
 55        {
 12956            Graphics.CopyTexture(rt, nTex);
 12957        }
 58        else
 59        {
 1460            nTex.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0);
 1461            nTex.Apply();
 62        }
 63
 14364        RenderTexture.ReleaseTemporary(rt);
 14365        RenderTexture.active = null;
 66
 14367        return nTex;
 68    }
 69
 70    public static Texture2D CopyTexture(Texture2D sourceTexture)
 71    {
 1772        Texture2D texture = new Texture2D(sourceTexture.width, sourceTexture.height, sourceTexture.format, false);
 73
 74        // Note: Surprisingly this works in WebGL here but it doesn't work in Resize()
 1775        Graphics.CopyTexture(sourceTexture, texture);
 76
 1777        return texture;
 78    }
 79}