< Summary

Class:GDX.CameraExtensions
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/CameraExtensions.cs
Covered lines:0
Uncovered lines:23
Coverable lines:23
Total lines:82
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)

./Packages/com.dotbunny.gdx/GDX/CameraExtensions.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 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 System.IO;
 8using UnityEngine;
 9
 10namespace GDX
 11{
 12    /// <summary>
 13    ///     <see cref="UnityEngine.Camera" /> Based Extension Methods
 14    /// </summary>
 15    /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception>
 16    [VisualScriptingCompatible(2)]
 17    public static class CameraExtensions
 18    {
 19        /// <summary>
 20        ///     Forces a <see cref="Camera" /> to render it's view into a texture.
 21        /// </summary>
 22        /// <param name="targetCamera">The target <see cref="Camera" /> to use.</param>
 23        /// <param name="width">The desired width of the rendered texture.</param>
 24        /// <param name="height">The desired height of the rendered texture.</param>
 25        /// <param name="depthBuffer">The desired depth of the rendered texture.</param>
 26        /// <remarks>This behaves differently then using <see cref="ScreenCapture" />.</remarks>
 27        /// <returns>The rendered view.</returns>
 28        public static Texture2D RenderToTexture(this Camera targetCamera, int width = 1920, int height = 1080,
 29            int depthBuffer = 24)
 030        {
 31            // Get a temporary render texture from the pool since its gonna be rapid.
 032            RenderTexture screenshotRenderTexture = RenderTexture.GetTemporary(width, height, depthBuffer);
 33
 34            // Cache a few previous things to restore after we are done
 035            RenderTexture previousTargetTexture = targetCamera.targetTexture;
 036            RenderTexture previousActiveTarget = RenderTexture.active;
 37
 38            // Tell the camera to render to the render texture.
 039            targetCamera.targetTexture = screenshotRenderTexture;
 040            targetCamera.Render();
 41
 042            Texture2D screenshotTexture = new Texture2D(width, height, TextureFormat.RGB24, false);
 043            RenderTexture.active = screenshotRenderTexture;
 044            screenshotTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
 045            screenshotTexture.Apply();
 46
 47            // Release our render texture.
 048            RenderTexture.active = previousActiveTarget;
 049            targetCamera.targetTexture = previousTargetTexture;
 050            screenshotRenderTexture.Release();
 51
 052            return screenshotTexture;
 053        }
 54
 55        /// <summary>
 56        ///     Forces a <see cref="Camera" /> through <see cref="RenderToTexture" /> encoding to PNG.
 57        /// </summary>
 58        /// <param name="targetCamera">The target <see cref="Camera" /> to use.</param>
 59        /// <param name="outputPath">The full path to output the PNG bytes.</param>
 60        /// <param name="width">The desired width of the rendered texture.</param>
 61        /// <param name="height">The desired height of the rendered texture.</param>
 62        /// <param name="depthBuffer">The desired depth of the rendered texture.</param>
 63        /// <returns>true/false if the capture was successful.</returns>
 64        /// <remarks>This does not indicate if the writing of the PNG was successful.</remarks>
 65#pragma warning disable IDE1006
 66        // ReSharper disable once InconsistentNaming
 67        public static bool RenderToPNG(this Camera targetCamera, string outputPath, int width = 1920, int height = 1080,
 68            int depthBuffer = 24)
 69#pragma warning restore IDE1006
 070        {
 071            Texture2D captureTexture = RenderToTexture(targetCamera, width, height, depthBuffer);
 072            if (captureTexture == null)
 073            {
 074                return false;
 75            }
 76
 077            File.WriteAllBytes(outputPath, captureTexture.EncodeToPNG());
 078            return true;
 079        }
 80    }
 81}
 82#endif // !UNITY_DOTSRUNTIME