< Summary

Class:DCL.Camera.FreeCameraMovement
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/States/EditorMode/FreeCameraMovement.cs
Covered lines:116
Uncovered lines:158
Coverable lines:274
Total lines:571
Line coverage:42.3% (116 of 274)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FreeCameraMovement()0%110100%
Awake()0%110100%
StartDectectingMovement()0%2100%
StopDetectingMovement()0%2100%
OnInputMouseUp(...)0%6200%
OnInputMouseDown(...)0%6200%
OnDestroy()0%110100%
Update()0%110100%
HandleCameraMovement()0%3.213071.43%
HandleCameraLook()0%2.062075%
HandleCameraPan()0%3.073080%
HandleCameraMovementInput()0%31.6614055.17%
GetTotalVelocity(...)0%12300%
SetCameraCanMove(...)0%2100%
OnGizmoTransformObjectEnd(...)0%2100%
OnGizmoTransformObjectStart(...)0%2100%
MouseWheel(...)0%12300%
MouseDragRaw(...)0%12300%
MouseDrag(...)0%20400%
CameraDrag(...)0%6200%
CameraLook(...)0%12300%
OnGetRotation()0%2100%
FocusOnEntities(...)0%72800%
SetPosition(...)0%110100%
LookAt(...)0%110100%
SmoothLookAt(...)0%2100%
SmoothLookAt(...)0%6200%
FindMidPoint(...)0%42600%
SmoothScroll()0%20400%
SmoothFocusOnTarget()0%30500%
SmoothLookAtCorutine()0%20400%
SetResetConfiguration(...)0%2100%
ResetCameraPosition()0%2100%
TakeSceneScreenshot(...)0%2100%
TakeSceneScreenshotCoroutine()0%20400%
TakeSceneScreenshotFromResetPosition(...)0%2100%
TakeSceneScreenshotFromResetPositionCoroutine()0%20400%
ScreenshotFromCamera(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/States/EditorMode/FreeCameraMovement.cs

#LineLine coverage
 1using Builder.Gizmos;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5
 6namespace DCL.Camera
 7{
 8    public class FreeCameraMovement : CameraStateBase
 9    {
 10        private const float CAMERA_ANGLE_THRESHOLD = 0.01f;
 11        private const float CAMERA_PAN_THRESHOLD = 0.001f;
 12        private const float CAMERA_MOVEMENT_THRESHOLD = 0.001f;
 13
 14        private const float CAMERA_MOVEMENT_DEACTIVATE_INPUT_MAGNITUD = 0.001f;
 15
 16        private const int SCENE_SNAPSHOT_WIDTH_RES = 854;
 17        private const int SCENE_SNAPSHOT_HEIGHT_RES = 480;
 18
 31419        public float focusDistance = 5f;
 20
 21        [Header("Camera Movement")]
 31422        public float xPlaneSpeedPercentCompensantion = 0.85f;
 23
 31424        public float movementSpeed = 5f;
 31425        public float lerpTime = 0.3F;
 26
 31427        public float lerpDeccelerationTime = 0.3F;
 31428        public float initalAcceleration = 2f;
 31429        public float speedClamp = 1f;
 30
 31        [Header("Camera Look")]
 31432        public float smoothLookAtSpeed = 5f;
 33
 31434        public float smoothCameraLookSpeed = 5f;
 31435        public float lookSpeedH = 2f;
 31436        public float lookSpeedV = 2f;
 37
 38        [Header("Camera Pan")]
 31439        public float smoothCameraPanAceleration = 5f;
 40
 31441        public float dragSpeed = 3f;
 42
 43        [Header("Camera Zoom")]
 31444        public float zoomSpeed = 2f;
 45
 46        [Header("InputActions")]
 47        [SerializeField] internal InputAction_Hold advanceFowardInputAction;
 48
 49        [SerializeField] internal InputAction_Hold advanceBackInputAction;
 50        [SerializeField] internal InputAction_Hold advanceLeftInputAction;
 51        [SerializeField] internal InputAction_Hold advanceRightInputAction;
 52        [SerializeField] internal InputAction_Hold advanceUpInputAction;
 53        [SerializeField] internal InputAction_Hold advanceDownInputAction;
 54        [SerializeField] internal InputAction_Hold cameraPanInputAction;
 55        [SerializeField] internal InputAction_Trigger zoomInFromKeyboardInputAction;
 56        [SerializeField] internal InputAction_Trigger zoomOutFromKeyboardInputAction;
 57
 31458        private Vector3 direction = Vector3.zero;
 59
 60        private float yaw = 0f;
 61        private float pitch = 0f;
 62
 63        private float panAxisX = 0f;
 64        private float panAxisY = 0f;
 65
 31466        private bool isCameraAbleToMove = true;
 67
 68        private bool isAdvancingForward = false;
 69        private bool isAdvancingBackward = false;
 70        private bool isAdvancingLeft = false;
 71        private bool isAdvancingRight = false;
 72        private bool isAdvancingUp = false;
 73        private bool isAdvancingDown = false;
 74
 75        private bool isDetectingMovement = false;
 76        private bool hasBeenMovement = false;
 77
 78        private bool isPanCameraActive = false;
 79        private bool isMouseRightClickDown = false;
 80
 81        private Coroutine smoothLookAtCor;
 82        private Coroutine smoothFocusOnTargetCor;
 83        private Coroutine smoothScrollCor;
 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        private Vector3 originalCameraPosition;
 314111        private Vector3 cameraVelocity = Vector3.zero;
 112        private Transform originalCameraLookAt;
 113
 114        private float lastMouseWheelTime;
 115        private float cameraPanAdvance;
 116        private float cameraLookAdvance;
 117
 118        public delegate void OnSnapshotsReady(Texture2D sceneSnapshot);
 119
 120        private void Awake()
 121        {
 7122            BuilderInWorldInputWrapper.OnMouseDrag += MouseDrag;
 7123            BuilderInWorldInputWrapper.OnMouseDragRaw += MouseDragRaw;
 7124            BuilderInWorldInputWrapper.OnMouseWheel += MouseWheel;
 125
 7126            BuilderInWorldInputWrapper.OnMouseDown += OnInputMouseDown;
 7127            BuilderInWorldInputWrapper.OnMouseUp += OnInputMouseUp;
 128
 7129            DCLBuilderGizmoManager.OnGizmoTransformObjectStart += OnGizmoTransformObjectStart;
 7130            DCLBuilderGizmoManager.OnGizmoTransformObjectEnd += OnGizmoTransformObjectEnd;
 131
 7132            advanceForwardStartDelegate = (action) => isAdvancingForward = true;
 7133            advanceForwardFinishedDelegate = (action) => isAdvancingForward = false;
 134
 7135            advanceFowardInputAction.OnStarted += advanceForwardStartDelegate;
 7136            advanceFowardInputAction.OnFinished += advanceForwardFinishedDelegate;
 137
 7138            advanceBackStartDelegate = (action) => isAdvancingBackward = true;
 7139            advanceBackFinishedDelegate = (action) => isAdvancingBackward = false;
 140
 7141            advanceBackInputAction.OnStarted += advanceBackStartDelegate;
 7142            advanceBackInputAction.OnFinished += advanceBackFinishedDelegate;
 143
 7144            advanceLeftStartDelegate = (action) => isAdvancingLeft = true;
 7145            advanceLeftFinishedDelegate = (action) => isAdvancingLeft = false;
 146
 7147            advanceLeftInputAction.OnStarted += advanceLeftStartDelegate;
 7148            advanceLeftInputAction.OnFinished += advanceLeftFinishedDelegate;
 149
 7150            advanceRightStartDelegate = (action) => isAdvancingRight = true;
 7151            advanceRightFinishedDelegate = (action) => isAdvancingRight = false;
 152
 7153            advanceRightInputAction.OnStarted += advanceRightStartDelegate;
 7154            advanceRightInputAction.OnFinished += advanceRightFinishedDelegate;
 155
 7156            advanceUpStartDelegate = (action) => isAdvancingUp = true;
 7157            advanceUpFinishedDelegate = (action) => isAdvancingUp = false;
 158
 7159            advanceUpInputAction.OnStarted += advanceUpStartDelegate;
 7160            advanceUpInputAction.OnFinished += advanceUpFinishedDelegate;
 161
 7162            advanceDownStartDelegate = (action) => isAdvancingDown = true;
 7163            advanceDownFinishedDelegate = (action) => isAdvancingDown = false;
 164
 7165            advanceDownInputAction.OnStarted += advanceDownStartDelegate;
 7166            advanceDownInputAction.OnFinished += advanceDownFinishedDelegate;
 167
 7168            cameraPanStartDelegate = (action) => isPanCameraActive = true;
 7169            cameraPanFinishedDelegate = (action) => isPanCameraActive = false;
 170
 7171            cameraPanInputAction.OnStarted += cameraPanStartDelegate;
 7172            cameraPanInputAction.OnFinished += cameraPanFinishedDelegate;
 173
 7174            zoomInFromKeyboardDelegate = (action) => MouseWheel(1f);
 7175            zoomInFromKeyboardInputAction.OnTriggered += zoomInFromKeyboardDelegate;
 176
 7177            zoomOutFromKeyboardDelegate = (action) => MouseWheel(-1f);
 7178            zoomOutFromKeyboardInputAction.OnTriggered += zoomOutFromKeyboardDelegate;
 7179        }
 180
 181        public void StartDectectingMovement()
 182        {
 0183            isDetectingMovement = true;
 0184            hasBeenMovement = false;
 0185        }
 186
 0187        public bool HasBeenMovement => hasBeenMovement;
 188
 0189        public void StopDetectingMovement() { isDetectingMovement = false; }
 190
 191        private void OnInputMouseUp(int buttonId, Vector3 mousePosition)
 192        {
 0193            if (buttonId != 1)
 0194                return;
 195
 0196            isMouseRightClickDown = false;
 0197        }
 198
 199        private void OnInputMouseDown(int buttonId, Vector3 mousePosition)
 200        {
 0201            if (buttonId != 1)
 0202                return;
 203
 0204            isMouseRightClickDown = true;
 0205            direction = Vector3.zero;
 0206        }
 207
 208        private void OnDestroy()
 209        {
 7210            BuilderInWorldInputWrapper.OnMouseDrag -= MouseDrag;
 7211            BuilderInWorldInputWrapper.OnMouseDragRaw -= MouseDragRaw;
 7212            BuilderInWorldInputWrapper.OnMouseWheel -= MouseWheel;
 213
 7214            BuilderInWorldInputWrapper.OnMouseDown -= OnInputMouseDown;
 7215            BuilderInWorldInputWrapper.OnMouseUp -= OnInputMouseUp;
 216
 7217            advanceFowardInputAction.OnStarted -= advanceForwardStartDelegate;
 7218            advanceFowardInputAction.OnFinished -= advanceForwardFinishedDelegate;
 219
 7220            advanceBackInputAction.OnStarted -= advanceBackStartDelegate;
 7221            advanceBackInputAction.OnFinished -= advanceBackFinishedDelegate;
 222
 7223            advanceLeftInputAction.OnStarted -= advanceLeftStartDelegate;
 7224            advanceLeftInputAction.OnFinished -= advanceLeftFinishedDelegate;
 225
 7226            advanceRightInputAction.OnStarted -= advanceRightStartDelegate;
 7227            advanceRightInputAction.OnFinished -= advanceRightFinishedDelegate;
 228
 7229            advanceDownInputAction.OnStarted -= advanceDownStartDelegate;
 7230            advanceDownInputAction.OnFinished -= advanceDownFinishedDelegate;
 231
 7232            advanceUpInputAction.OnStarted -= advanceUpStartDelegate;
 7233            advanceUpInputAction.OnFinished -= advanceUpFinishedDelegate;
 234
 7235            cameraPanInputAction.OnStarted -= cameraPanStartDelegate;
 7236            cameraPanInputAction.OnFinished -= cameraPanFinishedDelegate;
 237
 7238            zoomInFromKeyboardInputAction.OnTriggered -= zoomInFromKeyboardDelegate;
 7239            zoomOutFromKeyboardInputAction.OnTriggered -= zoomOutFromKeyboardDelegate;
 7240        }
 241
 242        private void Update()
 243        {
 473244            HandleCameraLook();
 473245            HandleCameraPan();
 473246            HandleCameraMovement();
 473247        }
 248
 249        #region CameraTransformChanges
 250
 251        private void HandleCameraMovement()
 252        {
 473253            HandleCameraMovementInput();
 473254            if (direction.magnitude >= CAMERA_MOVEMENT_DEACTIVATE_INPUT_MAGNITUD)
 0255                nextTranslation = direction;
 473256            nextTranslation = Vector3.Lerp(nextTranslation, Vector3.zero, lerpTime * Time.deltaTime);
 257
 473258            if (nextTranslation.magnitude >= CAMERA_MOVEMENT_THRESHOLD)
 0259                transform.Translate(nextTranslation * (movementSpeed * Time.deltaTime), Space.Self);
 473260        }
 261
 262        private void HandleCameraLook()
 263        {
 473264            Quaternion nextIteration =  Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(pitch, yaw, 0f)
 265
 473266            if (Mathf.Abs(nextIteration.eulerAngles.magnitude - transform.rotation.eulerAngles.magnitude) >= CAMERA_ANGL
 0267                transform.rotation = nextIteration;
 473268        }
 269
 270        private void HandleCameraPan()
 271        {
 473272            panAxisX = Mathf.Lerp(panAxisX, 0, cameraPanAdvance * Time.deltaTime);
 473273            panAxisY = Mathf.Lerp(panAxisY, 0, cameraPanAdvance * Time.deltaTime);
 274
 473275            if (Mathf.Abs(panAxisX) >= CAMERA_PAN_THRESHOLD || Mathf.Abs(panAxisY) >= CAMERA_PAN_THRESHOLD)
 0276                transform.Translate(panAxisX, panAxisY, 0);
 473277        }
 278
 279        #endregion
 280
 281        private void HandleCameraMovementInput()
 282        {
 473283            int velocityChangedCount = 0;
 473284            if (isAdvancingForward)
 285            {
 0286                cameraVelocity += GetTotalVelocity(Vector3.forward);
 0287                velocityChangedCount++;
 288            }
 289
 473290            if (isAdvancingBackward)
 291            {
 0292                cameraVelocity += GetTotalVelocity(Vector3.back);
 0293                velocityChangedCount++;
 294            }
 295
 473296            if (!isAdvancingBackward && !isAdvancingForward)
 473297                cameraVelocity.z = Mathf.Lerp(cameraVelocity.z, 0, lerpDeccelerationTime);
 298
 473299            if (isAdvancingRight)
 300            {
 0301                cameraVelocity += GetTotalVelocity(Vector3.right) * xPlaneSpeedPercentCompensantion;
 0302                velocityChangedCount++;
 303            }
 304
 473305            if (isAdvancingLeft)
 306            {
 0307                cameraVelocity += GetTotalVelocity(Vector3.left) * xPlaneSpeedPercentCompensantion;
 0308                velocityChangedCount++;
 309            }
 310
 473311            if (!isAdvancingRight && !isAdvancingLeft)
 473312                cameraVelocity.x = Mathf.Lerp(cameraVelocity.x, 0, lerpDeccelerationTime);
 313
 473314            if (isAdvancingUp)
 315            {
 0316                cameraVelocity += GetTotalVelocity(Vector3.up);
 0317                velocityChangedCount++;
 318            }
 319
 473320            if (isAdvancingDown)
 321            {
 0322                cameraVelocity += GetTotalVelocity(Vector3.down);
 0323                velocityChangedCount++;
 324            }
 325
 473326            if (!isAdvancingUp && !isAdvancingDown)
 473327                cameraVelocity.y = Mathf.Lerp(cameraVelocity.y, 0, lerpDeccelerationTime);
 328
 473329            if (velocityChangedCount != 0)
 0330                cameraVelocity = Vector3.ClampMagnitude(cameraVelocity, speedClamp);
 331
 473332            direction = cameraVelocity;
 473333        }
 334
 335        private Vector3 GetTotalVelocity(Vector3 velocityToAdd)
 336        {
 0337            if (!isMouseRightClickDown)
 0338                return  Vector3.zero;
 339
 0340            if (isDetectingMovement)
 0341                hasBeenMovement = true;
 0342            return velocityToAdd * (initalAcceleration * Time.deltaTime);
 343        }
 344
 0345        public void SetCameraCanMove(bool canMove) { isCameraAbleToMove = canMove; }
 346
 0347        private void OnGizmoTransformObjectEnd(string gizmoType) { isCameraAbleToMove = true; }
 348
 0349        private void OnGizmoTransformObjectStart(string gizmoType) { isCameraAbleToMove = false; }
 350
 351        private void MouseWheel(float axis)
 352        {
 0353            if (!isCameraAbleToMove)
 0354                return;
 355
 0356            if (smoothScrollCor != null)
 0357                CoroutineStarter.Stop(smoothScrollCor);
 358
 0359            float delta = Time.time - lastMouseWheelTime;
 0360            float scrollValue = axis * Mathf.Clamp01(delta);
 0361            lastMouseWheelTime = Time.time;
 362
 0363            smoothScrollCor = CoroutineStarter.Start(SmoothScroll(scrollValue));
 0364        }
 365
 366        private void MouseDragRaw(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 367        {
 0368            if (buttonId == 1 && !isPanCameraActive)
 0369                CameraLook(axisX, axisY);
 0370        }
 371
 372        private void MouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY)
 373        {
 0374            if (buttonId == 2 || buttonId == 1 && isPanCameraActive)
 0375                CameraDrag(axisX, axisY);
 0376        }
 377
 378        private void CameraDrag(float axisX, float axisY)
 379        {
 0380            if (!isCameraAbleToMove)
 0381                return;
 382
 0383            panAxisX += -axisX * Time.deltaTime * dragSpeed;
 0384            panAxisY += -axisY * Time.deltaTime * dragSpeed;
 0385            cameraPanAdvance = smoothCameraPanAceleration;
 0386        }
 387
 388        private void CameraLook(float axisX, float axisY)
 389        {
 0390            if (!isCameraAbleToMove || !isMouseRightClickDown)
 0391                return;
 392
 0393            yaw += lookSpeedH * axisX;
 0394            pitch -= lookSpeedV * axisY;
 0395            cameraLookAdvance = smoothCameraLookSpeed * Time.deltaTime;
 0396        }
 397
 0398        public override Vector3 OnGetRotation() { return transform.eulerAngles; }
 399
 400        public void FocusOnEntities(List<DCLBuilderInWorldEntity> entitiesToFocus)
 401        {
 0402            if (entitiesToFocus.Count <= 0)
 0403                return;
 404
 0405            Vector3 middlePoint = FindMidPoint(entitiesToFocus);
 0406            if (Vector3.positiveInfinity == middlePoint ||
 407                Vector3.negativeInfinity == middlePoint ||
 408                float.IsNaN(middlePoint.x) ||
 409                float.IsNaN(middlePoint.y) ||
 410                float.IsNaN(middlePoint.z))
 0411                return;
 412
 0413            if (smoothFocusOnTargetCor != null)
 0414                CoroutineStarter.Stop(smoothFocusOnTargetCor);
 0415            smoothFocusOnTargetCor = CoroutineStarter.Start(SmoothFocusOnTarget(middlePoint));
 0416            SmoothLookAt(middlePoint);
 0417        }
 418
 8419        public void SetPosition(Vector3 position) { transform.position = position; }
 420
 421        public void LookAt(Transform transformToLookAt)
 422        {
 4423            transform.LookAt(transformToLookAt);
 4424            yaw = transform.eulerAngles.y;
 4425            pitch = transform.eulerAngles.x;
 4426        }
 427
 0428        public void SmoothLookAt(Transform transformToLookAt) { SmoothLookAt(transformToLookAt.position); }
 429
 430        public void SmoothLookAt(Vector3 position)
 431        {
 0432            if (smoothLookAtCor != null)
 0433                CoroutineStarter.Stop(smoothLookAtCor);
 0434            smoothLookAtCor = CoroutineStarter.Start(SmoothLookAtCorutine(position));
 0435        }
 436
 437        Vector3 FindMidPoint(List<DCLBuilderInWorldEntity> entitiesToLook)
 438        {
 0439            Vector3 finalPosition = Vector3.zero;
 0440            int totalPoints = 0;
 0441            foreach (DCLBuilderInWorldEntity entity in entitiesToLook)
 442            {
 0443                if (entity.rootEntity.meshRootGameObject && entity.rootEntity.meshesInfo.renderers.Length > 0)
 444                {
 0445                    Vector3 midPointFromEntity = Vector3.zero;
 0446                    foreach (Renderer render in entity.rootEntity.renderers)
 447                    {
 0448                        if (render == null)
 449                            continue;
 0450                        midPointFromEntity += render.bounds.center;
 451                    }
 452
 0453                    midPointFromEntity /= entity.rootEntity.renderers.Length;
 0454                    finalPosition += midPointFromEntity;
 0455                    totalPoints++;
 456                }
 457            }
 458
 0459            finalPosition /= totalPoints;
 0460            return finalPosition;
 461        }
 462
 463        IEnumerator SmoothScroll(float axis)
 464        {
 0465            float scrollMovementDestination = axis * zoomSpeed;
 466
 0467            Vector3 targetPosition = transform.position + transform.TransformDirection(Vector3.forward * scrollMovementD
 468
 0469            float advance = 0;
 0470            while (advance <= 1)
 471            {
 0472                advance += smoothLookAtSpeed * Time.deltaTime;
 0473                Vector3 result = Vector3.Lerp(transform.position, targetPosition, advance);
 0474                transform.position = result;
 0475                yield return null;
 476            }
 0477        }
 478
 479        IEnumerator SmoothFocusOnTarget(Vector3 targetPosition)
 480        {
 0481            float advance = 0;
 0482            while (advance <= 1)
 483            {
 0484                advance += smoothLookAtSpeed * Time.deltaTime;
 0485                transform.position = Vector3.Lerp(transform.position, targetPosition, advance);
 0486                if (Vector3.Distance(transform.position, targetPosition) <= focusDistance)
 0487                    yield break;
 0488                yield return null;
 489            }
 0490        }
 491
 492        IEnumerator SmoothLookAtCorutine(Vector3 targetPosition)
 493        {
 0494            Quaternion targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
 0495            float advance = 0;
 0496            while (advance <= 1)
 497            {
 0498                advance += smoothLookAtSpeed * Time.deltaTime;
 0499                transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, advance);
 0500                yield return null;
 501            }
 502
 0503            yaw = transform.eulerAngles.y;
 0504            pitch = transform.eulerAngles.x;
 0505        }
 506
 507        public void SetResetConfiguration(Vector3 position, Transform lookAt)
 508        {
 0509            originalCameraPosition = position;
 0510            originalCameraLookAt = lookAt;
 0511        }
 512
 513        public void ResetCameraPosition()
 514        {
 0515            SetPosition(originalCameraPosition);
 0516            LookAt(originalCameraLookAt);
 0517            direction = Vector3.zero;
 0518        }
 519
 0520        public void TakeSceneScreenshot(OnSnapshotsReady onSuccess) { StartCoroutine(TakeSceneScreenshotCoroutine(onSucc
 521
 522        private IEnumerator TakeSceneScreenshotCoroutine(OnSnapshotsReady callback)
 523        {
 0524            var current = camera.targetTexture;
 0525            camera.targetTexture = null;
 526
 0527            yield return null;
 528
 0529            Texture2D sceneScreenshot = ScreenshotFromCamera(SCENE_SNAPSHOT_WIDTH_RES, SCENE_SNAPSHOT_HEIGHT_RES);
 0530            camera.targetTexture = current;
 0531            callback?.Invoke(sceneScreenshot);
 0532        }
 533
 0534        public void TakeSceneScreenshotFromResetPosition(OnSnapshotsReady onSuccess) { StartCoroutine(TakeSceneScreensho
 535
 536        private IEnumerator TakeSceneScreenshotFromResetPositionCoroutine(OnSnapshotsReady callback)
 537        {
 538            // Store current camera position/direction
 0539            Vector3 currentPos = transform.position;
 0540            Vector3 currentLookAt = transform.forward;
 0541            SetPosition(originalCameraPosition);
 0542            transform.LookAt(originalCameraLookAt);
 543
 0544            var current = camera.targetTexture;
 0545            camera.targetTexture = null;
 546
 0547            yield return null;
 548
 0549            Texture2D sceneScreenshot = ScreenshotFromCamera(SCENE_SNAPSHOT_WIDTH_RES, SCENE_SNAPSHOT_HEIGHT_RES);
 0550            camera.targetTexture = current;
 0551            callback?.Invoke(sceneScreenshot);
 552
 553            // Restore camera position/direction after the screenshot
 0554            SetPosition(currentPos);
 0555            transform.forward = currentLookAt;
 0556        }
 557
 558        private Texture2D ScreenshotFromCamera(int width, int height)
 559        {
 0560            RenderTexture rt = new RenderTexture(width, height, 32);
 0561            camera.targetTexture = rt;
 0562            Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
 0563            camera.Render();
 0564            RenderTexture.active = rt;
 0565            screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
 0566            screenShot.Apply();
 567
 0568            return screenShot;
 569        }
 570    }
 571}