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