| | 1 | | using Builder.Gizmos; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace 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 | |
|
| 314 | 19 | | public float focusDistance = 5f; |
| | 20 | |
|
| | 21 | | [Header("Camera Movement")] |
| 314 | 22 | | public float xPlaneSpeedPercentCompensantion = 0.85f; |
| | 23 | |
|
| 314 | 24 | | public float movementSpeed = 5f; |
| 314 | 25 | | public float lerpTime = 0.3F; |
| | 26 | |
|
| 314 | 27 | | public float lerpDeccelerationTime = 0.3F; |
| 314 | 28 | | public float initalAcceleration = 2f; |
| 314 | 29 | | public float speedClamp = 1f; |
| | 30 | |
|
| | 31 | | [Header("Camera Look")] |
| 314 | 32 | | public float smoothLookAtSpeed = 5f; |
| | 33 | |
|
| 314 | 34 | | public float smoothCameraLookSpeed = 5f; |
| 314 | 35 | | public float lookSpeedH = 2f; |
| 314 | 36 | | public float lookSpeedV = 2f; |
| | 37 | |
|
| | 38 | | [Header("Camera Pan")] |
| 314 | 39 | | public float smoothCameraPanAceleration = 5f; |
| | 40 | |
|
| 314 | 41 | | public float dragSpeed = 3f; |
| | 42 | |
|
| | 43 | | [Header("Camera Zoom")] |
| 314 | 44 | | 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 | |
|
| 314 | 58 | | 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 | |
|
| 314 | 66 | | 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; |
| 314 | 111 | | 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 | | { |
| 7 | 122 | | BuilderInWorldInputWrapper.OnMouseDrag += MouseDrag; |
| 7 | 123 | | BuilderInWorldInputWrapper.OnMouseDragRaw += MouseDragRaw; |
| 7 | 124 | | BuilderInWorldInputWrapper.OnMouseWheel += MouseWheel; |
| | 125 | |
|
| 7 | 126 | | BuilderInWorldInputWrapper.OnMouseDown += OnInputMouseDown; |
| 7 | 127 | | BuilderInWorldInputWrapper.OnMouseUp += OnInputMouseUp; |
| | 128 | |
|
| 7 | 129 | | DCLBuilderGizmoManager.OnGizmoTransformObjectStart += OnGizmoTransformObjectStart; |
| 7 | 130 | | DCLBuilderGizmoManager.OnGizmoTransformObjectEnd += OnGizmoTransformObjectEnd; |
| | 131 | |
|
| 7 | 132 | | advanceForwardStartDelegate = (action) => isAdvancingForward = true; |
| 7 | 133 | | advanceForwardFinishedDelegate = (action) => isAdvancingForward = false; |
| | 134 | |
|
| 7 | 135 | | advanceFowardInputAction.OnStarted += advanceForwardStartDelegate; |
| 7 | 136 | | advanceFowardInputAction.OnFinished += advanceForwardFinishedDelegate; |
| | 137 | |
|
| 7 | 138 | | advanceBackStartDelegate = (action) => isAdvancingBackward = true; |
| 7 | 139 | | advanceBackFinishedDelegate = (action) => isAdvancingBackward = false; |
| | 140 | |
|
| 7 | 141 | | advanceBackInputAction.OnStarted += advanceBackStartDelegate; |
| 7 | 142 | | advanceBackInputAction.OnFinished += advanceBackFinishedDelegate; |
| | 143 | |
|
| 7 | 144 | | advanceLeftStartDelegate = (action) => isAdvancingLeft = true; |
| 7 | 145 | | advanceLeftFinishedDelegate = (action) => isAdvancingLeft = false; |
| | 146 | |
|
| 7 | 147 | | advanceLeftInputAction.OnStarted += advanceLeftStartDelegate; |
| 7 | 148 | | advanceLeftInputAction.OnFinished += advanceLeftFinishedDelegate; |
| | 149 | |
|
| 7 | 150 | | advanceRightStartDelegate = (action) => isAdvancingRight = true; |
| 7 | 151 | | advanceRightFinishedDelegate = (action) => isAdvancingRight = false; |
| | 152 | |
|
| 7 | 153 | | advanceRightInputAction.OnStarted += advanceRightStartDelegate; |
| 7 | 154 | | advanceRightInputAction.OnFinished += advanceRightFinishedDelegate; |
| | 155 | |
|
| 7 | 156 | | advanceUpStartDelegate = (action) => isAdvancingUp = true; |
| 7 | 157 | | advanceUpFinishedDelegate = (action) => isAdvancingUp = false; |
| | 158 | |
|
| 7 | 159 | | advanceUpInputAction.OnStarted += advanceUpStartDelegate; |
| 7 | 160 | | advanceUpInputAction.OnFinished += advanceUpFinishedDelegate; |
| | 161 | |
|
| 7 | 162 | | advanceDownStartDelegate = (action) => isAdvancingDown = true; |
| 7 | 163 | | advanceDownFinishedDelegate = (action) => isAdvancingDown = false; |
| | 164 | |
|
| 7 | 165 | | advanceDownInputAction.OnStarted += advanceDownStartDelegate; |
| 7 | 166 | | advanceDownInputAction.OnFinished += advanceDownFinishedDelegate; |
| | 167 | |
|
| 7 | 168 | | cameraPanStartDelegate = (action) => isPanCameraActive = true; |
| 7 | 169 | | cameraPanFinishedDelegate = (action) => isPanCameraActive = false; |
| | 170 | |
|
| 7 | 171 | | cameraPanInputAction.OnStarted += cameraPanStartDelegate; |
| 7 | 172 | | cameraPanInputAction.OnFinished += cameraPanFinishedDelegate; |
| | 173 | |
|
| 7 | 174 | | zoomInFromKeyboardDelegate = (action) => MouseWheel(1f); |
| 7 | 175 | | zoomInFromKeyboardInputAction.OnTriggered += zoomInFromKeyboardDelegate; |
| | 176 | |
|
| 7 | 177 | | zoomOutFromKeyboardDelegate = (action) => MouseWheel(-1f); |
| 7 | 178 | | zoomOutFromKeyboardInputAction.OnTriggered += zoomOutFromKeyboardDelegate; |
| 7 | 179 | | } |
| | 180 | |
|
| | 181 | | public void StartDectectingMovement() |
| | 182 | | { |
| 0 | 183 | | isDetectingMovement = true; |
| 0 | 184 | | hasBeenMovement = false; |
| 0 | 185 | | } |
| | 186 | |
|
| 0 | 187 | | public bool HasBeenMovement => hasBeenMovement; |
| | 188 | |
|
| 0 | 189 | | public void StopDetectingMovement() { isDetectingMovement = false; } |
| | 190 | |
|
| | 191 | | private void OnInputMouseUp(int buttonId, Vector3 mousePosition) |
| | 192 | | { |
| 0 | 193 | | if (buttonId != 1) |
| 0 | 194 | | return; |
| | 195 | |
|
| 0 | 196 | | isMouseRightClickDown = false; |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | private void OnInputMouseDown(int buttonId, Vector3 mousePosition) |
| | 200 | | { |
| 0 | 201 | | if (buttonId != 1) |
| 0 | 202 | | return; |
| | 203 | |
|
| 0 | 204 | | isMouseRightClickDown = true; |
| 0 | 205 | | direction = Vector3.zero; |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | private void OnDestroy() |
| | 209 | | { |
| 7 | 210 | | BuilderInWorldInputWrapper.OnMouseDrag -= MouseDrag; |
| 7 | 211 | | BuilderInWorldInputWrapper.OnMouseDragRaw -= MouseDragRaw; |
| 7 | 212 | | BuilderInWorldInputWrapper.OnMouseWheel -= MouseWheel; |
| | 213 | |
|
| 7 | 214 | | BuilderInWorldInputWrapper.OnMouseDown -= OnInputMouseDown; |
| 7 | 215 | | BuilderInWorldInputWrapper.OnMouseUp -= OnInputMouseUp; |
| | 216 | |
|
| 7 | 217 | | advanceFowardInputAction.OnStarted -= advanceForwardStartDelegate; |
| 7 | 218 | | advanceFowardInputAction.OnFinished -= advanceForwardFinishedDelegate; |
| | 219 | |
|
| 7 | 220 | | advanceBackInputAction.OnStarted -= advanceBackStartDelegate; |
| 7 | 221 | | advanceBackInputAction.OnFinished -= advanceBackFinishedDelegate; |
| | 222 | |
|
| 7 | 223 | | advanceLeftInputAction.OnStarted -= advanceLeftStartDelegate; |
| 7 | 224 | | advanceLeftInputAction.OnFinished -= advanceLeftFinishedDelegate; |
| | 225 | |
|
| 7 | 226 | | advanceRightInputAction.OnStarted -= advanceRightStartDelegate; |
| 7 | 227 | | advanceRightInputAction.OnFinished -= advanceRightFinishedDelegate; |
| | 228 | |
|
| 7 | 229 | | advanceDownInputAction.OnStarted -= advanceDownStartDelegate; |
| 7 | 230 | | advanceDownInputAction.OnFinished -= advanceDownFinishedDelegate; |
| | 231 | |
|
| 7 | 232 | | advanceUpInputAction.OnStarted -= advanceUpStartDelegate; |
| 7 | 233 | | advanceUpInputAction.OnFinished -= advanceUpFinishedDelegate; |
| | 234 | |
|
| 7 | 235 | | cameraPanInputAction.OnStarted -= cameraPanStartDelegate; |
| 7 | 236 | | cameraPanInputAction.OnFinished -= cameraPanFinishedDelegate; |
| | 237 | |
|
| 7 | 238 | | zoomInFromKeyboardInputAction.OnTriggered -= zoomInFromKeyboardDelegate; |
| 7 | 239 | | zoomOutFromKeyboardInputAction.OnTriggered -= zoomOutFromKeyboardDelegate; |
| 7 | 240 | | } |
| | 241 | |
|
| | 242 | | private void Update() |
| | 243 | | { |
| 473 | 244 | | HandleCameraLook(); |
| 473 | 245 | | HandleCameraPan(); |
| 473 | 246 | | HandleCameraMovement(); |
| 473 | 247 | | } |
| | 248 | |
|
| | 249 | | #region CameraTransformChanges |
| | 250 | |
|
| | 251 | | private void HandleCameraMovement() |
| | 252 | | { |
| 473 | 253 | | HandleCameraMovementInput(); |
| 473 | 254 | | if (direction.magnitude >= CAMERA_MOVEMENT_DEACTIVATE_INPUT_MAGNITUD) |
| 0 | 255 | | nextTranslation = direction; |
| 473 | 256 | | nextTranslation = Vector3.Lerp(nextTranslation, Vector3.zero, lerpTime * Time.deltaTime); |
| | 257 | |
|
| 473 | 258 | | if (nextTranslation.magnitude >= CAMERA_MOVEMENT_THRESHOLD) |
| 0 | 259 | | transform.Translate(nextTranslation * (movementSpeed * Time.deltaTime), Space.Self); |
| 473 | 260 | | } |
| | 261 | |
|
| | 262 | | private void HandleCameraLook() |
| | 263 | | { |
| 473 | 264 | | Quaternion nextIteration = Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(pitch, yaw, 0f) |
| | 265 | |
|
| 473 | 266 | | if (Mathf.Abs(nextIteration.eulerAngles.magnitude - transform.rotation.eulerAngles.magnitude) >= CAMERA_ANGL |
| 0 | 267 | | transform.rotation = nextIteration; |
| 473 | 268 | | } |
| | 269 | |
|
| | 270 | | private void HandleCameraPan() |
| | 271 | | { |
| 473 | 272 | | panAxisX = Mathf.Lerp(panAxisX, 0, cameraPanAdvance * Time.deltaTime); |
| 473 | 273 | | panAxisY = Mathf.Lerp(panAxisY, 0, cameraPanAdvance * Time.deltaTime); |
| | 274 | |
|
| 473 | 275 | | if (Mathf.Abs(panAxisX) >= CAMERA_PAN_THRESHOLD || Mathf.Abs(panAxisY) >= CAMERA_PAN_THRESHOLD) |
| 0 | 276 | | transform.Translate(panAxisX, panAxisY, 0); |
| 473 | 277 | | } |
| | 278 | |
|
| | 279 | | #endregion |
| | 280 | |
|
| | 281 | | private void HandleCameraMovementInput() |
| | 282 | | { |
| 473 | 283 | | int velocityChangedCount = 0; |
| 473 | 284 | | if (isAdvancingForward) |
| | 285 | | { |
| 0 | 286 | | cameraVelocity += GetTotalVelocity(Vector3.forward); |
| 0 | 287 | | velocityChangedCount++; |
| | 288 | | } |
| | 289 | |
|
| 473 | 290 | | if (isAdvancingBackward) |
| | 291 | | { |
| 0 | 292 | | cameraVelocity += GetTotalVelocity(Vector3.back); |
| 0 | 293 | | velocityChangedCount++; |
| | 294 | | } |
| | 295 | |
|
| 473 | 296 | | if (!isAdvancingBackward && !isAdvancingForward) |
| 473 | 297 | | cameraVelocity.z = Mathf.Lerp(cameraVelocity.z, 0, lerpDeccelerationTime); |
| | 298 | |
|
| 473 | 299 | | if (isAdvancingRight) |
| | 300 | | { |
| 0 | 301 | | cameraVelocity += GetTotalVelocity(Vector3.right) * xPlaneSpeedPercentCompensantion; |
| 0 | 302 | | velocityChangedCount++; |
| | 303 | | } |
| | 304 | |
|
| 473 | 305 | | if (isAdvancingLeft) |
| | 306 | | { |
| 0 | 307 | | cameraVelocity += GetTotalVelocity(Vector3.left) * xPlaneSpeedPercentCompensantion; |
| 0 | 308 | | velocityChangedCount++; |
| | 309 | | } |
| | 310 | |
|
| 473 | 311 | | if (!isAdvancingRight && !isAdvancingLeft) |
| 473 | 312 | | cameraVelocity.x = Mathf.Lerp(cameraVelocity.x, 0, lerpDeccelerationTime); |
| | 313 | |
|
| 473 | 314 | | if (isAdvancingUp) |
| | 315 | | { |
| 0 | 316 | | cameraVelocity += GetTotalVelocity(Vector3.up); |
| 0 | 317 | | velocityChangedCount++; |
| | 318 | | } |
| | 319 | |
|
| 473 | 320 | | if (isAdvancingDown) |
| | 321 | | { |
| 0 | 322 | | cameraVelocity += GetTotalVelocity(Vector3.down); |
| 0 | 323 | | velocityChangedCount++; |
| | 324 | | } |
| | 325 | |
|
| 473 | 326 | | if (!isAdvancingUp && !isAdvancingDown) |
| 473 | 327 | | cameraVelocity.y = Mathf.Lerp(cameraVelocity.y, 0, lerpDeccelerationTime); |
| | 328 | |
|
| 473 | 329 | | if (velocityChangedCount != 0) |
| 0 | 330 | | cameraVelocity = Vector3.ClampMagnitude(cameraVelocity, speedClamp); |
| | 331 | |
|
| 473 | 332 | | direction = cameraVelocity; |
| 473 | 333 | | } |
| | 334 | |
|
| | 335 | | private Vector3 GetTotalVelocity(Vector3 velocityToAdd) |
| | 336 | | { |
| 0 | 337 | | if (!isMouseRightClickDown) |
| 0 | 338 | | return Vector3.zero; |
| | 339 | |
|
| 0 | 340 | | if (isDetectingMovement) |
| 0 | 341 | | hasBeenMovement = true; |
| 0 | 342 | | return velocityToAdd * (initalAcceleration * Time.deltaTime); |
| | 343 | | } |
| | 344 | |
|
| 0 | 345 | | public void SetCameraCanMove(bool canMove) { isCameraAbleToMove = canMove; } |
| | 346 | |
|
| 0 | 347 | | private void OnGizmoTransformObjectEnd(string gizmoType) { isCameraAbleToMove = true; } |
| | 348 | |
|
| 0 | 349 | | private void OnGizmoTransformObjectStart(string gizmoType) { isCameraAbleToMove = false; } |
| | 350 | |
|
| | 351 | | private void MouseWheel(float axis) |
| | 352 | | { |
| 0 | 353 | | if (!isCameraAbleToMove) |
| 0 | 354 | | return; |
| | 355 | |
|
| 0 | 356 | | if (smoothScrollCor != null) |
| 0 | 357 | | CoroutineStarter.Stop(smoothScrollCor); |
| | 358 | |
|
| 0 | 359 | | float delta = Time.time - lastMouseWheelTime; |
| 0 | 360 | | float scrollValue = axis * Mathf.Clamp01(delta); |
| 0 | 361 | | lastMouseWheelTime = Time.time; |
| | 362 | |
|
| 0 | 363 | | smoothScrollCor = CoroutineStarter.Start(SmoothScroll(scrollValue)); |
| 0 | 364 | | } |
| | 365 | |
|
| | 366 | | private void MouseDragRaw(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 367 | | { |
| 0 | 368 | | if (buttonId == 1 && !isPanCameraActive) |
| 0 | 369 | | CameraLook(axisX, axisY); |
| 0 | 370 | | } |
| | 371 | |
|
| | 372 | | private void MouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 373 | | { |
| 0 | 374 | | if (buttonId == 2 || buttonId == 1 && isPanCameraActive) |
| 0 | 375 | | CameraDrag(axisX, axisY); |
| 0 | 376 | | } |
| | 377 | |
|
| | 378 | | private void CameraDrag(float axisX, float axisY) |
| | 379 | | { |
| 0 | 380 | | if (!isCameraAbleToMove) |
| 0 | 381 | | return; |
| | 382 | |
|
| 0 | 383 | | panAxisX += -axisX * Time.deltaTime * dragSpeed; |
| 0 | 384 | | panAxisY += -axisY * Time.deltaTime * dragSpeed; |
| 0 | 385 | | cameraPanAdvance = smoothCameraPanAceleration; |
| 0 | 386 | | } |
| | 387 | |
|
| | 388 | | private void CameraLook(float axisX, float axisY) |
| | 389 | | { |
| 0 | 390 | | if (!isCameraAbleToMove || !isMouseRightClickDown) |
| 0 | 391 | | return; |
| | 392 | |
|
| 0 | 393 | | yaw += lookSpeedH * axisX; |
| 0 | 394 | | pitch -= lookSpeedV * axisY; |
| 0 | 395 | | cameraLookAdvance = smoothCameraLookSpeed * Time.deltaTime; |
| 0 | 396 | | } |
| | 397 | |
|
| 0 | 398 | | public override Vector3 OnGetRotation() { return transform.eulerAngles; } |
| | 399 | |
|
| | 400 | | public void FocusOnEntities(List<DCLBuilderInWorldEntity> entitiesToFocus) |
| | 401 | | { |
| 0 | 402 | | if (entitiesToFocus.Count <= 0) |
| 0 | 403 | | return; |
| | 404 | |
|
| 0 | 405 | | Vector3 middlePoint = FindMidPoint(entitiesToFocus); |
| 0 | 406 | | if (Vector3.positiveInfinity == middlePoint || |
| | 407 | | Vector3.negativeInfinity == middlePoint || |
| | 408 | | float.IsNaN(middlePoint.x) || |
| | 409 | | float.IsNaN(middlePoint.y) || |
| | 410 | | float.IsNaN(middlePoint.z)) |
| 0 | 411 | | return; |
| | 412 | |
|
| 0 | 413 | | if (smoothFocusOnTargetCor != null) |
| 0 | 414 | | CoroutineStarter.Stop(smoothFocusOnTargetCor); |
| 0 | 415 | | smoothFocusOnTargetCor = CoroutineStarter.Start(SmoothFocusOnTarget(middlePoint)); |
| 0 | 416 | | SmoothLookAt(middlePoint); |
| 0 | 417 | | } |
| | 418 | |
|
| 8 | 419 | | public void SetPosition(Vector3 position) { transform.position = position; } |
| | 420 | |
|
| | 421 | | public void LookAt(Transform transformToLookAt) |
| | 422 | | { |
| 4 | 423 | | transform.LookAt(transformToLookAt); |
| 4 | 424 | | yaw = transform.eulerAngles.y; |
| 4 | 425 | | pitch = transform.eulerAngles.x; |
| 4 | 426 | | } |
| | 427 | |
|
| 0 | 428 | | public void SmoothLookAt(Transform transformToLookAt) { SmoothLookAt(transformToLookAt.position); } |
| | 429 | |
|
| | 430 | | public void SmoothLookAt(Vector3 position) |
| | 431 | | { |
| 0 | 432 | | if (smoothLookAtCor != null) |
| 0 | 433 | | CoroutineStarter.Stop(smoothLookAtCor); |
| 0 | 434 | | smoothLookAtCor = CoroutineStarter.Start(SmoothLookAtCorutine(position)); |
| 0 | 435 | | } |
| | 436 | |
|
| | 437 | | Vector3 FindMidPoint(List<DCLBuilderInWorldEntity> entitiesToLook) |
| | 438 | | { |
| 0 | 439 | | Vector3 finalPosition = Vector3.zero; |
| 0 | 440 | | int totalPoints = 0; |
| 0 | 441 | | foreach (DCLBuilderInWorldEntity entity in entitiesToLook) |
| | 442 | | { |
| 0 | 443 | | if (entity.rootEntity.meshRootGameObject && entity.rootEntity.meshesInfo.renderers.Length > 0) |
| | 444 | | { |
| 0 | 445 | | Vector3 midPointFromEntity = Vector3.zero; |
| 0 | 446 | | foreach (Renderer render in entity.rootEntity.renderers) |
| | 447 | | { |
| 0 | 448 | | if (render == null) |
| | 449 | | continue; |
| 0 | 450 | | midPointFromEntity += render.bounds.center; |
| | 451 | | } |
| | 452 | |
|
| 0 | 453 | | midPointFromEntity /= entity.rootEntity.renderers.Length; |
| 0 | 454 | | finalPosition += midPointFromEntity; |
| 0 | 455 | | totalPoints++; |
| | 456 | | } |
| | 457 | | } |
| | 458 | |
|
| 0 | 459 | | finalPosition /= totalPoints; |
| 0 | 460 | | return finalPosition; |
| | 461 | | } |
| | 462 | |
|
| | 463 | | IEnumerator SmoothScroll(float axis) |
| | 464 | | { |
| 0 | 465 | | float scrollMovementDestination = axis * zoomSpeed; |
| | 466 | |
|
| 0 | 467 | | Vector3 targetPosition = transform.position + transform.TransformDirection(Vector3.forward * scrollMovementD |
| | 468 | |
|
| 0 | 469 | | float advance = 0; |
| 0 | 470 | | while (advance <= 1) |
| | 471 | | { |
| 0 | 472 | | advance += smoothLookAtSpeed * Time.deltaTime; |
| 0 | 473 | | Vector3 result = Vector3.Lerp(transform.position, targetPosition, advance); |
| 0 | 474 | | transform.position = result; |
| 0 | 475 | | yield return null; |
| | 476 | | } |
| 0 | 477 | | } |
| | 478 | |
|
| | 479 | | IEnumerator SmoothFocusOnTarget(Vector3 targetPosition) |
| | 480 | | { |
| 0 | 481 | | float advance = 0; |
| 0 | 482 | | while (advance <= 1) |
| | 483 | | { |
| 0 | 484 | | advance += smoothLookAtSpeed * Time.deltaTime; |
| 0 | 485 | | transform.position = Vector3.Lerp(transform.position, targetPosition, advance); |
| 0 | 486 | | if (Vector3.Distance(transform.position, targetPosition) <= focusDistance) |
| 0 | 487 | | yield break; |
| 0 | 488 | | yield return null; |
| | 489 | | } |
| 0 | 490 | | } |
| | 491 | |
|
| | 492 | | IEnumerator SmoothLookAtCorutine(Vector3 targetPosition) |
| | 493 | | { |
| 0 | 494 | | Quaternion targetRotation = Quaternion.LookRotation(targetPosition - transform.position); |
| 0 | 495 | | float advance = 0; |
| 0 | 496 | | while (advance <= 1) |
| | 497 | | { |
| 0 | 498 | | advance += smoothLookAtSpeed * Time.deltaTime; |
| 0 | 499 | | transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, advance); |
| 0 | 500 | | yield return null; |
| | 501 | | } |
| | 502 | |
|
| 0 | 503 | | yaw = transform.eulerAngles.y; |
| 0 | 504 | | pitch = transform.eulerAngles.x; |
| 0 | 505 | | } |
| | 506 | |
|
| | 507 | | public void SetResetConfiguration(Vector3 position, Transform lookAt) |
| | 508 | | { |
| 0 | 509 | | originalCameraPosition = position; |
| 0 | 510 | | originalCameraLookAt = lookAt; |
| 0 | 511 | | } |
| | 512 | |
|
| | 513 | | public void ResetCameraPosition() |
| | 514 | | { |
| 0 | 515 | | SetPosition(originalCameraPosition); |
| 0 | 516 | | LookAt(originalCameraLookAt); |
| 0 | 517 | | direction = Vector3.zero; |
| 0 | 518 | | } |
| | 519 | |
|
| 0 | 520 | | public void TakeSceneScreenshot(OnSnapshotsReady onSuccess) { StartCoroutine(TakeSceneScreenshotCoroutine(onSucc |
| | 521 | |
|
| | 522 | | private IEnumerator TakeSceneScreenshotCoroutine(OnSnapshotsReady callback) |
| | 523 | | { |
| 0 | 524 | | var current = camera.targetTexture; |
| 0 | 525 | | camera.targetTexture = null; |
| | 526 | |
|
| 0 | 527 | | yield return null; |
| | 528 | |
|
| 0 | 529 | | Texture2D sceneScreenshot = ScreenshotFromCamera(SCENE_SNAPSHOT_WIDTH_RES, SCENE_SNAPSHOT_HEIGHT_RES); |
| 0 | 530 | | camera.targetTexture = current; |
| 0 | 531 | | callback?.Invoke(sceneScreenshot); |
| 0 | 532 | | } |
| | 533 | |
|
| 0 | 534 | | public void TakeSceneScreenshotFromResetPosition(OnSnapshotsReady onSuccess) { StartCoroutine(TakeSceneScreensho |
| | 535 | |
|
| | 536 | | private IEnumerator TakeSceneScreenshotFromResetPositionCoroutine(OnSnapshotsReady callback) |
| | 537 | | { |
| | 538 | | // Store current camera position/direction |
| 0 | 539 | | Vector3 currentPos = transform.position; |
| 0 | 540 | | Vector3 currentLookAt = transform.forward; |
| 0 | 541 | | SetPosition(originalCameraPosition); |
| 0 | 542 | | transform.LookAt(originalCameraLookAt); |
| | 543 | |
|
| 0 | 544 | | var current = camera.targetTexture; |
| 0 | 545 | | camera.targetTexture = null; |
| | 546 | |
|
| 0 | 547 | | yield return null; |
| | 548 | |
|
| 0 | 549 | | Texture2D sceneScreenshot = ScreenshotFromCamera(SCENE_SNAPSHOT_WIDTH_RES, SCENE_SNAPSHOT_HEIGHT_RES); |
| 0 | 550 | | camera.targetTexture = current; |
| 0 | 551 | | callback?.Invoke(sceneScreenshot); |
| | 552 | |
|
| | 553 | | // Restore camera position/direction after the screenshot |
| 0 | 554 | | SetPosition(currentPos); |
| 0 | 555 | | transform.forward = currentLookAt; |
| 0 | 556 | | } |
| | 557 | |
|
| | 558 | | private Texture2D ScreenshotFromCamera(int width, int height) |
| | 559 | | { |
| 0 | 560 | | RenderTexture rt = new RenderTexture(width, height, 32); |
| 0 | 561 | | camera.targetTexture = rt; |
| 0 | 562 | | Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false); |
| 0 | 563 | | camera.Render(); |
| 0 | 564 | | RenderTexture.active = rt; |
| 0 | 565 | | screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); |
| 0 | 566 | | screenShot.Apply(); |
| | 567 | |
|
| 0 | 568 | | return screenShot; |
| | 569 | | } |
| | 570 | | } |
| | 571 | | } |