< Summary

Class:DCL.Builder.ScreenshotCameraController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Cameras/ScreenshotCameraController.cs
Covered lines:19
Uncovered lines:27
Coverable lines:46
Total lines:95
Line coverage:41.3% (19 of 46)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Init(...)0%3.653058.33%
Dispose()0%110100%
TakeSceneAerialScreenshot(...)0%2100%
TakeSceneScreenshot(...)0%110100%
TakeSceneScreenshot(...)0%110100%
TakeSceneScreenshot(...)0%110100%
TakeScreenshot(...)0%7.933018.18%
ScreenshotFromCamera(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Cameras/ScreenshotCameraController.cs

#LineLine coverage
 1using DCL.Camera;
 2using DCL.Configuration;
 3using DCL.Controllers;
 4using UnityEngine;
 5
 6namespace DCL.Builder
 7{
 8    public class ScreenshotCameraController : IScreenshotCameraController
 9    {
 10        private IContext context;
 11        private UnityEngine.Camera screenshotCamera;
 12        internal IFreeCameraMovement freeCameraMovement;
 13
 14        public void Init(IContext context)
 15        {
 716            this.context = context;
 717            GameObject screenshotCameraGameObject = new GameObject("BuilderScreenshotCamera");
 718            screenshotCamera = screenshotCameraGameObject.AddComponent<UnityEngine.Camera>();
 719            screenshotCamera.depth = -9999;
 20
 721            screenshotCamera.gameObject.SetActive(false);
 22
 723            if (context.sceneReferences.cameraController != null)
 24            {
 025                var cameraController = context.sceneReferences.cameraController.GetComponent<Camera.CameraController>();
 26                // We assign the same culling configuration as the camera
 027                var biwCulling = BIWUtils.GetBIWCulling(cameraController.GetCulling());
 028                screenshotCamera.cullingMask = biwCulling;
 29
 030                if(cameraController.TryGetCameraStateByType<FreeCameraMovement>(out CameraStateBase cameraState))
 031                    freeCameraMovement = (FreeCameraMovement) cameraState;
 32            }
 733        }
 34
 435        public void Dispose() { GameObject.Destroy(screenshotCamera.gameObject); }
 36
 37        public void TakeSceneAerialScreenshot(IParcelScene parcelScene, IScreenshotCameraController.OnSnapshotsReady onS
 38        {
 039            Vector3 pointToLookAt = BIWUtils.CalculateUnityMiddlePoint(parcelScene);
 040            float heightPosition = context.editorContext.godModeDynamicVariablesAsset.aerialScreenshotHeight * Mathf.Sqr
 041            Vector3 cameraPosition = pointToLookAt  + Vector3.up * heightPosition;
 42
 043            TakeSceneScreenshot(cameraPosition, pointToLookAt, BIWSettings.AERIAL_SCREENSHOT_WIDTH, BIWSettings.AERIAL_S
 044        }
 45
 46        public void TakeSceneScreenshot(IScreenshotCameraController.OnSnapshotsReady onSuccess)
 47        {
 148            screenshotCamera.transform.position = freeCameraMovement.GetCameraPosition;
 149            screenshotCamera.transform.rotation = freeCameraMovement.gameObject.transform.rotation;
 50
 151            TakeScreenshot(onSuccess, BIWSettings.SCENE_SNAPSHOT_WIDTH_RES, BIWSettings.SCENE_SNAPSHOT_HEIGHT_RES);
 152        }
 53
 254        public void TakeSceneScreenshot(Vector3 camPosition, Vector3 pointToLookAt, IScreenshotCameraController.OnSnapsh
 55
 56        public void TakeSceneScreenshot(Vector3 camPosition, Vector3 pointToLookAt, int width, int height, IScreenshotCa
 57        {
 158            screenshotCamera.transform.position = camPosition;
 159            screenshotCamera.transform.LookAt(pointToLookAt);
 160            TakeScreenshot(onSuccess, width, height);
 161        }
 62
 63        private void TakeScreenshot(IScreenshotCameraController.OnSnapshotsReady callback, int width, int height)
 64        {
 265            if (UnityEngine.Camera.main == null)
 266                return;
 67
 68            //We deselect the entities to take better photos
 069            context.editorContext.entityHandler.DeselectEntities();
 70
 071            screenshotCamera.gameObject.SetActive(true);
 72
 073            var current = screenshotCamera.targetTexture;
 074            screenshotCamera.targetTexture = null;
 75
 076            Texture2D sceneScreenshot = ScreenshotFromCamera(width, height);
 077            screenshotCamera.targetTexture = current;
 078            screenshotCamera.gameObject.SetActive(false);
 79
 080            callback?.Invoke(sceneScreenshot);
 081        }
 82
 83        private Texture2D ScreenshotFromCamera(int width, int height)
 84        {
 085            RenderTexture rt = new RenderTexture(width, height, 32);
 086            screenshotCamera.targetTexture = rt;
 087            Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
 088            screenshotCamera.Render();
 089            RenderTexture.active = rt;
 090            screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
 091            screenShot.Apply();
 092            return screenShot;
 93        }
 94    }
 95}