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