| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | namespace DCLFeatures.ScreencaptureCamera.CameraObject |
| | 8 | | { |
| | 9 | | public class ScreenRecorder |
| | 10 | | { |
| | 11 | | private const int TARGET_FRAME_WIDTH = 1920; |
| | 12 | | private const int TARGET_FRAME_HEIGHT = 1080; |
| | 13 | | private const float FRAME_SCALE = 0.87f; // Defines the scale of the frame in relation to the screen |
| | 14 | |
|
| | 15 | | private readonly float targetAspectRatio; |
| | 16 | | private readonly RectTransform canvasRectTransform; |
| | 17 | |
|
| 0 | 18 | | private readonly Texture2D screenshot = new (TARGET_FRAME_WIDTH, TARGET_FRAME_HEIGHT, TextureFormat.RGB24, false |
| | 19 | |
|
| | 20 | | private RenderTexture originalBaseTargetTexture; |
| | 21 | |
|
| | 22 | | private ScreenFrameData debugTargetScreenFrame; |
| | 23 | |
|
| 0 | 24 | | public bool IsCapturing { get; private set; } |
| | 25 | |
|
| 0 | 26 | | public ScreenRecorder(RectTransform canvasRectTransform) |
| | 27 | | { |
| 0 | 28 | | targetAspectRatio = (float)TARGET_FRAME_WIDTH / TARGET_FRAME_HEIGHT; |
| 0 | 29 | | Debug.Assert(targetAspectRatio != 0, "Target aspect ratio cannot be null"); |
| | 30 | |
|
| 0 | 31 | | this.canvasRectTransform = canvasRectTransform; |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public virtual IEnumerator CaptureScreenshot(Action<Texture2D> onComplete) |
| | 35 | | { |
| 0 | 36 | | IsCapturing = true; |
| | 37 | |
|
| 0 | 38 | | yield return new WaitForEndOfFrame(); // for UI to appear on screenshot. Converting to UniTask didn't work : |
| | 39 | |
|
| 0 | 40 | | ScreenFrameData currentScreenFrame = CalculateCurrentScreenFrame(); |
| 0 | 41 | | (_, float targetRescale) = CalculateTargetScreenFrame(currentScreenFrame); |
| 0 | 42 | | int roundedUpscale = Mathf.CeilToInt(targetRescale); |
| | 43 | |
|
| | 44 | | // Hotfix for MacOS Desktop crashing on taking screenshot when Explore panel is open |
| 0 | 45 | | if (DataStore.i.exploreV2.isOpen.Get() && Application.platform == RuntimePlatform.OSXPlayer) |
| 0 | 46 | | roundedUpscale = 1; |
| | 47 | |
|
| 0 | 48 | | ScreenFrameData rescaledScreenFrame = CalculateRoundRescaledScreenFrame(currentScreenFrame, roundedUpscale); |
| | 49 | |
|
| 0 | 50 | | Texture2D screenshotTexture = ScreenCapture.CaptureScreenshotAsTexture(roundedUpscale); // upscaled Screen F |
| 0 | 51 | | Texture2D upscaledFrameTexture = CropTexture2D(screenshotTexture, rescaledScreenFrame.CalculateFrameCorners( |
| 0 | 52 | | ResizeTexture2D(upscaledFrameTexture); |
| | 53 | |
|
| 0 | 54 | | onComplete?.Invoke(screenshot); |
| | 55 | |
|
| 0 | 56 | | Object.Destroy(screenshotTexture); |
| 0 | 57 | | Object.Destroy(upscaledFrameTexture); |
| | 58 | |
|
| 0 | 59 | | IsCapturing = false; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | private static Texture2D CropTexture2D(Texture2D texture, Vector2Int startCorner, int width, int height) |
| | 63 | | { |
| 0 | 64 | | Color[] pixels = texture.GetPixels(startCorner.x, startCorner.y, width, height); |
| | 65 | |
|
| 0 | 66 | | var result = new Texture2D(width, height, TextureFormat.RGB24, false); |
| 0 | 67 | | result.SetPixels(pixels); |
| 0 | 68 | | result.Apply(); |
| | 69 | |
|
| 0 | 70 | | return result; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | private void ResizeTexture2D(Texture originalTexture) |
| | 74 | | { |
| 0 | 75 | | var renderTexture = RenderTexture.GetTemporary(TARGET_FRAME_WIDTH, TARGET_FRAME_HEIGHT, 0); |
| 0 | 76 | | RenderTexture.active = renderTexture; |
| | 77 | |
|
| | 78 | | // Copy and scale the original texture into the RenderTexture |
| 0 | 79 | | Graphics.Blit(originalTexture, renderTexture); |
| | 80 | |
|
| | 81 | | // Read the pixel data from the RenderTexture into the Texture2D |
| 0 | 82 | | screenshot.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); |
| 0 | 83 | | screenshot.Apply(); |
| | 84 | |
|
| 0 | 85 | | RenderTexture.active = null; |
| 0 | 86 | | RenderTexture.ReleaseTemporary(renderTexture); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private ScreenFrameData CalculateCurrentScreenFrame() |
| | 90 | | { |
| 0 | 91 | | var screenFrameData = new ScreenFrameData |
| | 92 | | { |
| | 93 | | ScreenWidth = canvasRectTransform.rect.width * canvasRectTransform.lossyScale.x, |
| | 94 | | ScreenHeight = canvasRectTransform.rect.height * canvasRectTransform.lossyScale.y, |
| | 95 | | }; |
| | 96 | |
|
| | 97 | | // Adjust current by smallest side |
| 0 | 98 | | if (screenFrameData.ScreenAspectRatio > targetAspectRatio) // Height is the limiting dimension, so scaling w |
| | 99 | | { |
| 0 | 100 | | screenFrameData.FrameHeight = screenFrameData.ScreenHeight * FRAME_SCALE; |
| 0 | 101 | | screenFrameData.FrameWidth = screenFrameData.FrameHeight * targetAspectRatio; |
| | 102 | | } |
| | 103 | | else // Width is the limiting dimension, so scaling height based on it |
| | 104 | | { |
| 0 | 105 | | screenFrameData.FrameWidth = screenFrameData.ScreenWidth * FRAME_SCALE; |
| 0 | 106 | | screenFrameData.FrameHeight = screenFrameData.FrameWidth / targetAspectRatio; |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | return screenFrameData; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | private static (ScreenFrameData data, float targetRescale) CalculateTargetScreenFrame(ScreenFrameData currentScr |
| | 113 | | { |
| 0 | 114 | | var screenFrameData = new ScreenFrameData(); |
| | 115 | |
|
| 0 | 116 | | float upscaleFrameWidth = TARGET_FRAME_WIDTH / currentScreenFrameData.FrameWidth; |
| 0 | 117 | | float upscaleFrameHeight = TARGET_FRAME_HEIGHT / currentScreenFrameData.FrameHeight; |
| 0 | 118 | | Debug.Assert(Math.Abs(upscaleFrameWidth - upscaleFrameHeight) < 0.01f, "Screenshot upscale factors should be |
| | 119 | |
|
| 0 | 120 | | float targetRescale = upscaleFrameWidth; |
| | 121 | |
|
| 0 | 122 | | screenFrameData.ScreenWidth = currentScreenFrameData.ScreenWidth * upscaleFrameWidth; |
| 0 | 123 | | screenFrameData.ScreenHeight = currentScreenFrameData.ScreenHeight * upscaleFrameWidth; |
| 0 | 124 | | screenFrameData.FrameWidth = currentScreenFrameData.FrameWidth * upscaleFrameWidth; |
| 0 | 125 | | screenFrameData.FrameHeight = currentScreenFrameData.FrameHeight * upscaleFrameWidth; |
| 0 | 126 | | Debug.Assert(Math.Abs(screenFrameData.FrameWidth - TARGET_FRAME_WIDTH) < 0.1f, "Calculated screenshot width |
| 0 | 127 | | Debug.Assert(Math.Abs(screenFrameData.FrameHeight - TARGET_FRAME_HEIGHT) < 0.1f, "Calculated screenshot heig |
| | 128 | |
|
| 0 | 129 | | return (screenFrameData, targetRescale); |
| | 130 | | } |
| | 131 | |
|
| | 132 | | private static ScreenFrameData CalculateRoundRescaledScreenFrame(ScreenFrameData rescalingScreenFrame, int round |
| 0 | 133 | | new () |
| | 134 | | { |
| | 135 | | FrameWidth = rescalingScreenFrame.FrameWidth * roundedRescaleFactor, |
| | 136 | | FrameHeight = rescalingScreenFrame.FrameHeight * roundedRescaleFactor, |
| | 137 | | ScreenWidth = rescalingScreenFrame.ScreenWidth * roundedRescaleFactor, |
| | 138 | | ScreenHeight = rescalingScreenFrame.ScreenHeight * roundedRescaleFactor, |
| | 139 | | }; |
| | 140 | | } |
| | 141 | | } |