| | 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 | |
|
| | 7 | | using System.IO; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace 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) |
| 0 | 30 | | { |
| | 31 | | // Get a temporary render texture from the pool since its gonna be rapid. |
| 0 | 32 | | RenderTexture screenshotRenderTexture = RenderTexture.GetTemporary(width, height, depthBuffer); |
| | 33 | |
|
| | 34 | | // Cache a few previous things to restore after we are done |
| 0 | 35 | | RenderTexture previousTargetTexture = targetCamera.targetTexture; |
| 0 | 36 | | RenderTexture previousActiveTarget = RenderTexture.active; |
| | 37 | |
|
| | 38 | | // Tell the camera to render to the render texture. |
| 0 | 39 | | targetCamera.targetTexture = screenshotRenderTexture; |
| 0 | 40 | | targetCamera.Render(); |
| | 41 | |
|
| 0 | 42 | | Texture2D screenshotTexture = new Texture2D(width, height, TextureFormat.RGB24, false); |
| 0 | 43 | | RenderTexture.active = screenshotRenderTexture; |
| 0 | 44 | | screenshotTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0); |
| 0 | 45 | | screenshotTexture.Apply(); |
| | 46 | |
|
| | 47 | | // Release our render texture. |
| 0 | 48 | | RenderTexture.active = previousActiveTarget; |
| 0 | 49 | | targetCamera.targetTexture = previousTargetTexture; |
| 0 | 50 | | screenshotRenderTexture.Release(); |
| | 51 | |
|
| 0 | 52 | | return screenshotTexture; |
| 0 | 53 | | } |
| | 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 |
| 0 | 70 | | { |
| 0 | 71 | | Texture2D captureTexture = RenderToTexture(targetCamera, width, height, depthBuffer); |
| 0 | 72 | | if (captureTexture == null) |
| 0 | 73 | | { |
| 0 | 74 | | return false; |
| | 75 | | } |
| | 76 | |
|
| 0 | 77 | | File.WriteAllBytes(outputPath, captureTexture.EncodeToPNG()); |
| 0 | 78 | | return true; |
| 0 | 79 | | } |
| | 80 | | } |
| | 81 | | } |
| | 82 | | #endif // !UNITY_DOTSRUNTIME |