< Summary

Class:MainScripts.DCL.Controllers.HUD.CharacterPreview.PreviewCameraController
Assembly:CharacterPreviewController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/CharacterPreview/PreviewCameraController.cs
Covered lines:0
Uncovered lines:53
Coverable lines:53
Total lines:133
Line coverage:0% (0 of 53)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:15
Method coverage:0% (0 of 15)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PreviewCameraController()0%2100%
SetCamera(...)0%2100%
SetTargetTexture(...)0%2100%
SetCameraEnabled(...)0%2100%
SetCameraLimits(...)0%2100%
ConfigureZoom(...)0%2100%
TakeSnapshot(...)0%2100%
SetFocus(...)0%6200%
MoveCamera(...)0%2100%
Dispose()0%2100%
CameraTransitionAsync()0%20400%
ApplyCameraLimits(...)0%6200%
GetCameraYPositionBasedOnZPosition()0%2100%
GetOffsetBasedOnZLimits(...)0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/CharacterPreview/PreviewCameraController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Tasks;
 3using System.Collections.Generic;
 4using System.Threading;
 5using UnityEngine;
 6using UnityEngine.Experimental.Rendering;
 7
 8namespace MainScripts.DCL.Controllers.HUD.CharacterPreview
 9{
 10    public class PreviewCameraController : IPreviewCameraController
 11    {
 12        private const int SUPER_SAMPLING = 1;
 13        private const float CAMERA_TRANSITION_TIME = 0.3f;
 14
 15        private Camera camera;
 16        private Transform cameraTransform;
 17        private Bounds cameraLimits;
 18        private (float verticalCenterRef, float bottomMaxOffset, float topMaxOffset) zoomConfig;
 019        private CancellationTokenSource cts = new ();
 20
 021        public RenderTexture CurrentTargetTexture => camera.targetTexture;
 22
 23        public void SetCamera(Camera cameraToUse, RenderTexture targetTexture = null)
 24        {
 025            this.camera = cameraToUse;
 026            cameraTransform = this.camera.transform;
 027            SetTargetTexture(targetTexture);
 028        }
 29
 30        public void SetTargetTexture(RenderTexture targetTexture) =>
 031            camera.targetTexture = targetTexture;
 32
 33        public void SetCameraEnabled(bool isEnabled) =>
 034            camera.enabled = isEnabled;
 35
 36        public void SetCameraLimits(Bounds limits) =>
 037            cameraLimits = limits;
 38
 39        public void ConfigureZoom(float verticalCenterRef, float bottomMaxOffset, float topMaxOffset)
 40        {
 041            zoomConfig.verticalCenterRef = verticalCenterRef;
 042            zoomConfig.bottomMaxOffset = bottomMaxOffset;
 043            zoomConfig.topMaxOffset = topMaxOffset;
 044        }
 45
 46        public Texture2D TakeSnapshot(int width, int height)
 47        {
 48            //To enable Post Processing effect add GraphicsFormat.R16G16B16A16_SFloat to the render texture
 049            RenderTexture rt = new RenderTexture(width * SUPER_SAMPLING, height * SUPER_SAMPLING, 32);
 050            camera.targetTexture = rt;
 051            Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
 052            camera.Render();
 053            RenderTexture.active = rt;
 054            screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
 055            screenShot.Apply();
 56
 057            return screenShot;
 58        }
 59
 60        public void SetFocus(Transform transformToMove, bool useTransition = true)
 61        {
 062            cts = cts.SafeRestart();
 63
 064            if (useTransition)
 65            {
 066                CameraTransitionAsync(cameraTransform.position, transformToMove.position,
 67                    cameraTransform.rotation, transformToMove.rotation,
 68                    CAMERA_TRANSITION_TIME,
 69                    cts.Token).Forget();
 70            }
 71            else
 72            {
 073                cameraTransform.position = transformToMove.position;
 074                cameraTransform.rotation = transformToMove.rotation;
 75            }
 076        }
 77
 78        public void MoveCamera(Vector3 positionDelta, bool changeYLimitsDependingOnZPosition)
 79        {
 080            cameraTransform.Translate(positionDelta, Space.World);
 081            ApplyCameraLimits(changeYLimitsDependingOnZPosition);
 082        }
 83
 84        public void Dispose() =>
 085            cts.SafeCancelAndDispose();
 86
 87        private async UniTask CameraTransitionAsync(
 88            Vector3 initPos, Vector3 endPos,
 89            Quaternion initRotation, Quaternion endRotation,
 90            float time, CancellationToken ct)
 91        {
 092            float currentTime = 0;
 093            float inverseTime = 1 / time;
 94
 095            while (currentTime < time)
 96            {
 097                currentTime = Mathf.Clamp(currentTime + Time.deltaTime, 0, time);
 098                cameraTransform.position = Vector3.Lerp(initPos, endPos, currentTime * inverseTime);
 099                cameraTransform.rotation = Quaternion.Lerp(initRotation, endRotation, currentTime * inverseTime);
 0100                await UniTask.NextFrame(ct);
 101            }
 0102        }
 103
 104        private void ApplyCameraLimits(bool changeYLimitsDependingOnZPosition)
 105        {
 0106            Vector3 pos = cameraTransform.localPosition;
 0107            pos.x = Mathf.Clamp(pos.x, cameraLimits.min.x, cameraLimits.max.x);
 0108            pos.z = Mathf.Clamp(pos.z, cameraLimits.min.z, cameraLimits.max.z);
 109
 0110            pos.y = changeYLimitsDependingOnZPosition ?
 111                GetCameraYPositionBasedOnZPosition() :
 112                Mathf.Clamp(pos.y, cameraLimits.min.y, cameraLimits.max.y);
 113
 0114            cameraTransform.localPosition = pos;
 0115        }
 116
 117        private float GetCameraYPositionBasedOnZPosition()
 118        {
 0119            Vector3 cameraPosition = cameraTransform.localPosition;
 0120            float minY = zoomConfig.verticalCenterRef - GetOffsetBasedOnZLimits(cameraPosition.z, zoomConfig.bottomMaxOf
 0121            float maxY = zoomConfig.verticalCenterRef + GetOffsetBasedOnZLimits(cameraPosition.z, zoomConfig.topMaxOffse
 0122            return Mathf.Clamp(cameraPosition.y, minY, maxY);
 123        }
 124
 125        private float GetOffsetBasedOnZLimits(float zValue, float maxOffset)
 126        {
 0127            if (zValue >= cameraLimits.max.z) return 0f;
 0128            if (zValue <= cameraLimits.min.z) return maxOffset;
 0129            float progress = (zValue - cameraLimits.min.z) / (cameraLimits.max.z - cameraLimits.min.z);
 0130            return maxOffset - (progress * maxOffset);
 131        }
 132    }
 133}