| | 1 | | using UnityEngine; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Models; |
| | 4 | | using Builder.Gizmos; |
| | 5 | |
|
| | 6 | | namespace Builder |
| | 7 | | { |
| | 8 | | public class DCLBuilderCamera : MonoBehaviour |
| | 9 | | { |
| | 10 | | public static System.Action<Camera, float> OnCameraZoomChanged; |
| | 11 | |
|
| | 12 | | [Header("References")] |
| | 13 | | public Transform pitchPivot; |
| | 14 | |
|
| | 15 | | public Transform yawPivot; |
| | 16 | | public Transform rootPivot; |
| | 17 | | public Camera builderCamera; |
| | 18 | |
|
| | 19 | | [Space()] |
| | 20 | | [Header("Input Settings")] |
| 1 | 21 | | public float rotationSpeed = 5f; |
| | 22 | |
|
| | 23 | | [Header("Pitch")] |
| 1 | 24 | | public float pitchAmount = 10f; |
| | 25 | |
|
| | 26 | | public float pitchAngleMin = 0f; |
| 1 | 27 | | public float pitchAngleMax = 89f; |
| | 28 | |
|
| | 29 | | [Header("Yaw")] |
| 1 | 30 | | public float yawAmount = 10f; |
| | 31 | |
|
| | 32 | | [Header("Pan")] |
| 1 | 33 | | public float panSpeed = 5f; |
| | 34 | |
|
| 1 | 35 | | public float panAmount = 0.2f; |
| | 36 | |
|
| | 37 | | [Header("Zoom")] |
| 1 | 38 | | public float zoomSpeed = 15f; |
| | 39 | |
|
| 1 | 40 | | public float zoomAmount = 5f; |
| | 41 | |
|
| | 42 | | private float pitchCurrent = 0; |
| | 43 | | private float pitchTarget = 0; |
| | 44 | | private float yawCurrent = 0; |
| | 45 | | private float yawTarget = 0; |
| 1 | 46 | | private Vector3 panCurrent = Vector3.zero; |
| 1 | 47 | | private Vector3 panTarget = Vector3.zero; |
| | 48 | | private float zoomCurrent = 0; |
| | 49 | | private float zoomTarget = 0; |
| | 50 | |
|
| 1 | 51 | | private float zoomDefault = DCLBuilderConfig.config.camera.zoomDefault; |
| 1 | 52 | | private float zoomMin = DCLBuilderConfig.config.camera.zoomMin; |
| 1 | 53 | | private float zoomMax = DCLBuilderConfig.config.camera.zoomMax; |
| | 54 | |
|
| | 55 | | private bool isObjectBeingDrag = false; |
| | 56 | |
|
| | 57 | | private Vector2 sceneBoundaryMin; |
| | 58 | | private Vector2 sceneBoundaryMax; |
| | 59 | |
|
| | 60 | | private bool isGameObjectActive = false; |
| | 61 | |
|
| | 62 | | private void Awake() |
| | 63 | | { |
| 1 | 64 | | zoomCurrent = zoomTarget = -zoomDefault; |
| 1 | 65 | | pitchCurrent = pitchTarget = pitchPivot.localEulerAngles.x; |
| 1 | 66 | | yawCurrent = yawTarget = yawPivot.localEulerAngles.y; |
| | 67 | |
|
| 1 | 68 | | DCLBuilderBridge.OnPreviewModeChanged += OnPreviewModeChanged; |
| 1 | 69 | | } |
| | 70 | |
|
| 2 | 71 | | private void OnDestroy() { DCLBuilderBridge.OnPreviewModeChanged -= OnPreviewModeChanged; } |
| | 72 | |
|
| | 73 | | private void Update() |
| | 74 | | { |
| 11 | 75 | | yawCurrent += (yawTarget - yawCurrent) * Time.deltaTime * rotationSpeed; |
| 11 | 76 | | if (Mathf.Abs(yawTarget - yawCurrent) < 0.04f) |
| | 77 | | { |
| 11 | 78 | | yawCurrent = yawTarget; |
| | 79 | | } |
| | 80 | |
|
| 11 | 81 | | yawPivot.localRotation = Quaternion.Euler(0, yawCurrent, 0); |
| | 82 | |
|
| 11 | 83 | | pitchCurrent += (pitchTarget - pitchCurrent) * Time.deltaTime * rotationSpeed; |
| 11 | 84 | | if (Mathf.Abs(pitchTarget - pitchCurrent) < 0.04f) |
| | 85 | | { |
| 11 | 86 | | pitchCurrent = pitchTarget; |
| | 87 | | } |
| | 88 | |
|
| 11 | 89 | | pitchPivot.localRotation = Quaternion.Euler(pitchCurrent, 0, 0); |
| | 90 | |
|
| 11 | 91 | | float zoomPrev = zoomCurrent; |
| 11 | 92 | | zoomCurrent += (zoomTarget - zoomCurrent) * Time.deltaTime * zoomSpeed; |
| 11 | 93 | | builderCamera.transform.localPosition = new Vector3(0, 0, zoomCurrent); |
| | 94 | |
|
| 11 | 95 | | Vector3 panOffset = panTarget - panCurrent; |
| 11 | 96 | | float sqDist = panOffset.magnitude; |
| 11 | 97 | | if (sqDist >= 0.01f) |
| | 98 | | { |
| 0 | 99 | | panCurrent = panCurrent + panOffset.normalized * sqDist * panSpeed * Time.deltaTime; |
| 0 | 100 | | rootPivot.localPosition = panCurrent; |
| | 101 | | } |
| | 102 | |
|
| 11 | 103 | | if (zoomPrev != zoomCurrent) |
| | 104 | | { |
| 0 | 105 | | OnCameraZoomChanged?.Invoke(builderCamera, zoomCurrent); |
| | 106 | | } |
| 11 | 107 | | } |
| | 108 | |
|
| | 109 | | private void OnEnable() |
| | 110 | | { |
| 1 | 111 | | if (!isGameObjectActive) |
| | 112 | | { |
| 1 | 113 | | DCLBuilderInput.OnMouseDrag += OnMouseDrag; |
| 1 | 114 | | DCLBuilderInput.OnMouseWheel += OnMouseWheel; |
| 1 | 115 | | DCLBuilderBridge.OnSetKeyDown += OnKeyboardButtonHold; |
| 1 | 116 | | DCLBuilderBridge.OnZoomFromUI += OnZoomFormUI; |
| 1 | 117 | | DCLBuilderBridge.OnSetCameraPosition += OnSetCameraPosition; |
| 1 | 118 | | DCLBuilderBridge.OnSetCameraRotation += OnSetCameraRotation; |
| 1 | 119 | | DCLBuilderBridge.OnResetCameraZoom += OnResetCameraZoom; |
| 1 | 120 | | DCLBuilderObjectDragger.OnDraggingObjectStart += OnDragObjectStart; |
| 1 | 121 | | DCLBuilderObjectDragger.OnDraggingObjectEnd += OnDragObjectEnd; |
| 1 | 122 | | DCLBuilderGizmoManager.OnGizmoTransformObjectStart += OnGizmoTransformObjectStart; |
| 1 | 123 | | DCLBuilderGizmoManager.OnGizmoTransformObjectEnd += OnGizmoTransformObjectEnd; |
| 1 | 124 | | DCLBuilderConfig.OnConfigChanged += OnConfigChanged; |
| | 125 | | } |
| | 126 | |
|
| 1 | 127 | | isGameObjectActive = true; |
| 1 | 128 | | } |
| | 129 | |
|
| | 130 | | private void OnDisable() |
| | 131 | | { |
| 1 | 132 | | isGameObjectActive = false; |
| 1 | 133 | | DCLBuilderInput.OnMouseDrag -= OnMouseDrag; |
| 1 | 134 | | DCLBuilderInput.OnMouseWheel -= OnMouseWheel; |
| 1 | 135 | | DCLBuilderBridge.OnSetKeyDown -= OnKeyboardButtonHold; |
| 1 | 136 | | DCLBuilderBridge.OnZoomFromUI -= OnZoomFormUI; |
| 1 | 137 | | DCLBuilderBridge.OnSetCameraPosition -= OnSetCameraPosition; |
| 1 | 138 | | DCLBuilderBridge.OnSetCameraRotation -= OnSetCameraRotation; |
| 1 | 139 | | DCLBuilderBridge.OnResetCameraZoom -= OnResetCameraZoom; |
| 1 | 140 | | DCLBuilderObjectDragger.OnDraggingObjectStart -= OnDragObjectStart; |
| 1 | 141 | | DCLBuilderObjectDragger.OnDraggingObjectEnd -= OnDragObjectEnd; |
| 1 | 142 | | DCLBuilderGizmoManager.OnGizmoTransformObjectStart -= OnGizmoTransformObjectStart; |
| 1 | 143 | | DCLBuilderGizmoManager.OnGizmoTransformObjectEnd -= OnGizmoTransformObjectEnd; |
| 1 | 144 | | DCLBuilderConfig.OnConfigChanged -= OnConfigChanged; |
| 1 | 145 | | } |
| | 146 | |
|
| | 147 | | private void OnMouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 148 | | { |
| 0 | 149 | | if (buttonId == 0) |
| | 150 | | { |
| 0 | 151 | | if (CanOrbit()) |
| | 152 | | { |
| 0 | 153 | | yawTarget += axisX * yawAmount; |
| 0 | 154 | | pitchTarget = Mathf.Clamp(pitchTarget - axisY * pitchAmount, pitchAngleMin, pitchAngleMax); |
| | 155 | | } |
| 0 | 156 | | } |
| 0 | 157 | | else if (buttonId == 1) |
| | 158 | | { |
| 0 | 159 | | Pan(0, -axisX * panAmount, -axisY * panAmount); |
| | 160 | | } |
| 0 | 161 | | } |
| | 162 | |
|
| 0 | 163 | | private void OnMouseWheel(float axisValue) { Zoom(Mathf.Sign(axisValue) * zoomAmount); } |
| | 164 | |
|
| | 165 | | private void OnKeyboardButtonHold(KeyCode keyCode) |
| | 166 | | { |
| | 167 | | switch (keyCode) |
| | 168 | | { |
| | 169 | | case KeyCode.UpArrow: |
| 0 | 170 | | Pan(panAmount, 0, 0); |
| 0 | 171 | | break; |
| | 172 | | case KeyCode.DownArrow: |
| 0 | 173 | | Pan(-panAmount, 0, 0); |
| 0 | 174 | | break; |
| | 175 | | case KeyCode.LeftArrow: |
| 0 | 176 | | Pan(0, -panAmount, 0); |
| 0 | 177 | | break; |
| | 178 | | case KeyCode.RightArrow: |
| 0 | 179 | | Pan(0, panAmount, 0); |
| | 180 | | break; |
| | 181 | | } |
| 0 | 182 | | } |
| | 183 | |
|
| 0 | 184 | | private void OnZoomFormUI(float delta) { Zoom(-delta); } |
| | 185 | |
|
| | 186 | | private void Pan(float forward, float right, float up) |
| | 187 | | { |
| 0 | 188 | | Vector3 panOffset = ((yawPivot.right * right) + (pitchPivot.up * up) + (rootPivot.forward * forward)); |
| 0 | 189 | | panTarget += panOffset; |
| 0 | 190 | | } |
| | 191 | |
|
| 0 | 192 | | private void Zoom(float amount) { zoomTarget = Mathf.Clamp(zoomTarget + amount, -zoomMax, -zoomMin); } |
| | 193 | |
|
| 0 | 194 | | private void OnDragObjectStart() { isObjectBeingDrag = true; } |
| | 195 | |
|
| | 196 | | private void OnDragObjectEnd() |
| | 197 | | { |
| 0 | 198 | | isObjectBeingDrag = false; |
| 0 | 199 | | pitchTarget = pitchCurrent; |
| 0 | 200 | | yawTarget = yawCurrent; |
| 0 | 201 | | } |
| | 202 | |
|
| 0 | 203 | | private void OnGizmoTransformObjectStart(string gizmoType) { OnDragObjectStart(); } |
| | 204 | |
|
| 0 | 205 | | private void OnGizmoTransformObjectEnd(string gizmoType) { OnDragObjectEnd(); } |
| | 206 | |
|
| | 207 | | private void OnSetCameraPosition(Vector3 position) |
| | 208 | | { |
| 0 | 209 | | panCurrent = position; |
| 0 | 210 | | rootPivot.transform.localPosition = panCurrent; |
| 0 | 211 | | panTarget = panCurrent; |
| 0 | 212 | | pitchCurrent += (pitchTarget - pitchCurrent) * Time.deltaTime * rotationSpeed; |
| 0 | 213 | | pitchPivot.localRotation = Quaternion.Euler(pitchCurrent, 0, 0); |
| 0 | 214 | | } |
| | 215 | |
|
| | 216 | | private void OnSetCameraRotation(float yaw, float pitch) |
| | 217 | | { |
| 0 | 218 | | yawCurrent = yawTarget = yaw; |
| 0 | 219 | | pitchCurrent = pitchTarget = pitch; |
| 0 | 220 | | yawPivot.localRotation = Quaternion.Euler(0, yawCurrent, 0); |
| 0 | 221 | | pitchPivot.localRotation = Quaternion.Euler(pitchCurrent, 0, 0); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | private void OnResetCameraZoom() |
| | 225 | | { |
| 0 | 226 | | zoomCurrent = zoomTarget = -zoomDefault; |
| 0 | 227 | | builderCamera.transform.position.Set(0, 0, zoomCurrent); |
| 0 | 228 | | OnCameraZoomChanged?.Invoke(builderCamera, zoomCurrent); |
| 0 | 229 | | } |
| | 230 | |
|
| 0 | 231 | | private bool CanOrbit() { return !isObjectBeingDrag; } |
| | 232 | |
|
| | 233 | | private void CalcSceneBoundaries(IParcelScene scene) |
| | 234 | | { |
| 0 | 235 | | sceneBoundaryMin = Vector2.zero; |
| 0 | 236 | | sceneBoundaryMax = Vector2.zero; |
| | 237 | |
|
| 0 | 238 | | if (scene != null && scene.sceneData != null) |
| | 239 | | { |
| 0 | 240 | | CalcSceneBoundaries(scene.sceneData); |
| | 241 | | } |
| 0 | 242 | | } |
| | 243 | |
|
| | 244 | | private void CalcSceneBoundaries(LoadParcelScenesMessage.UnityParcelScene scene) |
| | 245 | | { |
| 0 | 246 | | sceneBoundaryMin = Vector2.zero; |
| 0 | 247 | | sceneBoundaryMax = Vector2.zero; |
| | 248 | |
|
| 0 | 249 | | if (scene.parcels.Length > 0) |
| | 250 | | { |
| 0 | 251 | | foreach (Vector2Int parcel in scene.parcels) |
| | 252 | | { |
| 0 | 253 | | Vector3 parcelPosition = new Vector3(parcel.x, 0, parcel.y); |
| 0 | 254 | | sceneBoundaryMin.x = Mathf.Min(sceneBoundaryMin.x, parcelPosition.x); |
| 0 | 255 | | sceneBoundaryMin.y = Mathf.Min(sceneBoundaryMin.y, parcelPosition.z); |
| 0 | 256 | | sceneBoundaryMax.x = Mathf.Max(sceneBoundaryMax.x, parcelPosition.x); |
| 0 | 257 | | sceneBoundaryMax.y = Mathf.Max(sceneBoundaryMax.y, parcelPosition.z); |
| | 258 | | } |
| | 259 | | } |
| 0 | 260 | | } |
| | 261 | |
|
| 0 | 262 | | private void OnPreviewModeChanged(bool isPreview) { gameObject.SetActive(!isPreview); } |
| | 263 | |
|
| | 264 | | private void OnConfigChanged(BuilderConfig config) |
| | 265 | | { |
| 0 | 266 | | zoomMin = config.camera.zoomMin; |
| 0 | 267 | | zoomMax = config.camera.zoomMax; |
| 0 | 268 | | zoomDefault = config.camera.zoomDefault; |
| 0 | 269 | | zoomCurrent = zoomTarget = -zoomDefault; |
| 0 | 270 | | } |
| | 271 | | } |
| | 272 | | } |