< Summary

Class:GDX.CameraExtensions
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/CameraExtensions.cs
Covered lines:0
Uncovered lines:23
Coverable lines:23
Total lines:78
Line coverage:0% (0 of 23)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:2
Method coverage:0% (0 of 2)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RenderToTexture(...)0%2100%
RenderToPNG(...)0%6200%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/CameraExtensions.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5#if !UNITY_DOTSRUNTIME
 6
 7using UnityEngine;
 8
 9namespace GDX
 10{
 11    /// <summary>
 12    ///     <see cref="UnityEngine.Camera" /> Based Extension Methods
 13    /// </summary>
 14    /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception>
 15    [VisualScriptingCompatible(2)]
 16    public static class CameraExtensions
 17    {
 18        /// <summary>
 19        ///     Forces a <see cref="Camera"/> to render it's view into a texture.
 20        /// </summary>
 21        /// <param name="targetCamera">The target <see cref="Camera"/> to use.</param>
 22        /// <param name="width">The desired width of the rendered texture.</param>
 23        /// <param name="height">The desired height of the rendered texture.</param>
 24        /// <param name="depthBuffer">The desired depth of the rendered texture.</param>
 25        /// <remarks>This behaves differently then using <see cref="ScreenCapture"/>.</remarks>
 26        /// <returns>The rendered view.</returns>
 27        public static Texture2D RenderToTexture(this Camera targetCamera, int width = 1920, int height = 1080, int depth
 028        {
 29            // Get a temporary render texture from the pool since its gonna be rapid.
 030            RenderTexture screenshotRenderTexture = RenderTexture.GetTemporary(width, height, depthBuffer);
 31
 32            // Cache a few previous things to restore after we are done
 033            RenderTexture previousTargetTexture = targetCamera.targetTexture;
 034            RenderTexture previousActiveTarget = RenderTexture.active;
 35
 36            // Tell the camera to render to the render texture.
 037            targetCamera.targetTexture = screenshotRenderTexture;
 038            targetCamera.Render();
 39
 040            Texture2D screenshotTexture = new Texture2D(width, height, TextureFormat.RGB24, false);
 041            RenderTexture.active = screenshotRenderTexture;
 042            screenshotTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
 043            screenshotTexture.Apply();
 44
 45            // Release our render texture.
 046            RenderTexture.active = previousActiveTarget;
 047            targetCamera.targetTexture = previousTargetTexture;
 048            screenshotRenderTexture.Release();
 49
 050            return screenshotTexture;
 051        }
 52
 53        /// <summary>
 54        ///     Forces a <see cref="Camera"/> through <see cref="RenderToTexture"/> encoding to PNG.
 55        /// </summary>
 56        /// <param name="targetCamera">The target <see cref="Camera"/> to use.</param>
 57        /// <param name="outputPath">The full path to output the PNG bytes.</param>
 58        /// <param name="width">The desired width of the rendered texture.</param>
 59        /// <param name="height">The desired height of the rendered texture.</param>
 60        /// <param name="depthBuffer">The desired depth of the rendered texture.</param>
 61        /// <returns>true/false if the capture was successful.</returns>
 62        /// <remarks>This does not indicate if the writing of the PNG was successful.</remarks>
 63#pragma warning disable IDE1006
 64        // ReSharper disable once InconsistentNaming
 65        public static bool RenderToPNG(this Camera targetCamera, string outputPath, int width = 1920, int height = 1080,
 66#pragma warning restore IDE1006
 067        {
 068            Texture2D captureTexture = RenderToTexture(targetCamera, width, height, depthBuffer);
 069            if (captureTexture == null)
 070            {
 071                return false;
 72            }
 073            System.IO.File.WriteAllBytes(outputPath, captureTexture.EncodeToPNG());
 074            return true;
 075        }
 76    }
 77}
 78#endif