From 08fc6b4b02f881f5a00e177f805ac34f40bc02fb Mon Sep 17 00:00:00 2001 From: shenjianxing <”315615051@qq.com“> Date: Tue, 25 Mar 2025 15:22:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E6=8C=87=E8=A7=A6?= =?UTF-8?q?=E6=8E=A7=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Item/Show3DCamera.cs | 179 +++++++++++++++++++++++----- 1 file changed, 148 insertions(+), 31 deletions(-) diff --git a/Assets/Scripts/Item/Show3DCamera.cs b/Assets/Scripts/Item/Show3DCamera.cs index b2a5cafd..9239937b 100644 --- a/Assets/Scripts/Item/Show3DCamera.cs +++ b/Assets/Scripts/Item/Show3DCamera.cs @@ -43,6 +43,7 @@ public class Show3DCamera : MonoBehaviour Transform target; Vector3 targetPosition; Vector3 targetRotate; + private float prevTouchDistance; // 洢һ֡˫ָ private void Awake() { instance = this; @@ -124,27 +125,42 @@ public class Show3DCamera : MonoBehaviour UpdateCameraPosition(-1); } + void Update() { if (targetPos != null && lockMove == false && EventSystem.current.IsPointerOverGameObject() == false) { + // ȴָ + if (HandleThreeFingerDrag()) return; + bool isTouching = Input.touchCount > 0; - - if (Input.GetMouseButtonDown(0)) + // תǰȴ˫ָ + bool isZooming = false; + if (isTouching && Input.touchCount == 2) + { + HandleTouchZoom(); + isZooming = true; // + } + + // ʼų˫ָ + if (isTouching && Input.touchCount == 1) + { + if (Input.GetTouch(0).phase == TouchPhase.Began) + { + mouseDownPosition = Input.GetTouch(0).position; + } + } + // 갴 + else if (Input.GetMouseButtonDown(0)) { - // ¼갴ʱλ mouseDownPosition = Input.mousePosition; } - // סʱת - if (Input.GetMouseButton(0)) + // ޸ĺתų״̬ + if (!isZooming && (isTouching ? (Input.GetTouch(0).phase == TouchPhase.Moved) : Input.GetMouseButton(0))) { - // 갴º̧λ֮ľ - float distance = Vector2.Distance(mouseDownPosition, Input.mousePosition); - if (distance > 1f) - { - RotateCamera(); - } + // ƳжֱӦ + RotateCamera(); } // @@ -154,7 +170,6 @@ public class Show3DCamera : MonoBehaviour ZoomCamera(scroll); } - // סҼʱƶĿ if (Input.GetMouseButton(1)) { @@ -163,6 +178,91 @@ public class Show3DCamera : MonoBehaviour DetectHoveredObject(); } } + // Ψһָbool + private bool HandleThreeFingerDrag() + { + if (Input.touchCount == 5) + { + // ƽƶ + Vector2 totalDelta = Vector2.zero; + foreach (Touch t in Input.touches) + { + totalDelta += t.deltaPosition; + } + Vector2 delta = totalDelta / 3f; + + // DPIӦ + float dpiScale = Screen.dpi == 0 ? 1 : Screen.dpi / 160f; + float sensitivity = moveSpeed * 0.1f / dpiScale; + + // Ӧƶֵ2أ + if (delta.magnitude > 2f) + { + // ޸ĺƶ + MoveTarget(delta.x * sensitivity, delta.y * sensitivity); + } + + // + return true; + } + return false; + } + + + // ˫ָŴ + // + private float zoomSmoothVelocity; // ƽٶȻ + [SerializeField] private float zoomSmoothTime = 0.1f; // ƽʱ + private void HandleTouchZoom() + { + if (Input.touchCount == 2) + { + Touch touch0 = Input.GetTouch(0); + Touch touch1 = Input.GetTouch(1); + // ˫ָʱתر + if (touch0.phase == TouchPhase.Began || touch1.phase == TouchPhase.Began) + { + yaw = transform.eulerAngles.y; // ֵǰתǶ + pitch = transform.eulerAngles.x; + mouseDownPosition = Vector2.zero; + } + + Vector2 touch0Pos = touch0.position; + Vector2 touch1Pos = touch1.position; + float currentDistance = Vector2.Distance(touch0Pos, touch1Pos); + + // DPIӦ + float dpi = Screen.dpi == 0 ? 200 : Screen.dpi; + float zoomFactor = 0.01f * (200 / dpi); // ׼DPIΪ200 + + if (touch0.phase == TouchPhase.Began || touch1.phase == TouchPhase.Began) + { + prevTouchDistance = currentDistance; + zoomSmoothVelocity = 0; // ƽٶ + } + else if (touch0.phase == TouchPhase.Moved || touch1.phase == TouchPhase.Moved) + { + float deltaDistance = currentDistance - prevTouchDistance; + + // ʹƽ + float targetDistance = distance - deltaDistance * zoomFactor; + targetDistance = Mathf.Clamp(targetDistance, distanceMin, distanceMax); + + distance = Mathf.SmoothDamp( + distance, + targetDistance, + ref zoomSmoothVelocity, + zoomSmoothTime + ); + + offset = new Vector3(0, 0, -distance); + UpdateCameraPosition(); + + prevTouchDistance = currentDistance; + } + } + } + // ͣ public void DetectHoveredObject() @@ -252,11 +352,15 @@ public class Show3DCamera : MonoBehaviour } - // ƶĿ - private void MoveTarget() + // ޸ĺƶͳһԴ + private void MoveTarget(float mouseX = 0, float mouseY = 0) { - float mouseX = Input.GetAxis("Mouse X") * moveSpeed; - float mouseY = Input.GetAxis("Mouse Y") * moveSpeed; + // ԶжԴ + if (Mathf.Approximately(mouseX, 0) && Mathf.Approximately(mouseY, 0)) + { + mouseX = Input.GetAxis("Mouse X") * moveSpeed; + mouseY = Input.GetAxis("Mouse Y") * moveSpeed; + } transform.Translate(new Vector3(-mouseX, -mouseY, 0)); targetPos += new Vector3(-mouseX, -mouseY, 0); @@ -270,31 +374,44 @@ public class Show3DCamera : MonoBehaviour UpdateCameraPosition(); } - // ת + // ޸RotateCamera private void RotateCamera() { - // ȡƶ - float mouseX = Input.GetAxis("Mouse X") * rotateSpeed; - float mouseY = Input.GetAxis("Mouse Y") * rotateSpeed; + // ʹô + float deltaX = 0, deltaY = 0; + + // + if (Input.touchCount == 1) + { + Touch touch = Input.GetTouch(0); + deltaX = touch.deltaPosition.x; + deltaY = touch.deltaPosition.y; + } + // 괦 + else + { + deltaX = Input.GetAxis("Mouse X") * 10; // ԭ + deltaY = Input.GetAxis("Mouse Y") * 10; + } + + // ӦDPIţؼ + float dpiScale = Screen.dpi == 0 ? 1 : Screen.dpi / 160f; + deltaX *= rotateSpeed * Time.deltaTime / dpiScale; + deltaY *= rotateSpeed * Time.deltaTime / dpiScale; + switch (type) { case RotationType.Orbit: - - // ƫǺ͸ - yaw += mouseX; - pitch -= mouseY; // ע⣺ Y ƶ븩෴ - pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y); // ƸǷΧ - - // λ + yaw += deltaX; + pitch -= deltaY; + pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y); UpdateCameraPosition(); break; case RotationType.Spherical: - // ȫɵתX/Y/Z - target.Rotate(Vector3.up, mouseX, Space.World); // ˮƽYת - target.Rotate(Vector3.right, -mouseY, Space.Self); // ֱ򱾵Xת + target.Rotate(Vector3.up, deltaX, Space.World); + target.Rotate(Vector3.right, -deltaY, Space.Self); break; } - } public void ChangeMode(RotationType type)