< 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:81
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,
 7        bool linear = false, bool useGPUCopy = true)
 8    {
 2909        if (source.width <= maxTextureSize && source.height <= maxTextureSize)
 14710            return source;
 11
 14312        int width = source.width;
 14313        int height = source.height;
 14
 14315        float factor = GetScalingFactor(width, height, maxTextureSize);
 16
 14317        Texture2D dstTex = Resize(source, (int) (width * factor),
 18            (int) (height * factor), linear, useGPUCopy);
 19
 14320        if (Application.isPlaying)
 14321            Object.Destroy(source);
 22        else
 023            Object.DestroyImmediate(source);
 24
 025        return dstTex;
 26    }
 27
 28    public static float GetScalingFactor(int width, int height, int maxTextureSize)
 29    {
 030        if (width >= height)
 31        {
 032            return (float)maxTextureSize / width;
 33        }
 034        return (float)maxTextureSize / height;
 35    }
 36
 37    public static Texture2D Resize(Texture2D source, int newWidth, int newHeight, bool linear = false, bool useGPUCopy =
 38    {
 14339        newWidth = Mathf.Max(1, newWidth);
 14340        newHeight = Mathf.Max(1, newHeight);
 41
 42        // RenderTexture default format is ARGB32
 14343        Texture2D nTex = new Texture2D(newWidth, newHeight, TextureFormat.ARGB32, 1, linear);
 14344        nTex.filterMode = source.filterMode;
 14345        nTex.wrapMode = source.wrapMode;
 46
 14347        RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
 14348        rt.filterMode = FilterMode.Point;
 14349        source.filterMode = FilterMode.Point;
 50
 14351        RenderTexture.active = rt;
 14352        Graphics.Blit(source, rt);
 53
 54        // GPU Texture copy doesn't work for the Asset Bundles Converter since Application.isPlaying is false
 14355        bool supportsGPUTextureCopy = Application.isPlaying && SystemInfo.copyTextureSupport != CopyTextureSupport.None;
 14356        if (supportsGPUTextureCopy && useGPUCopy)
 57        {
 12958            Graphics.CopyTexture(rt, nTex);
 12959        }
 60        else
 61        {
 1462            nTex.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0);
 1463            nTex.Apply();
 64        }
 65
 14366        RenderTexture.ReleaseTemporary(rt);
 14367        RenderTexture.active = null;
 68
 14369        return nTex;
 70    }
 71
 72    public static Texture2D CopyTexture(Texture2D sourceTexture)
 73    {
 174        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()
 177        Graphics.CopyTexture(sourceTexture, texture);
 78
 179        return texture;
 80    }
 81}