< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ClampSize(...)0%4.134080%
GetScalingFactor(...)0%2.152066.67%
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    {
 2749        if (source.width <= maxTextureSize && source.height <= maxTextureSize)
 13510            return source;
 11
 13912        int width = source.width;
 13913        int height = source.height;
 14
 13915        float factor = GetScalingFactor(width, height, maxTextureSize);
 16
 13917        Texture2D dstTex = Resize(source, (int) (width * factor),
 18            (int) (height * factor), linear, useGPUCopy);
 19
 13920        if (Application.isPlaying)
 13921            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    {
 20830        if (width >= height)
 31        {
 20832            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    {
 13939        newWidth = Mathf.Max(1, newWidth);
 13940        newHeight = Mathf.Max(1, newHeight);
 41
 42        // RenderTexture default format is ARGB32
 13943        Texture2D nTex = new Texture2D(newWidth, newHeight, TextureFormat.ARGB32, 1, linear);
 13944        nTex.filterMode = source.filterMode;
 13945        nTex.wrapMode = source.wrapMode;
 46
 13947        RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
 13948        rt.filterMode = FilterMode.Point;
 13949        source.filterMode = FilterMode.Point;
 50
 13951        RenderTexture.active = rt;
 13952        Graphics.Blit(source, rt);
 53
 54        // GPU Texture copy doesn't work for the Asset Bundles Converter since Application.isPlaying is false
 13955        bool supportsGPUTextureCopy = Application.isPlaying && SystemInfo.copyTextureSupport != CopyTextureSupport.None;
 13956        if (supportsGPUTextureCopy && useGPUCopy)
 57        {
 12558            Graphics.CopyTexture(rt, nTex);
 59        }
 60        else
 61        {
 1462            nTex.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0);
 1463            nTex.Apply();
 64        }
 65
 13966        RenderTexture.ReleaseTemporary(rt);
 13967        RenderTexture.active = null;
 68
 13969        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}