| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.SettingsCommon; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Camera |
| | 8 | | { |
| | 9 | | public class FreeCameraMovement : CameraStateBase, IFreeCameraMovement |
| | 10 | | { |
| | 11 | | private const float CAMERA_ANGLE_THRESHOLD = 0.01f; |
| | 12 | | private const float CAMERA_PAN_THRESHOLD = 0.001f; |
| | 13 | | private const float CAMERA_MOVEMENT_THRESHOLD = 0.001f; |
| | 14 | |
|
| | 15 | | private const float CAMERA_MOVEMENT_DEACTIVATE_INPUT_MAGNITUD = 0.001f; |
| | 16 | |
|
| | 17 | | private const int SCENE_SNAPSHOT_WIDTH_RES = 854; |
| | 18 | | private const int SCENE_SNAPSHOT_HEIGHT_RES = 480; |
| | 19 | |
|
| 612 | 20 | | public float focusDistance = 5f; |
| | 21 | |
|
| | 22 | | [Header("Camera Movement")] |
| 612 | 23 | | public float xPlaneSpeedPercentCompensantion = 0.85f; |
| | 24 | |
|
| 612 | 25 | | public float movementSpeed = 5f; |
| 612 | 26 | | public float lerpTime = 0.3F; |
| | 27 | |
|
| 612 | 28 | | public float lerpDeccelerationTime = 0.3F; |
| 612 | 29 | | public float initalAcceleration = 2f; |
| 612 | 30 | | public float speedClamp = 1f; |
| | 31 | |
|
| | 32 | | [Header("Camera Look")] |
| 612 | 33 | | public float smoothLookAtSpeed = 5f; |
| | 34 | |
|
| 612 | 35 | | public float smoothCameraLookSpeed = 5f; |
| 612 | 36 | | public float lookSpeedH = 2f; |
| 612 | 37 | | public float lookSpeedV = 2f; |
| | 38 | |
|
| | 39 | | [Header("Camera Pan")] |
| 612 | 40 | | public float smoothCameraPanAceleration = 5f; |
| | 41 | |
|
| 612 | 42 | | public float dragSpeed = 3f; |
| | 43 | |
|
| | 44 | | [Header("Camera Zoom")] |
| 612 | 45 | | public float zoomSpeed = 2f; |
| | 46 | |
|
| | 47 | | [Header("InputActions")] |
| | 48 | | [SerializeField] internal InputAction_Hold advanceFowardInputAction; |
| | 49 | |
|
| | 50 | | [SerializeField] internal InputAction_Hold advanceBackInputAction; |
| | 51 | | [SerializeField] internal InputAction_Hold advanceLeftInputAction; |
| | 52 | | [SerializeField] internal InputAction_Hold advanceRightInputAction; |
| | 53 | | [SerializeField] internal InputAction_Hold advanceUpInputAction; |
| | 54 | | [SerializeField] internal InputAction_Hold advanceDownInputAction; |
| | 55 | | [SerializeField] internal InputAction_Hold cameraPanInputAction; |
| | 56 | | [SerializeField] internal InputAction_Trigger zoomInFromKeyboardInputAction; |
| | 57 | | [SerializeField] internal InputAction_Trigger zoomOutFromKeyboardInputAction; |
| | 58 | |
|
| 612 | 59 | | internal Vector3 direction = Vector3.zero; |
| | 60 | |
|
| | 61 | | internal float yaw = 0f; |
| | 62 | | internal float pitch = 0f; |
| | 63 | |
|
| | 64 | | internal float panAxisX = 0f; |
| | 65 | | internal float panAxisY = 0f; |
| | 66 | |
|
| 612 | 67 | | internal bool isCameraAbleToMove = true; |
| | 68 | |
|
| | 69 | | internal bool isAdvancingForward = false; |
| | 70 | | internal bool isAdvancingBackward = false; |
| | 71 | | internal bool isAdvancingLeft = false; |
| | 72 | | internal bool isAdvancingRight = false; |
| | 73 | | internal bool isAdvancingUp = false; |
| | 74 | | internal bool isAdvancingDown = false; |
| | 75 | |
|
| | 76 | | internal bool isDetectingMovement = false; |
| | 77 | | internal bool hasBeenMovement = false; |
| | 78 | |
|
| | 79 | | internal bool isPanCameraActive = false; |
| | 80 | | internal bool isMouseRightClickDown = false; |
| | 81 | |
|
| | 82 | | internal Coroutine smoothLookAtCoroutine; |
| | 83 | | internal Coroutine smoothFocusOnTargetCoroutine; |
| | 84 | | internal Coroutine smoothScrollCoroutine; |
| | 85 | | internal Coroutine takeScreenshotCoroutine; |
| | 86 | |
|
| | 87 | | private InputAction_Hold.Started advanceForwardStartDelegate; |
| | 88 | | private InputAction_Hold.Finished advanceForwardFinishedDelegate; |
| | 89 | |
|
| | 90 | | private InputAction_Hold.Started advanceBackStartDelegate; |
| | 91 | | private InputAction_Hold.Finished advanceBackFinishedDelegate; |
| | 92 | |
|
| | 93 | | private InputAction_Hold.Started advanceLeftStartDelegate; |
| | 94 | | private InputAction_Hold.Finished advanceLeftFinishedDelegate; |
| | 95 | |
|
| | 96 | | private InputAction_Hold.Started advanceRightStartDelegate; |
| | 97 | | private InputAction_Hold.Finished advanceRightFinishedDelegate; |
| | 98 | |
|
| | 99 | | private InputAction_Hold.Started advanceDownStartDelegate; |
| | 100 | | private InputAction_Hold.Finished advanceDownFinishedDelegate; |
| | 101 | |
|
| | 102 | | private InputAction_Hold.Started advanceUpStartDelegate; |
| | 103 | | private InputAction_Hold.Finished advanceUpFinishedDelegate; |
| | 104 | |
|
| | 105 | | private InputAction_Hold.Started cameraPanStartDelegate; |
| | 106 | | private InputAction_Hold.Finished cameraPanFinishedDelegate; |
| | 107 | |
|
| | 108 | | private InputAction_Trigger.Triggered zoomInFromKeyboardDelegate; |
| | 109 | | private InputAction_Trigger.Triggered zoomOutFromKeyboardDelegate; |
| | 110 | |
|
| | 111 | | private Vector3 nextTranslation; |
| | 112 | | internal Vector3 originalCameraPosition; |
| | 113 | | internal Vector3 originalCameraPointToLookAt; |
| 612 | 114 | | private Vector3 cameraVelocity = Vector3.zero; |
| | 115 | |
|
| | 116 | | private float lastMouseWheelTime; |
| | 117 | | private float cameraPanAdvance; |
| | 118 | | private float cameraLookAdvance; |
| | 119 | | internal UnityEngine.Camera screenshotCamera; |
| | 120 | |
|
| | 121 | | public bool invertMouseY = false; |
| | 122 | |
|
| | 123 | | private void Awake() |
| | 124 | | { |
| 50 | 125 | | BIWInputWrapper.OnMouseDrag += MouseDrag; |
| 50 | 126 | | BIWInputWrapper.OnMouseDragRaw += MouseDragRaw; |
| 50 | 127 | | BIWInputWrapper.OnMouseWheel += MouseWheel; |
| | 128 | |
|
| 50 | 129 | | BIWInputWrapper.OnMouseDown += OnInputMouseDown; |
| 50 | 130 | | BIWInputWrapper.OnMouseUp += OnInputMouseUp; |
| | 131 | |
|
| 50 | 132 | | advanceForwardStartDelegate = (action) => isAdvancingForward = true; |
| 50 | 133 | | advanceForwardFinishedDelegate = (action) => isAdvancingForward = false; |
| | 134 | |
|
| 50 | 135 | | if (advanceFowardInputAction != null) |
| | 136 | | { |
| 36 | 137 | | advanceFowardInputAction.OnStarted += advanceForwardStartDelegate; |
| 36 | 138 | | advanceFowardInputAction.OnFinished += advanceForwardFinishedDelegate; |
| | 139 | | } |
| | 140 | |
|
| 50 | 141 | | advanceBackStartDelegate = (action) => isAdvancingBackward = true; |
| 50 | 142 | | advanceBackFinishedDelegate = (action) => isAdvancingBackward = false; |
| | 143 | |
|
| 50 | 144 | | if (advanceBackInputAction != null) |
| | 145 | | { |
| 36 | 146 | | advanceBackInputAction.OnStarted += advanceBackStartDelegate; |
| 36 | 147 | | advanceBackInputAction.OnFinished += advanceBackFinishedDelegate; |
| | 148 | | } |
| | 149 | |
|
| 50 | 150 | | advanceLeftStartDelegate = (action) => isAdvancingLeft = true; |
| 50 | 151 | | advanceLeftFinishedDelegate = (action) => isAdvancingLeft = false; |
| | 152 | |
|
| 50 | 153 | | if (advanceLeftInputAction != null) |
| | 154 | | { |
| 36 | 155 | | advanceLeftInputAction.OnStarted += advanceLeftStartDelegate; |
| 36 | 156 | | advanceLeftInputAction.OnFinished += advanceLeftFinishedDelegate; |
| | 157 | | } |
| | 158 | |
|
| 50 | 159 | | advanceRightStartDelegate = (action) => isAdvancingRight = true; |
| 50 | 160 | | advanceRightFinishedDelegate = (action) => isAdvancingRight = false; |
| | 161 | |
|
| 50 | 162 | | if (advanceRightInputAction != null) |
| | 163 | | { |
| 36 | 164 | | advanceRightInputAction.OnStarted += advanceRightStartDelegate; |
| 36 | 165 | | advanceRightInputAction.OnFinished += advanceRightFinishedDelegate; |
| | 166 | | } |
| | 167 | |
|
| 50 | 168 | | advanceUpStartDelegate = (action) => isAdvancingUp = true; |
| 50 | 169 | | advanceUpFinishedDelegate = (action) => isAdvancingUp = false; |
| | 170 | |
|
| 50 | 171 | | if (advanceUpInputAction != null) |
| | 172 | | { |
| 36 | 173 | | advanceUpInputAction.OnStarted += advanceUpStartDelegate; |
| 36 | 174 | | advanceUpInputAction.OnFinished += advanceUpFinishedDelegate; |
| | 175 | | } |
| | 176 | |
|
| 50 | 177 | | advanceDownStartDelegate = (action) => isAdvancingDown = true; |
| 50 | 178 | | advanceDownFinishedDelegate = (action) => isAdvancingDown = false; |
| | 179 | |
|
| 50 | 180 | | if (advanceDownInputAction != null) |
| | 181 | | { |
| 36 | 182 | | advanceDownInputAction.OnStarted += advanceDownStartDelegate; |
| 36 | 183 | | advanceDownInputAction.OnFinished += advanceDownFinishedDelegate; |
| | 184 | | } |
| | 185 | |
|
| 50 | 186 | | cameraPanStartDelegate = (action) => isPanCameraActive = true; |
| 50 | 187 | | cameraPanFinishedDelegate = (action) => isPanCameraActive = false; |
| | 188 | |
|
| 50 | 189 | | if (cameraPanInputAction != null) |
| | 190 | | { |
| 36 | 191 | | cameraPanInputAction.OnStarted += cameraPanStartDelegate; |
| 36 | 192 | | cameraPanInputAction.OnFinished += cameraPanFinishedDelegate; |
| | 193 | | } |
| | 194 | |
|
| 50 | 195 | | zoomInFromKeyboardDelegate = (action) => MouseWheel(1f); |
| | 196 | |
|
| 50 | 197 | | if (zoomInFromKeyboardInputAction != null) |
| 36 | 198 | | zoomInFromKeyboardInputAction.OnTriggered += zoomInFromKeyboardDelegate; |
| | 199 | |
|
| 50 | 200 | | zoomOutFromKeyboardDelegate = (action) => MouseWheel(-1f); |
| | 201 | |
|
| 50 | 202 | | if (zoomOutFromKeyboardInputAction != null) |
| 36 | 203 | | zoomOutFromKeyboardInputAction.OnTriggered += zoomOutFromKeyboardDelegate; |
| | 204 | |
|
| 50 | 205 | | invertMouseY = Settings.i.generalSettings.Data.invertYAxis; |
| | 206 | |
|
| 50 | 207 | | Settings.i.generalSettings.OnChanged += UpdateLocalGeneralSettings; |
| 50 | 208 | | } |
| | 209 | |
|
| | 210 | | private void UpdateLocalGeneralSettings(GeneralSettings obj) |
| | 211 | | { |
| 0 | 212 | | invertMouseY = obj.invertYAxis; |
| 0 | 213 | | } |
| | 214 | |
|
| | 215 | | public void StartDetectingMovement() |
| | 216 | | { |
| 0 | 217 | | isDetectingMovement = true; |
| 0 | 218 | | hasBeenMovement = false; |
| 0 | 219 | | } |
| | 220 | |
|
| 0 | 221 | | public bool HasBeenMovement() { return hasBeenMovement; } |
| | 222 | |
|
| 0 | 223 | | public void StopDetectingMovement() { isDetectingMovement = false; } |
| | 224 | |
|
| | 225 | | internal void OnInputMouseUp(int buttonId, Vector3 mousePosition) |
| | 226 | | { |
| 0 | 227 | | if (buttonId != 1) |
| 0 | 228 | | return; |
| | 229 | |
|
| 0 | 230 | | isMouseRightClickDown = false; |
| 0 | 231 | | } |
| | 232 | |
|
| | 233 | | internal void OnInputMouseDown(int buttonId, Vector3 mousePosition) |
| | 234 | | { |
| 1 | 235 | | if (buttonId != 1) |
| 0 | 236 | | return; |
| | 237 | |
|
| 1 | 238 | | isMouseRightClickDown = true; |
| 1 | 239 | | direction = Vector3.zero; |
| 1 | 240 | | } |
| | 241 | |
|
| | 242 | | private void OnDestroy() |
| | 243 | | { |
| 50 | 244 | | BIWInputWrapper.OnMouseDrag -= MouseDrag; |
| 50 | 245 | | BIWInputWrapper.OnMouseDragRaw -= MouseDragRaw; |
| 50 | 246 | | BIWInputWrapper.OnMouseWheel -= MouseWheel; |
| | 247 | |
|
| 50 | 248 | | BIWInputWrapper.OnMouseDown -= OnInputMouseDown; |
| 50 | 249 | | BIWInputWrapper.OnMouseUp -= OnInputMouseUp; |
| | 250 | |
|
| 50 | 251 | | if (advanceFowardInputAction != null) |
| | 252 | | { |
| 36 | 253 | | advanceFowardInputAction.OnStarted -= advanceForwardStartDelegate; |
| 36 | 254 | | advanceFowardInputAction.OnFinished -= advanceForwardFinishedDelegate; |
| | 255 | | } |
| | 256 | |
|
| 50 | 257 | | if (advanceBackInputAction != null) |
| | 258 | | { |
| 36 | 259 | | advanceBackInputAction.OnStarted -= advanceBackStartDelegate; |
| 36 | 260 | | advanceBackInputAction.OnFinished -= advanceBackFinishedDelegate; |
| | 261 | | } |
| | 262 | |
|
| 50 | 263 | | if (advanceLeftInputAction != null) |
| | 264 | | { |
| 36 | 265 | | advanceLeftInputAction.OnStarted -= advanceLeftStartDelegate; |
| 36 | 266 | | advanceLeftInputAction.OnFinished -= advanceLeftFinishedDelegate; |
| | 267 | | } |
| | 268 | |
|
| 50 | 269 | | if (advanceRightInputAction != null) |
| | 270 | | { |
| 36 | 271 | | advanceRightInputAction.OnStarted -= advanceRightStartDelegate; |
| 36 | 272 | | advanceRightInputAction.OnFinished -= advanceRightFinishedDelegate; |
| | 273 | | } |
| | 274 | |
|
| 50 | 275 | | if (advanceDownInputAction != null) |
| | 276 | | { |
| 36 | 277 | | advanceDownInputAction.OnStarted -= advanceDownStartDelegate; |
| 36 | 278 | | advanceDownInputAction.OnFinished -= advanceDownFinishedDelegate; |
| | 279 | | } |
| | 280 | |
|
| 50 | 281 | | if (advanceUpInputAction != null) |
| | 282 | | { |
| 36 | 283 | | advanceUpInputAction.OnStarted -= advanceUpStartDelegate; |
| 36 | 284 | | advanceUpInputAction.OnFinished -= advanceUpFinishedDelegate; |
| | 285 | | } |
| | 286 | |
|
| 50 | 287 | | if (cameraPanInputAction != null) |
| | 288 | | { |
| 36 | 289 | | cameraPanInputAction.OnStarted -= cameraPanStartDelegate; |
| 36 | 290 | | cameraPanInputAction.OnFinished -= cameraPanFinishedDelegate; |
| | 291 | | } |
| | 292 | |
|
| 50 | 293 | | if (zoomInFromKeyboardInputAction != null) |
| 36 | 294 | | zoomInFromKeyboardInputAction.OnTriggered -= zoomInFromKeyboardDelegate; |
| | 295 | |
|
| 50 | 296 | | if (zoomOutFromKeyboardInputAction != null) |
| 36 | 297 | | zoomOutFromKeyboardInputAction.OnTriggered -= zoomOutFromKeyboardDelegate; |
| | 298 | |
|
| 50 | 299 | | if (smoothScrollCoroutine != null) |
| 1 | 300 | | CoroutineStarter.Stop(smoothScrollCoroutine); |
| | 301 | |
|
| 50 | 302 | | if (smoothLookAtCoroutine != null) |
| 1 | 303 | | CoroutineStarter.Stop(smoothLookAtCoroutine); |
| | 304 | |
|
| 50 | 305 | | if (smoothFocusOnTargetCoroutine != null) |
| 1 | 306 | | CoroutineStarter.Stop(smoothFocusOnTargetCoroutine); |
| | 307 | |
|
| 50 | 308 | | if (takeScreenshotCoroutine != null) |
| 2 | 309 | | CoroutineStarter.Stop(takeScreenshotCoroutine); |
| | 310 | |
|
| 50 | 311 | | Settings.i.generalSettings.OnChanged -= UpdateLocalGeneralSettings; |
| 50 | 312 | | } |
| | 313 | |
|
| | 314 | | private void Update() |
| | 315 | | { |
| 100 | 316 | | HandleCameraLook(); |
| 100 | 317 | | HandleCameraPan(); |
| 100 | 318 | | HandleCameraMovement(); |
| 100 | 319 | | } |
| | 320 | |
|
| | 321 | | #region CameraTransformChanges |
| | 322 | |
|
| | 323 | | private void HandleCameraMovement() |
| | 324 | | { |
| 100 | 325 | | HandleCameraMovementInput(); |
| 100 | 326 | | if (direction.magnitude >= CAMERA_MOVEMENT_DEACTIVATE_INPUT_MAGNITUD) |
| 0 | 327 | | nextTranslation = direction; |
| 100 | 328 | | nextTranslation = Vector3.Lerp(nextTranslation, Vector3.zero, lerpTime * Time.deltaTime); |
| | 329 | |
|
| 100 | 330 | | if (nextTranslation.magnitude >= CAMERA_MOVEMENT_THRESHOLD) |
| 0 | 331 | | transform.Translate(nextTranslation * (movementSpeed * Time.deltaTime), Space.Self); |
| 100 | 332 | | } |
| | 333 | |
|
| | 334 | | private void HandleCameraLook() |
| | 335 | | { |
| 100 | 336 | | Quaternion nextIteration = Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(pitch, yaw, 0f) |
| | 337 | |
|
| 100 | 338 | | if (Mathf.Abs(nextIteration.eulerAngles.magnitude - transform.rotation.eulerAngles.magnitude) >= CAMERA_ANGL |
| 0 | 339 | | transform.rotation = nextIteration; |
| 100 | 340 | | } |
| | 341 | |
|
| | 342 | | private void HandleCameraPan() |
| | 343 | | { |
| 100 | 344 | | panAxisX = Mathf.Lerp(panAxisX, 0, cameraPanAdvance * Time.deltaTime); |
| 100 | 345 | | panAxisY = Mathf.Lerp(panAxisY, 0, cameraPanAdvance * Time.deltaTime); |
| | 346 | |
|
| 100 | 347 | | if (Mathf.Abs(panAxisX) >= CAMERA_PAN_THRESHOLD || Mathf.Abs(panAxisY) >= CAMERA_PAN_THRESHOLD) |
| 0 | 348 | | transform.Translate(panAxisX, panAxisY, 0); |
| 100 | 349 | | } |
| | 350 | |
|
| | 351 | | #endregion |
| | 352 | |
|
| | 353 | | internal void HandleCameraMovementInput() |
| | 354 | | { |
| 102 | 355 | | int velocityChangedCount = 0; |
| 102 | 356 | | if (isAdvancingForward) |
| | 357 | | { |
| 1 | 358 | | cameraVelocity += GetTotalVelocity(Vector3.forward); |
| 1 | 359 | | velocityChangedCount++; |
| | 360 | | } |
| | 361 | |
|
| 102 | 362 | | if (isAdvancingBackward) |
| | 363 | | { |
| 1 | 364 | | cameraVelocity += GetTotalVelocity(Vector3.back); |
| 1 | 365 | | velocityChangedCount++; |
| | 366 | | } |
| | 367 | |
|
| 102 | 368 | | if (!isAdvancingBackward && !isAdvancingForward) |
| 100 | 369 | | cameraVelocity.z = Mathf.Lerp(cameraVelocity.z, 0, lerpDeccelerationTime); |
| | 370 | |
|
| 102 | 371 | | if (isAdvancingRight) |
| | 372 | | { |
| 1 | 373 | | cameraVelocity += GetTotalVelocity(Vector3.right) * xPlaneSpeedPercentCompensantion; |
| 1 | 374 | | velocityChangedCount++; |
| | 375 | | } |
| | 376 | |
|
| 102 | 377 | | if (isAdvancingLeft) |
| | 378 | | { |
| 1 | 379 | | cameraVelocity += GetTotalVelocity(Vector3.left) * xPlaneSpeedPercentCompensantion; |
| 1 | 380 | | velocityChangedCount++; |
| | 381 | | } |
| | 382 | |
|
| 102 | 383 | | if (!isAdvancingRight && !isAdvancingLeft) |
| 100 | 384 | | cameraVelocity.x = Mathf.Lerp(cameraVelocity.x, 0, lerpDeccelerationTime); |
| | 385 | |
|
| 102 | 386 | | if (isAdvancingUp) |
| | 387 | | { |
| 1 | 388 | | cameraVelocity += GetTotalVelocity(Vector3.up); |
| 1 | 389 | | velocityChangedCount++; |
| | 390 | | } |
| | 391 | |
|
| 102 | 392 | | if (isAdvancingDown) |
| | 393 | | { |
| 1 | 394 | | cameraVelocity += GetTotalVelocity(Vector3.down); |
| 1 | 395 | | velocityChangedCount++; |
| | 396 | | } |
| | 397 | |
|
| 102 | 398 | | if (!isAdvancingUp && !isAdvancingDown) |
| 100 | 399 | | cameraVelocity.y = Mathf.Lerp(cameraVelocity.y, 0, lerpDeccelerationTime); |
| | 400 | |
|
| 102 | 401 | | if (velocityChangedCount != 0) |
| 2 | 402 | | cameraVelocity = Vector3.ClampMagnitude(cameraVelocity, speedClamp); |
| | 403 | |
|
| 102 | 404 | | direction = cameraVelocity; |
| 102 | 405 | | } |
| | 406 | |
|
| | 407 | | private Vector3 GetTotalVelocity(Vector3 velocityToAdd) |
| | 408 | | { |
| 6 | 409 | | if (!isMouseRightClickDown) |
| 0 | 410 | | return Vector3.zero; |
| | 411 | |
|
| 6 | 412 | | if (isDetectingMovement) |
| 0 | 413 | | hasBeenMovement = true; |
| 6 | 414 | | return velocityToAdd * (initalAcceleration * Time.deltaTime); |
| | 415 | | } |
| | 416 | |
|
| 0 | 417 | | public void SetCameraCanMove(bool canMove) { isCameraAbleToMove = canMove; } |
| | 418 | |
|
| | 419 | | internal void MouseWheel(float axis) |
| | 420 | | { |
| 1 | 421 | | if (!isCameraAbleToMove) |
| 0 | 422 | | return; |
| | 423 | |
|
| 1 | 424 | | if (smoothScrollCoroutine != null) |
| 0 | 425 | | CoroutineStarter.Stop(smoothScrollCoroutine); |
| | 426 | |
|
| 1 | 427 | | float delta = Time.time - lastMouseWheelTime; |
| 1 | 428 | | float scrollValue = axis * Mathf.Clamp01(delta); |
| 1 | 429 | | lastMouseWheelTime = Time.time; |
| | 430 | |
|
| 1 | 431 | | smoothScrollCoroutine = CoroutineStarter.Start(SmoothScroll(scrollValue)); |
| 1 | 432 | | } |
| | 433 | |
|
| | 434 | | internal void MouseDragRaw(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 435 | | { |
| 1 | 436 | | if (buttonId == 1 && !isPanCameraActive) |
| 1 | 437 | | CameraLook(axisX, axisY); |
| 1 | 438 | | } |
| | 439 | |
|
| | 440 | | internal void MouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 441 | | { |
| 1 | 442 | | if (buttonId == 2 || buttonId == 1 && isPanCameraActive) |
| 1 | 443 | | CameraDrag(axisX, axisY); |
| 1 | 444 | | } |
| | 445 | |
|
| | 446 | | private void CameraDrag(float axisX, float axisY) |
| | 447 | | { |
| 1 | 448 | | if (!isCameraAbleToMove) |
| 0 | 449 | | return; |
| | 450 | |
|
| 1 | 451 | | panAxisX += -axisX * Time.deltaTime * dragSpeed; |
| 1 | 452 | | panAxisY += -axisY * Time.deltaTime * dragSpeed; |
| 1 | 453 | | cameraPanAdvance = smoothCameraPanAceleration; |
| 1 | 454 | | } |
| | 455 | |
|
| | 456 | | private void CameraLook(float axisX, float axisY) |
| | 457 | | { |
| 1 | 458 | | if (!isCameraAbleToMove || !isMouseRightClickDown) |
| 0 | 459 | | return; |
| | 460 | |
|
| 1 | 461 | | yaw += lookSpeedH * axisX; |
| 1 | 462 | | if (invertMouseY) |
| | 463 | | { |
| 0 | 464 | | pitch -= lookSpeedV * -axisY; |
| 0 | 465 | | } |
| | 466 | | else { |
| 1 | 467 | | pitch -= lookSpeedV * axisY; |
| | 468 | | } |
| 1 | 469 | | cameraLookAdvance = smoothCameraLookSpeed * Time.deltaTime; |
| 1 | 470 | | } |
| | 471 | |
|
| 1 | 472 | | public override Vector3 OnGetRotation() { return transform.eulerAngles; } |
| | 473 | |
|
| | 474 | | public void FocusOnEntities(List<BIWEntity> entitiesToFocus) |
| | 475 | | { |
| 1 | 476 | | if (entitiesToFocus.Count <= 0) |
| 0 | 477 | | return; |
| | 478 | |
|
| 1 | 479 | | Vector3 middlePoint = FindMidPoint(entitiesToFocus); |
| 1 | 480 | | if (Vector3.positiveInfinity == middlePoint || |
| | 481 | | Vector3.negativeInfinity == middlePoint || |
| | 482 | | float.IsNaN(middlePoint.x) || |
| | 483 | | float.IsNaN(middlePoint.y) || |
| | 484 | | float.IsNaN(middlePoint.z)) |
| 0 | 485 | | return; |
| | 486 | |
|
| 1 | 487 | | if (smoothFocusOnTargetCoroutine != null) |
| 0 | 488 | | CoroutineStarter.Stop(smoothFocusOnTargetCoroutine); |
| 1 | 489 | | smoothFocusOnTargetCoroutine = CoroutineStarter.Start(SmoothFocusOnTarget(middlePoint)); |
| 1 | 490 | | SmoothLookAt(middlePoint); |
| 1 | 491 | | } |
| | 492 | |
|
| 2 | 493 | | public void SetPosition(Vector3 position) { transform.position = position; } |
| | 494 | |
|
| | 495 | | public void LookAt(Transform transformToLookAt) |
| | 496 | | { |
| 36 | 497 | | LookAt(transformToLookAt.position); |
| 36 | 498 | | } |
| | 499 | |
|
| | 500 | | public void LookAt(Vector3 pointToLookAt) |
| | 501 | | { |
| 37 | 502 | | transform.LookAt(pointToLookAt); |
| 37 | 503 | | yaw = transform.eulerAngles.y; |
| 37 | 504 | | pitch = transform.eulerAngles.x; |
| 37 | 505 | | } |
| | 506 | |
|
| 0 | 507 | | public void SmoothLookAt(Transform transformToLookAt) { SmoothLookAt(transformToLookAt.position); } |
| | 508 | |
|
| | 509 | | public void SmoothLookAt(Vector3 position) |
| | 510 | | { |
| 1 | 511 | | if (smoothLookAtCoroutine != null) |
| 0 | 512 | | CoroutineStarter.Stop(smoothLookAtCoroutine); |
| 1 | 513 | | smoothLookAtCoroutine = CoroutineStarter.Start(SmoothLookAtCorutine(position)); |
| 1 | 514 | | } |
| | 515 | |
|
| | 516 | | Vector3 FindMidPoint(List<BIWEntity> entitiesToLook) |
| | 517 | | { |
| 1 | 518 | | Vector3 finalPosition = Vector3.zero; |
| 1 | 519 | | int totalPoints = 0; |
| 4 | 520 | | foreach (BIWEntity entity in entitiesToLook) |
| | 521 | | { |
| 1 | 522 | | if (entity.rootEntity.meshRootGameObject && entity.rootEntity.meshesInfo.renderers.Length > 0) |
| | 523 | | { |
| 1 | 524 | | Vector3 midPointFromEntity = Vector3.zero; |
| 4 | 525 | | foreach (Renderer render in entity.rootEntity.renderers) |
| | 526 | | { |
| 1 | 527 | | if (render == null) |
| | 528 | | continue; |
| 1 | 529 | | midPointFromEntity += render.bounds.center; |
| | 530 | | } |
| | 531 | |
|
| 1 | 532 | | midPointFromEntity /= entity.rootEntity.renderers.Length; |
| 1 | 533 | | finalPosition += midPointFromEntity; |
| 1 | 534 | | totalPoints++; |
| | 535 | | } |
| | 536 | | } |
| | 537 | |
|
| 1 | 538 | | finalPosition /= totalPoints; |
| 1 | 539 | | return finalPosition; |
| | 540 | | } |
| | 541 | |
|
| | 542 | | IEnumerator SmoothScroll(float axis) |
| | 543 | | { |
| 1 | 544 | | float scrollMovementDestination = axis * zoomSpeed; |
| | 545 | |
|
| 1 | 546 | | Vector3 targetPosition = transform.position + transform.TransformDirection(Vector3.forward * scrollMovementD |
| | 547 | |
|
| 1 | 548 | | float advance = 0; |
| 1 | 549 | | while (advance <= 1) |
| | 550 | | { |
| 1 | 551 | | advance += smoothLookAtSpeed * Time.deltaTime; |
| 1 | 552 | | Vector3 result = Vector3.Lerp(transform.position, targetPosition, advance); |
| 1 | 553 | | transform.position = result; |
| 1 | 554 | | yield return null; |
| | 555 | | } |
| 0 | 556 | | } |
| | 557 | |
|
| | 558 | | IEnumerator SmoothFocusOnTarget(Vector3 targetPosition) |
| | 559 | | { |
| 1 | 560 | | float advance = 0; |
| 17 | 561 | | while (advance <= 1) |
| | 562 | | { |
| 17 | 563 | | advance += smoothLookAtSpeed * Time.deltaTime; |
| 17 | 564 | | transform.position = Vector3.Lerp(transform.position, targetPosition, advance); |
| 17 | 565 | | if (Vector3.Distance(transform.position, targetPosition) <= focusDistance) |
| 1 | 566 | | yield break; |
| 16 | 567 | | yield return null; |
| | 568 | | } |
| 0 | 569 | | } |
| | 570 | |
|
| | 571 | | IEnumerator SmoothLookAtCorutine(Vector3 targetPosition) |
| | 572 | | { |
| 1 | 573 | | Quaternion targetRotation = Quaternion.LookRotation(targetPosition - transform.position); |
| 1 | 574 | | float advance = 0; |
| 24 | 575 | | while (advance <= 1) |
| | 576 | | { |
| 24 | 577 | | advance += smoothLookAtSpeed * Time.deltaTime; |
| 24 | 578 | | transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, advance); |
| 24 | 579 | | yield return null; |
| | 580 | | } |
| | 581 | |
|
| 0 | 582 | | yaw = transform.eulerAngles.y; |
| 0 | 583 | | pitch = transform.eulerAngles.x; |
| 0 | 584 | | } |
| | 585 | |
|
| | 586 | | public void SetResetConfiguration(Vector3 position, Transform lookAt) |
| | 587 | | { |
| 38 | 588 | | SetResetConfiguration(position, lookAt.position); |
| 38 | 589 | | } |
| | 590 | |
|
| | 591 | | public void SetResetConfiguration(Vector3 position, Vector3 pointToLook) |
| | 592 | | { |
| 0 | 593 | | originalCameraPosition = position; |
| 0 | 594 | | originalCameraPointToLookAt = pointToLook; |
| 0 | 595 | | } |
| | 596 | |
|
| | 597 | | public void ResetCameraPosition() |
| | 598 | | { |
| 1 | 599 | | SetPosition(originalCameraPosition); |
| 1 | 600 | | LookAt(originalCameraPointToLookAt); |
| 1 | 601 | | direction = Vector3.zero; |
| 1 | 602 | | } |
| | 603 | |
|
| 4 | 604 | | public void TakeSceneScreenshot(IFreeCameraMovement.OnSnapshotsReady onSuccess) { takeScreenshotCoroutine = Coro |
| | 605 | |
|
| | 606 | | private IEnumerator TakeSceneScreenshotCoroutine(IFreeCameraMovement.OnSnapshotsReady callback) |
| | 607 | | { |
| 2 | 608 | | UnityEngine.Camera camera = UnityEngine.Camera.main; |
| 2 | 609 | | if (screenshotCamera != null) |
| 2 | 610 | | camera = screenshotCamera; |
| | 611 | |
|
| 2 | 612 | | var current = camera.targetTexture; |
| 2 | 613 | | camera.targetTexture = null; |
| | 614 | |
|
| 2 | 615 | | yield return null; |
| | 616 | |
|
| 2 | 617 | | Texture2D sceneScreenshot = ScreenshotFromCamera(camera, SCENE_SNAPSHOT_WIDTH_RES, SCENE_SNAPSHOT_HEIGHT_RES |
| 2 | 618 | | camera.targetTexture = current; |
| 2 | 619 | | callback?.Invoke(sceneScreenshot); |
| 2 | 620 | | } |
| | 621 | |
|
| 0 | 622 | | public void TakeSceneScreenshotFromResetPosition(IFreeCameraMovement.OnSnapshotsReady onSuccess) { StartCoroutin |
| | 623 | |
|
| | 624 | | private IEnumerator TakeSceneScreenshotFromResetPositionCoroutine(IFreeCameraMovement.OnSnapshotsReady callback) |
| | 625 | | { |
| | 626 | | // Store current camera position/direction |
| 0 | 627 | | Vector3 currentPos = transform.position; |
| 0 | 628 | | Vector3 currentLookAt = transform.forward; |
| 0 | 629 | | SetPosition(originalCameraPosition); |
| 0 | 630 | | transform.LookAt(originalCameraPointToLookAt); |
| | 631 | |
|
| 0 | 632 | | var current = camera.targetTexture; |
| 0 | 633 | | camera.targetTexture = null; |
| | 634 | |
|
| 0 | 635 | | yield return null; |
| | 636 | |
|
| 0 | 637 | | Texture2D sceneScreenshot = ScreenshotFromCamera(camera, SCENE_SNAPSHOT_WIDTH_RES, SCENE_SNAPSHOT_HEIGHT_RES |
| 0 | 638 | | camera.targetTexture = current; |
| 0 | 639 | | callback?.Invoke(sceneScreenshot); |
| | 640 | |
|
| | 641 | | // Restore camera position/direction after the screenshot |
| 0 | 642 | | SetPosition(currentPos); |
| 0 | 643 | | transform.forward = currentLookAt; |
| 0 | 644 | | } |
| | 645 | |
|
| | 646 | | private Texture2D ScreenshotFromCamera(UnityEngine.Camera cameraToScreenshot, int width, int height) |
| | 647 | | { |
| 2 | 648 | | RenderTexture rt = new RenderTexture(width, height, 32); |
| 2 | 649 | | cameraToScreenshot.targetTexture = rt; |
| 2 | 650 | | Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false); |
| 2 | 651 | | cameraToScreenshot.Render(); |
| 2 | 652 | | RenderTexture.active = rt; |
| 2 | 653 | | screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); |
| 2 | 654 | | screenShot.Apply(); |
| | 655 | |
|
| 2 | 656 | | return screenShot; |
| | 657 | | } |
| | 658 | | } |
| | 659 | | } |