< Summary

Class:DCL.Camera.FreeCameraMovement
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/States/EditorMode/FreeCameraController/FreeCameraMovement.cs
Covered lines:261
Uncovered lines:42
Coverable lines:303
Total lines:638
Line coverage:86.1% (261 of 303)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FreeCameraMovement()0%110100%
Awake()0%10100100%
StartDectectingMovement()0%2100%
HasBeenMovement()0%2100%
StopDetectingMovement()0%2100%
OnInputMouseUp(...)0%2.062075%
OnInputMouseDown(...)0%2.032080%
OnDestroy()0%14140100%
Update()0%110100%
HandleCameraMovement()0%3.213071.43%
HandleCameraLook()0%2.062075%
HandleCameraPan()0%3.073080%
HandleCameraMovementInput()0%14140100%
GetTotalVelocity(...)0%3.583060%
SetCameraCanMove(...)0%2100%
MouseWheel(...)0%3.13077.78%
MouseDragRaw(...)0%330100%
MouseDrag(...)0%440100%
CameraDrag(...)0%2.022083.33%
CameraLook(...)0%3.043083.33%
OnGetRotation()0%110100%
FocusOnEntities(...)0%9.738070%
SetPosition(...)0%110100%
LookAt(...)0%110100%
LookAt(...)0%110100%
SmoothLookAt(...)0%2100%
SmoothLookAt(...)0%2.062075%
FindMidPoint(...)0%660100%
SmoothScroll()0%4.024088.89%
SmoothFocusOnTarget()0%5.055087.5%
SmoothLookAtCorutine()0%440100%
SetResetConfiguration(...)0%110100%
SetResetConfiguration(...)0%2100%
ResetCameraPosition()0%110100%
TakeSceneScreenshot(...)0%110100%
TakeSceneScreenshotCoroutine()0%550100%
TakeSceneScreenshotFromResetPosition(...)0%2100%
TakeSceneScreenshotFromResetPositionCoroutine()0%20400%
ScreenshotFromCamera(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/States/EditorMode/FreeCameraController/FreeCameraMovement.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5namespace DCL.Camera
 6{
 7    public class FreeCameraMovement : CameraStateBase, IFreeCameraMovement
 8    {
 9        private const float CAMERA_ANGLE_THRESHOLD = 0.01f;
 10        private const float CAMERA_PAN_THRESHOLD = 0.001f;
 11        private const float CAMERA_MOVEMENT_THRESHOLD = 0.001f;
 12
 13        private const float CAMERA_MOVEMENT_DEACTIVATE_INPUT_MAGNITUD = 0.001f;
 14
 15        private const int SCENE_SNAPSHOT_WIDTH_RES = 854;
 16        private const int SCENE_SNAPSHOT_HEIGHT_RES = 480;
 17
 24518        public float focusDistance = 5f;
 19
 20        [Header("Camera Movement")]
 24521        public float xPlaneSpeedPercentCompensantion = 0.85f;
 22
 24523        public float movementSpeed = 5f;
 24524        public float lerpTime = 0.3F;
 25
 24526        public float lerpDeccelerationTime = 0.3F;
 24527        public float initalAcceleration = 2f;
 24528        public float speedClamp = 1f;
 29
 30        [Header("Camera Look")]
 24531        public float smoothLookAtSpeed = 5f;
 32
 24533        public float smoothCameraLookSpeed = 5f;
 24534        public float lookSpeedH = 2f;
 24535        public float lookSpeedV = 2f;
 36
 37        [Header("Camera Pan")]
 24538        public float smoothCameraPanAceleration = 5f;
 39
 24540        public float dragSpeed = 3f;
 41
 42        [Header("Camera Zoom")]
 24543        public float zoomSpeed = 2f;
 44
 45        [Header("InputActions")]
 46        [SerializeField] internal InputAction_Hold advanceFowardInputAction;
 47
 48        [SerializeField] internal InputAction_Hold advanceBackInputAction;
 49        [SerializeField] internal InputAction_Hold advanceLeftInputAction;
 50        [SerializeField] internal InputAction_Hold advanceRightInputAction;
 51        [SerializeField] internal InputAction_Hold advanceUpInputAction;
 52        [SerializeField] internal InputAction_Hold advanceDownInputAction;
 53        [SerializeField] internal InputAction_Hold cameraPanInputAction;
 54        [SerializeField] internal InputAction_Trigger zoomInFromKeyboardInputAction;
 55        [SerializeField] internal InputAction_Trigger zoomOutFromKeyboardInputAction;
 56
 24557        internal Vector3 direction = Vector3.zero;
 58
 59        internal float yaw = 0f;
 60        internal float pitch = 0f;
 61
 62        internal float panAxisX = 0f;
 63        internal float panAxisY = 0f;
 64
 24565        internal bool isCameraAbleToMove = true;
 66
 67        internal bool isAdvancingForward = false;
 68        internal bool isAdvancingBackward = false;
 69        internal bool isAdvancingLeft = false;
 70        internal bool isAdvancingRight = false;
 71        internal bool isAdvancingUp = false;
 72        internal bool isAdvancingDown = false;
 73
 74        internal bool isDetectingMovement = false;
 75        internal bool hasBeenMovement = false;
 76
 77        internal bool isPanCameraActive = false;
 78        internal bool isMouseRightClickDown = false;
 79
 80        internal Coroutine smoothLookAtCoroutine;
 81        internal Coroutine smoothFocusOnTargetCoroutine;
 82        internal Coroutine smoothScrollCoroutine;
 83        internal Coroutine takeScreenshotCoroutine;
 84
 85        private InputAction_Hold.Started advanceForwardStartDelegate;
 86        private InputAction_Hold.Finished advanceForwardFinishedDelegate;
 87
 88        private InputAction_Hold.Started advanceBackStartDelegate;
 89        private InputAction_Hold.Finished advanceBackFinishedDelegate;
 90
 91        private InputAction_Hold.Started advanceLeftStartDelegate;
 92        private InputAction_Hold.Finished advanceLeftFinishedDelegate;
 93
 94        private InputAction_Hold.Started advanceRightStartDelegate;
 95        private InputAction_Hold.Finished advanceRightFinishedDelegate;
 96
 97        private InputAction_Hold.Started advanceDownStartDelegate;
 98        private InputAction_Hold.Finished advanceDownFinishedDelegate;
 99
 100        private InputAction_Hold.Started advanceUpStartDelegate;
 101        private InputAction_Hold.Finished advanceUpFinishedDelegate;
 102
 103        private InputAction_Hold.Started cameraPanStartDelegate;
 104        private InputAction_Hold.Finished cameraPanFinishedDelegate;
 105
 106        private InputAction_Trigger.Triggered zoomInFromKeyboardDelegate;
 107        private InputAction_Trigger.Triggered zoomOutFromKeyboardDelegate;
 108
 109        private Vector3 nextTranslation;
 110        internal Vector3 originalCameraPosition;
 111        internal Vector3 originalCameraPointToLookAt;
 245112        private Vector3 cameraVelocity = Vector3.zero;
 113
 114        private float lastMouseWheelTime;
 115        private float cameraPanAdvance;
 116        private float cameraLookAdvance;
 117        internal UnityEngine.Camera screenshotCamera;
 118
 119        private void Awake()
 120        {
 17121            BIWInputWrapper.OnMouseDrag += MouseDrag;
 17122            BIWInputWrapper.OnMouseDragRaw += MouseDragRaw;
 17123            BIWInputWrapper.OnMouseWheel += MouseWheel;
 124
 17125            BIWInputWrapper.OnMouseDown += OnInputMouseDown;
 17126            BIWInputWrapper.OnMouseUp += OnInputMouseUp;
 127
 17128            advanceForwardStartDelegate = (action) => isAdvancingForward = true;
 17129            advanceForwardFinishedDelegate = (action) => isAdvancingForward = false;
 130
 17131            if (advanceFowardInputAction != null)
 132            {
 3133                advanceFowardInputAction.OnStarted += advanceForwardStartDelegate;
 3134                advanceFowardInputAction.OnFinished += advanceForwardFinishedDelegate;
 135            }
 136
 17137            advanceBackStartDelegate = (action) => isAdvancingBackward = true;
 17138            advanceBackFinishedDelegate = (action) => isAdvancingBackward = false;
 139
 17140            if (advanceBackInputAction != null)
 141            {
 3142                advanceBackInputAction.OnStarted += advanceBackStartDelegate;
 3143                advanceBackInputAction.OnFinished += advanceBackFinishedDelegate;
 144            }
 145
 17146            advanceLeftStartDelegate = (action) => isAdvancingLeft = true;
 17147            advanceLeftFinishedDelegate = (action) => isAdvancingLeft = false;
 148
 17149            if (advanceLeftInputAction != null)
 150            {
 3151                advanceLeftInputAction.OnStarted += advanceLeftStartDelegate;
 3152                advanceLeftInputAction.OnFinished += advanceLeftFinishedDelegate;
 153            }
 154
 17155            advanceRightStartDelegate = (action) => isAdvancingRight = true;
 17156            advanceRightFinishedDelegate = (action) => isAdvancingRight = false;
 157
 17158            if (advanceRightInputAction != null)
 159            {
 3160                advanceRightInputAction.OnStarted += advanceRightStartDelegate;
 3161                advanceRightInputAction.OnFinished += advanceRightFinishedDelegate;
 162            }
 163
 17164            advanceUpStartDelegate = (action) => isAdvancingUp = true;
 17165            advanceUpFinishedDelegate = (action) => isAdvancingUp = false;
 166
 17167            if (advanceUpInputAction != null)
 168            {
 3169                advanceUpInputAction.OnStarted += advanceUpStartDelegate;
 3170                advanceUpInputAction.OnFinished += advanceUpFinishedDelegate;
 171            }
 172
 17173            advanceDownStartDelegate = (action) => isAdvancingDown = true;
 17174            advanceDownFinishedDelegate = (action) => isAdvancingDown = false;
 175
 17176            if (advanceDownInputAction != null)
 177            {
 3178                advanceDownInputAction.OnStarted += advanceDownStartDelegate;
 3179                advanceDownInputAction.OnFinished += advanceDownFinishedDelegate;
 180            }
 181
 17182            cameraPanStartDelegate = (action) => isPanCameraActive = true;
 17183            cameraPanFinishedDelegate = (action) => isPanCameraActive = false;
 184
 17185            if (cameraPanInputAction != null)
 186            {
 3187                cameraPanInputAction.OnStarted += cameraPanStartDelegate;
 3188                cameraPanInputAction.OnFinished += cameraPanFinishedDelegate;
 189            }
 190
 17191            zoomInFromKeyboardDelegate = (action) => MouseWheel(1f);
 192
 17193            if (zoomInFromKeyboardInputAction != null)
 3194                zoomInFromKeyboardInputAction.OnTriggered += zoomInFromKeyboardDelegate;
 195
 17196            zoomOutFromKeyboardDelegate = (action) => MouseWheel(-1f);
 197
 17198            if (zoomOutFromKeyboardInputAction != null)
 3199                zoomOutFromKeyboardInputAction.OnTriggered += zoomOutFromKeyboardDelegate;
 17200        }
 201
 202        public void StartDectectingMovement()
 203        {
 0204            isDetectingMovement = true;
 0205            hasBeenMovement = false;
 0206        }
 207
 0208        public bool HasBeenMovement() { return hasBeenMovement; }
 209
 0210        public void StopDetectingMovement() { isDetectingMovement = false; }
 211
 212        internal void OnInputMouseUp(int buttonId, Vector3 mousePosition)
 213        {
 1214            if (buttonId != 1)
 0215                return;
 216
 1217            isMouseRightClickDown = false;
 1218        }
 219
 220        internal void OnInputMouseDown(int buttonId, Vector3 mousePosition)
 221        {
 1222            if (buttonId != 1)
 0223                return;
 224
 1225            isMouseRightClickDown = true;
 1226            direction = Vector3.zero;
 1227        }
 228
 229        private void OnDestroy()
 230        {
 17231            BIWInputWrapper.OnMouseDrag -= MouseDrag;
 17232            BIWInputWrapper.OnMouseDragRaw -= MouseDragRaw;
 17233            BIWInputWrapper.OnMouseWheel -= MouseWheel;
 234
 17235            BIWInputWrapper.OnMouseDown -= OnInputMouseDown;
 17236            BIWInputWrapper.OnMouseUp -= OnInputMouseUp;
 237
 17238            if (advanceFowardInputAction != null)
 239            {
 3240                advanceFowardInputAction.OnStarted -= advanceForwardStartDelegate;
 3241                advanceFowardInputAction.OnFinished -= advanceForwardFinishedDelegate;
 242            }
 243
 17244            if (advanceBackInputAction != null)
 245            {
 3246                advanceBackInputAction.OnStarted -= advanceBackStartDelegate;
 3247                advanceBackInputAction.OnFinished -= advanceBackFinishedDelegate;
 248            }
 249
 17250            if (advanceLeftInputAction != null)
 251            {
 3252                advanceLeftInputAction.OnStarted -= advanceLeftStartDelegate;
 3253                advanceLeftInputAction.OnFinished -= advanceLeftFinishedDelegate;
 254            }
 255
 17256            if (advanceRightInputAction != null)
 257            {
 3258                advanceRightInputAction.OnStarted -= advanceRightStartDelegate;
 3259                advanceRightInputAction.OnFinished -= advanceRightFinishedDelegate;
 260            }
 261
 17262            if (advanceDownInputAction != null)
 263            {
 3264                advanceDownInputAction.OnStarted -= advanceDownStartDelegate;
 3265                advanceDownInputAction.OnFinished -= advanceDownFinishedDelegate;
 266            }
 267
 17268            if (advanceUpInputAction != null)
 269            {
 3270                advanceUpInputAction.OnStarted -= advanceUpStartDelegate;
 3271                advanceUpInputAction.OnFinished -= advanceUpFinishedDelegate;
 272            }
 273
 17274            if (cameraPanInputAction != null)
 275            {
 3276                cameraPanInputAction.OnStarted -= cameraPanStartDelegate;
 3277                cameraPanInputAction.OnFinished -= cameraPanFinishedDelegate;
 278            }
 279
 17280            if (zoomInFromKeyboardInputAction != null)
 3281                zoomInFromKeyboardInputAction.OnTriggered -= zoomInFromKeyboardDelegate;
 282
 17283            if (zoomOutFromKeyboardInputAction != null)
 3284                zoomOutFromKeyboardInputAction.OnTriggered -= zoomOutFromKeyboardDelegate;
 285
 17286            if (smoothScrollCoroutine != null)
 1287                CoroutineStarter.Stop(smoothScrollCoroutine);
 288
 17289            if (smoothLookAtCoroutine != null)
 1290                CoroutineStarter.Stop(smoothLookAtCoroutine);
 291
 17292            if (smoothFocusOnTargetCoroutine != null)
 1293                CoroutineStarter.Stop(smoothFocusOnTargetCoroutine);
 294
 17295            if (takeScreenshotCoroutine != null)
 2296                CoroutineStarter.Stop(takeScreenshotCoroutine);
 17297        }
 298
 299        private void Update()
 300        {
 274301            HandleCameraLook();
 274302            HandleCameraPan();
 274303            HandleCameraMovement();
 274304        }
 305
 306        #region CameraTransformChanges
 307
 308        private void HandleCameraMovement()
 309        {
 274310            HandleCameraMovementInput();
 274311            if (direction.magnitude >= CAMERA_MOVEMENT_DEACTIVATE_INPUT_MAGNITUD)
 0312                nextTranslation = direction;
 274313            nextTranslation = Vector3.Lerp(nextTranslation, Vector3.zero, lerpTime * Time.deltaTime);
 314
 274315            if (nextTranslation.magnitude >= CAMERA_MOVEMENT_THRESHOLD)
 0316                transform.Translate(nextTranslation * (movementSpeed * Time.deltaTime), Space.Self);
 274317        }
 318
 319        private void HandleCameraLook()
 320        {
 274321            Quaternion nextIteration =  Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(pitch, yaw, 0f)
 322
 274323            if (Mathf.Abs(nextIteration.eulerAngles.magnitude - transform.rotation.eulerAngles.magnitude) >= CAMERA_ANGL
 0324                transform.rotation = nextIteration;
 274325        }
 326
 327        private void HandleCameraPan()
 328        {
 274329            panAxisX = Mathf.Lerp(panAxisX, 0, cameraPanAdvance * Time.deltaTime);
 274330            panAxisY = Mathf.Lerp(panAxisY, 0, cameraPanAdvance * Time.deltaTime);
 331
 274332            if (Mathf.Abs(panAxisX) >= CAMERA_PAN_THRESHOLD || Mathf.Abs(panAxisY) >= CAMERA_PAN_THRESHOLD)
 0333                transform.Translate(panAxisX, panAxisY, 0);
 274334        }
 335
 336        #endregion
 337
 338        internal void HandleCameraMovementInput()
 339        {
 276340            int velocityChangedCount = 0;
 276341            if (isAdvancingForward)
 342            {
 1343                cameraVelocity += GetTotalVelocity(Vector3.forward);
 1344                velocityChangedCount++;
 345            }
 346
 276347            if (isAdvancingBackward)
 348            {
 1349                cameraVelocity += GetTotalVelocity(Vector3.back);
 1350                velocityChangedCount++;
 351            }
 352
 276353            if (!isAdvancingBackward && !isAdvancingForward)
 274354                cameraVelocity.z = Mathf.Lerp(cameraVelocity.z, 0, lerpDeccelerationTime);
 355
 276356            if (isAdvancingRight)
 357            {
 1358                cameraVelocity += GetTotalVelocity(Vector3.right) * xPlaneSpeedPercentCompensantion;
 1359                velocityChangedCount++;
 360            }
 361
 276362            if (isAdvancingLeft)
 363            {
 1364                cameraVelocity += GetTotalVelocity(Vector3.left) * xPlaneSpeedPercentCompensantion;
 1365                velocityChangedCount++;
 366            }
 367
 276368            if (!isAdvancingRight && !isAdvancingLeft)
 274369                cameraVelocity.x = Mathf.Lerp(cameraVelocity.x, 0, lerpDeccelerationTime);
 370
 276371            if (isAdvancingUp)
 372            {
 1373                cameraVelocity += GetTotalVelocity(Vector3.up);
 1374                velocityChangedCount++;
 375            }
 376
 276377            if (isAdvancingDown)
 378            {
 1379                cameraVelocity += GetTotalVelocity(Vector3.down);
 1380                velocityChangedCount++;
 381            }
 382
 276383            if (!isAdvancingUp && !isAdvancingDown)
 274384                cameraVelocity.y = Mathf.Lerp(cameraVelocity.y, 0, lerpDeccelerationTime);
 385
 276386            if (velocityChangedCount != 0)
 2387                cameraVelocity = Vector3.ClampMagnitude(cameraVelocity, speedClamp);
 388
 276389            direction = cameraVelocity;
 276390        }
 391
 392        private Vector3 GetTotalVelocity(Vector3 velocityToAdd)
 393        {
 6394            if (!isMouseRightClickDown)
 0395                return  Vector3.zero;
 396
 6397            if (isDetectingMovement)
 0398                hasBeenMovement = true;
 6399            return velocityToAdd * (initalAcceleration * Time.deltaTime);
 400        }
 401
 0402        public void SetCameraCanMove(bool canMove) { isCameraAbleToMove = canMove; }
 403
 404        internal void MouseWheel(float axis)
 405        {
 1406            if (!isCameraAbleToMove)
 0407                return;
 408
 1409            if (smoothScrollCoroutine != null)
 0410                CoroutineStarter.Stop(smoothScrollCoroutine);
 411
 1412            float delta = Time.time - lastMouseWheelTime;
 1413            float scrollValue = axis * Mathf.Clamp01(delta);
 1414            lastMouseWheelTime = Time.time;
 415
 1416            smoothScrollCoroutine = CoroutineStarter.Start(SmoothScroll(scrollValue));
 1417        }
 418
 419        internal void MouseDragRaw(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 420        {
 1421            if (buttonId == 1 && !isPanCameraActive)
 1422                CameraLook(axisX, axisY);
 1423        }
 424
 425        internal void MouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 426        {
 1427            if (buttonId == 2 || buttonId == 1 && isPanCameraActive)
 1428                CameraDrag(axisX, axisY);
 1429        }
 430
 431        private void CameraDrag(float axisX, float axisY)
 432        {
 1433            if (!isCameraAbleToMove)
 0434                return;
 435
 1436            panAxisX += -axisX * Time.deltaTime * dragSpeed;
 1437            panAxisY += -axisY * Time.deltaTime * dragSpeed;
 1438            cameraPanAdvance = smoothCameraPanAceleration;
 1439        }
 440
 441        private void CameraLook(float axisX, float axisY)
 442        {
 1443            if (!isCameraAbleToMove || !isMouseRightClickDown)
 0444                return;
 445
 1446            yaw += lookSpeedH * axisX;
 1447            pitch -= lookSpeedV * axisY;
 1448            cameraLookAdvance = smoothCameraLookSpeed * Time.deltaTime;
 1449        }
 450
 1451        public override Vector3 OnGetRotation() { return transform.eulerAngles; }
 452
 453        public void FocusOnEntities(List<BIWEntity> entitiesToFocus)
 454        {
 1455            if (entitiesToFocus.Count <= 0)
 0456                return;
 457
 1458            Vector3 middlePoint = FindMidPoint(entitiesToFocus);
 1459            if (Vector3.positiveInfinity == middlePoint ||
 460                Vector3.negativeInfinity == middlePoint ||
 461                float.IsNaN(middlePoint.x) ||
 462                float.IsNaN(middlePoint.y) ||
 463                float.IsNaN(middlePoint.z))
 0464                return;
 465
 1466            if (smoothFocusOnTargetCoroutine != null)
 0467                CoroutineStarter.Stop(smoothFocusOnTargetCoroutine);
 1468            smoothFocusOnTargetCoroutine = CoroutineStarter.Start(SmoothFocusOnTarget(middlePoint));
 1469            SmoothLookAt(middlePoint);
 1470        }
 471
 2472        public void SetPosition(Vector3 position) { transform.position = position; }
 473
 474        public void LookAt(Transform transformToLookAt)
 475        {
 37476            LookAt(transformToLookAt.position);
 37477        }
 478
 479        public void LookAt(Vector3 pointToLookAt)
 480        {
 38481            transform.LookAt(pointToLookAt);
 38482            yaw = transform.eulerAngles.y;
 38483            pitch = transform.eulerAngles.x;
 38484        }
 485
 0486        public void SmoothLookAt(Transform transformToLookAt) { SmoothLookAt(transformToLookAt.position); }
 487
 488        public void SmoothLookAt(Vector3 position)
 489        {
 1490            if (smoothLookAtCoroutine != null)
 0491                CoroutineStarter.Stop(smoothLookAtCoroutine);
 1492            smoothLookAtCoroutine = CoroutineStarter.Start(SmoothLookAtCorutine(position));
 1493        }
 494
 495        Vector3 FindMidPoint(List<BIWEntity> entitiesToLook)
 496        {
 1497            Vector3 finalPosition = Vector3.zero;
 1498            int totalPoints = 0;
 4499            foreach (BIWEntity entity in entitiesToLook)
 500            {
 1501                if (entity.rootEntity.meshRootGameObject && entity.rootEntity.meshesInfo.renderers.Length > 0)
 502                {
 1503                    Vector3 midPointFromEntity = Vector3.zero;
 4504                    foreach (Renderer render in entity.rootEntity.renderers)
 505                    {
 1506                        if (render == null)
 507                            continue;
 1508                        midPointFromEntity += render.bounds.center;
 509                    }
 510
 1511                    midPointFromEntity /= entity.rootEntity.renderers.Length;
 1512                    finalPosition += midPointFromEntity;
 1513                    totalPoints++;
 514                }
 515            }
 516
 1517            finalPosition /= totalPoints;
 1518            return finalPosition;
 519        }
 520
 521        IEnumerator SmoothScroll(float axis)
 522        {
 1523            float scrollMovementDestination = axis * zoomSpeed;
 524
 1525            Vector3 targetPosition = transform.position + transform.TransformDirection(Vector3.forward * scrollMovementD
 526
 1527            float advance = 0;
 1528            while (advance <= 1)
 529            {
 1530                advance += smoothLookAtSpeed * Time.deltaTime;
 1531                Vector3 result = Vector3.Lerp(transform.position, targetPosition, advance);
 1532                transform.position = result;
 1533                yield return null;
 534            }
 0535        }
 536
 537        IEnumerator SmoothFocusOnTarget(Vector3 targetPosition)
 538        {
 1539            float advance = 0;
 17540            while (advance <= 1)
 541            {
 17542                advance += smoothLookAtSpeed * Time.deltaTime;
 17543                transform.position = Vector3.Lerp(transform.position, targetPosition, advance);
 17544                if (Vector3.Distance(transform.position, targetPosition) <= focusDistance)
 1545                    yield break;
 16546                yield return null;
 547            }
 0548        }
 549
 550        IEnumerator SmoothLookAtCorutine(Vector3 targetPosition)
 551        {
 1552            Quaternion targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
 1553            float advance = 0;
 36554            while (advance <= 1)
 555            {
 35556                advance += smoothLookAtSpeed * Time.deltaTime;
 35557                transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, advance);
 35558                yield return null;
 559            }
 560
 1561            yaw = transform.eulerAngles.y;
 1562            pitch = transform.eulerAngles.x;
 1563        }
 564
 565        public void SetResetConfiguration(Vector3 position, Transform lookAt)
 566        {
 39567            SetResetConfiguration(position,lookAt.position);
 39568        }
 569
 570        public void SetResetConfiguration(Vector3 position, Vector3 pointToLook)
 571        {
 0572            originalCameraPosition = position;
 0573            originalCameraPointToLookAt = pointToLook;
 0574        }
 575
 576        public void ResetCameraPosition()
 577        {
 1578            SetPosition(originalCameraPosition);
 1579            LookAt(originalCameraPointToLookAt);
 1580            direction = Vector3.zero;
 1581        }
 582
 4583        public void TakeSceneScreenshot(IFreeCameraMovement.OnSnapshotsReady onSuccess) { takeScreenshotCoroutine = Coro
 584
 585        private IEnumerator TakeSceneScreenshotCoroutine(IFreeCameraMovement.OnSnapshotsReady callback)
 586        {
 2587            UnityEngine.Camera camera = UnityEngine.Camera.main;
 2588            if (screenshotCamera != null)
 2589                camera = screenshotCamera;
 590
 2591            var current = camera.targetTexture;
 2592            camera.targetTexture = null;
 593
 2594            yield return null;
 595
 2596            Texture2D sceneScreenshot = ScreenshotFromCamera(camera, SCENE_SNAPSHOT_WIDTH_RES, SCENE_SNAPSHOT_HEIGHT_RES
 2597            camera.targetTexture = current;
 2598            callback?.Invoke(sceneScreenshot);
 2599        }
 600
 0601        public void TakeSceneScreenshotFromResetPosition(IFreeCameraMovement.OnSnapshotsReady onSuccess) { StartCoroutin
 602
 603        private IEnumerator TakeSceneScreenshotFromResetPositionCoroutine(IFreeCameraMovement.OnSnapshotsReady callback)
 604        {
 605            // Store current camera position/direction
 0606            Vector3 currentPos = transform.position;
 0607            Vector3 currentLookAt = transform.forward;
 0608            SetPosition(originalCameraPosition);
 0609            transform.LookAt(originalCameraPointToLookAt);
 610
 0611            var current = camera.targetTexture;
 0612            camera.targetTexture = null;
 613
 0614            yield return null;
 615
 0616            Texture2D sceneScreenshot = ScreenshotFromCamera(camera, SCENE_SNAPSHOT_WIDTH_RES, SCENE_SNAPSHOT_HEIGHT_RES
 0617            camera.targetTexture = current;
 0618            callback?.Invoke(sceneScreenshot);
 619
 620            // Restore camera position/direction after the screenshot
 0621            SetPosition(currentPos);
 0622            transform.forward = currentLookAt;
 0623        }
 624
 625        private Texture2D ScreenshotFromCamera(UnityEngine.Camera cameraToScreenshot, int width, int height)
 626        {
 2627            RenderTexture rt = new RenderTexture(width, height, 32);
 2628            cameraToScreenshot.targetTexture = rt;
 2629            Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
 2630            cameraToScreenshot.Render();
 2631            RenderTexture.active = rt;
 2632            screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
 2633            screenShot.Apply();
 634
 2635            return screenShot;
 636        }
 637    }
 638}